style
[psensor.git] / src / lib / lmsensor.c
index 8f7999b..bf76c3a 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-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
+ */
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
 
 #include "psensor.h"
 
-double
-lmsensor_get_value(const sensors_chip_name *name,
-                  const sensors_subfeature *sub)
+static int init_done;
+
+static double get_value(const sensors_chip_name *name,
+                       const sensors_subfeature *sub)
 {
        double val;
        int err;
@@ -42,12 +42,12 @@ lmsensor_get_value(const sensors_chip_name *name,
                fprintf(stderr,
                        _("ERROR: Can't get value of subfeature %s: %s\n"),
                        sub->name, sensors_strerror(err));
-               val = UNKNOWN_VALUE;
+               val = UNKNOWN_DBL_VALUE;
        }
        return val;
 }
 
-double lmsensor_get_temp_input(struct psensor *sensor)
+static double get_temp_input(struct psensor *sensor)
 {
        const sensors_chip_name *chip = sensor->iname;
        const sensors_feature *feature = sensor->feature;
@@ -57,12 +57,12 @@ double lmsensor_get_temp_input(struct psensor *sensor)
        sf = sensors_get_subfeature(chip,
                                    feature, SENSORS_SUBFEATURE_TEMP_INPUT);
        if (sf)
-               return lmsensor_get_value(chip, sf);
+               return get_value(chip, sf);
        else
-               return UNKNOWN_VALUE;
+               return UNKNOWN_DBL_VALUE;
 }
 
-double lmsensor_get_fan_input(struct psensor *sensor)
+static double get_fan_input(struct psensor *sensor)
 {
        const sensors_chip_name *chip = sensor->iname;
        const sensors_feature *feature = sensor->feature;
@@ -72,30 +72,33 @@ double lmsensor_get_fan_input(struct psensor *sensor)
        sf = sensors_get_subfeature(chip,
                                    feature, SENSORS_SUBFEATURE_FAN_INPUT);
        if (sf)
-               return lmsensor_get_value(chip, sf);
+               return get_value(chip, sf);
        else
-               return UNKNOWN_VALUE;
+               return UNKNOWN_DBL_VALUE;
 }
 
 void lmsensor_psensor_list_update(struct psensor **sensors)
 {
        struct psensor **s_ptr = sensors;
 
+       if (!init_done)
+               return ;
+
        while (*s_ptr) {
                struct psensor *sensor = *s_ptr;
 
                if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
                        psensor_set_current_value
-                           (sensor, lmsensor_get_temp_input(sensor));
+                           (sensor, get_temp_input(sensor));
                else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
-                       psensor_set_current_value
-                           (sensor, lmsensor_get_fan_input(sensor));
+                       psensor_set_current_value(sensor,
+                                                 get_fan_input(sensor));
 
                s_ptr++;
        }
 }
 
-struct psensor *
+static struct psensor *
 lmsensor_psensor_create(const sensors_chip_name *chip,
                        const sensors_feature *feature,
                        int values_max_length)
@@ -124,7 +127,7 @@ lmsensor_psensor_create(const sensors_chip_name *chip,
        }
 
        sf = sensors_get_subfeature(chip, feature, fault_subfeature);
-       if (sf && lmsensor_get_value(chip, sf))
+       if (sf && get_value(chip, sf))
                return NULL;
 
        label = sensors_get_label(chip, feature);
@@ -149,7 +152,7 @@ lmsensor_psensor_create(const sensors_chip_name *chip,
        psensor->feature = feature;
 
        if (feature->type == SENSORS_FEATURE_TEMP
-           && (lmsensor_get_temp_input(psensor) == UNKNOWN_VALUE)) {
+           && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
                free(psensor);
                return NULL;
        }
@@ -157,7 +160,46 @@ lmsensor_psensor_create(const sensors_chip_name *chip,
        return psensor;
 }
 
-int lmsensor_init()
+struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
+                                          int vn)
+{
+       const sensors_chip_name *chip;
+       int chip_nr = 0;
+       struct psensor **tmp, **result;
+       const sensors_feature *feature;
+       struct psensor *s;
+       int i;
+
+       if (!init_done)
+               return NULL;
+
+       result = sensors;
+       while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
+
+               i = 0;
+               while ((feature = sensors_get_features(chip, &i))) {
+
+                       if (feature->type == SENSORS_FEATURE_TEMP
+                           || feature->type == SENSORS_FEATURE_FAN) {
+
+                               s = lmsensor_psensor_create(chip, feature, vn);
+
+                               if (s) {
+                                       tmp = psensor_list_add(result, s);
+
+                                       if (tmp != sensors)
+                                               free(result);
+
+                                       result = tmp;
+                               }
+                       }
+               }
+       }
+
+       return result;
+}
+
+void lmsensor_init()
 {
        int err = sensors_init(NULL);
 
@@ -165,8 +207,14 @@ int lmsensor_init()
                fprintf(stderr,
                        _("ERROR: lm-sensors initialization failure: %s\n"),
                        sensors_strerror(err));
-               return 0;
+               init_done = 0;
        } else {
-               return 1;
+               init_done = 1;
        }
 }
+
+void lmsensor_cleanup()
+{
+       if (init_done)
+               sensors_cleanup();
+}