[layer23] request a layer1 reset on startup of layer2 applications
[osmocom-bb.git] / src / host / layer23 / src / mobile / app_mobile.c
1 /* "Application" code of the layer2/3 stack */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  */
24
25 #include <errno.h>
26 #include <signal.h>
27 #include <time.h>
28
29 #include <osmocom/bb/common/osmocom_data.h>
30 #include <osmocom/bb/common/l1ctl.h>
31 #include <osmocom/bb/common/l23_app.h>
32 #include <osmocom/bb/common/lapdm.h>
33 #include <osmocom/bb/common/logging.h>
34 #include <osmocom/bb/mobile/gsm48_rr.h>
35 #include <osmocom/bb/mobile/sysinfo.h>
36 #include <osmocom/bb/mobile/vty.h>
37 #include <osmocom/vty/telnet_interface.h>
38
39 #include <osmocore/msgb.h>
40 #include <osmocore/talloc.h>
41 #include <osmocore/select.h>
42 #include <osmocore/signal.h>
43
44 extern struct log_target *stderr_target;
45 static const char *config_file = "/etc/osmocom/osmocom.cfg";
46 extern void *l23_ctx;
47 extern unsigned short vty_port;
48
49 static int started = 0;
50
51 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg);
52
53 int mobile_work(struct osmocom_ms *ms)
54 {
55         int work = 0, w;
56
57         do {
58                 w = 0;
59                 w |= gsm48_rsl_dequeue(ms);
60                 w |= gsm48_rr_dequeue(ms);
61                 w |= gsm48_mmxx_dequeue(ms);
62                 w |= gsm48_mmr_dequeue(ms);
63                 w |= gsm48_mmevent_dequeue(ms);
64                 w |= gsm322_plmn_dequeue(ms);
65                 w |= gsm322_cs_dequeue(ms);
66                 w |= mncc_dequeue(ms);
67                 if (w)
68                         work = 1;
69         } while (w);
70         return work;
71 }
72
73 static int signal_cb(unsigned int subsys, unsigned int signal,
74                      void *handler_data, void *signal_data)
75 {
76         struct osmocom_ms *ms;
77         struct gsm_settings *set;
78         struct msgb *nmsg;
79
80         if (subsys != SS_L1CTL)
81                 return 0;
82
83         switch (signal) {
84         case S_L1CTL_RESET:
85                 if (started)
86                         break;
87                 started = 1;
88                 ms = signal_data;
89                 set = &ms->settings;
90                 /* insert test card, if enabled */
91                 if (set->simtype == GSM_SIM_TYPE_TEST)
92                         gsm_subscr_testcard(ms, set->test_rplmn_mcc,
93                                                 set->test_rplmn_mnc);
94                 /* start PLMN + cell selection process */
95                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
96                 if (!nmsg)
97                         return -ENOMEM;
98                 gsm322_plmn_sendmsg(ms, nmsg);
99                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
100                 if (!nmsg)
101                         return -ENOMEM;
102                 gsm322_cs_sendmsg(ms, nmsg);
103         }
104         return 0;
105 }
106
107 int mobile_exit(struct osmocom_ms *ms)
108 {
109         struct gsm48_mmlayer *mm = &ms->mmlayer;
110
111         if (!mm->power_off && started) {
112                 struct msgb *nmsg;
113
114                 mm->power_off = 1;
115                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
116                 if (!nmsg)
117                         return -ENOMEM;
118                 gsm48_mmevent_msg(mm->ms, nmsg);
119
120                 return -EBUSY;
121         }
122
123         /* in case there is a lockup during exit */
124         signal(SIGINT, SIG_DFL);
125         signal(SIGHUP, SIG_DFL);
126         signal(SIGTERM, SIG_DFL);
127         signal(SIGPIPE, SIG_DFL);
128
129         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
130         gsm322_exit(ms);
131         gsm48_mm_exit(ms);
132         gsm48_rr_exit(ms);
133         gsm_subscr_exit(ms);
134         gsm48_cc_exit(ms);
135
136         printf("Power off!\n");
137
138         return 0;
139 }
140
141 static struct vty_app_info vty_info = {
142         .name = "OsmocomBB",
143         .version = PACKAGE_VERSION,
144         .go_parent_cb = ms_vty_go_parent,
145 };
146
147 int l23_app_init(struct osmocom_ms *ms)
148 {
149         int rc;
150         struct telnet_connection dummy_conn;
151
152 //      log_parse_category_mask(stderr_target, "DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
153         log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
154
155         srand(time(NULL));
156
157         gsm_settings_init(ms);
158         gsm48_cc_init(ms);
159         gsm_support_init(ms);
160         gsm_subscr_init(ms);
161         gsm48_rr_init(ms);
162         gsm48_mm_init(ms);
163         INIT_LLIST_HEAD(&ms->trans_list);
164         ms->cclayer.mncc_recv = mncc_recv_mobile;
165         gsm322_init(ms);
166
167         l23_app_work = mobile_work;
168         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
169         l23_app_exit = mobile_exit;
170
171         vty_init(&vty_info);
172         ms_vty_init();
173         dummy_conn.priv = NULL;
174         rc = vty_read_config_file(config_file, &dummy_conn);
175         if (rc < 0) {
176                 fprintf(stderr, "Failed to parse the config file: '%s'\n",
177                         config_file);
178                 fprintf(stderr, "Please check or create config file using: "
179                         "'touch %s'\n", config_file);
180                 return rc;
181         }
182         telnet_init(l23_ctx, NULL, vty_port);
183         if (rc < 0)
184                 return rc;
185         printf("VTY available on port %u.\n", vty_port);
186
187         gsm_random_imei(&ms->settings);
188
189         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
190         printf("Mobile initialized, please start phone now!\n");
191         return 0;
192 }
193