pespin has submitted this change. ( 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/settings.h 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 6 files changed, 87 insertions(+), 77 deletions(-)
Approvals: laforge: Looks good to me, but someone else must approve Jenkins Builder: Verified fixeria: Looks good to me, approved
diff --git a/src/host/layer23/include/osmocom/bb/common/settings.h b/src/host/layer23/include/osmocom/bb/common/settings.h index 01db2a7..e312a1c 100644 --- a/src/host/layer23/include/osmocom/bb/common/settings.h +++ b/src/host/layer23/include/osmocom/bb/common/settings.h @@ -90,7 +90,8 @@ int plmn_mode; /* PLMN_MODE_* */
/* SIM */ - int sim_type; /* selects card on power on */ + int sim_type; /* enum gsm_subscriber_sim_type, + * selects card on power on */ char emergency_imsi[OSMO_IMSI_BUF_SIZE];
/* SMS */ 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 e284c9c..b623742 100644 --- a/src/host/layer23/src/common/subscriber.c +++ b/src/host/layer23/src/common/subscriber.c @@ -45,6 +45,10 @@ { 0, NULL } };
+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 int gsm_subscr_remove_sapcard(struct osmocom_ms *ms);
static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg); @@ -141,6 +145,46 @@ return 0; }
+/* Insert card */ +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); + break; + case GSM_SIM_TYPE_TEST: + rc = gsm_subscr_insert_testcard(ms); + break; + case GSM_SIM_TYPE_SAP: + rc = gsm_subscr_insert_sapcard(ms); + break; + default: + return -EINVAL; + } + + if (rc < 0) { + subscr->sim_valid = false; + return rc; + } + return rc; +} + /* Detach card */ int gsm_subscr_remove(struct osmocom_ms *ms) { @@ -175,29 +219,18 @@ */
/* 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 */ @@ -762,23 +795,12 @@ }
/* 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 */ @@ -1215,24 +1237,13 @@ */
/* 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 56ff6bc..427c8aa 100644 --- a/src/host/layer23/src/common/vty.c +++ b/src/host/layer23/src/common/vty.c @@ -496,6 +496,7 @@ }
set = &ms->settings; + set->sim_type = GSM_SIM_TYPE_TEST;
if (argc == 2) { vty_out(vty, "Give MNC together with MCC%s", VTY_NEWLINE); @@ -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; @@ -562,6 +563,7 @@ "Name of MS (see "show ms")\n") { struct osmocom_ms *ms; + struct gsm_settings *set;
ms = l23_vty_get_ms(argv[0], vty); if (!ms) @@ -573,7 +575,9 @@ return CMD_WARNING; }
- if (gsm_subscr_sapcard(ms) != 0) + set = &ms->settings; + set->sim_type = GSM_SIM_TYPE_SAP; + if (gsm_subscr_insert(ms) != 0) return CMD_WARNING;
return CMD_SUCCESS; @@ -583,6 +587,7 @@ "SIM actions\nAttach SIM from reader\nName of MS (see "show ms")") { struct osmocom_ms *ms; + struct gsm_settings *set;
ms = l23_vty_get_ms(argv[0], vty); if (!ms) @@ -594,7 +599,9 @@ return CMD_WARNING; }
- gsm_subscr_simcard(ms); + set = &ms->settings; + set->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 211a008..fdb3821 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;