aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-e1gen.c
blob: 8517651b5b3bab0fe9ac974d82f4e41c1d3f49d4 (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
/*
 * osmo-e1gen.c
 *
 * (C) 2020 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

#define _GNU_SOURCE
#include <getopt.h>

#include <talloc.h>

#include <osmocom/core/application.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/logging.h>
#include <osmocom/vty/vty.h>
#include <osmocom/vty/command.h>

#include "e1d.h"
#include "usb.h"
#include "log.h"

#include "e1gen/osmo_e1f.h"

#ifndef OSMO_VTY_PORT_E1D
#define OSMO_VTY_PORT_E1D	4269
#endif

/***********************************************************************
 * global variables
 ***********************************************************************/

extern struct e1_daemon *vty_e1d;
static const char *g_config_file = "osmo-e1d.cfg";
static void *g_e1d_ctx = NULL;
static int g_shutdown = 0;

/***********************************************************************
 * stubs for external linkage of normal osmo-e1d
 ***********************************************************************/

void e1_ts_stop(struct e1_ts *ts) {}
int e1d_vpair_create(struct e1_daemon *e1d, unsigned int num_lines) { return -1; }
struct e1_intf *e1d_vpair_intf_peer(struct e1_intf *intf) { return NULL; }



struct e1gen_line_data {
	struct ice1usb_tx_config tx_config;
	struct ice1usb_rx_config rx_config;
	struct osmo_e1f_instance e1f;
};

static void e1f_notify_cb(struct osmo_e1f_instance *e1f, enum osmo_e1f_notify_event evt,
			  bool present, void *data)
{
	struct e1_line *line = e1f->priv;
	LOGPLI(line, DE1D, LOGL_NOTICE, "NOTIFY: %s %s\n",
		osmo_e1f_notify_event_name(evt), present ? "PRESENT" : "ABSENT");
}

static const struct e1gen_line_data default_ld = {
	.tx_config = {
		.mode = ICE1USB_TX_MODE_TRANSP,
		.timing = ICE1USB_TX_TIME_SRC_LOCAL,
		.ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF,
		.alarm = 0,
	},
	.rx_config = {
		.mode = ICE1USB_RX_MODE_MULTIFRAME,
		//.mode = ICE1USB_RX_MODE_TRANSP,
	},
};

struct e1gen_line_data *ensure_gld(struct e1_line *line)
{
	struct e1gen_line_data *gld = (struct e1gen_line_data *) line->e1gen_priv;

	if (gld)
		return gld;

	LOGPLI(line, DE1D, LOGL_INFO, "Creating e1gen structures\n");

	line->e1gen_priv = gld = talloc_zero(line, struct e1gen_line_data);
	gld->tx_config = default_ld.tx_config;
	gld->rx_config = default_ld.rx_config;
	osmo_e1f_instance_init(&gld->e1f, "LINE", e1f_notify_cb, true, line);

	/* set the default configuration */
	e1_usb_ctrl_set_tx_cfg(line, gld->tx_config.mode, gld->tx_config.timing,
				gld->tx_config.ext_loopback, gld->tx_config.alarm);
	e1_usb_ctrl_set_rx_cfg(line, gld->rx_config.mode);

	return gld;
}

/*! process (demultiplex) input data for the specified e1_line.
 *  \param[in] line E1 line for which to genrate output data
 *  \param[in] buf input buffer of multiplexed data received on line
 *  \param[in] size size of buf in octets */
int e1_line_demux_in(struct e1_line *line, const uint8_t *buf, int size,  int frame_base)
{
	struct e1gen_line_data *gld = ensure_gld(line);
	int offs;

	for (offs = 0; offs + 32 <= size; offs += 32) {
		//printf("Rx "OSMO_BIT_SPEC"\n", OSMO_BIT_PRINT(buf[offs]));
		//printf("Rx %s\n", osmo_hexdump(buf + offs, 32));
		osmo_e1f_rx_frame(&gld->e1f, buf + offs);
	}
	return 0;
}

/*! generate (multiplex) output data for the specified e1_line
 *  \param[in] line E1 line for which to genrate output data
 *  \param[in] buf caller-allocated output buffer for multiplexed data
 *  \param[in] fts number of E1 frames (32 bytes each) to generate
 *  \return number of bytes written to buf */
int e1_line_mux_out(struct e1_line *line, uint8_t *buf, int fts)
{
	struct e1gen_line_data *gld = ensure_gld(line);
	int f;

	//printf("FRAME:\n");
	for (f = 0; f < fts; f++) {
		osmo_e1f_pull_tx_frame(&gld->e1f, buf + f*32);
		//printf("Tx "OSMO_BIT_SPEC"\n", OSMO_BIT_PRINT(buf[f*32]));
		//printf("Tx %s\n", osmo_hexdump(buf + f*32, 32));
	}

	return 32*fts;
}

static struct cmd_node line_node = {
	(enum node_type) LINE_NODE,
	"%s(line)# ",
	1,
};

#define TX_STR "Transmitter\n"
#define OFF_ON_STR "Off\n" "On\n"

DEFUN(line_tx_alarm, line_tx_alarm_cmd,
	"tx report-alarm (0|1)",
	TX_STR "Report Alarm (A-Bits)\n" OFF_ON_STR)
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);
	int on = atoi(argv[0]);

	gld->e1f.tx.remote_alarm = on ? true : false;

	return CMD_SUCCESS;
}

