Skip to content

Commit 267acfb

Browse files
Revert "params: unique default value (#35798)"
This reverts commit abd657e.
1 parent 347cee4 commit 267acfb

File tree

11 files changed

+55
-55
lines changed

11 files changed

+55
-55
lines changed

common/params.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ ParamKeyType Params::getKeyType(const std::string &key) {
123123
return keys[key].type;
124124
}
125125

126-
std::optional<std::string> Params::getKeyDefaultValue(const std::string &key) {
126+
std::string Params::getKeyDefaultValue(const std::string &key) {
127127
return keys[key].default_value;
128128
}
129129

common/params.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <future>
44
#include <map>
5-
#include <optional>
65
#include <string>
76
#include <tuple>
87
#include <utility>
@@ -34,7 +33,7 @@ enum ParamKeyType {
3433
struct ParamKeyAttributes {
3534
uint32_t flags;
3635
ParamKeyType type;
37-
std::optional<std::string> default_value = std::nullopt;
36+
std::string default_value = "";
3837
};
3938

4039
class Params {
@@ -49,7 +48,7 @@ class Params {
4948
bool checkKey(const std::string &key);
5049
ParamKeyFlag getKeyFlag(const std::string &key);
5150
ParamKeyType getKeyType(const std::string &key);
52-
std::optional<std::string> getKeyDefaultValue(const std::string &key);
51+
std::string getKeyDefaultValue(const std::string &key);
5352
inline std::string getParamPath(const std::string &key = {}) {
5453
return params_path + params_prefix + (key.empty() ? "" : "/" + key);
5554
}

common/params_keys.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ inline static std::unordered_map<std::string, ParamKeyAttributes> keys = {
1919
{"CalibrationParams", {PERSISTENT, BYTES}},
2020
{"CameraDebugExpGain", {CLEAR_ON_MANAGER_START, STRING}},
2121
{"CameraDebugExpTime", {CLEAR_ON_MANAGER_START, STRING}},
22-
{"CarBatteryCapacity", {PERSISTENT, INT, "0"}},
22+
{"CarBatteryCapacity", {PERSISTENT, INT}},
2323
{"CarParams", {CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION, BYTES}},
2424
{"CarParamsCache", {CLEAR_ON_MANAGER_START, BYTES}},
2525
{"CarParamsPersistent", {PERSISTENT, BYTES}},

common/params_pyx.pyx

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import json
55
from libcpp cimport bool
66
from libcpp.string cimport string
77
from libcpp.vector cimport vector
8-
from libcpp.optional cimport optional
98

109
cdef extern from "common/params.h":
1110
cpdef enum ParamKeyFlag:
@@ -37,7 +36,7 @@ cdef extern from "common/params.h":
3736
int putBool(string, bool) nogil
3837
bool checkKey(string) nogil
3938
ParamKeyType getKeyType(string) nogil
40-
optional[string] getKeyDefaultValue(string) nogil
39+
string getKeyDefaultValue(string) nogil
4140
string getParamPath(string) nogil
4241
void clearAll(ParamKeyFlag)
4342
vector[string] allKeys()
@@ -74,45 +73,40 @@ cdef class Params:
7473
raise UnknownKeyName(key)
7574
return key
7675

77-
def cast(self, t, value, default):
78-
if value is None:
79-
return None
80-
try:
81-
if t == STRING:
82-
return value.decode("utf-8")
83-
elif t == BOOL:
84-
return value == b"1"
85-
elif t == INT:
86-
return int(value)
87-
elif t == FLOAT:
88-
return float(value)
89-
elif t == TIME:
90-
return datetime.datetime.fromisoformat(value.decode("utf-8"))
91-
elif t == JSON:
92-
return json.loads(value)
93-
elif t == BYTES:
94-
return value
95-
else:
96-
raise TypeError()
97-
except (TypeError, ValueError):
98-
return self.cast(t, default, None)
99-
100-
def get(self, key, bool block=False):
76+
def get(self, key, bool block=False, default=None):
10177
cdef string k = self.check_key(key)
102-
cdef ParamKeyType t = self.p.getKeyType(k)
78+
cdef ParamKeyType t = self.p.getKeyType(ensure_bytes(key))
10379
cdef string val
10480
with nogil:
10581
val = self.p.get(k, block)
10682

107-
default = self.get_default_value(k)
10883
if val == b"":
10984
if block:
11085
# If we got no value while running in blocked mode
11186
# it means we got an interrupt while waiting
11287
raise KeyboardInterrupt
11388
else:
114-
return self.cast(t, default, None)
115-
return self.cast(t, val, default)
89+
return default
90+
91+
try:
92+
if t == STRING:
93+
return val.decode("utf-8")
94+
elif t == BOOL:
95+
return val == b"1"
96+
elif t == INT:
97+
return int(val)
98+
elif t == FLOAT:
99+
return float(val)
100+
elif t == TIME:
101+
return datetime.datetime.fromisoformat(val.decode("utf-8"))
102+
elif t == JSON:
103+
return json.loads(val)
104+
elif t == BYTES:
105+
return val
106+
else:
107+
return default
108+
except (TypeError, ValueError):
109+
return default
116110

117111
def get_bool(self, key, bool block=False):
118112
cdef string k = self.check_key(key)
@@ -162,5 +156,4 @@ cdef class Params:
162156
return self.p.allKeys()
163157

164158
def get_default_value(self, key):
165-
cdef optional[string] default = self.p.getKeyDefaultValue(self.check_key(key))
166-
return default.value() if default.has_value() else None
159+
return self.p.getKeyDefaultValue(self.check_key(key))

common/tests/test_params.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,10 @@ def test_params_all_keys(self):
110110
assert len(keys) == len(set(keys))
111111
assert b"CarParams" in keys
112112

113-
def test_params_default_value(self):
114-
self.params.remove("LanguageSetting")
115-
self.params.remove("LongitudinalPersonality")
116-
self.params.remove("LiveParameters")
117-
118-
assert isinstance(self.params.get("LanguageSetting"), str)
119-
assert isinstance(self.params.get("LongitudinalPersonality"), int)
120-
assert self.params.get("LiveParameters") is None
113+
def test_params_default_init_value(self):
114+
assert self.params.get_default_value("LanguageSetting")
115+
assert self.params.get_default_value("LongitudinalPersonality")
116+
assert not self.params.get_default_value("LiveParameters")
121117

122118
def test_params_get_type(self):
123119
# json
@@ -131,9 +127,18 @@ def test_params_get_type(self):
131127
# bool
132128
self.params.put("AdbEnabled", "1")
133129
assert self.params.get("AdbEnabled")
134-
assert isinstance(self.params.get("AdbEnabled"), bool)
135130

136131
# time
137132
now = datetime.datetime.now(datetime.UTC)
138133
self.params.put("InstallDate", str(now))
139134
assert self.params.get("InstallDate") == now
135+
136+
def test_params_get_default(self):
137+
now = datetime.datetime.now(datetime.UTC)
138+
self.params.remove("InstallDate")
139+
assert self.params.get("InstallDate") is None
140+
assert self.params.get("InstallDate", default=now) == now
141+
142+
self.params.put("BootCount", "1xx1")
143+
assert self.params.get("BootCount") is None
144+
assert self.params.get("BootCount", default=1441) == 1441

selfdrive/selfdrived/selfdrived.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __init__(self, CP=None):
109109
self.logged_comm_issue = None
110110
self.not_running_prev = None
111111
self.experimental_mode = False
112-
self.personality = self.params.get('LongitudinalPersonality')
112+
self.personality = self.read_personality_param()
113113
self.recalibrating_seen = False
114114
self.state_machine = StateMachine()
115115
self.rk = Ratekeeper(100, print_delay_threshold=None)
@@ -477,13 +477,16 @@ def step(self):
477477

478478
self.CS_prev = CS
479479

480+
def read_personality_param(self):
481+
return self.params.get('LongitudinalPersonality', default=log.LongitudinalPersonality.standard)
482+
480483
def params_thread(self, evt):
481484
while not evt.is_set():
482485
self.is_metric = self.params.get_bool("IsMetric")
483486
self.is_ldw_enabled = self.params.get_bool("IsLdwEnabled")
484487
self.disengage_on_accelerator = self.params.get_bool("DisengageOnAccelerator")
485488
self.experimental_mode = self.params.get_bool("ExperimentalMode") and self.CP.openpilotLongitudinalControl
486-
self.personality = self.params.get('LongitudinalPersonality')
489+
self.personality = self.read_personality_param()
487490
time.sleep(0.1)
488491

489492
def run(self):

selfdrive/ui/layouts/settings/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def __init__(self):
4141
self._scroller = Scroller(items, line_separator=True, spacing=0)
4242

4343
def _initialize_items(self):
44-
dongle_id = self._params.get("DongleId") or "N/A"
45-
serial = self._params.get("HardwareSerial") or "N/A"
44+
dongle_id = self._params.get("DongleId", default="N/A")
45+
serial = self._params.get("HardwareSerial", default="N/A")
4646

4747
items = [
4848
text_item("Dongle ID", dongle_id),

selfdrive/ui/layouts/settings/toggles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self):
5454
buttons=["Aggressive", "Standard", "Relaxed"],
5555
button_width=255,
5656
callback=self._set_longitudinal_personality,
57-
selected_index=self._params.get("LongitudinalPersonality"),
57+
selected_index=self._params.get("LongitudinalPersonality", default=0),
5858
icon="speed_limit.png"
5959
),
6060
toggle_item(

selfdrive/ui/widgets/pairing_dialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self):
2323

2424
def _get_pairing_url(self) -> str:
2525
try:
26-
dongle_id = self.params.get("DongleId") or ""
26+
dongle_id = self.params.get("DongleId", default="")
2727
token = Api(dongle_id).get_token()
2828
except Exception as e:
2929
cloudlog.warning(f"Failed to get pairing token: {e}")

system/athena/athenad.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,12 @@ def getPublicKey() -> str | None:
532532

533533
@dispatcher.add_method
534534
def getSshAuthorizedKeys() -> str:
535-
return cast(str, Params().get("GithubSshKeys") or "")
535+
return cast(str, Params().get("GithubSshKeys", default=""))
536536

537537

538538
@dispatcher.add_method
539539
def getGithubUsername() -> str:
540-
return cast(str, Params().get("GithubUsername") or "")
540+
return cast(str, Params().get("GithubUsername", default=""))
541541

542542
@dispatcher.add_method
543543
def getSimInfo():

0 commit comments

Comments
 (0)