common: add new class OpenPilotPrefix (#26753)

* new class OpenPilotPrefix

* move random_string to util

* rename file

* style

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
old-commit-hash: 0d8254e959
taco
Dean Lee 2 years ago committed by GitHub
parent b2d2104360
commit 725e0665df
  1. 34
      common/prefix.h
  2. 13
      common/util.cc
  3. 1
      common/util.h
  4. 1
      release/files_common
  5. 12
      tools/cabana/cabana.cc

@ -0,0 +1,34 @@
#pragma once
#include <cassert>
#include <string>
#include "common/params.h"
#include "common/util.h"
class OpenpilotPrefix {
public:
OpenpilotPrefix(std::string prefix = {}) {
if (prefix.empty()) {
prefix = util::random_string(15);
}
msgq_path = "/dev/shm/" + prefix;
bool ret = util::create_directories(msgq_path, 0777);
assert(ret);
setenv("OPENPILOT_PREFIX", prefix.c_str(), 1);
}
~OpenpilotPrefix() {
auto param_path = Params().getParamPath();
if (util::file_exists(param_path)) {
std::string real_path = util::readlink(param_path);
system(util::string_format("rm %s -rf", real_path.c_str()).c_str());
unlink(param_path.c_str());
}
system(util::string_format("rm %s -rf", msgq_path.c_str()).c_str());
unsetenv("OPENPILOT_PREFIX");
}
private:
std::string msgq_path;
};

@ -10,6 +10,7 @@
#include <dirent.h> #include <dirent.h>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <random>
#include <sstream> #include <sstream>
#ifdef __linux__ #ifdef __linux__
@ -228,6 +229,18 @@ std::string hexdump(const uint8_t* in, const size_t size) {
return ss.str(); return ss.str();
} }
std::string random_string(std::string::size_type length) {
const char* chrs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::mt19937 rg{std::random_device{}()};
std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
std::string s;
s.reserve(length);
while (length--) {
s += chrs[pick(rg)];
}
return s;
}
std::string dir_name(std::string const &path) { std::string dir_name(std::string const &path) {
size_t pos = path.find_last_of("/"); size_t pos = path.find_last_of("/");
if (pos == std::string::npos) return ""; if (pos == std::string::npos) return "";

@ -75,6 +75,7 @@ int getenv(const char* key, int default_val);
float getenv(const char* key, float default_val); float getenv(const char* key, float default_val);
std::string hexdump(const uint8_t* in, const size_t size); std::string hexdump(const uint8_t* in, const size_t size);
std::string random_string(std::string::size_type length);
std::string dir_name(std::string const& path); std::string dir_name(std::string const& path);
// **** file fhelpers ***** // **** file fhelpers *****

@ -146,6 +146,7 @@ selfdrive/debug/vw_mqb_config.py
common/SConscript common/SConscript
common/version.h common/version.h
common/prefix.h
common/swaglog.h common/swaglog.h
common/swaglog.cc common/swaglog.cc
common/statlog.h common/statlog.h

@ -1,8 +1,7 @@
#include <QApplication> #include <QApplication>
#include <QDir>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QUuid>
#include "common/prefix.h"
#include "selfdrive/ui/qt/util.h" #include "selfdrive/ui/qt/util.h"
#include "tools/cabana/mainwin.h" #include "tools/cabana/mainwin.h"
@ -23,12 +22,6 @@ int main(int argc, char *argv[]) {
cmd_parser.showHelp(); cmd_parser.showHelp();
} }
QString uuid = QUuid::createUuid().toString(QUuid::WithoutBraces);
QString msgq_path = "/dev/shm/" + uuid;
QDir dir;
dir.mkdir(msgq_path);
setenv("OPENPILOT_PREFIX", qPrintable(uuid), 1);
const QString route = args.empty() ? DEMO_ROUTE : args.first(); const QString route = args.empty() ? DEMO_ROUTE : args.first();
uint32_t replay_flags = REPLAY_FLAG_NONE; uint32_t replay_flags = REPLAY_FLAG_NONE;
@ -38,6 +31,7 @@ int main(int argc, char *argv[]) {
replay_flags |= REPLAY_FLAG_QCAMERA; replay_flags |= REPLAY_FLAG_QCAMERA;
} }
OpenpilotPrefix op_prefix;
CANMessages p(&app); CANMessages p(&app);
int ret = 0; int ret = 0;
if (p.loadRoute(route, cmd_parser.value("data_dir"), replay_flags)) { if (p.loadRoute(route, cmd_parser.value("data_dir"), replay_flags)) {
@ -45,7 +39,5 @@ int main(int argc, char *argv[]) {
w.showMaximized(); w.showMaximized();
ret = app.exec(); ret = app.exec();
} }
dir.rmdir(msgq_path);
return ret; return ret;
} }

Loading…
Cancel
Save