Compare commits

...

5 Commits

Author SHA1 Message Date
firestarsdog 089fd38fc2 Add minimal ELM327 RFCOMM support 2026-09-20 19:55:18 -04:00
whoisdomi f15a1974d5 Ioniq 6 turn blips 2026-09-19 21:01:03 -05:00
whoisdomi 08139a021a Car Date/Time fallback when gps/wifi not available 2026-09-19 21:01:02 -05:00
whoisdomi fbe982f47b Ioniq 6 Date/Time DBC Signal
Added date/time signal from Ioniq 6 can
2026-09-19 21:01:01 -05:00
firestarsdog 5e6e978438 Gen2 Bolt HSA Fix? Maybe?
0x315 : Mode 1 when not engaged, not Mode 9
2026-09-19 18:25:59 -04:00
17 changed files with 532 additions and 47 deletions
+1 -1
View File
@@ -1197,7 +1197,7 @@ class CarController(CarControllerBase):
# cannot linger after a disengage or main-off event.
if should_send_bolt_acc_pedal_friction:
can_sends.append(gmcan.create_friction_brake_command(
self.packer_ch, friction_brake_bus, experiment_brake, idx, bolt_acc_pedal_friction_main_on,
self.packer_ch, friction_brake_bus, experiment_brake, idx, bolt_acc_pedal_friction_main_on and CC.longActive,
near_stop, at_full_stop, self.CP))
if self.CP.carFingerprint not in CC_ONLY_CAR:
friction_brake_bus = get_friction_brake_bus(self.CP)
@@ -441,12 +441,6 @@ def suppress_redundant_gv70_brake_cancel(CP, brake_pressed: bool, lat_active: bo
)
def clear_ioniq_6_torque_when_request_inactive(CP, apply_torque: int, apply_steer_req: bool) -> int:
if CP.carFingerprint == CAR.HYUNDAI_IONIQ_6 and not apply_steer_req:
return 0
return apply_torque
class CarController(CarControllerBase):
def __init__(self, dbc_names, CP):
super().__init__(dbc_names, CP)
@@ -642,8 +636,6 @@ class CarController(CarControllerBase):
if not CC.latActive:
apply_torque = 0
apply_torque = clear_ioniq_6_torque_when_request_inactive(self.CP, apply_torque, apply_steer_req)
# Hold torque with induced temporary fault when cutting the actuation bit
# FIXME: we don't use this with CAN FD?
torque_fault = CC.latActive and not apply_steer_req
@@ -20,8 +20,7 @@ from opendbc.car.hyundai.carcontroller import CarController, CANCEL_BUTTON_DELAY
should_track_stop_accel_directly_for_car, \
preserve_stock_canfd_lfa_status, \
preserve_stock_canfd_lkas_status, \
suppress_redundant_gv70_brake_cancel, \
clear_ioniq_6_torque_when_request_inactive
suppress_redundant_gv70_brake_cancel
from opendbc.car.hyundai.carstate import CarState, decode_canfd_camera_lead, decode_ioniq_6_blindspot_radar_state, \
get_canfd_cruise_available
from opendbc.car.hyundai.interface import CarInterface, KIA_EV9_ACCEL_MAX, get_communication_control_request
@@ -680,14 +679,7 @@ class TestHyundaiFingerprint:
assert not (CP.flags & HyundaiFlags.CANFD_LKA_STEERING)
assert bool(CP.flags & HyundaiFlags.CANFD_CAMERA_SCC)
def test_ioniq_6_clears_torque_with_inactive_safety_request(self):
ioniq_6_cp = SimpleNamespace(carFingerprint=CAR.HYUNDAI_IONIQ_6)
other_cp = SimpleNamespace(carFingerprint=CAR.KIA_EV6)
assert clear_ioniq_6_torque_when_request_inactive(ioniq_6_cp, -409, False) == 0
assert clear_ioniq_6_torque_when_request_inactive(ioniq_6_cp, -409, True) == -409
assert clear_ioniq_6_torque_when_request_inactive(other_cp, -409, False) == -409
def test_palisade_2023_uses_can_canfd_blended_layout(self):
palisade_2023 = CarInterface.get_params(CAR.HYUNDAI_PALISADE_2023, gen_empty_fingerprint(), [], True, False, False, None)
assert palisade_2023.flags & HyundaiFlags.CAN_CANFD_BLENDED
assert DBC[palisade_2023.carFingerprint][Bus.pt] == "hyundai_palisade_2023_generated"
@@ -727,10 +727,14 @@ BO_ 1259 LOCAL_TIME2: 8 XXX
SG_ NEW_SIGNAL_3 : 39|1@0+ (1,0) [0|1] "" XXX
BO_ 1264 LOCAL_TIME: 8 XXX
SG_ HOURS : 12|5@0+ (1,0) [0|31] "" XXX
SG_ MINUTES : 21|6@0+ (1,0) [0|63] "" XXX
SG_ SECONDS : 31|8@0+ (1,0) [0|59] "" XXX
SG_ HOURS : 8|8@1+ (1,0) [0|23] "" XXX
SG_ MINUTES : 16|8@1+ (1,0) [0|59] "" XXX
SG_ SECONDS : 24|8@1+ (1,0) [0|59] "" XXX
SG_ MONTH : 34|4@1+ (1,0) [1|12] "" XXX
SG_ YEAR : 40|8@1+ (1,2000) [2000|2255] "" XXX
SG_ DAY : 48|8@1+ (1,0) [1|31] "" XXX
CM_ BO_ 1264 "Cluster wall clock, 1Hz. Local time, not UTC. All 0xFF until the cluster initializes.";
CM_ SG_ 96 BRAKE_PRESSURE "User applied brake pedal pressure. Ramps from computer applied pressure on falling edge of cruise. Cruise cancels if !=0";
CM_ SG_ 101 BRAKE_POSITION "User applied brake pedal position, max is ~700. Signed on some vehicles";
CM_ SG_ 203 ADAS_ActvACISta "ADAS Active AngleControlInterface State";
@@ -964,10 +964,14 @@ BO_ 1259 LOCAL_TIME2: 8 XXX
SG_ NEW_SIGNAL_3 : 39|1@0+ (1,0) [0|1] "" XXX
BO_ 1264 LOCAL_TIME: 8 XXX
SG_ HOURS : 12|5@0+ (1,0) [0|31] "" XXX
SG_ MINUTES : 21|6@0+ (1,0) [0|63] "" XXX
SG_ SECONDS : 31|8@0+ (1,0) [0|59] "" XXX
SG_ HOURS : 8|8@1+ (1,0) [0|23] "" XXX
SG_ MINUTES : 16|8@1+ (1,0) [0|59] "" XXX
SG_ SECONDS : 24|8@1+ (1,0) [0|59] "" XXX
SG_ MONTH : 34|4@1+ (1,0) [1|12] "" XXX
SG_ YEAR : 40|8@1+ (1,2000) [2000|2255] "" XXX
SG_ DAY : 48|8@1+ (1,0) [1|31] "" XXX
CM_ BO_ 1264 "Cluster wall clock, 1Hz. Local time, not UTC. All 0xFF until the cluster initializes.";
CM_ SG_ 96 BRAKE_PRESSURE "User applied brake pedal pressure. Ramps from computer applied pressure on falling edge of cruise. Cruise cancels if !=0";
CM_ SG_ 101 BRAKE_POSITION "User applied brake pedal position, max is ~700. Signed on some vehicles";
CM_ SG_ 203 ADAS_ActvACISta "ADAS Active AngleControlInterface State";
+2
View File
@@ -179,6 +179,8 @@ testpaths = [
"system/tests",
"system/ubloxd",
"system/webrtc",
"starpilot/system/bluetooth/tests",
"starpilot/system/obd/tests",
"tools/lib/tests",
"tools/replay",
"tools/cabana",
@@ -56,6 +56,8 @@ class BluetoothDeviceButton(BigButton):
capabilities.append("audio")
if self.device.controller:
capabilities.append("controller")
if self.device.serial:
capabilities.append("serial")
self.set_value("connected" + (f" / {' / '.join(capabilities)}" if capabilities else ""))
self.set_enabled(True)
elif self.device.paired:
@@ -67,6 +69,8 @@ class BluetoothDeviceButton(BigButton):
capabilities.append("audio")
if self.device.controller:
capabilities.append("controller")
if self.device.serial:
capabilities.append("serial")
self.set_value("pair" + (f" / {' / '.join(capabilities)}" if capabilities else ""))
self.set_enabled(self._offroad)
@@ -264,7 +268,7 @@ class BluetoothLayoutMici(NavScroller):
status.offroad,
status.selected_audio,
status.pairing_address,
tuple((device.address, device.name, device.paired, device.connected, device.audio, device.controller) for device in status.devices),
tuple((device.address, device.name, device.paired, device.connected, device.audio, device.controller, device.serial) for device in status.devices),
)
if signature != self._last_signature:
self._last_signature = signature
@@ -103,9 +103,11 @@ def test_settings_constructs_a_dedicated_bluetooth_panel(monkeypatch):
def test_device_status_prioritizes_operations_then_connection_and_capabilities():
device = make_device(paired=True, connected=True, audio=True, controller=True)
serial_device = make_device(serial=True)
assert device_status_text(device, "connecting", ADDRESS) == "Connecting..."
assert device_status_text(device, "", ADDRESS) == "Connected / audio output / controller"
assert device_status_text(serial_device, "", "") == "Tap to pair / serial"
def test_device_action_policy_matches_the_daemon_onroad_rules():
+3 -2
View File
@@ -235,7 +235,7 @@ class BlueZClient:
continue
props = interfaces[DEVICE_IFACE]
uuids = [str(value).lower() for value in props.get("UUIDs", [])]
audio, controller = device_capabilities(uuids, int(props.get("Class", 0)), str(props.get("Icon", "")))
audio, controller, serial = device_capabilities(uuids, int(props.get("Class", 0)), str(props.get("Icon", "")))
device = {
"path": path,
"address": str(props.get("Address", "")),
@@ -248,9 +248,10 @@ class BlueZClient:
"uuids": uuids,
"audio": audio,
"controller": controller,
"serial": serial,
}
if include_hidden or show_pairing_device(device["address"], device["name"], device["paired"], device["trusted"], device["connected"],
device["blocked"], audio, controller, include_discovering):
device["blocked"], audio, controller, serial, include_discovering):
devices.append(device)
return sorted(devices, key=lambda device: (not device["connected"], not device["paired"], -(device["rssi"] or -127), device["name"].lower()))
+8 -4
View File
@@ -16,6 +16,7 @@ BLUETOOTH_RADIO_HELPER = "/usr/comma/bluetooth-radio"
A2DP_SINK_UUID = "0000110b-0000-1000-8000-00805f9b34fb"
HID_UUID = "00001124-0000-1000-8000-00805f9b34fb"
HOG_UUID = "00001812-0000-1000-8000-00805f9b34fb"
SPP_UUID = "00001101-0000-1000-8000-00805f9b34fb"
COMMAND_TIMEOUTS = {
"set_power": 90.0,
"start_scan": 20.0,
@@ -40,6 +41,7 @@ class BluetoothDevice:
uuids: tuple[str, ...] = ()
audio: bool = False
controller: bool = False
serial: bool = False
@classmethod
def from_dict(cls, value: dict[str, Any]) -> "BluetoothDevice":
@@ -54,6 +56,7 @@ class BluetoothDevice:
uuids=tuple(str(uuid).lower() for uuid in value.get("uuids", ())),
audio=bool(value.get("audio", False)),
controller=bool(value.get("controller", False)),
serial=bool(value.get("serial", False)),
)
@@ -86,21 +89,22 @@ class BluetoothStatus:
)
def device_capabilities(uuids: list[str] | tuple[str, ...], bluetooth_class: int = 0, icon: str = "") -> tuple[bool, bool]:
def device_capabilities(uuids: list[str] | tuple[str, ...], bluetooth_class: int = 0, icon: str = "") -> tuple[bool, bool, bool]:
normalized = {str(uuid).lower() for uuid in uuids}
major_class = (int(bluetooth_class) >> 8) & 0x1F
audio = A2DP_SINK_UUID in normalized or major_class == 0x04 or icon in {"audio-card", "audio-headphones", "audio-headset"}
controller = HID_UUID in normalized or HOG_UUID in normalized or major_class == 0x05 or icon in {"input-gaming", "input-mouse", "input-keyboard"}
return audio, controller
serial = SPP_UUID in normalized
return audio, controller, serial
def show_pairing_device(address: str, name: str, paired: bool, trusted: bool, connected: bool, blocked: bool,
audio: bool, controller: bool, discovering: bool = False) -> bool:
audio: bool, controller: bool, serial: bool, discovering: bool = False) -> bool:
known = paired or trusted or connected
normalized_address = "".join(character for character in address.upper() if character.isalnum())
normalized_name = "".join(character for character in name.upper() if character.isalnum())
named = bool(name) and name != "Unknown device" and normalized_name != normalized_address
return known or (named and not blocked and (audio or controller))
return known or (named and not blocked and (audio or controller or serial))
class _DesktopFakeBluetooth:
@@ -8,7 +8,7 @@ import pytest
from openpilot.starpilot.system.bluetooth.audio import BluetoothAudioSink
from openpilot.starpilot.system.bluetooth.bluez import PairingAgent
from openpilot.starpilot.system.bluetooth.daemon import BluetoothController
from openpilot.starpilot.system.bluetooth.protocol import (A2DP_SINK_UUID, HID_UUID, BluetoothClient, BluetoothDevice, BluetoothStatus,
from openpilot.starpilot.system.bluetooth.protocol import (A2DP_SINK_UUID, HID_UUID, SPP_UUID, BluetoothClient, BluetoothDevice, BluetoothStatus,
device_capabilities, show_pairing_device)
from openpilot.system import hardware
from openpilot.system.ui.lib.bluetooth_manager import BluetoothManager
@@ -169,23 +169,26 @@ class FakeProcess:
def test_protocol_round_trip_and_capabilities():
audio, controller = device_capabilities([A2DP_SINK_UUID, HID_UUID])
assert audio and controller
audio, controller, serial = device_capabilities([A2DP_SINK_UUID, HID_UUID, SPP_UUID])
assert audio and controller and serial
status = BluetoothStatus.from_dict({
"available": True,
"enabled": True,
"devices": [{"address": "00:11:22:33:44:55", "name": "Combo", "uuids": [A2DP_SINK_UUID, HID_UUID], "audio": True, "controller": True}],
"devices": [{"address": "00:11:22:33:44:55", "name": "Combo", "uuids": [A2DP_SINK_UUID, HID_UUID, SPP_UUID],
"audio": True, "controller": True, "serial": True}],
})
assert status.devices == (BluetoothDevice("00:11:22:33:44:55", "Combo", uuids=(A2DP_SINK_UUID, HID_UUID), audio=True, controller=True),)
assert status.devices == (BluetoothDevice("00:11:22:33:44:55", "Combo", uuids=(A2DP_SINK_UUID, HID_UUID, SPP_UUID),
audio=True, controller=True, serial=True),)
def test_pairing_list_filters_anonymous_and_irrelevant_advertisements():
assert not show_pairing_device("00:11:22:33:44:55", "00:11:22:33:44:55", False, False, False, False, False, False)
assert not show_pairing_device("00:11:22:33:44:55", "Nearby sensor", False, False, False, False, False, False)
assert show_pairing_device("00:11:22:33:44:55", "Media Remote", False, False, False, False, False, True)
assert show_pairing_device("00:11:22:33:44:55", "Media Remote", False, False, False, False, False, True, True)
assert not show_pairing_device("00:11:22:33:44:55", "Nearby sensor", False, False, False, False, False, False, True)
assert show_pairing_device("00:11:22:33:44:55", "Known device", True, True, False, False, False, False)
assert not show_pairing_device("00:11:22:33:44:55", "00:11:22:33:44:55", False, False, False, False, False, False, False)
assert not show_pairing_device("00:11:22:33:44:55", "Nearby sensor", False, False, False, False, False, False, False)
assert show_pairing_device("00:11:22:33:44:55", "Media Remote", False, False, False, False, False, True, False)
assert show_pairing_device("00:11:22:33:44:55", "Serial Adapter", False, False, False, False, False, False, True)
assert show_pairing_device("00:11:22:33:44:55", "Media Remote", False, False, False, False, False, True, False, True)
assert not show_pairing_device("00:11:22:33:44:55", "Nearby sensor", False, False, False, False, False, False, False, True)
assert show_pairing_device("00:11:22:33:44:55", "Known device", True, True, False, False, False, False, False)
def test_desktop_fake_bluetooth_is_stateful_and_interactive(monkeypatch, tmp_path):
+1
View File
@@ -0,0 +1 @@
+171
View File
@@ -0,0 +1,171 @@
import socket
import threading
import time
from typing import Any
DEFAULT_COMMAND_TIMEOUT = 5.0
DEFAULT_CONNECT_TIMEOUT = 10.0
ELM_IDENTITY_MARKERS = ("ELM", "STN", "OBD")
class RFCOMMTransport:
def __init__(self, connect_timeout: float = DEFAULT_CONNECT_TIMEOUT):
self.connect_timeout = connect_timeout
self.channel: int | None = None
self._socket: socket.socket | None = None
def connect(self, mac: str) -> None:
self.close()
address_family = getattr(socket, "AF_BLUETOOTH", None)
protocol = getattr(socket, "BTPROTO_RFCOMM", None)
if address_family is None or protocol is None:
raise RuntimeError("Native Bluetooth RFCOMM sockets are unavailable")
channels = tuple(dict.fromkeys((self.channel, 1, 2))) if self.channel is not None else (1, 2)
last_error: OSError | None = None
for channel in channels:
sock = socket.socket(address_family, socket.SOCK_STREAM, protocol)
try:
sock.settimeout(self.connect_timeout)
sock.connect((mac, channel))
except OSError as error:
last_error = error
sock.close()
continue
self._socket = sock
self.channel = channel
return
raise ConnectionError(f"Unable to connect to {mac} on RFCOMM channels 1 or 2") from last_error
def close(self) -> None:
sock = self._socket
self._socket = None
if sock is not None:
sock.close()
def read(self, size: int = 4096, timeout: float | None = None) -> bytes:
if self._socket is None:
raise ConnectionError("RFCOMM transport is not connected")
self._socket.settimeout(timeout)
data = self._socket.recv(size)
if not data:
self.close()
raise ConnectionError("RFCOMM device disconnected")
return data
def write(self, data: bytes) -> None:
if self._socket is None:
raise ConnectionError("RFCOMM transport is not connected")
try:
self._socket.sendall(data)
except OSError:
self.close()
raise
class ELM327:
def __init__(self, mac: str, transport: RFCOMMTransport | None = None, command_timeout: float = DEFAULT_COMMAND_TIMEOUT,
reconnect_attempts: int = 3, reconnect_backoff: float = 1.0, sleep=time.sleep):
self.mac = mac
self.command_timeout = command_timeout
self.reconnect_attempts = reconnect_attempts
self.reconnect_backoff = reconnect_backoff
self._transport: Any = transport or RFCOMMTransport()
self._lock = threading.RLock()
self._connected = False
self._initialized = False
self._sleep = sleep
def connect(self) -> str:
with self._lock:
self.close()
try:
return self._open_and_identify()
except Exception:
self.close()
raise
def close(self) -> None:
with self._lock:
self._transport.close()
self._connected = False
self._initialized = False
def command(self, cmd: str, timeout: float | None = None) -> str:
with self._lock:
try:
return self._command_once(cmd, self.command_timeout if timeout is None else timeout)
except OSError:
initialized = self._initialized
self.close()
self._reconnect(initialized)
raise
def initialize(self) -> None:
with self._lock:
try:
self._initialize_once()
except OSError:
self.close()
self._reconnect(initialize=True)
raise
def _open_and_identify(self) -> str:
self._transport.connect(self.mac)
self._connected = True
identity = self._command_once("ATI", self.command_timeout)
if not any(marker in identity.upper() for marker in ELM_IDENTITY_MARKERS):
raise RuntimeError("RFCOMM device did not identify as an ELM-compatible adapter")
return identity
def _initialize_once(self) -> None:
for command in ("ATZ", "ATE0", "ATL0", "ATI", "ATSP0"):
self._command_once(command, self.command_timeout)
self._initialized = True
def _reconnect(self, initialize: bool) -> None:
for attempt in range(self.reconnect_attempts):
if attempt:
self._sleep(self.reconnect_backoff * (2 ** (attempt - 1)))
try:
self._open_and_identify()
if initialize:
self._initialize_once()
return
except OSError:
self.close()
def _command_once(self, cmd: str, timeout: float) -> str:
if not self._connected:
raise ConnectionError("ELM327 is not connected")
command = cmd.rstrip("\r\n")
self._transport.write(command.encode("ascii") + b"\r")
deadline = time.monotonic() + timeout
response = bytearray()
while b">" not in response:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError(f"Timed out waiting for ELM327 response to {command!r}")
try:
chunk = self._transport.read(4096, timeout=remaining)
except TimeoutError as error:
raise TimeoutError(f"Timed out waiting for ELM327 response to {command!r}") from error
if not chunk:
raise ConnectionError("ELM327 disconnected before sending a prompt")
response.extend(chunk)
raw = bytes(response).split(b">", 1)[0]
return self._clean_response(raw, command)
@staticmethod
def _clean_response(raw: bytes, command: str) -> str:
lines = raw.decode("ascii", errors="replace").replace("\r\n", "\n").replace("\r", "\n").split("\n")
while lines and not lines[0].strip():
lines.pop(0)
while lines and not lines[-1].strip():
lines.pop()
if lines and lines[0].strip().upper() == command.upper():
lines.pop(0)
while lines and not lines[0].strip():
lines.pop(0)
return "\n".join(lines)
+1
View File
@@ -0,0 +1 @@
+226
View File
@@ -0,0 +1,226 @@
from collections import deque
import pytest
import openpilot.starpilot.system.obd.elm327 as elm327_module
from openpilot.starpilot.system.obd.elm327 import ELM327, RFCOMMTransport
class ScriptedTransport:
def __init__(self, responses):
self.responses = deque(responses)
self.current = deque()
self.connects = []
self.writes = []
self.connected = False
def connect(self, mac):
self.connects.append(mac)
self.connected = True
def close(self):
self.connected = False
def write(self, data):
if not self.connected:
raise ConnectionError("transport is closed")
self.writes.append(data)
self.current = deque(self.responses.popleft())
def read(self, _size=4096, timeout=None):
item = self.current.popleft()
if isinstance(item, BaseException):
raise item
return item
class FakeRFCOMMSocket:
def __init__(self, attempts, failing_channels):
self.attempts = attempts
self.failing_channels = failing_channels
self.closed = False
self.timeout = None
def settimeout(self, timeout):
self.timeout = timeout
def connect(self, address):
self.attempts.append(address)
if address[1] in self.failing_channels:
raise OSError("channel unavailable")
def close(self):
self.closed = True
def test_command_returns_clean_normal_response():
transport = ScriptedTransport([
[b"ATI\rELM327 v1.5\r>"],
[b"41 00 BE 3E B8 13\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
assert elm.connect() == "ELM327 v1.5"
assert elm.command("0100") == "41 00 BE 3E B8 13"
assert transport.writes == [b"ATI\r", b"0100\r"]
def test_command_handles_echo_crlf_chunks_and_multiline_response():
transport = ScriptedTransport([
[b"ATI\r\rELM327 v1.5\r\r>"],
[b"010C\r\rSEARCHING...\r\n", b"41 0C 1A F8\r\n>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
assert elm.command("010C") == "SEARCHING...\n41 0C 1A F8"
def test_command_preserves_substantive_spacing_and_blank_lines():
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
[b"0100\r\r VALUE \r\rSECOND\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
assert elm.command("0100") == " VALUE \n\nSECOND"
@pytest.mark.parametrize(("raw", "expected"), [
(b"NO DATA\r>", "NO DATA"),
(b"?\r>", "?"),
])
def test_command_returns_valid_adapter_states(raw, expected):
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
[raw],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
assert elm.command("0100") == expected
def test_initialize_uses_the_minimal_obd_command_sequence():
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
[b"ATZ\rELM327 v1.5\r>"],
[b"ATE0\rOK\r>"],
[b"OK\r>"],
[b"ELM327 v1.5\r>"],
[b"OK\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
elm.initialize()
assert transport.writes == [b"ATI\r", b"ATZ\r", b"ATE0\r", b"ATL0\r", b"ATI\r", b"ATSP0\r"]
@pytest.mark.parametrize("identity", [b"OK\r>", b"NO DATA\r>", b"CAN ERROR\r>", b"garbage\r>"])
def test_connect_requires_a_sensible_elm_identity(identity):
transport = ScriptedTransport([[identity]])
elm = ELM327("00:11:22:33:44:55", transport=transport)
with pytest.raises(RuntimeError, match="identify"):
elm.connect()
assert not transport.connected
def test_timeout_is_an_exception_and_reconnects_the_session():
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
[TimeoutError("read timed out")],
[b"ELM327 v1.5\r>"],
[b"NO DATA\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
with pytest.raises(TimeoutError, match="0100"):
elm.command("0100", timeout=0.01)
assert transport.connects == ["00:11:22:33:44:55", "00:11:22:33:44:55"]
assert elm.command("0100") == "NO DATA"
def test_disconnect_before_prompt_reconnects_and_reinitializes():
initialization = [
[b"ELM327 v1.5\r>"],
[b"OK\r>"],
[b"OK\r>"],
[b"ELM327 v1.5\r>"],
[b"OK\r>"],
]
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
*initialization,
[b"41 00 BE", b""],
[b"ELM327 v1.5\r>"],
*initialization,
[b"41 00 BE 3E B8 13\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
elm.initialize()
with pytest.raises(ConnectionError, match="prompt"):
elm.command("0100")
assert transport.connects == ["00:11:22:33:44:55", "00:11:22:33:44:55"]
assert transport.writes[-6:] == [b"ATI\r", b"ATZ\r", b"ATE0\r", b"ATL0\r", b"ATI\r", b"ATSP0\r"]
assert elm.command("0100") == "41 00 BE 3E B8 13"
def test_initialize_failure_reconnects_and_restarts_initialization():
transport = ScriptedTransport([
[b"ELM327 v1.5\r>"],
[b"ELM327 v1.5\r>"],
[b""],
[b"ELM327 v1.5\r>"],
[b"ELM327 v1.5\r>"],
[b"OK\r>"],
[b"OK\r>"],
[b"ELM327 v1.5\r>"],
[b"OK\r>"],
[b"NO DATA\r>"],
])
elm = ELM327("00:11:22:33:44:55", transport=transport)
elm.connect()
with pytest.raises(ConnectionError, match="prompt"):
elm.initialize()
assert transport.writes[-6:] == [b"ATI\r", b"ATZ\r", b"ATE0\r", b"ATL0\r", b"ATI\r", b"ATSP0\r"]
assert elm.command("0100") == "NO DATA"
def test_rfcomm_transport_falls_back_then_reuses_the_working_channel(monkeypatch):
attempts = []
failing_channels = {1}
def socket_factory(*_args):
return FakeRFCOMMSocket(attempts, failing_channels)
monkeypatch.setattr(elm327_module.socket, "AF_BLUETOOTH", 31, raising=False)
monkeypatch.setattr(elm327_module.socket, "BTPROTO_RFCOMM", 3, raising=False)
monkeypatch.setattr(elm327_module.socket, "socket", socket_factory)
transport = RFCOMMTransport()
transport.connect("00:11:22:33:44:55")
assert transport.channel == 2
transport.close()
failing_channels.clear()
transport.connect("00:11:22:33:44:55")
assert attempts == [
("00:11:22:33:44:55", 1),
("00:11:22:33:44:55", 2),
("00:11:22:33:44:55", 2),
]
+80 -4
View File
@@ -17,6 +17,67 @@ try:
except Exception:
TimezoneFinder = None
try:
from zoneinfo import ZoneInfo
except Exception:
ZoneInfo = None
# StarPilot variables
# Hyundai/Kia cluster wall clock, local time. Fallback when GPS has no fix.
CAR_CLOCK_ADDR = 0x4F0
CAR_CLOCK_BUS = 1
def parse_car_clock(dat: bytes):
# cluster sends all 0xFF until initialized
if len(dat) < 7:
return None
if all(b == 0xFF for b in dat) or all(b == 0 for b in dat):
return None
hour, minute, second = dat[1], dat[2], dat[3]
month = (dat[4] >> 2) & 0x0F
year = 2000 + dat[5]
day = dat[6]
try:
return datetime.datetime(year, month, day, hour, minute, second)
except ValueError:
return None
def read_car_clock(can_sock, timezone):
latest = None
for msg in messaging.drain_sock(can_sock):
for frame in msg.can:
if frame.address == CAR_CLOCK_ADDR and frame.src == CAR_CLOCK_BUS:
parsed = parse_car_clock(bytes(frame.dat))
if parsed is not None:
latest = parsed
if latest is None:
return None
utc = car_clock_to_utc(latest, timezone)
if utc is None or not (min_date() <= utc <= MAX_DATE):
return None
return utc
def car_clock_to_utc(local_dt, timezone):
# timezone comes from the last GPS fix, so this needs one to have happened once
if ZoneInfo is None or not timezone:
return None
if isinstance(timezone, bytes):
timezone = timezone.decode("utf-8", errors="replace")
try:
tz = ZoneInfo(timezone)
except Exception:
cloudlog.exception("timed.bad_timezone")
return None
# fold=0: on the duplicated hour of a DST fall back, assume the first pass
return local_dt.replace(tzinfo=tz, fold=0).astimezone(datetime.UTC).replace(tzinfo=None)
def set_time(new_time):
diff = datetime.datetime.now(datetime.UTC).replace(tzinfo=None) - new_time
@@ -64,10 +125,13 @@ def main() -> NoReturn:
pm = messaging.PubMaster(['clocks'])
sm = messaging.SubMaster([gps_location_service])
# StarPilot variables
can_sock = messaging.sub_sock('can', timeout=100)
# StarPilot variables
tf = TimezoneFinder() if TimezoneFinder is not None else None
timezonefinder_logged = False
car_clock_tz_logged = False
last_timezone = params.get("Timezone")
if last_timezone is not None:
@@ -83,11 +147,23 @@ def main() -> NoReturn:
gps = sm[gps_location_service]
gps_time = datetime.datetime.fromtimestamp(gps.unixTimestampMillis / 1000., datetime.UTC).replace(tzinfo=None)
if not sm.updated[gps_location_service] or (time.monotonic() - sm.logMonoTime[gps_location_service] / 1e9) > 2.0:
gps_usable = (sm.updated[gps_location_service] and
(time.monotonic() - sm.logMonoTime[gps_location_service] / 1e9) <= 2.0 and
gps.hasFix and min_date() <= gps_time <= MAX_DATE)
# StarPilot variables
# only corrects an invalid clock, so GPS and NTP always win
if not gps_usable and not system_time_valid():
if not last_timezone and not car_clock_tz_logged:
cloudlog.warning("timed: no saved timezone, cannot use car clock fallback")
car_clock_tz_logged = True
car_time = read_car_clock(can_sock, last_timezone)
if car_time is not None:
cloudlog.warning(f"timed: setting time from car clock: {car_time}")
set_time(car_time)
continue
if not gps.hasFix:
continue
if gps_time < min_date() or gps_time > MAX_DATE:
if not gps_usable:
continue
set_time(gps_time)
+2
View File
@@ -45,6 +45,8 @@ def device_status_text(device: BluetoothDevice, operation: str, selected_audio:
capabilities.append(tr("audio output") if selected_audio.upper() == device.address.upper() else tr("audio"))
if device.controller:
capabilities.append(tr("controller"))
if device.serial:
capabilities.append(tr("serial"))
capability_text = " / ".join(capabilities)
if device.connected: