Skip to content

Commit b3fa9e7

Browse files
committed
Improve computer model detection when device tree is not available
Closes #614.
1 parent fbe812b commit b3fa9e7

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

uhubctl.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,39 @@ static int ports2bitmap(char* const portlist)
396396
static int get_computer_model(char *model, int len)
397397
{
398398
int fd = open("/sys/firmware/devicetree/base/model", O_RDONLY);
399-
if (fd < 0) {
400-
return fd;
401-
}
402-
int bytes_read = read(fd, model, len-1);
403-
close(fd);
404-
if (bytes_read < 0) {
405-
return -1;
399+
if (fd >= 0) {
400+
int bytes_read = read(fd, model, len-1);
401+
close(fd);
402+
if (bytes_read < 0) {
403+
return -1;
404+
}
405+
model[bytes_read] = 0;
406+
} else {
407+
// devicetree is not available, try parsing /proc/cpuinfo instead.
408+
// most Raspberry Pi have /proc/cpuinfo about 1KB, so 4KB buffer should be plenty:
409+
char buffer[4096] = {0}; // fill buffer with all zeros
410+
fd = open("/proc/cpuinfo", O_RDONLY);
411+
if (fd < 0) {
412+
return -1;
413+
}
414+
int bytes_read = read(fd, buffer, sizeof(buffer)-1);
415+
close(fd);
416+
if (bytes_read < 0) {
417+
return -1;
418+
}
419+
buffer[bytes_read] = 0;
420+
char* model_start = strstr(buffer, "Model\t\t: ");
421+
if (model_start == NULL) {
422+
return -1;
423+
}
424+
char* model_name = model_start + 9;
425+
char* newline_pos = strchr(model_name, '\n');
426+
if (newline_pos != NULL) {
427+
*newline_pos = 0;
428+
}
429+
strncpy(model, model_name, len);
430+
model[len-1] = 0;
406431
}
407-
model[bytes_read] = 0;
408432
return 0;
409433
}
410434

0 commit comments

Comments
 (0)