measure with value of different types
[psensor.git] / src / lib / lmsensor.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include <locale.h>
21 #include <libintl.h>
22 #define _(str) gettext(str)
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <sensors/sensors.h>
29 #include <sensors/error.h>
30
31 #include "psensor.h"
32
33 double
34 lmsensor_get_value(const sensors_chip_name *name,
35                    const sensors_subfeature *sub)
36 {
37         double val;
38         int err;
39
40         err = sensors_get_value(name, sub->number, &val);
41         if (err) {
42                 fprintf(stderr,
43                         _("ERROR: Can't get value of subfeature %s: %s\n"),
44                         sub->name, sensors_strerror(err));
45                 val = UNKNOWN_DBL_VALUE;
46         }
47         return val;
48 }
49
50 double lmsensor_get_temp_input(struct psensor *sensor)
51 {
52         const sensors_chip_name *chip = sensor->iname;
53         const sensors_feature *feature = sensor->feature;
54
55         const sensors_subfeature *sf;
56
57         sf = sensors_get_subfeature(chip,
58                                     feature, SENSORS_SUBFEATURE_TEMP_INPUT);
59         if (sf)
60                 return lmsensor_get_value(chip, sf);
61         else
62                 return UNKNOWN_DBL_VALUE;
63 }
64
65 double lmsensor_get_fan_input(struct psensor *sensor)
66 {
67         const sensors_chip_name *chip = sensor->iname;
68         const sensors_feature *feature = sensor->feature;
69
70         const sensors_subfeature *sf;
71
72         sf = sensors_get_subfeature(chip,
73                                     feature, SENSORS_SUBFEATURE_FAN_INPUT);
74         if (sf)
75                 return lmsensor_get_value(chip, sf);
76         else
77                 return UNKNOWN_DBL_VALUE;
78 }
79
80 void lmsensor_psensor_list_update(struct psensor **sensors)
81 {
82         struct psensor **s_ptr = sensors;
83
84         while (*s_ptr) {
85                 struct psensor *sensor = *s_ptr;
86
87                 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
88                         psensor_set_current_value
89                             (sensor, lmsensor_get_temp_input(sensor));
90                 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
91                         psensor_set_current_value
92                             (sensor, lmsensor_get_fan_input(sensor));
93
94                 s_ptr++;
95         }
96 }
97
98 struct psensor *
99 lmsensor_psensor_create(const sensors_chip_name *chip,
100                         const sensors_feature *feature,
101                         int values_max_length)
102 {
103         char name[200];
104         const sensors_subfeature *sf;
105         char *label;
106         int type;
107         char *id;
108         struct psensor *psensor;
109         sensors_subfeature_type fault_subfeature;
110
111         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
112                 return NULL;
113
114         if (feature->type == SENSORS_FEATURE_TEMP) {
115                 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
116
117         } else if (feature->type == SENSORS_FEATURE_FAN) {
118                 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
119
120         } else {
121                 fprintf(stderr,
122                         _("ERROR: create_sensor, wrong feature type\n"));
123                 return NULL;
124         }
125
126         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
127         if (sf && lmsensor_get_value(chip, sf))
128                 return NULL;
129
130         label = sensors_get_label(chip, feature);
131         if (!label)
132                 return NULL;
133
134         type = 0;
135         if (feature->type == SENSORS_FEATURE_TEMP)
136                 type = SENSOR_TYPE_LMSENSOR_TEMP;
137         else if (feature->type == SENSORS_FEATURE_FAN)
138                 type = SENSOR_TYPE_LMSENSOR_FAN;
139         else
140                 return NULL;
141
142         id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
143                     1);
144         sprintf(id, "lmsensor %s %s", name, label);
145
146         psensor = psensor_create(id, label, type, values_max_length);
147
148         psensor->iname = chip;
149         psensor->feature = feature;
150
151         if (feature->type == SENSORS_FEATURE_TEMP
152             && (lmsensor_get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
153                 free(psensor);
154                 return NULL;
155         }
156
157         return psensor;
158 }
159
160 int lmsensor_init()
161 {
162         int err = sensors_init(NULL);
163
164         if (err) {
165                 fprintf(stderr,
166                         _("ERROR: lm-sensors initialization failure: %s\n"),
167                         sensors_strerror(err));
168                 return 0;
169         } else {
170                 return 1;
171         }
172 }