diff --git a/selfdrive/common/framebuffer.cc b/selfdrive/common/framebuffer.cc index 62ac7053..ac6bd3cb 100644 --- a/selfdrive/common/framebuffer.cc +++ b/selfdrive/common/framebuffer.cc @@ -40,7 +40,7 @@ void FrameBuffer::swap() { bool set_brightness(int brightness) { char bright[64]; snprintf(bright, sizeof(bright), "%d", brightness); - return 0 == write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright)); + return 0 == util::write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright)); } void FrameBuffer::set_power(int mode) { diff --git a/selfdrive/common/gpio.cc b/selfdrive/common/gpio.cc index 8a72388d..79dfce45 100644 --- a/selfdrive/common/gpio.cc +++ b/selfdrive/common/gpio.cc @@ -15,7 +15,7 @@ int gpio_init(int pin_nr, bool output){ return -1; } const char *value = output ? "out" : "in"; - return write_file(pin_dir_path, (void*)value, strlen(value)); + return util::write_file(pin_dir_path, (void*)value, strlen(value)); } int gpio_set(int pin_nr, bool high){ @@ -25,5 +25,5 @@ int gpio_set(int pin_nr, bool high){ if(pin_val_path_len <= 0){ return -1; } - return write_file(pin_val_path, (void*)(high ? "1" : "0"), 1); + return util::write_file(pin_val_path, (void*)(high ? "1" : "0"), 1); } diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index a9db1a15..86f29968 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -11,46 +11,6 @@ #include #endif // __linux__ -void* read_file(const char* path, size_t* out_len) { - FILE* f = fopen(path, "r"); - if (!f) { - return NULL; - } - fseek(f, 0, SEEK_END); - long f_len = ftell(f); - rewind(f); - - // malloc one extra byte so the file will always be NULL terminated - // cl_cached_program_from_file relies on this - char* buf = (char*)malloc(f_len+1); - assert(buf); - - size_t num_read = fread(buf, f_len, 1, f); - fclose(f); - - if (num_read != 1) { - free(buf); - return NULL; - } - - buf[f_len] = '\0'; - if (out_len) { - *out_len = f_len; - } - - return buf; -} - -int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) { - int fd = open(path, flags, mode); - if (fd == -1) { - return -1; - } - ssize_t n = write(fd, data, size); - close(fd); - return (n >= 0 && (size_t)n == size) ? 0 : -1; -} - void set_thread_name(const char* name) { #ifdef __linux__ // pthread_setname_np is dumb (fails instead of truncates) @@ -84,3 +44,30 @@ int set_core_affinity(int core) { return -1; #endif } + +namespace util { + +std::string read_file(const std::string& fn) { + std::ifstream ifs(fn, std::ios::binary | std::ios::ate); + if (ifs) { + std::ifstream::pos_type pos = ifs.tellg(); + std::string result; + result.resize(pos); + ifs.seekg(0, std::ios::beg); + ifs.read(result.data(), pos); + if (ifs) return result; + } + return ""; +} + +int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) { + int fd = open(path, flags, mode); + if (fd == -1) { + return -1; + } + ssize_t n = write(fd, data, size); + close(fd); + return (n >= 0 && (size_t)n == size) ? 0 : -1; +} + +} // namespace util diff --git a/selfdrive/common/util.h b/selfdrive/common/util.h index 1e437738..3715da73 100644 --- a/selfdrive/common/util.h +++ b/selfdrive/common/util.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -20,13 +19,6 @@ typedef void (*sighandler_t)(int sig); #endif -// Reads a file into a newly allocated buffer. -// -// Returns NULL on failure, otherwise the NULL-terminated file contents. -// The result must be freed by the caller. -void* read_file(const char* path, size_t* out_len); -int write_file(const char* path, const void* data, size_t size, int flags=O_WRONLY, mode_t mode=0777); - void set_thread_name(const char* name); int set_realtime_priority(int level); @@ -59,12 +51,9 @@ inline std::string string_format(const std::string& format, Args... args) { return std::string(buf.get(), buf.get() + size - 1); } -inline std::string read_file(const std::string &fn) { - std::ifstream t(fn); - std::stringstream buffer; - buffer << t.rdbuf(); - return buffer.str(); -} +std::string read_file(const std::string &fn); + +int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0777); inline std::string tohex(const uint8_t* buf, size_t buf_size) { std::unique_ptr hexbuf(new char[buf_size*2+1]); diff --git a/selfdrive/common/watchdog.cc b/selfdrive/common/watchdog.cc index 2e8afb39..5fb43f6e 100644 --- a/selfdrive/common/watchdog.cc +++ b/selfdrive/common/watchdog.cc @@ -12,6 +12,6 @@ bool watchdog_kick(){ std::string fn = watchdog_fn_prefix + std::to_string(getpid()); std::string cur_t = std::to_string(nanos_since_boot()); - int r = write_file(fn.c_str(), cur_t.data(), cur_t.length(), O_WRONLY | O_CREAT); + int r = util::write_file(fn.c_str(), cur_t.data(), cur_t.length(), O_WRONLY | O_CREAT); return r == 0; } diff --git a/selfdrive/modeld/runners/snpemodel.cc b/selfdrive/modeld/runners/snpemodel.cc index 538c2465..cc9c92f3 100644 --- a/selfdrive/modeld/runners/snpemodel.cc +++ b/selfdrive/modeld/runners/snpemodel.cc @@ -24,14 +24,13 @@ SNPEModel::SNPEModel(const char *path, float *loutput, size_t loutput_size, int } assert(zdl::SNPE::SNPEFactory::isRuntimeAvailable(Runtime)); #endif - size_t model_size; - model_data = (uint8_t *)read_file(path, &model_size); - assert(model_data); + model_data = util::read_file(path); + assert(model_data.size() > 0); // load model - std::unique_ptr container = zdl::DlContainer::IDlContainer::open(model_data, model_size); + std::unique_ptr container = zdl::DlContainer::IDlContainer::open((uint8_t*)model_data.data(), model_data.size()); if (!container) { PrintErrorStringAndExit(); } - printf("loaded model with size: %lu\n", model_size); + printf("loaded model with size: %lu\n", model_data.size()); // create model runner zdl::SNPE::SNPEBuilder snpeBuilder(container.get()); diff --git a/selfdrive/modeld/runners/snpemodel.h b/selfdrive/modeld/runners/snpemodel.h index 76339642..1c2ed2e2 100644 --- a/selfdrive/modeld/runners/snpemodel.h +++ b/selfdrive/modeld/runners/snpemodel.h @@ -24,9 +24,6 @@ class SNPEModel : public RunModel { public: SNPEModel(const char *path, float *loutput, size_t loutput_size, int runtime); - ~SNPEModel() { - if (model_data) free(model_data); - } void addRecurrent(float *state, int state_size); void addTrafficConvention(float *state, int state_size); void addDesire(float *state, int state_size); @@ -37,7 +34,7 @@ public: #endif private: - uint8_t *model_data = NULL; + std::string model_data; #if defined(QCOM) || defined(QCOM2) zdl::DlSystem::Runtime_t Runtime; diff --git a/selfdrive/proclogd/SConscript b/selfdrive/proclogd/SConscript index b80d1720..f95f2597 100644 --- a/selfdrive/proclogd/SConscript +++ b/selfdrive/proclogd/SConscript @@ -1,2 +1,2 @@ -Import('env', 'cereal', 'messaging') -env.Program('proclogd.cc', LIBS=[cereal, messaging, 'pthread', 'zmq', 'capnp', 'kj']) +Import('env', 'cereal', 'messaging', 'common') +env.Program('proclogd.cc', LIBS=[cereal, messaging, 'pthread', 'zmq', 'capnp', 'kj', 'common'])