diff --git a/openpilot/sunnypilot/system/ble/link.py b/openpilot/sunnypilot/system/ble/link.py index ef2f533f6b..e2bd7b2213 100644 --- a/openpilot/sunnypilot/system/ble/link.py +++ b/openpilot/sunnypilot/system/ble/link.py @@ -18,7 +18,7 @@ MTU_MIN = 23 FRAME_HEADER = 3 MAX_ATT_VALUE = 512 # Bluetooth caps an attribute value at 512 bytes whatever the MTU PRE_SESSION_CAP = 1024 -SESSION_CAP = 16384 +SESSION_CAP = 65536 RECORD_TYPE = 0x20 REKEY_INTERVAL = 65536 KEYS_INFO = b"sunnylink-ble-connect-v1 keys" diff --git a/openpilot/sunnypilot/system/ble/rpc.py b/openpilot/sunnypilot/system/ble/rpc.py index dfa83a662c..1ae47bec34 100644 --- a/openpilot/sunnypilot/system/ble/rpc.py +++ b/openpilot/sunnypilot/system/ble/rpc.py @@ -4,12 +4,17 @@ Copyright (c) 2021-, sunnypilot, and a number of other contributors. This file is part of sunnypilot and is licensed under the MIT License. See the LICENSE.md file in the root directory for more details. """ +import base64 +import gzip import json from openpilot.common.hardware import HARDWARE from openpilot.common.params import ParamKeyType from openpilot.common.version import get_version from openpilot.sunnypilot.sunnylink.athena.params_guard import BLOCKED_PARAMS, is_local_param +from openpilot.sunnypilot.models.model_name import DEFAULT_BIG_MODEL, DEFAULT_MODEL +from openpilot.sunnypilot.sunnylink.capabilities import CAPABILITY_LABELS, CAR_CAPABILITY_FIELDS, generate_capabilities +from openpilot.sunnypilot.sunnylink.tools.generate_settings_schema import generate_schema # what sunnylink's getParams reports for a key that was never set and has no default UNSET_VALUES = {ParamKeyType.BOOL: False, ParamKeyType.INT: 0, ParamKeyType.FLOAT: 0.0, ParamKeyType.STRING: "", ParamKeyType.JSON: {}} @@ -90,6 +95,8 @@ class Rpc: return _envelope(rid, result=self._get(args)), None if method == "params.set": return _envelope(rid, result=self._set(args)), None + if method == "params.metadata": + return _envelope(rid, result=self._params_metadata()), None if method == "hotspot.start": wifi = self._wifi() if wifi.is_tethering_active(): @@ -168,6 +175,21 @@ class Rpc: raise _refused(f"bad value for {key}") from e return {"ok": list(values)} + def _params_metadata(self) -> dict: + try: + schema = generate_schema() + schema["capabilities"] = generate_capabilities(self._params) + schema["capability_labels"] = CAPABILITY_LABELS + schema["car_capability_fields"] = CAR_CAPABILITY_FIELDS + schema["default_model"] = DEFAULT_MODEL + schema["default_big_model"] = DEFAULT_BIG_MODEL + schema["chestnut_active"] = self._params.get_bool("ChestnutActive") + raw = json.dumps(schema, separators=(",", ":")).encode("utf-8") + encoded = base64.b64encode(gzip.compress(raw)).decode("utf-8") + return {"params_metadata": encoded} + except Exception as e: + raise _refused("params metadata unavailable") from e + def _envelope(rid, result=None, error=None) -> dict: if error is not None: diff --git a/openpilot/sunnypilot/system/tests/test_ble_session.py b/openpilot/sunnypilot/system/tests/test_ble_session.py index 38b2eb5ab2..d5931f24b6 100644 --- a/openpilot/sunnypilot/system/tests/test_ble_session.py +++ b/openpilot/sunnypilot/system/tests/test_ble_session.py @@ -635,9 +635,24 @@ class TestRpcOverSession(OpenpilotTestCase): big = {"jsonrpc": "2.0", "id": 3, "method": "nope", "params": {"pad": "x" * 9000}} h.send_record(phone, s.RPC, json.dumps(big).encode(), 0.0) assert phone.connected and json.loads(phone.pop_record(s.RPC))["error"]["code"] == -32601 - h.send(phone, phone.records.seal(bytes([s.RPC]) + b" " * 17000), 0.0) + h.send(phone, phone.records.seal(bytes([s.RPC]) + b" " * (s.link.SESSION_CAP + 1000)), 0.0) assert not phone.connected + def test_params_metadata(self, mocker): + import base64 + import gzip + + h, phone = self.session(mocker) + phone.mtu = 247 + mocker.patch("openpilot.sunnypilot.sunnylink.tools.generate_settings_schema.generate_schema", return_value={"panels": []}) + mocker.patch("openpilot.sunnypilot.sunnylink.capabilities.generate_capabilities", return_value={"protocol_version": 1}) + response = phone.rpc("params.metadata", 0.0) + encoded = response["result"]["params_metadata"] + decoded = json.loads(gzip.decompress(base64.b64decode(encoded)).decode("utf-8")) + assert decoded["panels"] == [] + assert decoded["capabilities"] == {"protocol_version": 1} + assert "chestnut_active" in decoded + def test_stream_rejects_disallowed_port(self, mocker): from openpilot.sunnypilot.system.ble import stream as st h, phone = self.session(mocker)