46e0e0675d8b4f163f23760c337f0e8bb583a80f
[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 #include <time.h>
28
29 #include <osmocom/osmocom_data.h>
30 #include <osmocom/l1ctl.h>
31 #include <osmocom/l23_app.h>
32 #include <osmocom/gsm48_rr.h>
33 #include <osmocom/sysinfo.h>
34 #include <osmocom/lapdm.h>
35 #include <osmocom/gsmtap_util.h>
36 #include <osmocom/logging.h>
37 #include <osmocom/telnet_interface.h>
38 #include <osmocom/file.h>
39
40 #include <osmocore/msgb.h>
41 #include <osmocore/talloc.h>
42 #include <osmocore/select.h>
43 #include <osmocore/signal.h>
44
45 extern struct log_target *stderr_target;
46 static const char *config_file = "osmocom.cfg";
47
48 static int started = 0;
49
50 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg);
51
52 int mobile_work(struct osmocom_ms *ms)
53 {
54         int work = 0, w;
55
56         do {
57                 w = 0;
58                 w |= gsm48_rsl_dequeue(ms);
59                 w |= gsm48_rr_dequeue(ms);
60                 w |= gsm48_mmxx_dequeue(ms);
61                 w |= gsm48_mmr_dequeue(ms);
62                 w |= gsm48_mmevent_dequeue(ms);
63                 w |= gsm322_plmn_dequeue(ms);
64                 w |= gsm322_cs_dequeue(ms);
65                 w |= mncc_dequeue(ms);
66                 if (w)
67                         work = 1;
68         } while (w);
69         return work;
70 }
71
72 static int signal_cb(unsigned int subsys, unsigned int signal,
73                      void *handler_data, void *signal_data)
74 {
75         struct osmocom_ms *ms;
76         struct msgb *nmsg;
77
78         if (subsys != SS_L1CTL)
79                 return 0;
80
81         switch (signal) {
82         case S_L1CTL_RESET:
83                 if (started) {
84                         printf("L1_RESET, TODO: resend last radio request "
85                                 "(CCCH / dedicated / power scan)\n");
86                         break;
87                 }
88                 started = 1;
89                 ms = signal_data;
90                 /* insert test card, if enabled */
91                 if (ms->settings.simtype == GSM_SIM_TYPE_TEST)
92                         gsm_subscr_testcard(ms);
93                 /* start PLMN + cell selection process */
94                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
95                 if (!nmsg)
96                         return -ENOMEM;
97                 gsm322_plmn_sendmsg(ms, nmsg);
98                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
99                 if (!nmsg)
100                         return -ENOMEM;
101                 gsm322_cs_sendmsg(ms, nmsg);
102         }
103         return 0;
104 }
105
106 int mobile_exit(struct osmocom_ms *ms)
107 {
108         struct gsm48_mmlayer *mm = &ms->mmlayer;
109
110         if (!mm->power_off && started) {
111                 struct msgb *nmsg;
112
113                 mm->power_off = 1;
114                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
115                 if (!nmsg)
116                         return -ENOMEM;
117                 gsm48_mmevent_msg(mm->ms, nmsg);
118
119                 return -EBUSY;
120         }
121
122         /* in case there is a lockup during exit */
123         signal(SIGINT, SIG_DFL);
124         signal(SIGHUP, SIG_DFL);
125         signal(SIGTERM, SIG_DFL);
126         signal(SIGPIPE, SIG_DFL);
127
128         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
129         gsm322_exit(ms);
130         gsm48_mm_exit(ms);
131         gsm48_rr_exit(ms);
132         gsm_subscr_exit(ms);
133         gsm48_cc_exit(ms);
134
135         return 0;
136 }
137
138 int l23_app_init(struct osmocom_ms *ms)
139 {
140         int rc;
141
142         log_parse_category_mask(stderr_target, "DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG");
143
144         srand(time(NULL));
145
146         gsm_settings_init(ms);
147         gsm48_cc_init(ms);
148         gsm_support_init(ms);
149         gsm_subscr_init(ms);
150         gsm48_rr_init(ms);
151         gsm48_mm_init(ms);
152         INIT_LLIST_HEAD(&ms->trans_list);
153         ms->cclayer.mncc_recv = mncc_recv_dummy;
154         gsm322_init(ms);
155
156         l23_app_work = mobile_work;
157         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
158         l23_app_exit = mobile_exit;
159
160         telnet_init(ms, 4242);
161         rc = vty_read_config_file(config_file);
162         if (rc < 0) {
163                 fprintf(stderr, "Failed to parse the config file: '%s'\n",
164                         config_file);
165                 fprintf(stderr, "Please check or create config file using: "
166                         "'touch %s%s'\n", OSMOCOM_CONFDIR, config_file);
167                 return rc;
168         }
169
170         gsm_random_imei(&ms->settings);
171
172         return 0;
173 }
174