Debian Codegraph @

src/osmo-bts-lc15/misc/lc15bts_power.c

raw symbols SCIP
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 "lc15bts_power.h"
30
31 #define LC15BTS_PA_VOLTAGE 24000000
32
33 #define PA_SUPPLY_MIN_SYSFS "/var/lc15/pa-supply/min_microvolts"
34 #define PA_SUPPLY_MAX_SYSFS "/var/lc15/pa-supply/max_microvolts"
35
36 static const char *power_enable_devs[_NUM_POWER_SOURCES] = {
37 [LC15BTS_POWER_PA0] = "/var/lc15/pa-state/pa0/state",
38 [LC15BTS_POWER_PA1] = "/var/lc15/pa-state/pa1/state",
39 };
40
41 static const char *power_sensor_devs[_NUM_POWER_SOURCES] = {
42 [LC15BTS_POWER_SUPPLY] = "/var/lc15/pwr-sense/main-supply/",
43 [LC15BTS_POWER_PA0] = "/var/lc15/pwr-sense/pa0/",
44 [LC15BTS_POWER_PA1] = "/var/lc15/pwr-sense/pa1/",
45 };
46
47 static const char *power_sensor_type_str[_NUM_POWER_TYPES] = {
48 [LC15BTS_POWER_POWER] = "power",
49 [LC15BTS_POWER_VOLTAGE] = "voltage",
50 [LC15BTS_POWER_CURRENT] = "current",
51 };
52
53 int lc15bts_power_sensor_get(
54 enum lc15bts_power_source source,
55 enum lc15bts_power_type type,
56 int *power)
57 {
58 char buf[PATH_MAX];
59 char pwrstr[10];
60 int fd, rc;
61
62 if (source >= _NUM_POWER_SOURCES)
63 return -EINVAL;
64
65 if (type >= _NUM_POWER_TYPES)
66 return -EINVAL;
67
68 snprintf(buf, sizeof(buf)-1, "%s%s", power_sensor_devs[source], power_sensor_type_str[type]);
69 buf[sizeof(buf)-1] = '\0';
70
71 fd = open(buf, O_RDONLY);
72 if (fd < 0)
73 return fd;
74
75 rc = read(fd, pwrstr, sizeof(pwrstr));
76 pwrstr[sizeof(pwrstr)-1] = '\0';
77 if (rc < 0) {
78 close(fd);
79 return rc;
80 }
81 if (rc == 0) {
82 close(fd);
83 return -EIO;
84 }
85 close(fd);
86 *power = atoi(pwrstr);
87 return 0;
88 }
89
90
91 int lc15bts_power_set(
92 enum lc15bts_power_source source,
93 int en)
94 {
95 int fd;
96 int rc;
97
98 if ((source != LC15BTS_POWER_PA0)
99 && (source != LC15BTS_POWER_PA1) ) {
100 return -EINVAL;
101 }
102
103 fd = open(PA_SUPPLY_MAX_SYSFS, O_WRONLY);
104 if (fd < 0) {
105 return fd;
106 }
107 rc = write(fd, "32000000", 9);
108 close( fd );
109
110 if (rc != 9) {
111 return -1;
112 }
113
114 fd = open(PA_SUPPLY_MIN_SYSFS, O_WRONLY);
115 if (fd < 0) {
116 return fd;
117 }
118
119 /* TODO NTQ: Make the voltage configurable */
120 rc = write(fd, "24000000", 9);
121 close( fd );
122
123 if (rc != 9) {
124 return -1;
125 }
126
127 fd = open(power_enable_devs[source], O_WRONLY);
128 if (fd < 0) {
129 return fd;
130 }
131 rc = write(fd, en?"1":"0", 2);
132 close( fd );
133
134 if (rc != 2) {
135 return -1;
136 }
137
138 if (en) usleep(50*1000);
139
140 return 0;
141 }
142
143 int lc15bts_power_get(
144 enum lc15bts_power_source source)
145 {
146 int fd;
147 int rc;
148 int retVal = 0;
149 char enstr[10];
150
151 fd = open(power_enable_devs[source], O_RDONLY);
152 if (fd < 0) {
153 return fd;
154 }
155
156 rc = read(fd, enstr, sizeof(enstr));
157 enstr[rc-1] = '\0';
158
159 close(fd);
160
161 if (rc < 0) {
162 return rc;
163 }
164 if (rc == 0) {
165 return -EIO;
166 }
167
168 rc = strcmp(enstr, "enabled");
169 if(rc == 0) {
170 retVal = 1;
171 }
172
173 return retVal;
174 }
175
176 static const char *vswr_devs[_NUM_VSWR_SENSORS] = {
177 [LC15BTS_VSWR_TX0] = "/var/lc15/vswr/tx0/vswr",
178 [LC15BTS_VSWR_TX1] = "/var/lc15/vswr/tx1/vswr",
179 };
180
181 int lc15bts_vswr_get(enum lc15bts_vswr_sensor sensor, int *vswr)
182 {
183 char buf[PATH_MAX];
184 char vswrstr[8];
185 int fd, rc;
186
187 if (sensor < 0 || sensor >= _NUM_VSWR_SENSORS)
188 return -EINVAL;
189
190 snprintf(buf, sizeof(buf)-1, "%s", vswr_devs[sensor]);
191 buf[sizeof(buf)-1] = '\0';
192
193 fd = open(buf, O_RDONLY);
194 if (fd < 0)
195 return fd;
196
197 rc = read(fd, vswrstr, sizeof(vswrstr));
198 vswrstr[sizeof(vswrstr)-1] = '\0';
199 if (rc < 0) {
200 close(fd);
201 return rc;
202 }
203 if (rc == 0) {
204 close(fd);
205 return -EIO;
206 }
207 close(fd);
208 *vswr = atoi(vswrstr);
209 return 0;
210 }