Fixed restoration of the panel divider position.
[psensor.git] / src / lib / bcm2835.c
1 /*
2  * Copyright (C) 2017 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 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <bool.h>
28 #include <pio.h>
29 #include <bcm2835.h>
30
31 /*
32  * Support of the BCM2835 chip which is mostly used by the
33  * Raspberry PI3 and provides the temperature of the chip.
34  */
35 static const char *PROVIDER_NAME = "BCM2835";
36
37 /*
38  * If this file exists and contains bcm2835_thermal it can be assumed
39  * that a BCM2835 chip is present.
40  */
41 static const char *SYS_THERMAL_TYPE = "/sys/class/thermal/thermal_zone0/type";
42 static const char *BCM2835_TYPE = "bcm2835_thermal";
43 /* this file contains the temperature of the chip in celcius * 1000 */
44 static char *SYS_THERMAL_TEMP = "/sys/class/thermal/thermal_zone0/temp";
45
46 static void log_provider_info(const char *str)
47 {
48         log_info("%s: %s", PROVIDER_NAME, str);
49 }
50
51 static bool is_bcm2835_present(void)
52 {
53         bool ret;
54         char *str;
55
56         ret = is_file(SYS_THERMAL_TYPE);
57
58         if (!ret) {
59                 log_debug("%s: %s does not exist.",
60                           PROVIDER_NAME,
61                           SYS_THERMAL_TYPE);
62                 return false;
63         }
64
65         str = file_get_content(SYS_THERMAL_TYPE);
66
67         if (!str || strncmp(str, BCM2835_TYPE, strlen(BCM2835_TYPE) - 1)) {
68                 log_debug("%s: type: %s.", PROVIDER_NAME, str);
69                 ret = false;
70         } else {
71                 ret = true;
72         }
73
74         if (str)
75                 free(str);
76
77         return ret;
78 }
79
80 void bcm2835_psensor_list_append(struct psensor ***sensors, int vl)
81 {
82         struct psensor *p;
83
84         log_fct_enter();
85
86         if (is_bcm2835_present()) {
87                 log_provider_info(_("The BCM2835 (probably a Raspberry PI3) "
88                                     "has been detected"));
89
90                 p = psensor_create(strdup(PROVIDER_NAME),
91                                    strdup(PROVIDER_NAME),
92                                    strdup(PROVIDER_NAME),
93                                    SENSOR_TYPE_BCM2835 | SENSOR_TYPE_TEMP,
94                                    vl);
95
96                 psensor_list_append(sensors, p);
97         } else {
98                 log_provider_info("The BCM2835 has not been detected.");
99         }
100
101         log_fct_exit();
102 }
103
104 static double bcm2835_update_temp(struct psensor *s)
105 {
106         char *str, *end;
107         long l;
108         int n;
109
110         log_fct_enter();
111
112         str = file_get_content(SYS_THERMAL_TEMP);
113
114         n = strlen(str) - 1;
115         if (str[n] == '\n')
116                 str[n] = '\0';
117
118         if (str) {
119                 l = strtol(str, &end, 10);
120                 if (*end != '\0')
121                         log_debug("%s: found invalid value: %s.",
122                                   PROVIDER_NAME,
123                                   str);
124                 else
125                         psensor_set_current_value(s, l / 1000);
126                 free(str);
127         } else {
128                 log_err(_("Failed to get content of file %s."),
129                         SYS_THERMAL_TEMP);
130         }
131
132         log_fct_exit();
133
134         return 0;
135 }
136
137 void bcm2835_psensor_list_update(struct psensor **sensors)
138 {
139         struct psensor *s;
140
141         log_fct_enter();
142
143         for (; *sensors; sensors++) {
144                 s = *sensors;
145
146                 if (s->type & SENSOR_TYPE_REMOTE)
147                         continue;
148
149                 if (s->type & SENSOR_TYPE_BCM2835) {
150                         bcm2835_update_temp(s);
151                         break; /* only one possible sensor */
152                 }
153         }
154
155         log_fct_exit();
156 }