[PATCH] libosmocore[master]: use gsm48_decode_lai2() in gsm0808_dec_cell_id_list()

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/.

Stefan Sperling gerrit-no-reply at lists.osmocom.org
Thu Mar 15 18:48:11 UTC 2018


Review at  https://gerrit.osmocom.org/7309

use gsm48_decode_lai2() in gsm0808_dec_cell_id_list()

This makes gsm0808_dec_cell_id_list() properly decode 3-digit MNCs.
Add a test which encodes/decodes a LAI_AND_LAC list with 3-digit MNCs.

Change-Id: If6b941720de33dca66b6b1aa2cb95a3275708b7f
Related: OS#2847
---
M src/gsm/gsm0808_utils.c
M tests/gsm0808/gsm0808_test.c
2 files changed, 63 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/09/7309/1

diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 2d95ec6..ac1e852 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -677,16 +677,15 @@
 	return *tlv_len + 2;
 }
 
-/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC.
- * Return 0 if successful, negative on error. */
-static int decode_lai(const uint8_t *data, uint16_t *mcc, uint16_t *mnc, uint16_t *lac)
+/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
+static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
 {
 	struct gsm48_loc_area_id lai;
 
-	/* Copy data to stack to prevent unaligned access in gsm48_decode_lai(). */
+	/* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
 	memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
 
-	return gsm48_decode_lai(&lai, mcc, mnc, lac) ? -1 : 0;
+	gsm48_decode_lai2(&lai, decoded);
 }
 
 static int parse_cell_id_global_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
@@ -704,8 +703,7 @@
 			return -ENOSPC;
 		id = &cil->id_list[i].global;
 		lai_offset = i * elemlen;
-		if (decode_lai(&data[lai_offset], &id->lai.plmn.mcc, &id->lai.plmn.mnc, &id->lai.lac) != 0)
-			return -EINVAL;
+		decode_lai(&data[lai_offset], &id->lai);
 		ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
 		id->cell_identity = osmo_load16be(ci_be);
 		*consumed += elemlen;
@@ -776,8 +774,7 @@
 		if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
 			return -ENOSPC;
 		id = &cil->id_list[i].lai_and_lac;
-		if (decode_lai(&data[i * elemlen], &id->plmn.mcc, &id->plmn.mnc, &id->lac) != 0)
-			return -EINVAL;
+		decode_lai(&data[i * elemlen], id);
 		*consumed += elemlen;
 		remain -= elemlen;
 		i++;
diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c
index 8e21b0c..4da3929 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -864,6 +864,62 @@
 	msgb_free(msg);
 }
 
+static void test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac()
+{
+	struct gsm0808_cell_id_list2 enc_cil;
+	struct gsm0808_cell_id_list2 dec_cil;
+	struct osmo_location_area_id id;
+	struct msgb *msg;
+	uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x10, 0x04,
+		0x92, 0x61, 0x54, 0x23, 0x42,
+		0x92, 0x72, 0x54, 0x24, 0x43,
+		0x92, 0x83, 0x54, 0x25, 0x44
+	};
+	uint8_t rc_enc;
+	int rc_dec, i;
+
+	memset(&enc_cil, 0, sizeof(enc_cil));
+	enc_cil.id_discr = CELL_IDENT_LAI_AND_LAC;
+
+	id.plmn.mcc = 0x123;
+	osmo_mnc_from_str("456", &id.plmn.mnc, &id.plmn.mnc_3_digits);
+	id.lac = 0x2342;
+	memcpy(&enc_cil.id_list[0].lai_and_lac, &id, sizeof(id));
+
+	id.plmn.mcc = 0x124;
+	osmo_mnc_from_str("457", &id.plmn.mnc, &id.plmn.mnc_3_digits);
+	id.lac = 0x2443;
+	memcpy(&enc_cil.id_list[1].lai_and_lac, &id, sizeof(id));
+
+	id.plmn.mcc = 0x125;
+	osmo_mnc_from_str("458", &id.plmn.mnc, &id.plmn.mnc_3_digits);
+	id.lac = 0x2544;
+	memcpy(&enc_cil.id_list[2].lai_and_lac, &id, sizeof(id));
+
+	enc_cil.id_list_len = 3;
+
+	msg = msgb_alloc(1024, "output buffer");
+	rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
+	OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
+	OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
+
+	rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
+	OSMO_ASSERT(rc_dec == msg->len - 2);
+
+	OSMO_ASSERT(dec_cil.id_list_len == 3);
+	/* Check MAXLEN elements to ensure everything has been initialized. */
+	for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
+		struct osmo_location_area_id *enc_id;
+		struct osmo_location_area_id *dec_id;
+		enc_id = &enc_cil.id_list[i].lai_and_lac;
+		dec_id = &dec_cil.id_list[i].lai_and_lac;
+		OSMO_ASSERT(osmo_plmn_cmp(&enc_id->plmn, &dec_id->plmn) == 0);
+		OSMO_ASSERT(enc_id->lac == dec_id->lac);
+	}
+
+	msgb_free(msg);
+}
+
 int main(int argc, char **argv)
 {
 	printf("Testing generation of GSM0808 messages\n");
@@ -899,6 +955,7 @@
 	test_gsm0808_enc_dec_cell_id_list_single_lac();
 	test_gsm0808_enc_dec_cell_id_list_multi_lac();
 	test_gsm0808_enc_dec_cell_id_list_bss();
+	test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac();
 
 	printf("Done\n");
 	return EXIT_SUCCESS;

-- 
To view, visit https://gerrit.osmocom.org/7309
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If6b941720de33dca66b6b1aa2cb95a3275708b7f
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Stefan Sperling <ssperling at sysmocom.de>



More information about the gerrit-log mailing list