2016-06-29 18:41:58 +00:00
|
|
|
/*
|
|
|
|
* OsmocomBB <-> SDR connection bridge
|
|
|
|
*
|
|
|
|
* (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2018-03-10 21:18:06 +00:00
|
|
|
#include <time.h>
|
2016-06-29 18:41:58 +00:00
|
|
|
|
2017-07-06 06:05:27 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
#include <osmocom/core/fsm.h>
|
2016-06-29 18:41:58 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/signal.h>
|
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/application.h>
|
|
|
|
|
2017-07-06 04:50:34 +00:00
|
|
|
#include <osmocom/gsm/gsm_utils.h>
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
#include "trxcon.h"
|
2016-07-23 19:40:41 +00:00
|
|
|
#include "trx_if.h"
|
2016-06-29 18:41:58 +00:00
|
|
|
#include "logging.h"
|
2016-07-24 18:20:37 +00:00
|
|
|
#include "l1ctl.h"
|
2016-07-22 16:57:50 +00:00
|
|
|
#include "l1ctl_link.h"
|
2016-07-24 18:20:37 +00:00
|
|
|
#include "l1ctl_proto.h"
|
2017-07-04 13:55:12 +00:00
|
|
|
#include "scheduler.h"
|
|
|
|
#include "sched_trx.h"
|
2016-06-29 18:41:58 +00:00
|
|
|
|
|
|
|
#define COPYRIGHT \
|
|
|
|
"Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>\n" \
|
|
|
|
"License GPLv2+: GNU GPL version 2 or later " \
|
|
|
|
"<http://gnu.org/licenses/gpl.html>\n" \
|
|
|
|
"This is free software: you are free to change and redistribute it.\n" \
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\n\n"
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *debug_mask;
|
|
|
|
int daemonize;
|
|
|
|
int quit;
|
|
|
|
|
2016-07-22 16:57:50 +00:00
|
|
|
/* L1CTL specific */
|
|
|
|
struct l1ctl_link *l1l;
|
|
|
|
const char *bind_socket;
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* TRX specific */
|
|
|
|
struct trx_instance *trx;
|
2016-06-29 18:41:58 +00:00
|
|
|
const char *trx_ip;
|
|
|
|
uint16_t trx_base_port;
|
2017-11-23 13:05:00 +00:00
|
|
|
uint32_t trx_fn_advance;
|
2016-06-29 18:41:58 +00:00
|
|
|
} app_data;
|
|
|
|
|
|
|
|
void *tall_trx_ctx = NULL;
|
2017-05-31 02:28:40 +00:00
|
|
|
struct osmo_fsm_inst *trxcon_fsm;
|
|
|
|
|
|
|
|
static void trxcon_fsm_idle_action(struct osmo_fsm_inst *fi,
|
|
|
|
uint32_t event, void *data)
|
|
|
|
{
|
|
|
|
if (event == L1CTL_EVENT_CONNECT)
|
|
|
|
osmo_fsm_inst_state_chg(trxcon_fsm, TRXCON_STATE_MANAGED, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trxcon_fsm_managed_action(struct osmo_fsm_inst *fi,
|
|
|
|
uint32_t event, void *data)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case L1CTL_EVENT_DISCONNECT:
|
|
|
|
osmo_fsm_inst_state_chg(trxcon_fsm, TRXCON_STATE_IDLE, 0, 0);
|
|
|
|
|
|
|
|
if (app_data.trx->fsm->state != TRX_STATE_OFFLINE) {
|
2017-07-27 10:53:09 +00:00
|
|
|
/* Reset scheduler and clock counter */
|
|
|
|
sched_trx_reset(app_data.trx, 1);
|
2017-07-10 09:56:43 +00:00
|
|
|
|
2017-07-08 11:50:14 +00:00
|
|
|
/* TODO: implement trx_if_reset() */
|
2017-05-31 02:28:40 +00:00
|
|
|
trx_if_cmd_poweroff(app_data.trx);
|
2017-07-27 03:14:20 +00:00
|
|
|
trx_if_cmd_echo(app_data.trx);
|
2017-05-31 02:28:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TRX_EVENT_RSP_ERROR:
|
|
|
|
case TRX_EVENT_OFFLINE:
|
|
|
|
/* TODO: notify L2 & L3 about that */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGPFSML(fi, LOGL_ERROR, "Unhandled event %u\n", event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct osmo_fsm_state trxcon_fsm_states[] = {
|
|
|
|
[TRXCON_STATE_IDLE] = {
|
|
|
|
.in_event_mask = GEN_MASK(L1CTL_EVENT_CONNECT),
|
|
|
|
.out_state_mask = GEN_MASK(TRXCON_STATE_MANAGED),
|
|
|
|
.name = "IDLE",
|
|
|
|
.action = trxcon_fsm_idle_action,
|
|
|
|
},
|
|
|
|
[TRXCON_STATE_MANAGED] = {
|
|
|
|
.in_event_mask = (
|
|
|
|
GEN_MASK(L1CTL_EVENT_DISCONNECT) |
|
|
|
|
GEN_MASK(TRX_EVENT_RSP_ERROR) |
|
2017-07-27 02:57:13 +00:00
|
|
|
GEN_MASK(TRX_EVENT_OFFLINE)),
|
2017-05-31 02:28:40 +00:00
|
|
|
.out_state_mask = GEN_MASK(TRXCON_STATE_IDLE),
|
|
|
|
.name = "MANAGED",
|
|
|
|
.action = trxcon_fsm_managed_action,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-02-28 21:46:29 +00:00
|
|
|
static const struct value_string app_evt_names[] = {
|
|
|
|
OSMO_VALUE_STRING(L1CTL_EVENT_CONNECT),
|
|
|
|
OSMO_VALUE_STRING(L1CTL_EVENT_DISCONNECT),
|
|
|
|
OSMO_VALUE_STRING(TRX_EVENT_OFFLINE),
|
|
|
|
OSMO_VALUE_STRING(TRX_EVENT_RSP_ERROR),
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
static struct osmo_fsm trxcon_fsm_def = {
|
|
|
|
.name = "trxcon_app_fsm",
|
|
|
|
.states = trxcon_fsm_states,
|
|
|
|
.num_states = ARRAY_SIZE(trxcon_fsm_states),
|
|
|
|
.log_subsys = DAPP,
|
2018-02-28 21:46:29 +00:00
|
|
|
.event_names = app_evt_names,
|
2017-05-31 02:28:40 +00:00
|
|
|
};
|
2016-06-29 18:41:58 +00:00
|
|
|
|
|
|
|
static void print_usage(const char *app)
|
|
|
|
{
|
|
|
|
printf("Usage: %s\n", app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help(void)
|
|
|
|
{
|
|
|
|
printf(" Some help...\n");
|
|
|
|
printf(" -h --help this text\n");
|
|
|
|
printf(" -d --debug Change debug flags. Default: %s\n", DEBUG_DEFAULT);
|
|
|
|
printf(" -i --trx-ip IP address of host runing TRX (default 127.0.0.1)\n");
|
2018-02-22 15:10:01 +00:00
|
|
|
printf(" -p --trx-port Base port of TRX instance (default 6700)\n");
|
2017-11-23 13:05:00 +00:00
|
|
|
printf(" -f --trx-advance Scheduler clock advance (default 20)\n");
|
2016-06-29 18:41:58 +00:00
|
|
|
printf(" -s --socket Listening socket for layer23 (default /tmp/osmocom_l2)\n");
|
|
|
|
printf(" -D --daemonize Run as daemon\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"debug", 1, 0, 'd'},
|
|
|
|
{"socket", 1, 0, 's'},
|
|
|
|
{"trx-ip", 1, 0, 'i'},
|
|
|
|
{"trx-port", 1, 0, 'p'},
|
2017-11-23 13:05:00 +00:00
|
|
|
{"trx-advance", 1, 0, 'f'},
|
2016-06-29 18:41:58 +00:00
|
|
|
{"daemonize", 0, 0, 'D'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2017-11-23 13:05:00 +00:00
|
|
|
c = getopt_long(argc, argv, "d:i:p:f:s:Dh",
|
2016-06-29 18:41:58 +00:00
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_usage(argv[0]);
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
app_data.debug_mask = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
app_data.trx_ip = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
app_data.trx_base_port = atoi(optarg);
|
|
|
|
break;
|
2017-11-23 13:05:00 +00:00
|
|
|
case 'f':
|
|
|
|
app_data.trx_fn_advance = atoi(optarg);
|
|
|
|
break;
|
2016-06-29 18:41:58 +00:00
|
|
|
case 's':
|
|
|
|
app_data.bind_socket = optarg;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
app_data.daemonize = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_defaults(void)
|
|
|
|
{
|
|
|
|
app_data.bind_socket = "/tmp/osmocom_l2";
|
|
|
|
app_data.trx_ip = "127.0.0.1";
|
2018-02-22 15:10:01 +00:00
|
|
|
app_data.trx_base_port = 6700;
|
2017-11-23 13:05:00 +00:00
|
|
|
app_data.trx_fn_advance = 20;
|
2016-06-29 18:41:58 +00:00
|
|
|
|
|
|
|
app_data.debug_mask = NULL;
|
|
|
|
app_data.daemonize = 0;
|
|
|
|
app_data.quit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler(int signal)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "signal %u received\n", signal);
|
|
|
|
|
|
|
|
switch (signal) {
|
|
|
|
case SIGINT:
|
|
|
|
app_data.quit++;
|
|
|
|
break;
|
|
|
|
case SIGABRT:
|
|
|
|
case SIGUSR1:
|
|
|
|
case SIGUSR2:
|
|
|
|
talloc_report_full(tall_trx_ctx, stderr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
printf("%s", COPYRIGHT);
|
|
|
|
init_defaults();
|
|
|
|
handle_options(argc, argv);
|
|
|
|
|
|
|
|
/* Init talloc memory management system */
|
|
|
|
tall_trx_ctx = talloc_init("trxcon context");
|
|
|
|
msgb_talloc_ctx_init(tall_trx_ctx, 0);
|
|
|
|
|
|
|
|
/* Setup signal handlers */
|
|
|
|
signal(SIGINT, &signal_handler);
|
|
|
|
signal(SIGUSR1, &signal_handler);
|
|
|
|
signal(SIGUSR2, &signal_handler);
|
|
|
|
osmo_init_ignore_signals();
|
|
|
|
|
|
|
|
/* Init logging system */
|
|
|
|
trx_log_init(app_data.debug_mask);
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Allocate the application state machine */
|
|
|
|
osmo_fsm_register(&trxcon_fsm_def);
|
|
|
|
trxcon_fsm = osmo_fsm_inst_alloc(&trxcon_fsm_def, tall_trx_ctx,
|
|
|
|
NULL, LOGL_DEBUG, "main");
|
|
|
|
|
2016-07-22 16:57:50 +00:00
|
|
|
/* Init L1CTL server */
|
|
|
|
rc = l1ctl_link_init(&app_data.l1l, app_data.bind_socket);
|
|
|
|
if (rc)
|
|
|
|
goto exit;
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Init transceiver interface */
|
2018-03-01 13:40:32 +00:00
|
|
|
rc = trx_if_open(&app_data.trx, "0.0.0.0", app_data.trx_ip, app_data.trx_base_port);
|
2016-07-23 19:40:41 +00:00
|
|
|
if (rc)
|
|
|
|
goto exit;
|
|
|
|
|
2017-07-08 11:16:42 +00:00
|
|
|
/* Bind L1CTL with TRX and vice versa */
|
|
|
|
app_data.l1l->trx = app_data.trx;
|
|
|
|
app_data.trx->l1l = app_data.l1l;
|
|
|
|
|
2017-07-04 13:55:12 +00:00
|
|
|
/* Init scheduler */
|
2017-11-23 13:05:00 +00:00
|
|
|
rc = sched_trx_init(app_data.trx, app_data.trx_fn_advance);
|
2017-07-04 13:55:12 +00:00
|
|
|
if (rc)
|
|
|
|
goto exit;
|
|
|
|
|
2016-07-22 16:57:50 +00:00
|
|
|
LOGP(DAPP, LOGL_NOTICE, "Init complete\n");
|
2016-06-29 18:41:58 +00:00
|
|
|
|
|
|
|
if (app_data.daemonize) {
|
|
|
|
rc = osmo_daemonize();
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error during daemonize");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:18:06 +00:00
|
|
|
/* Initialize pseudo-random generator */
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2016-06-29 18:41:58 +00:00
|
|
|
while (!app_data.quit)
|
|
|
|
osmo_select_main(0);
|
|
|
|
|
|
|
|
exit:
|
2016-07-22 16:57:50 +00:00
|
|
|
/* Close active connections */
|
|
|
|
l1ctl_link_shutdown(app_data.l1l);
|
2017-07-04 13:55:12 +00:00
|
|
|
sched_trx_shutdown(app_data.trx);
|
2016-07-23 19:40:41 +00:00
|
|
|
trx_if_close(app_data.trx);
|
2016-07-22 16:57:50 +00:00
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Shutdown main state machine */
|
|
|
|
osmo_fsm_inst_free(trxcon_fsm);
|
|
|
|
|
2016-06-29 18:41:58 +00:00
|
|
|
/* Make Valgrind happy */
|
|
|
|
log_fini();
|
|
|
|
talloc_free(tall_trx_ctx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|