164f3edb11371e4579691e0c7d7ecd718c425dc6
[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         if (!strcmp(ms->settings.imei, "000000000000000")) {
207                 printf("***\nWarning: Mobile '%s' has default IMEI: %s\n",
208                         ms->name, ms->settings.imei);
209                 printf("This could relate your identitiy to other users with "
210                         "default IMEI.\n***\n");
211         }
212
213         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
214         printf("Mobile '%s' initialized, please start phone now!\n", ms->name);
215         return 0;
216 }
217
218 /* create ms instance */
219 struct osmocom_ms *mobile_new(char *name)
220 {
221         static struct osmocom_ms *ms;
222
223         ms = talloc_zero(l23_ctx, struct osmocom_ms);
224         if (!ms) {
225                 fprintf(stderr, "Failed to allocate MS\n");
226                 exit(1);
227         }
228         llist_add_tail(&ms->entity, &ms_list);
229
230         strcpy(ms->name, name);
231
232         ms->l2_wq.bfd.fd = -1;
233         ms->sap_wq.bfd.fd = -1;
234
235         gsm_support_init(ms);
236         gsm_settings_init(ms);
237
238         ms->shutdown = 2; /* being down */
239
240         if (mncc_recv_app) {
241                 struct msgb *msg;
242
243                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
244                 if (msg) {
245                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
246
247                         mncc->msg_type = MS_NEW;
248                         mncc_recv_app(ms, mncc->msg_type, mncc);
249                 }
250                 ms->cclayer.mncc_recv = mncc_recv_app;
251         } else
252                 ms->cclayer.mncc_recv = mncc_recv_dummy;
253
254         return ms;
255 }
256
257 /* destroy ms instance */
258 int mobile_delete(struct osmocom_ms *ms, int force)
259 {
260         int rc;
261
262         ms->deleting = 1;
263
264         if (ms->shutdown == 0 || (ms->shutdown == 1 && force)) {
265                 rc = mobile_exit(ms, force);
266                 if (rc < 0)
267                         return rc;
268         }
269
270         if (mncc_recv_app) {
271                 struct msgb *msg;
272
273                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
274                 if (msg) {
275                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
276
277                         mncc->msg_type = MS_DELETE;
278                         mncc_recv_app(ms, mncc->msg_type, mncc);
279                 }
280         }
281
282         return 0;
283 }
284
285 /* handle global shutdown */
286 int global_signal_cb(unsigned int subsys, unsigned int signal,
287                      void *handler_data, void *signal_data)
288 {
289         struct osmocom_ms *ms, *ms2;
290
291         if (subsys != SS_GLOBAL)
292                 return 0;
293
294         switch (signal) {
295         case S_GLOBAL_SHUTDOWN:
296                 llist_for_each_entry_safe(ms, ms2, &ms_list, entity)
297                         mobile_delete(ms, quit);
298
299                 /* if second signal is received, force to exit */
300                 quit = 1;
301                 break;
302         }
303         return 0;
304 }
305
306 /* global work handler */
307 int l23_app_work(int *_quit)
308 {
309         struct osmocom_ms *ms, *ms2;
310         int work = 0;
311
312         llist_for_each_entry_safe(ms, ms2, &ms_list, entity) {
313                 if (ms->shutdown != 2)
314                         work |= mobile_work(ms);
315                 if (ms->shutdown == 2) {
316                         if (ms->l2_wq.bfd.fd > -1) {
317                                 layer2_close(ms);
318                                 ms->l2_wq.bfd.fd = -1;
319                         }
320
321                         if (ms->sap_wq.bfd.fd > -1) {
322                                 sap_close(ms);
323                                 ms->sap_wq.bfd.fd = -1;
324                         }
325
326                         if (ms->deleting) {
327                                 gsm_settings_exit(ms);
328                                 llist_del(&ms->entity);
329                                 talloc_free(ms);
330                                 work = 1;
331                         }
332                 }
333         }
334
335         /* return, if a shutdown was scheduled (quit = 1) */
336         *_quit = quit;
337         return work;
338 }
339
340 /* global exit */
341 int l23_app_exit(void)
342 {
343         osmo_signal_unregister_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
344         osmo_signal_unregister_handler(SS_L1CTL, &mobile_signal_cb, NULL);
345         osmo_signal_unregister_handler(SS_GLOBAL, &global_signal_cb, NULL);
346
347         osmo_gps_close();
348
349         return 0;
350 }
351
352 static struct vty_app_info vty_info = {
353         .name = "OsmocomBB",
354         .version = PACKAGE_VERSION,
355         .go_parent_cb = ms_vty_go_parent,
356 };
357
358 /* global init */
359 int l23_app_init(int (*mncc_recv)(struct osmocom_ms *ms, int, void *),
360         const char *config_file, uint16_t vty_port)
361 {
362         struct telnet_connection dummy_conn;
363         int rc = 0;
364
365         mncc_recv_app = mncc_recv;
366
367         osmo_gps_init();
368
369         vty_init(&vty_info);
370         ms_vty_init();
371         dummy_conn.priv = NULL;
372         vty_reading = 1;
373         if (config_file != NULL) {
374                 rc = vty_read_config_file(config_file, &dummy_conn);
375                 if (rc < 0) {
376                         fprintf(stderr, "Failed to parse the config file:"
377                                         " '%s'\n", config_file);
378                         fprintf(stderr, "Please check or create config file"
379                                         " using: 'touch %s'\n", config_file);
380                         return rc;
381                 }
382         }
383         vty_reading = 0;
384         telnet_init(l23_ctx, NULL, vty_port);
385         if (rc < 0)
386                 return rc;
387         printf("VTY available on port %u.\n", vty_port);
388
389         osmo_signal_register_handler(SS_GLOBAL, &global_signal_cb, NULL);
390         osmo_signal_register_handler(SS_L1CTL, &mobile_signal_cb, NULL);
391         osmo_signal_register_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
392
393         if (llist_empty(&ms_list)) {
394                 struct osmocom_ms *ms;
395
396                 printf("No Mobile Station defined, creating: MS '1'\n");
397                 ms = mobile_new("1");
398                 if (ms)
399                         mobile_init(ms);
400         }
401
402         quit = 0;
403
404         return 0;
405 }
406