[layer23] Fix of GPS states
[osmocom-bb.git] / src / host / layer23 / src / mobile / app_mobile.c
index aa62be1..6ba4b9e 100644 (file)
@@ -34,6 +34,7 @@
 #include <osmocom/bb/mobile/gsm48_rr.h>
 #include <osmocom/bb/mobile/sysinfo.h>
 #include <osmocom/bb/mobile/vty.h>
+#include <osmocom/bb/mobile/gps.h>
 #include <osmocom/vty/telnet_interface.h>
 
 #include <osmocore/msgb.h>
@@ -45,10 +46,12 @@ extern struct log_target *stderr_target;
 static const char *config_file = "/etc/osmocom/osmocom.cfg";
 extern void *l23_ctx;
 extern unsigned short vty_port;
+extern int vty_reading;
 
-static int started = 0;
+int mobile_started = 0;
 
 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg);
+int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg);
 
 int mobile_work(struct osmocom_ms *ms)
 {
@@ -63,6 +66,7 @@ int mobile_work(struct osmocom_ms *ms)
                w |= gsm48_mmevent_dequeue(ms);
                w |= gsm322_plmn_dequeue(ms);
                w |= gsm322_cs_dequeue(ms);
+               w |= gsm_sim_job_dequeue(ms);
                w |= mncc_dequeue(ms);
                if (w)
                        work = 1;
@@ -82,24 +86,35 @@ static int signal_cb(unsigned int subsys, unsigned int signal,
 
        switch (signal) {
        case S_L1CTL_RESET:
-               if (started)
+               if (mobile_started)
                        break;
-               started = 1;
+
                ms = signal_data;
                set = &ms->settings;
+
                /* insert test card, if enabled */
-               if (set->simtype == GSM_SIM_TYPE_TEST)
+               switch (set->sim_type) {
+               case GSM_SIM_TYPE_READER:
+                       /* trigger sim card reader process */
+                       gsm_subscr_simcard(ms);
+                       break;
+               case GSM_SIM_TYPE_TEST:
                        gsm_subscr_testcard(ms, set->test_rplmn_mcc,
                                                set->test_rplmn_mnc);
-               /* start PLMN + cell selection process */
-               nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
-               if (!nmsg)
-                       return -ENOMEM;
-               gsm322_plmn_sendmsg(ms, nmsg);
-               nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
-               if (!nmsg)
-                       return -ENOMEM;
-               gsm322_cs_sendmsg(ms, nmsg);
+                       break;
+               default:
+                       /* no SIM, trigger PLMN selection process */
+                       nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
+                       if (!nmsg)
+                               return -ENOMEM;
+                       gsm322_plmn_sendmsg(ms, nmsg);
+                       nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
+                       if (!nmsg)
+                               return -ENOMEM;
+                       gsm322_cs_sendmsg(ms, nmsg);
+               }
+
+               mobile_started = 1;
        }
        return 0;
 }
@@ -108,7 +123,7 @@ int mobile_exit(struct osmocom_ms *ms)
 {
        struct gsm48_mmlayer *mm = &ms->mmlayer;
 
-       if (!mm->power_off && started) {
+       if (!mm->power_off && mobile_started) {
                struct msgb *nmsg;
 
                mm->power_off = 1;
@@ -127,11 +142,14 @@ int mobile_exit(struct osmocom_ms *ms)
        signal(SIGPIPE, SIG_DFL);
 
        unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
+       gps_close();
        gsm322_exit(ms);
        gsm48_mm_exit(ms);
        gsm48_rr_exit(ms);
        gsm_subscr_exit(ms);
        gsm48_cc_exit(ms);
+       gsm_sim_exit(ms);
+       gsm_settings_exit(ms);
 
        printf("Power off!\n");
 
@@ -149,19 +167,21 @@ int l23_app_init(struct osmocom_ms *ms)
        int rc;
        struct telnet_connection dummy_conn;
 
-//     log_parse_category_mask(stderr_target, "DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
-       log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
+//     log_parse_category_mask(stderr_target, "DL1C:DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
+       log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
+       log_set_log_level(stderr_target, LOGL_INFO);
 
        srand(time(NULL));
 
+       gps_init();
+       gsm_support_init(ms);
+       gsm_sim_init(ms);
        gsm_settings_init(ms);
        gsm48_cc_init(ms);
-       gsm_support_init(ms);
        gsm_subscr_init(ms);
        gsm48_rr_init(ms);
        gsm48_mm_init(ms);
        INIT_LLIST_HEAD(&ms->trans_list);
-       ms->cclayer.mncc_recv = mncc_recv_mobile;
        gsm322_init(ms);
 
        l23_app_work = mobile_work;
@@ -171,6 +191,7 @@ int l23_app_init(struct osmocom_ms *ms)
        vty_init(&vty_info);
        ms_vty_init();
        dummy_conn.priv = NULL;
+       vty_reading = 1;
        rc = vty_read_config_file(config_file, &dummy_conn);
        if (rc < 0) {
                fprintf(stderr, "Failed to parse the config file: '%s'\n",
@@ -179,13 +200,21 @@ int l23_app_init(struct osmocom_ms *ms)
                        "'touch %s'\n", config_file);
                return rc;
        }
+       vty_reading = 0;
        telnet_init(l23_ctx, NULL, vty_port);
        if (rc < 0)
                return rc;
+
+       if (ms->settings.ch_cap == GSM_CAP_SDCCH)
+               ms->cclayer.mncc_recv = mncc_recv_dummy;
+       else
+               ms->cclayer.mncc_recv = mncc_recv_mobile;
+
        printf("VTY available on port %u.\n", vty_port);
 
        gsm_random_imei(&ms->settings);
 
+       l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
        printf("Mobile initialized, please start phone now!\n");
        return 0;
 }