layer23: make LAPDm code mostly independent of 'struct osmocom_ms'
[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_channel_exit(&ms->lapdm_channel);
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         gsm_settings_arfcn(ms);
162         ms->lapdm_channel.lapdm_dcch.l1_ctx = ms;
163         ms->lapdm_channel.lapdm_dcch.l3_ctx = ms;
164         ms->lapdm_channel.lapdm_acch.l1_ctx = ms;
165         ms->lapdm_channel.lapdm_acch.l3_ctx = ms;
166         lapdm_channel_init(&ms->lapdm_channel);
167         gsm_sim_init(ms);
168         gsm48_cc_init(ms);
169         gsm_subscr_init(ms);
170         gsm48_rr_init(ms);
171         gsm48_mm_init(ms);
172         INIT_LLIST_HEAD(&ms->trans_list);
173         gsm322_init(ms);
174
175         rc = layer2_open(ms, ms->settings.layer2_socket_path);
176         if (rc < 0) {
177                 fprintf(stderr, "Failed during layer2_open()\n");
178                 ms->l2_wq.bfd.fd = -1;
179                 mobile_exit(ms, 1);
180                 return rc;
181         }
182
183 #if 0
184         rc = sap_open(ms, ms->settings.sap_socket_path);
185         if (rc < 0) {
186                 fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
187                 ms->sap_wq.bfd.fd = -1;
188                 mobile_exit(ms, 1);
189                 return rc;
190         }
191 #endif
192
193         if (mncc_recv_app)
194                 ms->cclayer.mncc_recv = mncc_recv_app;
195         else if (ms->settings.ch_cap == GSM_CAP_SDCCH)
196                 ms->cclayer.mncc_recv = mncc_recv_dummy;
197         else
198                 ms->cclayer.mncc_recv = mncc_recv_mobile;
199
200         gsm_random_imei(&ms->settings);
201
202         ms->shutdown = 0;
203         ms->started = 0;
204
205         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
206         printf("Mobile '%s' initialized, please start phone now!\n", ms->name);
207         return 0;
208 }
209
210 /* create ms instance */
211 struct osmocom_ms *mobile_new(char *name)
212 {
213         static struct osmocom_ms *ms;
214
215         ms = talloc_zero(l23_ctx, struct osmocom_ms);
216         if (!ms) {
217                 fprintf(stderr, "Failed to allocate MS\n");
218                 exit(1);
219         }
220         llist_add_tail(&ms->entity, &ms_list);
221
222         strcpy(ms->name, name);
223
224         ms->l2_wq.bfd.fd = -1;
225         ms->sap_wq.bfd.fd = -1;
226
227         gsm_support_init(ms);
228         gsm_settings_init(ms);
229
230         ms->shutdown = 2; /* being down */
231
232         if (mncc_recv_app) {
233                 struct msgb *msg;
234
235                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
236                 if (msg) {
237                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
238
239                         mncc->msg_type = MS_NEW;
240                         mncc_recv_app(ms, mncc->msg_type, mncc);
241                 }
242                 ms->cclayer.mncc_recv = mncc_recv_app;
243         } else
244                 ms->cclayer.mncc_recv = mncc_recv_dummy;
245
246         return ms;
247 }
248
249 /* destroy ms instance */
250 int mobile_delete(struct osmocom_ms *ms, int force)
251 {
252         int rc;
253
254         ms->deleting = 1;
255
256         if (ms->shutdown == 0 || (ms->shutdown == 1 && force)) {
257                 rc = mobile_exit(ms, force);
258                 if (rc < 0)
259                         return rc;
260         }
261
262         if (mncc_recv_app) {
263                 struct msgb *msg;
264
265                 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
266                 if (msg) {
267                         struct gsm_mncc *mncc = (struct gsm_mncc *)msg->data;
268
269                         mncc->msg_type = MS_DELETE;
270                         mncc_recv_app(ms, mncc->msg_type, mncc);
271                 }
272         }
273
274         return 0;
275 }
276
277 /* handle global shutdown */
278 int global_signal_cb(unsigned int subsys, unsigned int signal,
279                      void *handler_data, void *signal_data)
280 {
281         struct osmocom_ms *ms, *ms2;
282
283         if (subsys != SS_GLOBAL)
284                 return 0;
285
286         switch (signal) {
287         case S_GLOBAL_SHUTDOWN:
288                 llist_for_each_entry_safe(ms, ms2, &ms_list, entity)
289                         mobile_delete(ms, quit);
290
291                 /* if second signal is received, force to exit */
292                 quit = 1;
293                 break;
294         }
295         return 0;
296 }
297
298 /* global work handler */
299 int l23_app_work(int *_quit)
300 {
301         struct osmocom_ms *ms, *ms2;
302         int work = 0;
303
304         llist_for_each_entry_safe(ms, ms2, &ms_list, entity) {
305                 if (ms->shutdown != 2)
306                         work |= mobile_work(ms);
307                 if (ms->shutdown == 2) {
308                         if (ms->l2_wq.bfd.fd > -1) {
309                                 layer2_close(ms);
310                                 ms->l2_wq.bfd.fd = -1;
311                         }
312
313                         if (ms->sap_wq.bfd.fd > -1) {
314                                 sap_close(ms);
315                                 ms->sap_wq.bfd.fd = -1;
316                         }
317
318                         if (ms->deleting) {
319                                 gsm_settings_exit(ms);
320                                 llist_del(&ms->entity);
321                                 talloc_free(ms);
322                                 work = 1;
323                         }
324                 }
325         }
326
327         /* return, if a shutdown was scheduled (quit = 1) */
328         *_quit = quit;
329         return work;
330 }
331
332 /* global exit */
333 int l23_app_exit(void)
334 {
335         osmo_signal_unregister_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
336         osmo_signal_unregister_handler(SS_L1CTL, &mobile_signal_cb, NULL);
337         osmo_signal_unregister_handler(SS_GLOBAL, &global_signal_cb, NULL);
338
339         osmo_gps_close();
340
341         return 0;
342 }
343
344 static struct vty_app_info vty_info = {
345         .name = "OsmocomBB",
346         .version = PACKAGE_VERSION,
347         .go_parent_cb = ms_vty_go_parent,
348 };
349
350 /* global init */
351 int l23_app_init(int (*mncc_recv)(struct osmocom_ms *ms, int, void *),
352         const char *config_file, uint16_t vty_port)
353 {
354         struct telnet_connection dummy_conn;
355         int rc = 0;
356
357         mncc_recv_app = mncc_recv;
358
359         osmo_gps_init();
360
361         vty_init(&vty_info);
362         ms_vty_init();
363         dummy_conn.priv = NULL;
364         vty_reading = 1;
365         if (config_file != NULL) {
366                 rc = vty_read_config_file(config_file, &dummy_conn);
367                 if (rc < 0) {
368                         fprintf(stderr, "Failed to parse the config file:"
369                                         " '%s'\n", config_file);
370                         fprintf(stderr, "Please check or create config file"
371                                         " using: 'touch %s'\n", config_file);
372                         return rc;
373                 }
374         }
375         vty_reading = 0;
376         telnet_init(l23_ctx, NULL, vty_port);
377         if (rc < 0)
378                 return rc;
379         printf("VTY available on port %u.\n", vty_port);
380
381         osmo_signal_register_handler(SS_GLOBAL, &global_signal_cb, NULL);
382         osmo_signal_register_handler(SS_L1CTL, &mobile_signal_cb, NULL);
383         osmo_signal_register_handler(SS_L1CTL, &gsm322_l1_signal, NULL);
384
385         if (llist_empty(&ms_list)) {
386                 struct osmocom_ms *ms;
387
388                 printf("No Mobile Station defined, creating: MS '1'\n");
389                 ms = mobile_new("1");
390                 if (ms)
391                         mobile_init(ms);
392         }
393
394         quit = 0;
395
396         return 0;
397 }
398