From d17f2caa04598fb127822a63d359f27a77b2d635 Mon Sep 17 00:00:00 2001 From: m5agent <329351612+m5agent@users.noreply.github.com> Date: Wed, 2 Dec 2026 00:08:14 +0800 Subject: [PATCH] Dispatch native semantic button edges through board mappings --- docs/device-service-api.md | 9 +-- docs/device-service.openapi.yaml | 4 ++ docs/project-snapshot.md | 8 +-- .../tests/test_device_service_esp32sim.py | 70 ++++++++++--------- platform/tools/device_service.py | 19 +++-- platform/tools/device_service_input.py | 4 +- .../api_sweep/inventory_api.source.json | 2 +- 7 files changed, 69 insertions(+), 47 deletions(-) diff --git a/docs/device-service-api.md b/docs/device-service-api.md index 07718a075..c03896dcb 100644 --- a/docs/device-service-api.md +++ b/docs/device-service-api.md @@ -4601,10 +4601,11 @@ and returns `timing.completion:"queued"`. Cardputer and to `button` elsewhere. Chords use physical key names. Core2 touch buttons share a single contact and cannot form a simultaneous chord. These host-timed controls are separate from the legacy virtual - key queue. Avoid overlapping queued and direct input on the same key. StopWatch - on esp32sim dispatches physical A/B chords and double clicks through its - existing GPIO mappings using host-timed edges. The chord presses both - controls before waiting and releases them in reverse order. The timing + key queue. Avoid overlapping queued and direct input on the same key. +- esp32sim uses the board's press and release mappings for button chords and + double clicks. The chord presses all controls before waiting and releases + them in reverse order. A board with missing or shared button edges returns + HTTP 409 `unsupported_input` before any edges are sent. The host timing receipt describes dispatch. Check guest evidence for the application action. - Cardputer and Cardputer-ADV on esp32sim accept the same keyboard chord payload. A key without an esp32sim mapping, such as `alt_r` on the original diff --git a/docs/device-service.openapi.yaml b/docs/device-service.openapi.yaml index 85fe4116a..f32a2098d 100644 --- a/docs/device-service.openapi.yaml +++ b/docs/device-service.openapi.yaml @@ -7524,6 +7524,7 @@ components: not: required: [hold_seconds] - type: object + description: esp32sim double clicks require a mapped press and release for the selected control. required: [kind, control] additionalProperties: false properties: @@ -7559,6 +7560,9 @@ components: description: >- Hold two to four distinct physical controls. Cardputer and Cardputer-ADV keyboard chords are supported by QEMU and esp32sim. + esp32sim button chords require independent press and release + mappings for all selected controls. Missing or shared edges + return HTTP 409 unsupported_input before any input is sent. Send physical keys for Fn combinations, for example [fn, comma]. required: [kind, keys] additionalProperties: false diff --git a/docs/project-snapshot.md b/docs/project-snapshot.md index 5dc88c1a6..019968160 100644 --- a/docs/project-snapshot.md +++ b/docs/project-snapshot.md @@ -155,11 +155,11 @@ ## 代码与文档规模 -- `platform/tools`:`105` 个 Python 模块,`63237` 行;规模最大的模块如下。 +- `platform/tools`:`105` 个 Python 模块,`63248` 行;规模最大的模块如下。 | 模块 | 行数 | | --- | ---: | -| `device_service.py` | 16555 | +| `device_service.py` | 16564 | | `firmware_manifest.py` | 3640 | | `user_firmware_matrix.py` | 2347 | | `network_sandbox.py` | 2171 | @@ -168,11 +168,11 @@ - `tests/`:`184` 个可执行 Python 测试文件,分布在 `85` 个目录;`253` 个 evidence JSON。 - `patches/`:`17` 个补丁组,`386` 个 patch 文件。 -- 文档:`762` 个 Markdown 页面;除本生成页外共 `84337` 行。 +- 文档:`762` 个 Markdown 页面;除本生成页外共 `84338` 行。 ## 文档与测试 -- 平台测试:`2616` 项。 +- 平台测试:`2617` 项。 - Docs 新鲜度登记:`290` 行;其中非历史页 `269` 行,历史固定页 `21` 行。 - 文档职责分布:待办 `3`,契约 `9`,证据/差异 `135`,历史固定 `21`,实现/设计 `15`,参考 `101`,工作流/导航 `6`。 - 全量文档盘点:`762` 个 Markdown 页面;职责分布:权威/专题页 `290`,设备档案 `41`,示例 `1`,治理/入口 `9`,文档导航 `1`,其他 `10`,补丁说明 `22`,平台说明 `26`,仓内历史记录 `5`,测试/用例说明 `249`,未登记 docs 页面 `4`,第三方来源说明 `104`。 diff --git a/platform/tests/test_device_service_esp32sim.py b/platform/tests/test_device_service_esp32sim.py index 5c3e73d9a..0799f39a1 100644 --- a/platform/tests/test_device_service_esp32sim.py +++ b/platform/tests/test_device_service_esp32sim.py @@ -213,37 +213,43 @@ def test_cardputer_adv_fixed_softap_uses_only_the_private_fixture_socket(self): with self.assertRaises(ValueError): runtime.spawn_command(installation, board, 12, Path("/session/flash"), network=network) - def test_stopwatch_semantic_inputs_drive_the_mapped_edges(self): - session = Esp32SimDeviceSession.__new__(Esp32SimDeviceSession) - session.spec = load_device_specs()["stopwatch"] - session.board = runtime.BOARDS["stopwatch"] - session.runtime = "esp32sim" - session.state = "running" - session.control = Mock() - session._lock = threading.Lock() - session._stop_event = Mock() - session._check_cancelled = Mock() - session._require_runtime_permission = Mock() - session._record_event = Mock() - session.snapshot = Mock(return_value={}) + def test_semantic_button_inputs_drive_the_mapped_edges(self): cases = [ ({"kind": "button", "control": "a", "action": "double_click"}, [("a", 0), ("a", 1)] * 2, [0.12, 0.12, 0.12, 0], 2), - ({"kind": "chord", "target": "button", "keys": ["a", "b"], "seconds": 1}, - [("a", 0), ("b", 0), ("b", 1), ("a", 1)], [1, 0], 1), + ({"kind": "button", "control": "b", "action": "double_click"}, + [("b", 0), ("b", 1)] * 2, [0.12, 0.12, 0.12, 0], 2), + ({"kind": "chord", "target": "button", "keys": ["a", "b"]}, + [("a", 0), ("b", 0), ("b", 1), ("a", 1)], [0.45, 0], 1), ] - for payload, edges, waits, presses in cases: - with self.subTest(payload=payload): - session.control.reset_mock() - session._stop_event.reset_mock() - result = session.inject({**payload, "settle_seconds": 0}) - self.assertTrue(result["accepted"]) - self.assertEqual(result["timing"]["presses"], presses) - self.assertEqual([call.args for call in session.control.request.call_args_list], - [("button", (name, level, 0)) for name, level in edges]) - self.assertEqual([call.args[0] for call in session._stop_event.wait.call_args_list], waits) - - def _keyboard_session(self, device): + for device in ("stopwatch", "stickc-plus2", "basic-v27", "stackchan"): + for payload, edges, waits, presses in cases: + with self.subTest(device=device, payload=payload): + session = self._input_session(device) + result = session.inject({**payload, "settle_seconds": 0}) + self.assertTrue(result["accepted"]) + self.assertEqual(result["timing"]["presses"], presses) + self.assertEqual([call.args for call in session.control.request.call_args_list], + [("button", (name, level, 0)) for name, level in edges]) + self.assertEqual([call.args[0] for call in session._stop_event.wait.call_args_list], waits) + + def test_semantic_buttons_reject_unavailable_or_shared_edges_before_dispatch(self): + chord = {"kind": "chord", "target": "button", "keys": ["a", "b"]} + for device, payload in ( + ("papers3", chord), + ("atoms3", chord), + ("core2", chord), + ("stopwatch", {**chord, "keys": ["a", "power"]}), + ("cores3", {"kind": "button", "control": "power", "action": "double_click"}), + ): + with self.subTest(device=device, payload=payload): + session = self._input_session(device) + with self.assertRaises(ServiceError) as error: + session.inject(payload) + self.assertEqual((error.exception.code, error.exception.status), ("unsupported_input", 409)) + session.control.request.assert_not_called() + + def _input_session(self, device): session = Esp32SimDeviceSession.__new__(Esp32SimDeviceSession) session.spec = load_device_specs()[device] session.board = runtime.BOARDS[device] @@ -261,7 +267,7 @@ def _keyboard_session(self, device): def test_cardputer_keyboard_chords_use_physical_key_edges(self): for device in ("cardputer", "cardputer-adv"): with self.subTest(device=device): - session = self._keyboard_session(device) + session = self._input_session(device) payload = {"kind": "chord", "keys": ["Fn", "shift", "comma"], "settle_seconds": 0.2} receipt = session.inject(payload) self.assertTrue(receipt["accepted"]) @@ -297,7 +303,7 @@ def test_cardputer_keyboard_chords_use_physical_key_edges(self): def test_esp32sim_chords_check_every_qemu_profile_key(self): for device in ("cardputer", "cardputer-adv"): - session = self._keyboard_session(device) + session = self._input_session(device) for key in board_controls(session.spec)["keyboard"]: with self.subTest(device=device, key=key): session.control.reset_mock() @@ -319,7 +325,7 @@ def test_esp32sim_chords_check_every_qemu_profile_key(self): self.assertEqual(session.control.request.call_count, 4) def test_keyboard_chord_releases_on_partial_press_failure(self): - session = self._keyboard_session("cardputer-adv") + session = self._input_session("cardputer-adv") failure = ServiceError("runtime_input_rejected", "second press failed", 409) session.control.request.side_effect = [None, failure, None, None] with self.assertRaises(ServiceError) as error: @@ -332,10 +338,10 @@ def test_keyboard_chord_releases_on_partial_press_failure(self): session._record_event.assert_not_called() def test_chord_on_unmapped_board_returns_conflict(self): - session = self._keyboard_session("cores3") + session = self._input_session("cores3") with self.assertRaises(ServiceError) as error: session.inject({"kind": "chord", "keys": ["fn", "1"]}) - self.assertEqual((error.exception.code, error.exception.status), ("not_supported_on_runtime", 409)) + self.assertEqual((error.exception.code, error.exception.status), ("unsupported_input", 409)) session.control.request.assert_not_called() def test_dual_kmeter_variant_preserves_control_board_and_bounds_fixture(self): diff --git a/platform/tools/device_service.py b/platform/tools/device_service.py index 892c826be..cc35e5d08 100644 --- a/platform/tools/device_service.py +++ b/platform/tools/device_service.py @@ -11795,11 +11795,13 @@ def _decode_frame_metrics(self, data, *, analyze): def validate_input(self, payload): if isinstance(payload, dict) and ( - (payload.get("kind") == "chord" and self.spec.id in {"stopwatch", "cardputer", "cardputer-adv"}) - or (self.spec.id == "stopwatch" and payload.get("kind") == "button" - and payload.get("action") == "double_click") + payload.get("kind") == "chord" + or (payload.get("kind") == "button" and payload.get("action") == "double_click") ): - command = normalize_semantic_input(self, payload, native=True) + command = normalize_semantic_input( + self, payload, native=True, + native_button_code=lambda name: esp32sim.gpio_button(self.spec.id, name), + ) if command["target"] == "keyboard": for key in command["qcodes"]: if esp32sim.KEY_ALIASES.get(key, key) not in esp32sim.KEYS: @@ -11819,7 +11821,14 @@ def inject(self, payload): target = command["target"] field = "key" if target == "keyboard" else "control" names = command["qcodes"] if target == "keyboard" else command["names"] - edges = self._input_edges(target, field, names) + try: + edges = self._input_edges(target, field, names) + except ServiceError as error: + if target != "button" or error.code not in {"invalid_input", "not_supported_on_runtime"}: + raise + raise ServiceError("unsupported_input", + "esp32sim requires an independent press and release mapping for each button", + 409) from error receipt = ( {key: value for key, value in command.items() if key not in {"op", "qcodes"}} if command["kind"] == "chord" else command diff --git a/platform/tools/device_service_input.py b/platform/tools/device_service_input.py index 33fea62bd..0364dd5be 100644 --- a/platform/tools/device_service_input.py +++ b/platform/tools/device_service_input.py @@ -88,7 +88,7 @@ def keyboard_code(spec, controls, key, *, in_chord=False): return [key] -def normalize_semantic_input(session, payload, *, native): +def normalize_semantic_input(session, payload, *, native, native_button_code=None): kind = payload["kind"] if not native or getattr(session, "management_binding", None): raise unavailable("semantic actions require an independent native QMP session") @@ -143,6 +143,8 @@ def normalize_semantic_input(session, payload, *, native): qcodes.extend(keyboard_code(session.spec, controls, name, in_chord=kind == "chord")) else: qcode = controls.get("buttons", {}).get(name) + if qcode is None and native_button_code is not None: + qcode = native_button_code(name) if qcode is None: raise unavailable(f"button {name} has no model input on this board") qcodes.append(qcode) diff --git a/tests/uiflow2/api_sweep/inventory_api.source.json b/tests/uiflow2/api_sweep/inventory_api.source.json index eb8b25cd1..6041c4cf6 100644 --- a/tests/uiflow2/api_sweep/inventory_api.source.json +++ b/tests/uiflow2/api_sweep/inventory_api.source.json @@ -3,7 +3,7 @@ "scope": "Every documented Device Service HTTP operation; positive functionality and rejection-only coverage are distinct.", "contract": "docs/device-service.openapi.yaml", "contract_version": "1.103.0", - "contract_sha256": "d4c8895d8f98907d38005b4bef72629ebd36fb84f7d80eae19655b2cf2fe6961", + "contract_sha256": "22460745ac3d6d2edeb76853546aeeca680e85ecdf20cdf3591a8d57962a9834", "validation_category": "virtual-device and live HTTP; hardware NOT RUN", "operation_id_format": "METHOD /OpenAPI/path/{parameter}", "operations": [