aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-sysmo/misc/sysmobts_mgr_vty.c
blob: 9c7e7d3041263e54dfbc050f65ba2ad703ffeec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* (C) 2014 by sysmocom - s.f.m.c. GmbH
 *
 * All Rights Reserved
 *
 * Author: Alvaro Neira Ayuso <anayuso@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <osmocom/vty/vty.h>
#include <osmocom/vty/command.h>
#include <osmocom/vty/misc.h>

#include <osmo-bts/logging.h>

#include "sysmobts_misc.h"
#include "sysmobts_mgr.h"
#include "btsconfig.h"

static struct sysmobts_mgr_instance *s_mgr;

static const char copyright[] =
	"(C) 2012 by Harald Welte <laforge@gnumonks.org>\r\n"
	"(C) 2014 by Holger Hans Peter Freyther\r\n"
	"License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
	"This is free software: you are free to change and redistribute it.\r\n"
	"There is NO WARRANTY, to the extent permitted by law.\r\n";

static enum node_type go_to_parent(struct vty *vty)
{
	switch (vty->node) {
	case MGR_NODE:
		vty->node = CONFIG_NODE;
		break;
	case ACT_NORM_NODE:
	case ACT_WARN_NODE:
	case ACT_CRIT_NODE:
	case LIMIT_RF_NODE:
	case LIMIT_DIGITAL_NODE:
	case LIMIT_BOARD_NODE:
	case LIMIT_PA_NODE:
		vty->node = MGR_NODE;
		break;
	default:
		vty->node = CONFIG_NODE;
	}
	return vty->node;
}

static int is_config_node(struct vty *vty, int node)
{
	switch (node) {
	case MGR_NODE:
	case ACT_NORM_NODE:
	case ACT_WARN_NODE:
	case ACT_CRIT_NODE:
	case LIMIT_RF_NODE:
	case LIMIT_DIGITAL_NODE:
	case LIMIT_BOARD_NODE:
	case LIMIT_PA_NODE:
		return 1;
	default:
		return 0;
	}
}

static struct vty_app_info vty_info = {
	.name           = "sysmobts-mgr",
	.version        = PACKAGE_VERSION,
	.go_parent_cb   = go_to_parent,
	.is_config_node = is_config_node,
	.copyright	= copyright,
};


#define MGR_STR			"Configure sysmobts-mgr\n"

static struct cmd_node mgr_node = {
	MGR_NODE,
	"%s(sysmobts-mgr)# ",
	1,
};

static struct cmd_node act_norm_node = {
	ACT_NORM_NODE,
	"%s(action-normal)# ",
	1,
};

static struct cmd_node act_warn_node = {
	ACT_WARN_NODE,
	"%s(action-warn)# ",
	1,
};

static struct cmd_node act_crit_node = {
	ACT_CRIT_NODE,
	"%s(action-critical)# ",
	1,
};

static struct cmd_node limit_rf_node = {
	LIMIT_RF_NODE,
	"%s(limit-rf)# ",
	1,
};

static struct cmd_node limit_digital_node = {
	LIMIT_DIGITAL_NODE,
	"%s(limit-digital)# ",
	1,
};

static struct cmd_node limit_board_node = {
	LIMIT_BOARD_NODE,
	"%s(limit-board)# ",
	1,
};

static struct cmd_node limit_pa_node = {
	LIMIT_PA_NODE,
	"%s(limit-pa)# ",
	1,
};

DEFUN(cfg_mgr, cfg_mgr_cmd,
	"sysmobts-mgr",
	MGR_STR)
{
	vty->node = MGR_NODE;
	return CMD_SUCCESS;
}

static void write_temp_limit(struct vty *vty, const char *name,
				struct sysmobts_temp_limit *limit)
{
	vty_out(vty, " %s%s", name, VTY_NEWLINE);
	vty_out(vty, "   threshold warning %d%s",
		limit->thresh_warn, VTY_NEWLINE);
	vty_out(vty, "   threshold critical %d%s",
		limit->thresh_crit, VTY_NEWLINE);
}