DEFUN(line_tx_crc4, line_tx_crc4_cmd,
	"tx generate-crc4 (0|1)",
	TX_STR "Generate CRC4\n" OFF_ON_STR)
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);
	int on = atoi(argv[0]);

	gld->e1f.crc4_enabled = on ? true : false;

	return CMD_SUCCESS;
}

DEFUN(line_tx_report_crc4, line_tx_report_crc4_cmd,
	"tx report-crc4-error (0|1)",
	TX_STR "Report a CRC4 Error\n" OFF_ON_STR)
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);
	int on = atoi(argv[0]);

	gld->e1f.tx.crc4_error = on ? true : false;

	return CMD_SUCCESS;
}

DEFUN(line_tx_sa48, line_tx_sa48_cmd,
	"tx sa4 <0-31>",
	TX_STR "Set the SA4..SA8 bit content\n")
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);

	gld->e1f.tx.sa4_sa8 = atoi(argv[0]);

	return CMD_SUCCESS;
}

DEFUN(line_tx_ais, line_tx_ais_cmd,
	"tx ais (0|1)",
	TX_STR "Transmit AIS (All-1)\n" OFF_ON_STR)
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);
	int on = atoi(argv[0]);

	gld->e1f.tx.ais = on ? true : false;

	return CMD_SUCCESS;
}

DEFUN(line_show, line_show_cmd,
	"show", "Show Line status\n")
{
	struct e1_line *line = vty->index;
	struct e1gen_line_data *gld = ensure_gld(line);
	struct osmo_e1f_instance *e1f = &gld->e1f;

	vty_out(vty, "Rx RemoteAlarm=%u, RemoteCrcErr=%u%s",
		e1f->rx.remote_alarm, e1f->rx.remote_crc4_error, VTY_NEWLINE);
	vty_out(vty, "Tx RemoteAlarm=%u, CrcErr=%u%s",
		e1f->tx.remote_alarm, e1f->tx.crc4_error, VTY_NEWLINE);

	return CMD_SUCCESS;
}

DEFUN(line, line_cmd,
	"interface <0-255> line <0-225>",
	"E1 Interface\n" "E1 Interface\n"
	"E1 Line number\n" "E1 Line number\n")
{
	struct e1_intf *intf;
	struct e1_line *line;

	intf = e1d_find_intf(vty_e1d, atoi(argv[0]));
	if (!intf) {
		vty_out(vty, "No such interface %s%s", argv[0], VTY_NEWLINE);
		return CMD_WARNING;
	}

	line = e1_intf_find_line(intf, atoi(argv[1]));
	if (!line) {
		vty_out(vty, "No such line %s in interface %s%s", argv[1], argv[0], VTY_NEWLINE);
		return CMD_WARNING;
	}

	vty->node = LINE_NODE;
	vty->index = line;

	return CMD_SUCCESS;
}

static void e1gen_init(void)
{
	install_element(ENABLE_NODE, &line_cmd);
	install_node(&line_node, NULL);
	install_element(LINE_NODE, &line_tx_alarm_cmd);
	install_element(LINE_NODE, &line_tx_crc4_cmd);
	install_element(LINE_NODE, &line_tx_report_crc4_cmd);
	install_element(LINE_NODE, &line_tx_sa48_cmd);
	install_element(LINE_NODE, &line_tx_ais_cmd);
	install_element(LINE_NODE, &line_show_cmd);
}

