alarm limit edition using the temperature unit set in the global pref.
[psensor.git] / src / lib / psensor.c
index 3cd6956..89ed97e 100644 (file)
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2012 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <stdlib.h>
 #include <string.h>
 
 #include "psensor.h"
 #include "lmsensor.h"
 
+#ifdef HAVE_GTOP
+#include "cpu.h"
+#endif
+
+
 struct psensor *psensor_create(char *id, char *name,
                               unsigned int type, int values_max_length)
 {
@@ -85,6 +89,8 @@ 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);
 
@@ -194,7 +200,19 @@ int is_fan_type(unsigned int type)
        return type & SENSOR_TYPE_FAN;
 }
 
-char *psensor_value_to_string(unsigned int type, double value)
+double celcius_to_fahrenheit(double c)
+{
+       return c * (9.0/5.0) + 32;
+}
+
+double fahrenheit_to_celcius(double f)
+{
+       return (f - 32) * (5.0/9.0);
+}
+
+char *psensor_value_to_string(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 */
@@ -203,7 +221,14 @@ char *psensor_value_to_string(unsigned int type, double value)
        char *unit;
 
        if (is_temp_type(type))
-               unit = "C";
+               if (use_celcius) {
+                       unit = "°C";
+               } else {
+                       unit = "°F";
+                       value = celcius_to_fahrenheit(value);
+               }
+       else if (type & SENSOR_TYPE_CPU_USAGE)
+               unit = "%";
        else
                unit = "";
 
@@ -298,7 +323,7 @@ double get_min_value(struct psensor **sensors, int type)
   Returns the maximal value of a given 'type' (SENSOR_TYPE_TEMP or
   SENSOR_TYPE_FAN)
  */
-static double get_max_value(struct psensor **sensors, int type)
+double get_max_value(struct psensor **sensors, int type)
 {
        double m = UNKNOWN_DBL_VALUE;
        struct psensor **s = sensors;
@@ -367,19 +392,31 @@ double get_max_temp(struct psensor **sensors)
        return get_max_value(sensors, SENSOR_TYPE_TEMP);
 }
 
-struct psensor **get_all_sensors(int values_max_length)
+struct psensor **get_all_sensors(int use_libatasmart, int values_max_length)
 {
        struct psensor **psensors = NULL;
        struct psensor **tmp_psensors;
 
        psensors = lmsensor_psensor_list_add(NULL, values_max_length);
 
-       tmp_psensors = hdd_psensor_list_add(psensors, values_max_length);
-
-       if (tmp_psensors != psensors) {
-               free(psensors);
-               psensors = tmp_psensors;
+       if (!use_libatasmart) {
+               tmp_psensors = hddtemp_psensor_list_add(psensors,
+                                                       values_max_length);
+               if (tmp_psensors != psensors) {
+                       free(psensors);
+                       psensors = tmp_psensors;
+               }
        }
+#ifdef HAVE_ATASMART
+               else {
+                       tmp_psensors = hdd_psensor_list_add(psensors,
+                                                           values_max_length);
+                       if (tmp_psensors != psensors) {
+                               free(psensors);
+                               psensors = tmp_psensors;
+                       }
+               }
+#endif
 
        if (!psensors) {        /* there is no detected sensors */
                psensors = malloc(sizeof(struct psensor *));
@@ -391,39 +428,49 @@ struct psensor **get_all_sensors(int values_max_length)
 
 const char *psensor_type_to_str(unsigned int type)
 {
-       if (type & SENSOR_TYPE_REMOTE)
-               return "Remote";
-
-       if (type & SENSOR_TYPE_LMSENSOR_TEMP)
-               return "Temperature";
-
-       if (type & SENSOR_TYPE_LMSENSOR_FAN)
-               return "Fan";
-
-       if (type & SENSOR_TYPE_NVIDIA)
+       if ((type & SENSOR_TYPE_NVIDIA_TEMP) == SENSOR_TYPE_NVIDIA_TEMP)
                return "NVidia GPU Temperature";
 
-       if (type & SENSOR_TYPE_AMD_TEMP)
+       if ((type & SENSOR_TYPE_AMD_TEMP) == SENSOR_TYPE_AMD_TEMP)
                return "AMD GPU Temperature";
 
-       if (type & SENSOR_TYPE_AMD_FAN)
+       if ((type & SENSOR_TYPE_AMD_FAN) == SENSOR_TYPE_AMD_FAN)
                return "AMD GPU Fan Speed";
 
-       if (type & SENSOR_TYPE_HDD_TEMP)
+       if ((type & SENSOR_TYPE_HDD_TEMP) == SENSOR_TYPE_HDD_TEMP)
                return "HDD Temperature";
 
+       if (type & SENSOR_TYPE_CPU_USAGE)
+               return "CPU Usage";
+
+       if (type & SENSOR_TYPE_TEMP)
+               return "Temperature";
+
+       if (type & SENSOR_TYPE_FAN)
+               return "Fan";
+
+       if (type & SENSOR_TYPE_REMOTE)
+               return "Remote";
+
        return "N/A";           /* should not be possible */
 }
 
 
-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_TEMP) {
+               if (use_celcius)
+                       return _("°C");
+               else
+                       return _("°F");
+       }
 
        if (type & SENSOR_TYPE_FAN)
                return _("RPM");
 
+       if (type & SENSOR_TYPE_CPU_USAGE)
+               return _("%");
+
        return "N/A";
 }
 
@@ -431,8 +478,30 @@ void psensor_list_update_measures(struct psensor **sensors)
 {
        lmsensor_psensor_list_update(sensors);
 
-       if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDD_TEMP))
+#ifdef HAVE_GTOP
+       cpu_psensor_list_update(sensors);
+#endif
+
+       if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDD_TEMP_HDDTEMP))
+               hddtemp_psensor_list_update(sensors);
+
+#ifdef HAVE_ATASMART
+       if (psensor_list_contains_type(sensors,
+                                      SENSOR_TYPE_HDD_TEMP_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()