aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-trx/sched_lchan_tchf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/osmo-bts-trx/sched_lchan_tchf.c')
-rw-r--r--src/osmo-bts-trx/sched_lchan_tchf.c642
1 files changed, 373 insertions, 269 deletions
diff --git a/src/osmo-bts-trx/sched_lchan_tchf.c b/src/osmo-bts-trx/sched_lchan_tchf.c
index 689925fb..5a3e80ac 100644
--- a/src/osmo-bts-trx/sched_lchan_tchf.c
+++ b/src/osmo-bts-trx/sched_lchan_tchf.c
@@ -1,7 +1,7 @@
/*
* (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
* (C) 2015-2017 by Harald Welte <laforge@gnumonks.org>
- * Contributions by sysmocom - s.f.m.c. GmbH
+ * (C) 2020-2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
*
* All Rights Reserved
*
@@ -13,7 +13,7 @@
* 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.
+ * 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/>.
@@ -30,11 +30,12 @@
#include <osmocom/gsm/gsm0502.h>
#include <osmocom/codec/codec.h>
-#include <osmocom/codec/ecu.h>
#include <osmocom/coding/gsm0503_coding.h>
#include <osmocom/coding/gsm0503_amr_dtx.h>
+#include <osmocom/netif/amr.h>
+
#include <osmo-bts/bts.h>
#include <osmo-bts/l1sap.h>
#include <osmo-bts/logging.h>
@@ -43,28 +44,80 @@
#include <osmo-bts/msg_utils.h>
#include <sched_utils.h>
-#include <loops.h>
+#include <amr_loop.h>
+
+/* 3GPP TS 45.009, table 3.2.1.3-{1,3}: AMR on Uplink TCH/F.
+ *
+ * +---+---+---+---+---+---+---+---+
+ * | a | b | c | d | e | f | g | h | Burst 'a' received first
+ * +---+---+---+---+---+---+---+---+
+ * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Speech/FACCH frame (bursts 'a' .. 'h')
+ *
+ * TDMA frame number of burst 'h' is always used as the table index. */
+static const uint8_t sched_tchf_ul_amr_cmi_map[26] = {
+ [7] = 1, /* TCH/F: a=0 / h=7 */
+ [16] = 1, /* TCH/F: a=8 / h=16 */
+ [24] = 1, /* TCH/F: a=17 / h=24 */
+};
+
+/* TDMA frame number of burst 'a' should be used as the table index. */
+static const uint8_t sched_tchf_dl_amr_cmi_map[26] = {
+ [4] = 1, /* TCH/F: a=4 */
+ [13] = 1, /* TCH/F: a=13 */
+ [21] = 1, /* TCH/F: a=21 */
+};
+
+extern const uint8_t sched_tchh_dl_amr_cmi_map[26];
+
+static int decode_fr_facch(struct l1sched_ts *l1ts,
+ const struct trx_ul_burst_ind *bi)
+{
+ struct l1sched_chan_state *chan_state = &l1ts->chan_state[bi->chan];
+ const sbit_t *bursts_p = chan_state->ul_bursts;
+ struct l1sched_meas_set meas_avg;
+ uint8_t data[GSM_MACBLOCK_LEN];
+ int n_errors, n_bits_total;
+ int rc;
+
+ rc = gsm0503_tch_fr_facch_decode(&data[0], BUFTAIL8(bursts_p),
+ &n_errors, &n_bits_total);
+ if (rc != GSM_MACBLOCK_LEN)
+ return rc;
+
+ /* average measurements of the last 8 bursts, obtain TDMA Fn of the first burst */
+ trx_sched_meas_avg(chan_state, &meas_avg, SCHED_MEAS_AVG_M_S8N8);
+
+ _sched_compose_ph_data_ind(l1ts, meas_avg.fn, bi->chan,
+ &data[0], GSM_MACBLOCK_LEN,
+ compute_ber10k(n_bits_total, n_errors),
+ meas_avg.rssi,
+ meas_avg.toa256,
+ meas_avg.ci_cb,
+ PRES_INFO_UNKNOWN);
+ return GSM_MACBLOCK_LEN;
+}
-/*! \brief a single TCH/F burst was received by the PHY, process it */
+/* Process a single Uplink TCH/F burst received by the PHY.
+ * This function is visualized in file 'doc/trx_sched_tch.txt'. */
int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
{
struct l1sched_chan_state *chan_state = &l1ts->chan_state[bi->chan];
struct gsm_lchan *lchan = chan_state->lchan;
- sbit_t *burst, **bursts_p = &chan_state->ul_bursts;
- uint8_t *mask = &chan_state->ul_mask;
+ sbit_t *burst, *bursts_p = chan_state->ul_bursts;
+ uint32_t *mask = &chan_state->ul_mask;
uint8_t rsl_cmode = chan_state->rsl_cmode;
uint8_t tch_mode = chan_state->tch_mode;
- uint8_t tch_data[128]; /* just to be safe */
- enum sched_meas_avg_mode meas_avg_mode = SCHED_MEAS_AVG_M_OCTO;
+ uint8_t tch_data[290]; /* large enough to hold 290 unpacked bits for CSD */
+ enum sched_meas_avg_mode meas_avg_mode = SCHED_MEAS_AVG_M_S8N8;
struct l1sched_meas_set meas_avg;
int rc, amr = 0;
int n_errors = 0;
int n_bits_total = 0;
- bool bfi_flag = false;
unsigned int fn_begin;
uint16_t ber10k;
uint8_t is_sub = 0;
uint8_t ft;
+ bool amr_is_cmr;
/* If handover RACH detection is turned on, treat this burst as an Access Burst.
* Handle NOPE.ind as usually to ensure proper Uplink measurement reporting. */
@@ -73,17 +126,11 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
LOGL1SB(DL1P, LOGL_DEBUG, l1ts, bi, "Received TCH/F, bid=%u\n", bi->bid);
- /* allocate burst memory, if not already */
- if (!*bursts_p) {
- *bursts_p = talloc_zero_size(tall_bts_ctx, 928);
- if (!*bursts_p)
- return -ENOMEM;
- }
-
- /* clear burst */
+ /* shift the buffer by 4 bursts leftwards */
if (bi->bid == 0) {
- memset(*bursts_p + 464, 0, 464);
- *mask = 0x0;
+ memmove(BUFPOS(bursts_p, 0), BUFPOS(bursts_p, 4), 20 * BPLEN);
+ memset(BUFPOS(bursts_p, 20), 0, 4 * BPLEN);
+ *mask = *mask << 4;
}
/* update mask */
@@ -92,43 +139,48 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
/* store measurements */
trx_sched_meas_push(chan_state, bi);
- /* copy burst to end of buffer of 8 bursts */
- burst = *bursts_p + bi->bid * 116 + 464;
+ /* copy burst to end of buffer of 24 bursts */
+ burst = BUFPOS(bursts_p, 20 + bi->bid);
if (bi->burst_len > 0) {
memcpy(burst, bi->burst + 3, 58);
memcpy(burst + 58, bi->burst + 87, 58);
- } else
- memset(burst, 0, 116);
+ }
/* wait until complete set of bursts */
if (bi->bid != 3)
return 0;
- /* check for complete set of bursts */
- if ((*mask & 0xf) != 0xf) {
- LOGL1SB(DL1P, LOGL_NOTICE, l1ts, bi,
- "Received incomplete frame (%u/%u)\n",
- bi->fn % l1ts->mf_period, l1ts->mf_period);
+ /* fill up the burst buffer so that we have 8 bursts in there */
+ if (OSMO_UNLIKELY((*mask & 0xff) != 0xff)) {
+ LOGL1SB(DL1P, LOGL_DEBUG, l1ts, bi,
+ "UL burst buffer is not filled up: mask=0x%02x != 0xff\n",
+ *mask);
+ return 0; /* TODO: send BFI */
}
- *mask = 0x0;
- /* decode
- * also shift buffer by 4 bursts for interleaving */
- switch ((rsl_cmode != RSL_CMOD_SPD_SPEECH) ? GSM48_CMODE_SPEECH_V1
- : tch_mode) {
+ /* TCH/F: speech and signalling frames are interleaved over 8 bursts, while
+ * CSD frames are interleaved over 22 bursts. Unless we're in CSD mode,
+ * decode only the last 8 bursts to avoid introducing additional delays. */
+ switch (tch_mode) {
+ case GSM48_CMODE_SIGN:
case GSM48_CMODE_SPEECH_V1: /* FR */
- rc = gsm0503_tch_fr_decode(tch_data, *bursts_p, 1, 0, &n_errors, &n_bits_total);
- if (rc >= 0)
- lchan_set_marker(osmo_fr_check_sid(tch_data, rc), lchan); /* DTXu */
+ rc = gsm0503_tch_fr_decode(tch_data, BUFTAIL8(bursts_p),
+ 1, 0, &n_errors, &n_bits_total);
+ if (rc == GSM_FR_BYTES) /* only for valid *speech* frames */
+ lchan_set_marker(osmo_fr_is_any_sid(tch_data), lchan); /* DTXu */
break;
case GSM48_CMODE_SPEECH_EFR: /* EFR */
- rc = gsm0503_tch_fr_decode(tch_data, *bursts_p, 1, 1, &n_errors, &n_bits_total);
+ rc = gsm0503_tch_fr_decode(tch_data, BUFTAIL8(bursts_p),
+ 1, 1, &n_errors, &n_bits_total);
+ if (rc == GSM_EFR_BYTES) /* only for valid *speech* frames */
+ lchan_set_marker(osmo_efr_is_any_sid(tch_data), lchan); /* DTXu */
break;
case GSM48_CMODE_SPEECH_AMR: /* AMR */
/* the first FN 0,8,17 defines that CMI is included in frame,
* the first FN 4,13,21 defines that CMR is included in frame.
* NOTE: A frame ends 7 FN after start.
*/
+ amr_is_cmr = !sched_tchf_ul_amr_cmi_map[bi->fn % 26];
/* The AFS_ONSET frame itself does not result into an RTP frame
* since it only contains a recognition pattern that marks the
@@ -138,21 +190,24 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
if (chan_state->amr_last_dtx == AFS_ONSET)
lchan_set_marker(false, lchan);
- /* we store tch_data + 2 header bytes, the amr variable set to
- * 2 will allow us to skip the first 2 bytes in case we did
- * receive an FACCH frame instead of a voice frame (we do not
- * know this before we actually decode the frame) */
- amr = 2;
- rc = gsm0503_tch_afs_decode_dtx(tch_data + amr, *bursts_p,
- (((bi->fn + 26 - 7) % 26) >> 2) & 1, chan_state->codec,
- chan_state->codecs, &chan_state->ul_ft,
+ /* Store AMR payload in tch-data with an offset of 2 bytes, so
+ * that we can easily prepend/fill the RTP AMR header (struct
+ * amr_hdr) with osmo_amr_rtp_enc() later on. The amr variable
+ * is used far below to account for the decoded offset in case
+ * we receive an FACCH frame instead of a voice frame (we
+ * do not know this before we actually decode the frame) */
+ amr = sizeof(struct amr_hdr);
+ rc = gsm0503_tch_afs_decode_dtx(tch_data + amr, BUFTAIL8(bursts_p),
+ amr_is_cmr, chan_state->codec, chan_state->codecs, &chan_state->ul_ft,
&chan_state->ul_cmr, &n_errors, &n_bits_total, &chan_state->amr_last_dtx);
/* Tag all frames that are not regular AMR voice frames as
* SUB-Frames */
if (chan_state->amr_last_dtx != AMR_OTHER) {
- LOGL1SB(DL1P, LOGL_DEBUG, l1ts, bi, "Received AMR SID frame: %s\n",
- gsm0503_amr_dtx_frame_name(chan_state->amr_last_dtx));
+ LOGL1SB(DL1P, LOGL_DEBUG, l1ts, bi,
+ "Received AMR DTX frame (rc=%d, BER %d/%d): %s\n",
+ rc, n_errors, n_bits_total,
+ gsm0503_amr_dtx_frame_name(chan_state->amr_last_dtx));
is_sub = 1;
}
@@ -173,20 +228,18 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
switch (chan_state->amr_last_dtx) {
case AFS_SID_FIRST:
case AFS_SID_UPDATE_CN:
- meas_avg_mode = SCHED_MEAS_AVG_M8_FIRST_QUAD;
+ meas_avg_mode = SCHED_MEAS_AVG_M_S8N4;
break;
case AFS_SID_UPDATE:
case AFS_ONSET:
- meas_avg_mode = SCHED_MEAS_AVG_M_QUAD;
+ meas_avg_mode = SCHED_MEAS_AVG_M_S4N4;
break;
}
- if (rc)
- trx_loop_amr_input(chan_state, n_errors, n_bits_total);
/* only good speech frames get rtp header */
if (rc != GSM_MACBLOCK_LEN && rc >= 4) {
if (chan_state->amr_last_dtx == AMR_OTHER) {
- ft = chan_state->codec[chan_state->ul_cmr];
+ ft = chan_state->codec[chan_state->ul_ft];
} else {
/* SID frames will always get Frame Type Index 8 (AMR_SID) */
ft = AMR_SID;
@@ -197,44 +250,70 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
}
break;
+ /* CSD (TCH/F9.6): 12.0 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_12k0:
+ /* FACCH/F does not steal TCH/F9.6 frames, but only disturbs some bits */
+ decode_fr_facch(l1ts, bi);
+ rc = gsm0503_tch_fr96_decode(&tch_data[0], BUFPOS(bursts_p, 0),
+ &n_errors, &n_bits_total);
+ meas_avg_mode = SCHED_MEAS_AVG_M_S24N22;
+ break;
+ /* CSD (TCH/F4.8): 6.0 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_6k0:
+ /* FACCH/F does not steal TCH/F4.8 frames, but only disturbs some bits */
+ decode_fr_facch(l1ts, bi);
+ rc = gsm0503_tch_fr48_decode(&tch_data[0], BUFPOS(bursts_p, 0),
+ &n_errors, &n_bits_total);
+ meas_avg_mode = SCHED_MEAS_AVG_M_S24N22;
+ break;
+ /* CSD (TCH/F2.4): 3.6 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_3k6:
+ /* TCH/F2.4 employs the same interleaving as TCH/FS (8 bursts),
+ * so FACCH/F *does* steal TCH/F2.4 frames completely. */
+ if (decode_fr_facch(l1ts, bi) == GSM_MACBLOCK_LEN)
+ return 0; /* TODO: emit BFI */
+ rc = gsm0503_tch_fr24_decode(&tch_data[0], BUFTAIL8(bursts_p),
+ &n_errors, &n_bits_total);
+ meas_avg_mode = SCHED_MEAS_AVG_M_S8N8;
+ break;
+ /* CSD (TCH/F14.4): 14.5 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_14k5:
+ /* FACCH/F does not steal TCH/F14.4 frames, but only disturbs some bits */
+ decode_fr_facch(l1ts, bi);
+ rc = gsm0503_tch_fr144_decode(&tch_data[0], BUFPOS(bursts_p, 0),
+ &n_errors, &n_bits_total);
+ meas_avg_mode = SCHED_MEAS_AVG_M_S24N22;
+ break;
default:
LOGL1SB(DL1P, LOGL_ERROR, l1ts, bi,
"TCH mode %u invalid, please fix!\n",
tch_mode);
return -EINVAL;
}
- memcpy(*bursts_p, *bursts_p + 464, 464);
+
+ ber10k = compute_ber10k(n_bits_total, n_errors);
/* average measurements of the last N (depends on mode) bursts */
trx_sched_meas_avg(chan_state, &meas_avg, meas_avg_mode);
+ /* meas_avg.fn now contains TDMA frame number of the first burst */
+ fn_begin = meas_avg.fn;
+
+ if (tch_mode == GSM48_CMODE_SPEECH_AMR)
+ trx_loop_amr_input(chan_state, &meas_avg);
/* Check if the frame is bad */
- if (rc < 0) {
- LOGL1SB(DL1P, LOGL_NOTICE, l1ts, bi, "Received bad data (%u/%u)\n",
- bi->fn % l1ts->mf_period, l1ts->mf_period);
- bfi_flag = true;
- } else if (rc < 4) {
+ if (rc < 4) {
LOGL1SB(DL1P, LOGL_NOTICE, l1ts, bi,
- "Received bad data (%u/%u) with invalid codec mode %d\n",
- bi->fn % l1ts->mf_period, l1ts->mf_period, rc);
- bfi_flag = true;
- }
-
- if (rc != GSM_MACBLOCK_LEN && lchan->ecu_state)
- osmo_ecu_frame_in(lchan->ecu_state, bfi_flag, tch_data, rc);
-
- ber10k = compute_ber10k(n_bits_total, n_errors);
- if (bfi_flag)
- goto bfi;
-
- /* FACCH */
- if (rc == GSM_MACBLOCK_LEN) {
- fn_begin = gsm0502_fn_remap(bi->fn, FN_REMAP_FACCH_F);
+ BAD_DATA_MSG_FMT "\n", BAD_DATA_MSG_ARGS);
+ rc = 0; /* this is how we signal BFI to l1sap */
+ } else if (rc == GSM_MACBLOCK_LEN) { /* FACCH/F */
_sched_compose_ph_data_ind(l1ts, fn_begin, bi->chan,
- tch_data + amr, GSM_MACBLOCK_LEN,
- meas_avg.rssi, meas_avg.toa256,
- meas_avg.ci_cb, ber10k,
- PRES_INFO_UNKNOWN);
+ &tch_data[amr], GSM_MACBLOCK_LEN,
+ ber10k,
+ meas_avg.rssi,
+ meas_avg.toa256,
+ meas_avg.ci_cb,
+ PRES_INFO_UNKNOWN);
/* If we are in SPEECH mode we will generate a fake (BFI) TCH
* indication as well. This indication is needed by the higher
@@ -244,184 +323,82 @@ int rx_tchf_fn(struct l1sched_ts *l1ts, const struct trx_ul_burst_ind *bi)
* the fake (BFI) TCH indication we set meas_avg.rssi to zero.
* Doing so tells l1sap.c to ignore the measurement result. */
meas_avg.rssi = 0;
-
-bfi:
- if (rsl_cmode == RSL_CMOD_SPD_SPEECH) {
- /* indicate bad frame */
- if (lchan->tch.dtx.ul_sid) {
- /* DTXu: pause in progress. Push empty payload to upper layers */
- rc = 0;
- goto compose_l1sap;
- }
-
- /* If there is an ECU active on this channel, use its output */
- if (lchan->ecu_state) {
- rc = osmo_ecu_frame_out(lchan->ecu_state, tch_data);
- if (rc >= 0) /* Otherwise we send a BFI */
- goto compose_l1sap;
- }
-
- switch (tch_mode) {
- case GSM48_CMODE_SPEECH_V1: /* FR */
- memset(tch_data, 0, GSM_FR_BYTES);
- tch_data[0] = 0xd0;
- rc = GSM_FR_BYTES;
- break;
- case GSM48_CMODE_SPEECH_EFR: /* EFR */
- memset(tch_data, 0, GSM_EFR_BYTES);
- tch_data[0] = 0xc0;
- rc = GSM_EFR_BYTES;
- break;
- case GSM48_CMODE_SPEECH_AMR: /* AMR */
- rc = osmo_amr_rtp_enc(tch_data,
- chan_state->codec[chan_state->dl_cmr],
- chan_state->codec[chan_state->dl_ft],
- AMR_BAD);
- if (rc < 2) {
- LOGL1SB(DL1P, LOGL_ERROR, l1ts, bi,
- "Failed to encode AMR_BAD frame (rc=%d), "
- "not sending BFI\n", rc);
- return -EINVAL;
- }
- memset(tch_data + 2, 0, rc - 2);
- break;
- default:
- LOGL1SB(DL1P, LOGL_ERROR, l1ts, bi,
- "TCH mode %u invalid, please fix!\n", tch_mode);
- return -EINVAL;
- }
- }
+ rc = 0;
}
- if (rsl_cmode != RSL_CMOD_SPD_SPEECH)
+ if (rsl_cmode == RSL_CMOD_SPD_SIGN)
return 0;
/* TCH or BFI */
-compose_l1sap:
- fn_begin = gsm0502_fn_remap(bi->fn, FN_REMAP_TCH_F);
- return _sched_compose_tch_ind(l1ts, fn_begin, bi->chan, tch_data, rc,
- /* FIXME: what should we use for BFI here? */
- bfi_flag ? bi->toa256 : meas_avg.toa256, ber10k,
- bfi_flag ? bi->rssi : meas_avg.rssi, is_sub);
+ return _sched_compose_tch_ind(l1ts, fn_begin, bi->chan,
+ &tch_data[0], rc,
+ ber10k,
+ meas_avg.rssi,
+ meas_avg.toa256,
+ meas_avg.ci_cb,
+ is_sub);
}
/* common section for generation of TCH bursts (TCH/H and TCH/F).
* FIXME: this function is over-complicated, refactor / get rid of it. */
-void tx_tch_common(struct l1sched_ts *l1ts, struct trx_dl_burst_req *br,
- struct msgb **_msg_tch, struct msgb **_msg_facch)
+void tch_dl_dequeue(struct l1sched_ts *l1ts, struct trx_dl_burst_req *br,
+ struct msgb **msg_tch, struct msgb **msg_facch)
{
- struct msgb *msg1, *msg2, *msg_tch = NULL, *msg_facch = NULL;
+ struct msgb *msg1, *msg2;
struct l1sched_chan_state *chan_state = &l1ts->chan_state[br->chan];
uint8_t rsl_cmode = chan_state->rsl_cmode;
uint8_t tch_mode = chan_state->tch_mode;
struct osmo_phsap_prim *l1sap;
- /* handle loss detection of received TCH frames */
- if (rsl_cmode == RSL_CMOD_SPD_SPEECH
- && ++(chan_state->lost_frames) > 5) {
- uint8_t tch_data[GSM_FR_BYTES];
- int len;
-
- LOGL1SB(DL1P, LOGL_NOTICE, l1ts, br, "Missing TCH bursts detected, sending BFI\n");
-
- /* indicate bad frame */
- switch (tch_mode) {
- case GSM48_CMODE_SPEECH_V1: /* FR / HR */
- if (br->chan != TRXC_TCHF) { /* HR */
- tch_data[0] = 0x70; /* F = 0, FT = 111 */
- memset(tch_data + 1, 0, 14);
- len = 15;
- break;
- }
- memset(tch_data, 0, GSM_FR_BYTES);
- len = GSM_FR_BYTES;
- break;
- case GSM48_CMODE_SPEECH_EFR: /* EFR */
- if (br->chan != TRXC_TCHF)
- goto inval_mode1;
- memset(tch_data, 0, GSM_EFR_BYTES);
- len = GSM_EFR_BYTES;
- break;
- case GSM48_CMODE_SPEECH_AMR: /* AMR */
- len = osmo_amr_rtp_enc(tch_data,
- chan_state->codec[chan_state->dl_cmr],
- chan_state->codec[chan_state->dl_ft], AMR_BAD);
- if (len < 2) {
- LOGL1SB(DL1P, LOGL_ERROR, l1ts, br,
- "Failed to encode AMR_BAD frame (rc=%d), "
- "not sending BFI\n", len);
- return;
- }
- memset(tch_data + 2, 0, len - 2);
- break;
- default:
-inval_mode1:
- LOGL1SB(DL1P, LOGL_ERROR, l1ts, br, "TCH mode invalid, please fix!\n");
- len = 0;
- }
-
- if (len) {
- /* Note: RSSI/ToA256 is set to 0 to indicate to the higher
- * layers that this is a faked tch_ind */
- _sched_compose_tch_ind(l1ts, br->fn, br->chan,
- tch_data, len, 0, 10000, 0, 0);
- }
- }
-
/* get frame and unlink from queue */
msg1 = _sched_dequeue_prim(l1ts, br);
msg2 = _sched_dequeue_prim(l1ts, br);
if (msg1) {
l1sap = msgb_l1sap_prim(msg1);
if (l1sap->oph.primitive == PRIM_TCH) {
- msg_tch = msg1;
+ *msg_tch = msg1;
if (msg2) {
l1sap = msgb_l1sap_prim(msg2);
if (l1sap->oph.primitive == PRIM_TCH) {
LOGL1SB(DL1P, LOGL_FATAL, l1ts, br, "TCH twice, please FIX!\n");
msgb_free(msg2);
} else
- msg_facch = msg2;
+ *msg_facch = msg2;
}
} else {
- msg_facch = msg1;
+ *msg_facch = msg1;
if (msg2) {
l1sap = msgb_l1sap_prim(msg2);
if (l1sap->oph.primitive != PRIM_TCH) {
LOGL1SB(DL1P, LOGL_FATAL, l1ts, br, "FACCH twice, please FIX!\n");
msgb_free(msg2);
} else
- msg_tch = msg2;
+ *msg_tch = msg2;
}
}
- } else if (msg2) {
- l1sap = msgb_l1sap_prim(msg2);
- if (l1sap->oph.primitive == PRIM_TCH)
- msg_tch = msg2;
- else
- msg_facch = msg2;
}
/* check validity of message */
- if (msg_facch && msgb_l2len(msg_facch) != GSM_MACBLOCK_LEN) {
+ if (*msg_facch != NULL && msgb_l2len(*msg_facch) != GSM_MACBLOCK_LEN) {
LOGL1SB(DL1P, LOGL_FATAL, l1ts, br, "Prim has odd len=%u != %u\n",
- msgb_l2len(msg_facch), GSM_MACBLOCK_LEN);
+ msgb_l2len(*msg_facch), GSM_MACBLOCK_LEN);
/* free message */
- msgb_free(msg_facch);
- msg_facch = NULL;
+ msgb_free(*msg_facch);
+ *msg_facch = NULL;
}
/* check validity of message, get AMR ft and cmr */
- if (!msg_facch && msg_tch) {
+ if (*msg_tch != NULL) {
int len;
uint8_t cmr_codec;
- int cmr, ft, i;
+ int ft, i;
enum osmo_amr_type ft_codec;
enum osmo_amr_quality bfi;
int8_t sti, cmi;
+ bool amr_is_cmr;
- if (rsl_cmode != RSL_CMOD_SPD_SPEECH) {
- LOGL1SB(DL1P, LOGL_NOTICE, l1ts, br, "Dropping speech frame, "
+ if (OSMO_UNLIKELY(rsl_cmode == RSL_CMOD_SPD_SIGN)) {
+ LOGL1SB(DL1P, LOGL_NOTICE, l1ts, br, "Dropping a TCH frame, "
"because we are not in speech mode\n");
goto free_bad_msg;
}
@@ -429,7 +406,7 @@ inval_mode1:
switch (tch_mode) {
case GSM48_CMODE_SPEECH_V1: /* FR / HR */
if (br->chan != TRXC_TCHF) /* HR */
- len = 15;
+ len = GSM_HR_BYTES;
else
len = GSM_FR_BYTES;
break;
@@ -439,31 +416,28 @@ inval_mode1:
len = GSM_EFR_BYTES;
break;
case GSM48_CMODE_SPEECH_AMR: /* AMR */
- len = osmo_amr_rtp_dec(msg_tch->l2h, msgb_l2len(msg_tch),
+ len = osmo_amr_rtp_dec(msgb_l2((*msg_tch)), msgb_l2len(*msg_tch),
&cmr_codec, &cmi, &ft_codec,
&bfi, &sti);
- cmr = -1;
+ if (len < 0) {
+ LOGL1SB(DL1P, LOGL_ERROR, l1ts, br, "Cannot send invalid AMR payload\n");
+ goto free_bad_msg;
+ }
ft = -1;
for (i = 0; i < chan_state->codecs; i++) {
- if (chan_state->codec[i] == cmr_codec)
- cmr = i;
if (chan_state->codec[i] == ft_codec)
ft = i;
}
- if (cmr >= 0) { /* new request */
- chan_state->dl_cmr = cmr;
- /* disable AMR loop */
- trx_loop_amr_set(chan_state, 0);
- } else {
- /* enable AMR loop */
- trx_loop_amr_set(chan_state, 1);
- }
if (ft < 0) {
LOGL1SB(DL1P, LOGL_ERROR, l1ts, br,
"Codec (FT = %d) of RTP frame not in list\n", ft_codec);
goto free_bad_msg;
}
- if (fn_is_codec_mode_request(br->fn) && chan_state->dl_ft != ft) {
+ if (br->chan == TRXC_TCHF)
+ amr_is_cmr = !sched_tchf_dl_amr_cmi_map[br->fn % 26];
+ else /* TRXC_TCHH_0 or TRXC_TCHH_1 */
+ amr_is_cmr = !sched_tchh_dl_amr_cmi_map[br->fn % 26];
+ if (amr_is_cmr && chan_state->dl_ft != ft) {
LOGL1SB(DL1P, LOGL_NOTICE, l1ts, br, "Codec (FT = %d) "
" of RTP cannot be changed now, but in next frame\n", ft_codec);
goto free_bad_msg;
@@ -473,100 +447,230 @@ inval_mode1:
LOGL1SB(DL1P, LOGL_NOTICE, l1ts, br, "Transmitting 'bad AMR frame'\n");
goto free_bad_msg;
}
+ /* pull the AMR header, it's not being sent over Um */
+ (*msg_tch)->l2h += sizeof(struct amr_hdr);
+ len -= sizeof(struct amr_hdr);
+ break;
+ case GSM48_CMODE_DATA_14k5: /* TCH/F14.4 */
+ if (OSMO_UNLIKELY(br->chan != TRXC_TCHF))
+ goto inval_mode2;
+ len = 290;
+ break;
+ case GSM48_CMODE_DATA_12k0: /* TCH/F9.6 */
+ if (OSMO_UNLIKELY(br->chan != TRXC_TCHF))
+ goto inval_mode2;
+ len = 4 * 60;
+ break;
+ case GSM48_CMODE_DATA_6k0: /* TCH/[FH]4.8 */
+ if (br->chan == TRXC_TCHF)
+ len = 2 * 60;
+ else
+ len = 4 * 60;
+ break;
+ case GSM48_CMODE_DATA_3k6: /* TCH/[FH]2.4 */
+ if (br->chan == TRXC_TCHF)
+ len = 2 * 36;
+ else
+ len = 4 * 36;
break;
default:
inval_mode2:
LOGL1SB(DL1P, LOGL_ERROR, l1ts, br, "TCH mode invalid, please fix!\n");
goto free_bad_msg;
}
- if (len < 0) {
- LOGL1SB(DL1P, LOGL_ERROR, l1ts, br, "Cannot send invalid AMR payload\n");
- goto free_bad_msg;
- }
- if (msgb_l2len(msg_tch) != len) {
+ if (msgb_l2len(*msg_tch) != len) {
LOGL1SB(DL1P, LOGL_ERROR, l1ts, br, "Cannot send payload with "
"invalid length! (expecting %d, received %d)\n",
- len, msgb_l2len(msg_tch));
+ len, msgb_l2len(*msg_tch));
free_bad_msg:
/* free message */
- msgb_free(msg_tch);
- msg_tch = NULL;
- goto send_frame;
+ msgb_free(*msg_tch);
+ *msg_tch = NULL;
}
}
+}
+
+struct msgb *tch_dummy_msgb(size_t size, uint8_t pad)
+{
+ struct msgb *msg;
+
+ msg = msgb_alloc(size, __func__);
+ OSMO_ASSERT(msg != NULL);
-send_frame:
- *_msg_tch = msg_tch;
- *_msg_facch = msg_facch;
+ msg->l2h = msgb_put(msg, size);
+ memset(msg->l2h, pad, size);
+
+ return msg;
}
/* obtain a to-be-transmitted TCH/F (Full Traffic Channel) burst */
int tx_tchf_fn(struct l1sched_ts *l1ts, struct trx_dl_burst_req *br)
{
- struct msgb *msg_tch = NULL, *msg_facch = NULL;
struct l1sched_chan_state *chan_state = &l1ts->chan_state[br->chan];
uint8_t tch_mode = chan_state->tch_mode;
- ubit_t *burst, **bursts_p = &chan_state->dl_bursts;
+ ubit_t *burst, *bursts_p = chan_state->dl_bursts;
+ uint8_t *mask = &chan_state->dl_mask;
+ struct msgb *msg_facch = NULL;
+ struct msgb *msg_tch = NULL;
+ struct msgb *msg = NULL;
/* send burst, if we already got a frame */
if (br->bid > 0) {
- if (!*bursts_p)
- return 0;
+ if ((*mask & 0x01) != 0x01)
+ return -ENOMSG;
goto send_burst;
}
- tx_tch_common(l1ts, br, &msg_tch, &msg_facch);
+ *mask = *mask << 4;
/* BURST BYPASS */
- /* allocate burst memory, if not already,
- * otherwise shift buffer by 4 bursts for interleaving */
- if (!*bursts_p) {
- *bursts_p = talloc_zero_size(tall_bts_ctx, 928);
- if (!*bursts_p)
- return -ENOMEM;
- } else {
- memcpy(*bursts_p, *bursts_p + 464, 464);
- memset(*bursts_p + 464, 0, 464);
- }
+ /* shift buffer by 4 bursts for interleaving */
+ memmove(BUFPOS(bursts_p, 0), BUFPOS(bursts_p, 4), 20 * BPLEN);
+ memset(BUFPOS(bursts_p, 20), 0, 4 * BPLEN);
+
+ /* dequeue a TCH and/or a FACCH message to be transmitted */
+ tch_dl_dequeue(l1ts, br, &msg_tch, &msg_facch);
+ if (msg_tch == NULL && msg_facch == NULL) {
+ int rc;
+
+ LOGL1SB(DL1P, LOGL_DEBUG, l1ts, br, "No TCH or FACCH prim for transmit.\n");
+ /* - If the channel mode is TCH/FS or TCH/EFS, transmit a dummy
+ * speech block with inverted CRC3, designed to induce a BFI
+ * condition in the MS receiver.
+ * - If the channel mode is TCH/AFS, transmit a dummy speech
+ * block with inverted CRC6, designed to induce a BFI
+ * condition in the MS receiver.
+ * - If the channel mode is one of the CSD modes, transmit an
+ * idle frame as described in 3GPP TS 44.021, sections 8.1.6
+ * and 10.2.3 (all data, status and E-bits set to binary '1').
+ * - In all other channel modes, transmit dummy FACCH
+ * like we always did before.
+ */
+ switch (tch_mode) {
+ case GSM48_CMODE_DATA_12k0:
+ case GSM48_CMODE_DATA_6k0:
+ case GSM48_CMODE_DATA_3k6:
+ case GSM48_CMODE_DATA_14k5:
+ break; /* see below */
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ rc = gsm0503_tch_fr_encode(BUFPOS(bursts_p, 0), NULL, 0, 1);
+ if (rc == 0)
+ goto send_burst;
+ /* fall-through */
+ case GSM48_CMODE_SIGN:
+ default:
+ if (tch_mode == GSM48_CMODE_SPEECH_AMR) {
+ /* the first FN 4,13,21 defines that CMI is included in frame,
+ * the first FN 0,8,17 defines that CMR is included in frame.
+ */
+ rc = gsm0503_tch_afs_encode(BUFPOS(bursts_p, 0),
+ NULL, 0,
+ !sched_tchf_dl_amr_cmi_map[br->fn % 26],
+ chan_state->codec,
+ chan_state->codecs,
+ chan_state->dl_ft,
+ chan_state->dl_cmr);
+ if (rc == 0)
+ goto send_burst;
+ }
- /* no message at all */
- if (!msg_tch && !msg_facch) {
- LOGL1SB(DL1P, LOGL_INFO, l1ts, br, "No TCH or FACCH prim for transmit.\n");
- goto send_burst;
+ /* TODO: use randomized padding */
+ msg_facch = tch_dummy_msgb(GSM_MACBLOCK_LEN, GSM_MACBLOCK_PADDING);
+ /* dummy LAPDm func=UI frame */
+ msg_facch->l2h[0] = 0x03;
+ msg_facch->l2h[1] = 0x03;
+ msg_facch->l2h[2] = 0x01;
+ break;
+ }
}
- /* encode bursts (prioritize FACCH) */
- if (msg_facch)
- gsm0503_tch_fr_encode(*bursts_p, msg_facch->l2h, msgb_l2len(msg_facch),
- 1);
- else if (tch_mode == GSM48_CMODE_SPEECH_AMR)
+ /* Unlike SACCH, FACCH has no dedicated slots on the multiframe layout.
+ * It's multiplexed together with TCH (speech or data) frames basically
+ * by replacing (stealing) their bits, either completely or partly. */
+ msg = (msg_facch != NULL) ? msg_facch : msg_tch;
+ if (msg == msg_facch)
+ chan_state->dl_facch_bursts = 8;
+
+ /* populate the buffer with bursts */
+ switch (tch_mode) {
+ case GSM48_CMODE_SIGN:
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ gsm0503_tch_fr_encode(BUFPOS(bursts_p, 0), msg->l2h, msgb_l2len(msg), 1);
+ break;
+ case GSM48_CMODE_SPEECH_AMR:
/* the first FN 4,13,21 defines that CMI is included in frame,
* the first FN 0,8,17 defines that CMR is included in frame.
*/
- gsm0503_tch_afs_encode(*bursts_p, msg_tch->l2h + 2,
- msgb_l2len(msg_tch) - 2, fn_is_codec_mode_request(br->fn),
- chan_state->codec, chan_state->codecs,
- chan_state->dl_ft,
- chan_state->dl_cmr);
- else
- gsm0503_tch_fr_encode(*bursts_p, msg_tch->l2h, msgb_l2len(msg_tch), 1);
-
- /* free message */
- if (msg_tch)
- msgb_free(msg_tch);
- if (msg_facch)
- msgb_free(msg_facch);
+ gsm0503_tch_afs_encode(BUFPOS(bursts_p, 0),
+ msgb_l2(msg), msgb_l2len(msg),
+ !sched_tchf_dl_amr_cmi_map[br->fn % 26],
+ chan_state->codec,
+ chan_state->codecs,
+ chan_state->dl_ft,
+ chan_state->dl_cmr);
+ break;
+ /* CSD (TCH/F9.6): 12.0 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_12k0:
+ if (msg_tch == NULL)
+ msg_tch = tch_dummy_msgb(4 * 60, 0x01);
+ gsm0503_tch_fr96_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_tch));
+ if (msg_facch != NULL)
+ gsm0503_tch_fr_facch_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_facch));
+ break;
+ /* CSD (TCH/F4.8): 6.0 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_6k0:
+ if (msg_tch == NULL)
+ msg_tch = tch_dummy_msgb(2 * 60, 0x01);
+ gsm0503_tch_fr48_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_tch));
+ if (msg_facch != NULL)
+ gsm0503_tch_fr_facch_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_facch));
+ break;
+ /* CSD (TCH/F2.4): 3.6 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_3k6:
+ /* FACCH/F does steal a TCH/F2.4 frame completely */
+ if (msg_facch != NULL) {
+ gsm0503_tch_fr_facch_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_facch));
+ } else {
+ if (msg_tch == NULL)
+ msg_tch = tch_dummy_msgb(2 * 36, 0x01);
+ gsm0503_tch_fr24_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_tch));
+ }
+ break;
+ /* CSD (TCH/F14.4): 14.5 kbit/s radio interface rate */
+ case GSM48_CMODE_DATA_14k5:
+ if (msg_tch == NULL)
+ msg_tch = tch_dummy_msgb(290, 0x01);
+ gsm0503_tch_fr144_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_tch));
+ if (msg_facch != NULL)
+ gsm0503_tch_fr_facch_encode(BUFPOS(bursts_p, 0), msgb_l2(msg_facch));
+ break;
+ default:
+ OSMO_ASSERT(0);
+ }
+
+ /* free messages */
+ msgb_free(msg_tch);
+ msgb_free(msg_facch);
send_burst:
/* compose burst */
- burst = *bursts_p + br->bid * 116;
+ burst = BUFPOS(bursts_p, br->bid);
memcpy(br->burst + 3, burst, 58);
memcpy(br->burst + 61, TRX_GMSK_NB_TSC(br), 26);
memcpy(br->burst + 87, burst + 58, 58);
br->burst_len = GSM_BURST_LEN;
+ if (chan_state->dl_facch_bursts > 0) {
+ chan_state->dl_facch_bursts--;
+ br->flags |= TRX_BR_F_FACCH;
+ }
+
+ *mask |= (1 << br->bid);
+
LOGL1SB(DL1P, LOGL_DEBUG, l1ts, br, "Transmitting burst=%u.\n", br->bid);
return 0;