falconia submitted this change.

View Change

Approvals: fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified pespin: Looks good to me, approved
gsm: new struct and en/decoding functions for BC-IE

Legacy struct gsm_mncc_bearer_cap is unable to represent 14.4 kbit/s
mode of CSD, which is now supported by Osmocom BSS, and even for
the mandatory set of BC-IE octets for all data modes, it lacks
some of the bits. Introduce a new set of structures to serve
as a replacement, as well as new encoding and decoding functions
using the new structs.

These new structures and functions were originally developed by
American 2G Cooperative as part of Hera MSC back end, interfacing
to OsmoMSC via the proposed MNCC raw mode and doing our own encoding
and decoding of GSM 04.08 CC messages. The present patch is a small
step toward making these ThemWi functional improvements available
in pure Osmocom, in addition to Osmocom+ThemWi hybrid. The structures
and functions have also been extended to be usable on the mobile side
of GSM, in addition to the network side.

Change-Id: Ie864734fe1dfd1587bafddd4248ce69a7e066525
---
M TODO-RELEASE
M include/osmocom/gsm/gsm48_ie.h
M src/gsm/gsm48_ie.c
M src/gsm/libosmogsm.map
4 files changed, 678 insertions(+), 4 deletions(-)

diff --git a/TODO-RELEASE b/TODO-RELEASE
index 5f1904b..ac30d0a 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -10,3 +10,4 @@
vty added VTY node for libsosmo-sigtran L_CS7_QOS_CLASS_NODE
vty added VTY nodes for libosmo-octoi L_OCTOI_SRV_NODE, L_OCTOI_ACCOUNT_NODE, L_OCTOI_CLNT_NODE, L_OCTOI_CLNT_ACCOUNT_NODE
core added osmo_sockaddr_pton()
+gsm added osmo_gsm48_decode_bcap(), osmo_gsm48_encode_bcap(), osmo_gsm48_bcap_apply_ms_rules()
diff --git a/include/osmocom/gsm/gsm48_ie.h b/include/osmocom/gsm/gsm48_ie.h
index 4768283..0f006eb 100644
--- a/include/osmocom/gsm/gsm48_ie.h
+++ b/include/osmocom/gsm/gsm48_ie.h
@@ -2,16 +2,98 @@

#pragma once

+#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

+#include <osmocom/core/bits.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/defs.h>
#include <osmocom/gsm/tlv.h>
#include <osmocom/gsm/mncc.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>

