mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-08-20 05:03:45 +08:00
9fb9b73620
# Conflicts: # README.md # SConstruct # conftest.py # docs/CARS.md # msgq_repo # opendbc_repo # openpilot/common/params_keys.h # openpilot/common/params_pyx.pyx # openpilot/common/tests/test_swaglog.cc # openpilot/selfdrive/car/card.py # openpilot/selfdrive/car/tests/test_car_interfaces.py # openpilot/selfdrive/car/tests/test_cruise_speed.py # openpilot/selfdrive/car/tests/test_models.py # openpilot/selfdrive/controls/controlsd.py # openpilot/selfdrive/controls/lib/latcontrol_torque.py # openpilot/selfdrive/controls/lib/longitudinal_planner.py # openpilot/selfdrive/controls/plannerd.py # openpilot/selfdrive/controls/radard.py # openpilot/selfdrive/controls/tests/test_longcontrol.py # openpilot/selfdrive/locationd/torqued.py # openpilot/selfdrive/modeld/modeld.py # openpilot/selfdrive/monitoring/dmonitoringd.py # openpilot/selfdrive/monitoring/test_monitoring.py # openpilot/selfdrive/selfdrived/selfdrived.py # openpilot/selfdrive/selfdrived/tests/test_alertmanager.py # openpilot/selfdrive/test/longitudinal_maneuvers/plant.py # openpilot/selfdrive/test/process_replay/migration.py # openpilot/selfdrive/test/process_replay/process_replay.py # openpilot/selfdrive/ui/feedback/feedbackd.py # openpilot/selfdrive/ui/layouts/settings/device.py # openpilot/selfdrive/ui/layouts/settings/toggles.py # openpilot/selfdrive/ui/mici/layouts/onboarding.py # openpilot/selfdrive/ui/onroad/augmented_road_view.py # openpilot/selfdrive/ui/tests/test_soundd.py # openpilot/selfdrive/ui/translations/app.pot # openpilot/selfdrive/ui/translations/app_de.po # openpilot/selfdrive/ui/translations/app_en.po # openpilot/selfdrive/ui/translations/app_es.po # openpilot/selfdrive/ui/translations/app_fr.po # openpilot/selfdrive/ui/translations/app_ja.po # openpilot/selfdrive/ui/translations/app_ko.po # openpilot/selfdrive/ui/translations/app_pt-BR.po # openpilot/selfdrive/ui/translations/app_th.po # openpilot/selfdrive/ui/translations/app_tr.po # openpilot/selfdrive/ui/translations/app_uk.po # openpilot/selfdrive/ui/translations/app_zh-CHS.po # openpilot/selfdrive/ui/translations/app_zh-CHT.po # openpilot/system/athena/athenad.py # openpilot/system/hardware/hardwared.py # openpilot/system/loggerd/deleter.py # openpilot/system/manager/process_config.py # openpilot/system/ui/lib/application.py # panda # pyproject.toml # tinygrad_repo # uv.lock
175 lines
4.6 KiB
C++
175 lines
4.6 KiB
C++
#include <cstddef>
|
|
#include <cstdio>
|
|
#include <exception>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "common/params.h"
|
|
|
|
typedef struct {
|
|
const char *data;
|
|
size_t size;
|
|
} ParamsBuffer;
|
|
|
|
struct ParamsHandle {
|
|
ParamsHandle(const char *path, size_t path_size) : params(std::string(path, path_size)), keys(params.allKeys()) {
|
|
}
|
|
|
|
Params params;
|
|
const std::vector<std::string> keys;
|
|
};
|
|
|
|
namespace {
|
|
thread_local char last_error[512] = {};
|
|
thread_local std::string result;
|
|
|
|
void set_error(const char *error) {
|
|
snprintf(last_error, sizeof(last_error), "%s", error);
|
|
}
|
|
|
|
ParamsBuffer return_string(std::string value) {
|
|
result = std::move(value);
|
|
return {result.data(), result.size()};
|
|
}
|
|
|
|
template <typename Result, typename Callable>
|
|
Result translate_exceptions(Result failure, Callable &&callable) noexcept {
|
|
last_error[0] = '\0';
|
|
try {
|
|
return callable();
|
|
} catch (const std::exception &e) {
|
|
set_error(e.what());
|
|
} catch (...) {
|
|
set_error("unknown C++ exception");
|
|
}
|
|
return failure;
|
|
}
|
|
|
|
template <typename Callable>
|
|
void translate_exceptions(Callable &&callable) noexcept {
|
|
translate_exceptions(false, [&]() {
|
|
callable();
|
|
return true;
|
|
});
|
|
}
|
|
} // namespace
|
|
|
|
extern "C" {
|
|
|
|
ParamsHandle *params_create(const char *path, size_t path_size) noexcept {
|
|
return translate_exceptions(static_cast<ParamsHandle *>(nullptr), [&]() {
|
|
return new ParamsHandle(path, path_size);
|
|
});
|
|
}
|
|
|
|
void params_destroy(ParamsHandle *handle) noexcept {
|
|
translate_exceptions([&]() {
|
|
delete handle;
|
|
});
|
|
}
|
|
|
|
const char *params_last_error() noexcept {
|
|
return last_error;
|
|
}
|
|
|
|
void params_clear_all(ParamsHandle *handle, unsigned int flag) noexcept {
|
|
translate_exceptions([&]() {
|
|
handle->params.clearAll(static_cast<ParamKeyFlag>(flag));
|
|
});
|
|
}
|
|
|
|
bool params_check_key(ParamsHandle *handle, const char *key) noexcept {
|
|
return translate_exceptions(false, [&]() {
|
|
return handle->params.checkKey(key);
|
|
});
|
|
}
|
|
|
|
int params_get_key_type(ParamsHandle *handle, const char *key) noexcept {
|
|
return translate_exceptions(-1, [&]() {
|
|
return static_cast<int>(handle->params.getKeyType(key));
|
|
});
|
|
}
|
|
|
|
ParamsBuffer params_get_default(ParamsHandle *handle, const char *key) noexcept {
|
|
return translate_exceptions(ParamsBuffer{nullptr, 0}, [&]() {
|
|
auto value = handle->params.getKeyDefaultValue(key);
|
|
if (!value.has_value()) {
|
|
return ParamsBuffer{nullptr, 0};
|
|
}
|
|
return return_string(*value);
|
|
});
|
|
}
|
|
|
|
ParamsBuffer params_get(ParamsHandle *handle, const char *key, bool block) noexcept {
|
|
return translate_exceptions(ParamsBuffer{nullptr, 0}, [&]() {
|
|
return return_string(handle->params.get(key, block));
|
|
});
|
|
}
|
|
|
|
bool params_get_bool(ParamsHandle *handle, const char *key, bool block) noexcept {
|
|
return translate_exceptions(false, [&]() {
|
|
return handle->params.getBool(key, block);
|
|
});
|
|
}
|
|
|
|
int params_put(ParamsHandle *handle, const char *key, const char *value, size_t size, bool block) noexcept {
|
|
return translate_exceptions(-1, [&]() {
|
|
if (block) {
|
|
return handle->params.put(key, value, size);
|
|
}
|
|
handle->params.putNonBlocking(key, std::string(value, size));
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
int params_put_bool(ParamsHandle *handle, const char *key, bool value, bool block) noexcept {
|
|
return translate_exceptions(-1, [&]() {
|
|
if (block) {
|
|
return handle->params.putBool(key, value);
|
|
}
|
|
handle->params.putBoolNonBlocking(key, value);
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
int params_remove(ParamsHandle *handle, const char *key) noexcept {
|
|
return translate_exceptions(-1, [&]() {
|
|
return handle->params.remove(key);
|
|
});
|
|
}
|
|
|
|
ParamsBuffer params_get_path(ParamsHandle *handle, const char *key, size_t key_size) noexcept {
|
|
return translate_exceptions(ParamsBuffer{nullptr, 0}, [&]() {
|
|
return return_string(handle->params.getParamPath(std::string(key, key_size)));
|
|
});
|
|
}
|
|
|
|
size_t params_keys_size(ParamsHandle *handle) noexcept {
|
|
return translate_exceptions(size_t{0}, [&]() {
|
|
return handle->keys.size();
|
|
});
|
|
}
|
|
|
|
ParamsBuffer params_key_at(ParamsHandle *handle, size_t index) noexcept {
|
|
return translate_exceptions(ParamsBuffer{nullptr, 0}, [&]() {
|
|
if (index >= handle->keys.size()) {
|
|
return ParamsBuffer{nullptr, 0};
|
|
}
|
|
return return_string(handle->keys[index]);
|
|
});
|
|
}
|
|
|
|
size_t params_keys_by_flag(ParamsHandle *handle, uint32_t flag, ParamsBuffer *out, size_t out_size) noexcept {
|
|
return translate_exceptions(size_t{0}, [&]() {
|
|
auto filtered = handle->params.allKeys(static_cast<ParamKeyFlag>(flag));
|
|
size_t count = std::min(filtered.size(), out_size);
|
|
for (size_t i = 0; i < count; i++) {
|
|
out[i] = return_string(filtered[i]);
|
|
}
|
|
return filtered.size();
|
|
});
|
|
}
|
|
|
|
} // extern "C"
|