[layer23] Detach SIM, if reading fails
[osmocom-bb.git] / src / host / layer23 / src / mobile / subscriber.c
index 00d823c..544d53f 100644 (file)
@@ -23,8 +23,8 @@
 #include <errno.h>
 #include <string.h>
 #include <arpa/inet.h>
-#include <osmocore/talloc.h>
-#include <osmocore/comp128.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/gsm/comp128.h>
 
 #include <osmocom/bb/common/logging.h>
 #include <osmocom/bb/common/osmocom_data.h>
@@ -153,7 +153,8 @@ int gsm_subscr_exit(struct osmocom_ms *ms)
  */
 
 /* Attach test card, no SIM must be currently attached */
-int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc)
+int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc,
+       uint16_t lac, uint32_t tmsi)
 {
        struct gsm_settings *set = &ms->settings;
        struct gsm_subscriber *subscr = &ms->subscr;
@@ -185,6 +186,10 @@ int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc)
        subscr->plmn_valid = set->test_rplmn_valid;
        subscr->plmn_mcc = mcc;
        subscr->plmn_mnc = mnc;
+       subscr->mcc = mcc;
+       subscr->mnc = mnc;
+       subscr->lac = lac;
+       subscr->tmsi = tmsi;
        subscr->always_search_hplmn = set->test_always;
        subscr->t6m_hplmn = 1; /* try to find home network every 6 min */
        strcpy(subscr->imsi, set->test_imsi);
@@ -194,9 +199,10 @@ int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc)
                gsm_imsi_mnc(subscr->imsi));
 
        if (subscr->plmn_valid)
-               LOGP(DMM, LOGL_INFO, "-> Test card registered to %s %s "
-                       "(%s, %s)\n", gsm_print_mcc(mcc), gsm_print_mnc(mnc),
-                       gsm_get_mcc(mcc), gsm_get_mnc(mcc, mnc));
+               LOGP(DMM, LOGL_INFO, "-> Test card registered to %s %s 0x%04x"
+                       "(%s, %s)\n", gsm_print_mcc(mcc),
+                       gsm_print_mnc(mnc), lac, gsm_get_mcc(mcc),
+                       gsm_get_mnc(mcc, mnc));
        else
                LOGP(DMM, LOGL_INFO, "-> Test card not registered\n");
 
@@ -486,7 +492,7 @@ static struct subscr_sim_file {
        { 1, { 0 },         0x2fe2, subscr_sim_iccid },
        { 1, { 0x7f20, 0 }, 0x6f07, subscr_sim_imsi },
        { 1, { 0x7f20, 0 }, 0x6f7e, subscr_sim_loci },
-       { 0, { 0x7f20, 0 }, 0x6f40, subscr_sim_msisdn },
+       { 0, { 0x7f10, 0 }, 0x6f40, subscr_sim_msisdn },
        { 0, { 0x7f20, 0 }, 0x6f20, subscr_sim_kc },
        { 0, { 0x7f20, 0 }, 0x6f30, subscr_sim_plmnsel },
        { 0, { 0x7f20, 0 }, 0x6f31, subscr_sim_hpplmn },
@@ -561,6 +567,7 @@ static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg)
        uint16_t payload_len = msg->len - sizeof(*sh);
        int rc;
        struct subscr_sim_file *sf = &subscr_sim_files[subscr->sim_file_index];
+       struct msgb *nmsg;
 
        /* error handling */
        if (sh->job_type == SIM_JOB_ERROR) {
@@ -606,6 +613,13 @@ static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg)
 
                        vty_notify(ms, NULL);
                        vty_notify(ms, "SIM failed, replace SIM!\n");
+
+                       /* detach simcard */
+                       subscr->sim_valid = 0;
+                       nmsg = gsm48_mmr_msgb_alloc(GSM48_MMR_NREG_REQ);
+                       if (!nmsg)
+                               return;
+                       gsm48_mmr_downmsg(ms, nmsg);
                }
                msgb_free(msg);