diff --git a/common/util.cc b/common/util.cc index 5ffd6e4099f27b..f4c69eb269f6d0 100644 --- a/common/util.cc +++ b/common/util.cc @@ -255,6 +255,29 @@ bool ends_with(const std::string& s, const std::string& suffix) { strcmp(s.c_str() + (s.size() - suffix.size()), suffix.c_str()) == 0; } +std::string strip(const std::string &str) { + auto should_trim = [](unsigned char ch) { + // trim whitespace or a null character + return std::isspace(ch) || ch == '\0'; + }; + + size_t start = 0; + while (start < str.size() && should_trim(static_cast(str[start]))) { + start++; + } + + if (start == str.size()) { + return ""; + } + + size_t end = str.size() - 1; + while (end > 0 && should_trim(static_cast(str[end]))) { + end--; + } + + return str.substr(start, end - start + 1); +} + std::string check_output(const std::string& command) { char buffer[128]; std::string result; diff --git a/common/util.h b/common/util.h index 7cfa008509e258..d4106b50028ca4 100644 --- a/common/util.h +++ b/common/util.h @@ -74,6 +74,7 @@ float getenv(const char* key, float default_val); std::string hexdump(const uint8_t* in, const size_t size); bool starts_with(const std::string &s1, const std::string &s2); bool ends_with(const std::string &s, const std::string &suffix); +std::string strip(const std::string &str); // ***** random helpers ***** int random_int(int min, int max); diff --git a/system/hardware/tici/hardware.h b/system/hardware/tici/hardware.h index da89bb9002a052..fcfb07830a4b41 100644 --- a/system/hardware/tici/hardware.h +++ b/system/hardware/tici/hardware.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -22,7 +23,7 @@ class HardwareTici : public HardwareNone { static std::string get_name() { std::string model = util::read_file("/sys/firmware/devicetree/base/model"); - return model.substr(std::string("comma ").size()); + return util::strip(model.substr(std::string("comma ").size())); } static cereal::InitData::DeviceType get_device_type() { @@ -32,7 +33,8 @@ class HardwareTici : public HardwareNone { {"mici", cereal::InitData::DeviceType::MICI} }; auto it = device_map.find(get_name()); - return it != device_map.end() ? it->second : cereal::InitData::DeviceType::UNKNOWN; + assert(it != device_map.end()); + return it->second; } static int get_voltage() { return std::atoi(util::read_file("/sys/class/hwmon/hwmon1/in1_input").c_str()); }