static void write_norm_action(struct vty *vty, const char *name, int actions)
{
	vty_out(vty, " %s%s", name, VTY_NEWLINE);
	vty_out(vty, "  %spa-on%s",
		(actions & TEMP_ACT_NORM_PA_ON) ? "" : "no ", VTY_NEWLINE);
	vty_out(vty, "  %sbts-service-on%s",
		(actions & TEMP_ACT_NORM_BTS_SRV_ON) ? "" : "no ", VTY_NEWLINE);
}

static void write_action(struct vty *vty, const char *name, int actions)
{
	vty_out(vty, " %s%s", name, VTY_NEWLINE);
#if 0
	vty_out(vty, "  %spower-control%s",
		(actions & TEMP_ACT_PWR_CONTRL) ? "" : "no ", VTY_NEWLINE);

	/* only on the sysmobts 2050 */
	vty_out(vty, "  %smaster-off%s",
		(actions & TEMP_ACT_MASTER_OFF) ? "" : "no ", VTY_NEWLINE);
	vty_out(vty, "  %sslave-off%s",
		(actions & TEMP_ACT_MASTER_OFF) ? "" : "no ", VTY_NEWLINE);
#endif
	vty_out(vty, "  %spa-off%s",
		(actions & TEMP_ACT_PA_OFF) ? "" : "no ", VTY_NEWLINE);
	vty_out(vty, "  %sbts-service-off%s",
		(actions & TEMP_ACT_BTS_SRV_OFF) ? "" : "no ", VTY_NEWLINE);
}

static int config_write_mgr(struct vty *vty)
{
	vty_out(vty, "sysmobts-mgr%s", VTY_NEWLINE);

	write_temp_limit(vty, "limits rf", &s_mgr->rf_limit);
	write_temp_limit(vty, "limits digital", &s_mgr->digital_limit);
	write_temp_limit(vty, "limits board", &s_mgr->board_limit);
	write_temp_limit(vty, "limits pa", &s_mgr->pa_limit);

	write_norm_action(vty, "actions normal", s_mgr->action_norm);
	write_action(vty, "actions warn", s_mgr->action_warn);
	write_action(vty, "actions critical", s_mgr->action_crit);

	return CMD_SUCCESS;
}

static int config_write_dummy(struct vty *vty)
{
	return CMD_SUCCESS;
}

#define CFG_LIMIT(name, expl, switch_to, variable)			\
DEFUN(cfg_limit_##name, cfg_limit_##name##_cmd,				\
	"limits " #name,						\
	"Configure Limits\n" expl)					\
{									\
	vty->node = switch_to;						\
	vty->index = &s_mgr->variable;					\
	return CMD_SUCCESS;						\
}

CFG_LIMIT(rf, "RF\n", LIMIT_RF_NODE, rf_limit)
CFG_LIMIT(digital, "Digital\n", LIMIT_DIGITAL_NODE, digital_limit)
CFG_LIMIT(board, "Board\n", LIMIT_BOARD_NODE, board_limit)
CFG_LIMIT(pa, "Power Amplifier\n", LIMIT_PA_NODE, pa_limit)
#undef CFG_LIMIT

DEFUN(cfg_limit_warning, cfg_thresh_warning_cmd,
	"threshold warning <0-200>",
	"Threshold to reach\n" "Warning level\n" "Range\n")
{
	struct sysmobts_temp_limit *limit = vty->index;
	limit->thresh_warn = atoi(argv[0]);
	return CMD_SUCCESS;
}

DEFUN(cfg_limit_crit, cfg_thresh_crit_cmd,
	"threshold critical <0-200>",
	"Threshold to reach\n" "Severe level\n" "Range\n")
{
	struct sysmobts_temp_limit *limit = vty->index;
	limit->thresh_crit = atoi(argv[0]);
	return CMD_SUCCESS;
}

#define CFG_ACTION(name, expl, switch_to, variable)			\
DEFUN(cfg_action_##name, cfg_action_##name##_cmd,			\
	"actions " #name,						\
	"Configure Actions\n" expl)					\
{									\
	vty->node = switch_to;						\
	vty->index = &s_mgr->variable;					\
	return CMD_SUCCESS;						\
}
CFG_ACTION(normal, "Normal Actions\n", ACT_NORM_NODE, action_norm)
CFG_ACTION(warn, "Warning Actions\n", ACT_WARN_NODE, action_warn)
CFG_ACTION(critical, "Critical Actions\n", ACT_CRIT_NODE, action_crit)
#undef CFG_ACTION

