Fixed descriptions of VTY interface commands.
[osmocom-bb.git] / src / host / layer23 / src / app_mobile.c
index 2e229a0..46e0e06 100644 (file)
@@ -23,6 +23,8 @@
  */
 
 #include <errno.h>
+#include <signal.h>
+#include <time.h>
 
 #include <osmocom/osmocom_data.h>
 #include <osmocom/l1ctl.h>
 #include <osmocom/lapdm.h>
 #include <osmocom/gsmtap_util.h>
 #include <osmocom/logging.h>
+#include <osmocom/telnet_interface.h>
+#include <osmocom/file.h>
 
 #include <osmocore/msgb.h>
 #include <osmocore/talloc.h>
 #include <osmocore/select.h>
 #include <osmocore/signal.h>
 
-extern int (*app_work) (struct osmocom_ms *ms);
+extern struct log_target *stderr_target;
+static const char *config_file = "osmocom.cfg";
+
+static int started = 0;
+
 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg);
 
 int mobile_work(struct osmocom_ms *ms)
@@ -72,8 +80,16 @@ static int signal_cb(unsigned int subsys, unsigned int signal,
 
        switch (signal) {
        case S_L1CTL_RESET:
+               if (started) {
+                       printf("L1_RESET, TODO: resend last radio request "
+                               "(CCCH / dedicated / power scan)\n");
+                       break;
+               }
+               started = 1;
                ms = signal_data;
-               gsm_subscr_testcard(ms, 1, 1, "0000000000");
+               /* insert test card, if enabled */
+               if (ms->settings.simtype == GSM_SIM_TYPE_TEST)
+                       gsm_subscr_testcard(ms);
                /* start PLMN + cell selection process */
                nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
                if (!nmsg)
@@ -87,29 +103,72 @@ static int signal_cb(unsigned int subsys, unsigned int signal,
        return 0;
 }
 
+int mobile_exit(struct osmocom_ms *ms)
+{
+       struct gsm48_mmlayer *mm = &ms->mmlayer;
+
+       if (!mm->power_off && started) {
+               struct msgb *nmsg;
+
+               mm->power_off = 1;
+               nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
+               if (!nmsg)
+                       return -ENOMEM;
+               gsm48_mmevent_msg(mm->ms, nmsg);
+
+               return -EBUSY;
+       }
+
+       /* in case there is a lockup during exit */
+       signal(SIGINT, SIG_DFL);
+       signal(SIGHUP, SIG_DFL);
+       signal(SIGTERM, SIG_DFL);
+       signal(SIGPIPE, SIG_DFL);
+
+       unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
+       gsm322_exit(ms);
+       gsm48_mm_exit(ms);
+       gsm48_rr_exit(ms);
+       gsm_subscr_exit(ms);
+       gsm48_cc_exit(ms);
+
+       return 0;
+}
+
 int l23_app_init(struct osmocom_ms *ms)
 {
+       int rc;
+
+       log_parse_category_mask(stderr_target, "DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG");
+
+       srand(time(NULL));
+
+       gsm_settings_init(ms);
+       gsm48_cc_init(ms);
        gsm_support_init(ms);
        gsm_subscr_init(ms);
-       gsm48_sysinfo_init(ms);
        gsm48_rr_init(ms);
        gsm48_mm_init(ms);
-       gsm48_cc_init(ms);
        INIT_LLIST_HEAD(&ms->trans_list);
        ms->cclayer.mncc_recv = mncc_recv_dummy;
        gsm322_init(ms);
+
        l23_app_work = mobile_work;
-       return register_signal_handler(SS_L1CTL, &signal_cb, NULL);
-}
+       register_signal_handler(SS_L1CTL, &signal_cb, NULL);
+       l23_app_exit = mobile_exit;
+
+       telnet_init(ms, 4242);
+       rc = vty_read_config_file(config_file);
+       if (rc < 0) {
+               fprintf(stderr, "Failed to parse the config file: '%s'\n",
+                       config_file);
+               fprintf(stderr, "Please check or create config file using: "
+                       "'touch %s%s'\n", OSMOCOM_CONFDIR, config_file);
+               return rc;
+       }
+
+       gsm_random_imei(&ms->settings);
 
-/* TODO handle this */
-int l23_app_exit(struct osmocom_ms *ms)
-{
-       gsm322_exit(ms);
-       gsm48_cc_exit(ms);
-       gsm48_mm_exit(ms);
-       gsm48_rr_exit(ms);
-       gsm_subscr_exit(ms);
        return 0;
 }