Skip to content

Commit b36db78

Browse files
tici: fix cpp device type (commaai#34315)
fix cpp
1 parent 9cf02ca commit b36db78

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

common/util.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,29 @@ bool ends_with(const std::string& s, const std::string& suffix) {
255255
strcmp(s.c_str() + (s.size() - suffix.size()), suffix.c_str()) == 0;
256256
}
257257

258+
std::string strip(const std::string &str) {
259+
auto should_trim = [](unsigned char ch) {
260+
// trim whitespace or a null character
261+
return std::isspace(ch) || ch == '\0';
262+
};
263+
264+
size_t start = 0;
265+
while (start < str.size() && should_trim(static_cast<unsigned char>(str[start]))) {
266+
start++;
267+
}
268+
269+
if (start == str.size()) {
270+
return "";
271+
}
272+
273+
size_t end = str.size() - 1;
274+
while (end > 0 && should_trim(static_cast<unsigned char>(str[end]))) {
275+
end--;
276+
}
277+
278+
return str.substr(start, end - start + 1);
279+
}
280+
258281
std::string check_output(const std::string& command) {
259282
char buffer[128];
260283
std::string result;

common/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ float getenv(const char* key, float default_val);
7474
std::string hexdump(const uint8_t* in, const size_t size);
7575
bool starts_with(const std::string &s1, const std::string &s2);
7676
bool ends_with(const std::string &s, const std::string &suffix);
77+
std::string strip(const std::string &str);
7778

7879
// ***** random helpers *****
7980
int random_int(int min, int max);

system/hardware/tici/hardware.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstdlib>
4+
#include <cassert>
45
#include <fstream>
56
#include <map>
67
#include <string>
@@ -22,7 +23,7 @@ class HardwareTici : public HardwareNone {
2223

2324
static std::string get_name() {
2425
std::string model = util::read_file("/sys/firmware/devicetree/base/model");
25-
return model.substr(std::string("comma ").size());
26+
return util::strip(model.substr(std::string("comma ").size()));
2627
}
2728

2829
static cereal::InitData::DeviceType get_device_type() {
@@ -32,7 +33,8 @@ class HardwareTici : public HardwareNone {
3233
{"mici", cereal::InitData::DeviceType::MICI}
3334
};
3435
auto it = device_map.find(get_name());
35-
return it != device_map.end() ? it->second : cereal::InitData::DeviceType::UNKNOWN;
36+
assert(it != device_map.end());
37+
return it->second;
3638
}
3739

3840
static int get_voltage() { return std::atoi(util::read_file("/sys/class/hwmon/hwmon1/in1_input").c_str()); }

0 commit comments

Comments
 (0)