LCLS, TS 29.205: add GCR routines

Add functions to encode and decode Global Call Reference as per
3GPP TS 29.205 Table B 2.1.9.1 add corresponding tests.

Change-Id: Iee95aa4e5c056645b6cb5667e4a067097d52dfbf
Related: OS#2487
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index e28ea33..ccb38ad 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -24,7 +24,7 @@
 			gsm_utils.c rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \
 			gprs_cipher_core.c gprs_rlc.c gsm0480.c abis_nm.c gsm0502.c \
 			gsm0411_utils.c gsm0411_smc.c gsm0411_smr.c gsm0414.c \
-			lapd_core.c lapdm.c kasumi.c gsm_04_08_gprs.c \
+			lapd_core.c lapdm.c kasumi.c gsm29205.c gsm_04_08_gprs.c \
 			auth_core.c auth_comp128v1.c auth_comp128v23.c \
 			auth_milenage.c milenage/aes-encblock.c gea.c \
 			milenage/aes-internal.c milenage/aes-internal-enc.c \
diff --git a/src/gsm/gsm29205.c b/src/gsm/gsm29205.c
new file mode 100644
index 0000000..0ef29b7
--- /dev/null
+++ b/src/gsm/gsm29205.c
@@ -0,0 +1,93 @@
+/*
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <osmocom/gsm/protocol/gsm_08_08.h>
+#include <osmocom/gsm/gsm29205.h>
+#include <osmocom/gsm/gsm0808.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/gsm/tlv.h>
+
+#include <errno.h>
+
+/*! \addtogroup gsm29205
+ *  @{
+ *  \file gsm29205.c
+ *  Functions related to 3GPP TS 29.205, primarily message generation/encoding.
+ */
+
+/*! Create Global Call Reference.
+ *  \param[out] msg Message Buffer for appending IE
+ *  \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
+ *  \returns number of bytes added to \a msg */
+uint8_t osmo_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
+{
+	uint8_t buf[2];
+
+	if (!g)
+		return 0;
+
+	if (g->net_len < 3 || g->net_len > 5)
+		return 0;
+
+	msgb_lv_put(msg, g->net_len, g->net);
+
+	osmo_store16be(g->node, &buf);
+	msgb_lv_put(msg, 2, buf);
+
+	msgb_lv_put(msg, 5, g->cr);
+
+	/* Length: LV(Net) + LV(Node) + LV(CRef) - see 3GPP TS §3.2.2.115 */
+	return (g->net_len + 1) + (2 + 1) + (5 + 1);
+}
+
+/*! Decode Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
+ *  \param[out] gcr Caller-provided memory to store Global Call Reference
+ *  \param[in] elem IE value to be decoded
+ *  \param[in] len Length of \a elem in bytes
+ *  \returns number of bytes parsed; negative on error */
+int osmo_dec_gcr(struct osmo_gcr_parsed *gcr, const uint8_t *elem, uint8_t len)
+{
+	uint16_t parsed = 1; /* account for length byte right away */
+
+	if (len < 13)
+		return -EBADMSG;
+
+	gcr->net_len = elem[0];
+	if (gcr->net_len < 3 || gcr->net_len > 5)
+		return -EINVAL;
+
+	memcpy(gcr->net, elem + parsed, gcr->net_len);
+	/* +1 for ignored Node ID length field */
+	parsed += (gcr->net_len + 1);
+
+	gcr->node = osmo_load16be(elem + parsed);
+	parsed += 2;
+
+	if (elem[parsed] != 5) /* see Table B 2.1.9.2 */
+		return -ENOENT;
+
+	parsed++;
+
+	memcpy(gcr->cr, elem + parsed, 5);
+
+	return parsed + 5;
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index dc4e0a6..e85ed6d 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -235,6 +235,9 @@
 gsm29118_create_release_req;
 gsm29118_create_service_abort_req;
 
+osmo_enc_gcr;
+osmo_dec_gcr;
+
 gsm0858_rsl_ul_meas_enc;
 
 gsm338_get_sms_alphabet;