Debian Codegraph @

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

raw
1 /* Copyright (C) 2015 by Yves Godin <[email protected]>
2 *
3 * Based on sysmoBTS:
4 * sysmobts_misc.c
5 * (C) 2012 by Harald Welte <[email protected]>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <time.h>
33 #include <sys/signal.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <osmocom/core/talloc.h>
38 #include <osmocom/core/utils.h>
39 #include <osmocom/core/msgb.h>
40 #include <osmocom/core/application.h>
41 #include <osmocom/vty/telnet_interface.h>
42 #include <osmocom/vty/logging.h>
43
44 #include "oc2gbts_mgr.h"
45 #include "btsconfig.h"
46 #include "oc2gbts_misc.h"
47 #include "oc2gbts_par.h"
48 #include "oc2gbts_temp.h"
49 #include "oc2gbts_power.h"
50 #include "oc2gbts_bid.h"
51
52 /*********************************************************************
53 * Temperature handling
54 *********************************************************************/
55
56 static const struct {
57 const char *name;
58 int has_max;
59 enum oc2gbts_temp_sensor sensor;
60 enum oc2gbts_par ee_par;
61 } temp_data[] = {
62 {
63 .name = "supply_temp",
64 .has_max = 1,
65 .sensor = OC2GBTS_TEMP_SUPPLY,
66 .ee_par = OC2GBTS_PAR_TEMP_SUPPLY_MAX,
67 }, {
68 .name = "soc_temp",
69 .has_max = 0,
70 .sensor = OC2GBTS_TEMP_SOC,
71 .ee_par = OC2GBTS_PAR_TEMP_SOC_MAX,
72 }, {
73 .name = "fpga_temp",
74 .has_max = 0,
75 .sensor = OC2GBTS_TEMP_FPGA,
76 .ee_par = OC2GBTS_PAR_TEMP_FPGA_MAX,
77
78 }, {
79 .name = "rmsdet_temp",
80 .has_max = 1,
81 .sensor = OC2GBTS_TEMP_RMSDET,
82 .ee_par = OC2GBTS_PAR_TEMP_RMSDET_MAX,
83 }, {
84 .name = "ocxo_temp",
85 .has_max = 1,
86 .sensor = OC2GBTS_TEMP_OCXO,
87 .ee_par = OC2GBTS_PAR_TEMP_OCXO_MAX,
88 }, {
89 .name = "tx_temp",
90 .has_max = 0,
91 .sensor = OC2GBTS_TEMP_TX,
92 .ee_par = OC2GBTS_PAR_TEMP_TX_MAX,
93 }, {
94 .name = "pa_temp",
95 .has_max = 1,
96 .sensor = OC2GBTS_TEMP_PA,
97 .ee_par = OC2GBTS_PAR_TEMP_PA_MAX,
98 }
99 };
100
101 static const struct {
102 const char *name;
103 int has_max;
104 enum oc2gbts_power_source sensor_source;
105 enum oc2gbts_power_type sensor_type;
106 enum oc2gbts_par ee_par;
107 } power_data[] = {
108 {
109 .name = "supply_volt",
110 .has_max = 1,
111 .sensor_source = OC2GBTS_POWER_SUPPLY,
112 .sensor_type = OC2GBTS_POWER_VOLTAGE,
113 .ee_par = OC2GBTS_PAR_VOLT_SUPPLY_MAX,
114 }, {
115 .name = "supply_pwr",
116 .has_max = 1,
117 .sensor_source = OC2GBTS_POWER_SUPPLY,
118 .sensor_type = OC2GBTS_POWER_POWER,
119 .ee_par = OC2GBTS_PAR_PWR_SUPPLY_MAX,
120 }, {
121 .name = "pa_pwr",
122 .has_max = 1,
123 .sensor_source = OC2GBTS_POWER_PA,
124 .sensor_type = OC2GBTS_POWER_POWER,
125 .ee_par = OC2GBTS_PAR_PWR_PA_MAX,
126 }
127 };
128
129 static const struct {
130 const char *name;
131 int has_max;
132 enum oc2gbts_vswr_sensor sensor;
133 enum oc2gbts_par ee_par;
134 } vswr_data[] = {
135 {
136 .name = "vswr",
137 .has_max = 0,
138 .sensor = OC2GBTS_VSWR,
139 .ee_par = OC2GBTS_PAR_VSWR_MAX,
140 }
141 };
142
143 static const struct value_string power_unit_strs[] = {
144 { OC2GBTS_POWER_POWER, "W" },
145 { OC2GBTS_POWER_VOLTAGE, "V" },
146 { 0, NULL }
147 };
148
149 void oc2gbts_check_temp(int no_rom_write)
150 {
151 int temp_old[ARRAY_SIZE(temp_data)];
152 int temp_cur[ARRAY_SIZE(temp_data)];
153 int i, rc;
154
155 for (i = 0; i < ARRAY_SIZE(temp_data); i++) {
156 int ret = -99;
157
158 if (temp_data[i].sensor == OC2GBTS_TEMP_PA &&
159 !oc2gbts_option_get(OC2GBTS_OPTION_PA_TEMP))
160 continue;
161
162 rc = oc2gbts_par_get_int(temp_data[i].ee_par, &ret);
163 temp_old[i] = ret * 1000;
164
165 oc2gbts_temp_get(temp_data[i].sensor, &temp_cur[i]);
166 if (temp_cur[i] < 0 && temp_cur[i] > -1000) {
167 LOGP(DTEMP, LOGL_ERROR, "Error reading temperature (%d)\n", temp_data[i].sensor);
168 continue;
169 }
170
171 LOGP(DTEMP, LOGL_DEBUG, "Current %s temperature: %d.%d C\n",
172 temp_data[i].name, temp_cur[i]/1000, temp_cur[i]%1000);
173
174 if (temp_cur[i] > temp_old[i]) {
175 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
176 "temperature: %d.%d C\n", temp_data[i].name,
177 temp_cur[i]/1000, temp_old[i]%1000);
178
179 if (!no_rom_write) {
180 rc = oc2gbts_par_set_int(temp_data[i].ee_par,
181 temp_cur[i]/1000);
182 if (rc < 0)
183 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
184 "max temp %d (%s)\n", temp_data[i].name,
185 rc, strerror(errno));
186 }
187 }
188 }
189 }
190
191 void oc2gbts_check_power(int no_rom_write)
192 {
193 int power_old[ARRAY_SIZE(power_data)];
194 int power_cur[ARRAY_SIZE(power_data)];
195 int i, rc;
196 int div_ratio;
197
198 for (i = 0; i < ARRAY_SIZE(power_data); i++) {
199 int ret = 0;
200
201 if (power_data[i].sensor_source == OC2GBTS_POWER_PA &&
202 !oc2gbts_option_get(OC2GBTS_OPTION_PA))
203 continue;
204
205 rc = oc2gbts_par_get_int(power_data[i].ee_par, &ret);
206 switch(power_data[i].sensor_type) {
207 case OC2GBTS_POWER_VOLTAGE:
208 div_ratio = 1000;
209 break;
210 case OC2GBTS_POWER_POWER:
211 div_ratio = 1000000;
212 break;
213 default:
214 div_ratio = 1000;
215 }
216 power_old[i] = ret * div_ratio;
217
218 oc2gbts_power_sensor_get(power_data[i].sensor_source, power_data[i].sensor_type, &power_cur[i]);
219 if (power_cur[i] < 0 && power_cur[i] > -1000) {
220 LOGP(DTEMP, LOGL_ERROR, "Error reading power (%d) (%d)\n", power_data[i].sensor_source, power_data[i].sensor_type);
221 continue;
222 }
223 LOGP(DTEMP, LOGL_DEBUG, "Current %s power: %d.%d %s\n",
224 power_data[i].name, power_cur[i]/div_ratio, power_cur[i]%div_ratio,
225 get_value_string(power_unit_strs, power_data[i].sensor_type));
226
227 if (power_cur[i] > power_old[i]) {
228 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
229 "power: %d.%d %s\n", power_data[i].name,
230 power_cur[i]/div_ratio, power_cur[i]%div_ratio,
231 get_value_string(power_unit_strs, power_data[i].sensor_type));
232
233 if (!no_rom_write) {
234 rc = oc2gbts_par_set_int(power_data[i].ee_par,
235 power_cur[i]/div_ratio);
236 if (rc < 0)
237 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
238 "max power %d (%s)\n", power_data[i].name,
239 rc, strerror(errno));
240 }
241 }
242 }
243 }
244
245 void oc2gbts_check_vswr(int no_rom_write)
246 {
247 int vswr_old[ARRAY_SIZE(vswr_data)];
248 int vswr_cur[ARRAY_SIZE(vswr_data)];
249 int i, rc;
250
251 for (i = 0; i < ARRAY_SIZE(vswr_data); i++) {
252 int ret = 0;
253
254 if (vswr_data[i].sensor == OC2GBTS_VSWR &&
255 (!oc2gbts_option_get(OC2GBTS_OPTION_RMS_FWD) ||
256 !oc2gbts_option_get(OC2GBTS_OPTION_RMS_REFL)))
257 continue;
258
259 rc = oc2gbts_par_get_int(vswr_data[i].ee_par, &ret);
260 vswr_old[i] = ret * 1000;
261
262 oc2gbts_vswr_get(vswr_data[i].sensor, &vswr_cur[i]);
263 if (vswr_cur[i] < 0 && vswr_cur[i] > -1000) {
264 LOGP(DTEMP, LOGL_ERROR, "Error reading vswr (%d)\n", vswr_data[i].sensor);
265 continue;
266 }
267
268 LOGP(DTEMP, LOGL_DEBUG, "Current %s vswr: %d.%d\n",
269 vswr_data[i].name, vswr_cur[i]/1000, vswr_cur[i]%1000);
270
271 if (vswr_cur[i] > vswr_old[i]) {
272 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
273 "vswr: %d.%d C\n", vswr_data[i].name,
274 vswr_cur[i]/1000, vswr_old[i]%1000);
275
276 if (!no_rom_write) {
277 rc = oc2gbts_par_set_int(vswr_data[i].ee_par,
278 vswr_cur[i]/1000);
279 if (rc < 0)
280 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
281 "max vswr %d (%s)\n", vswr_data[i].name,
282 rc, strerror(errno));
283 }
284 }
285 }
286 }
287
288 /*********************************************************************
289 * Hours handling
290 *********************************************************************/
291 static time_t last_update;
292
293 int oc2gbts_update_hours(int no_rom_write)
294 {
295 time_t now = time(NULL);
296 int rc, op_hrs = 0;
297
298 /* first time after start of manager program */
299 if (last_update == 0) {
300 last_update = now;
301
302 rc = oc2gbts_par_get_int(OC2GBTS_PAR_HOURS, &op_hrs);
303 if (rc < 0) {
304 LOGP(DTEMP, LOGL_ERROR, "Unable to read "
305 "operational hours: %d (%s)\n", rc,
306 strerror(errno));
307 /* create a new file anyway */
308 if (!no_rom_write)
309 rc = oc2gbts_par_set_int(OC2GBTS_PAR_HOURS, op_hrs);
310
311 return rc;
312 }
313
314 LOGP(DTEMP, LOGL_INFO, "Total hours of Operation: %u\n",
315 op_hrs);
316
317 return 0;
318 }
319
320 if (now >= last_update + 3600) {
321 rc = oc2gbts_par_get_int(OC2GBTS_PAR_HOURS, &op_hrs);
322 if (rc < 0) {
323 LOGP(DTEMP, LOGL_ERROR, "Unable to read "
324 "operational hours: %d (%s)\n", rc,
325 strerror(errno));
326 return rc;
327 }
328
329 /* number of hours to increase */
330 op_hrs += (now-last_update)/3600;
331
332 LOGP(DTEMP, LOGL_INFO, "Total hours of Operation: %u\n",
333 op_hrs);
334
335 if (!no_rom_write) {
336 rc = oc2gbts_par_set_int(OC2GBTS_PAR_HOURS, op_hrs);
337 if (rc < 0)
338 return rc;
339 }
340
341 last_update = now;
342 }
343
344 return 0;
345 }
346
347 /*********************************************************************
348 * Firmware reloading
349 *********************************************************************/
350
351 static const char *fw_sysfs[_NUM_FW] = {
352 [OC2GBTS_FW_DSP] = "/sys/kernel/debug/remoteproc/remoteproc0/recovery",
353 };
354
355 int oc2gbts_firmware_reload(enum oc2gbts_firmware_type type)
356 {
357 int fd;
358 int rc;
359
360 switch (type) {
361 case OC2GBTS_FW_DSP:
362 fd = open(fw_sysfs[type], O_WRONLY);
363 if (fd < 0) {
364 LOGP(DFW, LOGL_ERROR, "unable to open firmware device %s: %s\n",
365 fw_sysfs[type], strerror(errno));
366 close(fd);
367 return fd;
368 }
369 rc = write(fd, "restart", 8);
370 if (rc < 8) {
371 LOGP(DFW, LOGL_ERROR, "short write during "
372 "fw write to %s\n", fw_sysfs[type]);
373 close(fd);
374 return -EIO;
375 }
376 close(fd);
377 default:
378 return -EINVAL;
379 }
380 return 0;
381 }