From eba601a964cc55d44274e04d29d47610e224675b Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 23 Sep 2007 12:00:59 +0000 Subject: [PATCH] Split sensors_get_all_features() into two distinct functions, one to get the list of all main features, and one to get the list of all the subfeatures of a given main feature. This is a more logical interface for applications to use. The current implementation is admittedly less than optimal, because the storage structures weren't meant for it, but this issue can (and will) be addressed later. git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@4831 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/access.c | 35 +++++++++++++++++++-- lib/libsensors.3 | 28 +++++++++++------ lib/sensors.h | 22 ++++++++----- prog/sensord/chips.c | 35 +++++++++------------ prog/sensord/sense.c | 25 ++++++++------- prog/sensors/chips.c | 75 ++++++++++++++++++++++---------------------- 6 files changed, 130 insertions(+), 90 deletions(-) diff --git a/lib/access.c b/lib/access.c index 1de8e4af..44f9c611 100644 --- a/lib/access.c +++ b/lib/access.c @@ -343,9 +343,8 @@ const char *sensors_get_adapter_name(const sensors_bus_id *bus) return NULL; } -/* nr-1 is the last feature returned */ -const sensors_feature_data *sensors_get_all_features(const sensors_chip_name *name, - int *nr) +static const sensors_feature_data * +sensors_get_all_features(const sensors_chip_name *name, int *nr) { sensors_feature_data *feature_list; int i; @@ -363,6 +362,36 @@ const sensors_feature_data *sensors_get_all_features(const sensors_chip_name *na return NULL; } +const sensors_feature_data * +sensors_get_features(const sensors_chip_name *name, int *nr) +{ + const sensors_feature_data *feature; + + while ((feature = sensors_get_all_features(name, nr))) { + if (feature->mapping == SENSORS_NO_MAPPING) + return feature; + } + return NULL; /* end of list */ +} + +const sensors_feature_data * +sensors_get_all_subfeatures(const sensors_chip_name *name, int feature, int *nr) +{ + const sensors_feature_data *subfeature; + + /* Seek directly to the first subfeature */ + if (*nr < feature) + *nr = feature; + + subfeature = sensors_get_all_features(name, nr); + if (!subfeature) + return NULL; /* end of list */ + if (subfeature->number == feature || + subfeature->mapping == feature) + return subfeature; + return NULL; /* end of subfeature list */ +} + /* Evaluate an expression */ int sensors_eval_expr(const sensors_chip_name *name, const sensors_expr *expr, diff --git a/lib/libsensors.3 b/lib/libsensors.3 index 43a8f2c1..94f7ea4d 100644 --- a/lib/libsensors.3 +++ b/lib/libsensors.3 @@ -44,8 +44,10 @@ libsensors \- publicly accessible functions provided by the sensors library .B int sensors_do_chip_sets(const sensors_chip_name *name); .B const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name \fB*match, int *nr);\fP -.B const sensors_feature_data *sensors_get_all_features +.B const sensors_feature_data *sensors_get_features \fB(const sensors_chip_name *name, int *nr);\fP +.B const sensors_feature_data *sensors_get_all_subfeatures + \fB(const sensors_chip_name *name, int feature, int *nr);\fP .B const char *libsensors_version; .fi .SH DESCRIPTION @@ -121,16 +123,22 @@ The flags field is a bitfield, its value is a combination of SENSORS_MODE_R (readable), SENSORS_MODE_W (writable) and SENSORS_COMPUTE_MAPPING (affected by the computation rules of the main feature). -\fBconst sensors_feature_data *sensors_get_all_features - (const sensors_chip_name *name, int *nr);\fP +\fBconst sensors_feature_data *sensors_get_features(const sensors_chip_name *name, int *nr);\fP .br -This returns all features of a specific chip. They are returned in bunches: -everything with the same mapping is returned just after each other, with -the master feature in front (that feature does not map to itself, but -has SENSORS_NO_MAPPING as mapping field). nr is an internally used variable. -Set it to zero to start again at the begin of the list. If no more features -are found NULL is returned. Do not try to change the returned structure; you -will corrupt internal data structures. +This returns all main features of a specific chip. nr is an internally +used variable. Set it to zero to start at the begin of the list. If no +more features are found NULL is returned. +Do not try to change the returned structure; you will corrupt internal +data structures. + +\fBconst sensors_feature_data *sensors_get_all_subfeatures(const sensors_chip_name *name, int feature, int *nr);\fP +.br +This returns all subfeatures of a given main feature (including that +main feature itself, in first position.) nr is an internally used +variable. Set it to zero to start at the begin of the list. If no more +features are found NULL is returned. +Do not try to change the returned structure; you will corrupt internal +data structures. \fBconst char *libsensors_version;\fP .br diff --git a/lib/sensors.h b/lib/sensors.h index d734224e..81c465aa 100644 --- a/lib/sensors.h +++ b/lib/sensors.h @@ -183,16 +183,22 @@ typedef struct sensors_feature_data { unsigned int flags; } sensors_feature_data; -/* This returns all features of a specific chip. They are returned in - bunches: everything with the same mapping is returned just after each - other, with the master feature in front (that feature does not map to - itself, but has SENSORS_NO_MAPPING as mapping field). nr is - an internally used variable. Set it to zero to start again at the - begin of the list. If no more features are found NULL is returned. +/* This returns all main features of a specific chip. nr is an internally + used variable. Set it to zero to start at the begin of the list. If no + more features are found NULL is returned. Do not try to change the returned structure; you will corrupt internal data structures. */ -const sensors_feature_data *sensors_get_all_features - (const sensors_chip_name *name, int *nr); +const sensors_feature_data * +sensors_get_features(const sensors_chip_name *name, int *nr); + +/* This returns all subfeatures of a given main feature (including that + main feature itself, in first position.) nr is an internally used + variable. Set it to zero to start at the begin of the list. If no more + features are found NULL is returned. + Do not try to change the returned structure; you will corrupt internal + data structures. */ +const sensors_feature_data * +sensors_get_all_subfeatures(const sensors_chip_name *name, int feature, int *nr); #ifdef __cplusplus } diff --git a/prog/sensord/chips.c b/prog/sensord/chips.c index e7d5c60d..529553f4 100644 --- a/prog/sensord/chips.c +++ b/prog/sensord/chips.c @@ -142,14 +142,14 @@ rrdF3 static void getAvailableFeatures (const sensors_chip_name *name, const sensors_feature_data *feature, - int i, short *has_features, + short *has_features, int *feature_nrs, int size, int first_val) { const sensors_feature_data *iter; + int i = 0; - while ((iter = sensors_get_all_features (name, &i)) && - iter->mapping == feature->number) { + while ((iter = sensors_get_all_subfeatures(name, feature->number, &i))) { int index0; index0 = iter->type - first_val - 1; @@ -166,7 +166,7 @@ static void getAvailableFeatures (const sensors_chip_name *name, #define IN_FEATURE_NR(x) feature_nrs[x - SENSORS_FEATURE_IN - 1] static void fillChipVoltage (FeatureDescriptor *voltage, const sensors_chip_name *name, - const sensors_feature_data *feature, int i) + const sensors_feature_data *feature) { const int size = SENSORS_FEATURE_IN_BEEP - SENSORS_FEATURE_IN; short has_features[SENSORS_FEATURE_IN_BEEP - SENSORS_FEATURE_IN] = { 0, }; @@ -177,7 +177,7 @@ static void fillChipVoltage (FeatureDescriptor *voltage, voltage->type = DataType_voltage; voltage->dataNumbers[pos++] = feature->number; - getAvailableFeatures (name, feature, i, has_features, + getAvailableFeatures (name, feature, has_features, feature_nrs, size, SENSORS_FEATURE_IN); if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) && @@ -214,7 +214,7 @@ static void fillChipVoltage (FeatureDescriptor *voltage, #define TEMP_FEATURE_NR(x) feature_nrs[x - SENSORS_FEATURE_TEMP - 1] static void fillChipTemperature (FeatureDescriptor *temperature, const sensors_chip_name *name, - const sensors_feature_data *feature, int i) + const sensors_feature_data *feature) { const int size = SENSORS_FEATURE_TEMP_BEEP - SENSORS_FEATURE_TEMP; short has_features[SENSORS_FEATURE_TEMP_BEEP - SENSORS_FEATURE_TEMP] = { 0, }; @@ -225,7 +225,7 @@ static void fillChipTemperature (FeatureDescriptor *temperature, temperature->type = DataType_temperature; temperature->dataNumbers[pos++] = feature->number; - getAvailableFeatures (name, feature, i, has_features, + getAvailableFeatures (name, feature, has_features, feature_nrs, size, SENSORS_FEATURE_TEMP); if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN) && @@ -265,7 +265,7 @@ static void fillChipTemperature (FeatureDescriptor *temperature, #define FAN_FEATURE_NR(x) feature_nrs[x - SENSORS_FEATURE_FAN - 1] static void fillChipFan (FeatureDescriptor *fan, const sensors_chip_name *name, - const sensors_feature_data *feature, int i) + const sensors_feature_data *feature) { const int size = SENSORS_FEATURE_FAN_BEEP - SENSORS_FEATURE_FAN; short has_features[SENSORS_FEATURE_FAN_BEEP - SENSORS_FEATURE_FAN] = { 0, }; @@ -276,7 +276,7 @@ static void fillChipFan (FeatureDescriptor *fan, fan->type = DataType_rpm; fan->dataNumbers[pos++] = feature->number; - getAvailableFeatures (name, feature, i, has_features, + getAvailableFeatures (name, feature, has_features, feature_nrs, size, SENSORS_FEATURE_FAN); if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN)) { @@ -341,10 +341,8 @@ FeatureDescriptor * generateChipFeatures (const sensors_chip_name *chip) /* How many main features do we have? */ nr = 0; - while ((sensor = sensors_get_all_features(chip, &nr))) { - if (sensor->mapping == SENSORS_NO_MAPPING) - count++; - } + while ((sensor = sensors_get_features(chip, &nr))) + count++; /* Allocate the memory we need */ features = calloc(count, sizeof(FeatureDescriptor)); @@ -354,19 +352,16 @@ FeatureDescriptor * generateChipFeatures (const sensors_chip_name *chip) /* Fill in the data structures */ count = 0; nr = 0; - while ((sensor = sensors_get_all_features(chip, &nr))) { - if (sensor->mapping != SENSORS_NO_MAPPING) - continue; - + while ((sensor = sensors_get_features(chip, &nr))) { switch (sensor->type) { case SENSORS_FEATURE_TEMP: - fillChipTemperature(&features[count], chip, sensor, nr); + fillChipTemperature(&features[count], chip, sensor); break; case SENSORS_FEATURE_IN: - fillChipVoltage(&features[count], chip, sensor, nr); + fillChipVoltage(&features[count], chip, sensor); break; case SENSORS_FEATURE_FAN: - fillChipFan(&features[count], chip, sensor, nr); + fillChipFan(&features[count], chip, sensor); break; case SENSORS_FEATURE_VID: fillChipVid(&features[count], sensor); diff --git a/prog/sensord/sense.c b/prog/sensord/sense.c index ea6ef8cb..7861fa54 100644 --- a/prog/sensord/sense.c +++ b/prog/sensord/sense.c @@ -36,18 +36,21 @@ int getRawLabel (const sensors_chip_name *name, int feature, const char **label) { - const sensors_feature_data *rawFeature; - int nr = 0, err = 0; - do { - rawFeature = sensors_get_all_features (name, &nr); - } while (rawFeature && (rawFeature->number != feature)); - /* TODO: Ensure labels match RRD construct and are not repeated! */ - if (!rawFeature) { - err = -1; - } else { - *label = rawFeature->name; + const sensors_feature_data *mainfeat, *sub; + int a, b; + + a = 0; + while ((mainfeat = sensors_get_features(name, &a))) { + b = 0; + while ((sub = sensors_get_all_subfeatures(name, mainfeat->number, &b))) { + if (sub->number == feature) { + *label = sub->name; + return 0; + } + } } - return err; + /* TODO: Ensure labels match RRD construct and are not repeated! */ + return -1; } static const char * diff --git a/prog/sensors/chips.c b/prog/sensors/chips.c index bcd0b518..83d113c9 100644 --- a/prog/sensors/chips.c +++ b/prog/sensors/chips.c @@ -30,30 +30,34 @@ void print_chip_raw(const sensors_chip_name *name) { - int a; - const sensors_feature_data *data; + int a, b; + const sensors_feature_data *feature, *sub; char *label; double val; a = 0; - while ((data = sensors_get_all_features(name, &a))) { - if (!(label = sensors_get_label(name, data->number))) { - printf("ERROR: Can't get feature `%s' label!\n", - data->name); - continue; + while ((feature = sensors_get_features(name, &a))) { + b = 0; + while ((sub = sensors_get_all_subfeatures(name, feature->number, + &b))) { + if (!(label = sensors_get_label(name, sub->number))) { + printf("ERROR: Can't get feature `%s' label!\n", + sub->name); + continue; + } + if (sub->flags & SENSORS_MODE_R) { + if (sensors_get_value(name, sub->number, &val)) + printf("ERROR: Can't get feature `%s' " + "data!\n", sub->name); + else if (sub->mapping != SENSORS_NO_MAPPING) + printf(" %s: %.2f\n", label, val); + else + printf("%s: %.2f (%s)\n", label, val, + sub->name); + } else + printf("(%s)\n", label); + free(label); } - if (data->flags & SENSORS_MODE_R) { - if (sensors_get_value(name, data->number, &val)) - printf("ERROR: Can't get feature `%s' data!\n", - data->name); - else if (data->mapping != SENSORS_NO_MAPPING) - printf(" %s: %.2f\n", label, val); - else - printf("%s: %.2f (%s)\n", label, val, - data->name); - } else - printf("(%s)\n", label); - free(label); } } @@ -70,14 +74,14 @@ static void print_label(const char *label, int space) static void sensors_get_available_features(const sensors_chip_name *name, const sensors_feature_data *feature, - int i, short *has_features, + short *has_features, double *feature_vals, int size, int first_val) { const sensors_feature_data *iter; + int i = 0; - while ((iter = sensors_get_all_features(name, &i)) && - iter->mapping == feature->number) { + while ((iter = sensors_get_all_subfeatures(name, feature->number, &i))) { int indx, err; indx = iter->type - first_val - 1; @@ -104,9 +108,7 @@ static int sensors_get_label_size(const sensors_chip_name *name) unsigned int max_size = 11; /* 11 as minumum label width */ i = 0; - while ((iter = sensors_get_all_features(name, &i))) { - if (iter->mapping != SENSORS_NO_MAPPING) - continue; + while ((iter = sensors_get_features(name, &i))) { if ((label = sensors_get_label(name, iter->number)) && strlen(label) > max_size) max_size = strlen(label); @@ -141,7 +143,7 @@ static void print_temp_limits(double limit1, double limit2, #define TEMP_FEATURE(x) has_features[x - SENSORS_FEATURE_TEMP - 1] #define TEMP_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_TEMP - 1] static void print_chip_temp(const sensors_chip_name *name, - const sensors_feature_data *feature, int i, + const sensors_feature_data *feature, int label_size) { double val, limit1, limit2; @@ -163,7 +165,7 @@ static void print_chip_temp(const sensors_chip_name *name, return; } - sensors_get_available_features(name, feature, i, has_features, + sensors_get_available_features(name, feature, has_features, feature_vals, size, SENSORS_FEATURE_TEMP); @@ -279,7 +281,7 @@ static void print_chip_temp(const sensors_chip_name *name, #define IN_FEATURE(x) has_features[x - SENSORS_FEATURE_IN - 1] #define IN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_IN - 1] static void print_chip_in(const sensors_chip_name *name, - const sensors_feature_data *feature, int i, + const sensors_feature_data *feature, int label_size) { const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN; @@ -299,7 +301,7 @@ static void print_chip_in(const sensors_chip_name *name, return; } - sensors_get_available_features(name, feature, i, has_features, + sensors_get_available_features(name, feature, has_features, feature_vals, size, SENSORS_FEATURE_IN); print_label(label, label_size); @@ -344,7 +346,7 @@ static void print_chip_in(const sensors_chip_name *name, #define FAN_FEATURE(x) has_features[x - SENSORS_FEATURE_FAN - 1] #define FAN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_FAN - 1] static void print_chip_fan(const sensors_chip_name *name, - const sensors_feature_data *feature, int i, + const sensors_feature_data *feature, int label_size) { char *label; @@ -367,7 +369,7 @@ static void print_chip_fan(const sensors_chip_name *name, print_label(label, label_size); free(label); - sensors_get_available_features(name, feature, i, has_features, + sensors_get_available_features(name, feature, has_features, feature_vals, size, SENSORS_FEATURE_FAN); if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) && @@ -432,19 +434,16 @@ void print_chip(const sensors_chip_name *name) label_size = sensors_get_label_size(name); i = 0; - while ((feature = sensors_get_all_features(name, &i))) { - if (feature->mapping != SENSORS_NO_MAPPING) - continue; - + while ((feature = sensors_get_features(name, &i))) { switch (feature->type) { case SENSORS_FEATURE_TEMP: - print_chip_temp(name, feature, i, label_size); + print_chip_temp(name, feature, label_size); break; case SENSORS_FEATURE_IN: - print_chip_in(name, feature, i, label_size); + print_chip_in(name, feature, label_size); break; case SENSORS_FEATURE_FAN: - print_chip_fan(name, feature, i, label_size); + print_chip_fan(name, feature, label_size); break; case SENSORS_FEATURE_VID: print_chip_vid(name, feature->number, label_size);