+/* The following structures are intended as a replacement for
+ * legacy struct gsm_mncc_bearer_cap. */
+
+/* Versions of 3GPP TS 24.008 that correspond to the final round
+ * of enhancements to GSM define a total of 9 different GSM speech
+ * versions - see Table 10.5.103/3GPP TS 24.008. Therefore,
+ * we should support this many speech versions in our decoded
+ * BC-IE data structure. */
+#define OSMO_BCAP_MAX_SPEECH_VER 9
+
+struct osmo_bcap_speech {
+ bool ctm;
+ uint8_t vers[OSMO_BCAP_MAX_SPEECH_VER];
+ uint8_t num_vers;
+};
+
+struct osmo_bcap_data {
+ /* fields from octet 4 */
+ ubit_t compress;
+ uint8_t structure;
+ ubit_t duplex;
+ ubit_t config;
+ ubit_t nirr;
+ ubit_t establish;
+ /* fields from octet 5 */
+ uint8_t rate_adaption;
+ uint8_t sig_access;
+ /* fields from octet 5a */
+ uint8_t other_itc;
+ uint8_t other_ra;
+ /* fields from octet 5b */
+ uint8_t v120_params;
+ /* fields from octet 6 */
+ uint8_t uil1p;
+ ubit_t async;
+ /* fields from octet 6a */
+ ubit_t stop_bits_ind;
+ ubit_t v110_negot;
+ ubit_t data_bits_ind;
+ uint8_t user_rate;
+ /* fields from octet 6b */
+ uint8_t interm_rate;
+ ubit_t nic_tx;
+ ubit_t nic_rx;
+ uint8_t parity;
+ /* fields from octet 6c */
+ uint8_t transp;
+ uint8_t modem_type;
+ /* fields from octet 6d */
+ uint8_t other_modem_type;
+ uint8_t fnur;
+ /* fields from octet 6e */
+ ubit_t accept_tchf144;
+ ubit_t accept_tchf96;
+ ubit_t accept_tchf48;
+ uint8_t max_tch_minus1;
+ /* fields from octet 6f */
+ uint8_t uimi;
+ uint8_t waiur;
+ /* fields from octet 6g */
+ ubit_t accept_tch288;
+ ubit_t accept_tch320;
+ ubit_t accept_tch432;
+ uint8_t asymmetry;
+ /* fields from octet 7 */
+ uint8_t uil2p;
+ bool have_uil2p;
+};
+
+struct osmo_bearer_cap {
+ uint8_t itc;
+ ubit_t tmod;
+ ubit_t coding;
+ uint8_t rrq;
+ union {
+ struct osmo_bcap_speech speech;
+ struct osmo_bcap_data data;
+ };
+};
+
/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
int gsm48_decode_bcd_number(char *output, int output_len,
const uint8_t *bcd_lv, int h_len)
@@ -25,10 +107,17 @@
int h_len, const char *input);
/* decode 'bearer capability' */
int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
- const uint8_t *lv);
+ const uint8_t *lv)
+ OSMO_DEPRECATED("Use osmo_gsm48_decode_bcap() instead");
+int osmo_gsm48_decode_bcap(struct osmo_bearer_cap *bc, const uint8_t *ie_val,
+ uint8_t ie_len, bool for_ms);
/* encode 'bearer capability' */
int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
- const struct gsm_mncc_bearer_cap *bcap);
+ const struct gsm_mncc_bearer_cap *bcap)
+ OSMO_DEPRECATED("Use osmo_gsm48_encode_bcap() instead");
+int osmo_gsm48_encode_bcap(struct msgb *msg, bool lv_only,
+ const struct osmo_bearer_cap *bc, bool for_ms);
+int osmo_gsm48_bcap_apply_ms_rules(struct osmo_bearer_cap *bc);
/* decode 'call control cap' */
int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv);
/* encode 'call control cap' */
diff --git a/src/gsm/gsm48_ie.c b/src/gsm/gsm48_ie.c
index 866e0da..09c0d50 100644
--- a/src/gsm/gsm48_ie.c
+++ b/src/gsm/gsm48_ie.c
@@ -185,7 +185,7 @@
return (bcd_cur - bcd_lv);
}

-/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
+/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5), legacy version.
* \param[out] bcap Caller-provided memory for decoded output
* \param[in] lv LV portion of TS 04.08 Bearer Capability
* \returns 0 on success; negative on error */
@@ -315,7 +315,7 @@
return 0;
}

-/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
+/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5), legacy version.
* \param[out] msg Message Buffer to which IE is to be appended
* \param[in] lv_only Write only LV portion (1) or TLV (0)
* \param[in] bcap Decoded Bearer Capability to be encoded
@@ -392,6 +392,587 @@
return 0;
}

