[2/2] host/mobile: Improved SAP interface integration

Submitted by Вадим Яницкий on Jan. 10, 2016, 1:14 p.m.

Details

Message ID 1452431658-13570-2-git-send-email-axilirator@gmail.com
State New
Headers show

Commit Message

Вадим Яницкий Jan. 10, 2016, 1:14 p.m.
From: Яницкий Ва дим <axilirator@gmail.com>

1) Now the SAP interface is selectable as SIM source using the 'sim sap'
   command in VTY.
2) SAP connection starts only if it is configured as SIM source.
3) Fixed sap_socket_path configuration r/w errors.
---
 .../include/osmocom/bb/common/sap_interface.h      |  1 +
 .../layer23/include/osmocom/bb/mobile/subscriber.h |  5 +-
 src/host/layer23/src/common/sap_interface.c        | 15 +-----
 src/host/layer23/src/common/sim.c                  |  8 ++--
 src/host/layer23/src/mobile/app_mobile.c           |  3 ++
 src/host/layer23/src/mobile/subscriber.c           | 53 ++++++++++++++++++++++
 src/host/layer23/src/mobile/vty_interface.c        | 40 ++++++++++++++--
 7 files changed, 104 insertions(+), 21 deletions(-)

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 73aaa78..7cb2259 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;
 }
 
@@ -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);
@@ -1557,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;
@@ -1574,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;
@@ -2801,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);