+ rc = layer2_open(ms, ms->settings.layer2_socket_path);
+ if (rc < 0) {
+ fprintf(stderr, "Failed during layer2_open()\n");
+ ms->l2_wq.bfd.fd = -1;
+ mobile_exit(ms, 1);
+ return rc;
+ }
+
+#if 0
+ rc = sap_open(ms, ms->settings.sap_socket_path);
+ if (rc < 0) {
+ fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
+ ms->sap_wq.bfd.fd = -1;
+ mobile_exit(ms, 1);
+ return rc;
+ }
+#endif
+
+ if (mncc_recv_app)
+ ms->cclayer.mncc_recv = mncc_recv_app;
+ else if (ms->settings.ch_cap == GSM_CAP_SDCCH)
+ ms->cclayer.mncc_recv = mncc_recv_dummy;
+ else
+ ms->cclayer.mncc_recv = mncc_recv_mobile;
+
+ gsm_random_imei(&ms->settings);
+
+ ms->shutdown = 0;
+ ms->started = 0;
+
+ if (!strcmp(ms->settings.imei, "000000000000000")) {
+ printf("***\nWarning: Mobile '%s' has default IMEI: %s\n",
+ ms->name, ms->settings.imei);
+ printf("This could relate your identitiy to other users with "
+ "default IMEI.\n***\n");
+ }
+
+ l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
+ printf("Mobile '%s' initialized, please start phone now!\n", ms->name);
+ return 0;
+}
+
+/* create ms instance */
+struct osmocom_ms *mobile_new(char *name)
+{
+ static struct osmocom_ms *ms;
+
+ ms = talloc_zero(l23_ctx, struct osmocom_ms);
+ if (!ms) {
+ fprintf(stderr, "Failed to allocate MS\n");
+ exit(1);
+ }
+ llist_add_tail(&ms->entity, &ms_list);
+
+ strcpy(ms->name, name);
+
+ ms->l2_wq.bfd.fd = -1;
+ ms->sap_wq.bfd.fd = -1;
+
+ gsm_support_init(ms);
+ gsm_settings_init(ms);
+
+ ms->shutdown = 2; /* being down */
+
+ if (mncc_recv_app) {
+ struct msgb *msg;
+
+ msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
+ if (msg) {
+ struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
+
+ mncc->msg_type = MS_NEW;
+ mncc_recv_app(ms, mncc->msg_type, mncc);
+ }
+ ms->cclayer.mncc_recv = mncc_recv_app;
+ } else
+ ms->cclayer.mncc_recv = mncc_recv_dummy;
+
+ return ms;
+}
+
+/* destroy ms instance */
+int mobile_delete(struct osmocom_ms *ms, int force)
+{
+ int rc;
+
+ ms->deleting = 1;
+
+ if (ms->shutdown == 0 || (ms->shutdown == 1 && force)) {
+ rc = mobile_exit(ms, force);
+ if (rc < 0)
+ return rc;
+ }
+
+ if (mncc_recv_app) {
+ struct msgb *msg;
+
+ msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
+ if (msg) {
+ struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
+
+ mncc->msg_type = MS_DELETE;
+ mncc_recv_app(ms, mncc->msg_type, mncc);
+ }
+ }
+
+ return 0;
+}
+
+/* handle global shutdown */
+int global_signal_cb(unsigned int subsys, unsigned int signal,
+ void *handler_data, void *signal_data)
+{
+ struct osmocom_ms *ms, *ms2;
+
+ if (subsys != SS_GLOBAL)
+ return 0;
+
+ switch (signal) {
+ case S_GLOBAL_SHUTDOWN:
+ llist_for_each_entry_safe(ms, ms2, &ms_list, entity)
+ mobile_delete(ms, quit);
+
+ /* if second signal is received, force to exit */
+ quit = 1;
+ break;
+ }
+ return 0;
+}
+
+/* global work handler */
+int l23_app_work(int *_quit)
+{
+ struct osmocom_ms *ms, *ms2;
+ int work = 0;
+
+ llist_for_each_entry_safe(ms, ms2, &ms_list, entity) {
+ if (ms->shutdown != 2)
+ work |= mobile_work(ms);
+ if (ms->shutdown == 2) {
+ if (ms->l2_wq.bfd.fd > -1) {
+ layer2_close(ms);
+ ms->l2_wq.bfd.fd = -1;
+ }
+
+ if (ms->sap_wq.bfd.fd > -1) {
+ sap_close(ms);
+ ms->sap_wq.bfd.fd = -1;
+ }
+
+ if (ms->deleting) {
+ gsm_settings_exit(ms);
+ llist_del(&ms->entity);
+ talloc_free(ms);
+ work = 1;
+ }
+ }
+ }
+
+ /* return, if a shutdown was scheduled (quit = 1) */
+ *_quit = quit;
+ return work;
+}
+
+/* global exit */
+int l23_app_exit(void)
+{
+ osmo_signal_unregister_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
+ osmo_signal_unregister_handler(SS_L1CTL, &mobile_signal_cb, NULL);
+ osmo_signal_unregister_handler(SS_GLOBAL, &global_signal_cb, NULL);
+
+ osmo_gps_close();
+
+ return 0;
+}
+
+static struct vty_app_info vty_info = {
+ .name = "OsmocomBB",
+ .version = PACKAGE_VERSION,
+ .go_parent_cb = ms_vty_go_parent,
+};
+
+/* global init */
+int l23_app_init(int (*mncc_recv)(struct osmocom_ms *ms, int, void *),
+ const char *config_file, uint16_t vty_port)
+{
+ struct telnet_connection dummy_conn;
+ int rc = 0;
+
+ mncc_recv_app = mncc_recv;
+
+ osmo_gps_init();