[PATCH] libosmocore[master]: Add generic Mobile Identity encoder

Max gerrit-no-reply at lists.osmocom.org
Thu Feb 15 10:44:58 UTC 2018


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

Add generic Mobile Identity encoder

Add generic function which allows caller to set Mobile Identity
explicitly. This allows to use IMEI or IMEISV for example. Make
gsm48_generate_mid_from_imsi() into wrapper around new function.

Change-Id: Id79be7abfff75ecd0d248bbeed93e605abeec9b3
---
M include/osmocom/gsm/gsm48.h
M src/gsm/gsm48.c
M src/gsm/libosmogsm.map
3 files changed, 30 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/59/6459/1

diff --git a/include/osmocom/gsm/gsm48.h b/include/osmocom/gsm/gsm48.h
index 69431de..9ec5463 100644
--- a/include/osmocom/gsm/gsm48.h
+++ b/include/osmocom/gsm/gsm48.h
@@ -31,6 +31,7 @@
 			uint16_t mnc, uint16_t lac);
 int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi);
 int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi);
+uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type);
 
 /* Convert Mobile Identity (10.5.1.4) to string */
 int gsm48_mi_to_string(char *string, const int str_len,
diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index 4570802..b58e9e2 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -584,37 +584,40 @@
 	return 7;
 }
 
+/*! Generate TS 24.008 §10.5.1.4 Mobile ID
+ *  \param[out] buf Caller-provided output buffer
+ *  \param[in] id Identity to be encoded
+ *  \returns number of bytes used in \a buf */
+uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
+{
+	uint8_t length = strnlen(id, 255), i, off = 0, odd = (length & 1) == 1;
+
+	buf[0] = GSM48_IE_MOBILE_ID;
+	buf[2] = osmo_char2bcd(id[0]) << 4 | mi_type | (odd << 3);
+
+	/* if the length is even we will fill half of the last octet */
+	buf[1] = (length + (odd ? 1 : 2)) >> 1;
+
+	for (i = 1; i < buf[1]; ++i) {
+		uint8_t upper, lower = osmo_char2bcd(id[++off]);
+		if (!odd && off + 1 == length)
+			upper = 0x0f;
+		else
+			upper = osmo_char2bcd(id[++off]) & 0x0f;
+
+		buf[2 + i] = (upper << 4) | lower;
+	}
+
+	return 2 + buf[1];
+}
+
 /*! Generate TS 04.08 Mobile ID from IMSI
  *  \param[out] buf Caller-provided output buffer
  *  \param[in] imsi IMSI to be encoded
  *  \returns number of bytes used in \a buf */
 int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
 {
-	unsigned int length = strlen(imsi), i, off = 0;
-	uint8_t odd = (length & 0x1) == 1;
-
-	buf[0] = GSM48_IE_MOBILE_ID;
-	buf[2] = osmo_char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3);
-
-	/* if the length is even we will fill half of the last octet */
-	if (odd)
-		buf[1] = (length + 1) >> 1;
-	else
-		buf[1] = (length + 2) >> 1;
-
-	for (i = 1; i < buf[1]; ++i) {
-		uint8_t lower, upper;
-
-		lower = osmo_char2bcd(imsi[++off]);
-		if (!odd && off + 1 == length)
-			upper = 0x0f;
-		else
-			upper = osmo_char2bcd(imsi[++off]) & 0x0f;
-
-		buf[2 + i] = (upper << 4) | lower;
-	}
-
-	return 2 + buf[1];
+	return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
 }
 
 /*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 53b6f6e..7a74718 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -246,6 +246,7 @@
 gsm48_encode_ssversion;
 gsm48_encode_useruser;
 gsm48_generate_lai;
+gsm48_generate_mid;
 gsm48_generate_mid_from_imsi;
 gsm48_generate_mid_from_tmsi;
 gsm48_mi_to_string;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id79be7abfff75ecd0d248bbeed93e605abeec9b3
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>


More information about the gerrit-log mailing list