Skip to content

Commit 5fb85ed

Browse files
authored
add function read_db_bytes (#1662)
old-commit-hash: 90d97de
1 parent ea0292d commit 5fb85ed

File tree

6 files changed

+56
-93
lines changed

6 files changed

+56
-93
lines changed

selfdrive/boardd/boardd.cc

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,45 +85,39 @@ void *safety_setter_thread(void *s) {
8585
libusb_control_transfer(dev_handle, 0x40, 0xdc, (uint16_t)(cereal::CarParams::SafetyModel::ELM327), 0, NULL, 0, TIMEOUT);
8686
pthread_mutex_unlock(&usb_lock);
8787

88-
char *value_vin;
89-
size_t value_vin_sz = 0;
90-
9188
// switch to SILENT when CarVin param is read
9289
while (1) {
9390
if (do_exit) return NULL;
94-
const int result = read_db_value("CarVin", &value_vin, &value_vin_sz);
95-
if (value_vin_sz > 0) {
91+
std::vector<char> value_vin = read_db_bytes("CarVin");
92+
if (value_vin.size() > 0) {
9693
// sanity check VIN format
97-
assert(value_vin_sz == 17);
94+
assert(value_vin.size() == 17);
95+
std::string str_vin(value_vin.begin(), value_vin.end());
96+
LOGW("got CarVin %s", str_vin.c_str());
9897
break;
9998
}
10099
usleep(100*1000);
101100
}
102-
LOGW("got CarVin %s", value_vin);
103-
free(value_vin);
104101

105102
// VIN query done, stop listening to OBDII
106103
pthread_mutex_lock(&usb_lock);
107104
libusb_control_transfer(dev_handle, 0x40, 0xdc, (uint16_t)(cereal::CarParams::SafetyModel::NO_OUTPUT), 0, NULL, 0, TIMEOUT);
108105
pthread_mutex_unlock(&usb_lock);
109106

110-
char *value;
111-
size_t value_sz = 0;
112-
107+
std::vector<char> params;
113108
LOGW("waiting for params to set safety model");
114109
while (1) {
115110
if (do_exit) return NULL;
116111

117-
const int result = read_db_value("CarParams", &value, &value_sz);
118-
if (value_sz > 0) break;
112+
params = read_db_bytes("CarParams");
113+
if (params.size() > 0) break;
119114
usleep(100*1000);
120115
}
121-
LOGW("got %d bytes CarParams", value_sz);
116+
LOGW("got %d bytes CarParams", params.size());
122117

123118
// format for board, make copy due to alignment issues, will be freed on out of scope
124-
auto amsg = kj::heapArray<capnp::word>((value_sz / sizeof(capnp::word)) + 1);
125-
memcpy(amsg.begin(), value, value_sz);
126-
free(value);
119+
auto amsg = kj::heapArray<capnp::word>((params.size() / sizeof(capnp::word)) + 1);
120+
memcpy(amsg.begin(), params.data(), params.size());
127121

128122
capnp::FlatArrayMessageReader cmsg(amsg);
129123
cereal::CarParams::Reader car_params = cmsg.getRoot<cereal::CarParams>();
@@ -407,18 +401,15 @@ void can_health(PubMaster &pm) {
407401
bool cdp_mode = health.usb_power_mode == (uint8_t)(cereal::HealthData::UsbPowerMode::CDP);
408402
bool no_ignition_exp = no_ignition_cnt > NO_IGNITION_CNT_MAX;
409403
if ((no_ignition_exp || (voltage_f < VBATT_PAUSE_CHARGING)) && cdp_mode && !ignition) {
410-
char *disable_power_down = NULL;
411-
size_t disable_power_down_sz = 0;
412-
const int result = read_db_value("DisablePowerDown", &disable_power_down, &disable_power_down_sz);
413-
if (disable_power_down_sz != 1 || disable_power_down[0] != '1') {
404+
std::vector<char> disable_power_down = read_db_bytes("DisablePowerDown");
405+
if (disable_power_down.size() != 1 || disable_power_down[0] != '1') {
414406
printf("TURN OFF CHARGING!\n");
415407
pthread_mutex_lock(&usb_lock);
416408
libusb_control_transfer(dev_handle, 0xc0, 0xe6, (uint16_t)(cereal::HealthData::UsbPowerMode::CLIENT), 0, NULL, 0, TIMEOUT);
417409
pthread_mutex_unlock(&usb_lock);
418410
printf("POWER DOWN DEVICE\n");
419411
system("service call power 17 i32 0 i32 1");
420412
}
421-
if (disable_power_down) free(disable_power_down);
422413
}
423414
if (!no_ignition_exp && (voltage_f > VBATT_START_CHARGING) && !cdp_mode) {
424415
printf("TURN ON CHARGING!\n");

selfdrive/common/params.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <map>
1515
#include <string>
16+
#include <string.h>
1617

1718
#include "common/util.h"
1819
#include "common/utilpp.h"
@@ -353,3 +354,15 @@ int read_db_all(std::map<std::string, std::string> *params, bool persistent_para
353354
close(lock_fd);
354355
return 0;
355356
}
357+
358+
std::vector<char> read_db_bytes(const char* param_name, bool persistent_param) {
359+
std::vector<char> bytes;
360+
char* value;
361+
size_t sz;
362+
int result = read_db_value(param_name, &value, &sz, persistent_param);
363+
if (result == 0) {
364+
bytes.assign(value, value+sz);
365+
free(value);
366+
}
367+
return bytes;
368+
}

selfdrive/common/params.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifndef _SELFDRIVE_COMMON_PARAMS_H_
2-
#define _SELFDRIVE_COMMON_PARAMS_H_
3-
1+
#pragma once
42
#include <stddef.h>
53

64
#ifdef __cplusplus
@@ -39,7 +37,7 @@ void read_db_value_blocking(const char* key, char** value, size_t* value_sz, boo
3937
#ifdef __cplusplus
4038
#include <map>
4139
#include <string>
40+
#include <vector>
4241
int read_db_all(std::map<std::string, std::string> *params, bool persistent_param = false);
42+
std::vector<char> read_db_bytes(const char* param_name, bool persistent_param = false);
4343
#endif
44-
45-
#endif // _SELFDRIVE_COMMON_PARAMS_H_

selfdrive/locationd/paramsd.cc

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,33 @@ int main(int argc, char *argv[]) {
3131
Localizer localizer;
3232

3333
// Read car params
34-
char *value;
35-
size_t value_sz = 0;
36-
34+
std::vector<char> params;
3735
LOGW("waiting for params to set vehicle model");
3836
while (true) {
39-
read_db_value("CarParams", &value, &value_sz);
40-
if (value_sz > 0) break;
37+
params = read_db_bytes("CarParams");
38+
if (params.size() > 0) break;
4139
usleep(100*1000);
4240
}
43-
LOGW("got %d bytes CarParams", value_sz);
41+
LOGW("got %d bytes CarParams", params.size());
4442

4543
// make copy due to alignment issues
46-
auto amsg = kj::heapArray<capnp::word>((value_sz / sizeof(capnp::word)) + 1);
47-
memcpy(amsg.begin(), value, value_sz);
48-
free(value);
49-
44+
auto amsg = kj::heapArray<capnp::word>((params.size() / sizeof(capnp::word)) + 1);
45+
memcpy(amsg.begin(), params.data(), params.size());
46+
5047
capnp::FlatArrayMessageReader cmsg(amsg);
5148
cereal::CarParams::Reader car_params = cmsg.getRoot<cereal::CarParams>();
5249

5350
// Read params from previous run
54-
const int result = read_db_value("LiveParameters", &value, &value_sz);
55-
5651
std::string fingerprint = car_params.getCarFingerprint();
5752
std::string vin = car_params.getCarVin();
5853
double sR = car_params.getSteerRatio();
5954
double x = 1.0;
6055
double ao = 0.0;
6156
double posenet_invalid_count = 0;
62-
63-
if (result == 0){
64-
auto str = std::string(value, value_sz);
65-
free(value);
66-
57+
std::vector<char> live_params = read_db_bytes("LiveParameters");
58+
if (live_params.size() > 0){
6759
std::string err;
60+
std::string str(live_params.begin(), live_params.end());
6861
auto json = json11::Json::parse(str, err);
6962
if (json.is_null() || !err.empty()) {
7063
std::string log = "Error parsing json: " + err;

selfdrive/loggerd/loggerd.cc

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,8 @@ void encoder_thread(bool is_streaming, bool raw_clips, bool front) {
9191
int err;
9292

9393
if (front) {
94-
char *value;
95-
const int result = read_db_value("RecordFront", &value, NULL);
96-
if (result != 0) return;
97-
if (value[0] != '1') { free(value); return; }
98-
free(value);
94+
std::vector<char> value = read_db_bytes("RecordFront");
95+
if (value.size() == 0 || value[0] != '1') return;
9996
LOGW("recording front camera");
10097

10198
set_thread_name("FrontCameraEncoder");
@@ -454,30 +451,23 @@ kj::Array<capnp::word> gen_init_data() {
454451
init.setDirty(true);
455452
}
456453

457-
char* git_commit = NULL;
458-
size_t size;
459-
read_db_value("GitCommit", &git_commit, &size);
460-
if (git_commit) {
461-
init.setGitCommit(capnp::Text::Reader(git_commit, size));
454+
std::vector<char> git_commit = read_db_bytes("GitCommit");
455+
if (git_commit.size() > 0) {
456+
init.setGitCommit(capnp::Text::Reader(git_commit.data(), git_commit.size()));
462457
}
463458

464-
char* git_branch = NULL;
465-
read_db_value("GitBranch", &git_branch, &size);
466-
if (git_branch) {
467-
init.setGitBranch(capnp::Text::Reader(git_branch, size));
459+
std::vector<char> git_branch = read_db_bytes("GitBranch");
460+
if (git_branch.size() > 0) {
461+
init.setGitBranch(capnp::Text::Reader(git_branch.data(), git_branch.size()));
468462
}
469463

470-
char* git_remote = NULL;
471-
read_db_value("GitRemote", &git_remote, &size);
472-
if (git_remote) {
473-
init.setGitRemote(capnp::Text::Reader(git_remote, size));
464+
std::vector<char> git_remote = read_db_bytes("GitRemote");
465+
if (git_remote.size() > 0) {
466+
init.setGitRemote(capnp::Text::Reader(git_remote.data(), git_remote.size()));
474467
}
475468

476-
char* passive = NULL;
477-
read_db_value("Passive", &passive, NULL);
478-
init.setPassive(passive && strlen(passive) && passive[0] == '1');
479-
480-
469+
std::vector<char> passive = read_db_bytes("Passive");
470+
init.setPassive(passive.size() > 0 && passive[0] == '1');
481471
{
482472
// log params
483473
std::map<std::string, std::string> params;
@@ -491,27 +481,7 @@ kj::Array<capnp::word> gen_init_data() {
491481
i++;
492482
}
493483
}
494-
495-
496-
auto words = capnp::messageToFlatArray(msg);
497-
498-
if (git_commit) {
499-
free((void*)git_commit);
500-
}
501-
502-
if (git_branch) {
503-
free((void*)git_branch);
504-
}
505-
506-
if (git_remote) {
507-
free((void*)git_remote);
508-
}
509-
510-
if (passive) {
511-
free((void*)passive);
512-
}
513-
514-
return words;
484+
return capnp::messageToFlatArray(msg);
515485
}
516486

517487
static int clear_locks_fn(const char* fpath, const struct stat *sb, int tyupeflag) {

selfdrive/modeld/models/driving.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ void model_init(ModelState* s, cl_device_id device_id, cl_context context, int t
6060
for (int i = 0; i < TRAFFIC_CONVENTION_LEN; i++) s->traffic_convention[i] = 0.0;
6161
s->m->addTrafficConvention(s->traffic_convention, TRAFFIC_CONVENTION_LEN);
6262

63-
char *string;
64-
const int result = read_db_value("IsRHD", &string, NULL);
65-
if (result == 0) {
66-
bool is_rhd = string[0] == '1';
67-
free(string);
63+
std::vector<char> result = read_db_bytes("IsRHD");
64+
if (result.size() > 0) {
65+
bool is_rhd = result[0] == '1';
6866
if (is_rhd) {
6967
s->traffic_convention[1] = 1.0;
7068
} else {

0 commit comments

Comments
 (0)