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