02f25418412b6f1e3d43f4adf53547b680ba904c
[osmocom-bb.git] / src / host / layer23 / src / 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/osmocom_data.h>
29 #include <osmocom/l1ctl.h>
30 #include <osmocom/l23_app.h>
31 #include <osmocom/gsm48_rr.h>
32 #include <osmocom/sysinfo.h>
33 #include <osmocom/lapdm.h>
34 #include <osmocom/gsmtap_util.h>
35 #include <osmocom/logging.h>
36
37 #include <osmocore/msgb.h>
38 #include <osmocore/talloc.h>
39 #include <osmocore/select.h>
40 #include <osmocore/signal.h>
41
42 extern struct log_target *stderr_target;
43
44 static int started = 0;
45
46 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg);
47
48 int mobile_work(struct osmocom_ms *ms)
49 {
50         int work = 0, w;
51
52         do {
53                 w = 0;
54                 w |= gsm48_rsl_dequeue(ms);
55                 w |= gsm48_rr_dequeue(ms);
56                 w |= gsm48_mmxx_dequeue(ms);
57                 w |= gsm48_mmr_dequeue(ms);
58                 w |= gsm48_mmevent_dequeue(ms);
59                 w |= gsm322_plmn_dequeue(ms);
60                 w |= gsm322_cs_dequeue(ms);
61                 w |= mncc_dequeue(ms);
62                 if (w)
63                         work = 1;
64         } while (w);
65         return work;
66 }
67
68 static int signal_cb(unsigned int subsys, unsigned int signal,
69                      void *handler_data, void *signal_data)
70 {
71         struct osmocom_ms *ms;
72         struct msgb *nmsg;
73
74         if (subsys != SS_L1CTL)
75                 return 0;
76
77         switch (signal) {
78         case S_L1CTL_RESET:
79                 if (started) {
80                         printf("L1_RESET, TODO: resend last radio request "
81                                 "(CCCH / dedicated / power scan)\n");
82                         break;
83                 }
84                 started = 1;
85                 ms = signal_data;
86                 gsm_subscr_testcard(ms, 1, 1, "0000000000");
87                 ms->subscr.plmn_valid = 1;
88                 ms->subscr.plmn_mcc = 1;
89                 ms->subscr.plmn_mnc = 1;
90                 /* start PLMN + cell selection process */
91                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
92                 if (!nmsg)
93                         return -ENOMEM;
94                 gsm322_plmn_sendmsg(ms, nmsg);
95                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
96                 if (!nmsg)
97                         return -ENOMEM;
98                 gsm322_cs_sendmsg(ms, nmsg);
99         }
100         return 0;
101 }
102
103 int mobile_exit(struct osmocom_ms *ms)
104 {
105         struct gsm48_mmlayer *mm = &ms->mmlayer;
106
107         if (!mm->power_off && started) {
108                 struct msgb *nmsg;
109
110                 mm->power_off = 1;
111                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
112                 if (!nmsg)
113                         return -ENOMEM;
114                 gsm48_mmevent_msg(mm->ms, nmsg);
115
116                 return -EBUSY;
117         }
118
119         /* in case there is a lockup during exit */
120         signal(SIGINT, SIG_DFL);
121         signal(SIGHUP, SIG_DFL);
122         signal(SIGTERM, SIG_DFL);
123         signal(SIGPIPE, SIG_DFL);
124
125         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
126         gsm322_exit(ms);
127         gsm48_mm_exit(ms);
128         gsm48_rr_exit(ms);
129         gsm_subscr_exit(ms);
130         gsm48_cc_exit(ms);
131
132         return 0;
133 }
134
135 int l23_app_init(struct osmocom_ms *ms)
136 {
137         log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG");
138
139         gsm48_cc_init(ms);
140         gsm_support_init(ms);
141         gsm_subscr_init(ms);
142         gsm48_rr_init(ms);
143         gsm48_mm_init(ms);
144         INIT_LLIST_HEAD(&ms->trans_list);
145         ms->cclayer.mncc_recv = mncc_recv_dummy;
146         gsm322_init(ms);
147         l23_app_work = mobile_work;
148         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
149         l23_app_exit = mobile_exit;
150
151         return 0;
152 }
153