pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmocom-bb/+/32751 )


Change subject: layer23: Generalize subscriber SIM insert API
......................................................................

layer23: Generalize subscriber SIM insert API

With this patch, during VTY config the SIM type is selected, and the app
calls a generic gsm_subscriber_insert() API which will take of
internally initializing and starting whatever specific-backend setup is
needed.

Change-Id: I5aa34ae297ec0114e1d2355d59fdd77b43b35464
---
M src/host/layer23/include/osmocom/bb/common/subscriber.h
M src/host/layer23/src/common/subscriber.c
M src/host/layer23/src/common/vty.c
M src/host/layer23/src/mobile/app_mobile.c
M src/host/layer23/src/modem/app_modem.c
5 files changed, 78 insertions(+), 80 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/51/32751/1

diff --git a/src/host/layer23/include/osmocom/bb/common/subscriber.h 
b/src/host/layer23/include/osmocom/bb/common/subscriber.h
index 8f0966e..8119103 100644
--- a/src/host/layer23/include/osmocom/bb/common/subscriber.h
+++ b/src/host/layer23/include/osmocom/bb/common/subscriber.h
@@ -32,7 +32,7 @@
 #define GSM_SIM_IS_READER(type) \
        (type == GSM_SIM_TYPE_L1PHY || type == GSM_SIM_TYPE_SAP)