DEFUN(cfg_action_pa_on, cfg_action_pa_on_cmd,
	"pa-on",
	"Switch the Power Amplifier on\n")
{
	int *action = vty->index;
	*action |= TEMP_ACT_NORM_PA_ON;
	return CMD_SUCCESS;
}

DEFUN(cfg_no_action_pa_on, cfg_no_action_pa_on_cmd,
	"no pa-on",
	NO_STR "Switch the Power Amplifier on\n")
{
	int *action = vty->index;
	*action &= ~TEMP_ACT_NORM_PA_ON;
	return CMD_SUCCESS;
}

DEFUN(cfg_action_bts_srv_on, cfg_action_bts_srv_on_cmd,
	"bts-service-on",
	"Start the systemd sysmobts.service\n")
{
	int *action = vty->index;
	*action |= TEMP_ACT_NORM_BTS_SRV_ON;
	return CMD_SUCCESS;
}

DEFUN(cfg_no_action_bts_srv_on, cfg_no_action_bts_srv_on_cmd,
	"no bts-service-on",
	NO_STR "Start the systemd sysmobts.service\n")
{
	int *action = vty->index;
	*action &= ~TEMP_ACT_NORM_BTS_SRV_ON;
	return CMD_SUCCESS;
}

DEFUN(cfg_action_pa_off, cfg_action_pa_off_cmd,
	"pa-off",
	"Switch the Power Amplifier off\n")
{
	int *action = vty->index;
	*action |= TEMP_ACT_PA_OFF;
	return CMD_SUCCESS;
}

DEFUN(cfg_no_action_pa_off, cfg_no_action_pa_off_cmd,
	"no pa-off",
	NO_STR "Do not switch off the Power Amplifier\n")
{
	int *action = vty->index;
	*action &= ~TEMP_ACT_PA_OFF;
	return CMD_SUCCESS;
}

DEFUN(cfg_action_bts_srv_off, cfg_action_bts_srv_off_cmd,
	"bts-service-off",
	"Stop the systemd sysmobts.service\n")
{
	int *action = vty->index;
	*action |= TEMP_ACT_BTS_SRV_OFF;
	return CMD_SUCCESS;
}

DEFUN(cfg_no_action_bts_srv_off, cfg_no_action_bts_srv_off_cmd,
	"no bts-service-off",
	NO_STR "Stop the systemd sysmobts.service\n")
{
	int *action = vty->index;
	*action &= ~TEMP_ACT_BTS_SRV_OFF;
	return CMD_SUCCESS;
}

DEFUN(show_mgr, show_mgr_cmd, "show manager",
      SHOW_STR "Display information about the manager")
{
	vty_out(vty, "Temperature control state: %s%s",
		sysmobts_mgr_temp_get_state(s_mgr->state), VTY_NEWLINE);
	vty_out(vty, "Current Temperatures%s", VTY_NEWLINE);
	vty_out(vty, " Digital: %f Celcius%s",
		sysmobts_temp_get(SYSMOBTS_TEMP_DIGITAL,
					SYSMOBTS_TEMP_INPUT) / 1000.0f,
		VTY_NEWLINE);
	vty_out(vty, " RF:      %f Celcius%s",
		sysmobts_temp_get(SYSMOBTS_TEMP_RF,
					SYSMOBTS_TEMP_INPUT) / 1000.0f,
		VTY_NEWLINE);
	if (is_sbts2050()) {
		int temp_pa, temp_board;
		struct sbts2050_power_status status;

		vty_out(vty, " sysmoBTS 2050 is %s%s",
			is_sbts2050_master() ? "master" : "slave", VTY_NEWLINE);

		sbts2050_uc_check_temp(&temp_pa, &temp_board);
		vty_out(vty, " sysmoBTS 2050 PA: %d Celcius%s", temp_pa, VTY_NEWLINE);
		vty_out(vty, " sysmoBTS 2050 PA: %d Celcius%s", temp_board, VTY_NEWLINE);

		sbts2050_uc_get_status(&status);
		vty_out(vty, "Power Status%s", VTY_NEWLINE);
		vty_out(vty, " Main Supply :(ON)  [(24.00)Vdc, %4.2f A]%s",
			status.main_supply_current, VTY_NEWLINE);
		vty_out(vty, " Master SF   : %s  [%6.2f Vdc, %4.2f A]%s",
			status.master_enabled ? "ON " : "OFF",
			status.master_voltage, status.master_current,
			VTY_NEWLINE);
		vty_out(vty, " Slave SF    : %s   [%6.2f Vdc, %4.2f A]%s",
			status.slave_enabled ? "ON" : "OFF",
			status.slave_voltage, status.slave_current,
			VTY_NEWLINE);
		vty_out(vty, " Power Amp   : %s  [%6.2f Vdc, %4.2f A]%s",
			status.pa_enabled ? "ON" : "OFF",
			status.pa_voltage, status.pa_current,
			VTY_NEWLINE);
		vty_out(vty, " PA Bias     : %s  [%6.2f Vdc, ---- A]%s",
			status.pa_enabled ? "ON" : "OFF",
			status.pa_bias_voltage,
			VTY_NEWLINE);
	}

	return CMD_SUCCESS;
}

