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