-enum {
+enum gsm_subscriber_sim_type {
        GSM_SIM_TYPE_NONE = 0,
        GSM_SIM_TYPE_L1PHY,
        GSM_SIM_TYPE_TEST,
@@ -43,8 +43,8 @@
        struct osmocom_ms       *ms;

        /* status */
-       uint8_t                 sim_type; /* type of sim */
-       uint8_t                 sim_valid; /* sim inserted and valid */
+       enum gsm_subscriber_sim_type sim_type; /* type of sim */
+       bool                    sim_valid; /* sim inserted and valid */
        enum gsm_sub_sim_ustate ustate; /* update status */
        uint8_t                 imsi_attached; /* attached state */

@@ -98,17 +98,16 @@

 int gsm_subscr_init(struct osmocom_ms *ms);
 int gsm_subscr_exit(struct osmocom_ms *ms);
-int gsm_subscr_testcard(struct osmocom_ms *ms);
+int gsm_subscr_insert(struct osmocom_ms *ms);
+int gsm_subscr_remove(struct osmocom_ms *ms);
+
 int gsm_subscr_sap_rsp_cb(struct osmocom_ms *ms, int res_code,
        uint8_t res_type, uint16_t param_len, const uint8_t *param_val);
-int gsm_subscr_sapcard(struct osmocom_ms *ms);
-int gsm_subscr_simcard(struct osmocom_ms *ms);
 void gsm_subscr_sim_pin(struct osmocom_ms *ms, char *pin1, char *pin2,
        int8_t mode);
 int gsm_subscr_write_loci(struct osmocom_ms *ms);
 int gsm_subscr_generate_kc(struct osmocom_ms *ms, uint8_t key_seq,
        uint8_t *rand, uint8_t no_sim);
-int gsm_subscr_remove(struct osmocom_ms *ms);
 void new_sim_ustate(struct gsm_subscriber *subscr, int state);
 int gsm_subscr_del_forbidden_plmn(struct gsm_subscriber *subscr, uint16_t mcc,
        uint16_t mnc);
diff --git a/src/host/layer23/src/common/subscriber.c 
b/src/host/layer23/src/common/subscriber.c
index a5d5e8a..617ba58 100644
--- a/src/host/layer23/src/common/subscriber.c
+++ b/src/host/layer23/src/common/subscriber.c
@@ -37,6 +37,10 @@
  * if list is changed, the result is not written back to SIM */
 //#define TEST_EMPTY_FPLMN

+static int gsm_subscr_insert_simcard(struct osmocom_ms *ms);
+static int gsm_subscr_insert_testcard(struct osmocom_ms *ms);
+static int gsm_subscr_insert_sapcard(struct osmocom_ms *ms);
+
 static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg);
 static void subscr_sim_update_cb(struct osmocom_ms *ms, struct msgb *msg);
 static void subscr_sim_key_cb(struct osmocom_ms *ms, struct msgb *msg);
@@ -136,29 +140,17 @@
  */

 /* Attach test card, no SIM must be currently attached */
-int gsm_subscr_testcard(struct osmocom_ms *ms)
+int gsm_subscr_insert_testcard(struct osmocom_ms *ms)
 {
        struct gsm_settings *set = &ms->settings;
        struct gsm_subscriber *subscr = &ms->subscr;

-       if (subscr->sim_valid) {
-               LOGP(DMM, LOGL_ERROR, "Cannot insert card, until current card "
-                       "is detached.\n");
-               return -EBUSY;
-       }
-
        if (!osmo_imsi_str_valid(set->test_sim.imsi)) {
                LOGP(DMM, LOGL_ERROR, "Wrong IMSI format\n");
                return -EINVAL;
        }

-       /* reset subscriber */
-       gsm_subscr_exit(ms);
-       gsm_subscr_init(ms);
-
-       subscr->sim_type = GSM_SIM_TYPE_TEST;
        sprintf(subscr->sim_name, "test");
-       subscr->sim_valid = 1;
        subscr->imsi_attached = set->test_sim.imsi_attached;
        subscr->acc_barr = set->test_sim.barr; /* we may access barred cell */
        subscr->acc_class = 0xffff; /* we have any access class */
@@ -723,23 +715,11 @@
 }

 /* Attach SIM reader, no SIM must be currently attached */
-int gsm_subscr_simcard(struct osmocom_ms *ms)
+int gsm_subscr_insert_simcard(struct osmocom_ms *ms)
 {
        struct gsm_subscriber *subscr = &ms->subscr;

-       if (subscr->sim_valid) {
-               LOGP(DMM, LOGL_ERROR, "Cannot attach card, until current card "
-                       "is detached.\n");
-               return -EBUSY;
-       }
-
-       /* reset subscriber */
-       gsm_subscr_exit(ms);
-       gsm_subscr_init(ms);
-
-       subscr->sim_type = GSM_SIM_TYPE_L1PHY;
        sprintf(subscr->sim_name, "sim");
-       subscr->sim_valid = 1;
        subscr->ustate = GSM_SIM_U2_NOT_UPDATED;

        /* start with first index */
@@ -993,9 +973,45 @@
 }

 /*
- * detach
+ * generic
  */

+int gsm_subscr_insert(struct osmocom_ms *ms)
+{
+       struct gsm_settings *set = &ms->settings;
+       struct gsm_subscriber *subscr = &ms->subscr;
+       int rc;
+
+       if (subscr->sim_valid) {
+               LOGP(DMM, LOGL_ERROR, "Cannot insert card, until current card 
is removed.\n");
+               return -EBUSY;
+       }
+
+       /* reset subscriber */
+       gsm_subscr_exit(ms);
+       gsm_subscr_init(ms);
+
+       subscr->sim_valid = true;
+
+       switch (set->sim_type) {
+       case GSM_SIM_TYPE_L1PHY:
+               /* trigger sim card reader process */
+               rc = gsm_subscr_insert_simcard(ms);
+       case GSM_SIM_TYPE_TEST:
+               rc = gsm_subscr_insert_testcard(ms);
+       case GSM_SIM_TYPE_SAP:
+               rc = gsm_subscr_insert_sapcard(ms);
+       default:
+               return -EINVAL;
+       }
+
+       if (rc < 0) {
+               subscr->sim_valid = false;
+               return rc;
+       }
+
+}
+
 static int gsm_subscr_remove_sapcard(struct osmocom_ms *ms);

 /* Detach card */
@@ -1223,24 +1239,12 @@
  */

 /* Attach SIM card over SAP */
-int gsm_subscr_sapcard(struct osmocom_ms *ms)
+int gsm_subscr_insert_sapcard(struct osmocom_ms *ms)
 {
        struct gsm_subscriber *subscr = &ms->subscr;
        int rc;

-       if (subscr->sim_valid) {
-               LOGP(DMM, LOGL_ERROR, "Cannot insert card, until current card "
-                       "is detached.\n");
-               return -EBUSY;
-       }
-
-       /* reset subscriber */
-       gsm_subscr_exit(ms);
-       gsm_subscr_init(ms);
-
-       subscr->sim_type = GSM_SIM_TYPE_SAP;
        sprintf(subscr->sim_name, "sap");
-       subscr->sim_valid = 1;

        /* Try to connect to the SAP interface */
        l23_vty_ms_notify(ms, NULL);
diff --git a/src/host/layer23/src/common/vty.c 
b/src/host/layer23/src/common/vty.c
index a48eaa9..69a3dce 100644
--- a/src/host/layer23/src/common/vty.c
+++ b/src/host/layer23/src/common/vty.c
@@ -495,6 +495,7 @@
                return CMD_WARNING;
        }

+       ms->subscr.sim_type = GSM_SIM_TYPE_TEST;
        set = &ms->settings;

        if (argc == 2) {
@@ -527,7 +528,7 @@

        set->test_sim.imsi_attached = attached;

-       rc = gsm_subscr_testcard(ms);
+       rc = gsm_subscr_insert(ms);
        if (rc < 0) {
                vty_out(vty, "Attach test SIM card failed: %d%s", rc, 
VTY_NEWLINE);
                return CMD_WARNING;
@@ -573,7 +574,8 @@
                return CMD_WARNING;
        }

-       if (gsm_subscr_sapcard(ms) != 0)
+       ms->subscr.sim_type = GSM_SIM_TYPE_SAP;
+       if (gsm_subscr_insert(ms) != 0)
                return CMD_WARNING;

        return CMD_SUCCESS;
@@ -594,7 +596,8 @@
                return CMD_WARNING;
        }

-       gsm_subscr_simcard(ms);
+       ms->subscr.sim_type = GSM_SIM_TYPE_L1PHY;
+       gsm_subscr_insert(ms);

        return CMD_SUCCESS;
 }
diff --git a/src/host/layer23/src/mobile/app_mobile.c 
b/src/host/layer23/src/mobile/app_mobile.c
index 76f11f3..9ce1ad2 100644
--- a/src/host/layer23/src/mobile/app_mobile.c
+++ b/src/host/layer23/src/mobile/app_mobile.c
@@ -139,7 +139,6 @@
                            void *handler_data, void *signal_data)
 {
        struct osmocom_ms *ms;
-       struct gsm_settings *set;
        struct msgb *nmsg;

        if (subsys != SS_L1CTL)
@@ -148,7 +147,6 @@
        switch (signal) {
        case S_L1CTL_RESET:
                ms = signal_data;
-               set = &ms->settings;

                /* waiting for reset after shutdown */
                if (ms->shutdown == MS_SHUTDOWN_WAIT_RESET) {
@@ -160,19 +158,10 @@
                if (ms->started)
                        break;

-               /* insert test card, if enabled */
-               switch (set->sim_type) {
-               case GSM_SIM_TYPE_L1PHY:
-                       /* trigger sim card reader process */
-                       gsm_subscr_simcard(ms);
-                       break;
-               case GSM_SIM_TYPE_TEST:
-                       gsm_subscr_testcard(ms);
-                       break;
-               case GSM_SIM_TYPE_SAP:
-                       gsm_subscr_sapcard(ms);
-                       break;
-               default:
+               if (ms->settings.sim_type != GSM_SIM_TYPE_NONE) {
+                       /* insert sim card */
+                       gsm_subscr_insert(ms);
+               } else {
                        /* no SIM, trigger PLMN selection process */
                        nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
                        if (!nmsg)
diff --git a/src/host/layer23/src/modem/app_modem.c 
b/src/host/layer23/src/modem/app_modem.c
index da1a37f..805b5b1 100644
--- a/src/host/layer23/src/modem/app_modem.c
+++ b/src/host/layer23/src/modem/app_modem.c
@@ -194,7 +194,6 @@
                            void *handler_data, void *signal_data)
 {
        struct osmocom_ms *ms;
-       struct gsm_settings *set;

        if (subsys != SS_L1CTL)
                return 0;
@@ -206,24 +205,14 @@
                app_data.ms = ms;

                /* insert test card, if enabled */
-               set = &ms->settings;
-               switch (set->sim_type) {
-               case GSM_SIM_TYPE_L1PHY:
-                       /* trigger sim card reader process */
-                       gsm_subscr_simcard(ms);
-                       break;
-               case GSM_SIM_TYPE_TEST:
-                       gsm_subscr_testcard(ms);
-                       break;
-               case GSM_SIM_TYPE_SAP:
-                       gsm_subscr_sapcard(ms);
-                       break;
-               default:
+               if (ms->settings.sim_type != GSM_SIM_TYPE_NONE) {
+                       /* insert sim card */
+                       gsm_subscr_insert(ms);
+               } else {
                        /* No SIM, trigger PLMN selection process.
                         * FIXME: not implemented. Code in mobile needs to be
                         * moved to common/ and reuse it here.
                         */
-                       break;
                }

                ms->started = true;

--
To view, visit https://gerrit.osmocom.org/c/osmocom-bb/+/32751
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Change-Id: I5aa34ae297ec0114e1d2355d59fdd77b43b35464
Gerrit-Change-Number: 32751
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <[email protected]>
Gerrit-MessageType: newchange

Reply via email to