Improved SAP interface integration

Submitted by Вадим Яницкий on Jan. 4, 2016, 9:42 a.m.

Details

Message ID CAPEnp=aY52v+yHL10SbwzXkhrCkkj3PMpRq5fviLYd01GDGSAQ@mail.gmail.com
State New
Headers show

Commit Message

Вадим Яницкий Jan. 4, 2016, 9:42 a.m.
This is final patch version. I did some tests and it seems works fine. One
thing that I can't test is how SAP connection works because I have no
working sap-server setup. If anyone have please test. Now the `sim sap
MS_NAME` command just enables SAP connection and `sim remove MS_MANE`
closes connection if it's active.

What I have done:
- Fixed GPSD configuration r/w errors;
- Fixed sap_socket_path configuration r/w errors;
- SAP interface starts only if it is configured as SIM source;
- Now SAP interface is selectable as SIM source using the sim command in
VTY.

 {
@@ -1075,7 +1101,7 @@ DEFUN(cfg_gps_host, cfg_gps_host_cmd, "gps host
HOST:PORT",
     if (colon != NULL) {
         memcpy(g.gpsd_host, argv[0], colon - argv[0] - 1);
         g.gpsd_host[colon - argv[0]] = '\0';
-        memcpy(g.gpsd_port, colon, strlen(colon));
+        memcpy(g.gpsd_port, colon + 1, strlen(colon));
         g.gpsd_port[strlen(colon)] = '\0';
     } else {
         snprintf(g.gpsd_host, ARRAY_SIZE(g.gpsd_host), "%s", argv[0]);
@@ -1295,6 +1321,9 @@ static void config_write_ms(struct vty *vty, struct
osmocom_ms *ms)
         case GSM_SIM_TYPE_TEST:
         vty_out(vty, " sim test%s", VTY_NEWLINE);
         break;
+        case GSM_SIM_TYPE_SAP:
+        vty_out(vty, " sim sap%s", VTY_NEWLINE);
+        break;
     }
     vty_out(vty, " network-selection-mode %s%s", (set->plmn_mode
             == PLMN_MODE_AUTO) ? "auto" : "manual", VTY_NEWLINE);
@@ -1499,8 +1528,7 @@ static int config_write(struct vty *vty)
     struct osmocom_ms *ms;

 #ifdef _HAVE_GPSD
-    vty_out(vty, "gpsd host %s%s", g.gpsd_host, VTY_NEWLINE);
-    vty_out(vty, "gpsd port %s%s", g.gpsd_port, VTY_NEWLINE);
+    vty_out(vty, "gpsd host %s:%s%s", g.gpsd_host, g.gpsd_port,
VTY_NEWLINE);
 #endif
     vty_out(vty, "gps device %s%s", g.device, VTY_NEWLINE);
     if (g.baud)
@@ -1558,9 +1586,10 @@ DEFUN(cfg_ms_sap, cfg_ms_sap_cmd, "sap-socket PATH",
     return CMD_SUCCESS;
 }

-DEFUN(cfg_ms_sim, cfg_ms_sim_cmd, "sim (none|reader|test)",
+DEFUN(cfg_ms_sim, cfg_ms_sim_cmd, "sim (none|reader|test|sap)",
     "Set SIM card to attach when powering on\nAttach no SIM\n"
-    "Attach SIM from reader\nAttach bulit in test SIM")
+    "Attach SIM from reader\nAttach bulit in test SIM\n"
+    "Attach SIM over SAP interface")
 {
     struct osmocom_ms *ms = vty->index;
     struct gsm_settings *set = &ms->settings;
@@ -1575,6 +1604,9 @@ DEFUN(cfg_ms_sim, cfg_ms_sim_cmd, "sim
(none|reader|test)",
     case 't':
         set->sim_type = GSM_SIM_TYPE_TEST;
         break;
+    case 's':
+        set->sim_type = GSM_SIM_TYPE_SAP;
+        break;
     default:
         vty_out(vty, "unknown SIM type%s", VTY_NEWLINE);
         return CMD_WARNING;
@@ -2802,6 +2834,7 @@ int ms_vty_init(void)

     install_element(ENABLE_NODE, &sim_test_cmd);
     install_element(ENABLE_NODE, &sim_test_att_cmd);
+    install_element(ENABLE_NODE, &sim_sap_cmd);
     install_element(ENABLE_NODE, &sim_reader_cmd);
     install_element(ENABLE_NODE, &sim_remove_cmd);
     install_element(ENABLE_NODE, &sim_pin_cmd);
@@ -2822,7 +2855,7 @@ int ms_vty_init(void)
     install_element(ENABLE_NODE, &delete_forbidden_plmn_cmd);

 #ifdef _HAVE_GPSD
-    install_element(CONFIG_NODE, &cfg_gps_host_cmd);
+    install_element(CONFIG_NODE, &cfg_gpsd_host_cmd);
 #endif
     install_element(CONFIG_NODE, &cfg_gps_device_cmd);
     install_element(CONFIG_NODE, &cfg_gps_baud_cmd);

Patch hide | download patch | download mbox

diff --git a/src/host/layer23/include/osmocom/bb/common/sap_interface.h
b/src/host/layer23/include/osmocom/bb/common/sap_interface.h
index bf19356..e4e64ce 100644
--- a/src/host/layer23/include/osmocom/bb/common/sap_interface.h
+++ b/src/host/layer23/include/osmocom/bb/common/sap_interface.h
@@ -11,6 +11,7 @@  int osmosap_sapsocket(struct osmocom_ms *ms, const char
*path);
 int osmosap_init(struct osmocom_ms *ms);

 enum osmosap_state {
+    SAP_SOCKET_ERROR,
     SAP_NOT_CONNECTED,
     SAP_IDLE,
     SAP_CONNECTION_UNDER_NEGOTIATION,
diff --git a/src/host/layer23/include/osmocom/bb/mobile/subscriber.h
b/src/host/layer23/include/osmocom/bb/mobile/subscriber.h
index 79a2ecc..ac785d4 100644
--- a/src/host/layer23/include/osmocom/bb/mobile/subscriber.h
+++ b/src/host/layer23/include/osmocom/bb/mobile/subscriber.h
@@ -23,7 +23,8 @@  struct gsm_sub_plmn_na {
 enum {
     GSM_SIM_TYPE_NONE = 0,
     GSM_SIM_TYPE_READER,
-    GSM_SIM_TYPE_TEST
+    GSM_SIM_TYPE_TEST,
+    GSM_SIM_TYPE_SAP
 };

 struct gsm_subscriber {
@@ -86,6 +87,8 @@  int gsm_subscr_init(struct osmocom_ms *ms);
 int gsm_subscr_exit(struct osmocom_ms *ms);
 int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc,
     uint16_t lac, uint32_t tmsi, uint8_t imsi_attached);
+int gsm_subscr_sapcard(struct osmocom_ms *ms);
+int gsm_subscr_remove_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);
diff --git a/src/host/layer23/src/common/sap_interface.c
b/src/host/layer23/src/common/sap_interface.c
index a56f4f2..052646a 100644
--- a/src/host/layer23/src/common/sap_interface.c
+++ b/src/host/layer23/src/common/sap_interface.c
@@ -515,7 +515,7 @@  int sap_open(struct osmocom_ms *ms, const char
*socket_path)
     rc = connect(ms->sap_wq.bfd.fd, (struct sockaddr *) &local,
sizeof(local));
     if (rc < 0) {
         fprintf(stderr, "Failed to connect to '%s'\n", local.sun_path);
-        set->sap_socket_path[0] = 0;
+        ms->sap_entity.sap_state = SAP_SOCKET_ERROR;
         close(ms->sap_wq.bfd.fd);
         return rc;
     }
@@ -582,22 +582,11 @@  int osmosap_sapsocket(struct osmocom_ms *ms, const
char *path)
 int osmosap_init(struct osmocom_ms *ms)
 {
     struct osmosap_entity *sap = &ms->sap_entity;
-    int rc;

+    LOGP(DSAP, LOGL_INFO, "init SAP client\n");
     sap->sap_state = SAP_NOT_CONNECTED;
     sap->max_msg_size = GSM_SAP_LENGTH;

-    LOGP(DSAP, LOGL_INFO, "init SAP client\n");
-
-    if(ms->settings.sap_socket_path){
-        rc = sap_open(ms, ms->settings.sap_socket_path);
-        if (rc < 0) {
-            fprintf(stderr, "Failed during sap_open(), no SAP based SIM
reader\n");
-            ms->sap_wq.bfd.fd = -1;
-            return rc;
-        }
-    }
-
     return 0;
 }

diff --git a/src/host/layer23/src/common/sim.c
b/src/host/layer23/src/common/sim.c
index 8e8d7bf..9aad966 100644
--- a/src/host/layer23/src/common/sim.c
+++ b/src/host/layer23/src/common/sim.c
@@ -188,12 +188,12 @@  static int sim_apdu_send(struct osmocom_ms *ms,
uint8_t *data, uint16_t length)

     /* adding SAP client support
      * it makes more sense to do it here then in L1CTL */
-    if(ms->settings.sap_socket_path[0] == 0) {
-        LOGP(DSIM, LOGL_INFO, "Using built-in SIM reader\n");
-        l1ctl_tx_sim_req(ms, data, length);
-    } else {
+    if (ms->subscr.sim_type == GSM_SIM_TYPE_SAP) {
         LOGP(DSIM, LOGL_INFO, "Using SAP backend\n");
         osmosap_send_apdu(ms, data, length);
+    } else {
+        LOGP(DSIM, LOGL_INFO, "Using built-in SIM reader\n");
+        l1ctl_tx_sim_req(ms, data, length);
     }

     return 0;
diff --git a/src/host/layer23/src/mobile/app_mobile.c
b/src/host/layer23/src/mobile/app_mobile.c
index 3895ad6..e076741 100644
--- a/src/host/layer23/src/mobile/app_mobile.c
+++ b/src/host/layer23/src/mobile/app_mobile.c
@@ -115,6 +115,9 @@  int mobile_signal_cb(unsigned int subsys, unsigned int
signal,
                 set->test_rplmn_mnc, set->test_lac,
                 set->test_tmsi, set->test_imsi_attached);
             break;
+        case GSM_SIM_TYPE_SAP:
+            gsm_subscr_sapcard(ms);
+            break;
         default:
             /* no SIM, trigger PLMN selection process */
             nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
diff --git a/src/host/layer23/src/mobile/subscriber.c
b/src/host/layer23/src/mobile/subscriber.c
index ba3c328..455db50 100644
--- a/src/host/layer23/src/mobile/subscriber.c
+++ b/src/host/layer23/src/mobile/subscriber.c
@@ -28,6 +28,7 @@ 

 #include <osmocom/bb/common/logging.h>
 #include <osmocom/bb/common/osmocom_data.h>
+#include <osmocom/bb/common/sap_interface.h>
 #include <osmocom/bb/common/networks.h>
 #include <osmocom/bb/mobile/vty.h>

@@ -1256,3 +1257,55 @@  void gsm_subscr_dump(struct gsm_subscriber *subscr,
     }
 }

+/*
+ * SAP interface integration
+ */
+
+/* Attach SIM card over SAP */
+int gsm_subscr_sapcard(struct osmocom_ms *ms)
+{
+    struct gsm_subscriber *subscr = &ms->subscr;
+    struct msgb *nmsg;
+    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 */
+    vty_notify(ms, NULL);
+    vty_notify(ms, "Connecting to the SAP interface...\n");
+    rc = sap_open(ms, ms->settings.sap_socket_path);
+    if (rc < 0) {
+        LOGP(DSAP, LOGL_ERROR, "Failed during sap_open(), no SAP based SIM
reader\n");
+        vty_notify(ms, "SAP connection error!\n");
+        ms->sap_wq.bfd.fd = -1;
+
+        /* Detach SIM */
+        subscr->sim_valid = 0;
+        nmsg = gsm48_mmr_msgb_alloc(GSM48_MMR_NREG_REQ);
+        if (!nmsg)
+            return -ENOMEM;
+        gsm48_mmr_downmsg(ms, nmsg);
+
+        return rc;
+    }
+
+    return 0;
+}
+
+/* Deattach sapcard */
+int gsm_subscr_remove_sapcard(struct osmocom_ms *ms)
+{
+    return sap_close(ms);
+}
diff --git a/src/host/layer23/src/mobile/vty_interface.c
b/src/host/layer23/src/mobile/vty_interface.c
index 5782a17..5a1414d 100644
--- a/src/host/layer23/src/mobile/vty_interface.c
+++ b/src/host/layer23/src/mobile/vty_interface.c
@@ -534,6 +534,29 @@  DEFUN(sim_test_att, sim_test_att_cmd,
     return _sim_test_cmd(vty, argc, argv, 1);
 }

+DEFUN(sim_sap, sim_sap_cmd, "sim sap MS_NAME",
+    "SIM actions\nAttach SIM over SAP interface\n"
+    "Name of MS (see \"show ms\")\n")
+{
+    struct osmocom_ms *ms;
+
+    ms = get_ms(argv[0], vty);
+    if (!ms)
+        return CMD_WARNING;
+
+    if (ms->subscr.sim_valid) {
+        vty_out(vty, "SIM already attached, remove first!%s",
+            VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+
+    if (gsm_subscr_sapcard(ms) != 0) {
+        return CMD_WARNING;
+    }
+
+    return CMD_SUCCESS;
+}
+
 DEFUN(sim_reader, sim_reader_cmd, "sim reader MS_NAME",
     "SIM actions\nAttach SIM from reader\nName of MS (see \"show ms\")")
 {
@@ -568,8 +591,11 @@  DEFUN(sim_remove, sim_remove_cmd, "sim remove MS_NAME",
         return CMD_WARNING;
     }

-    gsm_subscr_remove(ms);
+    if (ms->subscr.sim_type == GSM_SIM_TYPE_SAP) {
+        gsm_subscr_remove_sapcard(ms);
+    }

+    gsm_subscr_remove(ms);
     return CMD_SUCCESS;
 }

@@ -1067,7 +1093,7 @@  DEFUN(cfg_no_gps_enable, cfg_no_gps_enable_cmd, "no
gps enable",
 }

 #ifdef _HAVE_GPSD
-DEFUN(cfg_gps_host, cfg_gps_host_cmd, "gps host HOST:PORT",
+DEFUN(cfg_gpsd_host, cfg_gpsd_host_cmd, "gpsd host HOST:PORT",
     "GPS receiver\nSelect gpsd host and port\n"
     "IP and port (optional) of the host running gpsd")