/***********************************************************************
 * initialization (mostly shared with osmo-e1d.c
 ***********************************************************************/

static struct vty_app_info vty_info = {
	.name = "osmo-e1gen",
	.version = PACKAGE_VERSION,
	.copyright =
	"(C) 2019-2020 by Sylvain Munaut and Harald Welte\r\n",
	"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.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 void sig_handler(int signo)
{
	fprintf(stdout, "signal %d received\n", signo);
	switch (signo) {
	case SIGINT:
	case SIGTERM:
		fprintf(stdout, "shutting down\n");
		g_shutdown = 1;
		break;
	case SIGABRT:
	case SIGUSR1:
		talloc_report(g_e1d_ctx, stderr);
		talloc_report_full(g_e1d_ctx, stderr);
		break;
	case SIGUSR2:
		talloc_report_full(g_e1d_ctx, stderr);
		break;
	default:
		break;
	}
}

static void print_help(void)
{
	printf("  Some useful help...\n");
	printf("  -h --help			This text.\n");
	printf("  -d --debug option		--debug=DE1D:DXFR enable debugging.\n");
	printf("  -c --config-file filename	The config file to use.\n");
}

static void handle_options(int argc, char **argv)
{
	while (1) {
		int option_index = 0, c;
		static const struct option long_options[] = {
			{"help", 0, 0, 'h'},
			{"debug", 1, 0, 'd'},
			{"config-file", 1, 0, 'c'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "hd:c:", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_help();
			exit(0);
			break;
		case 'd':
			log_parse_category_mask(osmo_stderr_target, optarg);
			break;
		case 'c':
			g_config_file = optarg;
			break;
		default:
			fprintf(stderr, "Error in command line options. Exiting.\n");
			exit(-1);
		}
	}

	if (argc > optind) {
		fprintf(stderr, "Unsupported positional arguments on command line\n");
		exit(2);
	}
}

int main(int argc, char **argv)
{
	struct e1_daemon *e1d = NULL;
	struct sched_param sp;
	int rv;

	/* talloc init */
	g_e1d_ctx = talloc_named_const(NULL, 0, "osmo-e1d");
	msgb_talloc_ctx_init(g_e1d_ctx, 0);

	/* logging init */
	osmo_init_logging2(g_e1d_ctx, &log_info);

	/* signals init */
	signal(SIGINT, &sig_handler);
	signal(SIGTERM, &sig_handler);
	signal(SIGABRT, &sig_handler);
	signal(SIGUSR1, &sig_handler);
	signal(SIGUSR2, &sig_handler);
	osmo_init_ignore_signals();

	/* rt prio */
	memset(&sp, 0x00, sizeof(sp));
        sp.sched_priority = 50;
        rv = sched_setscheduler(0, SCHED_RR, &sp);
	if (rv != 0) {
		LOGP(DE1D, LOGL_ERROR, "Failed to set Real-Time priority. USB comms might be unstable.\n");
		perror("sched_setscheduler");
	}

	/* main state */
	e1d = talloc_zero(g_e1d_ctx, struct e1_daemon);
	OSMO_ASSERT(e1d);

	INIT_LLIST_HEAD(&e1d->interfaces);
	vty_init(&vty_info);
	logging_vty_add_cmds();
	e1d_vty_init(e1d);

	handle_options(argc, argv);

	rv = vty_read_config_file(g_config_file, NULL);
	if (rv < 0) {
		LOGP(DE1D, LOGL_FATAL, "Failed to parse the config file '%s'\n", g_config_file);
		exit(2);
	}

	rv = telnet_init_default(g_e1d_ctx, e1d, OSMO_VTY_PORT_E1D);
	if (rv != 0) {
		LOGP(DE1D, LOGL_FATAL, "Failed to bind VTY interface to %s:%u\n",
			vty_get_bind_addr(), OSMO_VTY_PORT_E1D);
		exit(1);
	}

	osmo_e1f_init();
	e1gen_init();

	/* probe devices */
	rv = e1_usb_init(e1d);
	if (rv != 0) {
		LOGP(DE1D, LOGL_ERROR, "Failed to prove usb devices\n");
	}

	/* main loop */
	while (!g_shutdown) {
		osmo_select_main(0);
	}

	talloc_free(e1d);

	return 0;
}