X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fpsensor.c;h=c028c5ef8ae8af7a27b29797ed9a81fa4b18a317;hb=b028afb4f41b141ee26e113b8b27f0d086dc8e1e;hp=138b19f3a1b9a59099e93dca198a59fa0ad48d2b;hpb=e57d7f801ae20f02e0cb28107a89b31fa51de1c1;p=psensor.git diff --git a/src/lib/psensor.c b/src/lib/psensor.c index 138b19f..c028c5e 100644 --- a/src/lib/psensor.c +++ b/src/lib/psensor.c @@ -32,14 +32,18 @@ #endif -struct psensor *psensor_create(char *id, char *name, - unsigned int type, int values_max_length) +struct psensor *psensor_create(char *id, + char *name, + char *chip, + unsigned int type, + int values_max_length) { struct psensor *psensor = (struct psensor *)malloc(sizeof(struct psensor)); psensor->id = id; psensor->name = name; + psensor->chip = chip; psensor->enabled = 1; psensor->min = UNKNOWN_DBL_VALUE; psensor->max = UNKNOWN_DBL_VALUE; @@ -49,18 +53,20 @@ struct psensor *psensor_create(char *id, char *name, psensor->values_max_length = values_max_length; psensor->measures = measures_dbl_create(values_max_length); - psensor->alarm_limit = 0; + psensor->alarm_enabled = 0; + psensor->alarm_high_threshold = 0; + psensor->alarm_low_threshold = 0; psensor->cb_alarm_raised = NULL; psensor->cb_alarm_raised_data = NULL; psensor->alarm_raised = 0; - psensor->alarm_enabled = 0; - psensor->url = NULL; psensor->color = NULL; + psensor->appindicator_enabled = 0; + return psensor; } @@ -89,9 +95,14 @@ void psensor_values_resize(struct psensor *s, int new_size) void psensor_free(struct psensor *sensor) { if (sensor) { + log_debug("Cleanup %s", sensor->id); + free(sensor->name); free(sensor->id); + if (sensor->chip) + free(sensor->chip); + if (sensor->color) free(sensor->color); @@ -198,38 +209,46 @@ int is_fan_type(unsigned int type) return type & SENSOR_TYPE_FAN; } -static double celcius_to_fahrenheit(double c) +double celcius_to_fahrenheit(double c) { return c * (9.0/5.0) + 32; } -char *psensor_value_to_string(unsigned int type, - double value, - int use_celcius) +double fahrenheit_to_celcius(double f) +{ + return (f - 32) * (5.0/9.0); +} + +char * +psensor_value_to_str(unsigned int type, double value, int use_celcius) { - /* should not be possible to exceed 20 characters with temp or - rpm values the .x part is never displayed */ - char *str = malloc(20); + char *str; + const char *unit; - char *unit; + /* + * should not be possible to exceed 20 characters with temp or + * rpm values the .x part is never displayed + */ + str = malloc(20); - if (is_temp_type(type)) - if (use_celcius) { - unit = "C"; - } else { - unit = "F"; - value = celcius_to_fahrenheit(value); - } - else if (type & SENSOR_TYPE_CPU_USAGE) - unit = "%"; - else - unit = ""; + unit = psensor_type_to_unit_str(type, use_celcius); + + if (!use_celcius) + value = celcius_to_fahrenheit(value); sprintf(str, "%.0f%s", value, unit); return str; } +char * +psensor_measure_to_str(const struct measure *m, + unsigned int type, + unsigned int use_celcius) +{ + return psensor_value_to_str(type, m->value, use_celcius); +} + void psensor_set_current_value(struct psensor *sensor, double value) { struct timeval tv; @@ -248,7 +267,7 @@ psensor_set_current_measure(struct psensor *s, &s->measures[1], (s->values_max_length - 1) * sizeof(struct measure)); - s->measures[s->values_max_length - 1].value.d_num = v; + s->measures[s->values_max_length - 1].value = v; s->measures[s->values_max_length - 1].time = tv; if (s->min == UNKNOWN_DBL_VALUE || v < s->min) @@ -257,11 +276,10 @@ psensor_set_current_measure(struct psensor *s, if (s->max == UNKNOWN_DBL_VALUE || v > s->max) s->max = v; - if (s->alarm_limit && s->alarm_enabled) { - if (v > s->alarm_limit) { + if (s->alarm_enabled) { + if (v > s->alarm_high_threshold || v < s->alarm_low_threshold) { if (!s->alarm_raised && s->cb_alarm_raised) - s->cb_alarm_raised(s, - s->cb_alarm_raised_data); + s->cb_alarm_raised(s, s->cb_alarm_raised_data); s->alarm_raised = 1; } else { @@ -272,7 +290,7 @@ psensor_set_current_measure(struct psensor *s, double psensor_get_current_value(struct psensor *sensor) { - return sensor->measures[sensor->values_max_length - 1].value.d_num; + return sensor->measures[sensor->values_max_length - 1].value; } struct measure *psensor_get_current_measure(struct psensor *sensor) @@ -297,7 +315,7 @@ double get_min_value(struct psensor **sensors, int type) double t; for (i = 0; i < sensor->values_max_length; i++) { - t = sensor->measures[i].value.d_num; + t = sensor->measures[i].value; if (t == UNKNOWN_DBL_VALUE) continue; @@ -328,7 +346,7 @@ double get_max_value(struct psensor **sensors, int type) int i; double t; for (i = 0; i < sensor->values_max_length; i++) { - t = sensor->measures[i].value.d_num; + t = sensor->measures[i].value; if (t == UNKNOWN_DBL_VALUE) continue; @@ -387,7 +405,7 @@ double get_max_temp(struct psensor **sensors) struct psensor **get_all_sensors(int use_libatasmart, int values_max_length) { - struct psensor **psensors = NULL; + struct psensor **psensors; struct psensor **tmp_psensors; psensors = lmsensor_psensor_list_add(NULL, values_max_length); @@ -421,19 +439,22 @@ struct psensor **get_all_sensors(int use_libatasmart, int values_max_length) const char *psensor_type_to_str(unsigned int type) { - if ((type & SENSOR_TYPE_NVIDIA_TEMP) == SENSOR_TYPE_NVIDIA_TEMP) - return "NVidia GPU Temperature"; - - if ((type & SENSOR_TYPE_AMD_TEMP) == SENSOR_TYPE_AMD_TEMP) - return "AMD GPU Temperature"; - - if ((type & SENSOR_TYPE_AMD_FAN) == SENSOR_TYPE_AMD_FAN) - return "AMD GPU Fan Speed"; + if (type & SENSOR_TYPE_NVCTRL) + return "NVidia GPU"; + + if (type & SENSOR_TYPE_ATIADL) { + if (type & SENSOR_TYPE_TEMP) + return "AMD GPU Temperature"; + else if (type & SENSOR_TYPE_RPM) + return "AMD GPU Fan Speed"; + else /* type & SENSOR_TYPE_USAGE */ + return "AMD GPU Usage"; + } if ((type & SENSOR_TYPE_HDD_TEMP) == SENSOR_TYPE_HDD_TEMP) return "HDD Temperature"; - if (type & SENSOR_TYPE_CPU_USAGE) + if ((type & SENSOR_TYPE_CPU_USAGE) == SENSOR_TYPE_CPU_USAGE) return "CPU Usage"; if (type & SENSOR_TYPE_TEMP) @@ -442,25 +463,30 @@ const char *psensor_type_to_str(unsigned int type) if (type & SENSOR_TYPE_FAN) return "Fan"; + if (type & SENSOR_TYPE_CPU) + return "CPU"; + if (type & SENSOR_TYPE_REMOTE) return "Remote"; - return "N/A"; /* should not be possible */ + return "N/A"; } -const char *psensor_type_to_unit_str(unsigned int type) +const char *psensor_type_to_unit_str(unsigned int type, int use_celcius) { - if (type & SENSOR_TYPE_TEMP) - return _("C"); - - if (type & SENSOR_TYPE_FAN) + if (is_temp_type(type)) { + if (use_celcius) + return "\302\260C"; + else + return "\302\260F"; + } else if (is_fan_type(type)) { return _("RPM"); - - if (type & SENSOR_TYPE_CPU_USAGE) + } else if (type & SENSOR_TYPE_CPU_USAGE) { return _("%"); - - return "N/A"; + } else { + return _("N/A"); + } } void psensor_list_update_measures(struct psensor **sensors) @@ -471,16 +497,27 @@ void psensor_list_update_measures(struct psensor **sensors) cpu_psensor_list_update(sensors); #endif - if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDD_TEMP_HDDTEMP)) + if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDDTEMP)) hddtemp_psensor_list_update(sensors); #ifdef HAVE_ATASMART - if (psensor_list_contains_type(sensors, - SENSOR_TYPE_HDD_TEMP_ATASMART)) + if (psensor_list_contains_type(sensors, SENSOR_TYPE_ATASMART)) hdd_psensor_list_update(sensors); #endif } +void psensor_log_measures(struct psensor **sensors) +{ + if (log_level == LOG_DEBUG) + while (*sensors) { + log_debug("Measure: %s %.2f", + (*sensors)->name, + psensor_get_current_value(*sensors)); + + sensors++; + } +} + void psensor_init() { lmsensor_init();