blob: ffad6c082ceea99cb9d6a2dbfca7191c1e6d2472 [file] [log] [blame]
Eric Wildd40300e2022-05-07 15:36:47 +02001/*
2 * OsmocomBB <-> SDR connection bridge
3 * Transceiver interface handlers
4 *
5 * Copyright (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
6 * Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <stdio.h>
25#include <errno.h>
26#include <stdint.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <netinet/in.h>
32
33#include <sys/eventfd.h>
34
35#include <osmocom/core/logging.h>
36#include <osmocom/core/select.h>
37#include <osmocom/core/socket.h>
38#include <osmocom/core/talloc.h>
39#include <osmocom/core/bits.h>
40#include <osmocom/core/fsm.h>
41
42#include <osmocom/gsm/gsm_utils.h>
43
44#include "l1ctl.h"
45#include "trxcon.h"
46#include "trx_if.h"
47#include "logging.h"
48#include "scheduler.h"
49
Eric935c8cb2022-06-06 00:48:09 +020050#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +020051#include "../Transceiver52M/l1if.h"
Eric935c8cb2022-06-06 00:48:09 +020052#endif
Eric Wildd40300e2022-05-07 15:36:47 +020053
54static struct value_string trx_evt_names[] = {
55 { 0, NULL } /* no events? */
56};
57
58static struct osmo_fsm_state trx_fsm_states[] = {
59 [TRX_STATE_OFFLINE] = {
60 .out_state_mask = (
61 GEN_MASK(TRX_STATE_IDLE) |
62 GEN_MASK(TRX_STATE_RSP_WAIT)),
63 .name = "OFFLINE",
64 },
65 [TRX_STATE_IDLE] = {
66 .out_state_mask = UINT32_MAX,
67 .name = "IDLE",
68 },
69 [TRX_STATE_ACTIVE] = {
70 .out_state_mask = (
71 GEN_MASK(TRX_STATE_IDLE) |
72 GEN_MASK(TRX_STATE_RSP_WAIT)),
73 .name = "ACTIVE",
74 },
75 [TRX_STATE_RSP_WAIT] = {
76 .out_state_mask = (
77 GEN_MASK(TRX_STATE_IDLE) |
78 GEN_MASK(TRX_STATE_ACTIVE) |
79 GEN_MASK(TRX_STATE_OFFLINE)),
80 .name = "RSP_WAIT",
81 },
82};
83
84static struct osmo_fsm trx_fsm = {
85 .name = "trx_interface_fsm",
86 .states = trx_fsm_states,
87 .num_states = ARRAY_SIZE(trx_fsm_states),
88 .log_subsys = DTRX,
89 .event_names = trx_evt_names,
90};
91
92static int trx_udp_open(void *priv, struct osmo_fd *ofd, const char *host_local,
93 uint16_t port_local, const char *host_remote, uint16_t port_remote,
94 int (*cb)(struct osmo_fd *fd, unsigned int what))
95{
96 int rc;
97
98 ofd->data = priv;
99 ofd->fd = -1;
100 ofd->cb = cb;
101
102 /* Init UDP Connection */
103 rc = osmo_sock_init2_ofd(ofd, AF_UNSPEC, SOCK_DGRAM, 0, host_local, port_local,
104 host_remote, port_remote,
105 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
106 return rc;
107}
108
109static void trx_udp_close(struct osmo_fd *ofd)
110{
111 if (ofd->fd > 0) {
112 osmo_fd_unregister(ofd);
113 close(ofd->fd);
114 ofd->fd = -1;
115 }
116}
117
118/* ------------------------------------------------------------------------ */
119/* Control (CTRL) interface handlers */
120/* ------------------------------------------------------------------------ */
121/* Commands on the Per-ARFCN Control Interface */
122/* */
123/* The per-ARFCN control interface uses a command-response protocol. */
124/* Commands are NULL-terminated ASCII strings, one per UDP socket. */
125/* Each command has a corresponding response. */
126/* Every command is of the form: */
127/* */
128/* CMD <cmdtype> [params] */
129/* */
130/* The <cmdtype> is the actual command. */
131/* Parameters are optional depending on the commands type. */
132/* Every response is of the form: */
133/* */
134/* RSP <cmdtype> <status> [result] */
135/* */
136/* The <status> is 0 for success and a non-zero error code for failure. */
137/* Successful responses may include results, depending on the command type. */
138/* ------------------------------------------------------------------------ */
139
140static void trx_ctrl_timer_cb(void *data);
141
142/* Send first CTRL message and start timer */
143static void trx_ctrl_send(struct trx_instance *trx)
144{
145 struct trx_ctrl_msg *tcm;
146
147 if (llist_empty(&trx->trx_ctrl_list))
148 return;
149 tcm = llist_entry(trx->trx_ctrl_list.next, struct trx_ctrl_msg, list);
150
Eric935c8cb2022-06-06 00:48:09 +0200151#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200152 char* cmd = malloc(TRXC_BUF_SIZE);
153 memcpy(cmd, tcm->cmd, TRXC_BUF_SIZE);
154
155 /* Send command */
156 LOGP(DTRX, LOGL_DEBUG, "Sending control '%s'\n", tcm->cmd);
157 trxif_to_trx_c(cmd);
Eric935c8cb2022-06-06 00:48:09 +0200158
159#else
160 /* Send command */
161 LOGP(DTRX, LOGL_DEBUG, "Sending control '%s'\n", tcm->cmd);
162 send(trx->trx_ofd_ctrl.fd, tcm->cmd, strlen(tcm->cmd) + 1, 0);
163#endif
Eric Wildd40300e2022-05-07 15:36:47 +0200164
165 /* Trigger state machine */
166 if (trx->fsm->state != TRX_STATE_RSP_WAIT) {
167 trx->prev_state = trx->fsm->state;
168 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_RSP_WAIT, 0, 0);
169 }
170
171 /* Start expire timer */
172 trx->trx_ctrl_timer.data = trx;
173 trx->trx_ctrl_timer.cb = trx_ctrl_timer_cb;
174 osmo_timer_schedule(&trx->trx_ctrl_timer, 2, 0);
175}
176
177static void trx_ctrl_timer_cb(void *data)
178{
179 struct trx_instance *trx = (struct trx_instance *) data;
180 struct trx_ctrl_msg *tcm;
181
182 /* Queue may be cleaned at this moment */
183 if (llist_empty(&trx->trx_ctrl_list))
184 return;
185
186 LOGP(DTRX, LOGL_NOTICE, "No response from transceiver...\n");
187
188 tcm = llist_entry(trx->trx_ctrl_list.next, struct trx_ctrl_msg, list);
189 if (++tcm->retry_cnt > 3) {
190 LOGP(DTRX, LOGL_NOTICE, "Transceiver offline\n");
191 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_OFFLINE, 0, 0);
192 osmo_fsm_inst_dispatch(trxcon_fsm, TRX_EVENT_OFFLINE, trx);
193 return;
194 }
195
196 /* Attempt to send a command again */
197 trx_ctrl_send(trx);
198}
199
200/* Add a new CTRL command to the trx_ctrl_list */
201static int trx_ctrl_cmd(struct trx_instance *trx, int critical,
202 const char *cmd, const char *fmt, ...)
203{
204 struct trx_ctrl_msg *tcm;
205 int len, pending = 0;
206 va_list ap;
207
208 /* TODO: make sure that transceiver online */
209
210 if (!llist_empty(&trx->trx_ctrl_list))
211 pending = 1;
212
213 /* Allocate a message */
214 tcm = talloc_zero(trx, struct trx_ctrl_msg);
215 if (!tcm)
216 return -ENOMEM;
217
218 /* Fill in command arguments */
219 if (fmt && fmt[0]) {
220 len = snprintf(tcm->cmd, sizeof(tcm->cmd) - 1, "CMD %s ", cmd);
221 va_start(ap, fmt);
222 vsnprintf(tcm->cmd + len, sizeof(tcm->cmd) - len - 1, fmt, ap);
223 va_end(ap);
224 } else {
225 snprintf(tcm->cmd, sizeof(tcm->cmd) - 1, "CMD %s", cmd);
226 }
227
228 tcm->cmd_len = strlen(cmd);
229 tcm->critical = critical;
230 llist_add_tail(&tcm->list, &trx->trx_ctrl_list);
231 LOGP(DTRX, LOGL_INFO, "Adding new control '%s'\n", tcm->cmd);
232
233 /* Send message, if no pending messages */
234 if (!pending)
235 trx_ctrl_send(trx);
236
237 return 0;
238}
239
240/*
241 * Power Control
242 *
243 * ECHO is used to check transceiver availability.
244 * CMD ECHO
245 * RSP ECHO <status>
246 *
247 * POWEROFF shuts off transmitter power and stops the demodulator.
248 * CMD POWEROFF
249 * RSP POWEROFF <status>
250 *
251 * POWERON starts the transmitter and starts the demodulator.
252 * Initial power level is very low.
253 * This command fails if the transmitter and receiver are not yet tuned.
254 * This command fails if the transmit or receive frequency creates a conflict
255 * with another ARFCN that is already running.
256 * If the transceiver is already on, it response with success to this command.
257 * CMD POWERON
258 * RSP POWERON <status>
259 */
260
261int trx_if_cmd_sync(struct trx_instance *trx)
262{
263 return trx_ctrl_cmd(trx, 1, "SYNC", "");
264}
265
266int trx_if_cmd_echo(struct trx_instance *trx)
267{
268 return trx_ctrl_cmd(trx, 1, "ECHO", "");
269}
270
271int trx_if_cmd_poweroff(struct trx_instance *trx)
272{
273 return trx_ctrl_cmd(trx, 1, "POWEROFF", "");
274}
275
276int trx_if_cmd_poweron(struct trx_instance *trx)
277{
278 if (trx->powered_up) {
279 /* FIXME: this should be handled by the FSM, not here! */
280 LOGP(DTRX, LOGL_ERROR, "Suppressing POWERON as we're already powered up\n");
281 return -EAGAIN;
282 }
283 return trx_ctrl_cmd(trx, 1, "POWERON", "");
284}
285
286/*
287 * Timeslot Control
288 *
289 * SETSLOT sets the format of the uplink timeslots in the ARFCN.
290 * The <timeslot> indicates the timeslot of interest.
291 * The <chantype> indicates the type of channel that occupies the timeslot.
292 * A chantype of zero indicates the timeslot is off.
293 * CMD SETSLOT <timeslot> <chantype>
294 * RSP SETSLOT <status> <timeslot> <chantype>
295 */
296
297int trx_if_cmd_setslot(struct trx_instance *trx, uint8_t tn, uint8_t type)
298{
299 return trx_ctrl_cmd(trx, 1, "SETSLOT", "%u %u", tn, type);
300}
301
302/*
303 * Tuning Control
304 *
305 * (RX/TX)TUNE tunes the receiver to a given frequency in kHz.
306 * This command fails if the receiver is already running.
307 * (To re-tune you stop the radio, re-tune, and restart.)
308 * This command fails if the transmit or receive frequency
309 * creates a conflict with another ARFCN that is already running.
310 * CMD (RX/TX)TUNE <kHz>
311 * RSP (RX/TX)TUNE <status> <kHz>
312 */
313
314int trx_if_cmd_rxtune(struct trx_instance *trx, uint16_t band_arfcn)
315{
316 uint16_t freq10;
317
318 /* RX is downlink on MS side */
319 freq10 = gsm_arfcn2freq10(band_arfcn, 0);
320 if (freq10 == 0xffff) {
321 LOGP(DTRX, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn);
322 return -ENOTSUP;
323 }
324
325 return trx_ctrl_cmd(trx, 1, "RXTUNE", "%u", freq10 * 100);
326}
327
328int trx_if_cmd_txtune(struct trx_instance *trx, uint16_t band_arfcn)
329{
330 uint16_t freq10;
331
332 /* TX is uplink on MS side */
333 freq10 = gsm_arfcn2freq10(band_arfcn, 1);
334 if (freq10 == 0xffff) {
335 LOGP(DTRX, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn);
336 return -ENOTSUP;
337 }
338
339 return trx_ctrl_cmd(trx, 1, "TXTUNE", "%u", freq10 * 100);
340}
341
342/*
343 * Power measurement
344 *
345 * MEASURE instructs the transceiver to perform a power
346 * measurement on specified frequency. After receiving this
347 * request, transceiver should quickly re-tune to requested
348 * frequency, measure power level and re-tune back to the
349 * previous frequency.
350 * CMD MEASURE <kHz>
351 * RSP MEASURE <status> <kHz> <dB>
352 */
353
354int trx_if_cmd_measure(struct trx_instance *trx,
355 uint16_t band_arfcn_start, uint16_t band_arfcn_stop)
356{
357 uint16_t freq10;
358
359 /* Update ARFCN range for measurement */
360 trx->pm_band_arfcn_start = band_arfcn_start;
361 trx->pm_band_arfcn_stop = band_arfcn_stop;
362
363 /* Calculate a frequency for current ARFCN (DL) */
364 freq10 = gsm_arfcn2freq10(band_arfcn_start, 0);
365 if (freq10 == 0xffff) {
366 LOGP(DTRX, LOGL_ERROR, "ARFCN %d not defined\n", band_arfcn_start);
367 return -ENOTSUP;
368 }
369
370 return trx_ctrl_cmd(trx, 1, "MEASURE", "%u", freq10 * 100);
371}
372
373static void trx_if_measure_rsp_cb(struct trx_instance *trx, char *resp)
374{
375 unsigned int freq10;
376 uint16_t band_arfcn;
377 int dbm;
378
379 /* Parse freq. and power level */
380 sscanf(resp, "%u %d", &freq10, &dbm);
381 freq10 /= 100;
382
383 /* Check received ARFCN against expected */
384 band_arfcn = gsm_freq102arfcn((uint16_t) freq10, 0);
385 if (band_arfcn != trx->pm_band_arfcn_start) {
386 LOGP(DTRX, LOGL_ERROR, "Power measurement error: "
387 "response ARFCN=%u doesn't match expected ARFCN=%u\n",
388 band_arfcn &~ ARFCN_FLAG_MASK,
389 trx->pm_band_arfcn_start &~ ARFCN_FLAG_MASK);
390 return;
391 }
392
393 /* Send L1CTL_PM_CONF */
394 l1ctl_tx_pm_conf(trx->l1l, band_arfcn, dbm,
395 band_arfcn == trx->pm_band_arfcn_stop);
396
397 /* Schedule a next measurement */
398 if (band_arfcn != trx->pm_band_arfcn_stop)
399 trx_if_cmd_measure(trx, ++band_arfcn, trx->pm_band_arfcn_stop);
400}
401
402/*
403 * Timing Advance control
404 *
405 * SETTA instructs the transceiver to transmit bursts in
406 * advance calculated from requested TA value. This value is
407 * normally between 0 and 63, with each step representing
408 * an advance of one bit period (about 3.69 microseconds).
409 * Since OsmocomBB has a special feature, which allows one
410 * to spoof the distance from BTS, the range is extended.
411 * CMD SETTA <-128..127>
412 * RSP SETTA <status> <TA>
413 */
414
415int trx_if_cmd_setta(struct trx_instance *trx, int8_t ta)
416{
417 return trx_ctrl_cmd(trx, 0, "SETTA", "%d", ta);
418}
419
420/*
421 * Frequency Hopping parameters indication.
422 *
423 * SETFH instructs transceiver to enable frequency hopping mode
424 * using the given HSN, MAIO, and Mobile Allocation parameters.
425 *
426 * CMD SETFH <HSN> <MAIO> <RXF1> <TXF1> [... <RXFN> <TXFN>]
427 *
428 * where <RXFN> and <TXFN> is a pair of Rx/Tx frequencies (in kHz)
429 * corresponding to one ARFCN the Mobile Allocation. Note that the
430 * channel list is expected to be sorted in ascending order.
431 */
432
433int trx_if_cmd_setfh(struct trx_instance *trx, uint8_t hsn,
434 uint8_t maio, uint16_t *ma, size_t ma_len)
435{
436 /* Reserve some room for CMD SETFH <HSN> <MAIO> */
437 char ma_buf[TRXC_BUF_SIZE - 24];
438 size_t ma_buf_len = sizeof(ma_buf) - 1;
439 uint16_t rx_freq, tx_freq;
440 char *ptr;
441 int i, rc;
442
443 /* Make sure that Mobile Allocation has at least one ARFCN */
444 if (!ma_len || ma == NULL) {
445 LOGP(DTRX, LOGL_ERROR, "Mobile Allocation is empty?!?\n");
446 return -EINVAL;
447 }
448
449 /* Compose a sequence of Rx/Tx frequencies (mobile allocation) */
450 for (i = 0, ptr = ma_buf; i < ma_len; i++) {
451 /* Convert ARFCN to a pair of Rx/Tx frequencies (Hz * 10) */
452 rx_freq = gsm_arfcn2freq10(ma[i], 0); /* Rx: Downlink */
453 tx_freq = gsm_arfcn2freq10(ma[i], 1); /* Tx: Uplink */
454 if (rx_freq == 0xffff || tx_freq == 0xffff) {
455 LOGP(DTRX, LOGL_ERROR, "Failed to convert ARFCN %u "
456 "to a pair of Rx/Tx frequencies\n",
457 ma[i] & ~ARFCN_FLAG_MASK);
458 return -EINVAL;
459 }
460
461 /* Append a pair of Rx/Tx frequencies (in kHz) to the buffer */
462 rc = snprintf(ptr, ma_buf_len, "%u %u ", rx_freq * 100, tx_freq * 100);
463 if (rc < 0 || rc > ma_buf_len) { /* Prevent buffer overflow */
464 LOGP(DTRX, LOGL_ERROR, "Not enough room to encode "
465 "Mobile Allocation (N=%zu)\n", ma_len);
466 return -ENOSPC;
467 }
468
469 /* Move pointer */
470 ma_buf_len -= rc;
471 ptr += rc;
472 }
473
474 /* Overwrite the last space */
475 *(ptr - 1) = '\0';
476
477 return trx_ctrl_cmd(trx, 1, "SETFH", "%u %u %s", hsn, maio, ma_buf);
478}
479
480/* Get response from CTRL socket */
481static int trx_ctrl_read_cb(struct osmo_fd *ofd, unsigned int what)
482{
483 struct trx_instance *trx = ofd->data;
484 struct trx_ctrl_msg *tcm;
485 int resp, rsp_len;
486 char buf[TRXC_BUF_SIZE], *p;
Eric935c8cb2022-06-06 00:48:09 +0200487 ssize_t read_len;
Eric Wildd40300e2022-05-07 15:36:47 +0200488
Eric935c8cb2022-06-06 00:48:09 +0200489#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200490 char* response = trxif_from_trx_c();
491 if (!response) {
492 LOGP(DTRX, LOGL_ERROR, "read() failed with rc=%zd\n", response);
493 goto rsp_error;
494 }
495 memcpy(buf, response, TRXC_BUF_SIZE);
496 free(response);
Eric935c8cb2022-06-06 00:48:09 +0200497#else
498 read_len = read(ofd->fd, buf, sizeof(buf) - 1);
499 if (read_len <= 0) {
500 LOGP(DTRX, LOGL_ERROR, "read() failed with rc=%zd\n", read_len);
501 return read_len;
502 }
503 buf[read_len] = '\0';
504#endif
Eric Wildd40300e2022-05-07 15:36:47 +0200505
506 if (!!strncmp(buf, "RSP ", 4)) {
507 LOGP(DTRX, LOGL_NOTICE, "Unknown message on CTRL port: %s\n", buf);
508 return 0;
509 }
510
511 /* Calculate the length of response item */
512 p = strchr(buf + 4, ' ');
513 rsp_len = p ? p - buf - 4 : strlen(buf) - 4;
514
515 LOGP(DTRX, LOGL_INFO, "Response message: '%s'\n", buf);
516
517 /* Abort expire timer */
518 if (osmo_timer_pending(&trx->trx_ctrl_timer))
519 osmo_timer_del(&trx->trx_ctrl_timer);
520
521 /* Get command for response message */
522 if (llist_empty(&trx->trx_ctrl_list)) {
523 LOGP(DTRX, LOGL_NOTICE, "Response message without command\n");
524 return -EINVAL;
525 }
526
527 tcm = llist_entry(trx->trx_ctrl_list.next,
528 struct trx_ctrl_msg, list);
529
530 /* Check if response matches command */
531 if (!!strncmp(buf + 4, tcm->cmd + 4, rsp_len)) {
532 LOGP(DTRX, (tcm->critical) ? LOGL_FATAL : LOGL_ERROR,
533 "Response message '%s' does not match command "
534 "message '%s'\n", buf, tcm->cmd);
535 goto rsp_error;
536 }
537
538 /* Check for response code */
539 sscanf(p + 1, "%d", &resp);
540 if (resp) {
541 LOGP(DTRX, (tcm->critical) ? LOGL_FATAL : LOGL_ERROR,
542 "Transceiver rejected TRX command with "
543 "response: '%s'\n", buf);
544
545 if (tcm->critical)
546 goto rsp_error;
547 }
548
549 /* Trigger state machine */
550 if (!strncmp(tcm->cmd + 4, "POWERON", 7)) {
551 trx->powered_up = true;
552 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_ACTIVE, 0, 0);
553 }
554 else if (!strncmp(tcm->cmd + 4, "POWEROFF", 8)) {
555 trx->powered_up = false;
556 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_IDLE, 0, 0);
557 }
558 else if (!strncmp(tcm->cmd + 4, "MEASURE", 7))
559 trx_if_measure_rsp_cb(trx, buf + 14);
560 else if (!strncmp(tcm->cmd + 4, "ECHO", 4))
561 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_IDLE, 0, 0);
562 else
563 osmo_fsm_inst_state_chg(trx->fsm, trx->prev_state, 0, 0);
564
565 /* Remove command from list */
566 llist_del(&tcm->list);
567 talloc_free(tcm);
568
569 /* Send next message, if any */
570 trx_ctrl_send(trx);
571
572 return 0;
573
574rsp_error:
575 /* Notify higher layers about the problem */
576 osmo_fsm_inst_dispatch(trxcon_fsm, TRX_EVENT_RSP_ERROR, trx);
577 return -EIO;
578}
579
580/* ------------------------------------------------------------------------ */
581/* Data interface handlers */
582/* ------------------------------------------------------------------------ */
583/* DATA interface */
584/* */
585/* Messages on the data interface carry one radio burst per UDP message. */
586/* */
587/* Received Data Burst: */
588/* 1 byte timeslot index */
589/* 4 bytes GSM frame number, BE */
590/* 1 byte RSSI in -dBm */
591/* 2 bytes correlator timing offset in 1/256 symbol steps, 2's-comp, BE */
592/* 148 bytes soft symbol estimates, 0 -> definite "0", 255 -> definite "1" */
593/* 2 bytes are not used, but being sent by OsmoTRX */
594/* */
595/* Transmit Data Burst: */
596/* 1 byte timeslot index */
597/* 4 bytes GSM frame number, BE */
598/* 1 byte transmit level wrt ARFCN max, -dB (attenuation) */
599/* 148 bytes output symbol values, 0 & 1 */
600/* ------------------------------------------------------------------------ */
601
602static int trx_data_rx_cb(struct osmo_fd *ofd, unsigned int what)
603{
604 struct trx_instance *trx = ofd->data;
605 struct trx_meas_set meas;
606 uint8_t buf[TRXD_BUF_SIZE];
607 sbit_t bits[148];
608 int8_t rssi, tn;
609 int16_t toa256;
610 uint32_t fn;
611 ssize_t read_len;
612
Eric935c8cb2022-06-06 00:48:09 +0200613#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200614 struct trxd_from_trx* rcvd = trxif_from_trx_d();
615 if (!rcvd) {
616 LOGP(DTRX, LOGL_ERROR, "read() failed with rc=%zd\n", rcvd);
617 return rcvd;
618 }
619
620 tn = rcvd->ts;
621 fn = rcvd->fn;
622 rssi = -(int8_t) rcvd->rssi;
623 toa256 = (int16_t) rcvd->toa;
624
625 /* Copy and convert bits {254..0} to sbits {-127..127} */
626 //osmo_ubit2sbit(bits, rcvd->symbols, 148);
627 memcpy(bits, rcvd->symbols, 148);
628
629 free(rcvd);
Eric935c8cb2022-06-06 00:48:09 +0200630#else
631 read_len = read(ofd->fd, buf, sizeof(buf));
632 if (read_len <= 0) {
633 LOGP(DTRXD, LOGL_ERROR, "read() failed with rc=%zd\n", read_len);
634 return read_len;
635 }
636
637 if (read_len != 158) {
638 LOGP(DTRXD, LOGL_ERROR,
639 "Got data message with invalid "
640 "length '%zd'\n",
641 read_len);
642 return -EINVAL;
643 }
644#endif
645 tn = buf[0];
646 fn = osmo_load32be(buf + 1);
647 rssi = -(int8_t)buf[5];
648 toa256 = ((int16_t)(buf[6] << 8) | buf[7]);
649
650 /* Copy and convert bits {254..0} to sbits {-127..127} */
651 //osmo_ubit2sbit(bits, buf + 8, 148);
652 memcpy(bits, buf + 8, 148);
Eric Wildd40300e2022-05-07 15:36:47 +0200653
654 if (tn >= 8) {
655 LOGP(DTRXD, LOGL_ERROR, "Illegal TS %d\n", tn);
656 return -EINVAL;
657 }
658
659 if (fn >= 2715648) {
660 LOGP(DTRXD, LOGL_ERROR, "Illegal FN %u\n", fn);
661 return -EINVAL;
662 }
663
664 LOGP(DTRXD, LOGL_DEBUG, "RX burst tn=%u fn=%u rssi=%d toa=%d\n",
665 tn, fn, rssi, toa256);
666
667 /* Group the measurements together */
668 meas = (struct trx_meas_set) {
669 .toa256 = toa256,
670 .rssi = rssi,
671 .fn = fn,
672 };
673
674 /* Poke scheduler */
675 sched_trx_handle_rx_burst(trx, tn, fn, bits, 148, &meas);
676
677 /* Correct local clock counter */
678 if (fn % 51 == 0)
679 sched_clck_handle(&trx->sched, fn);
680
681 return 0;
682}
683
684int trx_if_tx_burst(struct trx_instance *trx, uint8_t tn, uint32_t fn,
685 uint8_t pwr, const ubit_t *bits)
686{
Eric935c8cb2022-06-06 00:48:09 +0200687#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200688 struct trxd_to_trx* t = malloc(sizeof(struct trxd_to_trx));
689 t->ts = tn;
690 t->fn = fn;
691 t->txlev = pwr;
692 memcpy(t->symbols, bits, 148);
693 trxif_to_trx_d(t);
Eric935c8cb2022-06-06 00:48:09 +0200694#else
695 uint8_t buf[TRXD_BUF_SIZE];
696
697 /**
698 * We must be sure that we have clock,
699 * and we have sent all control data
700 *
701 * TODO: introduce proper state machines for both
702 * transceiver and its TRXC interface.
703 */
704#if 0
705 if (trx->fsm->state != TRX_STATE_ACTIVE) {
706 LOGP(DTRXD, LOGL_ERROR, "Ignoring TX data, "
707 "transceiver isn't ready\n");
708 return -EAGAIN;
709 }
710#endif
711
712 LOGP(DTRXD, LOGL_DEBUG, "TX burst tn=%u fn=%u pwr=%u\n", tn, fn, pwr);
713
714 buf[0] = tn;
715 osmo_store32be(fn, buf + 1);
716 buf[5] = pwr;
717
718 /* Copy ubits {0,1} */
719 memcpy(buf + 6, bits, 148);
720
721 /* Send data to transceiver */
722 send(trx->trx_ofd_data.fd, buf, 154, 0);
723
724#endif
Eric Wildd40300e2022-05-07 15:36:47 +0200725 return 0;
726}
727
728/* Init TRX interface (TRXC, TRXD sockets and FSM) */
729struct trx_instance *trx_if_open(void *tall_ctx,
730 const char *local_host, const char *remote_host,
731 uint16_t base_port)
732{
733 struct trx_instance *trx;
734 int rc;
735
736 LOGP(DTRX, LOGL_NOTICE, "Init transceiver interface "
737 "(%s:%u)\n", remote_host, base_port);
738
739 /* Try to allocate memory */
740 trx = talloc_zero(tall_ctx, struct trx_instance);
741 if (!trx) {
742 LOGP(DTRX, LOGL_ERROR, "Failed to allocate memory\n");
743 return NULL;
744 }
745
746 /* Allocate a new dedicated state machine */
747 trx->fsm = osmo_fsm_inst_alloc(&trx_fsm, trx,
748 NULL, LOGL_DEBUG, "trx_interface");
749 if (trx->fsm == NULL) {
750 LOGP(DTRX, LOGL_ERROR, "Failed to allocate an instance "
751 "of FSM '%s'\n", trx_fsm.name);
752 talloc_free(trx);
753 return NULL;
754 }
755
756 /* Initialize CTRL queue */
757 INIT_LLIST_HEAD(&trx->trx_ctrl_list);
758
Eric935c8cb2022-06-06 00:48:09 +0200759#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200760 rc = eventfd(0, 0);
761 osmo_fd_setup(get_c_fd(), rc, OSMO_FD_READ, trx_ctrl_read_cb, trx, 0);
762 osmo_fd_register(get_c_fd());
763
764 rc = eventfd(0, 0);
765 osmo_fd_setup(get_d_fd(), rc, OSMO_FD_READ, trx_data_rx_cb, trx, 0);
766 osmo_fd_register(get_d_fd());
Eric935c8cb2022-06-06 00:48:09 +0200767#else
768 /* Open sockets */
769 rc = trx_udp_open(trx, &trx->trx_ofd_ctrl, local_host, base_port + 101, remote_host, base_port + 1,
770 trx_ctrl_read_cb);
771 if (rc < 0)
772 goto udp_error;
Eric Wildd40300e2022-05-07 15:36:47 +0200773
Eric935c8cb2022-06-06 00:48:09 +0200774 rc = trx_udp_open(trx, &trx->trx_ofd_data, local_host, base_port + 102, remote_host, base_port + 2,
775 trx_data_rx_cb);
776 if (rc < 0)
777 goto udp_error;
778#endif
Eric Wildd40300e2022-05-07 15:36:47 +0200779 return trx;
Eric935c8cb2022-06-06 00:48:09 +0200780
781udp_error:
782 LOGP(DTRX, LOGL_ERROR, "Couldn't establish UDP connection\n");
783 osmo_fsm_inst_free(trx->fsm);
784 talloc_free(trx);
785 return NULL;
Eric Wildd40300e2022-05-07 15:36:47 +0200786}
787
788/* Flush pending control messages */
789void trx_if_flush_ctrl(struct trx_instance *trx)
790{
791 struct trx_ctrl_msg *tcm;
792
793 /* Reset state machine */
794 osmo_fsm_inst_state_chg(trx->fsm, TRX_STATE_IDLE, 0, 0);
795
796 /* Clear command queue */
797 while (!llist_empty(&trx->trx_ctrl_list)) {
798 tcm = llist_entry(trx->trx_ctrl_list.next,
799 struct trx_ctrl_msg, list);
800 llist_del(&tcm->list);
801 talloc_free(tcm);
802 }
803}
804
805void trx_if_close(struct trx_instance *trx)
806{
807 /* May be unallocated due to init error */
808 if (!trx)
809 return;
810
811 LOGP(DTRX, LOGL_NOTICE, "Shutdown transceiver interface\n");
812
813 /* Flush CTRL message list */
814 trx_if_flush_ctrl(trx);
815
816 /* Close sockets */
Eric935c8cb2022-06-06 00:48:09 +0200817#ifdef IPCIF
Eric Wildd40300e2022-05-07 15:36:47 +0200818 close(get_c_fd()->fd);
819 close(get_d_fd()->fd);
Eric935c8cb2022-06-06 00:48:09 +0200820#else
821 trx_udp_close(&trx->trx_ofd_ctrl);
822 trx_udp_close(&trx->trx_ofd_data);
823#endif
Eric Wildd40300e2022-05-07 15:36:47 +0200824
825 /* Free memory */
826 osmo_fsm_inst_free(trx->fsm);
827 talloc_free(trx);
828}
829
830static __attribute__((constructor)) void on_dso_load(void)
831{
832 OSMO_ASSERT(osmo_fsm_register(&trx_fsm) == 0);
833}