Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_temp.c

raw
1 /* Copyright (C) 2015 by Yves Godin <[email protected]>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include <stdint.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27
28 #include <osmocom/core/utils.h>
29
30 #include "oc2gbts_temp.h"
31
32 static const char *temp_devs[_NUM_TEMP_SENSORS] = {
33 [OC2GBTS_TEMP_SUPPLY] = "/var/oc2g/temp/main-supply/temp",
34 [OC2GBTS_TEMP_SOC] = "/var/oc2g/temp/cpu/temp",
35 [OC2GBTS_TEMP_FPGA] = "/var/oc2g/temp/fpga/temp",
36 [OC2GBTS_TEMP_RMSDET] = "/var/oc2g/temp/rmsdet/temp",
37 [OC2GBTS_TEMP_OCXO] = "/var/oc2g/temp/ocxo/temp",
38 [OC2GBTS_TEMP_TX] = "/var/oc2g/temp/tx0/temp",
39 [OC2GBTS_TEMP_PA] = "/var/oc2g/temp/pa0/temp",
40 };
41
42 int oc2gbts_temp_get(enum oc2gbts_temp_sensor sensor, int *temp)
43 {
44 char buf[PATH_MAX];
45 char tempstr[8];
46 int fd, rc;
47
48 if (sensor < 0 || sensor >= _NUM_TEMP_SENSORS)
49 return -EINVAL;
50
51 snprintf(buf, sizeof(buf)-1, "%s", temp_devs[sensor]);
52 buf[sizeof(buf)-1] = '\0';
53
54 fd = open(buf, O_RDONLY);
55 if (fd < 0)
56 return fd;
57
58 rc = read(fd, tempstr, sizeof(tempstr));
59 tempstr[sizeof(tempstr)-1] = '\0';
60 if (rc < 0) {
61 close(fd);
62 return rc;
63 }
64 if (rc == 0) {
65 close(fd);
66 return -EIO;
67 }
68 close(fd);
69 *temp = atoi(tempstr);
70 return 0;
71 }