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