params: auto decode based on type (#35794)

* type

* test

* more

* might as well use this

* one more

* live

* athena

* b

* also

* more

* now

* ah

* pigeon
This commit is contained in:
Maxime Desroches
2025-07-22 21:58:06 -07:00
committed by GitHub
parent dc1219d13f
commit bc5336d805
31 changed files with 93 additions and 92 deletions
+3 -2
View File
@@ -21,12 +21,13 @@ enum ParamKeyFlag {
};
enum ParamKeyType {
STRING = 0,
STRING = 0, // must be utf-8 decodable
BOOL = 1,
INT = 2,
FLOAT = 3,
TIME = 4, // ISO 8601
JSON = 5
JSON = 5,
BYTES = 6
};
struct ParamKeyAttributes {
+11 -11
View File
@@ -16,14 +16,14 @@ inline static std::unordered_map<std::string, ParamKeyAttributes> keys = {
{"AthenadUploadQueue", {PERSISTENT, JSON}},
{"AthenadRecentlyViewedRoutes", {PERSISTENT, STRING}},
{"BootCount", {PERSISTENT, INT}},
{"CalibrationParams", {PERSISTENT, STRING}},
{"CalibrationParams", {PERSISTENT, BYTES}},
{"CameraDebugExpGain", {CLEAR_ON_MANAGER_START, STRING}},
{"CameraDebugExpTime", {CLEAR_ON_MANAGER_START, STRING}},
{"CarBatteryCapacity", {PERSISTENT, INT}},
{"CarParams", {CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION, STRING}},
{"CarParamsCache", {CLEAR_ON_MANAGER_START, STRING}},
{"CarParamsPersistent", {PERSISTENT, STRING}},
{"CarParamsPrevRoute", {PERSISTENT, STRING}},
{"CarParams", {CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION, BYTES}},
{"CarParamsCache", {CLEAR_ON_MANAGER_START, BYTES}},
{"CarParamsPersistent", {PERSISTENT, BYTES}},
{"CarParamsPrevRoute", {PERSISTENT, BYTES}},
{"CompletedTrainingVersion", {PERSISTENT, STRING, "0"}},
{"ControlsReady", {CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION, BOOL}},
{"CurrentBootlog", {PERSISTENT, STRING}},
@@ -74,11 +74,11 @@ inline static std::unordered_map<std::string, ParamKeyAttributes> keys = {
{"LastPowerDropDetected", {CLEAR_ON_MANAGER_START, STRING}},
{"LastUpdateException", {CLEAR_ON_MANAGER_START, STRING}},
{"LastUpdateTime", {PERSISTENT, TIME}},
{"LiveDelay", {PERSISTENT, STRING}},
{"LiveParameters", {PERSISTENT, STRING}},
{"LiveParametersV2", {PERSISTENT, STRING}},
{"LiveTorqueParameters", {PERSISTENT | DONT_LOG, STRING}},
{"LocationFilterInitialState", {PERSISTENT, STRING}},
{"LiveDelay", {PERSISTENT, BYTES}},
{"LiveParameters", {PERSISTENT, BYTES}},
{"LiveParametersV2", {PERSISTENT, BYTES}},
{"LiveTorqueParameters", {PERSISTENT | DONT_LOG, BYTES}},
{"LocationFilterInitialState", {PERSISTENT, BYTES}},
{"LongitudinalManeuverMode", {CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION, BOOL}},
{"LongitudinalPersonality", {PERSISTENT, INT, std::to_string(static_cast<int>(cereal::LongitudinalPersonality::STANDARD))}},
{"NetworkMetered", {PERSISTENT, BOOL}},
@@ -99,7 +99,7 @@ inline static std::unordered_map<std::string, ParamKeyAttributes> keys = {
{"OpenpilotEnabledToggle", {PERSISTENT, BOOL, "1"}},
{"PandaHeartbeatLost", {CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION, BOOL}},
{"PandaSomResetTriggered", {CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION, BOOL}},
{"PandaSignatures", {CLEAR_ON_MANAGER_START, STRING}},
{"PandaSignatures", {CLEAR_ON_MANAGER_START, BYTES}},
{"PrimeType", {PERSISTENT, INT}},
{"RecordAudio", {PERSISTENT, BOOL}},
{"RecordFront", {PERSISTENT, BOOL}},
+21 -21
View File
@@ -23,6 +23,7 @@ cdef extern from "common/params.h":
FLOAT
TIME
JSON
BYTES
cdef cppclass c_Params "Params":
c_Params(string) except + nogil
@@ -72,26 +73,7 @@ cdef class Params:
raise UnknownKeyName(key)
return key
def cast(self, value, t, default):
try:
if t == STRING:
return value
elif t == BOOL:
return value == b"1"
elif t == INT:
return int(value)
elif t == FLOAT:
return float(value)
elif t == TIME:
return datetime.datetime.fromisoformat(value)
elif t == JSON:
return json.loads(value)
else:
return default
except (TypeError, ValueError):
return default
def get(self, key, bool block=False, encoding=None, default=None):
def get(self, key, bool block=False, default=None):
cdef string k = self.check_key(key)
cdef ParamKeyType t = self.p.getKeyType(ensure_bytes(key))
cdef string val
@@ -106,7 +88,25 @@ cdef class Params:
else:
return default
return self.cast(val if encoding is None else val.decode(encoding), t, default)
try:
if t == STRING:
return val.decode("utf-8")
elif t == BOOL:
return val == b"1"
elif t == INT:
return int(val)
elif t == FLOAT:
return float(val)
elif t == TIME:
return datetime.datetime.fromisoformat(val.decode("utf-8"))
elif t == JSON:
return json.loads(val)
elif t == BYTES:
return val
else:
return default
except (TypeError, ValueError):
return default
def get_bool(self, key, bool block=False):
cdef string k = self.check_key(key)
+8 -8
View File
@@ -14,7 +14,7 @@ class TestParams:
def test_params_put_and_get(self):
self.params.put("DongleId", "cb38263377b873ee")
assert self.params.get("DongleId") == b"cb38263377b873ee"
assert self.params.get("DongleId") == "cb38263377b873ee"
def test_params_non_ascii(self):
st = b"\xe1\x90\xff"
@@ -39,8 +39,8 @@ class TestParams:
def test_params_two_things(self):
self.params.put("DongleId", "bob")
self.params.put("AthenadPid", "123")
assert self.params.get("DongleId") == b"bob"
assert self.params.get("AthenadPid") == b"123"
assert self.params.get("DongleId") == "bob"
assert self.params.get("AthenadPid") == "123"
def test_params_get_block(self):
def _delayed_writer():
@@ -131,14 +131,14 @@ class TestParams:
# time
now = datetime.datetime.now(datetime.UTC)
self.params.put("InstallDate", str(now))
assert self.params.get("InstallDate", encoding="utf-8") == now
assert self.params.get("InstallDate") == now
def test_params_get_default(self):
now = datetime.datetime.now(datetime.UTC)
self.params.remove("InstallDate")
assert self.params.get("InstallDate", encoding="utf-8") is None
assert self.params.get("InstallDate", encoding="utf-8", default=now) == now
assert self.params.get("InstallDate") is None
assert self.params.get("InstallDate", default=now) == now
self.params.put("BootCount", "1xx1")
assert self.params.get("BootCount", encoding="utf-8") is None
assert self.params.get("BootCount", encoding="utf-8", default=1441) == 1441
assert self.params.get("BootCount") is None
assert self.params.get("BootCount", default=1441) == 1441