summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/osmocom/gsm/gsm0808_utils.h8
-rw-r--r--include/osmocom/gsm/protocol/gsm_08_08.h9
-rw-r--r--src/gsm/gsm0808_utils.c73
-rw-r--r--src/gsm/libosmogsm.map2
-rw-r--r--tests/gsm0808/gsm0808_test.c31
5 files changed, 123 insertions, 0 deletions
diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
index b5ddbdb7..48e737d1 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -47,3 +47,11 @@ uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
/* Decode Speech Codec list */
int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
const uint8_t *elem, uint8_t len);
+
+/* Encode Channel Type element */
+uint8_t gsm0808_enc_channel_type(struct msgb *msg,
+ const struct gsm0808_channel_type *ct);
+
+/* Decode Channel Type element */
+int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
+ const uint8_t *elem, uint8_t len);
diff --git a/include/osmocom/gsm/protocol/gsm_08_08.h b/include/osmocom/gsm/protocol/gsm_08_08.h
index 3e5514dc..e2355f68 100644
--- a/include/osmocom/gsm/protocol/gsm_08_08.h
+++ b/include/osmocom/gsm/protocol/gsm_08_08.h
@@ -438,3 +438,12 @@ struct gsm0808_speech_codec_list {
struct gsm0808_speech_codec codec[SPEECH_CODEC_MAXLEN];
uint8_t len;
};
+
+/* 3GPP TS 48.008 3.2.2.11 Channel Type */
+#define CH_TYPE_PERM_SPCH_MAXLEN 9
+struct gsm0808_channel_type {
+ uint8_t ch_indctr;
+ uint8_t ch_rate_type;
+ uint8_t perm_spch[CH_TYPE_PERM_SPCH_MAXLEN];
+ unsigned int perm_spch_len;
+};
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index eef6146d..ef0f9431 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -31,6 +31,8 @@
#define IP_V6_ADDR_LEN 16
#define IP_PORT_LEN 2
+#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
+#define CHANNEL_TYPE_ELEMENT_MINLEN 3
/* Encode AoIP transport address element */
uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
@@ -306,3 +308,74 @@ int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
return (int)(elem - old_elem);
}
+
+/* Encode Channel Type element */
+uint8_t gsm0808_enc_channel_type(struct msgb *msg,
+ const struct gsm0808_channel_type *ct)
+{
+ unsigned int i;
+ uint8_t byte;
+ uint8_t *old_tail;
+ uint8_t *tlv_len;
+
+ OSMO_ASSERT(msg);
+ OSMO_ASSERT(ct);
+ OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
+
+ /* FIXME: Implement encoding support for Data
+ * and Speech + CTM Text Telephony */
+ if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
+ && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
+ OSMO_ASSERT(false);
+
+ msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
+ tlv_len = msgb_put(msg, 1);
+ old_tail = msg->tail;
+
+ msgb_put_u8(msg, ct->ch_indctr & 0x0f);
+ msgb_put_u8(msg, ct->ch_rate_type);
+
+ for (i = 0; i < ct->perm_spch_len; i++) {
+ byte = ct->perm_spch[i];
+
+ if (i < ct->perm_spch_len - 1)
+ byte |= 0x80;
+ msgb_put_u8(msg, byte);
+ }
+
+ *tlv_len = (uint8_t) (msg->tail - old_tail);
+ return *tlv_len + 2;
+}
+
+/* Decode Channel Type element */
+int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
+ const uint8_t *elem, uint8_t len)
+{
+ unsigned int i;
+ uint8_t byte;
+ const uint8_t *old_elem = elem;
+
+ OSMO_ASSERT(ct);
+ if (!elem)
+ return -EINVAL;
+ if (len <= 0)
+ return -EINVAL;
+
+ memset(ct, 0, sizeof(*ct));
+
+ ct->ch_indctr = (*elem) & 0x0f;
+ elem++;
+ ct->ch_rate_type = (*elem) & 0x0f;
+ elem++;
+
+ for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
+ byte = *elem;
+ elem++;
+ ct->perm_spch[i] = byte & 0x7f;
+ if ((byte & 0x80) == 0x00)
+ break;
+ }
+ ct->perm_spch_len = i + 1;
+
+ return (int)(elem - old_elem);
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 518a5aa8..2ba0c427 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -146,6 +146,8 @@ gsm0808_enc_speech_codec;
gsm0808_dec_speech_codec;
gsm0808_enc_speech_codec_list;
gsm0808_dec_speech_codec_list;
+gsm0808_enc_channel_type;
+gsm0808_dec_channel_type;
gsm0858_rsl_ul_meas_enc;
diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c
index 4a4a1084..0dd9e846 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -541,6 +541,36 @@ static void test_gsm0808_enc_dec_speech_codec_list()
msgb_free(msg);
}
+static void test_gsm0808_enc_dec_channel_type()
+{
+ struct gsm0808_channel_type enc_ct;
+ struct gsm0808_channel_type dec_ct;
+ struct msgb *msg;
+ uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
+ 0x04, 0x01, 0x0b, 0xa1, 0x25
+ };
+ uint8_t rc_enc;
+ int rc_dec;
+
+ memset(&enc_ct, 0, sizeof(enc_ct));
+ enc_ct.ch_indctr = GSM0808_CHAN_SPEECH;
+ enc_ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
+ enc_ct.perm_spch[0] = GSM0808_PERM_FR3;
+ enc_ct.perm_spch[1] = GSM0808_PERM_HR3;
+ enc_ct.perm_spch_len = 2;
+
+ msg = msgb_alloc(1024, "output buffer");
+ rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
+ OSMO_ASSERT(rc_enc == 6);
+ OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
+
+ rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
+ OSMO_ASSERT(rc_dec == 4);
+ OSMO_ASSERT(memcmp(&enc_ct, &dec_ct, sizeof(enc_ct)) == 0);
+
+ msgb_free(msg);
+}
+
int main(int argc, char **argv)
{
printf("Testing generation of GSM0808 messages\n");
@@ -566,6 +596,7 @@ int main(int argc, char **argv)
test_gsm0808_enc_dec_speech_codec_ext();
test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
test_gsm0808_enc_dec_speech_codec_list();
+ test_gsm0808_enc_dec_channel_type();
printf("Done\n");
return EXIT_SUCCESS;