avoid to update SMART more than each 30s (costly operation for the udisks2 daemon)
[psensor.git] / src / lib / psensor.h
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 #ifndef _PSENSOR_PSENSOR_H_
20 #define _PSENSOR_PSENSOR_H_
21
22 #include <sensors/sensors.h>
23
24 #include "config.h"
25 #ifdef HAVE_ATASMART
26 #include <atasmart.h>
27 #endif
28
29 #include "bool.h"
30 #include "color.h"
31 #include <plog.h>
32 #include "measure.h"
33
34 enum psensor_type {
35         /* type of sensor values */
36         SENSOR_TYPE_TEMP = 0x00001,
37         SENSOR_TYPE_RPM = 0x00002,
38         SENSOR_TYPE_PERCENT = 0x00004,
39
40         /* Whether the sensor is remote */
41         SENSOR_TYPE_REMOTE = 0x00008,
42
43         /* Libraries used for retrieving sensor information */
44         SENSOR_TYPE_LMSENSOR = 0x00100,
45         SENSOR_TYPE_NVCTRL = 0x00200,
46         SENSOR_TYPE_GTOP = 0x00400,
47         SENSOR_TYPE_ATIADL = 0x00800,
48         SENSOR_TYPE_ATASMART = 0x01000,
49         SENSOR_TYPE_HDDTEMP = 0x02000,
50         SENSOR_TYPE_UDISKS2 = 0x800000,
51
52         /* Type of HW component */
53         SENSOR_TYPE_HDD = 0x04000,
54         SENSOR_TYPE_CPU = 0x08000,
55         SENSOR_TYPE_GPU = 0x10000,
56         SENSOR_TYPE_FAN = 0x20000,
57
58         SENSOR_TYPE_GRAPHICS = 0x40000,
59         SENSOR_TYPE_VIDEO = 0x80000,
60         SENSOR_TYPE_PCIE = 0x100000,
61         SENSOR_TYPE_MEMORY = 0x200000,
62         SENSOR_TYPE_AMBIENT = 0x400000,
63
64         /* Combinations */
65         SENSOR_TYPE_HDD_TEMP = (SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP),
66         SENSOR_TYPE_CPU_USAGE = (SENSOR_TYPE_CPU | SENSOR_TYPE_PERCENT)
67 };
68
69 struct psensor {
70         /* Human readable name of the sensor.  It may not be uniq. */
71         char *name;
72
73         /* Uniq id of the sensor */
74         char *id;
75
76         /* Name of the chip. */
77         char *chip;
78
79         /* lm-sensor */
80         const sensors_chip_name *iname;
81
82         const sensors_feature *feature;
83
84         /* Maximum length of 'values' */
85         int values_max_length;
86
87         /* Last registered measures of the sensor.  Index 0 for the
88            oldest measure.  */
89         struct measure *measures;
90
91         /* Color of the sensor used for the graph */
92         struct color *color;
93
94         /* Whether the graph sensor is displayed. */
95         bool graph_enabled;
96
97         /* see psensor_type */
98         unsigned int type;
99
100         /* The maximum detected value of the sensor */
101         double max;
102
103         /* The minimum detected value of the sensor */
104         double min;
105
106         /* Whether alarm alert is enabled for this sensor */
107         bool alarm_enabled;
108
109         int alarm_high_threshold;
110         int alarm_low_threshold;
111
112         /* Whether an alarm is raised for this sensor */
113         unsigned int alarm_raised;
114
115         void (*cb_alarm_raised)(struct psensor *, void *);
116         void *cb_alarm_raised_data;
117
118 #ifdef HAVE_NVIDIA
119         /* Nvidia id for the nvctrl */
120         int nvidia_id;
121 #endif
122 #ifdef HAVE_LIBATIADL
123         /* AMD id for the aticonfig */
124         int amd_id;
125 #endif
126 #ifdef HAVE_ATASMART
127         SkDisk *disk;
128 #endif
129
130         void *provider_data;
131         void (*provider_data_free_fct)(void *);
132
133         char *url;
134
135         bool appindicator_enabled;
136 };
137
138 struct psensor *psensor_create(char *id,
139                                char *name,
140                                char *chip,
141                                unsigned int type,
142                                int values_max_length);
143
144 void psensor_values_resize(struct psensor *s, int new_size);
145
146 void psensor_free(struct psensor *sensor);
147
148 void psensor_list_free(struct psensor **sensors);
149 int psensor_list_size(struct psensor **sensors);
150
151 struct psensor **psensor_list_filter_graph_enabled(struct psensor **);
152
153 struct psensor *psensor_list_get_by_id(struct psensor **sensors,
154                                        const char *id);
155
156 /*
157   Return 1 if there is at least one sensor of a given type, else
158   returns 0 */
159 int psensor_list_contains_type(struct psensor **sensors, unsigned int type);
160
161 int is_temp_type(unsigned int type);
162
163 double get_min_temp(struct psensor **sensors);
164 double get_max_temp(struct psensor **sensors);
165
166 double get_min_rpm(struct psensor **sensors);
167 double get_max_rpm(struct psensor **sensors);
168
169 /*
170   Get the maximal current value of all sensors of a given type.
171 */
172 double
173 psensor_get_max_current_value(struct psensor **sensors, unsigned int type);
174
175 /*
176   Converts the value of a sensor to a string.
177
178   parameter 'type' is SENSOR_TYPE_LMSENSOR_TEMP, SENSOR_TYPE_NVIDIA,
179   or SENSOR_TYPE_LMSENSOR_FAN
180 */
181 char *psensor_value_to_str(unsigned int type,
182                            double value,
183                            int use_celsius);
184
185 char *psensor_measure_to_str(const struct measure *m,
186                              unsigned int type,
187                              unsigned int use_celsius);
188
189 struct psensor **get_all_sensors(int use_libatasmart, int values_max_length);
190
191 struct psensor **psensor_list_add(struct psensor **sensors,
192                                   struct psensor *sensor);
193
194 void psensor_list_append(struct psensor ***sensors, struct psensor *sensor);
195
196 struct psensor **psensor_list_copy(struct psensor **);
197
198 void psensor_set_current_value(struct psensor *sensor, double value);
199 void psensor_set_current_measure(struct psensor *sensor, double value,
200                                  struct timeval tv);
201
202 double psensor_get_current_value(const struct psensor *);
203
204 struct measure *psensor_get_current_measure(struct psensor *sensor);
205
206 /*
207   Returns a string representation of a psensor type.
208 */
209 const char *psensor_type_to_str(unsigned int type);
210
211 const char *psensor_type_to_unit_str(unsigned int type, int use_celsius);
212
213 void psensor_list_update_measures(struct psensor **sensors);
214
215 void psensor_init();
216
217 void psensor_cleanup();
218
219 double get_max_value(struct psensor **sensors, int type);
220
221 char *psensor_current_value_to_str(const struct psensor *, unsigned int);
222
223 void psensor_log_measures(struct psensor **sensors);
224
225 #endif