From bec943f0b7b46680c107d9ee634a6e281222aa20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AF=D0=BD=D0=B8=D1=86=D0=BA=D0=B8=D0=B9=20=D0=92=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=B8=D0=BC?= Date: Sun, 10 Jan 2016 19:14:18 +0600 Subject: host/mobile: Improved SAP interface integration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Written-by: Яницкий Ва дим Signed-off-by: Sylvain Munaut --- .../include/osmocom/bb/common/sap_interface.h | 1 + .../layer23/include/osmocom/bb/mobile/subscriber.h | 5 +- src/host/layer23/src/common/sap_interface.c | 16 +------ 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(+), 22 deletions(-) (limited to 'src/host') 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 bf19356c..e4e64cef 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 79a2ecca..ac785d4a 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 a56f4f28..22f95503 100644 --- a/src/host/layer23/src/common/sap_interface.c +++ b/src/host/layer23/src/common/sap_interface.c @@ -500,7 +500,6 @@ int sap_open(struct osmocom_ms *ms, const char *socket_path) { ssize_t rc; struct sockaddr_un local; - struct gsm_settings *set = &ms->settings; ms->sap_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0); if (ms->sap_wq.bfd.fd < 0) { @@ -515,7 +514,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 +581,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 8e8d7bfe..9aad9666 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 3895ad67..e0767416 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 ba3c3281..455db506 100644 --- a/src/host/layer23/src/mobile/subscriber.c +++ b/src/host/layer23/src/mobile/subscriber.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -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 4e5c8645..eda181a9 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); -- cgit v1.2.3