jolly has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmocom-bb/+/34484?usp=email )


Change subject: Refactoring encoding of mobile identity at mobile application
......................................................................

Refactoring encoding of mobile identity at mobile application

Deprecated functions gsm48_generate_mid_from_*() are replaced by
osmo_mobile_identity_encode_msgb(). Clean up code.

Change-Id: I9ff429bd50d718530fdad64a276053a35c8928f2
---
M src/host/layer23/include/osmocom/bb/mobile/gsm48_mm.h
M src/host/layer23/src/mobile/gsm48_mm.c
M src/host/layer23/src/mobile/gsm48_rr.c
3 files changed, 75 insertions(+), 61 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/84/34484/1

diff --git a/src/host/layer23/include/osmocom/bb/mobile/gsm48_mm.h 
b/src/host/layer23/include/osmocom/bb/mobile/gsm48_mm.h
index 3ece82e..4d8a353 100644
--- a/src/host/layer23/include/osmocom/bb/mobile/gsm48_mm.h
+++ b/src/host/layer23/include/osmocom/bb/mobile/gsm48_mm.h
@@ -240,6 +240,7 @@
        int                     state;
 };

+int gsm48_encode_mi(struct osmocom_ms *ms, struct msgb *msg, bool tlv, uint8_t 
mi_type, bool emergency_imsi);
 uint8_t gsm48_current_pwr_lev(struct gsm_settings *set, uint16_t arfcn);
 int gsm48_mm_init(struct osmocom_ms *ms);
 int gsm48_mm_exit(struct osmocom_ms *ms);
diff --git a/src/host/layer23/src/mobile/gsm48_mm.c 
b/src/host/layer23/src/mobile/gsm48_mm.c
index 70a35f8..14a2721 100644
--- a/src/host/layer23/src/mobile/gsm48_mm.c
+++ b/src/host/layer23/src/mobile/gsm48_mm.c
@@ -272,41 +272,59 @@
        return length;
 }

