Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_power.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 <stdio.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28
29 #include "oc2gbts_power.h"
30
31 static const char *power_enable_devs[_NUM_POWER_SOURCES] = {
32 [OC2GBTS_POWER_PA] = "/var/oc2g/pa-state/pa0/state",
33 };
34
35 static const char *power_sensor_devs[_NUM_POWER_SOURCES] = {
36 [OC2GBTS_POWER_SUPPLY] = "/var/oc2g/pwr-sense/main-supply/",
37 [OC2GBTS_POWER_PA] = "/var/oc2g/pwr-sense/pa0/",
38 };
39
40 static const char *power_sensor_type_str[_NUM_POWER_TYPES] = {
41 [OC2GBTS_POWER_POWER] = "power",
42 [OC2GBTS_POWER_VOLTAGE] = "voltage",
43 [OC2GBTS_POWER_CURRENT] = "current",
44 };
45
46 int oc2gbts_power_sensor_get(
47 enum oc2gbts_power_source source,
48 enum oc2gbts_power_type type,
49 int *power)
50 {
51 char buf[PATH_MAX];
52 char pwrstr[10];
53 int fd, rc;
54
55 if (source >= _NUM_POWER_SOURCES)
56 return -EINVAL;
57
58 if (type >= _NUM_POWER_TYPES)
59 return -EINVAL;
60
61 snprintf(buf, sizeof(buf)-1, "%s%s", power_sensor_devs[source], power_sensor_type_str[type]);
62 buf[sizeof(buf)-1] = '\0';
63
64 fd = open(buf, O_RDONLY);
65 if (fd < 0)
66 return fd;
67
68 rc = read(fd, pwrstr, sizeof(pwrstr));
69 pwrstr[sizeof(pwrstr)-1] = '\0';
70 if (rc < 0) {
71 close(fd);
72 return rc;
73 }
74 if (rc == 0) {
75 close(fd);
76 return -EIO;
77 }
78 close(fd);
79 *power = atoi(pwrstr);
80 return 0;
81 }
82
83
84 int oc2gbts_power_set(
85 enum oc2gbts_power_source source,
86 int en)
87 {
88 int fd;
89 int rc;
90
91 if (source != OC2GBTS_POWER_PA) {
92 return -EINVAL;
93 }
94
95 fd = open(power_enable_devs[source], O_WRONLY);
96 if (fd < 0) {
97 return fd;
98 }
99 rc = write(fd, en?"1":"0", 2);
100 close( fd );
101
102 if (rc != 2) {
103 return -1;
104 }
105
106 if (en) usleep(50*1000);
107
108 return 0;
109 }
110
111 int oc2gbts_power_get(
112 enum oc2gbts_power_source source)
113 {
114 int fd;
115 int rc;
116 int retVal = 0;
117 char enstr[10];
118
119 fd = open(power_enable_devs[source], O_RDONLY);
120 if (fd < 0) {
121 return fd;
122 }
123
124 rc = read(fd, enstr, sizeof(enstr));
125 enstr[rc-1] = '\0';
126
127 close(fd);
128
129 if (rc < 0) {
130 return rc;
131 }
132 if (rc == 0) {
133 return -EIO;
134 }
135
136 rc = strcmp(enstr, "enabled");
137 if(rc == 0) {
138 retVal = 1;
139 }
140
141 return retVal;
142 }
143
144 static const char *vswr_devs[_NUM_VSWR_SENSORS] = {
145 [OC2GBTS_VSWR] = "/var/oc2g/vswr/tx0/vswr",
146 };
147
148 int oc2gbts_vswr_get(enum oc2gbts_vswr_sensor sensor, int *vswr)
149 {
150 char buf[PATH_MAX];
151 char vswrstr[8];
152 int fd, rc;
153
154 if (sensor < 0 || sensor >= _NUM_VSWR_SENSORS)
155 return -EINVAL;
156
157 snprintf(buf, sizeof(buf)-1, "%s", vswr_devs[sensor]);
158 buf[sizeof(buf)-1] = '\0';
159
160 fd = open(buf, O_RDONLY);
161 if (fd < 0)
162 return fd;
163
164 rc = read(fd, vswrstr, sizeof(vswrstr));
165 vswrstr[sizeof(vswrstr)-1] = '\0';
166 if (rc < 0) {
167 close(fd);
168 return rc;
169 }
170 if (rc == 0) {
171 close(fd);
172 return -EIO;
173 }
174 close(fd);
175 *vswr = atoi(vswrstr);
176 return 0;
177 }