Skip to content

Commit 37c5c18

Browse files
authored
feat: get device name from cpuinfo (#4)
1 parent 33f45a2 commit 37c5c18

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/core/runtime.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ static const char *k_usage =
4242
// bm_app_name can reference it at any time after init.
4343
const char *bm_sbc_app_name_runtime = "bm_sbc";
4444

45+
// Read Serial and Model from /proc/cpuinfo and build a device name.
46+
// Returns "rpi_<serial>" if Model contains "Raspberry Pi", otherwise
47+
// just "<serial>". Falls back to BM_SBC_DEVICE_NAME if unavailable.
48+
static const char *read_device_name() {
49+
static char buf[32];
50+
FILE *f = fopen("/proc/cpuinfo", "r");
51+
if (!f) return BM_SBC_DEVICE_NAME;
52+
53+
char serial_str[32] = {0};
54+
bool is_rpi = false;
55+
char line[128];
56+
while (fgets(line, sizeof(line), f)) {
57+
unsigned long long serial = 0;
58+
if (sscanf(line, "Serial : %llx", &serial) == 1) {
59+
snprintf(serial_str, sizeof(serial_str), "%llx", serial);
60+
} else if (strstr(line, "Model") && strstr(line, "Raspberry Pi")) {
61+
is_rpi = true;
62+
}
63+
}
64+
fclose(f);
65+
66+
if (serial_str[0] == '\0') return BM_SBC_DEVICE_NAME;
67+
snprintf(buf, sizeof(buf), "%s%s", is_rpi ? "rpi_" : "", serial_str);
68+
return buf;
69+
}
70+
4571
int bm_sbc_runtime_init(int argc, char **argv, const char *app_name) {
4672
bm_sbc_app_name_runtime = app_name;
4773
// Make stdout line-buffered so every bm_debug/printf call ending in '\n'
@@ -152,7 +178,7 @@ int bm_sbc_runtime_init(int argc, char **argv, const char *app_name) {
152178
memset(&dev_cfg, 0, sizeof(dev_cfg));
153179
dev_cfg.node_id = vpc.own_node_id;
154180
dev_cfg.git_sha = BM_SBC_GIT_SHA;
155-
dev_cfg.device_name = BM_SBC_DEVICE_NAME;
181+
dev_cfg.device_name = read_device_name();
156182
dev_cfg.version_string = version_str_buf;
157183
dev_cfg.vendor_id = BM_SBC_VENDOR_ID;
158184
dev_cfg.product_id = BM_SBC_PRODUCT_ID;

0 commit comments

Comments
 (0)