Debian Codegraph @

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

raw symbols SCIP
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 "lc15bts_mgr.h"
45 #include "btsconfig.h"
46 #include "lc15bts_misc.h"
47 #include "lc15bts_par.h"
48 #include "lc15bts_mgr.h"
49 #include "lc15bts_temp.h"
50 #include "lc15bts_power.h"
51
52 /*********************************************************************
53 * Temperature handling
54 *********************************************************************/
55
56 static const struct {
57 const char *name;
58 int has_max;
59 enum lc15bts_temp_sensor sensor;
60 enum lc15bts_par ee_par;
61 } temp_data[] = {
62 {
63 .name = "supply_temp",
64 .has_max = 1,
65 .sensor = LC15BTS_TEMP_SUPPLY,
66 .ee_par = LC15BTS_PAR_TEMP_SUPPLY_MAX,
67 }, {
68 .name = "soc_temp",
69 .has_max = 0,
70 .sensor = LC15BTS_TEMP_SOC,
71 .ee_par = LC15BTS_PAR_TEMP_SOC_MAX,
72 }, {
73 .name = "fpga_temp",
74 .has_max = 0,
75 .sensor = LC15BTS_TEMP_FPGA,
76 .ee_par = LC15BTS_PAR_TEMP_FPGA_MAX,
77
78 }, {
79 .name = "rmsdet_temp",
80 .has_max = 1,
81 .sensor = LC15BTS_TEMP_RMSDET,
82 .ee_par = LC15BTS_PAR_TEMP_RMSDET_MAX,
83 }, {
84 .name = "ocxo_temp",
85 .has_max = 1,
86 .sensor = LC15BTS_TEMP_OCXO,
87 .ee_par = LC15BTS_PAR_TEMP_OCXO_MAX,
88 }, {
89 .name = "tx0_temp",
90 .has_max = 0,
91 .sensor = LC15BTS_TEMP_TX0,
92 .ee_par = LC15BTS_PAR_TEMP_TX0_MAX,
93 }, {
94 .name = "tx1_temp",
95 .has_max = 0,
96 .sensor = LC15BTS_TEMP_TX1,
97 .ee_par = LC15BTS_PAR_TEMP_TX1_MAX,
98 }, {
99 .name = "pa0_temp",
100 .has_max = 1,
101 .sensor = LC15BTS_TEMP_PA0,
102 .ee_par = LC15BTS_PAR_TEMP_PA0_MAX,
103 }, {
104 .name = "pa1_temp",
105 .has_max = 1,
106 .sensor = LC15BTS_TEMP_PA1,
107 .ee_par = LC15BTS_PAR_TEMP_PA1_MAX,
108 }
109 };
110
111 static const struct {
112 const char *name;
113 int has_max;
114 enum lc15bts_power_source sensor_source;
115 enum lc15bts_power_type sensor_type;
116 enum lc15bts_par ee_par;
117 } power_data[] = {
118 {
119 .name = "supply_volt",
120 .has_max = 1,
121 .sensor_source = LC15BTS_POWER_SUPPLY,
122 .sensor_type = LC15BTS_POWER_VOLTAGE,
123 .ee_par = LC15BTS_PAR_VOLT_SUPPLY_MAX,
124 }, {
125 .name = "supply_pwr",
126 .has_max = 1,
127 .sensor_source = LC15BTS_POWER_SUPPLY,
128 .sensor_type = LC15BTS_POWER_POWER,
129 .ee_par = LC15BTS_PAR_PWR_SUPPLY_MAX,
130 }, {
131 .name = "pa0_pwr",
132 .has_max = 1,
133 .sensor_source = LC15BTS_POWER_PA0,
134 .sensor_type = LC15BTS_POWER_POWER,
135 .ee_par = LC15BTS_PAR_PWR_PA0_MAX,
136 }, {
137 .name = "pa1_pwr",
138 .has_max = 1,
139 .sensor_source = LC15BTS_POWER_PA1,
140 .sensor_type = LC15BTS_POWER_POWER,
141 .ee_par = LC15BTS_PAR_PWR_PA1_MAX,
142 }
143 };
144
145 static const struct {
146 const char *name;
147 int has_max;
148 enum lc15bts_vswr_sensor sensor;
149 enum lc15bts_par ee_par;
150 } vswr_data[] = {
151 {
152 .name = "tx0_vswr",
153 .has_max = 0,
154 .sensor = LC15BTS_VSWR_TX0,
155 .ee_par = LC15BTS_PAR_VSWR_TX0_MAX,
156 }, {
157 .name = "tx1_vswr",
158 .has_max = 0,
159 .sensor = LC15BTS_VSWR_TX1,
160 .ee_par = LC15BTS_PAR_VSWR_TX1_MAX,
161 }
162 };
163
164 static const struct value_string power_unit_strs[] = {
165 { LC15BTS_POWER_POWER, "W" },
166 { LC15BTS_POWER_VOLTAGE, "V" },
167 { 0, NULL }
168 };
169
170 void lc15bts_check_temp(int no_rom_write)
171 {
172 int temp_old[ARRAY_SIZE(temp_data)];
173 int temp_cur[ARRAY_SIZE(temp_data)];
174 int i, rc;
175
176 for (i = 0; i < ARRAY_SIZE(temp_data); i++) {
177 int ret;
178 rc = lc15bts_par_get_int(tall_mgr_ctx, temp_data[i].ee_par, &ret);
179 temp_old[i] = ret * 1000;
180
181 lc15bts_temp_get(temp_data[i].sensor, &temp_cur[i]);
182 if (temp_cur[i] < 0 && temp_cur[i] > -1000) {
183 LOGP(DTEMP, LOGL_ERROR, "Error reading temperature (%d): unexpected value %d\n",
184 temp_data[i].sensor, temp_cur[i]);
185 continue;
186 }
187
188 LOGP(DTEMP, LOGL_DEBUG, "Current %s temperature: %d.%d C\n",
189 temp_data[i].name, temp_cur[i]/1000, temp_cur[i]%1000);
190
191 if (temp_cur[i] > temp_old[i]) {
192 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
193 "temperature: %d.%d C\n", temp_data[i].name,
194 temp_cur[i]/1000, temp_old[i]%1000);
195
196 if (!no_rom_write) {
197 rc = lc15bts_par_set_int(tall_mgr_ctx, temp_data[i].ee_par, temp_cur[i]/1000);
198 if (rc < 0)
199 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
200 "max temp %d (%s)\n", temp_data[i].name,
201 rc, strerror(errno));
202 }
203 }
204 }
205 }
206
207 void lc15bts_check_power(int no_rom_write)
208 {
209 int power_old[ARRAY_SIZE(power_data)];
210 int power_cur[ARRAY_SIZE(power_data)];
211 int i, rc;
212 int div_ratio;
213
214 for (i = 0; i < ARRAY_SIZE(power_data); i++) {
215 int ret;
216 rc = lc15bts_par_get_int(tall_mgr_ctx, power_data[i].ee_par, &ret);
217 switch(power_data[i].sensor_type) {
218 case LC15BTS_POWER_VOLTAGE:
219 div_ratio = 1000;
220 break;
221 case LC15BTS_POWER_POWER:
222 div_ratio = 1000000;
223 break;
224 default:
225 div_ratio = 1000;
226 }
227 power_old[i] = ret * div_ratio;
228
229 lc15bts_power_sensor_get(power_data[i].sensor_source, power_data[i].sensor_type, &power_cur[i]);
230 if (power_cur[i] < 0 && power_cur[i] > -1000) {
231 LOGP(DTEMP, LOGL_ERROR, "Error reading power (%d) (%d)\n", power_data[i].sensor_source,
232 power_data[i].sensor_type);
233 continue;
234 }
235 LOGP(DTEMP, LOGL_DEBUG, "Current %s power: %d.%d %s\n",
236 power_data[i].name, power_cur[i]/div_ratio, power_cur[i]%div_ratio,
237 get_value_string(power_unit_strs, power_data[i].sensor_type));
238
239 if (power_cur[i] > power_old[i]) {
240 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
241 "power: %d.%d %s\n", power_data[i].name,
242 power_cur[i]/div_ratio, power_cur[i]%div_ratio,
243 get_value_string(power_unit_strs, power_data[i].sensor_type));
244
245 if (!no_rom_write) {
246 rc = lc15bts_par_set_int(tall_mgr_ctx, power_data[i].ee_par, power_cur[i]/div_ratio);
247 if (rc < 0)
248 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
249 "max power %d (%s)\n", power_data[i].name,
250 rc, strerror(errno));
251 }
252 }
253 }
254 }
255
256 void lc15bts_check_vswr(int no_rom_write)
257 {
258 int vswr_old[ARRAY_SIZE(vswr_data)];
259 int vswr_cur[ARRAY_SIZE(vswr_data)];
260 int i, rc;
261
262 for (i = 0; i < ARRAY_SIZE(vswr_data); i++) {
263 int ret;
264 rc = lc15bts_par_get_int(tall_mgr_ctx, vswr_data[i].ee_par, &ret);
265 vswr_old[i] = ret * 1000;
266
267 lc15bts_vswr_get(vswr_data[i].sensor, &vswr_cur[i]);
268 if (vswr_cur[i] < 0 && vswr_cur[i] > -1000) {
269 LOGP(DTEMP, LOGL_ERROR, "Error reading vswr (%d)\n", vswr_data[i].sensor);
270 continue;
271 }
272
273 LOGP(DTEMP, LOGL_DEBUG, "Current %s vswr: %d.%d\n",
274 vswr_data[i].name, vswr_cur[i]/1000, vswr_cur[i]%1000);
275
276 if (vswr_cur[i] > vswr_old[i]) {
277 LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
278 "vswr: %d.%d C\n", vswr_data[i].name,
279 vswr_cur[i]/1000, vswr_old[i]%1000);
280
281 if (!no_rom_write) {
282 rc = lc15bts_par_set_int(tall_mgr_ctx, vswr_data[i].ee_par, vswr_cur[i]/1000);
283 if (rc < 0)
284 LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
285 "max vswr %d (%s)\n", vswr_data[i].name,
286 rc, strerror(errno));
287 }
288 }
289 }
290 }
291
292 /*********************************************************************
293 * Hours handling
294 *********************************************************************/
295 static time_t last_update;
296
297 int lc15bts_update_hours(int no_rom_write)
298 {
299 time_t now = time(NULL);
300 int rc, op_hrs;
301
302 /* first time after start of manager program */
303 if (last_update == 0) {
304 last_update = now;
305
306 rc = lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_PAR_HOURS, &op_hrs);
307 if (rc < 0) {
308 LOGP(DTEMP, LOGL_ERROR, "Unable to read "
309 "operational hours: %d (%s)\n", rc,
310 strerror(errno));
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 = lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_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 = lc15bts_par_set_int(tall_mgr_ctx, LC15BTS_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 [LC15BTS_FW_DSP0] = "/sys/kernel/debug/remoteproc/remoteproc0/recovery",
353 [LC15BTS_FW_DSP1] = "/sys/kernel/debug/remoteproc/remoteproc0/recovery",
354 };
355
356 int lc15bts_firmware_reload(enum lc15bts_firmware_type type)
357 {
358 int fd;
359 int rc;
360
361 switch (type) {
362 case LC15BTS_FW_DSP0:
363 case LC15BTS_FW_DSP1:
364 fd = open(fw_sysfs[type], O_WRONLY);
365 if (fd < 0) {
366 LOGP(DFW, LOGL_ERROR, "unable to open firmware device %s: %s\n",
367 fw_sysfs[type], strerror(errno));
368 close(fd);
369 return fd;
370 }
371 rc = write(fd, "restart", 8);
372 if (rc < 8) {
373 LOGP(DFW, LOGL_ERROR, "short write during "
374 "fw write to %s\n", fw_sysfs[type]);
375 close(fd);
376 return -EIO;
377 }
378 close(fd);
379 default:
380 return -EINVAL;
381 }
382 return 0;
383 }