Fixed: Fresh install of 1.2.0 complains about missing sensor config keys. (LP :#1650378)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 5 Feb 2017 23:38:47 +0000 (00:38 +0100)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 5 Feb 2017 23:38:47 +0000 (00:38 +0100)
NEWS
src/cfg.c
src/cfg.h
src/main.c

diff --git a/NEWS b/NEWS
index 0a6a2d1..1e9d4ae 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,8 @@ v1.2.1
 * documentation files are generated with asciidoctor instead of
   asciidoc.
 * added support of BCM2835 which is mostly used in the Raspberry PI3.
+* Fresh install of 1.2.0 complains about missing sensor config
+  keys. (LP :#1650378)
 
 v1.2.0
 ------
index d94f256..61e8b0c 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -602,7 +602,10 @@ static char *sensor_get_str(const char *sid, const char *att)
        return g_key_file_get_string(kfile, sid, att, NULL);
 }
 
-static bool sensor_get_double(const char *sid, const char *att, double *d)
+static bool sensor_get_double(const char *sid,
+                             const char *att,
+                             double *d,
+                             double d_default)
 {
        GKeyFile *kfile;
        GError *err;
@@ -611,6 +614,18 @@ static bool sensor_get_double(const char *sid, const char *att, double *d)
        kfile = get_sensor_key_file();
 
        err = NULL;
+       if (!g_key_file_has_key(kfile, sid, att, &err)) {
+               if (err) {
+                       log_err(err->message);
+                       g_error_free(err);
+                       return false;
+               }
+
+               *d = d_default;
+               return true;
+       }
+
+       err = NULL;
        v = g_key_file_get_double(kfile, sid, att, &err);
 
        if (err) {
@@ -770,9 +785,11 @@ void config_set_sensor_graph_enabled(const char *sid, bool enabled)
        sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
 }
 
-bool config_get_sensor_alarm_high_threshold(const char *sid, double *v)
+bool config_get_sensor_alarm_high_threshold(const char *sid,
+                                           double *v,
+                                           double d)
 {
-       return sensor_get_double(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, v);
+       return sensor_get_double(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, v, d);
 }
 
 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
@@ -780,9 +797,9 @@ void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
        sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
 }
 
-bool config_get_sensor_alarm_low_threshold(const char *sid, double *v)
+bool config_get_sensor_alarm_low_threshold(const char *sid, double *v, double d)
 {
-       return sensor_get_double(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, v);
+       return sensor_get_double(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, v, d);
 }
 
 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
index 524e513..9860aa1 100644 (file)
--- a/src/cfg.h
+++ b/src/cfg.h
@@ -76,10 +76,10 @@ void config_cleanup(void);
 GdkRGBA *config_get_sensor_color(const char *);
 void config_set_sensor_color(const char *, const GdkRGBA *);
 
-bool config_get_sensor_alarm_high_threshold(const char *, double *);
+bool config_get_sensor_alarm_high_threshold(const char *, double *, double);
 void config_set_sensor_alarm_high_threshold(const char *, int);
 
-bool config_get_sensor_alarm_low_threshold(const char *, double *);
+bool config_get_sensor_alarm_low_threshold(const char *, double *, double);
 void config_set_sensor_alarm_low_threshold(const char *, int);
 
 bool config_get_sensor_alarm_enabled(const char *);
index a24c636..2868142 100644 (file)
@@ -229,7 +229,7 @@ associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
 {
        bool ret;
        struct psensor *s;
-       double high_temp;
+       double high_temp, high_default, low_default;
 
        high_temp = config_get_default_high_threshold_temperature();
 
@@ -239,23 +239,31 @@ associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
                s->cb_alarm_raised = cb_alarm_raised;
                s->cb_alarm_raised_data = ui;
 
-               ret = config_get_sensor_alarm_high_threshold
-                       (s->id, &s->alarm_high_threshold);
-
-               if (!ret) {
-                       if (s->max == UNKNOWN_DBL_VALUE) {
-                               if (s->type & SENSOR_TYPE_TEMP)
-                                       s->alarm_high_threshold = high_temp;
-                       } else {
-                               s->alarm_high_threshold = s->max;
-                       }
+               if (s->max == UNKNOWN_DBL_VALUE) {
+                       if (s->type & SENSOR_TYPE_TEMP)
+                               high_default = high_temp;
+                       else
+                               high_default = UNKNOWN_DBL_VALUE;
+               } else {
+                       high_default = s->max;
                }
 
+               ret = config_get_sensor_alarm_high_threshold
+                       (s->id, &s->alarm_high_threshold, high_default);
+
+               if (!ret)
+                       s->alarm_high_threshold = high_default;
+
+               if (s->min == UNKNOWN_DBL_VALUE)
+                       low_default = 0;
+               else
+                       low_default = s->min;
+
                ret = config_get_sensor_alarm_low_threshold
-                       (s->id, &s->alarm_low_threshold);
+                       (s->id, &s->alarm_low_threshold, low_default);
 
-               if (!ret && s->min != UNKNOWN_DBL_VALUE)
-                       s->alarm_low_threshold = s->min;
+               if (!ret)
+                       s->alarm_low_threshold = low_default;
 
                sensors++;
        }