Debian Codegraph @

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

raw
1 /* Main program for NuRAN Wireless OC-2G BTS management daemon */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_mgr.c
7 * (C) 2012 by Harald Welte <[email protected]>
8 * (C) 2014 by Holger Hans Peter Freyther
9 *
10 * All Rights Reserved
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27 #include <stdint.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <getopt.h>
32 #include <limits.h>
33 #include <sys/signal.h>
34 #include <sys/stat.h>
35
36 #include <osmocom/core/talloc.h>
37 #include <osmocom/core/application.h>
38 #include <osmocom/core/timer.h>
39 #include <osmocom/core/msgb.h>
40 #include <osmocom/vty/telnet_interface.h>
41 #include <osmocom/vty/logging.h>
42 #include <osmocom/vty/ports.h>
43
44 #include "misc/oc2gbts_misc.h"
45 #include "misc/oc2gbts_mgr.h"
46 #include "misc/oc2gbts_par.h"
47 #include "misc/oc2gbts_bid.h"
48 #include "misc/oc2gbts_power.h"
49 #include "misc/oc2gbts_swd.h"
50
51 #include "oc2gbts_led.h"
52
53 static int no_rom_write = 0;
54 static int daemonize = 0;
55 void *tall_mgr_ctx;
56
57 /* every 6 hours means 365*4 = 1460 rom writes per year (max) */
58 #define SENSOR_TIMER_SECS (6 * 3600)
59
60 /* every 1 hours means 365*24 = 8760 rom writes per year (max) */
61 #define HOURS_TIMER_SECS (1 * 3600)
62
63 /* the initial state */
64 static struct oc2gbts_mgr_instance manager = {
65 .config_file = "oc2gbts-mgr.cfg",
66 .temp = {
67 .supply_temp_limit = {
68 .thresh_warn_max = 80,
69 .thresh_crit_max = 85,
70 .thresh_warn_min = -40,
71 },
72 .soc_temp_limit = {
73 .thresh_warn_max = 95,
74 .thresh_crit_max = 100,
75 .thresh_warn_min = -40,
76 },
77 .fpga_temp_limit = {
78 .thresh_warn_max = 95,
79 .thresh_crit_max = 100,
80 .thresh_warn_min = -40,
81 },
82 .rmsdet_temp_limit = {
83 .thresh_warn_max = 80,
84 .thresh_crit_max = 85,
85 .thresh_warn_min = -40,
86 },
87 .ocxo_temp_limit = {
88 .thresh_warn_max = 80,
89 .thresh_crit_max = 85,
90 .thresh_warn_min = -40,
91 },
92 .tx_temp_limit = {
93 .thresh_warn_max = 80,
94 .thresh_crit_max = 85,
95 .thresh_warn_min = -20,
96 },
97 .pa_temp_limit = {
98 .thresh_warn_max = 80,
99 .thresh_crit_max = 85,
100 .thresh_warn_min = -40,
101 }
102 },
103 .volt = {
104 .supply_volt_limit = {
105 .thresh_warn_max = 30000,
106 .thresh_crit_max = 30500,
107 .thresh_warn_min = 19000,
108 .thresh_crit_min = 17500,
109 }
110 },
111 .pwr = {
112 .supply_pwr_limit = {
113 .thresh_warn_max = 30,
114 .thresh_crit_max = 40,
115 },
116 .pa_pwr_limit = {
117 .thresh_warn_max = 20,
118 .thresh_crit_max = 30,
119 }
120 },
121 .vswr = {
122 .vswr_limit = {
123 .thresh_warn_max = 3000,
124 .thresh_crit_max = 5000,
125 }
126 },
127 .gps = {
128 .gps_fix_limit = {
129 .thresh_warn_max = 7,
130 }
131 },
132 .state = {
133 .action_norm = SENSOR_ACT_NORM_PA_ON,
134 .action_warn = 0,
135 .action_crit = 0,
136 .action_comb = 0,
137 .state = STATE_NORMAL,
138 }
139 };
140
141 static struct osmo_timer_list sensor_timer;
142 static void check_sensor_timer_cb(void *unused)
143 {
144 oc2gbts_check_temp(no_rom_write);
145 oc2gbts_check_power(no_rom_write);
146 oc2gbts_check_vswr(no_rom_write);
147 osmo_timer_schedule(&sensor_timer, SENSOR_TIMER_SECS, 0);
148 /* TODO checks if oc2gbts_check_temp/oc2gbts_check_power/oc2gbts_check_vswr went ok */
149 oc2gbts_swd_event(&manager, SWD_CHECK_SENSOR);
150 }
151
152 static struct osmo_timer_list hours_timer;
153 static void hours_timer_cb(void *unused)
154 {
155 oc2gbts_update_hours(no_rom_write);
156
157 osmo_timer_schedule(&hours_timer, HOURS_TIMER_SECS, 0);
158 /* TODO: validates if oc2gbts_update_hours went correctly */
159 oc2gbts_swd_event(&manager, SWD_UPDATE_HOURS);
160 }
161
162 static void print_help(void)
163 {
164 printf("oc2gbts-mgr [-nsD] [-d cat]\n");
165 printf(" -n Do not write to ROM\n");
166 printf(" -s Disable color\n");
167 printf(" -d CAT enable debugging\n");
168 printf(" -D daemonize\n");
169 printf(" -c Specify the filename of the config file\n");
170 }
171
172 static int parse_options(int argc, char **argv)
173 {
174 int opt;
175
176 while ((opt = getopt(argc, argv, "nhsd:c:")) != -1) {
177 switch (opt) {
178 case 'n':
179 no_rom_write = 1;
180 break;
181 case 'h':
182 print_help();
183 return -1;
184 case 's':
185 log_set_use_color(osmo_stderr_target, 0);
186 break;
187 case 'd':
188 log_parse_category_mask(osmo_stderr_target, optarg);
189 break;
190 case 'D':
191 daemonize = 1;
192 break;
193 case 'c':
194 manager.config_file = optarg;
195 break;
196 default:
197 return -1;
198 }
199 }
200
201 return 0;
202 }
203
204 static void signal_handler(int signum)
205 {
206 fprintf(stderr, "signal %u received\n", signum);
207
208 switch (signum) {
209 case SIGINT:
210 oc2gbts_check_temp(no_rom_write);
211 oc2gbts_check_power(no_rom_write);
212 oc2gbts_check_vswr(no_rom_write);
213 oc2gbts_update_hours(no_rom_write);
214 exit(0);
215 break;
216 case SIGABRT:
217 /* in case of abort, we want to obtain a talloc report and
218 * then run default SIGABRT handler, who will generate coredump
219 * and abort the process. abort() should do this for us after we
220 * return, but program wouldn't exit if an external SIGABRT is
221 * received.
222 */
223 talloc_report_full(tall_mgr_ctx, stderr);
224 signal(SIGABRT, SIG_DFL);
225 raise(SIGABRT);
226 break;
227 case SIGUSR1:
228 case SIGUSR2:
229 talloc_report_full(tall_mgr_ctx, stderr);
230 break;
231 default:
232 break;
233 }
234 }
235
236 static struct log_info_cat mgr_log_info_cat[] = {
237 [DTEMP] = {
238 .name = "DTEMP",
239 .description = "Temperature monitoring",
240 .color = "\033[1;35m",
241 .enabled = 1, .loglevel = LOGL_NOTICE,
242 },
243 [DFW] = {
244 .name = "DFW",
245 .description = "Firmware management",
246 .color = "\033[1;36m",
247 .enabled = 1, .loglevel = LOGL_NOTICE,
248 },
249 [DFIND] = {
250 .name = "DFIND",
251 .description = "ipaccess-find handling",
252 .color = "\033[1;37m",
253 .enabled = 1, .loglevel = LOGL_NOTICE,
254 },
255 [DCALIB] = {
256 .name = "DCALIB",
257 .description = "Calibration handling",
258 .color = "\033[1;37m",
259 .enabled = 1, .loglevel = LOGL_NOTICE,
260 },
261 [DSWD] = {
262 .name = "DSWD",
263 .description = "Software Watchdog",
264 .color = "\033[1;37m",
265 .enabled = 1, .loglevel = LOGL_NOTICE,
266 },
267 };
268
269 static const struct log_info mgr_log_info = {
270 .cat = mgr_log_info_cat,
271 .num_cat = ARRAY_SIZE(mgr_log_info_cat),
272 };
273
274 static int mgr_log_init(void *ctx)
275 {
276 osmo_init_logging2(ctx, &mgr_log_info);
277 return 0;
278 }
279
280 int main(int argc, char **argv)
281 {
282 void *tall_msgb_ctx;
283 int rc;
284
285 tall_mgr_ctx = talloc_named_const(NULL, 1, "bts manager");
286 tall_msgb_ctx = talloc_named_const(tall_mgr_ctx, 1, "msgb");
287 msgb_talloc_ctx_init(tall_msgb_ctx, 0);
288
289 mgr_log_init(tall_mgr_ctx);
290
291 osmo_init_ignore_signals();
292 signal(SIGINT, &signal_handler);
293 signal(SIGABRT, &signal_handler);
294 signal(SIGUSR1, &signal_handler);
295 signal(SIGUSR2, &signal_handler);
296
297 rc = parse_options(argc, argv);
298 if (rc < 0)
299 exit(2);
300
301 oc2gbts_mgr_vty_init();
302 logging_vty_add_cmds();
303 rc = oc2gbts_mgr_parse_config(&manager);
304 if (rc < 0) {
305 LOGP(DFIND, LOGL_FATAL, "Cannot parse config file\n");
306 exit(1);
307 }
308
309 rc = telnet_init_default(tall_mgr_ctx, NULL, OSMO_VTY_PORT_BTSMGR);
310 if (rc < 0) {
311 fprintf(stderr, "Error initializing telnet\n");
312 exit(1);
313 }
314
315 INIT_LLIST_HEAD(&manager.oc2gbts_leds.list);
316 INIT_LLIST_HEAD(&manager.alarms.list);
317
318 /* Initialize the service watchdog notification for SWD_LAST event(s) */
319 if (oc2gbts_swd_init(&manager, (int)(SWD_LAST)) != 0)
320 exit(3);
321
322 /* start temperature check timer */
323 sensor_timer.cb = check_sensor_timer_cb;
324 check_sensor_timer_cb(NULL);
325
326 /* start operational hours timer */
327 hours_timer.cb = hours_timer_cb;
328 hours_timer_cb(NULL);
329
330 if (oc2gbts_option_get(OC2GBTS_OPTION_PA)) {
331 /* Enable the PAs */
332 rc = oc2gbts_power_set(OC2GBTS_POWER_PA, 1);
333 if (rc < 0) {
334 exit(3);
335 }
336 }
337 /* handle broadcast messages for ipaccess-find */
338 if (oc2gbts_mgr_nl_init() != 0)
339 exit(3);
340
341 /* Initialize the sensor control */
342 oc2gbts_mgr_sensor_init(&manager);
343
344 if (oc2gbts_mgr_calib_init(&manager) != 0)
345 exit(3);
346
347 if (oc2gbts_mgr_control_init(&manager))
348 exit(3);
349
350 if (daemonize) {
351 rc = osmo_daemonize();
352 if (rc < 0) {
353 perror("Error during daemonize");
354 exit(1);
355 }
356 }
357
358 while (1) {
359 log_reset_context();
360 osmo_select_main(0);
361 oc2gbts_swd_event(&manager, SWD_MAINLOOP);
362 }
363 }