Skip to content

Commit

Permalink
tici: fix cpp device type (commaai#34315)
Browse files Browse the repository at this point in the history
fix cpp
  • Loading branch information
adeebshihadeh authored Dec 27, 2024
1 parent 9cf02ca commit b36db78
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
23 changes: 23 additions & 0 deletions common/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>(str[start]))) {
start++;
}

if (start == str.size()) {
return "";
}

size_t end = str.size() - 1;
while (end > 0 && should_trim(static_cast<unsigned char>(str[end]))) {
end--;
}

return str.substr(start, end - start + 1);
}

std::string check_output(const std::string& command) {
char buffer[128];
std::string result;
Expand Down
1 change: 1 addition & 0 deletions common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions system/hardware/tici/hardware.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdlib>
#include <cassert>
#include <fstream>
#include <map>
#include <string>
Expand All @@ -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() {
Expand All @@ -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()); }
Expand Down

0 comments on commit b36db78

Please sign in to comment.