aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2010-03-28 18:14:50 +0800
committerHarald Welte <laforge@gnumonks.org>2010-03-28 18:14:50 +0800
commit9bb553ee40104c47f9a27d7662eb2867dd2b5d1e (patch)
tree4c3dbd39702a060ee1a403cd0f40a6a92123c6dc
parenta3b844cf45dc4d6a6a3686e8ce6e2447643b878e (diff)
import gsm48_mi_to_string() from OpenBSC0.1.3
-rw-r--r--include/osmocore/gsm48.h4
-rw-r--r--src/gsm48.c43
2 files changed, 47 insertions, 0 deletions
diff --git a/include/osmocore/gsm48.h b/include/osmocore/gsm48.h
index 1e96357..b752ee0 100644
--- a/include/osmocore/gsm48.h
+++ b/include/osmocore/gsm48.h
@@ -14,4 +14,8 @@ void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi);
int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi);
+/* Convert Mobile Identity (10.5.1.4) to string */
+int gsm48_mi_to_string(char *string, const int str_len,
+ const u_int8_t *mi, const int mi_len);
+
#endif
diff --git a/src/gsm48.c b/src/gsm48.c
index 5761c67..d024109 100644
--- a/src/gsm48.c
+++ b/src/gsm48.c
@@ -261,3 +261,46 @@ int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
return 2 + buf[1];
}
+
+/* Convert Mobile Identity (10.5.1.4) to string */
+int gsm48_mi_to_string(char *string, const int str_len, const u_int8_t *mi, const int mi_len)
+{
+ int i;
+ u_int8_t mi_type;
+ char *str_cur = string;
+ u_int32_t tmsi;
+
+ mi_type = mi[0] & GSM_MI_TYPE_MASK;
+
+ switch (mi_type) {
+ case GSM_MI_TYPE_NONE:
+ break;
+ case GSM_MI_TYPE_TMSI:
+ /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
+ if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
+ memcpy(&tmsi, &mi[1], 4);
+ tmsi = ntohl(tmsi);
+ return snprintf(string, str_len, "%u", tmsi);
+ }
+ break;
+ case GSM_MI_TYPE_IMSI:
+ case GSM_MI_TYPE_IMEI:
+ case GSM_MI_TYPE_IMEISV:
+ *str_cur++ = bcd2char(mi[0] >> 4);
+
+ for (i = 1; i < mi_len; i++) {
+ if (str_cur + 2 >= string + str_len)
+ return str_cur - string;
+ *str_cur++ = bcd2char(mi[i] & 0xf);
+ /* skip last nibble in last input byte when GSM_EVEN */
+ if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
+ *str_cur++ = bcd2char(mi[i] >> 4);
+ }
+ break;
+ default:
+ break;
+ }
+ *str_cur++ = '\0';
+
+ return str_cur - string;
+}