Cleanup util.h (#20849)
* cleanup util.h * fix build error:omx_encoder.cc:408: undefined reference to do_exit * fix build error * rebase master * move helper functions back to util * ExitHandleHelper->ExitHandlerHelper * std::clamp * struct ExitHandlerHelper to class * rebase master * cleanup * restore ExitHandler * prefer cassertlocal_plotjuggler
parent
f184bae8e0
commit
65e0b1e699
|
@ -2,13 +2,17 @@
|
|||
|
||||
#include "ae_gray_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <cassert>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
#include "selfdrive/common/util.h"
|
||||
#include "selfdrive/camerad/cameras/camera_common.h"
|
||||
|
||||
// needed by camera_common.cc
|
||||
ExitHandler do_exit;
|
||||
|
||||
void camera_autoexposure(CameraState *s, float grey_frac) {}
|
||||
|
||||
int main() {
|
||||
|
|
|
@ -2,7 +2,12 @@
|
|||
|
||||
#include <errno.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
|
@ -97,5 +102,74 @@ int write_file(const char* path, const void* data, size_t size, int flags, mode_
|
|||
return (n >= 0 && (size_t)n == size) ? 0 : -1;
|
||||
}
|
||||
|
||||
std::string readlink(const std::string &path) {
|
||||
char buff[4096];
|
||||
ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1);
|
||||
if (len != -1) {
|
||||
buff[len] = '\0';
|
||||
return std::string(buff);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& fn) {
|
||||
std::ifstream f(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 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]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
std::string dir_name(std::string const &path) {
|
||||
size_t pos = path.find_last_of("/");
|
||||
if (pos == std::string::npos) return "";
|
||||
return path.substr(0, pos);
|
||||
}
|
||||
|
||||
struct tm get_time(){
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
|
||||
struct tm sys_time;
|
||||
gmtime_r(&rawtime, &sys_time);
|
||||
|
||||
return sys_time;
|
||||
}
|
||||
|
||||
bool time_valid(struct tm sys_time){
|
||||
int year = 1900 + sys_time.tm_year;
|
||||
int month = 1 + sys_time.tm_mon;
|
||||
return (year > 2020) || (year == 2020 && month >= 10);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
|
|
|
@ -2,23 +2,16 @@
|
|||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <ctime>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include <ctime>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#ifndef sighandler_t
|
||||
typedef void (*sighandler_t)(int sig);
|
||||
|
@ -31,113 +24,53 @@ int set_core_affinity(int core);
|
|||
|
||||
namespace util {
|
||||
|
||||
// Time helpers
|
||||
inline struct tm get_time(){
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
|
||||
struct tm sys_time;
|
||||
gmtime_r(&rawtime, &sys_time);
|
||||
|
||||
return sys_time;
|
||||
}
|
||||
|
||||
inline bool time_valid(struct tm sys_time){
|
||||
int year = 1900 + sys_time.tm_year;
|
||||
int month = 1 + sys_time.tm_mon;
|
||||
return (year > 2020) || (year == 2020 && month >= 10);
|
||||
}
|
||||
// ***** Time helpers *****
|
||||
struct tm get_time();
|
||||
bool time_valid(struct tm sys_time);
|
||||
|
||||
// ***** math helpers *****
|
||||
|
||||
// map x from [a1, a2] to [b1, b2]
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T map_val(T x, T a1, T a2, T b1, T b2) {
|
||||
x = std::clamp(x, a1, a2);
|
||||
T ra = a2 - a1;
|
||||
T rb = b2 - b1;
|
||||
return (x - a1)*rb / ra + b1;
|
||||
return (x - a1) * rb / ra + b1;
|
||||
}
|
||||
|
||||
// ***** string helpers *****
|
||||
|
||||
inline bool starts_with(const std::string &s, const std::string &prefix) {
|
||||
inline bool starts_with(const std::string& s, const std::string& prefix) {
|
||||
return s.compare(0, prefix.size(), prefix) == 0;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline std::string string_format(const std::string& format, Args... args) {
|
||||
std::string string_format(const std::string& format, Args... args) {
|
||||
size_t size = snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
snprintf(buf.get(), size, format.c_str(), args...);
|
||||
return std::string(buf.get(), buf.get() + size - 1);
|
||||
}
|
||||
|
||||
std::string read_file(const std::string &fn);
|
||||
|
||||
int read_files_in_dir(std::string path, std::map<std::string, std::string> *contents);
|
||||
std::string getenv_default(const char* env_var, const char* suffix, const char* 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 dir_name(std::string const& path);
|
||||
|
||||
// **** file fhelpers *****
|
||||
std::string read_file(const std::string& fn);
|
||||
int read_files_in_dir(std::string path, std::map<std::string, std::string>* contents);
|
||||
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<char[]> hexbuf(new char[buf_size*2+1]);
|
||||
for (size_t i=0; i < buf_size; i++) {
|
||||
sprintf(&hexbuf[i*2], "%02x", buf[i]);
|
||||
}
|
||||
hexbuf[buf_size*2] = 0;
|
||||
return std::string(hexbuf.get(), hexbuf.get() + buf_size*2);
|
||||
}
|
||||
|
||||
inline 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);
|
||||
}
|
||||
|
||||
inline std::string dir_name(std::string const & path) {
|
||||
size_t pos = path.find_last_of("/");
|
||||
if (pos == std::string::npos) return "";
|
||||
return path.substr(0, pos);
|
||||
}
|
||||
|
||||
inline std::string readlink(const std::string &path) {
|
||||
char buff[4096];
|
||||
ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1);
|
||||
if (len != -1) {
|
||||
buff[len] = '\0';
|
||||
return std::string(buff);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
inline 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 readlink(const std::string& path);
|
||||
bool file_exists(const std::string& fn);
|
||||
|
||||
inline void sleep_for(const int milliseconds) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
||||
}
|
||||
|
||||
inline bool file_exists(const std::string& fn) {
|
||||
std::ifstream f(fn);
|
||||
return f.good();
|
||||
}
|
||||
|
||||
inline 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();
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace util
|
||||
|
||||
class ExitHandler {
|
||||
public:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include <cassert>
|
||||
|
||||
#include <kaitai/kaitaistream.h>
|
||||
|
||||
#include "cereal/messaging/messaging.h"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
Import('env', 'cereal', 'messaging', 'arch')
|
||||
Import('env', 'cereal', 'messaging', 'common', 'arch')
|
||||
|
||||
if arch == "aarch64":
|
||||
env.Program('logcatd', 'logcatd_android.cc', LIBS=[cereal, messaging, 'cutils', 'zmq', 'capnp', 'kj'])
|
||||
env.Program('logcatd', 'logcatd_android.cc', LIBS=[cereal, messaging, common, 'cutils', 'zmq', 'capnp', 'kj'])
|
||||
else:
|
||||
env.Program('logcatd', 'logcatd_systemd.cc', LIBS=[cereal, messaging, 'zmq', 'capnp', 'kj', 'systemd', 'json11'])
|
||||
env.Program('logcatd', 'logcatd_systemd.cc', LIBS=[cereal, messaging, common, 'zmq', 'capnp', 'kj', 'systemd', 'json11'])
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
|
Loading…
Reference in New Issue