Added support for NMEA GPS receiver
[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 static int 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 |= mncc_dequeue(ms);
68                 if (w)
69                         work = 1;
70         } while (w);
71         return work;
72 }
73
74 static int signal_cb(unsigned int subsys, unsigned int signal,
75                      void *handler_data, void *signal_data)
76 {
77         struct osmocom_ms *ms;
78         struct gsm_settings *set;
79         struct msgb *nmsg;
80
81         if (subsys != SS_L1CTL)
82                 return 0;
83
84         switch (signal) {
85         case S_L1CTL_RESET:
86                 if (started)
87                         break;
88                 started = 1;
89                 ms = signal_data;
90                 set = &ms->settings;
91                 /* insert test card, if enabled */
92                 if (set->simtype == GSM_SIM_TYPE_TEST)
93                         gsm_subscr_testcard(ms, set->test_rplmn_mcc,
94                                                 set->test_rplmn_mnc);
95                 /* start PLMN + cell selection process */
96                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
97                 if (!nmsg)
98                         return -ENOMEM;
99                 gsm322_plmn_sendmsg(ms, nmsg);
100                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON);
101                 if (!nmsg)
102                         return -ENOMEM;
103                 gsm322_cs_sendmsg(ms, nmsg);
104         }
105         return 0;
106 }
107
108 int mobile_exit(struct osmocom_ms *ms)
109 {
110         struct gsm48_mmlayer *mm = &ms->mmlayer;
111
112         if (!mm->power_off && started) {
113                 struct msgb *nmsg;
114
115                 mm->power_off = 1;
116                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
117                 if (!nmsg)
118                         return -ENOMEM;
119                 gsm48_mmevent_msg(mm->ms, nmsg);
120
121                 return -EBUSY;
122         }
123
124         /* in case there is a lockup during exit */
125         signal(SIGINT, SIG_DFL);
126         signal(SIGHUP, SIG_DFL);
127         signal(SIGTERM, SIG_DFL);
128         signal(SIGPIPE, SIG_DFL);
129
130         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
131         gps_close();
132         gsm322_exit(ms);
133         gsm48_mm_exit(ms);
134         gsm48_rr_exit(ms);
135         gsm_subscr_exit(ms);
136         gsm48_cc_exit(ms);
137
138         printf("Power off!\n");
139
140         return 0;
141 }
142
143 static struct vty_app_info vty_info = {
144         .name = "OsmocomBB",
145         .version = PACKAGE_VERSION,
146         .go_parent_cb = ms_vty_go_parent,
147 };
148
149 int l23_app_init(struct osmocom_ms *ms)
150 {
151         int rc;
152         struct telnet_connection dummy_conn;
153
154 //      log_parse_category_mask(stderr_target, "DRSL:DLAPDM:DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
155         log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DCC:DMNCC:DPAG:DSUM");
156
157         srand(time(NULL));
158
159         gsm_settings_init(ms);
160         gsm48_cc_init(ms);
161         gsm_support_init(ms);
162         gsm_subscr_init(ms);
163         gsm48_rr_init(ms);
164         gsm48_mm_init(ms);
165         INIT_LLIST_HEAD(&ms->trans_list);
166         ms->cclayer.mncc_recv = mncc_recv_mobile;
167         gsm322_init(ms);
168
169         l23_app_work = mobile_work;
170         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
171         l23_app_exit = mobile_exit;
172
173         vty_init(&vty_info);
174         ms_vty_init();
175         dummy_conn.priv = NULL;
176         rc = vty_read_config_file(config_file, &dummy_conn);
177         if (rc < 0) {
178                 fprintf(stderr, "Failed to parse the config file: '%s'\n",
179                         config_file);
180                 fprintf(stderr, "Please check or create config file using: "
181                         "'touch %s'\n", config_file);
182                 return rc;
183         }
184         telnet_init(l23_ctx, NULL, vty_port);
185         if (rc < 0)
186                 return rc;
187         printf("VTY available on port %u.\n", vty_port);
188
189         gsm_random_imei(&ms->settings);
190
191         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
192         printf("Mobile initialized, please start phone now!\n");
193         return 0;
194 }
195