Change in libosmocore[master]: add GAD coding for Location Services

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

neels gerrit-no-reply at lists.osmocom.org
Thu Oct 1 01:40:19 UTC 2020


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/20332 )


Change subject: add GAD coding for Location Services
......................................................................

add GAD coding for Location Services

GAD, Universal Geographical Area Description:
- decoded structs for all GAD elements
- encoding and decoding for Ellipsoid point with uncertainty circle
- other GAD element encodings are so far not implemented

Add encoding and decoding tests.

Change-Id: I7a9dd805a91b1ebb6353bde0cd169218acbf223c
---
M include/Makefile.am
A include/osmocom/gsm/gad.h
M src/gsm/Makefile.am
A src/gsm/gad.c
M src/gsm/libosmogsm.map
M tests/Makefile.am
A tests/gad/gad_test.c
A tests/gad/gad_test.ok
M tests/testsuite.at
9 files changed, 645 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/32/20332/1

diff --git a/include/Makefile.am b/include/Makefile.am
index a6341f1..a505952 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -92,6 +92,7 @@
                        osmocom/coding/gsm0503_interleaving.h \
                        osmocom/coding/gsm0503_coding.h \
                        osmocom/coding/gsm0503_amr_dtx.h \
+                       osmocom/gsm/gad.h \
                        osmocom/gsm/gsm0808.h \
                        osmocom/gsm/gsm29205.h \
                        osmocom/gsm/gsm0808_utils.h \