-/* encode 'mobile identity' */
-int gsm48_encode_mi(uint8_t *buf, struct msgb *msg, struct osmocom_ms *ms,
-       uint8_t mi_type)
+/* Encode and append 'mobile identity' of given type to message, based on 
current settings. */
+int gsm48_encode_mi(struct osmocom_ms *ms, struct msgb *msg, bool tlv, uint8_t 
mi_type, bool emergency_imsi)
 {
        struct gsm_subscriber *subscr = &ms->subscr;
        struct gsm_settings *set = &ms->settings;
-       uint8_t *ie;
+       struct osmo_mobile_identity mi = { };
+       int rc;
+       uint8_t *l;

-       switch(mi_type) {
+       /* Copy MI values according to their types. */
+       switch (mi_type) {
        case GSM_MI_TYPE_TMSI:
-               gsm48_generate_mid_from_tmsi(buf, subscr->tmsi);
+               mi.tmsi = subscr->tmsi;
                break;
        case GSM_MI_TYPE_IMSI:
-               gsm48_generate_mid_from_imsi(buf, subscr->imsi);
+               if (emergency_imsi)
+                       OSMO_STRLCPY_ARRAY(mi.imsi, set->emergency_imsi);
+               else
+                       OSMO_STRLCPY_ARRAY(mi.imsi, subscr->imsi);
                break;
        case GSM_MI_TYPE_IMEI:
-               gsm48_generate_mid_from_imsi(buf, set->imei);
+               OSMO_STRLCPY_ARRAY(mi.imei, set->imei);
                break;
        case GSM_MI_TYPE_IMEISV:
-               gsm48_generate_mid_from_imsi(buf, set->imeisv);
+               OSMO_STRLCPY_ARRAY(mi.imeisv, set->imeisv);
+               break;
+       }
+
+       /* Append IE type. */
+       if (tlv)
+               msgb_put_u8(msg, GSM48_IE_MOBILE_ID);
+
+       /* Generate MI or 'NONE', if not available. */
+       switch (mi_type) {
+       case GSM_MI_TYPE_TMSI:
+       case GSM_MI_TYPE_IMSI:
+       case GSM_MI_TYPE_IMEI:
+       case GSM_MI_TYPE_IMEISV:
+               mi.type = mi_type;
+               l = msgb_put(msg, 1);
+               rc = osmo_mobile_identity_encode_msgb(msg, &mi, true);
+               if (rc < 0) {
+                       LOGP(DMM, LOGL_ERROR, "Failed to encode mobile identity 
type %d. Please fix!\n", mi_type);
+                       *l = 1;
+                       msgb_put_u8(msg, 0xf0 | GSM_MI_TYPE_NONE);
+                       break;
+               }
+               *l = rc;
                break;
        case GSM_MI_TYPE_NONE:
        default:
-               buf[0] = GSM48_IE_MOBILE_ID;
-               buf[1] = 1;
-               buf[2] = 0xf0;
-               break;
-       }
-       /* alter MI type */
-       buf[2] = (buf[2] & ~GSM_MI_TYPE_MASK) | mi_type;
-
-       if (msg) {
-               /* MI as LV */
-               ie = msgb_put(msg, 1 + buf[1]);
-               memcpy(ie, buf + 1, 1 + buf[1]);
+               msgb_put_u8(msg, 1);
+               msgb_put_u8(msg, 0xf0 | mi_type);
        }

        return 0;
@@ -1816,7 +1834,6 @@
 {
        struct msgb *nmsg;
        struct gsm48_hdr *ngh;
-       uint8_t buf[11];

        LOGP(DMM, LOGL_INFO, "IDENTITY RESPONSE\n");

@@ -1828,8 +1845,8 @@
        ngh->proto_discr = GSM48_PDISC_MM;
        ngh->msg_type = GSM48_MT_MM_ID_RESP;

-       /* MI */
-       gsm48_encode_mi(buf, nmsg, ms, mi_type);
+       /* MI (LV) */
+       gsm48_encode_mi(ms, nmsg, false, mi_type, false);

        /* push RR header and send down */
        return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0, 0);
@@ -1847,7 +1864,6 @@
        struct msgb *nmsg;
        struct gsm48_hdr *ngh;
        uint8_t pwr_lev;
-       uint8_t buf[11];
        struct gsm48_classmark1 cm;


@@ -1868,13 +1884,13 @@
                pwr_lev = gsm48_current_pwr_lev(set, rr->cd_now.arfcn);
        gsm48_encode_classmark1(&cm, sup->rev_lev, sup->es_ind, set->a5_1,
                pwr_lev);
-        msgb_v_put(nmsg, *((uint8_t *)&cm));
-       /* MI */
+       msgb_v_put(nmsg, *((uint8_t *)&cm));
+       /* MI (LV) */
        if (subscr->tmsi != GSM_RESERVED_TMSI) { /* have TMSI ? */
-               gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_TMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_TMSI, false);
                LOGP(DMM, LOGL_INFO, " using TMSI 0x%08x\n", subscr->tmsi);
        } else {
-               gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_IMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMSI, false);
                LOGP(DMM, LOGL_INFO, " using IMSI %s\n", subscr->imsi);
        }

@@ -2372,7 +2388,6 @@
        struct gsm48_hdr *ngh;
        struct gsm48_loc_upd_req *nlu; /* NOTE: mi_len is part of struct */
        uint8_t pwr_lev;
-       uint8_t buf[11];
 
        LOGP(DMM, LOGL_INFO, "LOCATION UPDATING REQUEST\n");

@@ -2380,7 +2395,8 @@
        if (!nmsg)
                return -ENOMEM;
        ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
-       nlu = (struct gsm48_loc_upd_req *)msgb_put(nmsg, sizeof(*nlu));
+       /* Do not add mi_len to the message, this is done at gsm48_encode_mi(). 
*/
+       nlu = (struct gsm48_loc_upd_req *)msgb_put(nmsg, sizeof(*nlu) - 1);

        ngh->proto_discr = GSM48_PDISC_MM;
        ngh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST;
@@ -2399,16 +2415,14 @@
        pwr_lev = gsm48_current_pwr_lev(set, cs->sel_arfcn);
        gsm48_encode_classmark1(&nlu->classmark1, sup->rev_lev, sup->es_ind,
                set->a5_1, pwr_lev);
-       /* MI */
+       /* MI (LV) */
        if (subscr->tmsi != GSM_RESERVED_TMSI) { /* have TMSI ? */
-               gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_TMSI, false);
                LOGP(DMM, LOGL_INFO, " using TMSI 0x%08x\n", subscr->tmsi);
        } else {
-               gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMSI, false);
                LOGP(DMM, LOGL_INFO, " using IMSI %s\n", subscr->imsi);
        }
-       msgb_put(nmsg, buf[1]); /* length is part of nlu */
-       memcpy(&nlu->mi_len, buf + 1, 1 + buf[1]);

        new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_LUPD, 0);

@@ -2826,7 +2840,6 @@
        struct gsm48_hdr *ngh;
        struct gsm48_service_request *nsr; /* NOTE: includes MI length */
        uint8_t *cm2lv;
-       uint8_t buf[11];

        LOGP(DMM, LOGL_INFO, "CM SERVICE REQUEST (cause %d)\n", mm->est_cause);

@@ -2834,7 +2847,8 @@
        if (!nmsg)
                return -ENOMEM;
        ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
-       nsr = (struct gsm48_service_request *)msgb_put(nmsg, sizeof(*nsr));
+       /* Do not add mi_len to the message, this is done at gsm48_encode_mi(). 
*/
+       nsr = (struct gsm48_service_request *)msgb_put(nmsg, sizeof(*nsr) - 1);
        cm2lv = (uint8_t *)&nsr->classmark;

        ngh->proto_discr = GSM48_PDISC_MM;
