2013-02-05 10:45:28 +00:00
|
|
|
/*
|
2017-06-24 15:20:30 +00:00
|
|
|
* OpenBTS-style TRX interface/protocol handling
|
|
|
|
*
|
|
|
|
* This file contains the BTS-side implementation of the OpenBTS-style
|
|
|
|
* UDP TRX protocol. It manages the clock, control + burst-data UDP
|
|
|
|
* sockets and their respective protocol encoding/parsing.
|
2013-02-05 10:45:28 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Andreas Eversberg <jolly@eversberg.eu>
|
2016-01-09 12:13:37 +00:00
|
|
|
* Copyright (C) 2016 Harald Welte <laforge@gnumonks.org>
|
2013-02-05 10:45:28 +00:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/socket.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/bits.h>
|
|
|
|
|
2016-01-09 12:13:37 +00:00
|
|
|
#include <osmo-bts/phy_link.h>
|
2013-02-05 10:45:28 +00:00
|
|
|
#include <osmo-bts/logging.h>
|
|
|
|
#include <osmo-bts/bts.h>
|
2016-01-09 22:28:57 +00:00
|
|
|
#include <osmo-bts/scheduler.h>
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
#include "l1_if.h"
|
|
|
|
#include "trx_if.h"
|
|
|
|
|
2013-02-24 10:09:19 +00:00
|
|
|
/* enable to print RSSI level graph */
|
|
|
|
//#define TOA_RSSI_DEBUG
|
|
|
|
|
2013-04-09 08:55:37 +00:00
|
|
|
int transceiver_available = 0;
|
2013-04-07 10:49:52 +00:00
|
|
|
int settsc_enabled = 0;
|
|
|
|
int setbsic_enabled = 0;
|
2013-02-05 10:45:28 +00:00
|
|
|
|
2016-07-01 07:10:31 +00:00
|
|
|
#define TRX_MAX_BURST_LEN 512
|
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
/*
|
2017-06-24 15:20:30 +00:00
|
|
|
* socket helper functions
|
2013-02-05 10:45:28 +00:00
|
|
|
*/
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! convenience wrapper to open socket + fill in osmo_fd */
|
2017-05-26 10:45:33 +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-01-09 12:13:37 +00:00
|
|
|
int (*cb)(struct osmo_fd *fd, unsigned int what))
|
2013-02-05 10:45:28 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
ofd->fd = -1;
|
|
|
|
ofd->cb = cb;
|
|
|
|
ofd->data = priv;
|
|
|
|
|
2017-05-26 10:45:33 +00:00
|
|
|
/* Listen / Binds + Connect */
|
|
|
|
rc = osmo_sock_init2_ofd(ofd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host_local, port_local,
|
|
|
|
host_remote, port_remote, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
|
2013-02-05 10:45:28 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/* close socket + unregister osmo_fd */
|
2013-02-05 10:45:28 +00:00
|
|
|
static void trx_udp_close(struct osmo_fd *ofd)
|
|
|
|
{
|
|
|
|
if (ofd->fd > 0) {
|
|
|
|
osmo_fd_unregister(ofd);
|
|
|
|
close(ofd->fd);
|
|
|
|
ofd->fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-06-24 15:20:30 +00:00
|
|
|
* TRX clock socket
|
2013-02-05 10:45:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* get clock from clock socket */
|
|
|
|
static int trx_clk_read_cb(struct osmo_fd *ofd, unsigned int what)
|
|
|
|
{
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_link *plink = ofd->data;
|
|
|
|
struct phy_instance *pinst = phy_instance_by_num(plink, 0);
|
2013-02-05 10:45:28 +00:00
|
|
|
char buf[1500];
|
|
|
|
int len;
|
2014-04-06 12:39:23 +00:00
|
|
|
uint32_t fn;
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
len = recv(ofd->fd, buf, sizeof(buf) - 1, 0);
|
|
|
|
if (len <= 0)
|
|
|
|
return len;
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2014-04-06 12:39:23 +00:00
|
|
|
if (!!strncmp(buf, "IND CLOCK ", 10)) {
|
2013-02-05 10:45:28 +00:00
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Unknown message on clock port: %s\n",
|
|
|
|
buf);
|
2014-04-06 12:39:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sscanf(buf, "IND CLOCK %u", &fn);
|
|
|
|
LOGP(DTRX, LOGL_INFO, "Clock indication: fn=%u\n", fn);
|
|
|
|
|
2016-01-09 17:19:32 +00:00
|
|
|
if (fn >= GSM_HYPERFRAME) {
|
|
|
|
fn %= GSM_HYPERFRAME;
|
2014-04-06 12:39:23 +00:00
|
|
|
LOGP(DTRX, LOGL_ERROR, "Indicated clock's FN is not wrapping "
|
|
|
|
"correctly, correcting to fn=%u\n", fn);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/* inform core TRX clock handling code that a FN has been received */
|
2016-01-09 12:13:37 +00:00
|
|
|
trx_sched_clock(pinst->trx->bts, fn);
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-06-24 15:20:30 +00:00
|
|
|
* TRX ctrl socket
|
2013-02-05 10:45:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void trx_ctrl_timer_cb(void *data);
|
|
|
|
|
|
|
|
/* send first ctrl message and start timer */
|
|
|
|
static void trx_ctrl_send(struct trx_l1h *l1h)
|
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
|
|
|
|
/* get first command */
|
|
|
|
if (llist_empty(&l1h->trx_ctrl_list))
|
|
|
|
return;
|
|
|
|
tcm = llist_entry(l1h->trx_ctrl_list.next, struct trx_ctrl_msg, list);
|
|
|
|
|
2016-01-09 12:13:37 +00:00
|
|
|
LOGP(DTRX, LOGL_DEBUG, "Sending control '%s' to %s\n", tcm->cmd,
|
|
|
|
phy_instance_name(l1h->phy_inst));
|
2013-02-05 10:45:28 +00:00
|
|
|
/* send command */
|
|
|
|
send(l1h->trx_ofd_ctrl.fd, tcm->cmd, strlen(tcm->cmd)+1, 0);
|
|
|
|
|
|
|
|
/* start timer */
|
|
|
|
l1h->trx_ctrl_timer.cb = trx_ctrl_timer_cb;
|
|
|
|
l1h->trx_ctrl_timer.data = l1h;
|
|
|
|
osmo_timer_schedule(&l1h->trx_ctrl_timer, 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send first ctrl message and start timer */
|
|
|
|
static void trx_ctrl_timer_cb(void *data)
|
|
|
|
{
|
|
|
|
struct trx_l1h *l1h = data;
|
|
|
|
|
2016-01-09 12:13:37 +00:00
|
|
|
LOGP(DTRX, LOGL_NOTICE, "No response from transceiver for %s\n",
|
|
|
|
phy_instance_name(l1h->phy_inst));
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
trx_ctrl_send(l1h);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send a new TRX control command.
|
|
|
|
* \param[inout] l1h TRX Layer1 handle to which to send command
|
|
|
|
* \param[in] criticial
|
|
|
|
* \param[in] cmd zero-terminated string containing command
|
|
|
|
* \param[in] fmt Format string (+ variable list of arguments)
|
|
|
|
* \returns 0 on success; negative on error
|
|
|
|
*
|
|
|
|
* The new ocommand will be added to the end of the control command
|
|
|
|
* queue.
|
|
|
|
*/
|
2013-02-05 12:46:03 +00:00
|
|
|
static int trx_ctrl_cmd(struct trx_l1h *l1h, int critical, const char *cmd,
|
|
|
|
const char *fmt, ...)
|
2013-02-05 10:45:28 +00:00
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
va_list ap;
|
2013-02-05 12:46:03 +00:00
|
|
|
int l, pending = 0;
|
|
|
|
|
2016-06-15 15:55:21 +00:00
|
|
|
if (!transceiver_available &&
|
|
|
|
!(!strcmp(cmd, "POWEROFF") || !strcmp(cmd, "POWERON"))) {
|
|
|
|
LOGP(DTRX, LOGL_ERROR, "CTRL %s ignored: No clock from "
|
|
|
|
"transceiver, please fix!\n", cmd);
|
2013-02-07 13:09:06 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2013-02-05 12:46:03 +00:00
|
|
|
if (!llist_empty(&l1h->trx_ctrl_list))
|
|
|
|
pending = 1;
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
/* create message */
|
|
|
|
tcm = talloc_zero(tall_bts_ctx, struct trx_ctrl_msg);
|
|
|
|
if (!tcm)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (fmt && fmt[0]) {
|
|
|
|
l = snprintf(tcm->cmd, sizeof(tcm->cmd)-1, "CMD %s ", cmd);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(tcm->cmd + l, sizeof(tcm->cmd) - l - 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
} else
|
|
|
|
snprintf(tcm->cmd, sizeof(tcm->cmd)-1, "CMD %s", cmd);
|
|
|
|
tcm->cmd_len = strlen(cmd);
|
2013-02-05 12:46:03 +00:00
|
|
|
tcm->critical = critical;
|
2013-02-05 10:45:28 +00:00
|
|
|
llist_add_tail(&tcm->list, &l1h->trx_ctrl_list);
|
2017-06-24 15:20:30 +00:00
|
|
|
LOGP(DTRX, LOGL_INFO, "Enqueuing TRX control command '%s'\n", tcm->cmd);
|
2013-02-05 10:45:28 +00:00
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/* send message, if we didn't already have pending messages */
|
2013-02-05 12:46:03 +00:00
|
|
|
if (!pending)
|
2013-02-05 10:45:28 +00:00
|
|
|
trx_ctrl_send(l1h);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "POWEROFF" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_poweroff(struct trx_l1h *l1h)
|
|
|
|
{
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
|
|
|
if (pinst->num == 0)
|
2015-06-15 04:33:37 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 1, "POWEROFF", "");
|
|
|
|
else
|
|
|
|
return 0;
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "POWERON" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_poweron(struct trx_l1h *l1h)
|
|
|
|
{
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
|
|
|
if (pinst->num == 0)
|
2015-06-15 04:33:37 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 1, "POWERON", "");
|
|
|
|
else
|
|
|
|
return 0;
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETTSC" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_settsc(struct trx_l1h *l1h, uint8_t tsc)
|
|
|
|
{
|
2013-04-07 10:49:52 +00:00
|
|
|
if (!settsc_enabled)
|
|
|
|
return 0;
|
|
|
|
/* if TSC is enabled only, the positive response is mandatory */
|
|
|
|
return trx_ctrl_cmd(l1h, (setbsic_enabled) ? 0 : 1, "SETTSC", "%d",
|
|
|
|
tsc);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETBSIC" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_setbsic(struct trx_l1h *l1h, uint8_t bsic)
|
|
|
|
{
|
2013-04-07 10:49:52 +00:00
|
|
|
if (!setbsic_enabled)
|
|
|
|
return 0;
|
|
|
|
/* if BSIC is enabled only, the positive response is mandatory */
|
|
|
|
return trx_ctrl_cmd(l1h, (settsc_enabled) ? 0 : 1, "SETBSIC", "%d",
|
|
|
|
bsic);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETRXGAIN" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_setrxgain(struct trx_l1h *l1h, int db)
|
|
|
|
{
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 0, "SETRXGAIN", "%d", db);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETPOWER" command to TRX */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_setpower(struct trx_l1h *l1h, int db)
|
|
|
|
{
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 0, "SETPOWER", "%d", db);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETMAXDLY" command to TRX, i.e. maximum delay for RACH bursts */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_setmaxdly(struct trx_l1h *l1h, int dly)
|
|
|
|
{
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 0, "SETMAXDLY", "%d", dly);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETMAXDLYNB" command to TRX, i.e. maximum delay for normal bursts */
|
2016-11-04 19:30:35 +00:00
|
|
|
int trx_if_cmd_setmaxdlynb(struct trx_l1h *l1h, int dly)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(l1h, 0, "SETMAXDLYNB", "%d", dly);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "SETSLOT" command to TRX: Configure Channel Combination for TS */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_setslot(struct trx_l1h *l1h, uint8_t tn, uint8_t type)
|
|
|
|
{
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 1, "SETSLOT", "%d %d", tn, type);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "RXTUNE" command to TRX: Tune Receiver to given ARFCN */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_rxtune(struct trx_l1h *l1h, uint16_t arfcn)
|
|
|
|
{
|
2016-08-04 13:59:43 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
2013-02-05 10:45:28 +00:00
|
|
|
uint16_t freq10;
|
|
|
|
|
2016-08-04 13:59:43 +00:00
|
|
|
if (pinst->trx->bts->band == GSM_BAND_1900)
|
|
|
|
arfcn |= ARFCN_PCS;
|
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
freq10 = gsm_arfcn2freq10(arfcn, 1); /* RX = uplink */
|
|
|
|
if (freq10 == 0xffff) {
|
2016-08-04 13:59:43 +00:00
|
|
|
LOGP(DTRX, LOGL_ERROR, "Arfcn %d not defined.\n",
|
|
|
|
arfcn & ~ARFCN_FLAG_MASK);
|
2013-02-05 10:45:28 +00:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 1, "RXTUNE", "%d", freq10 * 100);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "TXTUNE" command to TRX: Tune Transmitter to given ARFCN */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_cmd_txtune(struct trx_l1h *l1h, uint16_t arfcn)
|
|
|
|
{
|
2016-08-04 13:59:43 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
2013-02-05 10:45:28 +00:00
|
|
|
uint16_t freq10;
|
|
|
|
|
2016-08-04 13:59:43 +00:00
|
|
|
if (pinst->trx->bts->band == GSM_BAND_1900)
|
|
|
|
arfcn |= ARFCN_PCS;
|
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
freq10 = gsm_arfcn2freq10(arfcn, 0); /* TX = downlink */
|
|
|
|
if (freq10 == 0xffff) {
|
2016-08-04 13:59:43 +00:00
|
|
|
LOGP(DTRX, LOGL_ERROR, "Arfcn %d not defined.\n",
|
|
|
|
arfcn & ~ARFCN_FLAG_MASK);
|
2013-02-05 10:45:28 +00:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2013-02-05 12:46:03 +00:00
|
|
|
return trx_ctrl_cmd(l1h, 1, "TXTUNE", "%d", freq10 * 100);
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "HANDOVER" command to TRX: Enable handover RACH Detection on timeslot/sub-slot */
|
2013-06-12 07:12:04 +00:00
|
|
|
int trx_if_cmd_handover(struct trx_l1h *l1h, uint8_t tn, uint8_t ss)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(l1h, 1, "HANDOVER", "%d %d", tn, ss);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send "NOHANDOVER" command to TRX: Disable handover RACH Detection on timeslot/sub-slot */
|
2013-06-12 07:12:04 +00:00
|
|
|
int trx_if_cmd_nohandover(struct trx_l1h *l1h, uint8_t tn, uint8_t ss)
|
|
|
|
{
|
|
|
|
return trx_ctrl_cmd(l1h, 1, "NOHANDOVER", "%d %d", tn, ss);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Get + parse response from TRX ctrl socket */
|
2013-02-05 10:45:28 +00:00
|
|
|
static int trx_ctrl_read_cb(struct osmo_fd *ofd, unsigned int what)
|
|
|
|
{
|
|
|
|
struct trx_l1h *l1h = ofd->data;
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
2013-02-05 10:45:28 +00:00
|
|
|
char buf[1500];
|
|
|
|
int len, resp;
|
|
|
|
|
|
|
|
len = recv(ofd->fd, buf, sizeof(buf) - 1, 0);
|
|
|
|
if (len <= 0)
|
|
|
|
return len;
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
if (!strncmp(buf, "RSP ", 4)) {
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
char *p;
|
|
|
|
int rsp_len = 0;
|
|
|
|
|
|
|
|
/* calculate the length of response item */
|
|
|
|
p = strchr(buf + 4, ' ');
|
|
|
|
if (p)
|
|
|
|
rsp_len = p - buf - 4;
|
|
|
|
else
|
|
|
|
rsp_len = strlen(buf) - 4;
|
|
|
|
|
2013-02-21 14:41:41 +00:00
|
|
|
LOGP(DTRX, LOGL_INFO, "Response message: '%s'\n", buf);
|
2013-02-05 10:45:28 +00:00
|
|
|
|
2013-02-05 12:46:03 +00:00
|
|
|
/* abort timer and send next message, if any */
|
|
|
|
if (osmo_timer_pending(&l1h->trx_ctrl_timer))
|
|
|
|
osmo_timer_del(&l1h->trx_ctrl_timer);
|
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
/* get command for response message */
|
|
|
|
if (llist_empty(&l1h->trx_ctrl_list)) {
|
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Response message without "
|
|
|
|
"command\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
tcm = llist_entry(l1h->trx_ctrl_list.next, struct trx_ctrl_msg,
|
|
|
|
list);
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/* check if response matches command */
|
2013-02-05 10:45:28 +00:00
|
|
|
if (rsp_len != tcm->cmd_len) {
|
|
|
|
notmatch:
|
2013-02-21 14:41:41 +00:00
|
|
|
LOGP(DTRX, (tcm->critical) ? LOGL_FATAL : LOGL_NOTICE,
|
|
|
|
"Response message '%s' does not match command "
|
|
|
|
"message '%s'\n", buf, tcm->cmd);
|
|
|
|
goto rsp_error;
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
if (!!strncmp(buf + 4, tcm->cmd + 4, rsp_len))
|
|
|
|
goto notmatch;
|
|
|
|
|
|
|
|
/* check for response code */
|
2013-02-05 12:46:03 +00:00
|
|
|
sscanf(p + 1, "%d", &resp);
|
|
|
|
if (resp) {
|
|
|
|
LOGP(DTRX, (tcm->critical) ? LOGL_FATAL : LOGL_NOTICE,
|
2016-01-09 12:13:37 +00:00
|
|
|
"transceiver (%s) rejected TRX command "
|
|
|
|
"with response: '%s'\n",
|
|
|
|
phy_instance_name(pinst), buf);
|
2013-02-21 14:41:41 +00:00
|
|
|
rsp_error:
|
2013-02-05 12:46:03 +00:00
|
|
|
if (tcm->critical) {
|
2016-06-22 14:30:23 +00:00
|
|
|
bts_shutdown(pinst->trx->bts, "TRX-CTRL-MSG: CRITICAL");
|
2013-02-05 12:46:03 +00:00
|
|
|
/* keep tcm list, so process is stopped */
|
|
|
|
return -EIO;
|
2013-02-05 10:45:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove command from list */
|
|
|
|
llist_del(&tcm->list);
|
|
|
|
talloc_free(tcm);
|
|
|
|
|
|
|
|
trx_ctrl_send(l1h);
|
|
|
|
} else
|
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Unknown message on ctrl port: %s\n",
|
|
|
|
buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-06-24 15:20:30 +00:00
|
|
|
* TRX burst data socket
|
2013-02-05 10:45:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int trx_data_read_cb(struct osmo_fd *ofd, unsigned int what)
|
|
|
|
{
|
|
|
|
struct trx_l1h *l1h = ofd->data;
|
2016-07-01 07:10:31 +00:00
|
|
|
uint8_t buf[TRX_MAX_BURST_LEN];
|
2013-02-05 10:45:28 +00:00
|
|
|
int len;
|
|
|
|
uint8_t tn;
|
|
|
|
int8_t rssi;
|
2015-06-15 04:40:27 +00:00
|
|
|
float toa = 0.0;
|
2013-02-05 10:45:28 +00:00
|
|
|
uint32_t fn;
|
2016-07-01 07:10:31 +00:00
|
|
|
sbit_t bits[EGPRS_BURST_LEN];
|
|
|
|
int i, burst_len = GSM_BURST_LEN;
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
len = recv(ofd->fd, buf, sizeof(buf), 0);
|
2016-07-01 07:10:31 +00:00
|
|
|
if (len <= 0) {
|
2013-02-05 10:45:28 +00:00
|
|
|
return len;
|
2016-07-01 07:10:31 +00:00
|
|
|
} else if (len == EGPRS_BURST_LEN + 10) {
|
|
|
|
burst_len = EGPRS_BURST_LEN;
|
|
|
|
} else if (len != GSM_BURST_LEN + 10) {
|
2013-02-05 10:45:28 +00:00
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Got data message with invalid lenght "
|
|
|
|
"'%d'\n", len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
tn = buf[0];
|
|
|
|
fn = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
|
2013-02-24 10:09:19 +00:00
|
|
|
rssi = -(int8_t)buf[5];
|
2013-02-11 11:52:56 +00:00
|
|
|
toa = ((int16_t)(buf[6] << 8) | buf[7]) / 256.0F;
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
/* copy and convert bits {254..0} to sbits {-127..127} */
|
2016-07-01 07:10:31 +00:00
|
|
|
for (i = 0; i < burst_len; i++) {
|
2013-02-05 10:45:28 +00:00
|
|
|
if (buf[8 + i] == 255)
|
|
|
|
bits[i] = -127;
|
|
|
|
else
|
|
|
|
bits[i] = 127 - buf[8 + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tn >= 8) {
|
|
|
|
LOGP(DTRX, LOGL_ERROR, "Illegal TS %d\n", tn);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-01-09 17:19:32 +00:00
|
|
|
if (fn >= GSM_HYPERFRAME) {
|
2013-02-05 10:45:28 +00:00
|
|
|
LOGP(DTRX, LOGL_ERROR, "Illegal FN %u\n", fn);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-02-21 14:41:41 +00:00
|
|
|
LOGP(DTRX, LOGL_DEBUG, "RX burst tn=%u fn=%u rssi=%d toa=%.2f\n",
|
2013-02-05 10:45:28 +00:00
|
|
|
tn, fn, rssi, toa);
|
|
|
|
|
2013-02-24 10:09:19 +00:00
|
|
|
#ifdef TOA_RSSI_DEBUG
|
|
|
|
char deb[128];
|
|
|
|
|
|
|
|
sprintf(deb, "| 0 "
|
|
|
|
" | rssi=%4d toa=%4.2f fn=%u", rssi, toa, fn);
|
|
|
|
deb[1 + (128 + rssi) / 4] = '*';
|
|
|
|
fprintf(stderr, "%s\n", deb);
|
|
|
|
#endif
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/* feed received burst into scheduler code */
|
2016-07-01 07:10:31 +00:00
|
|
|
trx_sched_ul_burst(&l1h->l1s, tn, fn, bits, burst_len, rssi, toa);
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! Send burst data for given FN/timeslot to TRX
|
|
|
|
* \param[inout] l1h TRX Layer1 handle referring to TX
|
|
|
|
* \param[in] tn Timeslot Number (0..7)
|
|
|
|
* \param[in] fn GSM Frame Number
|
|
|
|
* \param[in] pwr Transmit Power to use
|
|
|
|
* \param[in] bits Unpacked bits to be transmitted
|
|
|
|
* \param[in] nbits Number of \a bits
|
|
|
|
* \returns 0 on success; negative on error */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_data(struct trx_l1h *l1h, uint8_t tn, uint32_t fn, uint8_t pwr,
|
2016-07-01 07:10:31 +00:00
|
|
|
const ubit_t *bits, uint16_t nbits)
|
2013-02-05 10:45:28 +00:00
|
|
|
{
|
2016-07-01 07:10:31 +00:00
|
|
|
uint8_t buf[TRX_MAX_BURST_LEN];
|
|
|
|
|
|
|
|
if ((nbits != GSM_BURST_LEN) && (nbits != EGPRS_BURST_LEN)) {
|
|
|
|
LOGP(DTRX, LOGL_ERROR, "Tx burst length %u invalid\n", nbits);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
LOGP(DTRX, LOGL_DEBUG, "TX burst tn=%u fn=%u pwr=%u\n", tn, fn, pwr);
|
|
|
|
|
|
|
|
buf[0] = tn;
|
|
|
|
buf[1] = (fn >> 24) & 0xff;
|
|
|
|
buf[2] = (fn >> 16) & 0xff;
|
|
|
|
buf[3] = (fn >> 8) & 0xff;
|
|
|
|
buf[4] = (fn >> 0) & 0xff;
|
|
|
|
buf[5] = pwr;
|
|
|
|
|
|
|
|
/* copy ubits {0,1} */
|
2016-07-01 07:10:31 +00:00
|
|
|
memcpy(buf + 6, bits, nbits);
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
/* we must be sure that we have clock, and we have sent all control
|
|
|
|
* data */
|
2013-04-09 08:55:37 +00:00
|
|
|
if (transceiver_available && llist_empty(&l1h->trx_ctrl_list)) {
|
2016-07-01 07:10:31 +00:00
|
|
|
send(l1h->trx_ofd_data.fd, buf, nbits + 6, 0);
|
2013-02-05 10:45:28 +00:00
|
|
|
} else
|
2013-04-09 08:55:37 +00:00
|
|
|
LOGP(DTRX, LOGL_DEBUG, "Ignoring TX data, transceiver "
|
2013-02-05 10:45:28 +00:00
|
|
|
"offline.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open/close
|
|
|
|
*/
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! open the PHY link using TRX protocol */
|
2016-01-09 12:13:37 +00:00
|
|
|
int bts_model_phy_link_open(struct phy_link *plink)
|
|
|
|
{
|
|
|
|
struct phy_instance *pinst;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
phy_link_state_set(plink, PHY_LINK_CONNECTING);
|
|
|
|
|
|
|
|
/* open the shared/common clock socket */
|
|
|
|
rc = trx_udp_open(plink, &plink->u.osmotrx.trx_ofd_clk,
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.local_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
plink->u.osmotrx.base_port_local,
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.remote_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
plink->u.osmotrx.base_port_remote,
|
|
|
|
trx_clk_read_cb);
|
|
|
|
if (rc < 0) {
|
|
|
|
phy_link_state_set(plink, PHY_LINK_SHUTDOWN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the individual instances with their ctrl+data sockets */
|
|
|
|
llist_for_each_entry(pinst, &plink->instances, list) {
|
|
|
|
pinst->u.osmotrx.hdl = l1if_open(pinst);
|
|
|
|
if (!pinst->u.osmotrx.hdl)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-06-16 10:36:20 +00:00
|
|
|
/* FIXME: is there better way to check/report TRX availability? */
|
|
|
|
transceiver_available = 1;
|
|
|
|
phy_link_state_set(plink, PHY_LINK_CONNECTED);
|
2016-01-09 12:13:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
phy_link_state_set(plink, PHY_LINK_SHUTDOWN);
|
|
|
|
llist_for_each_entry(pinst, &plink->instances, list) {
|
|
|
|
if (pinst->u.osmotrx.hdl) {
|
|
|
|
trx_if_close(pinst->u.osmotrx.hdl);
|
|
|
|
pinst->u.osmotrx.hdl = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trx_udp_close(&plink->u.osmotrx.trx_ofd_clk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! compute UDP port number used for TRX protocol */
|
2016-01-09 12:13:37 +00:00
|
|
|
static uint16_t compute_port(struct phy_instance *pinst, int remote, int is_data)
|
|
|
|
{
|
|
|
|
struct phy_link *plink = pinst->phy_link;
|
|
|
|
uint16_t inc = 1;
|
|
|
|
|
|
|
|
if (is_data)
|
|
|
|
inc = 2;
|
|
|
|
|
|
|
|
if (remote)
|
|
|
|
return plink->u.osmotrx.base_port_remote + (pinst->num << 1) + inc;
|
|
|
|
else
|
|
|
|
return plink->u.osmotrx.base_port_local + (pinst->num << 1) + inc;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! open a TRX interface. creates contro + data sockets */
|
2013-02-05 10:45:28 +00:00
|
|
|
int trx_if_open(struct trx_l1h *l1h)
|
|
|
|
{
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
|
|
|
struct phy_link *plink = pinst->phy_link;
|
2013-02-05 10:45:28 +00:00
|
|
|
int rc;
|
|
|
|
|
2016-01-09 12:13:37 +00:00
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Open transceiver for %s\n",
|
|
|
|
phy_instance_name(pinst));
|
2013-02-05 10:45:28 +00:00
|
|
|
|
2013-04-09 08:44:58 +00:00
|
|
|
/* initialize ctrl queue */
|
|
|
|
INIT_LLIST_HEAD(&l1h->trx_ctrl_list);
|
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
/* open sockets */
|
|
|
|
rc = trx_udp_open(l1h, &l1h->trx_ofd_ctrl,
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.local_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
compute_port(pinst, 0, 0),
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.remote_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
compute_port(pinst, 1, 0), trx_ctrl_read_cb);
|
2013-02-05 10:45:28 +00:00
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
rc = trx_udp_open(l1h, &l1h->trx_ofd_data,
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.local_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
compute_port(pinst, 0, 1),
|
2017-05-26 10:45:33 +00:00
|
|
|
plink->u.osmotrx.remote_ip,
|
2016-01-09 12:13:37 +00:00
|
|
|
compute_port(pinst, 1, 1), trx_data_read_cb);
|
2013-02-05 10:45:28 +00:00
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* enable all slots */
|
|
|
|
l1h->config.slotmask = 0xff;
|
|
|
|
|
2016-01-09 12:13:37 +00:00
|
|
|
/* FIXME: why was this only for TRX0 ? */
|
|
|
|
//if (l1h->trx->nr == 0)
|
|
|
|
trx_if_cmd_poweroff(l1h);
|
2013-02-22 06:52:51 +00:00
|
|
|
|
2013-02-05 10:45:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
trx_if_close(l1h);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! flush (delete) all pending control messages */
|
2013-02-05 10:45:28 +00:00
|
|
|
void trx_if_flush(struct trx_l1h *l1h)
|
|
|
|
{
|
|
|
|
struct trx_ctrl_msg *tcm;
|
|
|
|
|
|
|
|
/* free ctrl message list */
|
|
|
|
while (!llist_empty(&l1h->trx_ctrl_list)) {
|
|
|
|
tcm = llist_entry(l1h->trx_ctrl_list.next, struct trx_ctrl_msg,
|
|
|
|
list);
|
|
|
|
llist_del(&tcm->list);
|
|
|
|
talloc_free(tcm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! close the TRX for given handle (data + control socket) */
|
2013-02-05 10:45:28 +00:00
|
|
|
void trx_if_close(struct trx_l1h *l1h)
|
|
|
|
{
|
2016-01-09 12:13:37 +00:00
|
|
|
struct phy_instance *pinst = l1h->phy_inst;
|
|
|
|
LOGP(DTRX, LOGL_NOTICE, "Close transceiver for %s\n",
|
|
|
|
phy_instance_name(pinst));
|
2013-02-05 10:45:28 +00:00
|
|
|
|
|
|
|
trx_if_flush(l1h);
|
|
|
|
|
|
|
|
/* close sockets */
|
|
|
|
trx_udp_close(&l1h->trx_ofd_ctrl);
|
|
|
|
trx_udp_close(&l1h->trx_ofd_data);
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:20:30 +00:00
|
|
|
/*! determine if the TRX for given handle is powered up */
|
2016-01-09 22:03:34 +00:00
|
|
|
int trx_if_powered(struct trx_l1h *l1h)
|
|
|
|
{
|
|
|
|
return l1h->config.poweron;
|
|
|
|
}
|