diff --git a/openpilot/starpilot/system/starport/session.py b/openpilot/starpilot/system/starport/session.py index 1a79933bac..68bffc48a0 100644 --- a/openpilot/starpilot/system/starport/session.py +++ b/openpilot/starpilot/system/starport/session.py @@ -102,6 +102,8 @@ CODEC_NAMES = {3: "H.264", 5: "VP9", 6: "AV1", 7: "H.265"} # kind and then every hundredth, so a head unit repeating one cannot flood the session log. IGNORED_LOG_FIRST = 5 IGNORED_LOG_EVERY = 100 +INPUT_LOG_FIRST = 20 +INPUT_LOG_EVERY = 100 FRAME_RATES = {1: 60, 2: 30} # Software H.264 on the comma is the constraint: prefer 720p, then 480p. 1080p # is accepted only when nothing smaller is offered. @@ -797,6 +799,9 @@ class ProjectionSession(Session): """Open the touch/key channel so input is acknowledged; failures are not fatal.""" inputs = [ch for ch in self.channels if ch.get("input")] channel = next((ch for ch in inputs if ch.get("display_id") in (None, 0)), inputs[0] if inputs else None) + self.event("input_channels", channels=[{"channel": ch["id"], "display_id": ch.get("display_id"), + "config": str(ch.get("input_config") or InputConfig())} for ch in inputs], + selected=channel["id"] if channel is not None else None) if channel is None: return try: @@ -812,9 +817,14 @@ class ProjectionSession(Session): self.touch = TouchMapper(config, self.mode.width, self.mode.height, self.mode.margin_width, self.mode.margin_height, content_left=self.mode.content_left, content_top=self.mode.content_top) self.event("input_opened", channel=channel["id"], keycodes=list(config.keycodes), - touch=f"{config.touch_width}x{config.touch_height}") + touch=f"{config.touch_width}x{config.touch_height}", display_id=channel.get("display_id"), + mapping={"touch_width": self.touch.touch_width, "touch_height": self.touch.touch_height, + "video_width": self.touch.video_width, "video_height": self.touch.video_height, + "content_left": self.touch.left, "content_top": self.touch.top, + "content_width": self.touch.content_width, "content_height": self.touch.content_height, + "content_space": self.touch.content_space} if self.touch is not None else None) except (TimeoutError, ValueError) as error: - self.event("input_unavailable", error=str(error)) + self.event("input_unavailable", channel=channel["id"], error=str(error)) def _open_optional(self, channel: dict, name: str) -> bool: try: @@ -968,21 +978,31 @@ class ProjectionSession(Session): elif channel == self.input_channel: if kind == INPUT_EVENT: self.input_events += 1 + mapped = [] + disposition = "no_mapper" if self.touch is None else "unfocused" if not self.focused else "no_touch_event" if self.touch is not None and self.focused: try: - self.touch_events.extend(self.touch.decode(data)) + mapped = self.touch.decode(data) + self.touch_events.extend(mapped) + if mapped: + disposition = "queued" except ValueError as error: - self.event("input_invalid", error=str(error)) + disposition = "invalid" + self.event("input_invalid", channel=channel, kind=kind, error=str(error)) + if self.input_events <= INPUT_LOG_FIRST or self.input_events % INPUT_LOG_EVERY == 0: + self.event("input_event", channel=channel, kind=kind, count=self.input_events, bytes=len(data), + focused=self.focused, disposition=disposition, message=describe(data), + mapped=[{"kind": event.kind, "x": round(event.x, 5), "y": round(event.y, 5)} for event in mapped]) # The driver may be on the car's own screen while Android Auto plays the music. try: for action in self.keys.decode(data): - self.event("media_key", action=action) + self.event("media_key", channel=channel, action=action) if self.media_control is not None: self.media_control(action) except ValueError as error: - self.event("input_invalid", error=str(error)) + self.event("input_invalid", channel=channel, kind=kind, error=str(error)) elif kind == INPUT_BINDING_RESPONSE: - self.event("input_bound", status=signed(one(fields, 1, 0))) + self.event("input_bound", channel=channel, status=signed(one(fields, 1, 0))) else: self.ignored("input_ignored", channel, kind, data) elif channel is not None and channel == self.sensor_channel: diff --git a/openpilot/starpilot/system/starport/tests/test_media_controls.py b/openpilot/starpilot/system/starport/tests/test_media_controls.py index e26f1939a2..3bc4507954 100644 --- a/openpilot/starpilot/system/starport/tests/test_media_controls.py +++ b/openpilot/starpilot/system/starport/tests/test_media_controls.py @@ -53,7 +53,7 @@ def test_session_sends_media_keys_even_on_the_cars_own_screen(focused): session.handle(3, INPUT_EVENT, keys(key(87, False))) session.handle(3, INPUT_EVENT, keys(key(88, True))) assert session.actions == ['next', 'previous'] - assert ('media_key', {'action': 'next'}) in session.events + assert ('media_key', {'channel': 3, 'action': 'next'}) in session.events class Session: diff --git a/openpilot/starpilot/system/starport/tests/test_touch.py b/openpilot/starpilot/system/starport/tests/test_touch.py index 0118824e93..71ecb84ba9 100644 --- a/openpilot/starpilot/system/starport/tests/test_touch.py +++ b/openpilot/starpilot/system/starport/tests/test_touch.py @@ -1,7 +1,10 @@ """Receiver touch packets must preserve protobuf defaults and pointer state.""" import pytest +from unittest.mock import Mock +from openpilot.starpilot.system.starport.session import INPUT_BINDING_RESPONSE, INPUT_EVENT, VideoMode +from openpilot.starpilot.system.starport.tests.test_media_controls import session_with_input from openpilot.starpilot.system.starport.touch import InputConfig, TouchEvent, TouchMapper from openpilot.starpilot.system.starport.wire import field @@ -30,3 +33,83 @@ def test_move_without_a_press_does_not_start_a_gesture(): assert mapper.decode(packet(2)) == [] assert mapper.decode(packet(1)) == [] assert mapper.pointer is None + + +@pytest.fixture +def session(): + value = session_with_input(True) + value.touch = TouchMapper(InputConfig(), 1280, 720, 0, 0) + return value + + +def test_logs_wire_packet_and_mapped_gesture(session): + for action in (None, 2, 1): + session.handle(3, INPUT_EVENT, packet(action)) + records = [values for name, values in session.events if name == 'input_event'] + assert [row['mapped'][0]['kind'] for row in records] == ['down', 'move', 'up'] + assert all(row['channel'] == 3 and row['disposition'] == 'queued' for row in records) + assert records[0]['message'][3][0][1][0] == {1: [640], 2: [360]} + assert records[0]['mapped'] == [{'kind': 'down', 'x': .5, 'y': .5}] + assert len(session.touch_events) == 3 + + +@pytest.mark.parametrize('reason', ['unfocused', 'no_mapper', 'no_touch_event', 'invalid']) +def test_logs_why_touch_did_not_queue(session, reason): + data = packet(None) + if reason == 'unfocused': + session.focused = False + elif reason == 'no_mapper': + session.touch = None + elif reason == 'no_touch_event': + data = packet(2) + else: + data = field(3, b'\x80') + session.handle(3, INPUT_EVENT, data) + record = next(values for name, values in session.events if name == 'input_event') + assert record['disposition'] == reason + assert record['mapped'] == [] + assert session.touch_events == [] + + +def test_input_log_sampling_does_not_drop_gestures(session): + for _ in range(100): + session.handle(3, INPUT_EVENT, packet(None)) + session.handle(3, INPUT_EVENT, packet(1)) + records = [values for name, values in session.events if name == 'input_event'] + assert [row['count'] for row in records] == [*range(1, 21), 100, 200] + assert len(session.touch_events) == 200 + + +def test_channel_selection_and_mapping_are_logged(session): + session.channels = [{'id': 4, 'input': True, 'display_id': 1}, + {'id': 3, 'input': True, 'display_id': 0, 'input_config': InputConfig((), 1280, 720)}] + session.mode = VideoMode(5, 0, 1280, 720, 30, 0, 240) + session.send = Mock() + session.wait_for = Mock(return_value=field(1, 0)) + session.open_input() + selection = next(values for name, values in session.events if name == 'input_channels') + assert selection['selected'] == 3 + assert [ch['channel'] for ch in selection['channels']] == [4, 3] + opened = next(values for name, values in session.events if name == 'input_opened') + assert opened['channel'] == 3 + assert opened['mapping']['content_height'] == 480 + assert opened['mapping']['content_top'] == 120 + + +def test_no_advertised_input_channel_is_logged(session): + session.channels = [] + session.open_input() + assert session.events == [('input_channels', {'channels': [], 'selected': None})] + + +def test_failed_input_channel_is_identified(session): + session.channels = [{'id': 7, 'input': True}] + session.send = Mock() + session.wait_for = Mock(side_effect=TimeoutError('no response')) + session.open_input() + assert ('input_unavailable', {'channel': 7, 'error': 'no response'}) in session.events + + +def test_binding_response_includes_channel(session): + session.handle(3, INPUT_BINDING_RESPONSE, field(1, 0)) + assert session.events == [('input_bound', {'channel': 3, 'status': 0})]