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