2016-07-23 19:40:41 +00:00
|
|
|
/*
|
|
|
|
* OsmocomBB <-> SDR connection bridge
|
|
|
|
* Transceiver interface handlers
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
|
|
|
|
* Copyright (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 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 Affero 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 <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/socket.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/bits.h>
|
2017-05-31 02:28:40 +00:00
|
|
|
#include <osmocom/core/fsm.h>
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
#include <osmocom/gsm/gsm_utils.h>
|
|
|
|
|
2022-07-22 12:54:14 +00:00
|
|
|
#include <osmocom/bb/l1sched/l1sched.h>
|
2022-07-01 10:45:12 +00:00
|
|
|
#include <osmocom/bb/trxcon/l1ctl.h>
|
|
|
|
#include <osmocom/bb/trxcon/trxcon.h>
|
|
|
|
#include <osmocom/bb/trxcon/trx_if.h>
|
|
|
|
#include <osmocom/bb/trxcon/logging.h>
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2022-07-18 19:19:07 +00:00
|
|
|
#define S(x) (1 << (x))
|
|
|
|
|
2018-02-28 21:46:29 +00:00
|
|
|
static struct value_string trx_evt_names[] = {
|
|
|
|
{ 0, NULL } /* no events? */
|
|
|
|
};
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
static struct osmo_fsm_state trx_fsm_states[] = {
|
|
|
|
[TRX_STATE_OFFLINE] = {
|
|
|
|
.out_state_mask = (
|
2022-07-18 19:19:07 +00:00
|
|
|
S(TRX_STATE_IDLE) |
|
|
|
|
S(TRX_STATE_RSP_WAIT)),
|
2017-05-31 02:28:40 +00:00
|
|
|
.name = "OFFLINE",
|
|
|
|
},
|
|
|
|
[TRX_STATE_IDLE] = {
|
|
|
|
.out_state_mask = UINT32_MAX,
|
|
|
|
.name = "IDLE",
|
|
|
|
},
|
|
|
|
[TRX_STATE_ACTIVE] = {
|
|
|
|
.out_state_mask = (
|
2022-07-18 19:19:07 +00:00
|
|
|
S(TRX_STATE_IDLE) |
|
|
|
|
S(TRX_STATE_RSP_WAIT)),
|
2017-05-31 02:28:40 +00:00
|
|
|
.name = "ACTIVE",
|
|
|
|
},
|
|
|
|
[TRX_STATE_RSP_WAIT] = {
|
|
|
|
.out_state_mask = (
|
2022-07-18 19:19:07 +00:00
|
|
|
S(TRX_STATE_IDLE) |
|
|
|
|
S(TRX_STATE_ACTIVE) |
|
|
|
|
S(TRX_STATE_OFFLINE)),
|
2017-05-31 02:28:40 +00:00
|
|
|
.name = "RSP_WAIT",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct osmo_fsm trx_fsm = {
|
2022-07-20 00:22:29 +00:00
|
|
|
.name = "trx_interface",
|
2017-05-31 02:28:40 +00:00
|
|
|
.states = trx_fsm_states,
|
|
|
|
.num_states = ARRAY_SIZE(trx_fsm_states),
|
|
|
|
.log_subsys = DTRX,
|
2018-02-28 21:46:29 +00:00
|
|
|
.event_names = trx_evt_names,
|
2017-05-31 02:28:40 +00:00
|
|
|
};
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2018-03-01 13:40:32 +00:00
|
|
|
static int trx_udp_open(void *priv, struct osmo_fd *ofd, const char *host_local,
|
|
|
|
uint16_t port_local, const char *host_remote, uint16_t port_remote,
|
2016-07-23 19:40:41 +00:00
|
|
|
int (*cb)(struct osmo_fd *fd, unsigned int what))
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
ofd->data = priv;
|
|
|
|
ofd->fd = -1;
|
|
|
|
ofd->cb = cb;
|
|
|
|
|
2018-03-01 13:40:32 +00:00
|
|
|
/* Init UDP Connection */
|
|
|
|
rc = osmo_sock_init2_ofd(ofd, AF_UNSPEC, SOCK_DGRAM, 0, host_local, port_local,
|
|
|
|
host_remote, port_remote,
|
|
|
|
OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
|
2016-07-23 19:40:41 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trx_udp_close(struct osmo_fd *ofd)
|
|
|
|
{
|
|
|
|
if (ofd->fd > 0) {
|
|
|
|
osmo_fd_unregister(ofd);
|
|
|
|
close(ofd->fd);
|
|
|
|
ofd->fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* Control (CTRL) interface handlers */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* Commands on the Per-ARFCN Control Interface */
|
|
|
|
/* */
|
|
|
|
/* The per-ARFCN control interface uses a command-response protocol. */
|
|
|
|
/* Commands are NULL-terminated ASCII strings, one per UDP socket. */
|
|
|
|
/* Each command has a corresponding response. */
|
|
|
|
/* Every command is of the form: */
|
|
|
|
/* */
|
|
|
|
/* CMD <cmdtype> [params] */
|
|
|
|
/* */
|
|
|
|
/* The <cmdtype> is the actual command. */
|
|
|
|
/* Parameters are optional depending on the commands type. */
|
|
|
|
/* Every response is of the form: */
|
|
|
|
/* */
|
|
|
|
/* RSP <cmdtype> <status> [result] */
|
|
|
|
/* */
|
|
|
|
/* The <status> is 0 for success and a non-zero error code for failure. */
|
|
|
|
/* Successful responses may include results, depending on the command type. */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
static void trx_ctrl_timer_cb(void *data);
|
|
|
|
|
|
|
|
/* Send first CTRL message and start timer */
|
|
|
|
static void trx_ctrl_send(struct trx_instance *trx)
|
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
|
|
|
|
if (llist_empty(&trx->trx_ctrl_list))
|
|
|
|
return;
|
|
|
|
tcm = llist_entry(trx->trx_ctrl_list.next, struct trx_ctrl_msg, list);
|
|
|
|
|
|
|
|
/* Send command */
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_DEBUG, "Sending control '%s'\n", tcm->cmd);
|
2016-07-23 19:40:41 +00:00
|
|
|
send(trx->trx_ofd_ctrl.fd, tcm->cmd, strlen(tcm->cmd) + 1, 0);
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Trigger state machine */
|
2022-07-19 23:41:55 +00:00
|
|
|
if (trx->fi->state != TRX_STATE_RSP_WAIT) {
|
|
|
|
trx->prev_state = trx->fi->state;
|
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_RSP_WAIT, 0, 0);
|
2017-05-31 02:28:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Start expire timer */
|
|
|
|
trx->trx_ctrl_timer.data = trx;
|
|
|
|
trx->trx_ctrl_timer.cb = trx_ctrl_timer_cb;
|
|
|
|
osmo_timer_schedule(&trx->trx_ctrl_timer, 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trx_ctrl_timer_cb(void *data)
|
|
|
|
{
|
2017-05-31 02:28:40 +00:00
|
|
|
struct trx_instance *trx = (struct trx_instance *) data;
|
2022-07-12 20:30:55 +00:00
|
|
|
struct trxcon_inst *trxcon = trx->trxcon;
|
2017-05-31 02:28:40 +00:00
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
|
|
|
|
/* Queue may be cleaned at this moment */
|
|
|
|
if (llist_empty(&trx->trx_ctrl_list))
|
|
|
|
return;
|
|
|
|
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_NOTICE, "No response from transceiver...\n");
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
tcm = llist_entry(trx->trx_ctrl_list.next, struct trx_ctrl_msg, list);
|
|
|
|
if (++tcm->retry_cnt > 3) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_NOTICE, "Transceiver offline\n");
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_OFFLINE, 0, 0);
|
2022-07-12 20:30:55 +00:00
|
|
|
osmo_fsm_inst_dispatch(trxcon->fi, TRX_EVENT_OFFLINE, trx);
|
2017-05-31 02:28:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Attempt to send a command again */
|
2017-05-31 02:28:40 +00:00
|
|
|
trx_ctrl_send(trx);
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new CTRL command to the trx_ctrl_list */
|
|
|
|
static int trx_ctrl_cmd(struct trx_instance *trx, int critical,
|
|
|
|
const char *cmd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
int len, pending = 0;
|
|
|
|
va_list ap;
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* TODO: make sure that transceiver online */
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
if (!llist_empty(&trx->trx_ctrl_list))
|
|
|
|
pending = 1;
|
|
|
|
|
|
|
|
/* Allocate a message */
|
|
|
|
tcm = talloc_zero(trx, struct trx_ctrl_msg);
|
|
|
|
if (!tcm)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Fill in command arguments */
|
|
|
|
if (fmt && fmt[0]) {
|
|
|
|
len = snprintf(tcm->cmd, sizeof(tcm->cmd) - 1, "CMD %s ", cmd);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(tcm->cmd + len, sizeof(tcm->cmd) - len - 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
} else {
|
|
|
|
snprintf(tcm->cmd, sizeof(tcm->cmd) - 1, "CMD %s", cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcm->cmd_len = strlen(cmd);
|
|
|
|
tcm->critical = critical;
|
|
|
|
llist_add_tail(&tcm->list, &trx->trx_ctrl_list);
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_INFO, "Adding new control '%s'\n", tcm->cmd);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Send message, if no pending messages */
|
|
|
|
if (!pending)
|
|
|
|
trx_ctrl_send(trx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Power Control
|
|
|
|
*
|
2017-06-03 12:02:20 +00:00
|
|
|
* ECHO is used to check transceiver availability.
|
|
|
|
* CMD ECHO
|
|
|
|
* RSP ECHO <status>
|
|
|
|
*
|
2016-07-23 19:40:41 +00:00
|
|
|
* POWEROFF shuts off transmitter power and stops the demodulator.
|
|
|
|
* CMD POWEROFF
|
|
|
|
* RSP POWEROFF <status>
|
|
|
|
*
|
|
|
|
* POWERON starts the transmitter and starts the demodulator.
|
|
|
|
* Initial power level is very low.
|
|
|
|
* This command fails if the transmitter and receiver are not yet tuned.
|
|
|
|
* This command fails if the transmit or receive frequency creates a conflict
|
|
|
|
* with another ARFCN that is already running.
|
|
|
|
* If the transceiver is already on, it response with success to this command.
|
|
|
|
* CMD POWERON
|
|
|
|
* RSP POWERON <status>
|
|
|
|
*/
|
|
|
|
|
2017-06-03 12:02:20 +00:00
|
|
|
int trx_if_cmd_echo(struct trx_instance *trx)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(trx, 1, "ECHO", "");
|
|
|
|
}
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
int trx_if_cmd_poweroff(struct trx_instance *trx)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(trx, 1, "POWEROFF", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
int trx_if_cmd_poweron(struct trx_instance *trx)
|
|
|
|
{
|
2019-05-30 13:21:45 +00:00
|
|
|
if (trx->powered_up) {
|
|
|
|
/* FIXME: this should be handled by the FSM, not here! */
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Suppressing POWERON as we're already powered up\n");
|
2019-05-30 13:21:45 +00:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2016-07-23 19:40:41 +00:00
|
|
|
return trx_ctrl_cmd(trx, 1, "POWERON", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timeslot Control
|
|
|
|
*
|
|
|
|
* SETSLOT sets the format of the uplink timeslots in the ARFCN.
|
|
|
|
* The <timeslot> indicates the timeslot of interest.
|
|
|
|
* The <chantype> indicates the type of channel that occupies the timeslot.
|
|
|
|
* A chantype of zero indicates the timeslot is off.
|
|
|
|
* CMD SETSLOT <timeslot> <chantype>
|
|
|
|
* RSP SETSLOT <status> <timeslot> <chantype>
|
|
|
|
*/
|
|
|
|
|
2022-07-11 22:10:32 +00:00
|
|
|
int trx_if_cmd_setslot(struct trx_instance *trx, uint8_t tn,
|
|
|
|
enum gsm_phys_chan_config pchan)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
2022-07-11 22:10:32 +00:00
|
|
|
/* Values correspond to 'enum ChannelCombination' in osmo-trx.git */
|
|
|
|
static const uint8_t chan_types[_GSM_PCHAN_MAX] = {
|
|
|
|
[GSM_PCHAN_UNKNOWN] = 0,
|
|
|
|
[GSM_PCHAN_NONE] = 0,
|
|
|
|
[GSM_PCHAN_CCCH] = 4,
|
|
|
|
[GSM_PCHAN_CCCH_SDCCH4] = 5,
|
|
|
|
[GSM_PCHAN_CCCH_SDCCH4_CBCH] = 5,
|
|
|
|
[GSM_PCHAN_TCH_F] = 1,
|
|
|
|
[GSM_PCHAN_TCH_H] = 3,
|
|
|
|
[GSM_PCHAN_SDCCH8_SACCH8C] = 7,
|
|
|
|
[GSM_PCHAN_SDCCH8_SACCH8C_CBCH] = 7,
|
|
|
|
[GSM_PCHAN_PDCH] = 13,
|
|
|
|
};
|
|
|
|
|
|
|
|
return trx_ctrl_cmd(trx, 1, "SETSLOT", "%u %u", tn, chan_types[pchan]);
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tuning Control
|
|
|
|
*
|
|
|
|
* (RX/TX)TUNE tunes the receiver to a given frequency in kHz.
|
|
|
|
* This command fails if the receiver is already running.
|
|
|
|
* (To re-tune you stop the radio, re-tune, and restart.)
|
|
|
|
* This command fails if the transmit or receive frequency
|
|
|
|
* creates a conflict with another ARFCN that is already running.
|
|
|
|
* CMD (RX/TX)TUNE <kHz>
|
|
|
|
* RSP (RX/TX)TUNE <status> <kHz>
|
|
|
|
*/
|
|
|
|
|
2018-09-05 18:47:50 +00:00
|
|
|
int trx_if_cmd_rxtune(struct trx_instance *trx, uint16_t band_arfcn)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
|
|
|
uint16_t freq10;
|
|
|
|
|
|
|
|
/* RX is downlink on MS side */
|
2018-09-05 18:47:50 +00:00
|
|
|
freq10 = gsm_arfcn2freq10(band_arfcn, 0);
|
2016-07-23 19:40:41 +00:00
|
|
|
if (freq10 == 0xffff) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-09-05 18:56:08 +00:00
|
|
|
return trx_ctrl_cmd(trx, 1, "RXTUNE", "%u", freq10 * 100);
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 18:47:50 +00:00
|
|
|
int trx_if_cmd_txtune(struct trx_instance *trx, uint16_t band_arfcn)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
|
|
|
uint16_t freq10;
|
|
|
|
|
|
|
|
/* TX is uplink on MS side */
|
2018-09-05 18:47:50 +00:00
|
|
|
freq10 = gsm_arfcn2freq10(band_arfcn, 1);
|
2016-07-23 19:40:41 +00:00
|
|
|
if (freq10 == 0xffff) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-09-05 18:56:08 +00:00
|
|
|
return trx_ctrl_cmd(trx, 1, "TXTUNE", "%u", freq10 * 100);
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:10:41 +00:00
|
|
|
/*
|
|
|
|
* Power measurement
|
|
|
|
*
|
|
|
|
* MEASURE instructs the transceiver to perform a power
|
|
|
|
* measurement on specified frequency. After receiving this
|
|
|
|
* request, transceiver should quickly re-tune to requested
|
|
|
|
* frequency, measure power level and re-tune back to the
|
|
|
|
* previous frequency.
|
|
|
|
* CMD MEASURE <kHz>
|
|
|
|
* RSP MEASURE <status> <kHz> <dB>
|
|
|
|
*/
|
|
|
|
|
|
|
|
int trx_if_cmd_measure(struct trx_instance *trx,
|
2018-09-05 18:47:50 +00:00
|
|
|
uint16_t band_arfcn_start, uint16_t band_arfcn_stop)
|
2017-07-13 21:10:41 +00:00
|
|
|
{
|
|
|
|
uint16_t freq10;
|
|
|
|
|
|
|
|
/* Update ARFCN range for measurement */
|
2018-09-05 18:47:50 +00:00
|
|
|
trx->pm_band_arfcn_start = band_arfcn_start;
|
|
|
|
trx->pm_band_arfcn_stop = band_arfcn_stop;
|
2017-07-13 21:10:41 +00:00
|
|
|
|
|
|
|
/* Calculate a frequency for current ARFCN (DL) */
|
2018-09-05 18:47:50 +00:00
|
|
|
freq10 = gsm_arfcn2freq10(band_arfcn_start, 0);
|
2017-07-13 21:10:41 +00:00
|
|
|
if (freq10 == 0xffff) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn_start);
|
2017-07-13 21:10:41 +00:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-09-05 18:56:08 +00:00
|
|
|
return trx_ctrl_cmd(trx, 1, "MEASURE", "%u", freq10 * 100);
|
2017-07-13 21:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trx_if_measure_rsp_cb(struct trx_instance *trx, char *resp)
|
|
|
|
{
|
2022-07-12 20:30:55 +00:00
|
|
|
struct trxcon_inst *trxcon = trx->trxcon;
|
2017-07-13 21:10:41 +00:00
|
|
|
unsigned int freq10;
|
2018-09-05 18:47:50 +00:00
|
|
|
uint16_t band_arfcn;
|
2017-07-13 21:10:41 +00:00
|
|
|
int dbm;
|
|
|
|
|
|
|
|
/* Parse freq. and power level */
|
|
|
|
sscanf(resp, "%u %d", &freq10, &dbm);
|
|
|
|
freq10 /= 100;
|
|
|
|
|
|
|
|
/* Check received ARFCN against expected */
|
2018-09-05 18:47:50 +00:00
|
|
|
band_arfcn = gsm_freq102arfcn((uint16_t) freq10, 0);
|
|
|
|
if (band_arfcn != trx->pm_band_arfcn_start) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Power measurement error: "
|
2017-07-13 21:10:41 +00:00
|
|
|
"response ARFCN=%u doesn't match expected ARFCN=%u\n",
|
2022-07-19 22:49:12 +00:00
|
|
|
band_arfcn & ~ARFCN_FLAG_MASK,
|
|
|
|
trx->pm_band_arfcn_start & ~ARFCN_FLAG_MASK);
|
2017-07-13 21:10:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send L1CTL_PM_CONF */
|
2022-07-19 23:20:01 +00:00
|
|
|
l1ctl_tx_pm_conf(trxcon->l1c, band_arfcn, dbm,
|
2018-09-05 18:47:50 +00:00
|
|
|
band_arfcn == trx->pm_band_arfcn_stop);
|
2017-07-13 21:10:41 +00:00
|
|
|
|
|
|
|
/* Schedule a next measurement */
|
2018-09-05 18:47:50 +00:00
|
|
|
if (band_arfcn != trx->pm_band_arfcn_stop)
|
|
|
|
trx_if_cmd_measure(trx, ++band_arfcn, trx->pm_band_arfcn_stop);
|
2017-07-13 21:10:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 16:49:29 +00:00
|
|
|
/*
|
|
|
|
* Timing Advance control
|
|
|
|
*
|
|
|
|
* SETTA instructs the transceiver to transmit bursts in
|
|
|
|
* advance calculated from requested TA value. This value is
|
|
|
|
* normally between 0 and 63, with each step representing
|
|
|
|
* an advance of one bit period (about 3.69 microseconds).
|
2018-09-06 19:44:00 +00:00
|
|
|
* Since OsmocomBB has a special feature, which allows one
|
|
|
|
* to spoof the distance from BTS, the range is extended.
|
|
|
|
* CMD SETTA <-128..127>
|
2017-12-04 16:49:29 +00:00
|
|
|
* RSP SETTA <status> <TA>
|
|
|
|
*/
|
|
|
|
|
|
|
|
int trx_if_cmd_setta(struct trx_instance *trx, int8_t ta)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(trx, 0, "SETTA", "%d", ta);
|
|
|
|
}
|
|
|
|
|
2018-09-06 18:14:14 +00:00
|
|
|
/*
|
2020-05-15 12:18:21 +00:00
|
|
|
* Frequency Hopping parameters indication.
|
2018-09-06 18:14:14 +00:00
|
|
|
*
|
2020-05-15 12:18:21 +00:00
|
|
|
* SETFH instructs transceiver to enable frequency hopping mode
|
|
|
|
* using the given HSN, MAIO, and Mobile Allocation parameters.
|
|
|
|
*
|
|
|
|
* CMD SETFH <HSN> <MAIO> <RXF1> <TXF1> [... <RXFN> <TXFN>]
|
|
|
|
*
|
|
|
|
* where <RXFN> and <TXFN> is a pair of Rx/Tx frequencies (in kHz)
|
|
|
|
* corresponding to one ARFCN the Mobile Allocation. Note that the
|
|
|
|
* channel list is expected to be sorted in ascending order.
|
2018-09-06 18:14:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int trx_if_cmd_setfh(struct trx_instance *trx, uint8_t hsn,
|
|
|
|
uint8_t maio, uint16_t *ma, size_t ma_len)
|
|
|
|
{
|
2020-05-15 12:18:21 +00:00
|
|
|
/* Reserve some room for CMD SETFH <HSN> <MAIO> */
|
|
|
|
char ma_buf[TRXC_BUF_SIZE - 24];
|
|
|
|
size_t ma_buf_len = sizeof(ma_buf) - 1;
|
|
|
|
uint16_t rx_freq, tx_freq;
|
2018-09-06 18:14:14 +00:00
|
|
|
char *ptr;
|
|
|
|
int i, rc;
|
|
|
|
|
2020-05-15 12:18:21 +00:00
|
|
|
/* Make sure that Mobile Allocation has at least one ARFCN */
|
|
|
|
if (!ma_len || ma == NULL) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Mobile Allocation is empty?!?\n");
|
2018-09-06 18:14:14 +00:00
|
|
|
return -EINVAL;
|
2020-05-15 12:18:21 +00:00
|
|
|
}
|
2018-09-06 18:14:14 +00:00
|
|
|
|
2020-05-15 12:18:21 +00:00
|
|
|
/* Compose a sequence of Rx/Tx frequencies (mobile allocation) */
|
2018-09-06 18:14:14 +00:00
|
|
|
for (i = 0, ptr = ma_buf; i < ma_len; i++) {
|
2020-05-15 12:18:21 +00:00
|
|
|
/* Convert ARFCN to a pair of Rx/Tx frequencies (Hz * 10) */
|
|
|
|
rx_freq = gsm_arfcn2freq10(ma[i], 0); /* Rx: Downlink */
|
|
|
|
tx_freq = gsm_arfcn2freq10(ma[i], 1); /* Tx: Uplink */
|
|
|
|
if (rx_freq == 0xffff || tx_freq == 0xffff) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Failed to convert ARFCN %u "
|
2020-05-15 12:18:21 +00:00
|
|
|
"to a pair of Rx/Tx frequencies\n",
|
|
|
|
ma[i] & ~ARFCN_FLAG_MASK);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append a pair of Rx/Tx frequencies (in kHz) to the buffer */
|
|
|
|
rc = snprintf(ptr, ma_buf_len, "%u %u ", rx_freq * 100, tx_freq * 100);
|
|
|
|
if (rc < 0 || rc > ma_buf_len) { /* Prevent buffer overflow */
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Not enough room to encode "
|
2020-05-15 12:18:21 +00:00
|
|
|
"Mobile Allocation (N=%zu)\n", ma_len);
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2018-09-06 18:14:14 +00:00
|
|
|
|
|
|
|
/* Move pointer */
|
2020-05-15 12:18:21 +00:00
|
|
|
ma_buf_len -= rc;
|
2018-09-06 18:14:14 +00:00
|
|
|
ptr += rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Overwrite the last space */
|
|
|
|
*(ptr - 1) = '\0';
|
|
|
|
|
|
|
|
return trx_ctrl_cmd(trx, 1, "SETFH", "%u %u %s", hsn, maio, ma_buf);
|
|
|
|
}
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Get response from CTRL socket */
|
|
|
|
static int trx_ctrl_read_cb(struct osmo_fd *ofd, unsigned int what)
|
|
|
|
{
|
|
|
|
struct trx_instance *trx = ofd->data;
|
2022-07-12 20:30:55 +00:00
|
|
|
struct trxcon_inst *trxcon = trx->trxcon;
|
2016-07-23 19:40:41 +00:00
|
|
|
struct trx_ctrl_msg *tcm;
|
2019-05-08 21:18:19 +00:00
|
|
|
int resp, rsp_len;
|
2020-05-15 10:29:04 +00:00
|
|
|
char buf[TRXC_BUF_SIZE], *p;
|
2019-05-08 21:18:19 +00:00
|
|
|
ssize_t read_len;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2019-05-08 21:18:19 +00:00
|
|
|
read_len = read(ofd->fd, buf, sizeof(buf) - 1);
|
|
|
|
if (read_len <= 0) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "read() failed with rc=%zd\n", read_len);
|
2019-05-08 21:18:19 +00:00
|
|
|
return read_len;
|
2019-05-08 21:14:45 +00:00
|
|
|
}
|
2019-05-08 21:18:19 +00:00
|
|
|
buf[read_len] = '\0';
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
if (!!strncmp(buf, "RSP ", 4)) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_NOTICE, "Unknown message on CTRL port: %s\n", buf);
|
2016-07-23 19:40:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the length of response item */
|
|
|
|
p = strchr(buf + 4, ' ');
|
|
|
|
rsp_len = p ? p - buf - 4 : strlen(buf) - 4;
|
|
|
|
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_INFO, "Response message: '%s'\n", buf);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Abort expire timer */
|
2022-07-21 20:16:17 +00:00
|
|
|
osmo_timer_del(&trx->trx_ctrl_timer);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Get command for response message */
|
|
|
|
if (llist_empty(&trx->trx_ctrl_list)) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_NOTICE, "Response message without command\n");
|
2016-07-23 19:40:41 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcm = llist_entry(trx->trx_ctrl_list.next,
|
|
|
|
struct trx_ctrl_msg, list);
|
|
|
|
|
|
|
|
/* Check if response matches command */
|
2017-07-13 20:19:13 +00:00
|
|
|
if (!!strncmp(buf + 4, tcm->cmd + 4, rsp_len)) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, (tcm->critical) ? LOGL_FATAL : LOGL_ERROR,
|
2016-07-23 19:40:41 +00:00
|
|
|
"Response message '%s' does not match command "
|
|
|
|
"message '%s'\n", buf, tcm->cmd);
|
|
|
|
goto rsp_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for response code */
|
|
|
|
sscanf(p + 1, "%d", &resp);
|
|
|
|
if (resp) {
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, (tcm->critical) ? LOGL_FATAL : LOGL_ERROR,
|
2016-07-23 19:40:41 +00:00
|
|
|
"Transceiver rejected TRX command with "
|
|
|
|
"response: '%s'\n", buf);
|
|
|
|
|
|
|
|
if (tcm->critical)
|
|
|
|
goto rsp_error;
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Trigger state machine */
|
2019-05-30 13:21:45 +00:00
|
|
|
if (!strncmp(tcm->cmd + 4, "POWERON", 7)) {
|
|
|
|
trx->powered_up = true;
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_ACTIVE, 0, 0);
|
2019-05-30 13:21:45 +00:00
|
|
|
}
|
|
|
|
else if (!strncmp(tcm->cmd + 4, "POWEROFF", 8)) {
|
|
|
|
trx->powered_up = false;
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_IDLE, 0, 0);
|
2019-05-30 13:21:45 +00:00
|
|
|
}
|
2017-07-13 21:10:41 +00:00
|
|
|
else if (!strncmp(tcm->cmd + 4, "MEASURE", 7))
|
|
|
|
trx_if_measure_rsp_cb(trx, buf + 14);
|
2017-07-27 03:21:11 +00:00
|
|
|
else if (!strncmp(tcm->cmd + 4, "ECHO", 4))
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_IDLE, 0, 0);
|
2017-07-27 03:21:11 +00:00
|
|
|
else
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, trx->prev_state, 0, 0);
|
2017-05-31 02:28:40 +00:00
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Remove command from list */
|
|
|
|
llist_del(&tcm->list);
|
|
|
|
talloc_free(tcm);
|
|
|
|
|
|
|
|
/* Send next message, if any */
|
|
|
|
trx_ctrl_send(trx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rsp_error:
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Notify higher layers about the problem */
|
2022-07-12 20:30:55 +00:00
|
|
|
osmo_fsm_inst_dispatch(trxcon->fi, TRX_EVENT_RSP_ERROR, trx);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* Data interface handlers */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* DATA interface */
|
|
|
|
/* */
|
|
|
|
/* Messages on the data interface carry one radio burst per UDP message. */
|
|
|
|
/* */
|
|
|
|
/* Received Data Burst: */
|
|
|
|
/* 1 byte timeslot index */
|
|
|
|
/* 4 bytes GSM frame number, BE */
|
|
|
|
/* 1 byte RSSI in -dBm */
|
|
|
|
/* 2 bytes correlator timing offset in 1/256 symbol steps, 2's-comp, BE */
|
|
|
|
/* 148 bytes soft symbol estimates, 0 -> definite "0", 255 -> definite "1" */
|
2017-07-12 10:40:22 +00:00
|
|
|
/* 2 bytes are not used, but being sent by OsmoTRX */
|
2016-07-23 19:40:41 +00:00
|
|
|
/* */
|
|
|
|
/* Transmit Data Burst: */
|
|
|
|
/* 1 byte timeslot index */
|
|
|
|
/* 4 bytes GSM frame number, BE */
|
|
|
|
/* 1 byte transmit level wrt ARFCN max, -dB (attenuation) */
|
|
|
|
/* 148 bytes output symbol values, 0 & 1 */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2017-07-12 10:48:01 +00:00
|
|
|
static int trx_data_rx_cb(struct osmo_fd *ofd, unsigned int what)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
|
|
|
struct trx_instance *trx = ofd->data;
|
2022-07-12 20:30:55 +00:00
|
|
|
struct trxcon_inst *trxcon = trx->trxcon;
|
2022-07-01 18:42:26 +00:00
|
|
|
struct l1sched_meas_set meas;
|
2020-05-15 10:29:04 +00:00
|
|
|
uint8_t buf[TRXD_BUF_SIZE];
|
2016-07-23 19:40:41 +00:00
|
|
|
sbit_t bits[148];
|
|
|
|
int8_t rssi, tn;
|
2018-03-02 14:24:57 +00:00
|
|
|
int16_t toa256;
|
2016-07-23 19:40:41 +00:00
|
|
|
uint32_t fn;
|
2019-05-08 21:18:19 +00:00
|
|
|
ssize_t read_len;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2019-05-08 21:18:19 +00:00
|
|
|
read_len = read(ofd->fd, buf, sizeof(buf));
|
|
|
|
if (read_len <= 0) {
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_ERROR, "read() failed with rc=%zd\n", read_len);
|
2019-05-08 21:18:19 +00:00
|
|
|
return read_len;
|
2019-05-08 21:14:45 +00:00
|
|
|
}
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2021-04-29 12:32:58 +00:00
|
|
|
if (read_len < (8 + 148)) { /* TRXDv0 header + GMSK burst */
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_ERROR,
|
|
|
|
"Got data message with invalid length '%zd'\n", read_len);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tn = buf[0];
|
2020-06-14 08:27:23 +00:00
|
|
|
fn = osmo_load32be(buf + 1);
|
2016-07-23 19:40:41 +00:00
|
|
|
rssi = -(int8_t) buf[5];
|
2018-03-02 14:24:57 +00:00
|
|
|
toa256 = ((int16_t) (buf[6] << 8) | buf[7]);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Copy and convert bits {254..0} to sbits {-127..127} */
|
2017-12-03 17:18:54 +00:00
|
|
|
osmo_ubit2sbit(bits, buf + 8, 148);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
if (tn >= 8) {
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_ERROR, "Illegal TS %d\n", tn);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fn >= 2715648) {
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_ERROR, "Illegal FN %u\n", fn);
|
2016-07-23 19:40:41 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_DEBUG,
|
|
|
|
"RX burst tn=%u fn=%u rssi=%d toa=%d\n",
|
|
|
|
tn, fn, rssi, toa256);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2020-03-01 19:55:01 +00:00
|
|
|
/* Group the measurements together */
|
2022-07-01 18:42:26 +00:00
|
|
|
meas = (struct l1sched_meas_set) {
|
2020-03-01 19:55:01 +00:00
|
|
|
.toa256 = toa256,
|
|
|
|
.rssi = rssi,
|
2020-03-02 12:07:35 +00:00
|
|
|
.fn = fn,
|
2020-03-01 19:55:01 +00:00
|
|
|
};
|
|
|
|
|
2017-07-04 14:12:25 +00:00
|
|
|
/* Poke scheduler */
|
2022-07-12 20:30:55 +00:00
|
|
|
l1sched_handle_rx_burst(trxcon->sched, tn, fn, bits, 148, &meas);
|
2017-07-04 14:12:25 +00:00
|
|
|
|
2017-07-26 13:51:44 +00:00
|
|
|
/* Correct local clock counter */
|
|
|
|
if (fn % 51 == 0)
|
2022-07-12 20:30:55 +00:00
|
|
|
l1sched_clck_handle(trxcon->sched, fn);
|
2017-07-26 13:51:44 +00:00
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:14:34 +00:00
|
|
|
int trx_if_tx_burst(struct trx_instance *trx,
|
2022-07-01 18:42:26 +00:00
|
|
|
const struct l1sched_burst_req *br)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
2020-05-15 10:29:04 +00:00
|
|
|
uint8_t buf[TRXD_BUF_SIZE];
|
2021-06-14 21:14:34 +00:00
|
|
|
size_t length;
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/**
|
|
|
|
* We must be sure that we have clock,
|
|
|
|
* and we have sent all control data
|
|
|
|
*
|
2019-05-30 13:24:42 +00:00
|
|
|
* TODO: introduce proper state machines for both
|
|
|
|
* transceiver and its TRXC interface.
|
2017-05-31 02:28:40 +00:00
|
|
|
*/
|
2020-10-16 16:07:18 +00:00
|
|
|
#if 0
|
2022-07-19 23:41:55 +00:00
|
|
|
if (trx->fi->state != TRX_STATE_ACTIVE) {
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_ERROR,
|
|
|
|
"Ignoring TX data, transceiver isn't ready\n");
|
2017-05-31 02:28:40 +00:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2020-10-16 16:07:18 +00:00
|
|
|
#endif
|
2017-05-31 02:28:40 +00:00
|
|
|
|
2022-07-23 18:31:17 +00:00
|
|
|
LOGPFSMSL(trx->fi, DTRXD, LOGL_DEBUG,
|
|
|
|
"TX burst tn=%u fn=%u pwr=%u\n",
|
|
|
|
br->tn, br->fn, br->pwr);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2021-06-14 21:14:34 +00:00
|
|
|
buf[0] = br->tn;
|
|
|
|
osmo_store32be(br->fn, buf + 1);
|
|
|
|
buf[5] = br->pwr;
|
|
|
|
length = 6;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Copy ubits {0,1} */
|
2021-06-14 21:30:52 +00:00
|
|
|
if (br->burst_len != 0) {
|
|
|
|
memcpy(buf + 6, br->burst, br->burst_len);
|
|
|
|
length += br->burst_len;
|
|
|
|
}
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Send data to transceiver */
|
2021-06-14 21:14:34 +00:00
|
|
|
send(trx->trx_ofd_data.fd, buf, length, 0);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-17 04:03:48 +00:00
|
|
|
/* Init TRX interface (TRXC, TRXD sockets and FSM) */
|
2022-07-12 20:30:55 +00:00
|
|
|
struct trx_instance *trx_if_open(struct trxcon_inst *trxcon,
|
2019-01-17 04:03:48 +00:00
|
|
|
const char *local_host, const char *remote_host,
|
|
|
|
uint16_t base_port)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
2019-01-17 04:03:48 +00:00
|
|
|
struct trx_instance *trx;
|
2016-07-23 19:40:41 +00:00
|
|
|
int rc;
|
|
|
|
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trxcon->fi, LOGL_NOTICE, "Init transceiver interface "
|
2019-01-17 04:03:48 +00:00
|
|
|
"(%s:%u)\n", remote_host, base_port);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Try to allocate memory */
|
2022-07-12 20:30:55 +00:00
|
|
|
trx = talloc_zero(trxcon, struct trx_instance);
|
2019-01-17 04:03:48 +00:00
|
|
|
if (!trx) {
|
2022-07-22 10:03:46 +00:00
|
|
|
LOGPFSML(trxcon->fi, LOGL_ERROR, "Failed to allocate memory\n");
|
2019-01-17 04:03:48 +00:00
|
|
|
return NULL;
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 03:53:10 +00:00
|
|
|
/* Allocate a new dedicated state machine */
|
2022-07-19 23:51:15 +00:00
|
|
|
trx->fi = osmo_fsm_inst_alloc_child(&trx_fsm, trxcon->fi, TRX_EVENT_OFFLINE);
|
2022-07-19 23:41:55 +00:00
|
|
|
if (trx->fi == NULL) {
|
2022-07-22 10:03:46 +00:00
|
|
|
LOGPFSML(trxcon->fi, LOGL_ERROR, "Failed to allocate an instance "
|
2019-01-17 03:53:10 +00:00
|
|
|
"of FSM '%s'\n", trx_fsm.name);
|
2019-01-17 04:03:48 +00:00
|
|
|
talloc_free(trx);
|
|
|
|
return NULL;
|
2019-01-17 03:53:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Initialize CTRL queue */
|
2019-01-17 04:03:48 +00:00
|
|
|
INIT_LLIST_HEAD(&trx->trx_ctrl_list);
|
2016-07-23 19:40:41 +00:00
|
|
|
|
|
|
|
/* Open sockets */
|
2019-01-17 04:03:48 +00:00
|
|
|
rc = trx_udp_open(trx, &trx->trx_ofd_ctrl, local_host,
|
|
|
|
base_port + 101, remote_host, base_port + 1, trx_ctrl_read_cb);
|
2016-07-23 19:40:41 +00:00
|
|
|
if (rc < 0)
|
2019-01-17 04:03:48 +00:00
|
|
|
goto udp_error;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2019-01-17 04:03:48 +00:00
|
|
|
rc = trx_udp_open(trx, &trx->trx_ofd_data, local_host,
|
|
|
|
base_port + 102, remote_host, base_port + 2, trx_data_rx_cb);
|
2016-07-23 19:40:41 +00:00
|
|
|
if (rc < 0)
|
2019-01-17 04:03:48 +00:00
|
|
|
goto udp_error;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2022-07-12 20:30:55 +00:00
|
|
|
trx->trxcon = trxcon;
|
|
|
|
|
2019-01-17 04:03:48 +00:00
|
|
|
return trx;
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2019-01-17 04:03:48 +00:00
|
|
|
udp_error:
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_ERROR, "Couldn't establish UDP connection\n");
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_free(trx->fi);
|
2019-01-17 04:03:48 +00:00
|
|
|
talloc_free(trx);
|
|
|
|
return NULL;
|
2016-07-23 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush pending control messages */
|
2017-05-31 02:28:40 +00:00
|
|
|
void trx_if_flush_ctrl(struct trx_instance *trx)
|
2016-07-23 19:40:41 +00:00
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
|
2017-05-31 02:28:40 +00:00
|
|
|
/* Reset state machine */
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_state_chg(trx->fi, TRX_STATE_IDLE, 0, 0);
|
2017-05-31 02:28:40 +00:00
|
|
|
|
|
|
|
/* Clear command queue */
|
2016-07-23 19:40:41 +00:00
|
|
|
while (!llist_empty(&trx->trx_ctrl_list)) {
|
|
|
|
tcm = llist_entry(trx->trx_ctrl_list.next,
|
|
|
|
struct trx_ctrl_msg, list);
|
|
|
|
llist_del(&tcm->list);
|
|
|
|
talloc_free(tcm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void trx_if_close(struct trx_instance *trx)
|
|
|
|
{
|
2017-06-08 08:46:44 +00:00
|
|
|
/* May be unallocated due to init error */
|
|
|
|
if (!trx)
|
|
|
|
return;
|
|
|
|
|
2022-07-20 00:33:48 +00:00
|
|
|
LOGPFSML(trx->fi, LOGL_NOTICE, "Shutdown transceiver interface\n");
|
2016-07-23 19:40:41 +00:00
|
|
|
|
2022-07-20 00:14:43 +00:00
|
|
|
/* Abort TRXC response timer (if pending) */
|
|
|
|
osmo_timer_del(&trx->trx_ctrl_timer);
|
|
|
|
|
2016-07-23 19:40:41 +00:00
|
|
|
/* Flush CTRL message list */
|
|
|
|
trx_if_flush_ctrl(trx);
|
|
|
|
|
|
|
|
/* Close sockets */
|
|
|
|
trx_udp_close(&trx->trx_ofd_ctrl);
|
|
|
|
trx_udp_close(&trx->trx_ofd_data);
|
|
|
|
|
|
|
|
/* Free memory */
|
2022-07-19 23:41:55 +00:00
|
|
|
osmo_fsm_inst_free(trx->fi);
|
2016-07-23 19:40:41 +00:00
|
|
|
talloc_free(trx);
|
|
|
|
}
|
2019-01-17 03:45:56 +00:00
|
|
|
|
|
|
|
static __attribute__((constructor)) void on_dso_load(void)
|
|
|
|
{
|
|
|
|
OSMO_ASSERT(osmo_fsm_register(&trx_fsm) == 0);
|
|
|
|
}
|