add function read_db_bytes (#1662)

old-commit-hash: 90d97de74d
This commit is contained in:
Dean Lee 2020-06-10 04:46:49 +08:00 committed by GitHub
parent ea0292d86e
commit 5fb85ede56
6 changed files with 56 additions and 93 deletions

View File

@ -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<char> 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<char> 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<capnp::word>((value_sz / sizeof(capnp::word)) + 1);
memcpy(amsg.begin(), value, value_sz);
free(value);
auto amsg = kj::heapArray<capnp::word>((params.size() / sizeof(capnp::word)) + 1);
memcpy(amsg.begin(), params.data(), params.size());
capnp::FlatArrayMessageReader cmsg(amsg);
cereal::CarParams::Reader car_params = cmsg.getRoot<cereal::CarParams>();
@ -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<char> 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");

View File

@ -13,6 +13,7 @@
#include <map>
#include <string>
#include <string.h>
#include "common/util.h"
#include "common/utilpp.h"
@ -353,3 +354,15 @@ int read_db_all(std::map<std::string, std::string> *params, bool persistent_para
close(lock_fd);
return 0;
}
std::vector<char> read_db_bytes(const char* param_name, bool persistent_param) {
std::vector<char> 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;
}

View File

@ -1,6 +1,4 @@
#ifndef _SELFDRIVE_COMMON_PARAMS_H_
#define _SELFDRIVE_COMMON_PARAMS_H_
#pragma once
#include <stddef.h>
#ifdef __cplusplus
@ -39,7 +37,7 @@ void read_db_value_blocking(const char* key, char** value, size_t* value_sz, boo
#ifdef __cplusplus
#include <map>
#include <string>
#include <vector>
int read_db_all(std::map<std::string, std::string> *params, bool persistent_param = false);
std::vector<char> read_db_bytes(const char* param_name, bool persistent_param = false);
#endif
#endif // _SELFDRIVE_COMMON_PARAMS_H_

View File

@ -31,40 +31,33 @@ int main(int argc, char *argv[]) {
Localizer localizer;
// Read car params
char *value;
size_t value_sz = 0;
std::vector<char> 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<capnp::word>((value_sz / sizeof(capnp::word)) + 1);
memcpy(amsg.begin(), value, value_sz);
free(value);
auto amsg = kj::heapArray<capnp::word>((params.size() / sizeof(capnp::word)) + 1);
memcpy(amsg.begin(), params.data(), params.size());
capnp::FlatArrayMessageReader cmsg(amsg);
cereal::CarParams::Reader car_params = cmsg.getRoot<cereal::CarParams>();
// 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<char> 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;

View File

@ -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<char> 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<capnp::word> 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<char> 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<char> 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<char> 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<char> passive = read_db_bytes("Passive");
init.setPassive(passive.size() > 0 && passive[0] == '1');
{
// log params
std::map<std::string, std::string> params;
@ -491,27 +481,7 @@ kj::Array<capnp::word> 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) {

View File

@ -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<char> 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 {