+rpi_hw_types_t rpi_peripheral_find_hw_type(void)
+{
+ FILE *f_cpuinfo;
+ char *line = NULL;
+ size_t line_cap = 0;
+ size_t line_len;
+ const char *p = "Unknown";
+ const rpi_hw_types_map_t *tm;
+
+ if (rpi_hw_type != RPI_HW_TYPE_UNKNOWN)
+ return rpi_hw_type;
+
+ f_cpuinfo = fopen("/proc/cpuinfo", "r");
+ if (f_cpuinfo == NULL)
+ return RPI_HW_TYPE_ERROR;
+
+ while ((line_len = getline(&line, &line_cap, f_cpuinfo)) != -1) {
+ if (strncmp(line, "Hardware", 8) == 0) {
+ if (line[line_len - 1] == '\n') {
+ line[line_len - 1] = 0;
+ }
+ p = line + 8;
+ while (*p && isblank(*p))
+ p++;
+ if (*p != ':')
+ continue;
+ p++;
+ while (*p && isblank(*p))
+ p++;
+ for (tm = rpi_hw_types_map; tm->hw_text; tm++) {
+ if (!strcmp(p, tm->hw_text)) {
+ rpi_hw_type = tm->hw_code;
+ }
+ }
+ }
+ }
+
+ fclose(f_cpuinfo);
+
+ if (line != NULL)
+ free(line);
+
+ return rpi_hw_type;
+}
+