[layer23] Cleanup of mobile application
[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
28 #include <osmocom/bb/common/osmocom_data.h>
29 #include <osmocom/bb/common/l1l2_interface.h>
30 #include <osmocom/bb/common/l1ctl.h>
31 #include <osmocom/bb/common/lapdm.h>
32 #include <osmocom/bb/common/logging.h>
33 #include <osmocom/bb/common/gps.h>
34 #include <osmocom/bb/mobile/gsm48_rr.h>
35 #include <osmocom/bb/mobile/vty.h>
36 #include <osmocom/bb/mobile/app_mobile.h>
37 #include <osmocom/bb/mobile/mncc.h>
38 #include <osmocom/vty/telnet_interface.h>
39
40 #include <osmocore/msgb.h>
41 #include <osmocore/talloc.h>
42 #include <osmocore/select.h>
43 #include <osmocore/signal.h>
44
45 extern void *l23_ctx;
46 extern struct llist_head ms_list;
47 extern int vty_reading;
48
49 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg);
50 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg);
51 int (*mncc_recv_app)(struct osmocom_ms *ms, int, void *);
52 static int quit;
53
54 /* handle ms instance */
55 int mobile_work(struct osmocom_ms *ms)
56 {
57         int work = 0, w;
58
59         do {
60                 w = 0;
61                 w |= gsm48_rsl_dequeue(ms);
62                 w |= gsm48_rr_dequeue(ms);
63                 w |= gsm48_mmxx_dequeue(ms);
64                 w |= gsm48_mmr_dequeue(ms);
65                 w |= gsm48_mmevent_dequeue(ms);
66                 w |= gsm322_plmn_dequeue(ms);
67                 w |= gsm322_cs_dequeue(ms);
68                 w |= gsm_sim_job_dequeue(ms);
69                 w |= mncc_dequeue(ms);
70                 if (w)
71                         work = 1;
72         } while (w);
73         return work;
74 }
75
76 /* run ms instance, if layer1 is available */
77 int mobile_signal_cb(unsigned int subsys, unsigned int signal,
78                      void *handler_data, void *signal_data)
79 {
80         struct osmocom_ms *ms;
81         struct gsm_settings *set;
82         struct msgb *nmsg;
83
84         if (subsys != SS_L1CTL)
85                 return 0;
86
87         switch (signal) {
88         case S_L1CTL_RESET:
89                 ms = signal_data;
90                 set = &ms->settings;
91
92                 if (ms->started)
93                         break;
94
95                 /* insert test card, if enabled */
96                 switch (set->sim_type) {
97                 case GSM_SIM_TYPE_READER:
98                         /* trigger sim card reader process */
99                         gsm_subscr_simcard(ms);
100                         break;
101                 case GSM_SIM_TYPE_TEST:
102                         gsm_subscr_testcard(ms, set->test_rplmn_mcc,
103                                                 set->test_rplmn_mnc);
104                         break;
105                 default:
106                         /* no SIM, trigger PLMN selection process */
107                         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
108                         if (!nmsg)
109                                 return -ENOMEM;
110                         gsm322_plmn_sendmsg(ms, nmsg);
111                         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
112                         if (!nmsg)
113                                 return -ENOMEM;
114                         gsm322_cs_sendmsg(ms, nmsg);
115                 }
116
117                 ms->started = 1;
118         }
119         return 0;
120 }
121
122 /* power-off ms instance */
123 int mobile_exit(struct osmocom_ms *ms, int force)
124 {
125         struct gsm48_mmlayer *mm = &ms->mmlayer;
126
127         if (!force && ms->started) {
128                 struct msgb *nmsg;
129
130                 ms->shutdown = 1; /* going down */
131                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
132                 if (!nmsg)
133                         return -ENOMEM;
134                 gsm48_mmevent_msg(mm->ms, nmsg);
135
136                 return -EBUSY;
137         }
138
139         gsm322_exit(ms);
140         gsm48_mm_exit(ms);
141         gsm48_rr_exit(ms);
142         gsm_subscr_exit(ms);
143         gsm48_cc_exit(ms);
144         gsm_sim_exit(ms);
145         lapdm_exit(&ms->l2_entity.lapdm_acch);
146         lapdm_exit(&ms->l2_entity.lapdm_dcch);
147
148         ms->shutdown = 2; /* being down */
149         vty_notify(ms, NULL);
150         vty_notify(ms, "Power off!\n");
151         printf("Power off! (MS %s)\n", ms->name);
152
153         return 0;
154 }
155
156 /* power-on ms instance */
157 int mobile_init(struct osmocom_ms *ms)
158 {
159         int rc;
160
161         lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
162         lapdm_init(&ms->l2_entity.lapdm_acch, ms);
163         gsm_sim_init(ms);
164         gsm48_cc_init(ms);
165         gsm_subscr_init(ms);
166         gsm48_rr_init(ms);
167         gsm48_mm_init(ms);
168         INIT_LLIST_HEAD(&ms->trans_list);
169         gsm322_init(ms);
170
171         rc = layer2_open(ms, ms->settings.layer2_socket_path);
172         if (rc < 0) {
173                 fprintf(stderr, "Failed during layer2_open()\n");
174                 ms->l2_wq.bfd.fd = -1;
175                 mobile_exit(ms, 1);
176                 return rc;
177         }
178
179 #if 0
180         rc = sap_open(ms, ms->settings.sap_socket_path);
181         if (rc < 0) {
182                 fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
183                 ms->sap_wq.bfd.fd = -1;
184                 mobile_exit(ms, 1);
185                 return rc;
186         }
187 #endif
188
189         if (mncc_recv_app)
190                 ms->cclayer.mncc_recv = mncc_recv_app;
191         else if (ms->settings.ch_cap == GSM_CAP_SDCCH)
192                 ms->cclayer.mncc_recv = mncc_recv_dummy;
193         else
194                 ms->cclayer.mncc_recv = mncc_recv_mobile;
195
196         gsm_random_imei(&ms->settings);
197
198         ms->shutdown = 0;
199         ms->started = 0;
200
201         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
202         printf("Mobile '%s' initialized, please start phone now!\n", ms->name);
203         return 0;
204 }
205
206 /* create ms instance */
207 struct osmocom_ms *mobile_new(char *name)
208 {
209         static struct osmocom_ms *ms;
210
211         ms = talloc_zero(l23_ctx, struct osmocom_ms);
212         if (!ms) {
213                 fprintf(stderr, "Failed to allocate MS\n");
214                 exit(1);
215         }
216         llist_add_tail(&ms->entity, &ms_list);
217
218         strcpy(ms->name, name);
219
220         ms->l2_wq.bfd.fd = -1;
221         ms->sap_wq.bfd.fd = -1;
222
223         gsm_support_init(ms);
224         gsm_settings_init(ms);
225
226         ms->shutdown = 2; /* being down */
227
228         if (mncc_recv_app) {
229                 struct msgb *msg;
230
231                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
232                 if (msg) {
233                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
234
235                         mncc->msg_type = MS_NEW;
236                         mncc_recv_app(ms, mncc->msg_type, mncc);
237                 }
238                 ms->cclayer.mncc_recv = mncc_recv_app;
239         } else
240                 ms->cclayer.mncc_recv = mncc_recv_dummy;
241
242         return ms;
243 }
244
245 /* destroy ms instance */
246 int mobile_delete(struct osmocom_ms *ms, int force)
247 {
248         int rc;
249
250         ms->deleting = 1;
251
252         if (ms->shutdown == 0 || (ms->shutdown == 1 && force)) {
253                 rc = mobile_exit(ms, force);
254                 if (rc < 0)
255                         return rc;
256         }
257
258         if (mncc_recv_app) {
259                 struct msgb *msg;
260
261                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
262                 if (msg) {
263                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
264
265                         mncc->msg_type = MS_DELETE;
266                         mncc_recv_app(ms, mncc->msg_type, mncc);
267                 }
268         }
269
270         return 0;
271 }
272
273 /* handle global shutdown */
274 int global_signal_cb(unsigned int subsys, unsigned int signal,
275                      void *handler_data, void *signal_data)
276 {
277         struct osmocom_ms *ms, *ms2;
278
279         if (subsys != SS_GLOBAL)
280                 return 0;
281
282         switch (signal) {
283         case S_GLOBAL_SHUTDOWN:
284                 llist_for_each_entry_safe(ms, ms2, &ms_list, entity)
285                         mobile_delete(ms, quit);
286
287                 /* if second signal is received, force to exit */
288                 quit = 1;
289                 break;
290         }
291         return 0;
292 }
293
294 /* global work handler */
295 int l23_app_work(int *_quit)
296 {
297         struct osmocom_ms *ms, *ms2;
298         int work = 0;
299
300         llist_for_each_entry_safe(ms, ms2, &ms_list, entity) {
301                 if (ms->shutdown != 2)
302                         work |= mobile_work(ms);
303                 if (ms->shutdown == 2) {
304                         if (ms->l2_wq.bfd.fd > -1) {
305                                 layer2_close(ms);
306                                 ms->l2_wq.bfd.fd = -1;
307                         }
308
309                         if (ms->sap_wq.bfd.fd > -1) {
310                                 sap_close(ms);
311                                 ms->sap_wq.bfd.fd = -1;
312                         }
313
314                         if (ms->deleting) {
315                                 gsm_settings_exit(ms);
316                                 llist_del(&ms->entity);
317                                 talloc_free(ms);
318                                 work = 1;
319                         }
320                 }
321         }
322
323         /* return, if a shutdown was scheduled (quit = 1) */
324         *_quit = quit;
325         return work;
326 }
327
328 /* global exit */
329 int l23_app_exit(void)
330 {
331         unregister_signal_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
332         unregister_signal_handler(SS_L1CTL, &mobile_signal_cb, NULL);
333         unregister_signal_handler(SS_GLOBAL, &global_signal_cb, NULL);
334
335         gps_close();
336
337         return 0;
338 }
339
340 static struct vty_app_info vty_info = {
341         .name = "OsmocomBB",
342         .version = PACKAGE_VERSION,
343         .go_parent_cb = ms_vty_go_parent,
344 };
345
346 /* global init */
347 int l23_app_init(int (*mncc_recv)(struct osmocom_ms *ms, int, void *),
348         const char *config_file, uint16_t vty_port)
349 {
350         struct telnet_connection dummy_conn;
351         int rc;
352
353         mncc_recv_app = mncc_recv;
354
355         gps_init();
356
357         vty_init(&vty_info);
358         ms_vty_init();
359         dummy_conn.priv = NULL;
360         vty_reading = 1;
361         rc = vty_read_config_file(config_file, &dummy_conn);
362         if (rc < 0) {
363                 fprintf(stderr, "Failed to parse the config file: '%s'\n",
364                         config_file);
365                 fprintf(stderr, "Please check or create config file using: "
366                         "'touch %s'\n", config_file);
367                 return rc;
368         }
369         vty_reading = 0;
370         telnet_init(l23_ctx, NULL, vty_port);
371         if (rc < 0)
372                 return rc;
373         printf("VTY available on port %u.\n", vty_port);
374
375         register_signal_handler(SS_GLOBAL, &global_signal_cb, NULL);
376         register_signal_handler(SS_L1CTL, &mobile_signal_cb, NULL);
377         register_signal_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
378
379         if (llist_empty(&ms_list)) {
380                 struct osmocom_ms *ms;
381
382                 printf("No Mobile Station defined, creating: MS '1'\n");
383                 ms = mobile_new("1");
384                 if (ms)
385                         mobile_init(ms);
386         }
387
388         quit = 0;
389
390         return 0;
391 }
392