add params.metadata BLE RPC and raise session cap to 64 KiB

This commit is contained in:
nayan
2026-11-29 14:10:33 -05:00
parent 0637e1f6f9
commit e67f48ddb9
3 changed files with 44 additions and 2 deletions
+1 -1
View File
@@ -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"
+27
View File
@@ -4,6 +4,8 @@ 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
@@ -90,6 +92,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 +172,29 @@ class Rpc:
raise _refused(f"bad value for {key}") from e
return {"ok": list(values)}
def _params_metadata(self) -> dict:
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
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:
@@ -622,9 +622,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)