diff --git a/include/osmocom/gsm/gad.h b/include/osmocom/gsm/gad.h
new file mode 100644
index 0000000..88dbf57
--- /dev/null
+++ b/include/osmocom/gsm/gad.h
@@ -0,0 +1,172 @@
+/*! \defgroup gad 3GPP TS 23.032 GAD: Universal Geographical Area Description.
+ *  @{
+ *  \file gad.h
+ */
+/*
+ * (C) 2020 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels at hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 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/>.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include <osmocom/core/utils.h>
+
+struct msgb;
+
+enum osmo_gad_type {
+	/*! Ellipsoid point */
+	OSMO_GAD_TYPE_ELL_POINT = 0,
+	/*! Ellipsoid point with uncertainty circle. */
+	OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE = 1,
+	/*! Ellipsoid point with uncertainty ellipse. */
+	OSMO_GAD_TYPE_ELL_POINT_UNC_ELLIPSE = 3,
+	OSMO_GAD_TYPE_POLYGON = 5,
+	/*! Ellipsoid point with altitude. */
+	OSMO_GAD_TYPE_ELL_POINT_ALT = 8,
+	/*! Ellipsoid point with altitude and uncertainty ellipsoid. */
+	OSMO_GAD_TYPE_ELL_POINT_ALT_UNC_ELL = 9,
+	/*! Ellipsoid arc */
+	OSMO_GAD_TYPE_ELL_ARC = 10,
+	/*! High accuracy ellipsoid point with uncertainty ellipse. */
+	OSMO_GAD_TYPE_HA_ELL_POINT_UNC_ELLIPSE = 11,
+	/*! High accuracy ellipsoid point with altitude and uncertainty ellipsoid. */
+	OSMO_GAD_TYPE_HA_ELL_POINT_ALT_UNC_ELL = 12,
+};
+
+extern const struct value_string osmo_gad_type_names[];
+static inline const char *osmo_gad_type_name(enum osmo_gad_type val)
+{ return get_value_string(osmo_gad_type_names, val); }
+
+struct osmo_gad_ell_point {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+};
+
+struct osmo_gad_ell_point_unc_circle {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	/*! uncertainty circle radius in mm (m * 1e3) */
+	uint32_t unc;
+};
+
+struct osmo_gad_ell_point_unc_ellipse {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	uint32_t unc_semi_major;
+	uint32_t unc_semi_minor;
+	int16_t major_ori;
+	uint8_t confidence;
+};
+
+struct osmo_gad_polygon {
+	uint8_t num_points;
+	struct osmo_gad_ell_point point[15];
+};
+
+struct osmo_gad_ell_point_alt {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	/*! altitude in mm (m * 1e3) */
+	int32_t alt;
+};
+
+struct osmo_gad_ell_point_alt_unc_ell {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	/*! altitude in mm (m * 1e3) */
+	int32_t alt;
+	uint32_t unc_semi_major;
+	uint32_t unc_semi_minor;
+	int16_t major_ori;
+	/*! uncertainty in altitude in mm (m * 1e3) */
+	int32_t unc_alt;
+	uint8_t confidence;
+};
+
+struct osmo_gad_ell_arc {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	/*! inner circle radius in mm (m * 1e3) */
+	uint32_t inner_r;
+	/*! uncertainty circle radius in mm (m * 1e3) */
+	uint32_t unc_r;
+	int16_t ofs_angle;
+	int16_t incl_angle;
+	uint8_t confidence;
+};
+
+struct osmo_gad_ha_ell_point_alt_unc_ell {
+	/*! latitude in micro degrees (degrees * 1e6) */
+	int32_t lat;
+	/*! longitude in micro degrees (degrees * 1e6) */
+	int32_t lon;
+	/*! altitude in mm (m * 1e3) */
+	int32_t alt;
+	uint32_t unc_semi_major;
+	uint32_t unc_semi_minor;
+	int16_t major_ori;
+	uint8_t h_confidence;
+	int32_t unc_alt;
+	uint8_t v_confidence;
+};
+
+struct osmo_gad {
+	enum osmo_gad_type type;
+	union {
+		struct osmo_gad_ell_point ell_point;
+		struct osmo_gad_ell_point_unc_circle ell_point_unc_circle;
+		struct osmo_gad_ell_point_unc_ellipse ell_point_unc_ellipse;
+		struct osmo_gad_polygon polygon;
+		struct osmo_gad_ell_point_alt ell_point_alt;
+		struct osmo_gad_ell_point_alt_unc_ell ell_point_alt_unc_ell;
+		struct osmo_gad_ell_arc ell_arc;
+		struct osmo_gad_ell_point_unc_ellipse ha_ell_point_unc_ellipse;
+		struct osmo_gad_ha_ell_point_alt_unc_ell ha_ell_point_alt_unc_ell;
+	};
+};
+
+int osmo_gad_enc(struct msgb *msg, const struct osmo_gad *gad);
+const char *osmo_gad_dec(struct osmo_gad *gad, const uint8_t *data, uint8_t len);
+
+int osmo_gad_to_str_buf(char *buf, size_t buflen, const struct osmo_gad *gad);
+char *osmo_gad_to_str_c(void *ctx, const struct osmo_gad *gad);
+
+uint32_t osmo_gad_enc_lat(int32_t lat_deg_1e6);
+uint32_t osmo_gad_dec_lat(int32_t lat);
+uint32_t osmo_gad_enc_lon(int32_t lon_deg_1e6);
+uint32_t osmo_gad_dec_lon(int32_t lon);
+uint8_t osmo_gad_enc_unc(uint32_t mm);
+uint32_t osmo_gad_dec_unc(uint8_t unc);
+
+/*! @} */
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index c5232ab..4fa940b 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -32,7 +32,8 @@
 			milenage/milenage.c gan.c ipa.c gsm0341.c apn.c \
 			gsup.c gsup_sms.c gprs_gea.c gsm0503_conv.c oap.c gsm0808_utils.c \
 			gsm23003.c gsm23236.c mncc.c bts_features.c oap_client.c \
-			gsm29118.c gsm48_rest_octets.c cbsp.c gsm48049.c i460_mux.c
+			gsm29118.c gsm48_rest_octets.c cbsp.c gsm48049.c i460_mux.c \
+			gad.c
 libgsmint_la_LDFLAGS = -no-undefined
 libgsmint_la_LIBADD = $(top_builddir)/src/libosmocore.la
 
