mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-09-30 19:33:45 +08:00
openpilot v0.8.11 release
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
#include "selfdrive/common/clutil.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "selfdrive/common/util.h"
|
||||
|
||||
@@ -78,8 +74,10 @@ cl_device_id cl_get_device_id(cl_device_type device_type) {
|
||||
}
|
||||
|
||||
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args) {
|
||||
std::string src = util::read_file(path);
|
||||
assert(src.length() > 0);
|
||||
return cl_program_from_source(ctx, device_id, util::read_file(path), args);
|
||||
}
|
||||
|
||||
cl_program cl_program_from_source(cl_context ctx, cl_device_id device_id, const std::string& src, const char* args) {
|
||||
cl_program prg = CL_CHECK_ERR(clCreateProgramWithSource(ctx, 1, (const char*[]){src.c_str()}, NULL, &err));
|
||||
if (int err = clBuildProgram(prg, 1, &device_id, args, NULL, NULL); err != 0) {
|
||||
cl_print_build_errors(prg, device_id);
|
||||
@@ -88,6 +86,15 @@ cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const ch
|
||||
return prg;
|
||||
}
|
||||
|
||||
cl_program cl_program_from_binary(cl_context ctx, cl_device_id device_id, const uint8_t* binary, size_t length, const char* args) {
|
||||
cl_program prg = CL_CHECK_ERR(clCreateProgramWithBinary(ctx, 1, &device_id, &length, (const uint8_t*[]){binary}, NULL, &err));
|
||||
if (int err = clBuildProgram(prg, 1, &device_id, args, NULL, NULL); err != 0) {
|
||||
cl_print_build_errors(prg, device_id);
|
||||
assert(0);
|
||||
}
|
||||
return prg;
|
||||
}
|
||||
|
||||
// Given a cl code and return a string representation
|
||||
#define CL_ERR_TO_STR(err) case err: return #err
|
||||
const char* cl_get_error_string(int err) {
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <OpenCL/cl.h>
|
||||
#else
|
||||
#include <CL/cl.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
#define CL_CHECK(_expr) \
|
||||
do { \
|
||||
assert(CL_SUCCESS == (_expr)); \
|
||||
@@ -23,5 +22,7 @@
|
||||
})
|
||||
|
||||
cl_device_id cl_get_device_id(cl_device_type device_type);
|
||||
cl_program cl_program_from_source(cl_context ctx, cl_device_id device_id, const std::string& src, const char* args = nullptr);
|
||||
cl_program cl_program_from_binary(cl_context ctx, cl_device_id device_id, const uint8_t* binary, size_t length, const char* args = nullptr);
|
||||
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args);
|
||||
const char* cl_get_error_string(int err);
|
||||
|
||||
+44
-103
@@ -1,19 +1,9 @@
|
||||
#include "selfdrive/common/params.h"
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif // _GNU_SOURCE
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "selfdrive/common/swaglog.h"
|
||||
@@ -27,50 +17,27 @@ void params_sig_handler(int signal) {
|
||||
params_do_exit = 1;
|
||||
}
|
||||
|
||||
int fsync_dir(const char* path) {
|
||||
int fd = HANDLE_EINTR(open(path, O_RDONLY, 0755));
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = fsync(fd);
|
||||
int result_close = close(fd);
|
||||
if (result_close < 0) {
|
||||
result = result_close;
|
||||
int fsync_dir(const std::string &path) {
|
||||
int result = -1;
|
||||
int fd = HANDLE_EINTR(open(path.c_str(), O_RDONLY, 0755));
|
||||
if (fd >= 0) {
|
||||
result = fsync(fd);
|
||||
close(fd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int mkdir_p(std::string path) {
|
||||
char * _path = (char *)path.c_str();
|
||||
|
||||
for (char *p = _path + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0'; // Temporarily truncate
|
||||
if (mkdir(_path, 0775) != 0) {
|
||||
if (errno != EEXIST) return -1;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
if (mkdir(_path, 0775) != 0) {
|
||||
if (errno != EEXIST) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool create_params_path(const std::string ¶m_path, const std::string &key_path) {
|
||||
// Make sure params path exists
|
||||
if (!util::file_exists(param_path) && mkdir_p(param_path) != 0) {
|
||||
if (!util::file_exists(param_path) && !util::create_directories(param_path, 0775)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// See if the symlink exists, otherwise create it
|
||||
if (!util::file_exists(key_path)) {
|
||||
// 1) Create temp folder
|
||||
// 2) Set permissions
|
||||
// 3) Symlink it to temp link
|
||||
// 4) Move symlink to <params>/d
|
||||
// 2) Symlink it to temp link
|
||||
// 3) Move symlink to <params>/d
|
||||
|
||||
std::string tmp_path = param_path + "/.tmp_XXXXXX";
|
||||
// this should be OK since mkdtemp just replaces characters in place
|
||||
@@ -93,32 +60,26 @@ bool create_params_path(const std::string ¶m_path, const std::string &key_pa
|
||||
return true;
|
||||
}
|
||||
|
||||
void ensure_params_path(const std::string ¶ms_path) {
|
||||
std::string ensure_params_path(const std::string &path = {}) {
|
||||
std::string params_path = path.empty() ? Path::params() : path;
|
||||
if (!create_params_path(params_path, params_path + "/d")) {
|
||||
throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno));
|
||||
}
|
||||
return params_path;
|
||||
}
|
||||
|
||||
class FileLock {
|
||||
public:
|
||||
FileLock(const std::string& file_name, int op) : fn_(file_name), op_(op) {}
|
||||
|
||||
void lock() {
|
||||
fd_ = HANDLE_EINTR(open(fn_.c_str(), O_CREAT, 0775));
|
||||
if (fd_ < 0) {
|
||||
LOGE("Failed to open lock file %s, errno=%d", fn_.c_str(), errno);
|
||||
return;
|
||||
}
|
||||
if (HANDLE_EINTR(flock(fd_, op_)) < 0) {
|
||||
LOGE("Failed to lock file %s, errno=%d", fn_.c_str(), errno);
|
||||
public:
|
||||
FileLock(const std::string &fn) {
|
||||
fd_ = HANDLE_EINTR(open(fn.c_str(), O_CREAT, 0775));
|
||||
if (fd_ < 0 || HANDLE_EINTR(flock(fd_, LOCK_EX)) < 0) {
|
||||
LOGE("Failed to lock file %s, errno=%d", fn.c_str(), errno);
|
||||
}
|
||||
}
|
||||
|
||||
void unlock() { close(fd_); }
|
||||
~FileLock() { close(fd_); }
|
||||
|
||||
private:
|
||||
int fd_ = -1, op_;
|
||||
std::string fn_;
|
||||
int fd_ = -1;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, uint32_t> keys = {
|
||||
@@ -139,6 +100,8 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"DisableRadar", PERSISTENT}, // WARNING: THIS DISABLES AEB
|
||||
{"DisableUpdates", PERSISTENT},
|
||||
{"DongleId", PERSISTENT},
|
||||
{"DoReboot", CLEAR_ON_MANAGER_START},
|
||||
{"DoShutdown", CLEAR_ON_MANAGER_START},
|
||||
{"DoUninstall", CLEAR_ON_MANAGER_START},
|
||||
{"EnableWideCamera", CLEAR_ON_MANAGER_START},
|
||||
{"EndToEndToggle", PERSISTENT},
|
||||
@@ -153,6 +116,7 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"GsmRoaming", PERSISTENT},
|
||||
{"HardwareSerial", PERSISTENT},
|
||||
{"HasAcceptedTerms", PERSISTENT},
|
||||
{"HasPrime", PERSISTENT},
|
||||
{"IMEI", PERSISTENT},
|
||||
{"InstallDate", PERSISTENT},
|
||||
{"IsDriverViewEnabled", CLEAR_ON_MANAGER_START},
|
||||
@@ -169,13 +133,9 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"LastUpdateException", PERSISTENT},
|
||||
{"LastUpdateTime", PERSISTENT},
|
||||
{"LiveParameters", PERSISTENT},
|
||||
{"MapboxToken", PERSISTENT | DONT_LOG},
|
||||
{"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
||||
{"NavSettingTime24h", PERSISTENT},
|
||||
{"OpenpilotEnabledToggle", PERSISTENT},
|
||||
{"PandaDongleId", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
||||
{"PandaFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
||||
{"PandaFirmwareHex", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
||||
{"PandaHeartbeatLost", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
||||
{"Passive", PERSISTENT},
|
||||
{"PrimeRedirected", PERSISTENT},
|
||||
@@ -183,6 +143,7 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"RecordFrontLock", PERSISTENT}, // for the internal fleet
|
||||
{"ReleaseNotes", PERSISTENT},
|
||||
{"ShouldDoUpdate", CLEAR_ON_MANAGER_START},
|
||||
{"SnoozeUpdate", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
||||
{"SshEnabled", PERSISTENT},
|
||||
{"SubscriberInfo", PERSISTENT},
|
||||
{"TermsVersion", PERSISTENT},
|
||||
@@ -197,15 +158,15 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"ApiCache_DriveStats", PERSISTENT},
|
||||
{"ApiCache_NavDestinations", PERSISTENT},
|
||||
{"ApiCache_Owner", PERSISTENT},
|
||||
{"Offroad_CarUnrecognized", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
||||
{"Offroad_ChargeDisabled", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
||||
{"Offroad_ConnectivityNeeded", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_ConnectivityNeededPrompt", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_HardwareUnsupported", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_InvalidTime", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_IsTakingSnapshot", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_NeosUpdate", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_NvmeMissing", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_PandaFirmwareMismatch", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
||||
{"Offroad_NoFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
||||
{"Offroad_StorageMissing", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_TemperatureTooHigh", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_UnofficialHardware", CLEAR_ON_MANAGER_START},
|
||||
{"Offroad_UpdateFailed", CLEAR_ON_MANAGER_START},
|
||||
@@ -213,13 +174,9 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
|
||||
} // namespace
|
||||
|
||||
Params::Params() : params_path(Path::params()) {
|
||||
static std::once_flag once_flag;
|
||||
std::call_once(once_flag, ensure_params_path, params_path);
|
||||
}
|
||||
|
||||
Params::Params(const std::string &path) : params_path(path) {
|
||||
ensure_params_path(params_path);
|
||||
Params::Params(const std::string &path) {
|
||||
static std::string default_param_path = ensure_params_path();
|
||||
params_path = path.empty() ? default_param_path : ensure_params_path(path);
|
||||
}
|
||||
|
||||
bool Params::checkKey(const std::string &key) {
|
||||
@@ -253,16 +210,13 @@ int Params::put(const char* key, const char* value, size_t value_size) {
|
||||
// fsync to force persist the changes.
|
||||
if ((result = fsync(tmp_fd)) < 0) break;
|
||||
|
||||
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
||||
std::lock_guard<FileLock> lk(file_lock);
|
||||
FileLock file_lock(params_path + "/.lock");
|
||||
|
||||
// Move temp into place.
|
||||
std::string path = params_path + "/d/" + std::string(key);
|
||||
if ((result = rename(tmp_path.c_str(), path.c_str())) < 0) break;
|
||||
if ((result = rename(tmp_path.c_str(), getParamPath(key).c_str())) < 0) break;
|
||||
|
||||
// fsync parent directory
|
||||
path = params_path + "/d";
|
||||
result = fsync_dir(path.c_str());
|
||||
result = fsync_dir(getParamPath());
|
||||
} while (false);
|
||||
|
||||
close(tmp_fd);
|
||||
@@ -270,24 +224,18 @@ int Params::put(const char* key, const char* value, size_t value_size) {
|
||||
return result;
|
||||
}
|
||||
|
||||
int Params::remove(const char *key) {
|
||||
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
||||
std::lock_guard<FileLock> lk(file_lock);
|
||||
// Delete value.
|
||||
std::string path = params_path + "/d/" + key;
|
||||
int result = unlink(path.c_str());
|
||||
int Params::remove(const std::string &key) {
|
||||
FileLock file_lock(params_path + "/.lock");
|
||||
int result = unlink(getParamPath(key).c_str());
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
// fsync parent directory
|
||||
path = params_path + "/d";
|
||||
return fsync_dir(path.c_str());
|
||||
return fsync_dir(getParamPath());
|
||||
}
|
||||
|
||||
std::string Params::get(const char *key, bool block) {
|
||||
std::string path = params_path + "/d/" + key;
|
||||
std::string Params::get(const std::string &key, bool block) {
|
||||
if (!block) {
|
||||
return util::read_file(path);
|
||||
return util::read_file(getParamPath(key));
|
||||
} else {
|
||||
// blocking read until successful
|
||||
params_do_exit = 0;
|
||||
@@ -296,7 +244,7 @@ std::string Params::get(const char *key, bool block) {
|
||||
|
||||
std::string value;
|
||||
while (!params_do_exit) {
|
||||
if (value = util::read_file(path); !value.empty()) {
|
||||
if (value = util::read_file(getParamPath(key)); !value.empty()) {
|
||||
break;
|
||||
}
|
||||
util::sleep_for(100); // 0.1 s
|
||||
@@ -309,26 +257,19 @@ std::string Params::get(const char *key, bool block) {
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> Params::readAll() {
|
||||
FileLock file_lock(params_path + "/.lock", LOCK_SH);
|
||||
std::lock_guard<FileLock> lk(file_lock);
|
||||
|
||||
std::string key_path = params_path + "/d";
|
||||
return util::read_files_in_dir(key_path);
|
||||
FileLock file_lock(params_path + "/.lock");
|
||||
return util::read_files_in_dir(getParamPath());
|
||||
}
|
||||
|
||||
void Params::clearAll(ParamKeyType key_type) {
|
||||
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
||||
std::lock_guard<FileLock> lk(file_lock);
|
||||
FileLock file_lock(params_path + "/.lock");
|
||||
|
||||
std::string path;
|
||||
for (auto &[key, type] : keys) {
|
||||
if (type & key_type) {
|
||||
path = params_path + "/d/" + key;
|
||||
unlink(path.c_str());
|
||||
unlink(getParamPath(key).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// fsync parent directory
|
||||
path = params_path + "/d";
|
||||
fsync_dir(path.c_str());
|
||||
fsync_dir(getParamPath());
|
||||
}
|
||||
|
||||
+10
-47
@@ -1,9 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
enum ParamKeyType {
|
||||
PERSISTENT = 0x02,
|
||||
@@ -17,68 +15,33 @@ enum ParamKeyType {
|
||||
|
||||
class Params {
|
||||
public:
|
||||
Params();
|
||||
Params(const std::string &path);
|
||||
|
||||
Params(const std::string &path = {});
|
||||
bool checkKey(const std::string &key);
|
||||
ParamKeyType getKeyType(const std::string &key);
|
||||
inline std::string getParamPath(const std::string &key = {}) {
|
||||
return key.empty() ? params_path + "/d" : params_path + "/d/" + key;
|
||||
}
|
||||
|
||||
// Delete a value
|
||||
int remove(const char *key);
|
||||
inline int remove(const std::string &key) {
|
||||
return remove (key.c_str());
|
||||
}
|
||||
int remove(const std::string &key);
|
||||
void clearAll(ParamKeyType type);
|
||||
|
||||
// read all values
|
||||
std::map<std::string, std::string> readAll();
|
||||
|
||||
// helpers for reading values
|
||||
std::string get(const char *key, bool block = false);
|
||||
|
||||
inline std::string get(const std::string &key, bool block = false) {
|
||||
return get(key.c_str(), block);
|
||||
}
|
||||
|
||||
inline std::string getParamsPath() {
|
||||
return params_path;
|
||||
}
|
||||
|
||||
inline std::string getParamPath(std::string key) {
|
||||
return params_path + "/d/" + key;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::optional<T> get(const char *key, bool block = false) {
|
||||
std::istringstream iss(get(key, block));
|
||||
T value{};
|
||||
iss >> value;
|
||||
return iss.fail() ? std::nullopt : std::optional(value);
|
||||
}
|
||||
|
||||
std::string get(const std::string &key, bool block = false);
|
||||
inline bool getBool(const std::string &key) {
|
||||
return getBool(key.c_str());
|
||||
}
|
||||
|
||||
inline bool getBool(const char *key) {
|
||||
return get(key) == "1";
|
||||
}
|
||||
std::map<std::string, std::string> readAll();
|
||||
|
||||
// helpers for writing values
|
||||
int put(const char* key, const char* val, size_t value_size);
|
||||
|
||||
int put(const char *key, const char *val, size_t value_size);
|
||||
inline int put(const std::string &key, const std::string &val) {
|
||||
return put(key.c_str(), val.data(), val.size());
|
||||
}
|
||||
|
||||
inline int putBool(const char *key, bool val) {
|
||||
return put(key, val ? "1" : "0", 1);
|
||||
}
|
||||
|
||||
inline int putBool(const std::string &key, bool val) {
|
||||
return putBool(key.c_str(), val);
|
||||
return put(key.c_str(), val ? "1" : "0", 1);
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string params_path;
|
||||
std::string params_path;
|
||||
};
|
||||
|
||||
+81
-22
@@ -1,14 +1,15 @@
|
||||
#include "selfdrive/common/util.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
@@ -106,6 +107,32 @@ int write_file(const char* path, const void* data, size_t size, int flags, mode_
|
||||
return (n >= 0 && (size_t)n == size) ? 0 : -1;
|
||||
}
|
||||
|
||||
FILE* safe_fopen(const char* filename, const char* mode) {
|
||||
FILE* fp = NULL;
|
||||
do {
|
||||
fp = fopen(filename, mode);
|
||||
} while ((nullptr == fp) && (errno == EINTR));
|
||||
return fp;
|
||||
}
|
||||
|
||||
size_t safe_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
|
||||
size_t written = 0;
|
||||
do {
|
||||
size_t ret = ::fwrite((void*)((char *)ptr + written * size), size, count - written, stream);
|
||||
if (ret == 0 && errno != EINTR) break;
|
||||
written += ret;
|
||||
} while (written != count);
|
||||
return written;
|
||||
}
|
||||
|
||||
int safe_fflush(FILE *stream) {
|
||||
int ret = EOF;
|
||||
do {
|
||||
ret = fflush(stream);
|
||||
} while ((EOF == ret) && (errno == EINTR));
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string readlink(const std::string &path) {
|
||||
char buff[4096];
|
||||
ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1);
|
||||
@@ -121,6 +148,37 @@ bool file_exists(const std::string& fn) {
|
||||
return stat(fn.c_str(), &st) != -1;
|
||||
}
|
||||
|
||||
static bool createDirectory(std::string dir, mode_t mode) {
|
||||
auto verify_dir = [](const std::string& dir) -> bool {
|
||||
struct stat st = {};
|
||||
return (stat(dir.c_str(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR);
|
||||
};
|
||||
// remove trailing /'s
|
||||
while (dir.size() > 1 && dir.back() == '/') {
|
||||
dir.pop_back();
|
||||
}
|
||||
// try to mkdir this directory
|
||||
if (mkdir(dir.c_str(), mode) == 0) return true;
|
||||
if (errno == EEXIST) return verify_dir(dir);
|
||||
if (errno != ENOENT) return false;
|
||||
|
||||
// mkdir failed because the parent dir doesn't exist, so try to create it
|
||||
size_t slash = dir.rfind('/');
|
||||
if ((slash == std::string::npos || slash < 1) ||
|
||||
!createDirectory(dir.substr(0, slash), mode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// try again
|
||||
if (mkdir(dir.c_str(), mode) == 0) return true;
|
||||
return errno == EEXIST && verify_dir(dir);
|
||||
}
|
||||
|
||||
bool create_directories(const std::string& dir, mode_t mode) {
|
||||
if (dir.empty()) return false;
|
||||
return createDirectory(dir, mode);
|
||||
}
|
||||
|
||||
std::string getenv(const char* key, const char* default_val) {
|
||||
const char* val = ::getenv(key);
|
||||
return val ? val : default_val;
|
||||
@@ -136,28 +194,13 @@ float getenv(const char* key, float default_val) {
|
||||
return val ? atof(val) : default_val;
|
||||
}
|
||||
|
||||
std::string tohex(const uint8_t *buf, size_t buf_size) {
|
||||
std::unique_ptr<char[]> hexbuf(new char[buf_size * 2 + 1]);
|
||||
for (size_t i = 0; i < buf_size; i++) {
|
||||
sprintf(&hexbuf[i * 2], "%02x", buf[i]);
|
||||
std::string hexdump(const uint8_t* in, const size_t size) {
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::setfill('0');
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
ss << std::setw(2) << static_cast<unsigned int>(in[i]);
|
||||
}
|
||||
hexbuf[buf_size * 2] = 0;
|
||||
return std::string(hexbuf.get(), hexbuf.get() + buf_size * 2);
|
||||
}
|
||||
|
||||
std::string hexdump(const std::string& in) {
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::setfill('0');
|
||||
for (size_t i = 0; i < in.size(); i++) {
|
||||
ss << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(in[i]));
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string base_name(std::string const &path) {
|
||||
size_t pos = path.find_last_of("/");
|
||||
if (pos == std::string::npos) return path;
|
||||
return path.substr(pos + 1);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string dir_name(std::string const &path) {
|
||||
@@ -166,6 +209,22 @@ std::string dir_name(std::string const &path) {
|
||||
return path.substr(0, pos);
|
||||
}
|
||||
|
||||
std::string check_output(const std::string& command) {
|
||||
char buffer[128];
|
||||
std::string result;
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
|
||||
|
||||
if (!pipe) {
|
||||
return "";
|
||||
}
|
||||
|
||||
while (fgets(buffer, std::size(buffer), pipe.get()) != nullptr) {
|
||||
result += std::string(buffer);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct tm get_time() {
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
|
||||
+25
-11
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -15,20 +16,27 @@
|
||||
#include <vector>
|
||||
|
||||
// keep trying if x gets interrupted by a signal
|
||||
#define HANDLE_EINTR(x) \
|
||||
({ \
|
||||
decltype(x) ret; \
|
||||
int try_cnt = 0; \
|
||||
do { \
|
||||
ret = (x); \
|
||||
} while (ret == -1 && errno == EINTR && try_cnt++ < 100); \
|
||||
ret; \
|
||||
#define HANDLE_EINTR(x) \
|
||||
({ \
|
||||
decltype(x) ret_; \
|
||||
int try_cnt = 0; \
|
||||
do { \
|
||||
ret_ = (x); \
|
||||
} while (ret_ == -1 && errno == EINTR && try_cnt++ < 100); \
|
||||
ret_; \
|
||||
})
|
||||
|
||||
#ifndef sighandler_t
|
||||
typedef void (*sighandler_t)(int sig);
|
||||
#endif
|
||||
|
||||
const double MILE_TO_KM = 1.609344;
|
||||
const double KM_TO_MILE = 1. / MILE_TO_KM;
|
||||
const double MS_TO_KPH = 3.6;
|
||||
const double MS_TO_MPH = MS_TO_KPH * KM_TO_MILE;
|
||||
const double METER_TO_MILE = KM_TO_MILE / 1000.0;
|
||||
const double METER_TO_FOOT = 3.28084;
|
||||
|
||||
void set_thread_name(const char* name);
|
||||
|
||||
int set_realtime_priority(int level);
|
||||
@@ -65,17 +73,23 @@ std::string getenv(const char* key, const char* default_val = "");
|
||||
int getenv(const char* key, int default_val);
|
||||
float getenv(const char* key, float default_val);
|
||||
|
||||
std::string tohex(const uint8_t* buf, size_t buf_size);
|
||||
std::string hexdump(const std::string& in);
|
||||
std::string base_name(std::string const& path);
|
||||
std::string hexdump(const uint8_t* in, const size_t size);
|
||||
std::string dir_name(std::string const& path);
|
||||
|
||||
// **** file fhelpers *****
|
||||
std::string read_file(const std::string& fn);
|
||||
std::map<std::string, std::string> read_files_in_dir(const std::string& path);
|
||||
int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0664);
|
||||
|
||||
FILE* safe_fopen(const char* filename, const char* mode);
|
||||
size_t safe_fwrite(const void * ptr, size_t size, size_t count, FILE * stream);
|
||||
int safe_fflush(FILE *stream);
|
||||
|
||||
std::string readlink(const std::string& path);
|
||||
bool file_exists(const std::string& fn);
|
||||
bool create_directories(const std::string &dir, mode_t mode);
|
||||
|
||||
std::string check_output(const std::string& command);
|
||||
|
||||
inline void sleep_for(const int milliseconds) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define COMMA_VERSION "0.8.10"
|
||||
#define COMMA_VERSION "0.8.11"
|
||||
|
||||
@@ -53,7 +53,7 @@ EGLImageTexture::~EGLImageTexture() {
|
||||
EGLImageTexture::EGLImageTexture(const VisionBuf *buf) {
|
||||
glGenTextures(1, &frame_tex);
|
||||
glBindTexture(GL_TEXTURE_2D, frame_tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buf->width, buf->height, 0, GL_RGB, GL_UNSIGNED_BYTE, buf->addr);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buf->width, buf->height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
#include "selfdrive/common/watchdog.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "selfdrive/common/timing.h"
|
||||
#include "selfdrive/common/util.h"
|
||||
|
||||
const std::string watchdog_fn_prefix = "/dev/shm/wd_"; // + <pid>
|
||||
|
||||
bool watchdog_kick() {
|
||||
std::string fn = watchdog_fn_prefix + std::to_string(getpid());
|
||||
std::string cur_t = std::to_string(nanos_since_boot());
|
||||
static std::string fn = watchdog_fn_prefix + std::to_string(getpid());
|
||||
|
||||
int r = util::write_file(fn.c_str(), cur_t.data(), cur_t.length(), O_WRONLY | O_CREAT);
|
||||
return r == 0;
|
||||
uint64_t ts = nanos_since_boot();
|
||||
return util::write_file(fn.c_str(), &ts, sizeof(ts), O_WRONLY | O_CREAT) > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user