Merge tag 'upstream/0.8.0.5'
[psensor-pkg-debian.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 "log.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_USAGE = 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
51         /* Type of HW component */
52         SENSOR_TYPE_HDD = 0x04000,
53         SENSOR_TYPE_CPU = 0x08000,
54         SENSOR_TYPE_GPU = 0x10000,
55         SENSOR_TYPE_FAN = 0x20000,
56
57         /* Combinations */
58         SENSOR_TYPE_HDD_TEMP = (SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP),
59         SENSOR_TYPE_CPU_USAGE = (SENSOR_TYPE_CPU | SENSOR_TYPE_USAGE)
60 };
61
62 struct psensor {
63         /* Human readable name of the sensor.  It may not be uniq. */
64         char *name;
65
66         /* Uniq id of the sensor */
67         char *id;
68
69         /* Name of the chip. */
70         char *chip;
71
72         /* lm-sensor */
73         const sensors_chip_name *iname;
74         const sensors_feature *feature;
75
76         /* Maximum length of 'values' */
77         int values_max_length;
78
79         /* Last registered measures of the sensor.  Index 0 for the
80            oldest measure.  */
81         struct measure *measures;
82
83         /* Color of the sensor used for the graph */
84         struct color *color;
85
86         /* Whether the graph sensor is displayed. */
87         bool graph_enabled;
88
89         /* see psensor_type */
90         unsigned int type;
91
92         /* The maximum detected value of the sensor */
93         double max;
94
95         /* The minimum detected value of the sensor */
96         double min;
97
98         /* Whether alarm alert is enabled for this sensor */
99         bool alarm_enabled;
100
101         int alarm_high_threshold;
102         int alarm_low_threshold;
103
104         /* Whether an alarm is raised for this sensor */
105         unsigned int alarm_raised;
106
107         void (*cb_alarm_raised) (struct psensor *, void *);
108         void *cb_alarm_raised_data;
109
110 #ifdef HAVE_NVIDIA
111         /* Nvidia id for the nvctrl */
112         int nvidia_id;
113 #endif
114 #ifdef HAVE_LIBATIADL
115         /* AMD id for the aticonfig */
116         int amd_id;
117 #endif
118 #ifdef HAVE_ATASMART
119         SkDisk *disk;
120 #endif
121         char *url;
122
123         bool appindicator_enabled;
124 };
125
126 struct psensor *psensor_create(char *id,
127                                char *name,
128                                char *chip,
129                                unsigned int type,
130                                int values_max_length);
131
132 void psensor_values_resize(struct psensor *s, int new_size);
133
134 void psensor_free(struct psensor *sensor);
135
136 void psensor_list_free(struct psensor **sensors);
137 int psensor_list_size(struct psensor **sensors);
138
139 struct psensor **psensor_list_filter_graph_enabled(struct psensor **);
140
141 struct psensor *psensor_list_get_by_id(struct psensor **sensors,
142                                        const char *id);
143
144 /*
145   Return 1 if there is at least one sensor of a given type, else
146   returns 0 */
147 int psensor_list_contains_type(struct psensor **sensors, unsigned int type);
148
149 int is_temp_type(unsigned int type);
150 int is_fan_type(unsigned int type);
151
152 double get_min_temp(struct psensor **sensors);
153 double get_max_temp(struct psensor **sensors);
154
155 double get_min_rpm(struct psensor **sensors);
156 double get_max_rpm(struct psensor **sensors);
157
158 /*
159   Get the maximal current value of all sensors of a given type.
160 */
161 double
162 psensor_get_max_current_value(struct psensor **sensors, unsigned int type);
163
164 /*
165   Converts the value of a sensor to a string.
166
167   parameter 'type' is SENSOR_TYPE_LMSENSOR_TEMP, SENSOR_TYPE_NVIDIA,
168   or SENSOR_TYPE_LMSENSOR_FAN
169 */
170 char *psensor_value_to_str(unsigned int type,
171                            double value,
172                            int use_celcius);
173
174 char *psensor_measure_to_str(const struct measure *m,
175                              unsigned int type,
176                              unsigned int use_celcius);
177
178 struct psensor **get_all_sensors(int use_libatasmart, int values_max_length);
179
180 struct psensor **psensor_list_add(struct psensor **sensors,
181                                   struct psensor *sensor);
182
183 struct psensor **psensor_list_copy(struct psensor **);
184
185 void psensor_set_current_value(struct psensor *sensor, double value);
186 void psensor_set_current_measure(struct psensor *sensor, double value,
187                                  struct timeval tv);
188
189 double psensor_get_current_value(const struct psensor *);
190
191 struct measure *psensor_get_current_measure(struct psensor *sensor);
192
193 /*
194   Returns a string representation of a psensor type.
195 */
196 const char *psensor_type_to_str(unsigned int type);
197
198 const char *psensor_type_to_unit_str(unsigned int type, int use_celcius);
199
200 void psensor_list_update_measures(struct psensor **sensors);
201
202 void psensor_init();
203
204 void psensor_cleanup();
205
206 double get_max_value(struct psensor **sensors, int type);
207
208 double celcius_to_fahrenheit(double c);
209 double fahrenheit_to_celcius(double c);
210
211 char *psensor_current_value_to_str(const struct psensor *, unsigned int);
212
213 void psensor_log_measures(struct psensor **sensors);
214
215 #endif