mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-18 22:23:56 +08:00
hw.h: move Hardware-related paths to namespace Path (#21826)
* refactor getenv and path * apply review
This commit is contained in:
@@ -193,11 +193,11 @@ void fill_frame_data(cereal::FrameData::Builder &framed, const FrameMetadata &fr
|
||||
}
|
||||
|
||||
kj::Array<uint8_t> get_frame_image(const CameraBuf *b) {
|
||||
static const int x_min = getenv("XMIN") ? atoi(getenv("XMIN")) : 0;
|
||||
static const int y_min = getenv("YMIN") ? atoi(getenv("YMIN")) : 0;
|
||||
static const int env_xmax = getenv("XMAX") ? atoi(getenv("XMAX")) : -1;
|
||||
static const int env_ymax = getenv("YMAX") ? atoi(getenv("YMAX")) : -1;
|
||||
static const int scale = getenv("SCALE") ? atoi(getenv("SCALE")) : 1;
|
||||
static const int x_min = util::getenv("XMIN", 0);
|
||||
static const int y_min = util::getenv("YMIN", 0);
|
||||
static const int env_xmax = util::getenv("XMAX", -1);
|
||||
static const int env_ymax = util::getenv("YMAX", -1);
|
||||
static const int scale = util::getenv("SCALE", 1);
|
||||
|
||||
assert(b->cur_rgb_buf);
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
#include "selfdrive/common/util.h"
|
||||
|
||||
// id of the video capturing device
|
||||
const int ROAD_CAMERA_ID = getenv("ROADCAM_ID") ? atoi(getenv("ROADCAM_ID")) : 1;
|
||||
const int DRIVER_CAMERA_ID = getenv("DRIVERCAM_ID") ? atoi(getenv("DRIVERCAM_ID")) : 2;
|
||||
const int ROAD_CAMERA_ID = util::getenv("ROADCAM_ID", 1);
|
||||
const int DRIVER_CAMERA_ID = util::getenv("DRIVERCAM_ID", 2);
|
||||
|
||||
#define FRAME_WIDTH 1164
|
||||
#define FRAME_HEIGHT 874
|
||||
|
||||
@@ -33,10 +33,6 @@
|
||||
|
||||
namespace {
|
||||
|
||||
const std::string default_params_path = Hardware::PC() ? util::getenv_default("HOME", "/.comma/params", "/data/params")
|
||||
: "/data/params";
|
||||
const std::string persistent_params_path = Hardware::PC() ? default_params_path : "/persist/comma/params";
|
||||
|
||||
volatile sig_atomic_t params_do_exit = 0;
|
||||
void params_sig_handler(int signal) {
|
||||
params_do_exit = 1;
|
||||
@@ -231,11 +227,11 @@ std::unordered_map<std::string, uint32_t> keys = {
|
||||
|
||||
} // namespace
|
||||
|
||||
Params::Params(bool persistent_param) : Params(persistent_param ? persistent_params_path : default_params_path) {}
|
||||
Params::Params(bool persistent_param) : Params(persistent_param ? Path::persistent_params() : Path::params()) {}
|
||||
|
||||
std::once_flag default_params_path_ensured;
|
||||
Params::Params(const std::string &path) : params_path(path) {
|
||||
if (path == default_params_path) {
|
||||
if (path == Path::params()) {
|
||||
std::call_once(default_params_path_ensured, ensure_params_path, path);
|
||||
} else {
|
||||
ensure_params_path(path);
|
||||
|
||||
@@ -117,13 +117,19 @@ bool file_exists(const std::string& fn) {
|
||||
return f.good();
|
||||
}
|
||||
|
||||
std::string getenv_default(const char* env_var, const char * suffix, const char* default_val) {
|
||||
const char* env_val = getenv(env_var);
|
||||
if (env_val != NULL) {
|
||||
return std::string(env_val) + std::string(suffix);
|
||||
} else {
|
||||
return std::string(default_val);
|
||||
}
|
||||
std::string getenv(const char* key, const char* default_val) {
|
||||
const char* val = ::getenv(key);
|
||||
return val ? val : default_val;
|
||||
}
|
||||
|
||||
int getenv(const char* key, int default_val) {
|
||||
const char* val = ::getenv(key);
|
||||
return val ? atoi(val) : default_val;
|
||||
}
|
||||
|
||||
float getenv(const char* key, float default_val) {
|
||||
const char* val = ::getenv(key);
|
||||
return val ? atof(val) : default_val;
|
||||
}
|
||||
|
||||
std::string tohex(const uint8_t *buf, size_t buf_size) {
|
||||
|
||||
@@ -53,7 +53,10 @@ std::string string_format(const std::string& format, Args... args) {
|
||||
return std::string(buf.get(), buf.get() + size - 1);
|
||||
}
|
||||
|
||||
std::string getenv_default(const char* env_var, const char* suffix, const char* default_val);
|
||||
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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "selfdrive/hardware/base.h"
|
||||
#include "selfdrive/common/util.h"
|
||||
|
||||
#ifdef QCOM
|
||||
#include "selfdrive/hardware/eon/hardware.h"
|
||||
@@ -16,3 +17,22 @@ public:
|
||||
};
|
||||
#define Hardware HardwarePC
|
||||
#endif
|
||||
|
||||
namespace Path {
|
||||
inline static std::string HOME = util::getenv("HOME");
|
||||
inline std::string log_root() {
|
||||
if (const char *env = getenv("LOG_ROOT")) {
|
||||
return env;
|
||||
}
|
||||
return Hardware::PC() ? HOME + "/.comma/media/0/realdata" : "/data/media/0/realdata";
|
||||
}
|
||||
inline std::string params() {
|
||||
return Hardware::PC() ? HOME + "/.comma/params" : "/data/params";
|
||||
}
|
||||
inline std::string persistent_params() {
|
||||
return Hardware::PC() ? HOME + "/.comma/params" : "/persist/comma/params";
|
||||
}
|
||||
inline std::string rsa_file() {
|
||||
return Hardware::PC() ? HOME + "/.comma/persist/comma/id_rsa" : "/persist/comma/id_rsa";
|
||||
}
|
||||
} // namespace Path
|
||||
|
||||
@@ -15,11 +15,7 @@
|
||||
#include "selfdrive/common/swaglog.h"
|
||||
#include "selfdrive/hardware/hw.h"
|
||||
|
||||
const std::string DEFAULT_LOG_ROOT =
|
||||
Hardware::PC() ? util::getenv_default("HOME", "/.comma/media/0/realdata", "/data/media/0/realdata")
|
||||
: "/data/media/0/realdata";
|
||||
|
||||
const std::string LOG_ROOT = util::getenv_default("LOG_ROOT", "", DEFAULT_LOG_ROOT.c_str());
|
||||
const std::string LOG_ROOT = Path::log_root();
|
||||
|
||||
#define LOGGER_MAX_HANDLES 16
|
||||
|
||||
|
||||
@@ -18,12 +18,8 @@
|
||||
|
||||
namespace CommaApi {
|
||||
|
||||
const std::string private_key_path =
|
||||
Hardware::PC() ? util::getenv_default("HOME", "/.comma/persist/comma/id_rsa", "/persist/comma/id_rsa")
|
||||
: "/persist/comma/id_rsa";
|
||||
|
||||
QByteArray rsa_sign(const QByteArray &data) {
|
||||
auto file = QFile(private_key_path.c_str());
|
||||
auto file = QFile(Path::rsa_file().c_str());
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qDebug() << "No RSA private key found, please run manager.py or registration.py";
|
||||
return QByteArray();
|
||||
@@ -89,7 +85,7 @@ void HttpRequest::sendRequest(const QString &requestURL, const HttpRequest::Meth
|
||||
if(create_jwt) {
|
||||
token = CommaApi::create_jwt();
|
||||
} else {
|
||||
QString token_json = QString::fromStdString(util::read_file(util::getenv_default("HOME", "/.comma/auth.json", "/.comma/auth.json")));
|
||||
QString token_json = QString::fromStdString(util::read_file(util::getenv("HOME") + "/.comma/auth.json"));
|
||||
QJsonDocument json_d = QJsonDocument::fromJson(token_json.toUtf8());
|
||||
token = json_d["access_token"].toString();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ const int vwp_w = (Hardware::TICI() || (getenv("WIDE_UI") != NULL)) ? 2160 : 192
|
||||
const int vwp_h = 1080;
|
||||
|
||||
inline void setMainWindow(QWidget *w) {
|
||||
const float scale = getenv("SCALE") != NULL ? std::stof(getenv("SCALE")) : 1.0;
|
||||
const float scale = util::getenv("SCALE", 1.0f);
|
||||
w->setFixedSize(vwp_w*scale, vwp_h*scale);
|
||||
w->show();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user