static void register_limit(int limit)
{
	install_element(limit, &cfg_thresh_warning_cmd);
	install_element(limit, &cfg_thresh_crit_cmd);
}

static void register_normal_action(int act)
{
	install_element(act, &cfg_action_pa_on_cmd);
	install_element(act, &cfg_no_action_pa_on_cmd);
	install_element(act, &cfg_action_bts_srv_on_cmd);
	install_element(act, &cfg_no_action_bts_srv_on_cmd);
}

static void register_action(int act)
{
#if 0
	install_element(act, &cfg_action_pwr_contrl_cmd);
	install_element(act, &cfg_no_action_pwr_contrl_cmd);

	/* these only work on the sysmobts 2050 */
	install_element(act, &cfg_action_master_off_cmd);
	install_element(act, &cfg_no_action_master_off_cmd);
	install_element(act, &cfg_action_slave_off_cmd);
	install_element(act, &cfg_no_action_slave_off_cmd);
#endif
	install_element(act, &cfg_action_pa_off_cmd);
	install_element(act, &cfg_no_action_pa_off_cmd);
	install_element(act, &cfg_action_bts_srv_off_cmd);
	install_element(act, &cfg_no_action_bts_srv_off_cmd);
}

int sysmobts_mgr_vty_init(void)
{
	vty_init(&vty_info);

	install_element_ve(&show_mgr_cmd);

	install_node(&mgr_node, config_write_mgr);
	install_element(CONFIG_NODE, &cfg_mgr_cmd);
	vty_install_default(MGR_NODE);

	/* install the limit nodes */
	install_node(&limit_rf_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_limit_rf_cmd);
	register_limit(LIMIT_RF_NODE);
	vty_install_default(LIMIT_RF_NODE);

	install_node(&limit_digital_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_limit_digital_cmd);
	register_limit(LIMIT_DIGITAL_NODE);
	vty_install_default(LIMIT_DIGITAL_NODE);

	install_node(&limit_board_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_limit_board_cmd);
	register_limit(LIMIT_BOARD_NODE);
	vty_install_default(LIMIT_BOARD_NODE);

	install_node(&limit_pa_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_limit_pa_cmd);
	register_limit(LIMIT_PA_NODE);
	vty_install_default(LIMIT_PA_NODE);

	/* install the normal node */
	install_node(&act_norm_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_action_normal_cmd);
	register_normal_action(ACT_NORM_NODE);

	/* install the warning and critical node */
	install_node(&act_warn_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_action_warn_cmd);
	register_action(ACT_WARN_NODE);
	vty_install_default(ACT_WARN_NODE);

	install_node(&act_crit_node, config_write_dummy);
	install_element(MGR_NODE, &cfg_action_critical_cmd);
	register_action(ACT_CRIT_NODE);
	vty_install_default(ACT_CRIT_NODE);

	return 0;
}

int sysmobts_mgr_parse_config(struct sysmobts_mgr_instance *manager)
{
	int rc;

	s_mgr = manager;
	rc = vty_read_config_file(s_mgr->config_file, NULL);
	if (rc < 0) {
		fprintf(stderr, "Failed to parse the config file: '%s'\n",
				s_mgr->config_file);
		return rc;
	}

	return 0;
}