use max/min of the sensor for the threshold if not overriden by the configuration
[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 <config.h>
23
24 #include <bool.h>
25 #include <measure.h>
26 #include <plog.h>
27
28 enum psensor_type {
29         /* type of sensor values */
30         SENSOR_TYPE_TEMP = 0x00001,
31         SENSOR_TYPE_RPM = 0x00002,
32         SENSOR_TYPE_PERCENT = 0x00004,
33
34         /* Whether the sensor is remote */
35         SENSOR_TYPE_REMOTE = 0x00008,
36
37         /* Libraries used for retrieving sensor information */
38         SENSOR_TYPE_LMSENSOR = 0x00100,
39         SENSOR_TYPE_NVCTRL = 0x00200,
40         SENSOR_TYPE_GTOP = 0x00400,
41         SENSOR_TYPE_ATIADL = 0x00800,
42         SENSOR_TYPE_ATASMART = 0x01000,
43         SENSOR_TYPE_HDDTEMP = 0x02000,
44         SENSOR_TYPE_UDISKS2 = 0x800000,
45
46         /* Type of HW component */
47         SENSOR_TYPE_HDD = 0x04000,
48         SENSOR_TYPE_CPU = 0x08000,
49         SENSOR_TYPE_GPU = 0x10000,
50         SENSOR_TYPE_FAN = 0x20000,
51
52         SENSOR_TYPE_GRAPHICS = 0x40000,
53         SENSOR_TYPE_VIDEO = 0x80000,
54         SENSOR_TYPE_PCIE = 0x100000,
55         SENSOR_TYPE_MEMORY = 0x200000,
56         SENSOR_TYPE_AMBIENT = 0x400000,
57
58         /* Combinations */
59         SENSOR_TYPE_HDD_TEMP = (SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP),
60         SENSOR_TYPE_CPU_USAGE = (SENSOR_TYPE_CPU | SENSOR_TYPE_PERCENT)
61 };
62
63 struct psensor {
64         /* Human readable name of the sensor.  It may not be uniq. */
65         char *name;
66
67         /* Uniq id of the sensor */
68         char *id;
69
70         /* Name of the chip. */
71         char *chip;
72
73         /* Maximum length of 'values' */
74         int values_max_length;
75
76         /* Last registered measures of the sensor.  Index 0 for the
77          * oldest measure.  */
78         struct measure *measures;
79
80         /* see psensor_type */
81         unsigned int type;
82
83         double max;
84
85         double min;
86
87         /* The highest value detected during this session. */
88         double sess_highest;
89
90         /* The lowest value detected during this session. */
91         double sess_lowest;
92
93         double alarm_high_threshold;
94         double alarm_low_threshold;
95
96         /* Whether an alarm is raised for this sensor */
97         bool alarm_raised;
98
99         void (*cb_alarm_raised)(struct psensor *, void *);
100         void *cb_alarm_raised_data;
101
102 #ifdef HAVE_LIBATIADL
103         /* AMD id for the aticonfig */
104         int amd_id;
105 #endif
106
107         void *provider_data;
108         void (*provider_data_free_fct)(void *);
109 };
110
111 struct psensor *psensor_create(char *id,
112                                char *name,
113                                char *chip,
114                                unsigned int type,
115                                int values_max_length);
116
117 void psensor_values_resize(struct psensor *s, int new_size);
118
119 void psensor_free(struct psensor *sensor);
120
121 void psensor_list_free(struct psensor **sensors);
122 int psensor_list_size(struct psensor **sensors);
123
124 struct psensor *psensor_list_get_by_id(struct psensor **sensors,
125                                        const char *id);
126
127 int is_temp_type(unsigned int type);
128
129 double get_min_temp(struct psensor **sensors);
130 double get_max_temp(struct psensor **sensors);
131
132 double get_min_rpm(struct psensor **sensors);
133 double get_max_rpm(struct psensor **sensors);
134
135 /*
136  * Converts the value of a sensor to a string.
137  *
138  * parameter 'type' is SENSOR_TYPE_LMSENSOR_TEMP, SENSOR_TYPE_NVIDIA,
139  * or SENSOR_TYPE_LMSENSOR_FAN
140  */
141 char *psensor_value_to_str(unsigned int type,
142                            double value,
143                            int use_celsius);
144
145 char *psensor_measure_to_str(const struct measure *m,
146                              unsigned int type,
147                              unsigned int use_celsius);
148
149 struct psensor **psensor_list_add(struct psensor **sensors,
150                                   struct psensor *sensor);
151
152 void psensor_list_append(struct psensor ***sensors, struct psensor *sensor);
153
154 struct psensor **psensor_list_copy(struct psensor **);
155
156 void psensor_set_current_value(struct psensor *sensor, double value);
157 void psensor_set_current_measure(struct psensor *sensor, double value,
158                                  struct timeval tv);
159
160 double psensor_get_current_value(const struct psensor *);
161
162 struct measure *psensor_get_current_measure(struct psensor *sensor);
163
164 /* Returns a string representation of a psensor type. */
165 const char *psensor_type_to_str(unsigned int type);
166
167 const char *psensor_type_to_unit_str(unsigned int type, int use_celsius);
168
169 double get_max_value(struct psensor **sensors, int type);
170
171 char *psensor_current_value_to_str(const struct psensor *, unsigned int);
172
173 void psensor_log_measures(struct psensor **sensors);
174
175 #endif