[layer23] Minimum RX-level is now a configuratio option
[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 #include <time.h>
28
29 #include <osmocom/bb/common/osmocom_data.h>
30 #include <osmocom/bb/common/l1ctl.h>
31 #include <osmocom/bb/common/l23_app.h>
32 #include <osmocom/bb/common/lapdm.h>
33 #include <osmocom/bb/common/logging.h>
34 #include <osmocom/bb/mobile/gsm48_rr.h>
35 #include <osmocom/bb/mobile/sysinfo.h>
36 #include <osmocom/bb/mobile/vty.h>
37 #include <osmocom/bb/mobile/gps.h>
38 #include <osmocom/vty/telnet_interface.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 = "/etc/osmocom/osmocom.cfg";
47 extern void *l23_ctx;
48 extern unsigned short vty_port;
49
50 int mobile_started = 0;
51
52 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg);
53
54 int mobile_work(struct osmocom_ms *ms)
55 {
56         int work = 0, w;
57
58         do {
59                 w = 0;
60                 w |= gsm48_rsl_dequeue(ms);
61                 w |= gsm48_rr_dequeue(ms);
62                 w |= gsm48_mmxx_dequeue(ms);
63                 w |= gsm48_mmr_dequeue(ms);
64                 w |= gsm48_mmevent_dequeue(ms);
65                 w |= gsm322_plmn_dequeue(ms);
66                 w |= gsm322_cs_dequeue(ms);
67                 w |= gsm_sim_job_dequeue(ms);
68                 w |= mncc_dequeue(ms);
69                 if (w)
70                         work = 1;
71         } while (w);
72         return work;
73 }
74
75 static int signal_cb(unsigned int subsys, unsigned int signal,
76                      void *handler_data, void *signal_data)
77 {
78         struct osmocom_ms *ms;
79         struct gsm_settings *set;
80         struct msgb *nmsg;
81
82         if (subsys != SS_L1CTL)
83                 return 0;
84
85         switch (signal) {
86         case S_L1CTL_RESET:
87                 if (mobile_started)
88                         break;
89
90                 ms = signal_data;
91                 set = &ms->settings;
92
93                 /* insert test card, if enabled */
94                 switch (set->sim_type) {
95                 case GSM_SIM_TYPE_READER:
96                         /* trigger sim card reader process */
97                         gsm_subscr_simcard(ms);
98                         break;
99                 case GSM_SIM_TYPE_TEST:
100                         gsm_subscr_testcard(ms, set->test_rplmn_mcc,
101                                                 set->test_rplmn_mnc);
102                         break;
103                 default:
104                         /* no SIM, trigger PLMN selection process */
105                         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
106                         if (!nmsg)
107                                 return -ENOMEM;
108                         gsm322_plmn_sendmsg(ms, nmsg);
109                         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
110                         if (!nmsg)
111                                 return -ENOMEM;
112                         gsm322_cs_sendmsg(ms, nmsg);
113                 }
114
115                 mobile_started = 1;
116         }
117         return 0;
118 }
119
120 int mobile_exit(struct osmocom_ms *ms)
121 {
122         struct gsm48_mmlayer *mm = &ms->mmlayer;
123
124         if (!mm->power_off && mobile_started) {
125                 struct msgb *nmsg;
126
127                 mm->power_off = 1;
128                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
129                 if (!nmsg)
130                         return -ENOMEM;
131                 gsm48_mmevent_msg(mm->ms, nmsg);
132
133                 return -EBUSY;
134         }
135
136         /* in case there is a lockup during exit */
137         signal(SIGINT, SIG_DFL);
138         signal(SIGHUP, SIG_DFL);
139         signal(SIGTERM, SIG_DFL);
140         signal(SIGPIPE, SIG_DFL);
141
142         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
143         gps_close();
144         gsm322_exit(ms);
145         gsm48_mm_exit(ms);
146         gsm48_rr_exit(ms);
147         gsm_subscr_exit(ms);
148         gsm48_cc_exit(ms);
149         gsm_sim_exit(ms);
150
151         printf("Power off!\n");
152
153         return 0;
154 }
155
156 static struct vty_app_info vty_info = {
157         .name = "OsmocomBB",
158         .version = PACKAGE_VERSION,
159         .go_parent_cb = ms_vty_go_parent,
160 };
161
162 int l23_app_init(struct osmocom_ms *ms)
163 {
164         int rc;
165         struct telnet_connection dummy_conn;
166
167 //      log_parse_category_mask(stderr_target, "DL1C:DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
168         log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
169         log_set_log_level(stderr_target, LOGL_INFO);
170
171         srand(time(NULL));
172
173         gsm_support_init(ms);
174         gsm_sim_init(ms);
175         gsm_settings_init(ms);
176         gsm48_cc_init(ms);
177         gsm_subscr_init(ms);
178         gsm48_rr_init(ms);
179         gsm48_mm_init(ms);
180         INIT_LLIST_HEAD(&ms->trans_list);
181         ms->cclayer.mncc_recv = mncc_recv_mobile;
182         gsm322_init(ms);
183
184         l23_app_work = mobile_work;
185         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
186         l23_app_exit = mobile_exit;
187
188         vty_init(&vty_info);
189         ms_vty_init();
190         dummy_conn.priv = NULL;
191         rc = vty_read_config_file(config_file, &dummy_conn);
192         if (rc < 0) {
193                 fprintf(stderr, "Failed to parse the config file: '%s'\n",
194                         config_file);
195                 fprintf(stderr, "Please check or create config file using: "
196                         "'touch %s'\n", config_file);
197                 return rc;
198         }
199         telnet_init(l23_ctx, NULL, vty_port);
200         if (rc < 0)
201                 return rc;
202         printf("VTY available on port %u.\n", vty_port);
203
204         gsm_random_imei(&ms->settings);
205
206         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
207         printf("Mobile initialized, please start phone now!\n");
208         return 0;
209 }
210