sensors: Use hwmon interface for thermal.

Should work...
This commit is contained in:
Alastair Poole 2020-10-05 12:18:26 +01:00
parent 42b9ecadc6
commit 9fc5b94d6a
4 changed files with 76 additions and 30 deletions

View File

@ -84,6 +84,8 @@ system_info_all_free(Sys_Info *info)
snsr = info->sensors[i]; snsr = info->sensors[i];
if (snsr->name) if (snsr->name)
free(snsr->name); free(snsr->name);
if (snsr->child_name)
free(snsr->child_name);
free(snsr); free(snsr);
} }
if (info->sensors) if (info->sensors)

View File

@ -33,6 +33,7 @@ typedef struct
typedef struct typedef struct
{ {
char *name; char *name;
char *child_name;
double value; double value;
bool invalid; bool invalid;
} sensor_t; } sensor_t;

View File

@ -70,44 +70,78 @@ system_sensors_thermal_get(int *sensor_count)
} }
#elif defined(__linux__) #elif defined(__linux__)
sensor_t *sensor; sensor_t *sensor;
char *type, *value; DIR *dir;
struct dirent **names; struct dirent *dh;
int i, n; struct dirent **names = NULL;
char *link;
char buf[4096];
int seen[128];
int n, idx;
n = scandir("/sys/class/thermal", &names, 0, alphasort); dir = opendir("/sys/class/hwmon");
if (n < 0) return NULL; if (!dir) return NULL;
for (i = 0; i < n; i++) { while ((dh = readdir(dir)) != NULL)
if (strncmp(names[i]->d_name, "thermal_zone", 12)) {
snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s", dh->d_name);
link = realpath(buf, NULL);
if (!link) continue;
idx = 0;
memset(&seen, 0, sizeof(seen));
n = scandir(link, &names, 0, alphasort);
if (n < 0) continue;
for (int i = 0; i < n; i++)
{ {
free(names[i]); if (!strncmp(names[i]->d_name, "temp", 4))
continue;
}
type = file_contents(strsli_printf("/sys/class/thermal/%s/type", names[i]->d_name));
if (type)
{
sensors =
realloc(sensors, (1 + (*sensor_count)) * sizeof(sensor_t *));
sensors[(*sensor_count)++] =
sensor = calloc(1, sizeof(sensor_t));
sensor->name = type;
value = file_contents(strsli_printf("/sys/class/thermal/%s/temp", names[i]->d_name));
if (!value)
sensor->invalid = true;
else
{ {
sensor->value = (float)atoi(value) / 1000.0; int id = atoi(names[i]->d_name + 4);
free(value); if ((!id) || (id > sizeof(seen)))
{
free(names[i]);
continue;
}
int found = 0;
for (int j = 0; seen[j] != 0 && j < sizeof(seen); j++)
if (seen[j] == id) found = 1;
if (found)
{
free(names[i]);
continue;
}
sensors = realloc(sensors, (1 + (*sensor_count)) * sizeof(sensor_t *));
sensors[(*sensor_count)++] = sensor = calloc(1,sizeof(sensor_t));
snprintf(buf, sizeof(buf), "%s/name", link);
sensor->name = file_contents(buf);
snprintf(buf, sizeof(buf), "%s/temp%d_label", link, id);
sensor->child_name = file_contents(buf);
snprintf(buf, sizeof(buf), "%s/temp%d_input", link, id);
char *d = file_contents(buf);
if (d)
{
sensor->value = atoi(d);
if (sensor->value) sensor->value /= 1000;
free(d);
}
seen[idx++] = id;
} }
free(names[i]);
} }
free(names[i]); free(names);
free(link);
} }
free(names); closedir(dir);
#elif defined(__MacOS__) #elif defined(__MacOS__)
#endif #endif
return sensors; return sensors;

View File

@ -71,6 +71,7 @@ static Eina_Bool
_sensor_usage_add(Evas_Object *box, sensor_t **sensors, int count) _sensor_usage_add(Evas_Object *box, sensor_t **sensors, int count)
{ {
Evas_Object *frame, *vbox, *hbox, *pb, *ic, *label; Evas_Object *frame, *vbox, *hbox, *pb, *ic, *label;
Eina_Strbuf *name;
sensor_t *snsr; sensor_t *snsr;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -94,8 +95,14 @@ _sensor_usage_add(Evas_Object *box, sensor_t **sensors, int count)
evas_object_show(label); evas_object_show(label);
elm_box_pack_end(vbox, label); elm_box_pack_end(vbox, label);
name = eina_strbuf_new();
eina_strbuf_append(name, snsr->name);
if (snsr->child_name)
eina_strbuf_append_printf(name, " (%s)", snsr->child_name);
elm_object_text_set(label, eina_slstr_printf("%s", elm_object_text_set(label, eina_slstr_printf("%s",
snsr->name)); eina_strbuf_string_get(name)));
eina_strbuf_free(name);
hbox = elm_box_add(box); hbox = elm_box_add(box);
evas_object_size_hint_align_set(hbox, FILL, FILL); evas_object_size_hint_align_set(hbox, FILL, FILL);
@ -170,6 +177,8 @@ _misc_free(power_t *power, sensor_t **sensors, int sensor_count)
sensor_t *snsr = sensors[i]; sensor_t *snsr = sensors[i];
if (snsr->name) if (snsr->name)
free(snsr->name); free(snsr->name);
if (snsr->child_name)
free(snsr->child_name);
free(snsr); free(snsr);
} }
if (sensors) if (sensors)