Support for chip/feature/subfeature selection

Added -cp/-sp command line options which allow finer control on which
sensor is to be used as a data source for CPU or system, respectively.
This commit is contained in:
Emmanuel BENOîT 2021-05-10 13:03:58 +02:00
parent 5acfc0aed4
commit 947b14d1b7
3 changed files with 132 additions and 69 deletions

45
main.c
View file

@ -64,6 +64,7 @@ static void switch_light(void);
static void draw_cpudigit(int per); static void draw_cpudigit(int per);
static void draw_sysdigit(int per); static void draw_sysdigit(int per);
static void parse_arguments(int argc, char **argv); static void parse_arguments(int argc, char **argv);
static void parse_sensor_path(sensor_path_t *path, char *argument);
static void print_help(char *prog); static void print_help(char *prog);
int main(int argc, char **argv) int main(int argc, char **argv)
@ -363,11 +364,11 @@ static void parse_arguments(int argc, char **argv)
i++; i++;
} }
else if (!strcmp(argv[i], "-s")) { else if (!strcmp(argv[i], "-s")) {
char *t; sensor_path_t t;
t = cpu_feature_name; t = cpu_sensor_path;
cpu_feature_name = sys_feature_name; cpu_sensor_path = sys_sensor_path;
sys_feature_name = t; sys_sensor_path = t;
} }
else if (!strcmp(argv[i], "-cf")) { else if (!strcmp(argv[i], "-cf")) {
if (argc == i + 1) { if (argc == i + 1) {
@ -375,7 +376,7 @@ static void parse_arguments(int argc, char **argv)
exit(1); exit(1);
} }
cpu_feature_name = argv[i + 1]; cpu_sensor_path.subfeature = argv[i + 1];
i++; i++;
} }
else if (!strcmp(argv[i], "-sf")) { else if (!strcmp(argv[i], "-sf")) {
@ -384,7 +385,16 @@ static void parse_arguments(int argc, char **argv)
exit(1); exit(1);
} }
sys_feature_name = argv[i + 1]; sys_sensor_path.subfeature = argv[i + 1];
i++;
}
else if (!strcmp(argv[i], "-cp") || !strcmp(argv[i], "-sp")) {
if (argc == i + 1) {
fprintf(stderr, "%s: error parsing argument for option %s\n", argv[0], argv[i]);
exit(1);
}
parse_sensor_path(argv[i][1] == 'c' ? &cpu_sensor_path : &sys_sensor_path,
argv[i + 1]);
i++; i++;
} }
else if (!strcmp(argv[i], "-f")) else if (!strcmp(argv[i], "-f"))
@ -398,6 +408,25 @@ static void parse_arguments(int argc, char **argv)
} }
} }
static void parse_sensor_path(sensor_path_t *path, char *argument)
{
char const ** parts[] = { &path->chip, &path->feature, &path->subfeature };
int i = 0;
char * next;
while (i < 3) {
next = strchr(argument, '/');
if (next != argument) {
*parts[i] = argument;
}
if (!next) {
break;
}
*next = '\0';
argument = next + 1;
i ++;
}
}
static void print_help(char *prog) static void print_help(char *prog)
{ {
printf("Usage : %s [OPTIONS]\n", prog); printf("Usage : %s [OPTIONS]\n", prog);
@ -422,6 +451,10 @@ static void print_help(char *prog)
printf(" ('temp1_input' is default)\n"); printf(" ('temp1_input' is default)\n");
printf(" -sf <feature> which feature to use for sys temperature\n"); printf(" -sf <feature> which feature to use for sys temperature\n");
printf(" ('temp2_input' is default)\n"); printf(" ('temp2_input' is default)\n");
printf(" -cp <chip/feature/subft.> full cpu temperature sensor path\n");
printf(" ('//temp1_input' is default)\n");
printf(" -sp <chip/feature/subft.> full system temperature sensor path\n");
printf(" ('//temp2_input' is default)\n");
printf(" -s swap the cpu and sys temperatures\n"); printf(" -s swap the cpu and sys temperatures\n");
printf(" (/etc/sensors.conf is default)\n"); printf(" (/etc/sensors.conf is default)\n");
printf(" -f show temperatures in Fahrenheit\n"); printf(" -f show temperatures in Fahrenheit\n");

146
temp.c
View file

@ -1,25 +1,36 @@
#define DEBUG(x) x #define DEBUG(x) x;
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <sensors.h> #include <sensors.h>
#include <sensors/error.h>
#include "temp.h" #include "temp.h"
sensor_path_t cpu_sensor_path = { NULL, NULL, NULL };
sensor_path_t sys_sensor_path = { NULL, NULL, NULL };
temperature_t t_type = CELCIUS;
typedef struct sensor_source_t {
int valid;
sensors_chip_name const *chip;
int feature;
} sensor_source_t;
static FILE *f = NULL; static FILE *f = NULL;
static char sensors = 0; static char sensors = 0;
static const sensors_chip_name *chip_name = NULL; static sensor_source_t cpu_feature = {0, NULL, 0};
static const sensors_feature *feature; static sensor_source_t sys_feature = {0, NULL, 0};
char *cpu_feature_name = "temp1_input"; void temp_set_default(sensor_path_t *path, char const *subfeature)
char *sys_feature_name = "temp2_input"; {
if (!(path->chip || path->feature || path->subfeature)) {
temperature_t t_type = CELCIUS; path->subfeature = subfeature;
}
static int cpu_feature = 0; }
static int sys_feature = 0;
void temp_deinit() { void temp_deinit() {
if (f != NULL) { if (f != NULL) {
@ -31,17 +42,54 @@ void temp_deinit() {
} }
} }
void temp_init(const char *filename) { void temp_find_feature(sensor_path_t const *path, sensor_source_t *feature)
const sensors_chip_name *name; {
int chip_nr = 0, f1, f2; const sensors_chip_name *s_name;
const sensors_feature *s_feature;
const sensors_subfeature *s_subfeature;
char *s_feat_name;
char str[256]; char str[256];
int chip_nr = 0, f_nr;
char *feattext = NULL; while ((s_name = sensors_get_detected_chips(NULL, &chip_nr)) != NULL) {
const sensors_subfeature *subfeature; sensors_snprintf_chip_name(str, 256, s_name);
DEBUG(printf("L%d - chip_nr %d, name %s\n", __LINE__, chip_nr, str));
if (path->chip != NULL && strcasecmp(str, path->chip)) {
continue;
}
f_nr = 0;
while ((s_feature = sensors_get_features(s_name, &f_nr)) != NULL) {
s_feat_name = sensors_get_label(s_name, s_feature);
DEBUG(printf("L%d - feature #%d : %s\n", __LINE__, f_nr, s_feat_name));
if (path->feature != NULL && strcasecmp(s_feat_name, path->feature)) {
continue;
}
s_subfeature = sensors_get_subfeature(s_name, s_feature,
SENSORS_SUBFEATURE_TEMP_INPUT);
if (!s_subfeature) {
continue;
}
DEBUG(printf("L%d - subfeature %s\n", __LINE__, s_subfeature->name));
if (path->subfeature == NULL
|| !strcasecmp(s_subfeature->name, path->subfeature)) {
feature->valid = 1;
feature->chip = s_name;
feature->feature = s_subfeature->number;
DEBUG(printf("L%d - found\n", __LINE__));
return;
}
}
}
}
void temp_init(const char *filename) {
temp_set_default(&cpu_sensor_path, "temp1_input");
temp_set_default(&sys_sensor_path, "temp2_input");
atexit(temp_deinit); atexit(temp_deinit);
if (filename) { if (filename) {
f = fopen(filename, "r"); f = fopen(filename, "r");
if (f == NULL) { if (f == NULL) {
@ -57,52 +105,28 @@ void temp_init(const char *filename) {
} }
sensors = 1; sensors = 1;
while ((name = sensors_get_detected_chips(NULL, &chip_nr)) != NULL && temp_find_feature(&cpu_sensor_path, &cpu_feature);
chip_name == NULL) { temp_find_feature(&sys_sensor_path, &sys_feature);
f1 = f2 = 0; }
DEBUG(printf("chip_nr=%d %d\n",chip_nr,__LINE__);)
sensors_snprintf_chip_name(str, 256, name); unsigned int temp_read(sensor_source_t const *source) {
DEBUG(printf("chip name = %s (%d)\n",str,__LINE__);) double value;
if (source->valid) {
while ((feature = sensors_get_features( name, &f1)) != NULL) { if (sensors_get_value(source->chip, source->feature, &value)) {
return 0;
feattext = sensors_get_label( name, feature ); }
DEBUG(printf("f1=%d feattext=%s (%d) \n",f1,feattext,__LINE__);) if (t_type == FAHRENHEIT) {
value = TO_FAHRENHEIT(value);
if ( (subfeature = sensors_get_subfeature (name, feature, SENSORS_SUBFEATURE_TEMP_INPUT)) ) { } else if (t_type == KELVIN) {
DEBUG(printf("subfeature name =%s (%d) \n",subfeature->name,__LINE__);) value = TO_KELVIN(value);
if (strcmp(subfeature->name, cpu_feature_name) == 0) { }
cpu_feature = subfeature->number; return (unsigned int)(value);
chip_name = name; } else {
} return 0;
else if (strcmp(subfeature->name, sys_feature_name) == 0) { }
sys_feature = subfeature->number;
}
}
}
}
if (chip_name == NULL) {
fprintf(stderr, "could not find a suitable chip\n");
exit(1);
}
} }
void temp_getusage(unsigned int *cpu_temp, unsigned int *sys_temp) { void temp_getusage(unsigned int *cpu_temp, unsigned int *sys_temp) {
double cpu, sys; *cpu_temp = temp_read(&cpu_feature);
*sys_temp = temp_read(&sys_feature);
sensors_get_value(chip_name, cpu_feature, &cpu);
sensors_get_value(chip_name, sys_feature, &sys);
if (t_type == FAHRENHEIT) {
cpu = TO_FAHRENHEIT(cpu);
sys = TO_FAHRENHEIT(sys);
} else if (t_type == KELVIN) {
cpu = TO_KELVIN(cpu);
sys = TO_KELVIN(sys);
}
*cpu_temp = (unsigned int)(cpu);
*sys_temp = (unsigned int)(sys);
} }

10
temp.h
View file

@ -11,8 +11,14 @@ typedef enum temperature_t {
CELCIUS, FAHRENHEIT, KELVIN CELCIUS, FAHRENHEIT, KELVIN
} temperature_t; } temperature_t;
extern char *cpu_feature_name; typedef struct sensor_path_t {
extern char *sys_feature_name; char const *chip;
char const *feature;
char const *subfeature;
} sensor_path_t;
extern sensor_path_t cpu_sensor_path;
extern sensor_path_t sys_sensor_path;
extern temperature_t t_type; extern temperature_t t_type;