diff --git a/src/gsm/gad.c b/src/gsm/gad.c
new file mode 100644
index 0000000..9e17cf0
--- /dev/null
+++ b/src/gsm/gad.c
@@ -0,0 +1,339 @@
+/* 3GPP TS 23.032 GAD: Universal Geographical Area Description */
+/*
+ * (C) 2020 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels at hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 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/>.
+ *
+ */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/gsm/gad.h>
+
+/*! \addtogroup gad
+ *  @{
+ *  \file gad.c
+ *  Message encoding and decoding for 3GPP TS 23.032 GAD: Universal Geographical Area Description.
+ */
+
+const struct value_string osmo_gad_type_names[] = {
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_POINT),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_POINT_UNC_ELLIPSE),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_POLYGON),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_POINT_ALT),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_POINT_ALT_UNC_ELL),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_ELL_ARC),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_HA_ELL_POINT_UNC_ELLIPSE),
+	OSMO_VALUE_STRING(OSMO_GAD_TYPE_HA_ELL_POINT_ALT_UNC_ELL),
+	{}
+};
+
+static void put_u24be(struct msgb *msg, uint32_t val)
+{
+	uint8_t *pos = msgb_put(msg, 3);
+	osmo_store32be_ext(val, pos, 3);
+}
+
+uint32_t osmo_gad_enc_lat(int32_t lat_deg_1e6)
+{
+	/* N <= ((2**23)/90)*X < N+1
+	 * N: encoded latitude
+	 * X: latitude in degrees
+	 */
+	int32_t sign = 0;
+	int64_t x;
+	if (lat_deg_1e6 < 0) {
+		sign = 1 << 23;
+		lat_deg_1e6 = -lat_deg_1e6;
+	}
+	x = lat_deg_1e6;
+	x <<= 23;
+	x += (1 << 23) - 1;
+	x /= 90 * 1000000;
+	return sign | (x & 0x7fffff);
+}
+
+uint32_t osmo_gad_dec_lat(int32_t lat)
+{
+	int64_t sign = 1;
+	int64_t x;
+	if (lat & 0x800000) {
+		sign = -1;
+		lat &= 0x7fffff;
+	}
+	x = lat;
+	x *= 90 * 1000000;
+	x >>= 23;
+	x *= sign;
+	return x;
+}
+
+uint32_t osmo_gad_enc_lon(int32_t lon_deg_1e6)
+{
+	/* -180 .. 180 degrees mapped to a signed 24 bit integer.
+	 * N <= ((2**24)/360) * X < N+1
+	 * N: encoded longitude
+	 * X: longitude in degrees
+	 */
+	int64_t x = lon_deg_1e6;
+	x *= (1 << 24);
+	if (lon_deg_1e6 >= 0)
+		x += (1 << 24) - 1;
+	else
+		x -= (1 << 24) - 1;
+	x /= 360 * 1000000;
+	return (uint32_t)(x & 0xffffff);
+}
+
+uint32_t osmo_gad_dec_lon(int32_t lon)
+{
+	/* -180 .. 180 degrees mapped to a signed 24 bit integer.
+	 * N <= ((2**24)/360) * X < N+1
+	 * N: encoded longitude
+	 * X: longitude in degrees
+	 */
+	int64_t x;
+	if (lon & 0x800000) {
+		/* make the 24bit negative number to a 32bit negative number */
+		lon |= 0xff000000;
+	}
+	x = lon;
+	x *= 360 * 1000000;
+	x /= (1 << 24);
+	return x;
+}
+
+/*
+ * r = C((1+x)**K - 1)
+ * C = 10, x = 0.1
+ *
+ * def r(k):
+ *     return 10.*(((1+0.1)**k) -1 )
+ * for k in range(128):
+ *     print('%d,' % (r(k) * 1000.))
+ */
+static uint32_t table_uncertainty_1e3[128] = {
+	0, 1000, 2100, 3310, 4641, 6105, 7715, 9487, 11435, 13579, 15937, 18531, 21384, 24522, 27974, 31772, 35949,
+	40544, 45599, 51159, 57274, 64002, 71402, 79543, 88497, 98347, 109181, 121099, 134209, 148630, 164494, 181943,
+	201137, 222251, 245476, 271024, 299126, 330039, 364043, 401447, 442592, 487851, 537636, 592400, 652640, 718904,
+	791795, 871974, 960172, 1057189, 1163908, 1281299, 1410429, 1552472, 1708719, 1880591, 2069650, 2277615,
+	2506377, 2758014, 3034816, 3339298, 3674227, 4042650, 4447915, 4893707, 5384077, 5923485, 6516834, 7169517,
+	7887469, 8677216, 9545938, 10501531, 11552685, 12708953, 13980849, 15379933, 16918927, 18611820, 20474002,
+	22522402, 24775642, 27254206, 29980627, 32979690, 36278659, 39907525, 43899277, 48290205, 53120226, 58433248,
+	64277573, 70706330, 77777964, 85556760, 94113436, 103525780, 113879358, 125268293, 137796123, 151576735,
+	166735409, 183409950, 201751945, 221928139, 244121953, 268535149, 295389664, 324929630, 357423593, 393166952,
+	432484648, 475734112, 523308524, 575640376, 633205414, 696526955, 766180651, 842799716, 927080688, 1019789756,
+	1121769732, 1233947705, 1357343476, 1493078824, 1642387706, 1806627477,
+};
+
+uint32_t osmo_gad_dec_unc(uint8_t unc)
+{
+	return table_uncertainty_1e3[unc & 0x7f];
+}
+
+uint8_t osmo_gad_enc_unc(uint32_t mm)
+{
+	uint8_t unc;
+	for (unc = 0; unc < ARRAY_SIZE(table_uncertainty_1e3); unc++) {
+		if (table_uncertainty_1e3[unc] > mm)
+			return unc - 1;
+	}
+	return 127;
+}
+
+#ifdef GAD_FUTURE
+
+/*
+ * r = C((1+x)**K - 1)
+ * C = 0.3, x = 0.02
+ *
+ * def r(k):
+ *     return 0.3*(((1+0.02)**k) -1 )
+ * for k in range(256):
+ *     print('%d,' % (r(k) * 1000.))
+ */
+static uint32_t table_ha_uncertainty_1e3[256] = {
+	0, 6, 12, 18, 24, 31, 37, 44, 51, 58, 65, 73, 80, 88, 95, 103, 111, 120, 128, 137, 145, 154, 163, 173, 182, 192,
+	202, 212, 222, 232, 243, 254, 265, 276, 288, 299, 311, 324, 336, 349, 362, 375, 389, 402, 417, 431, 445, 460,
+	476, 491, 507, 523, 540, 556, 574, 591, 609, 627, 646, 665, 684, 703, 724, 744, 765, 786, 808, 830, 853, 876,
+	899, 923, 948, 973, 998, 1024, 1051, 1078, 1105, 1133, 1162, 1191, 1221, 1252, 1283, 1314, 1347, 1380, 1413,
+	1447, 1482, 1518, 1554, 1592, 1629, 1668, 1707, 1748, 1788, 1830, 1873, 1916, 1961, 2006, 2052, 2099, 2147,
+	2196, 2246, 2297, 2349, 2402, 2456, 2511, 2567, 2625, 2683, 2743, 2804, 2866, 2929, 2994, 3060, 3127, 3195,
+	3265, 3336, 3409, 3483, 3559, 3636, 3715, 3795, 3877, 3961, 4046, 4133, 4222, 4312, 4404, 4498, 4594, 4692,
+	4792, 4894, 4998, 5104, 5212, 5322, 5435, 5549, 5666, 5786, 5907, 6032, 6158, 6287, 6419, 6554, 6691, 6830,
+	6973, 7119, 7267, 7418, 7573, 7730, 7891, 8055, 8222, 8392, 8566, 8743, 8924, 9109, 9297, 9489, 9685, 9884,
+	10088, 10296, 10508, 10724, 10944, 11169, 11399, 11633, 11871, 12115, 12363, 12616, 12875, 13138, 13407, 13681,
+	13961, 14246, 14537, 14834, 15136, 15445, 15760, 16081, 16409, 16743, 17084, 17431, 17786, 18148, 18517, 18893,
+	19277, 19669, 20068, 20475, 20891, 21315, 21747, 22188, 22638, 23096, 23564, 24042, 24529, 25025, 25532, 26048,
+	26575, 27113, 27661, 28220, 28791, 29372, 29966, 30571, 31189, 31818, 32461, 33116, 33784, 34466, 35161, 35871,
+	36594, 37332, 38085, 38852, 39635, 40434, 41249, 42080, 42927, 43792, 44674, 45573, 46491,
+};
+
+static uint32_t osmo_gad_dec_ha_unc(uint8_t unc)
+{
+	return table_uncertainty_1e3[unc];
+}
+
+static uint8_t osmo_gad_enc_ha_unc(uint32_t mm)
+{
+	uint8_t unc;
+	for (unc = 0; unc < ARRAY_SIZE(table_ha_uncertainty_1e3); unc++) {
+		if (table_uncertainty_1e3[unc] > mm)
+			return unc - 1;
+	}
+	return 255;
+}
+
+#endif /* GAD_FUTURE */
+
+static int osmo_gad_enc_ell_point_unc_circle(struct msgb *msg, const struct osmo_gad_ell_point_unc_circle *v)
+{
+	uint8_t *old_tail = msg->tail;
+	msgb_put_u8(msg, OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE << 4);
+	put_u24be(msg, osmo_gad_enc_lat(v->lat));
+	put_u24be(msg, osmo_gad_enc_lon(v->lon));
+	msgb_put_u8(msg, osmo_gad_enc_unc(v->unc));
+	return (msg->tail - old_tail);
+}
+
+static const char *osmo_gad_dec_ell_point_unc_circle(struct osmo_gad_ell_point_unc_circle *v, const uint8_t *data,
+						     uint8_t len)
+{
+	uint32_t val;
+	if (len != 8)
+		return "Decoding GAD Ellipsoid point with uncertainty circle: Invalid length";
+
+	/* Load a 24bit big endian integer from data[1] */
+	val = osmo_load32be_ext_2(&data[1], 3);
+	v->lat = osmo_gad_dec_lat(val);
+
+	/* Load a 24bit big endian integer from data[4] */
+	val = osmo_load32be_ext_2(&data[4], 3);
+	v->lon = osmo_gad_dec_lon(val);
+
+	v->unc = osmo_gad_dec_unc(data[7] & 0x7f);
+	return NULL;
+}
+
+int osmo_gad_enc(struct msgb *msg, const struct osmo_gad *gad)
+{
+	switch (gad->type) {
+	case OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE:
+		return osmo_gad_enc_ell_point_unc_circle(msg, &gad->ell_point_unc_circle);
+	default:
+		return -ENOTSUP;
+	}
+}
+
+const char *osmo_gad_dec(struct osmo_gad *gad, const uint8_t *data, uint8_t len)
+{
+	if (len < 1)
+		return "Decoding GAD: zero length";
+	*gad = (struct osmo_gad){};
+	gad->type = data[0] >> 4;
+	switch (gad->type) {
+	case OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE:
+		return osmo_gad_dec_ell_point_unc_circle(&gad->ell_point_unc_circle, data, len);
+	default:
+		return "Decoding GAD: unsupported GAD type";
+	}
+}
+
+/*! Return a human readable representation of GAD (location estimate) data.
+ * \param[out] buf  Buffer to write string to.
+ * \param[in] buflen  sizeof(buf).
+ * \param[in] gad  Location data.
+ * \returns number of chars that would be written, like snprintf().
+ */
+int osmo_gad_to_str_buf(char *buf, size_t buflen, const struct osmo_gad *gad)
+{
+	struct osmo_strbuf sb = { .buf = buf, .len = buflen };
+
+	if (!gad) {
+		OSMO_STRBUF_PRINTF(sb, "null");
+		return sb.chars_needed;
+	}
+
+	switch (gad->type) {
+	case OSMO_GAD_TYPE_ELL_POINT:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-point{lat=");
+		OSMO_STRBUF_APPEND(sb, osmo_micros_to_float_str_buf, gad->ell_point.lat);
+		OSMO_STRBUF_PRINTF(sb, ",lon=");
+		OSMO_STRBUF_APPEND(sb, osmo_micros_to_float_str_buf, gad->ell_point.lon);
+		OSMO_STRBUF_PRINTF(sb, "}");
+		break;
+
+	case OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-point-with-uncertainty-circle{lat=");
+		OSMO_STRBUF_APPEND(sb, osmo_micros_to_float_str_buf, gad->ell_point_unc_circle.lat);
+		OSMO_STRBUF_PRINTF(sb, ",lon=");
+		OSMO_STRBUF_APPEND(sb, osmo_micros_to_float_str_buf, gad->ell_point_unc_circle.lon);
+		OSMO_STRBUF_PRINTF(sb, ",unc=%" PRIu32 "mm}", gad->ell_point_unc_circle.unc);
+		break;
+
+	case OSMO_GAD_TYPE_ELL_POINT_UNC_ELLIPSE:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-point-with-uncertainty-circle{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_POLYGON:
+		OSMO_STRBUF_PRINTF(sb, "Polygon{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_ELL_POINT_ALT:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-point-with-altitude{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_ELL_POINT_ALT_UNC_ELL:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-point-with-altitude-and-uncertainty-ellipsoid"
+				   "{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_ELL_ARC:
+		OSMO_STRBUF_PRINTF(sb, "Ellipsoid-arc{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_HA_ELL_POINT_UNC_ELLIPSE:
+		OSMO_STRBUF_PRINTF(sb, "High-accuracy-ellipsoid-point-with-uncertainty-ellipse"
+				   "{to-str-not-implemented}");
+		break;
+
+	case OSMO_GAD_TYPE_HA_ELL_POINT_ALT_UNC_ELL:
+		OSMO_STRBUF_PRINTF(sb, "High-accuracy-ellipsoid-point-with-altitude-and-uncertainty-ellipsoid"
+				   "{to-str-not-implemented}");
+		break;
+	default:
+		OSMO_STRBUF_PRINTF(sb, "unknown-type-%d", gad->type);
+		break;
+	}
+	return sb.chars_needed;
+}
+
+/*! Return a human readable representation of GAD (location estimate) data.
+ * \param[in] ctx  Talloc ctx to allocate string buffer from.
+ * \param[in] val  Value to convert to float.
+ * \returns resulting string, dynamically allocated.
+ */
+char *osmo_gad_to_str_c(void *ctx, const struct osmo_gad *gad)
+{
+	OSMO_NAME_C_IMPL(ctx, 128, "ERROR", osmo_gad_to_str_buf, gad)
+}
+
+/*! @} */
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 4ece107..a31f73a 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -705,5 +705,16 @@
 osmo_nri_ranges_to_str_buf;
 osmo_nri_ranges_to_str_c;
 
+osmo_gad_enc;
+osmo_gad_dec;
+osmo_gad_to_str_buf;
+osmo_gad_to_str_c;
+osmo_gad_enc_lat;
+osmo_gad_dec_lat;
+osmo_gad_enc_lon;
+osmo_gad_dec_lon;
+osmo_gad_enc_unc;
+osmo_gad_dec_unc;
+
 local: *;
 };
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7a0b4b1..9a2f217 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -38,6 +38,7 @@
                  dtx/dtx_gsm0503_test					\
                  i460_mux/i460_mux_test					\
 		 bitgen/bitgen_test					\
+		 gad/gad_test						\
 		 $(NULL)
 
 if ENABLE_MSGFILE
@@ -281,6 +282,9 @@
 bitgen_bitgen_test_SOURCES = bitgen/bitgen_test.c
 bitgen_bitgen_test_LDADD = $(LDADD)
 
+gad_gad_test_SOURCES = gad/gad_test.c
+gad_gad_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/gad.o
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -361,6 +365,7 @@
 	     exec/exec_test.ok exec/exec_test.err \
 	     i460_mux/i460_mux_test.ok \
 	     bitgen/bitgen_test.ok \
+	     gad/gad_test.ok \
 	     $(NULL)
 
 if ENABLE_LIBSCTP
diff --git a/tests/gad/gad_test.c b/tests/gad/gad_test.c
new file mode 100644
index 0000000..c15a1ef
--- /dev/null
+++ b/tests/gad/gad_test.c
@@ -0,0 +1,104 @@
+#include <stdio.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/gsm/gad.h>
+
+void test_gad_lat_lon_dec_enc_stability()
+{
+	uint32_t lat_enc;
+	uint32_t lon_enc;
+	printf("--- %s\n", __func__);
+	for (lat_enc = 0x0; lat_enc <= 0xffffff; lat_enc++) {
+		int32_t lat_dec = osmo_gad_dec_lat(lat_enc);
+		uint32_t enc2 = osmo_gad_enc_lat(lat_dec);
+		uint32_t want_enc = lat_enc;
+		/* "-0" == 0, because the highest bit is defined as a sign bit. */
+		if (lat_enc == 0x800000)
+			want_enc = 0;
+		if (enc2 != want_enc) {
+			printf("ERR: lat=%u --> %d --> %u\n", lat_enc, lat_dec, enc2);
+			printf("%d -> %u\n", lat_dec + 1, osmo_gad_enc_lat(lat_dec + 1));
+			OSMO_ASSERT(false);
+		}
+	}
+	printf("osmo_gad_dec_lat() -> osmo_gad_enc_lat() of %u values successful\n", lat_enc);
+	for (lon_enc = 0; lon_enc <= 0xffffff; lon_enc++) {
+		int32_t lon_dec = osmo_gad_dec_lon(lon_enc);
+		uint32_t enc2 = osmo_gad_enc_lon(lon_dec);
+		uint32_t want_enc = lon_enc;
+		if (enc2 != want_enc) {
+			printf("ERR: lon=%u 0x%x --> %d --> %u\n", lon_enc, lon_enc, lon_dec, enc2);
+			printf("%d -> %u\n", lon_dec + 1, osmo_gad_enc_lon(lon_dec + 1));
+			printf("%d -> %u\n", lon_dec - 1, osmo_gad_enc_lon(lon_dec - 1));
+			OSMO_ASSERT(false);
+		}
+	}
+	printf("osmo_gad_dec_lon() -> osmo_gad_enc_lon() of %u values successful\n", lon_enc);
+}
+
+struct osmo_gad gad_test_pdus[] = {
+	{
+		.type = OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE,
+		.ell_point_unc_circle = {
+			/* Values rounded to the nearest encodable value, for test result matching */
+			.lat = 23000006,
+			.lon = 42000002,
+			.unc = 442592,
+		},
+	},
+};
+
+void test_gad_enc_dec()
+{
+	struct osmo_gad *pdu;
+	printf("--- %s\n", __func__);
+
+	for (pdu = gad_test_pdus; (pdu - gad_test_pdus) < ARRAY_SIZE(gad_test_pdus); pdu++) {
+		struct msgb *msg = msgb_alloc(1024, __func__);
+		struct osmo_gad dec_pdu;
+		int rc;
+		const char *errmsg;
+		rc = osmo_gad_enc(msg, pdu);
+		if (rc <= 0) {
+			printf("[%ld] %s: ERROR: failed to encode pdu\n", (pdu - gad_test_pdus),
+			       osmo_gad_type_name(pdu->type));
+			goto loop_end;
+		}
+		if (rc != msg->len) {
+			printf("[%ld] %s: ERROR: osmo_gad_enc() returned length %d but msgb has %d bytes\n",
+			       (pdu - gad_test_pdus), osmo_gad_type_name(pdu->type),
+			       rc, msg->len);
+			goto loop_end;
+		}
+
+		memset(&dec_pdu, 0xff, sizeof(dec_pdu));
+		errmsg = osmo_gad_dec(&dec_pdu, msg->data, msg->len);
+		if (errmsg) {
+			printf("[%ld] %s: ERROR: failed to decode pdu: %s\n", (pdu - gad_test_pdus),
+			       osmo_gad_type_name(pdu->type), errmsg);
+			printf("     encoded data: %s\n", osmo_hexdump(msg->data, msg->len));
+			goto loop_end;
+		}
+
+		if (memcmp(pdu, &dec_pdu, sizeof(dec_pdu))) {
+			printf("[%ld] %s: ERROR: decoded PDU != encoded PDU\n", (pdu - gad_test_pdus),
+			       osmo_gad_type_name(pdu->type));
+			printf("     original struct: %s\n", osmo_hexdump((void*)pdu, sizeof(*pdu)));
+			printf("      decoded struct: %s\n", osmo_hexdump((void*)&dec_pdu, sizeof(dec_pdu)));
+			goto loop_end;
+		}
+
+		printf("[%ld] %s: ok\n", (pdu - gad_test_pdus), osmo_gad_type_name(pdu->type));
+
+loop_end:
+		msgb_free(msg);
+	}
+}
+
+int main()
+{
+	test_gad_lat_lon_dec_enc_stability();
+	test_gad_enc_dec();
+	return 0;
+}
diff --git a/tests/gad/gad_test.ok b/tests/gad/gad_test.ok
new file mode 100644
index 0000000..c22b2b3
--- /dev/null
+++ b/tests/gad/gad_test.ok
@@ -0,0 +1,5 @@
+--- test_gad_lat_lon_dec_enc_stability
+osmo_gad_dec_lat() -> osmo_gad_enc_lat() of 16777216 values successful
+osmo_gad_dec_lon() -> osmo_gad_enc_lon() of 16777216 values successful
+--- test_gad_enc_dec
+[0] OSMO_GAD_TYPE_ELL_POINT_UNC_CIRCLE: ok
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 493c16f..10cf74b 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -402,3 +402,9 @@
 cat $abs_srcdir/bitgen/bitgen_test.ok > expout
 AT_CHECK([$abs_top_builddir/tests/bitgen/bitgen_test], [0], [expout], [ignore])
 AT_CLEANUP
+
+AT_SETUP([gad])
+AT_KEYWORDS([gad])
+cat $abs_srcdir/gad/gad_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/gad/gad_test], [0], [expout], [ignore])
+AT_CLEANUP

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/20332
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I7a9dd805a91b1ebb6353bde0cd169218acbf223c
Gerrit-Change-Number: 20332
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201001/7c2d5929/attachment.htm>


More information about the gerrit-log mailing list