diff --git a/common/params.cc b/common/params.cc index 8ff5fc539d..7b2d4490ea 100644 --- a/common/params.cc +++ b/common/params.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -203,6 +204,14 @@ Params::Params(const std::string &path) { params_path = path.empty() ? default_param_path : ensure_params_path(prefix, path); } +std::vector Params::allKeys() const { + std::vector ret; + for (auto &p : keys) { + ret.push_back(p.first); + } + return ret; +} + bool Params::checkKey(const std::string &key) { return keys.find(key) != keys.end(); } diff --git a/common/params.h b/common/params.h index aa4b1d7af3..7758a015f6 100644 --- a/common/params.h +++ b/common/params.h @@ -2,6 +2,7 @@ #include #include +#include enum ParamKeyType { PERSISTENT = 0x02, @@ -15,6 +16,7 @@ enum ParamKeyType { class Params { public: Params(const std::string &path = {}); + std::vector allKeys() const; bool checkKey(const std::string &key); ParamKeyType getKeyType(const std::string &key); inline std::string getParamPath(const std::string &key = {}) { diff --git a/common/params_pyx.pyx b/common/params_pyx.pyx index 8d52b8d3f6..bbddda46ea 100755 --- a/common/params_pyx.pyx +++ b/common/params_pyx.pyx @@ -2,6 +2,7 @@ # cython: language_level = 3 from libcpp cimport bool from libcpp.string cimport string +from libcpp.vector cimport vector import threading cdef extern from "common/params.h": @@ -22,6 +23,7 @@ cdef extern from "common/params.h": bool checkKey(string) nogil string getParamPath(string) nogil void clearAll(ParamKeyType) + vector[string] allKeys() def ensure_bytes(v): @@ -99,6 +101,9 @@ cdef class Params: cdef string key_bytes = ensure_bytes(key) return self.p.getParamPath(key_bytes).decode("utf-8") + def all_keys(self): + return self.p.allKeys() + def put_nonblocking(key, val, d=""): threading.Thread(target=lambda: Params(d).put(key, val)).start() diff --git a/common/tests/test_params.py b/common/tests/test_params.py index 899a47fe34..ec13e82217 100644 --- a/common/tests/test_params.py +++ b/common/tests/test_params.py @@ -98,6 +98,14 @@ class TestParams(unittest.TestCase): assert q.get("CarParams") is None assert q.get("CarParams", True) == b"1" + def test_params_all_keys(self): + keys = Params().all_keys() + + # sanity checks + assert len(keys) > 20 + assert len(keys) == len(set(keys)) + assert b"CarParams" in keys + if __name__ == "__main__": unittest.main()