From 5fb85ede56885022c40d3a460b8f74afbf458f66 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 10 Jun 2020 04:46:49 +0800 Subject: [PATCH] add function read_db_bytes (#1662) old-commit-hash: 90d97de74ddc4672b35a5186e868794468a44345 --- selfdrive/boardd/boardd.cc | 35 +++++++----------- selfdrive/common/params.cc | 13 +++++++ selfdrive/common/params.h | 8 ++--- selfdrive/locationd/paramsd.cc | 27 ++++++-------- selfdrive/loggerd/loggerd.cc | 58 ++++++++---------------------- selfdrive/modeld/models/driving.cc | 8 ++--- 6 files changed, 56 insertions(+), 93 deletions(-) diff --git a/selfdrive/boardd/boardd.cc b/selfdrive/boardd/boardd.cc index bc6dcea457..f1f87cb8db 100644 --- a/selfdrive/boardd/boardd.cc +++ b/selfdrive/boardd/boardd.cc @@ -85,45 +85,39 @@ void *safety_setter_thread(void *s) { libusb_control_transfer(dev_handle, 0x40, 0xdc, (uint16_t)(cereal::CarParams::SafetyModel::ELM327), 0, NULL, 0, TIMEOUT); pthread_mutex_unlock(&usb_lock); - char *value_vin; - size_t value_vin_sz = 0; - // switch to SILENT when CarVin param is read while (1) { if (do_exit) return NULL; - const int result = read_db_value("CarVin", &value_vin, &value_vin_sz); - if (value_vin_sz > 0) { + std::vector value_vin = read_db_bytes("CarVin"); + if (value_vin.size() > 0) { // sanity check VIN format - assert(value_vin_sz == 17); + assert(value_vin.size() == 17); + std::string str_vin(value_vin.begin(), value_vin.end()); + LOGW("got CarVin %s", str_vin.c_str()); break; } usleep(100*1000); } - LOGW("got CarVin %s", value_vin); - free(value_vin); // VIN query done, stop listening to OBDII pthread_mutex_lock(&usb_lock); libusb_control_transfer(dev_handle, 0x40, 0xdc, (uint16_t)(cereal::CarParams::SafetyModel::NO_OUTPUT), 0, NULL, 0, TIMEOUT); pthread_mutex_unlock(&usb_lock); - char *value; - size_t value_sz = 0; - + std::vector params; LOGW("waiting for params to set safety model"); while (1) { if (do_exit) return NULL; - const int result = read_db_value("CarParams", &value, &value_sz); - if (value_sz > 0) break; + params = read_db_bytes("CarParams"); + if (params.size() > 0) break; usleep(100*1000); } - LOGW("got %d bytes CarParams", value_sz); + LOGW("got %d bytes CarParams", params.size()); // format for board, make copy due to alignment issues, will be freed on out of scope - auto amsg = kj::heapArray((value_sz / sizeof(capnp::word)) + 1); - memcpy(amsg.begin(), value, value_sz); - free(value); + auto amsg = kj::heapArray((params.size() / sizeof(capnp::word)) + 1); + memcpy(amsg.begin(), params.data(), params.size()); capnp::FlatArrayMessageReader cmsg(amsg); cereal::CarParams::Reader car_params = cmsg.getRoot(); @@ -407,10 +401,8 @@ void can_health(PubMaster &pm) { bool cdp_mode = health.usb_power_mode == (uint8_t)(cereal::HealthData::UsbPowerMode::CDP); bool no_ignition_exp = no_ignition_cnt > NO_IGNITION_CNT_MAX; if ((no_ignition_exp || (voltage_f < VBATT_PAUSE_CHARGING)) && cdp_mode && !ignition) { - char *disable_power_down = NULL; - size_t disable_power_down_sz = 0; - const int result = read_db_value("DisablePowerDown", &disable_power_down, &disable_power_down_sz); - if (disable_power_down_sz != 1 || disable_power_down[0] != '1') { + std::vector disable_power_down = read_db_bytes("DisablePowerDown"); + if (disable_power_down.size() != 1 || disable_power_down[0] != '1') { printf("TURN OFF CHARGING!\n"); pthread_mutex_lock(&usb_lock); libusb_control_transfer(dev_handle, 0xc0, 0xe6, (uint16_t)(cereal::HealthData::UsbPowerMode::CLIENT), 0, NULL, 0, TIMEOUT); @@ -418,7 +410,6 @@ void can_health(PubMaster &pm) { printf("POWER DOWN DEVICE\n"); system("service call power 17 i32 0 i32 1"); } - if (disable_power_down) free(disable_power_down); } if (!no_ignition_exp && (voltage_f > VBATT_START_CHARGING) && !cdp_mode) { printf("TURN ON CHARGING!\n"); diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 5ab2eae349..4d9c214d37 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -13,6 +13,7 @@ #include #include +#include #include "common/util.h" #include "common/utilpp.h" @@ -353,3 +354,15 @@ int read_db_all(std::map *params, bool persistent_para close(lock_fd); return 0; } + +std::vector read_db_bytes(const char* param_name, bool persistent_param) { + std::vector bytes; + char* value; + size_t sz; + int result = read_db_value(param_name, &value, &sz, persistent_param); + if (result == 0) { + bytes.assign(value, value+sz); + free(value); + } + return bytes; +} diff --git a/selfdrive/common/params.h b/selfdrive/common/params.h index ea002eb0de..49af04b4fd 100644 --- a/selfdrive/common/params.h +++ b/selfdrive/common/params.h @@ -1,6 +1,4 @@ -#ifndef _SELFDRIVE_COMMON_PARAMS_H_ -#define _SELFDRIVE_COMMON_PARAMS_H_ - +#pragma once #include #ifdef __cplusplus @@ -39,7 +37,7 @@ void read_db_value_blocking(const char* key, char** value, size_t* value_sz, boo #ifdef __cplusplus #include #include +#include int read_db_all(std::map *params, bool persistent_param = false); +std::vector read_db_bytes(const char* param_name, bool persistent_param = false); #endif - -#endif // _SELFDRIVE_COMMON_PARAMS_H_ diff --git a/selfdrive/locationd/paramsd.cc b/selfdrive/locationd/paramsd.cc index 3841ccc5bb..bb45ea2b4b 100644 --- a/selfdrive/locationd/paramsd.cc +++ b/selfdrive/locationd/paramsd.cc @@ -31,40 +31,33 @@ int main(int argc, char *argv[]) { Localizer localizer; // Read car params - char *value; - size_t value_sz = 0; - + std::vector params; LOGW("waiting for params to set vehicle model"); while (true) { - read_db_value("CarParams", &value, &value_sz); - if (value_sz > 0) break; + params = read_db_bytes("CarParams"); + if (params.size() > 0) break; usleep(100*1000); } - LOGW("got %d bytes CarParams", value_sz); + LOGW("got %d bytes CarParams", params.size()); // make copy due to alignment issues - auto amsg = kj::heapArray((value_sz / sizeof(capnp::word)) + 1); - memcpy(amsg.begin(), value, value_sz); - free(value); - + auto amsg = kj::heapArray((params.size() / sizeof(capnp::word)) + 1); + memcpy(amsg.begin(), params.data(), params.size()); + capnp::FlatArrayMessageReader cmsg(amsg); cereal::CarParams::Reader car_params = cmsg.getRoot(); // Read params from previous run - const int result = read_db_value("LiveParameters", &value, &value_sz); - std::string fingerprint = car_params.getCarFingerprint(); std::string vin = car_params.getCarVin(); double sR = car_params.getSteerRatio(); double x = 1.0; double ao = 0.0; double posenet_invalid_count = 0; - - if (result == 0){ - auto str = std::string(value, value_sz); - free(value); - + std::vector live_params = read_db_bytes("LiveParameters"); + if (live_params.size() > 0){ std::string err; + std::string str(live_params.begin(), live_params.end()); auto json = json11::Json::parse(str, err); if (json.is_null() || !err.empty()) { std::string log = "Error parsing json: " + err; diff --git a/selfdrive/loggerd/loggerd.cc b/selfdrive/loggerd/loggerd.cc index d9c5017f95..cb2e5d4753 100644 --- a/selfdrive/loggerd/loggerd.cc +++ b/selfdrive/loggerd/loggerd.cc @@ -91,11 +91,8 @@ void encoder_thread(bool is_streaming, bool raw_clips, bool front) { int err; if (front) { - char *value; - const int result = read_db_value("RecordFront", &value, NULL); - if (result != 0) return; - if (value[0] != '1') { free(value); return; } - free(value); + std::vector value = read_db_bytes("RecordFront"); + if (value.size() == 0 || value[0] != '1') return; LOGW("recording front camera"); set_thread_name("FrontCameraEncoder"); @@ -454,30 +451,23 @@ kj::Array gen_init_data() { init.setDirty(true); } - char* git_commit = NULL; - size_t size; - read_db_value("GitCommit", &git_commit, &size); - if (git_commit) { - init.setGitCommit(capnp::Text::Reader(git_commit, size)); + std::vector git_commit = read_db_bytes("GitCommit"); + if (git_commit.size() > 0) { + init.setGitCommit(capnp::Text::Reader(git_commit.data(), git_commit.size())); } - char* git_branch = NULL; - read_db_value("GitBranch", &git_branch, &size); - if (git_branch) { - init.setGitBranch(capnp::Text::Reader(git_branch, size)); + std::vector git_branch = read_db_bytes("GitBranch"); + if (git_branch.size() > 0) { + init.setGitBranch(capnp::Text::Reader(git_branch.data(), git_branch.size())); } - char* git_remote = NULL; - read_db_value("GitRemote", &git_remote, &size); - if (git_remote) { - init.setGitRemote(capnp::Text::Reader(git_remote, size)); + std::vector git_remote = read_db_bytes("GitRemote"); + if (git_remote.size() > 0) { + init.setGitRemote(capnp::Text::Reader(git_remote.data(), git_remote.size())); } - char* passive = NULL; - read_db_value("Passive", &passive, NULL); - init.setPassive(passive && strlen(passive) && passive[0] == '1'); - - + std::vector passive = read_db_bytes("Passive"); + init.setPassive(passive.size() > 0 && passive[0] == '1'); { // log params std::map params; @@ -491,27 +481,7 @@ kj::Array gen_init_data() { i++; } } - - - auto words = capnp::messageToFlatArray(msg); - - if (git_commit) { - free((void*)git_commit); - } - - if (git_branch) { - free((void*)git_branch); - } - - if (git_remote) { - free((void*)git_remote); - } - - if (passive) { - free((void*)passive); - } - - return words; + return capnp::messageToFlatArray(msg); } static int clear_locks_fn(const char* fpath, const struct stat *sb, int tyupeflag) { diff --git a/selfdrive/modeld/models/driving.cc b/selfdrive/modeld/models/driving.cc index 1940667d80..b24762ebf0 100644 --- a/selfdrive/modeld/models/driving.cc +++ b/selfdrive/modeld/models/driving.cc @@ -60,11 +60,9 @@ void model_init(ModelState* s, cl_device_id device_id, cl_context context, int t for (int i = 0; i < TRAFFIC_CONVENTION_LEN; i++) s->traffic_convention[i] = 0.0; s->m->addTrafficConvention(s->traffic_convention, TRAFFIC_CONVENTION_LEN); - char *string; - const int result = read_db_value("IsRHD", &string, NULL); - if (result == 0) { - bool is_rhd = string[0] == '1'; - free(string); + std::vector result = read_db_bytes("IsRHD"); + if (result.size() > 0) { + bool is_rhd = result[0] == '1'; if (is_rhd) { s->traffic_convention[1] = 1.0; } else {