@@ -2852,23 +2866,21 @@
        if (mm->est_cause == RR_EST_CAUSE_EMERGENCY && set->emergency_imsi[0]) {
                LOGP(DMM, LOGL_INFO, "-> Using IMSI %s for emergency\n",
                        set->emergency_imsi);
-               gsm48_generate_mid_from_imsi(buf, set->emergency_imsi);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMSI, true);
        } else
        if (!subscr->sim_valid) { /* have no SIM ? */
                LOGP(DMM, LOGL_INFO, "-> Using IMEI %s\n",
                        set->imei);
-               gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMEI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMEI, false);
        } else
        if (subscr->tmsi != GSM_RESERVED_TMSI) { /* have TMSI ? */
-               gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_TMSI, false);
                LOGP(DMM, LOGL_INFO, "-> Using TMSI\n");
        } else {
-               gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
+               gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMSI, false);
                LOGP(DMM, LOGL_INFO, "-> Using IMSI %s\n",
                        subscr->imsi);
        }
-       msgb_put(nmsg, buf[1]); /* length is part of nsr */
-       memcpy(&nsr->mi_len, buf + 1, 1 + buf[1]);
        /* prio is optional for eMLPP */

        /* push RR header and send down */
diff --git a/src/host/layer23/src/mobile/gsm48_rr.c 
b/src/host/layer23/src/mobile/gsm48_rr.c
index 1984d26..8c3bbd8 100644
--- a/src/host/layer23/src/mobile/gsm48_rr.c
+++ b/src/host/layer23/src/mobile/gsm48_rr.c
@@ -921,11 +921,9 @@
 /* send chiperhing mode complete */
 static int gsm48_rr_tx_cip_mode_cpl(struct osmocom_ms *ms, uint8_t cr)
 {
-       struct gsm_settings *set = &ms->settings;
        struct msgb *nmsg;
        struct gsm48_hdr *gh;
        struct gsm48_rr_hdr *nrrh;
-       uint8_t buf[11], *tlv;

        LOGP(DRR, LOGL_INFO, "CIPHERING MODE COMPLETE (cr %d)\n", cr);

@@ -938,13 +936,8 @@
        gh->msg_type = GSM48_MT_RR_CIPH_M_COMPL;

        /* MI */
-       if (cr) {
-               gsm48_generate_mid_from_imsi(buf, set->imeisv);
-               /* alter MI type */
-               buf[2] = (buf[2] & ~GSM_MI_TYPE_MASK) | GSM_MI_TYPE_IMEISV;
-               tlv = msgb_put(nmsg, 2 + buf[1]);
-               memcpy(tlv, buf, 2 + buf[1]);
-       }
+       if (cr)
+               gsm48_encode_mi(ms, nmsg, true, GSM_MI_TYPE_IMEISV, false);

        gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg, 0);

@@ -3208,12 +3201,11 @@
 static int gsm48_rr_dl_est(struct osmocom_ms *ms)
 {
        struct gsm48_rrlayer *rr = &ms->rrlayer;
-       struct gsm322_cellsel *cs = &ms->cellsel;
        struct gsm_subscriber *subscr = &ms->subscr;
+       struct gsm322_cellsel *cs = &ms->cellsel;
        struct msgb *nmsg;
        struct gsm48_hdr *gh;
        struct gsm48_pag_rsp *pr;
-       uint8_t mi[11];
        uint16_t ma[64];
        uint8_t ma_len;

@@ -3290,21 +3282,18 @@
                if (ms->subscr.tmsi != GSM_RESERVED_TMSI
                 && (osmo_lai_cmp(&ms->subscr.lai, &cs->sel_cgi.lai) == 0)
                 && rr->paging_mi_type == GSM_MI_TYPE_TMSI) {
-                       gsm48_generate_mid_from_tmsi(mi, subscr->tmsi);
+                       gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_TMSI, 
false);
                        LOGP(DRR, LOGL_INFO, "sending paging response with "
                                "TMSI\n");
                } else if (subscr->imsi[0]) {
-                       gsm48_generate_mid_from_imsi(mi, subscr->imsi);
+                       gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_IMSI, 
false);
                        LOGP(DRR, LOGL_INFO, "sending paging response with "
                                "IMSI\n");
                } else {
-                       mi[1] = 1;
-                       mi[2] = 0xf0 | GSM_MI_TYPE_NONE;
+                       gsm48_encode_mi(ms, nmsg, false, GSM_MI_TYPE_NONE, 
false);
                        LOGP(DRR, LOGL_INFO, "sending paging response without "
                                "TMSI/IMSI\n");
                }
-               msgb_put(nmsg, 1 + mi[1]);
-               memcpy(pr->data, mi + 1, 1 + mi[1]);
        }

 #ifdef TEST_FREQUENCY_MOD

--
To view, visit https://gerrit.osmocom.org/c/osmocom-bb/+/34484?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Change-Id: I9ff429bd50d718530fdad64a276053a35c8928f2
Gerrit-Change-Number: 34484
Gerrit-PatchSet: 1
Gerrit-Owner: jolly <andr...@eversberg.eu>
Gerrit-MessageType: newchange

Reply via email to