+/* Low-level decoding of BC-IE data portion */
+
+#define OSMO_BCAP_OCT5_MAX_EXT 2
+#define OSMO_BCAP_OCT6_MAX_EXT 7
+
+struct osmo_bcap_data_ll {
+ uint8_t octet_4;
+ uint8_t octet_5;
+ uint8_t octet_5_ext[OSMO_BCAP_OCT5_MAX_EXT];
+ uint8_t octet_6;
+ uint8_t octet_6_ext[OSMO_BCAP_OCT6_MAX_EXT];
+ uint8_t octet_7;
+ uint8_t extent_of_5;
+ uint8_t extent_of_6;
+ bool have_octet_7;
+};
+
+/* Low-level decoding of the complete BC-IE */
+
+struct osmo_bcap_ll {
+ uint8_t itc;
+ ubit_t tmod;
+ ubit_t coding;
+ uint8_t rrq;
+ union {
+ struct osmo_bcap_speech speech;
+ struct osmo_bcap_data_ll data;
+ };
+};
+
+/*! Low-level decode bearer capability IE of 3GPP TS 24.008 section 10.5.4.5.
+ * \param[out] lld Caller-provided memory for decoded output.
+ * \param[in] ie_val Value part of the received BC-IE.
+ * \param[in] ie_len Length of the received BC-IE value part.
+ * \returns 0 on success; negative on error.
+ */
+static int decode_bcap_ll(struct osmo_bcap_ll *lld, const uint8_t *ie_val,
+ uint8_t ie_len)
+{
+ const uint8_t *cp = ie_val;
+ const uint8_t *endp = ie_val + ie_len;
+
+ memset(lld, 0, sizeof(struct osmo_bcap_ll));
+ if (cp >= endp) /* octet 3 is always mandatory */
+ return -EINVAL;
+ lld->rrq = (*cp & 0x60) >> 5;
+ lld->coding = (*cp & 0x10) >> 4;
+ lld->tmod = (*cp & 0x08) >> 3;
+ lld->itc = *cp & 0x07;
+ switch (lld->itc) {
+ case GSM48_BCAP_ITCAP_SPEECH:
+ while (!(*cp & 0x80)) {
+ cp++;
+ if (cp >= endp)
+ return -EINVAL;
+ if (*cp & 0x40) /* not a speech version octet */
+ continue;
+ if ((cp == ie_val + 1) && (*cp & 0x20))
+ lld->speech.ctm = true;
+ if (lld->speech.num_vers >= OSMO_BCAP_MAX_SPEECH_VER)
+ break;
+ lld->speech.vers[lld->speech.num_vers++] = *cp & 0x0F;
+ }
+ break;
+ case GSM48_BCAP_ITCAP_UNR_DIG_INF:
+ case GSM48_BCAP_ITCAP_3k1_AUDIO:
+ case GSM48_BCAP_ITCAP_FAX_G3:
+ case GSM48_BCAP_ITCAP_OTHER:
+ /* There are no octets 3a etc for data, but the spec tells us
+ * to ignore them if received. */
+ while (!(*cp & 0x80)) {
+ cp++;
+ if (cp >= endp)
+ return -EINVAL;
+ }
+ cp++;
+ if (cp >= endp) /* octet 4 is mandatory for data */
+ return -EINVAL;
+ if (!(*cp & 0x80)) /* ... and does not allow extension */
+ return -EINVAL;
+ lld->data.octet_4 = *cp++;
+ if (cp >= endp) /* octet 5 is mandatory too */
+ return -EINVAL;
+ if ((*cp & 0x60) != 0x00) /* verify that it really is octet 5 */
+ return -EINVAL;
+ lld->data.octet_5 = *cp;
+ while (!(*cp & 0x80)) {
+ cp++;
+ if (cp >= endp)
+ return -EINVAL;
+ if (lld->data.extent_of_5 >= OSMO_BCAP_OCT5_MAX_EXT)
+ return -EINVAL;
+ lld->data.octet_5_ext[lld->data.extent_of_5++] = *cp;
+ }
+ cp++;
+ if (cp >= endp) /* octet 6 is mandatory */
+ return -EINVAL;
+ if ((*cp & 0x60) != 0x20) /* verify that it really is octet 6 */
+ return -EINVAL;
+ lld->data.octet_6 = *cp;
+ while (!(*cp & 0x80)) {
+ cp++;
+ if (cp >= endp)
+ return -EINVAL;
+ if (lld->data.extent_of_6 >= OSMO_BCAP_OCT6_MAX_EXT)
+ return -EINVAL;
+ lld->data.octet_6_ext[lld->data.extent_of_6++] = *cp;
+ }
+ cp++;
+ if (cp >= endp) /* octet 7 is optional */
+ break;
+ if ((*cp & 0x60) != 0x40) /* verify that it really is octet 7 */
+ return -EINVAL;
+ if (!(*cp & 0x80)) /* it does not allow extension */
+ return -EINVAL;
+ lld->data.have_octet_7 = true;
+ lld->data.octet_7 = *cp;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ return 0;
+}
+
+/*! Low-level encode bearer capability IE of 3GPP TS 24.008 section 10.5.4.5.
+ * \param[out] msg Message buffer to which BC-IE is to be appended.
+ * \param[in] lv_only Write only LV portion (true) or TLV (false).
+ * \param[in] lld Low-level decoded BC-IE to be encoded.
+ * \returns 0 on success; negative on error.
+ */
+static int encode_bcap_ll(struct msgb *msg, bool lv_only,
+ const struct osmo_bcap_ll *lld)
+{
+ uint8_t buf[14]; /* max len with all optional octets included */
+ uint8_t *cp = buf;
+ uint8_t octet_3 = 0;
+ uint8_t n, len, space_needed;
+
+ octet_3 |= lld->rrq << 5;
+ octet_3 |= lld->coding << 4;
+ octet_3 |= lld->tmod << 3;
+ octet_3 |= lld->itc;
+ switch (lld->itc) {
+ case GSM48_BCAP_ITCAP_SPEECH:
+ if (lld->speech.num_vers > OSMO_BCAP_MAX_SPEECH_VER)
+ return -EINVAL;
+ for (n = 0; n < lld->speech.num_vers; n++) {
+ *cp++ = octet_3;
+ octet_3 = lld->speech.vers[n];
+ if (n == 0 && lld->speech.ctm)
+ octet_3 |= 0x20;
+ }
+ octet_3 |= 0x80;
+ *cp++ = octet_3;
+ break;
+ case GSM48_BCAP_ITCAP_UNR_DIG_INF:
+ case GSM48_BCAP_ITCAP_3k1_AUDIO:
+ case GSM48_BCAP_ITCAP_FAX_G3:
+ case GSM48_BCAP_ITCAP_OTHER:
+ if (lld->data.extent_of_5 > OSMO_BCAP_OCT5_MAX_EXT)
+ return -EINVAL;
+ if (lld->data.extent_of_6 > OSMO_BCAP_OCT6_MAX_EXT)
+ return -EINVAL;
+ octet_3 |= 0x80;
+ *cp++ = octet_3;
+ *cp++ = lld->data.octet_4;
+ *cp++ = lld->data.octet_5;
+ for (n = 0; n < lld->data.extent_of_5; n++)
+ *cp++ = lld->data.octet_5_ext[n];
+ *cp++ = lld->data.octet_6;
+ for (n = 0; n < lld->data.extent_of_6; n++)
+ *cp++ = lld->data.octet_6_ext[n];
+ if (lld->data.have_octet_7)
+ *cp++ = lld->data.octet_7;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ len = cp - buf;
+ space_needed = lv_only ? len + 1 : len + 2;
+ if (msgb_tailroom(msg) < space_needed)
+ return -ENOSPC;
+ if (lv_only)
+ msgb_lv_put(msg, len, buf);
+ else
+ msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, len, buf);
+ return 0;
+}
+
+static void bcap_speech_dec_phase1_logic(struct osmo_bearer_cap *bc)
+{
+ switch (bc->rrq) {
+ case GSM48_BCAP_RRQ_FR_ONLY:
+ bc->speech.vers[0] = GSM48_BCAP_SV_FR;
+ bc->speech.num_vers = 1;
+ break;
+ case GSM48_BCAP_RRQ_DUAL_HR:
+ bc->speech.vers[0] = GSM48_BCAP_SV_HR;
+ bc->speech.vers[1] = GSM48_BCAP_SV_FR;
+ bc->speech.num_vers = 2;
+ break;
+ case GSM48_BCAP_RRQ_DUAL_FR:
+ bc->speech.vers[0] = GSM48_BCAP_SV_FR;
+ bc->speech.vers[1] = GSM48_BCAP_SV_HR;
+ bc->speech.num_vers = 2;
+ break;
+ }
+}
+
+static void bcap_hl_decode_data(struct osmo_bcap_data *hld,
+ const struct osmo_bcap_data_ll *lld)
+{
+ /* fields from octet 4 */
+ hld->compress = (lld->octet_4 & 0x40) >> 6;
+ hld->structure = (lld->octet_4 & 0x30) >> 4;
+ hld->duplex = (lld->octet_4 & 0x08) >> 3;
+ hld->config = (lld->octet_4 & 0x04) >> 2;
+ hld->nirr = (lld->octet_4 & 0x02) >> 1;
+ hld->establish = lld->octet_4 & 0x01;
+ /* fields from octet 5 */
+ hld->rate_adaption = (lld->octet_5 & 0x18) >> 3;
+ hld->sig_access = lld->octet_5 & 0x07;
+ /* fields from octet 5a */
+ if (lld->extent_of_5 >= 1) {
+ hld->other_itc = (lld->octet_5_ext[0] & 0x60) >> 5;
+ hld->other_ra = (lld->octet_5_ext[0] & 0x18) >> 3;
+ }
+ /* fields from octet 5b */
+ if (lld->extent_of_5 >= 2)
+ hld->v120_params = lld->octet_5_ext[1] & 0x7F;
+ /* fields from octet 6 */
+ hld->uil1p = (lld->octet_6 & 0x1E) >> 1;
+ hld->async = lld->octet_6 & 0x01;
+ /* fields from octet 6a */
+ hld->stop_bits_ind = (lld->octet_6_ext[0] & 0x40) >> 6;
+ hld->v110_negot = (lld->octet_6_ext[0] & 0x20) >> 5;
+ hld->data_bits_ind = (lld->octet_6_ext[0] & 0x10) >> 4;
+ hld->user_rate = lld->octet_6_ext[0] & 0x0F;
+ /* fields from octet 6b */
+ hld->interm_rate = (lld->octet_6_ext[1] & 0x60) >> 5;
+ hld->nic_tx = (lld->octet_6_ext[1] & 0x10) >> 4;
+ hld->nic_rx = (lld->octet_6_ext[1] & 0x08) >> 3;
+ hld->parity = lld->octet_6_ext[1] & 0x07;
+ /* fields from octet 6c */
+ hld->transp = (lld->octet_6_ext[2] & 0x60) >> 5;
+ hld->modem_type = lld->octet_6_ext[2] & 0x1F;
+ /* fields from octet 6d */
+ if (lld->extent_of_6 >= 4) {
+ hld->other_modem_type = (lld->octet_6_ext[3] & 0x60) >> 5;
+ hld->fnur = lld->octet_6_ext[3] & 0x1F;
+ }
+ /* fields from octet 6e */
+ if (lld->extent_of_6 >= 5) {
+ hld->accept_tchf144 = (lld->octet_6_ext[4] & 0x40) >> 6;
+ hld->accept_tchf96 = (lld->octet_6_ext[4] & 0x10) >> 4;
+ hld->accept_tchf48 = (lld->octet_6_ext[4] & 0x08) >> 3;
+ hld->max_tch_minus1 = lld->octet_6_ext[4] & 0x07;
+ }
+ /* fields from octet 6f */
+ if (lld->extent_of_6 >= 6) {
+ hld->uimi = (lld->octet_6_ext[5] & 0x70) >> 4;
+ hld->waiur = lld->octet_6_ext[5] & 0x0F;
+ }
+ /* fields from octet 6g */
+ if (lld->extent_of_6 >= 7) {
+ hld->accept_tch288 = (lld->octet_6_ext[6] & 0x40) >> 6;
+ hld->accept_tch320 = (lld->octet_6_ext[6] & 0x20) >> 5;
+ hld->accept_tch432 = (lld->octet_6_ext[6] & 0x10) >> 4;
+ hld->asymmetry = (lld->octet_6_ext[6] & 0x0C) >> 2;
+ }
+ /* fields from octet 7 */
+ if (lld->have_octet_7) {
+ hld->uil2p = lld->octet_7 & 0x1F;
+ hld->have_uil2p = true;
+ }
+}
+
+/*! High-level decode bearer capability IE of 3GPP TS 24.008 section 10.5.4.5.
+ * \param[out] bc Caller-provided memory for decoded output.
+ * \param[in] ie_val Value part of the received BC-IE.
+ * \param[in] ie_len Length of the received BC-IE value part.
+ * \param[in] for_ms True if decoding is done for GSM MS (network to MS
+ * direction) or false if decoding is done for an MSC (MS to network dir).
+ * \returns 0 on success; negative on error.
+ */
+int osmo_gsm48_decode_bcap(struct osmo_bearer_cap *bc, const uint8_t *ie_val,
+ uint8_t ie_len, bool for_ms)
+{
+ struct osmo_bcap_ll lld;
+ int rc;
+
+ memset(bc, 0, sizeof(struct osmo_bearer_cap));
+ rc = decode_bcap_ll(&lld, ie_val, ie_len);
+ if (rc < 0)
+ return rc;
+ bc->itc = lld.itc;
+ bc->tmod = lld.tmod;
+ bc->coding = lld.coding;
+ bc->rrq = lld.rrq;
+ switch (lld.itc) {
+ case GSM48_BCAP_ITCAP_SPEECH:
+ /* Table 10.5.103/3GPP TS 24.008, note at the very bottom
+ * of the table:
+ *
+ * Octet(s) 3a etc. network to MS direction
+ *
+ * The octet(s) 3a etc. shall be ignored by the MS.
+ *
+ * Given this strong "shall" language in the spec, block
+ * the passage of this speech version list in high-level
+ * decoding on behalf of MS, to prevent misunderstanding
+ * errors by downstream developers using this API.
+ *
+ * Furthermore if this "artificial suppression" logic
+ * were removed, bcap_speech_dec_phase1_logic() would get
+ * called in the normal case when the network sends no
+ * speech version list, and would give the MS application
+ * a misleading impression that the network specifically
+ * requested FRv1.
+ */
+ if (!for_ms) {
+ if (lld.speech.num_vers != 0) {
+ memcpy(&bc->speech, &lld.speech,
+ sizeof(struct osmo_bcap_speech));
+ } else {
+ bcap_speech_dec_phase1_logic(bc);
+ }
+ }
+ break;
+ case GSM48_BCAP_ITCAP_UNR_DIG_INF:
+ case GSM48_BCAP_ITCAP_3k1_AUDIO:
+ case GSM48_BCAP_ITCAP_FAX_G3:
+ case GSM48_BCAP_ITCAP_OTHER:
+ /* octets 6a, 6b and 6c are mandatory */
+ if (lld.data.extent_of_6 < 3)
+ return -EINVAL;
+ bcap_hl_decode_data(&bc->data, &lld.data);
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ return 0;
+}
+
+static void bcap_enc_trim_6d_6g(struct osmo_bcap_data_ll *lld,
+ bool ms_rules_6d_6e)
+{
+ if (lld->octet_6_ext[6]) {
+ lld->octet_6_ext[6] |= 0x80;
+ lld->extent_of_6 = 7;
+ return;
+ }
+ if (lld->octet_6_ext[5]) {
+ lld->octet_6_ext[5] |= 0x80;
+ lld->extent_of_6 = 6;
+ return;
+ }
+ if (lld->octet_6_ext[4]) {
+ lld->octet_6_ext[4] |= 0x80;
+ lld->extent_of_6 = 5;
+ return;
+ }
+ if (lld->octet_6_ext[3]) {
+ if (ms_rules_6d_6e) {
+ lld->octet_6_ext[4] |= 0x80;
+ lld->extent_of_6 = 5;
+ } else {
+ lld->octet_6_ext[3] |= 0x80;
+ lld->extent_of_6 = 4;
+ }
+ return;
+ }
+ lld->octet_6_ext[2] |= 0x80;
+ lld->extent_of_6 = 3;
+}
+
+static void bcap_hl_encode_data(struct osmo_bcap_data_ll *lld,
+ const struct osmo_bcap_data *hld,
+ enum gsm48_bcap_itcap itcap, bool for_ms)
+{
+ lld->octet_4 = 0x80;
+ lld->octet_4 |= hld->compress << 6;
+ lld->octet_4 |= hld->structure << 4;
+ lld->octet_4 |= hld->duplex << 3;
+ lld->octet_4 |= hld->config << 2;
+ lld->octet_4 |= hld->nirr << 1;
+ lld->octet_4 |= hld->establish;
+ lld->octet_5 = 0;
+ lld->octet_5 |= hld->rate_adaption << 3;
+ lld->octet_5 |= hld->sig_access;
+ if (itcap == GSM48_BCAP_ITCAP_OTHER ||
+ hld->rate_adaption == GSM48_BCAP_RA_OTHER) {
+ lld->octet_5_ext[0] = 0;
+ lld->octet_5_ext[0] |= (hld->other_itc) << 5;
+ lld->octet_5_ext[0] |= (hld->other_ra) << 3;
+ if (hld->rate_adaption == GSM48_BCAP_RA_OTHER &&
+ hld->other_ra == GSM48_BCAP_OTHER_RA_V120) {
+ lld->octet_5_ext[1] = 0x80 | hld->v120_params;
+ lld->extent_of_5 = 2;
+ } else {
+ lld->octet_5_ext[0] |= 0x80;
+ lld->extent_of_5 = 1;
+ }
+ } else {
+ lld->octet_5 |= 0x80;
+ lld->extent_of_5 = 0;
+ }
+ lld->octet_6 = 0x20;
+ lld->octet_6 |= hld->uil1p << 1;
+ lld->octet_6 |= hld->async;
+ lld->octet_6_ext[0] = 0;
+ lld->octet_6_ext[0] |= hld->stop_bits_ind << 6;
+ lld->octet_6_ext[0] |= hld->v110_negot << 5;
+ lld->octet_6_ext[0] |= hld->data_bits_ind << 4;
+ lld->octet_6_ext[0] |= hld->user_rate;
+ lld->octet_6_ext[1] = 0;
+ lld->octet_6_ext[1] |= hld->interm_rate << 5;
+ lld->octet_6_ext[1] |= hld->nic_tx << 4;
+ lld->octet_6_ext[1] |= hld->nic_rx << 3;
+ lld->octet_6_ext[1] |= hld->parity;
+ lld->octet_6_ext[2] = 0;
+ lld->octet_6_ext[2] |= hld->transp << 5;
+ lld->octet_6_ext[2] |= hld->modem_type;
+ /* Octets 6d through 6g: fill out all of them initially,
+ * then trim any unused ones. */
+ lld->octet_6_ext[3] = 0;
+ lld->octet_6_ext[3] |= hld->other_modem_type << 5;
+ lld->octet_6_ext[3] |= hld->fnur;
+ lld->octet_6_ext[4] = 0;
+ if (for_ms) {
+ lld->octet_6_ext[4] |= hld->accept_tchf144 << 6;
+ lld->octet_6_ext[4] |= hld->accept_tchf96 << 4;
+ lld->octet_6_ext[4] |= hld->accept_tchf48 << 3;
+ lld->octet_6_ext[4] |= hld->max_tch_minus1;
+ }
+ lld->octet_6_ext[5] = 0;
+ lld->octet_6_ext[5] |= hld->uimi << 4;
+ if (for_ms)
+ lld->octet_6_ext[5] |= hld->waiur;
+ lld->octet_6_ext[6] = 0;
+ if (for_ms) {
+ lld->octet_6_ext[6] |= hld->accept_tch288 << 6;
+ lld->octet_6_ext[6] |= hld->accept_tch320 << 5;
+ lld->octet_6_ext[6] |= hld->accept_tch432 << 4;
+ lld->octet_6_ext[6] |= hld->asymmetry << 2;
+ }
+ bcap_enc_trim_6d_6g(lld, for_ms);
+ if (hld->have_uil2p) {
+ lld->octet_7 = 0xC0 | hld->uil2p;
+ lld->have_octet_7 = true;
+ } else {
+ lld->have_octet_7 = false;
+ }
+}
+
+/*! High-level encode bearer capability IE of 3GPP TS 24.008 section 10.5.4.5.
+ * \param[out] msg Message buffer to which BC-IE is to be appended.
+ * \param[in] lv_only Write only LV portion (true) or TLV (false).
+ * \param[in] bc High-level decoded BC-IE to be encoded.
+ * \param[in] for_ms True if encoding is done for GSM MS (MS to network
+ * direction) or false if encoding is done for an MSC (network to MS dir).
+ * \returns 0 on success; negative on error.
+ */
+int osmo_gsm48_encode_bcap(struct msgb *msg, bool lv_only,
+ const struct osmo_bearer_cap *bc, bool for_ms)
+{
+ struct osmo_bcap_ll lld;
+
+ lld.itc = bc->itc;
+ lld.tmod = bc->tmod;
+ lld.coding = bc->coding;
+ if (for_ms)
+ lld.rrq = bc->rrq;
+ else
+ lld.rrq = GSM48_BCAP_RRQ_SPARE_NETWORK_TO_MS;
+ switch (lld.itc) {
+ case GSM48_BCAP_ITCAP_SPEECH:
+ if (for_ms) {
+ memcpy(&lld.speech, &bc->speech,
+ sizeof(struct osmo_bcap_speech));
+ } else {
+ memset(&lld.speech, 0, sizeof(struct osmo_bcap_speech));
+ }
+ break;
+ case GSM48_BCAP_ITCAP_UNR_DIG_INF:
+ case GSM48_BCAP_ITCAP_3k1_AUDIO:
+ case GSM48_BCAP_ITCAP_FAX_G3:
+ case GSM48_BCAP_ITCAP_OTHER:
+ bcap_hl_encode_data(&lld.data, &bc->data, lld.itc, for_ms);
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ return encode_bcap_ll(msg, lv_only, &lld);
+}
+
+/*! Apply rules for MS->network direction to GSM 04.08 bearer capability IE
+ * prior to encoding.
+ * \param[in/out] bc High-level decoded BC-IE to be preened.
+ * \returns 0 on success; negative on error.
+ *
+ * If the information transfer capability is anything other than speech,
+ * this function does nothing. However, if a speech call is to be made,
+ * the MS implementation calling this API needs to fill out the BC-IE
+ * structure as follows:
+ *
+ * - List all supported GSM speech codecs in the usual part of the struct,
+ * in whatever order of preference you wish to indicate, but with FRv1
+ * always included per GSM spec rules;
+ *
+ * - Leave the radio channel requirement field uninitialized.
+ *
+ * This function fills out the radio channel req field based on the indicated
+ * preference-ordered speech version list, and then conditionally deletes
+ * that speech version list if it includes only FRv1 and HRv1, as called for
+ * in 3GPP TS 24.008 section 10.5.4.5.1.
+ *
+ * A GSM MS application will typically first compose its desired BC-IE
+ * internally, then call osmo_gsm48_encode_bcap() to encode it. The present
+ * _optional_ API is intended for the composing stage, not the encoding stage,
+ * and it is optional in that some developer/research-oriented GSM MS
+ * may provide ability to intentionally violate these rules for network
+ * testing, in which case the call to the present function will be skipped.
+ */
+int osmo_gsm48_bcap_apply_ms_rules(struct osmo_bearer_cap *bc)
+{
+ int s;
+ int fr_pos = -1, hr_pos = -1;
+ bool newer_codecs = false;
+
+ /* preening is needed only for speech calls */
+ if (bc->itc != GSM48_BCAP_ITCAP_SPEECH)
+ return 0;
+
+ /* find FRv1 and HRv1 in the caller-filled speech version list */
+ for (s = 0; s < bc->speech.num_vers; s++) {
+ switch (bc->speech.vers[s]) {
+ case GSM48_BCAP_SV_FR:
+ if (fr_pos >= 0) /* duplicates invalid */
+ return -EINVAL;
+ fr_pos = s;
+ break;
+ case GSM48_BCAP_SV_HR:
+ if (hr_pos >= 0) /* duplicates invalid */
+ return -EINVAL;
+ hr_pos = s;
+ break;
+ default:
+ newer_codecs = true;
+ break;
+ }
+ }
+ /* FRv1 support is mandatory */
+ if (fr_pos < 0)
+ return -EINVAL;
+
+ /* Radio channel req bits are for ancient GSM networks that predate
+ * the addition of octets 3a etc: they indicate the v1-only subset
+ * of the full speech version list. */
+ if (hr_pos < 0)
+ bc->rrq = GSM48_BCAP_RRQ_FR_ONLY;
+ else if (fr_pos < hr_pos)
+ bc->rrq = GSM48_BCAP_RRQ_DUAL_FR;
+ else
+ bc->rrq = GSM48_BCAP_RRQ_DUAL_HR;
+
+ /* TS 24.008 section 10.5.4.5.1 says that octets 3a etc "shall"
+ * be included only if the MS supports either CTM text telephony
+ * or at least one newer-than-v1 speech codec. Other GSM MS
+ * implementations with available source code don't follow this
+ * stipulation, probably on the reasoning that if the code
+ * implementation is new enough to know about octets 3a etc,
+ * then the product it goes into will support at least EFR
+ * if not AMR - but let's honor the word of the spec
+ * for brownie points. */
+ if (!newer_codecs && !bc->speech.ctm)
+ bc->speech.num_vers = 0; /* empty list => no octet 3a */
+ return 0;
+}
+
/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
* \param[out] ccap Caller-provided memory for decoded CC capabilities
* \param[in] lv Length-Value of IE
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 88ee3ae..4c14793 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -436,6 +436,9 @@
gsm48_mi_to_string;
gsm48_chan_mode_to_vamos;
gsm48_chan_mode_to_non_vamos;
+osmo_gsm48_bcap_apply_ms_rules;
+osmo_gsm48_decode_bcap;
+osmo_gsm48_encode_bcap;
osmo_mobile_identity_to_str_buf;
osmo_mobile_identity_to_str_c;
osmo_mobile_identity_cmp;

To view, visit change 44192. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ie864734fe1dfd1587bafddd4248ce69a7e066525
Gerrit-Change-Number: 44192
Gerrit-PatchSet: 7
Gerrit-Owner: falconia <falcon@freecalypso.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: falconia <falcon@freecalypso.org>
Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: pespin <pespin@sysmocom.de>