1 /* "Application" code of the layer2/3 stack */
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>
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.
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.
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.
28 #include <osmocom/bb/common/osmocom_data.h>
29 #include <osmocom/bb/common/l1l2_interface.h>
30 #include <osmocom/bb/common/l1ctl.h>
31 #include <osmocom/bb/common/logging.h>
32 #include <osmocom/bb/common/gps.h>
33 #include <osmocom/bb/mobile/gsm48_rr.h>
34 #include <osmocom/bb/mobile/vty.h>
35 #include <osmocom/bb/mobile/app_mobile.h>
36 #include <osmocom/bb/mobile/mncc.h>
37 #include <osmocom/bb/mobile/voice.h>
38 #include <osmocom/vty/telnet_interface.h>
40 #include <osmocom/core/msgb.h>
41 #include <osmocom/core/talloc.h>
42 #include <osmocom/core/select.h>
43 #include <osmocom/core/signal.h>
45 #include <l1ctl_proto.h>
48 extern struct llist_head ms_list
;
49 extern int vty_reading
;
51 int mncc_recv_mobile(struct osmocom_ms
*ms
, int msg_type
, void *arg
);
52 int mncc_recv_dummy(struct osmocom_ms
*ms
, int msg_type
, void *arg
);
53 int (*mncc_recv_app
)(struct osmocom_ms
*ms
, int, void *);
56 /* handle ms instance */
57 int mobile_work(struct osmocom_ms
*ms
)
63 w
|= gsm48_rsl_dequeue(ms
);
64 w
|= gsm48_rr_dequeue(ms
);
65 w
|= gsm48_mmxx_dequeue(ms
);
66 w
|= gsm48_mmr_dequeue(ms
);
67 w
|= gsm48_mmevent_dequeue(ms
);
68 w
|= gsm322_plmn_dequeue(ms
);
69 w
|= gsm322_cs_dequeue(ms
);
70 w
|= gsm_sim_job_dequeue(ms
);
71 w
|= mncc_dequeue(ms
);
78 /* run ms instance, if layer1 is available */
79 int mobile_signal_cb(unsigned int subsys
, unsigned int signal
,
80 void *handler_data
, void *signal_data
)
82 struct osmocom_ms
*ms
;
83 struct gsm_settings
*set
;
86 if (subsys
!= SS_L1CTL
)
97 /* insert test card, if enabled */
98 switch (set
->sim_type
) {
99 case GSM_SIM_TYPE_READER
:
100 /* trigger sim card reader process */
101 gsm_subscr_simcard(ms
);
103 case GSM_SIM_TYPE_TEST
:
104 gsm_subscr_testcard(ms
, set
->test_rplmn_mcc
,
105 set
->test_rplmn_mnc
, set
->test_lac
,
109 /* no SIM, trigger PLMN selection process */
110 nmsg
= gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON
);
113 gsm322_plmn_sendmsg(ms
, nmsg
);
114 nmsg
= gsm322_msgb_alloc(GSM322_EVENT_SWITCH_ON
);
117 gsm322_cs_sendmsg(ms
, nmsg
);
125 /* power-off ms instance */
126 int mobile_exit(struct osmocom_ms
*ms
, int force
)
128 struct gsm48_mmlayer
*mm
= &ms
->mmlayer
;
130 if (!force
&& ms
->started
) {
133 ms
->shutdown
= 1; /* going down */
134 nmsg
= gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH
);
137 gsm48_mmevent_msg(mm
->ms
, nmsg
);
148 lapdm_channel_exit(&ms
->lapdm_channel
);
150 ms
->shutdown
= 2; /* being down */
151 vty_notify(ms
, NULL
);
152 vty_notify(ms
, "Power off!\n");
153 printf("Power off! (MS %s)\n", ms
->name
);
158 /* power-on ms instance */
159 int mobile_init(struct osmocom_ms
*ms
)
163 gsm_settings_arfcn(ms
);
165 lapdm_channel_init(&ms
->lapdm_channel
, LAPDM_MODE_MS
);
166 lapdm_channel_set_l1(&ms
->lapdm_channel
, l1ctl_ph_prim_cb
, ms
);
174 INIT_LLIST_HEAD(&ms
->trans_list
);
177 rc
= layer2_open(ms
, ms
->settings
.layer2_socket_path
);
179 fprintf(stderr
, "Failed during layer2_open()\n");
180 ms
->l2_wq
.bfd
.fd
= -1;
186 rc
= sap_open(ms
, ms
->settings
.sap_socket_path
);
188 fprintf(stderr
, "Failed during sap_open(), no SIM reader\n");
189 ms
->sap_wq
.bfd
.fd
= -1;
195 gsm_random_imei(&ms
->settings
);
200 if (!strcmp(ms
->settings
.imei
, "000000000000000")) {
201 printf("***\nWarning: Mobile '%s' has default IMEI: %s\n",
202 ms
->name
, ms
->settings
.imei
);
203 printf("This could relate your identitiy to other users with "
204 "default IMEI.\n***\n");
207 l1ctl_tx_reset_req(ms
, L1CTL_RES_T_FULL
);
208 printf("Mobile '%s' initialized, please start phone now!\n", ms
->name
);
212 /* create ms instance */
213 struct osmocom_ms
*mobile_new(char *name
)
215 static struct osmocom_ms
*ms
;
217 ms
= talloc_zero(l23_ctx
, struct osmocom_ms
);
219 fprintf(stderr
, "Failed to allocate MS\n");
222 llist_add_tail(&ms
->entity
, &ms_list
);
224 strcpy(ms
->name
, name
);
226 ms
->l2_wq
.bfd
.fd
= -1;
227 ms
->sap_wq
.bfd
.fd
= -1;
229 gsm_support_init(ms
);
230 gsm_settings_init(ms
);
232 ms
->shutdown
= 2; /* being down */
237 sprintf(name
, "/tmp/ms_mncc_%s", ms
->name
);
239 ms
->mncc_entity
.mncc_recv
= mncc_recv_app
;
240 ms
->mncc_entity
.sock_state
= mncc_sock_init(ms
, name
, l23_ctx
);
242 } else if (ms
->settings
.ch_cap
== GSM_CAP_SDCCH
)
243 ms
->mncc_entity
.mncc_recv
= mncc_recv_dummy
;
245 ms
->mncc_entity
.mncc_recv
= mncc_recv_mobile
;
251 /* destroy ms instance */
252 int mobile_delete(struct osmocom_ms
*ms
, int force
)
258 if (ms
->shutdown
== 0 || (ms
->shutdown
== 1 && force
)) {
259 rc
= mobile_exit(ms
, force
);
265 mncc_sock_exit(ms
->mncc_entity
.sock_state
);
266 ms
->mncc_entity
.sock_state
= NULL
;
272 /* handle global shutdown */
273 int global_signal_cb(unsigned int subsys
, unsigned int signal
,
274 void *handler_data
, void *signal_data
)
276 struct osmocom_ms
*ms
, *ms2
;
278 if (subsys
!= SS_GLOBAL
)
282 case S_GLOBAL_SHUTDOWN
:
283 llist_for_each_entry_safe(ms
, ms2
, &ms_list
, entity
)
284 mobile_delete(ms
, quit
);
286 /* if second signal is received, force to exit */
293 /* global work handler */
294 int l23_app_work(int *_quit
)
296 struct osmocom_ms
*ms
, *ms2
;
299 llist_for_each_entry_safe(ms
, ms2
, &ms_list
, entity
) {
300 if (ms
->shutdown
!= 2)
301 work
|= mobile_work(ms
);
302 if (ms
->shutdown
== 2) {
303 if (ms
->l2_wq
.bfd
.fd
> -1) {
305 ms
->l2_wq
.bfd
.fd
= -1;
308 if (ms
->sap_wq
.bfd
.fd
> -1) {
310 ms
->sap_wq
.bfd
.fd
= -1;
314 gsm_settings_exit(ms
);
315 llist_del(&ms
->entity
);
322 /* return, if a shutdown was scheduled (quit = 1) */
328 int l23_app_exit(void)
330 osmo_signal_unregister_handler(SS_L1CTL
, &gsm322_l1_signal
, NULL
);
331 osmo_signal_unregister_handler(SS_L1CTL
, &mobile_signal_cb
, NULL
);
332 osmo_signal_unregister_handler(SS_GLOBAL
, &global_signal_cb
, NULL
);
341 static struct vty_app_info vty_info
= {
343 .version
= PACKAGE_VERSION
,
344 .go_parent_cb
= ms_vty_go_parent
,
348 int l23_app_init(int (*mncc_recv
)(struct osmocom_ms
*ms
, int, void *),
349 const char *config_file
, uint16_t vty_port
)
351 struct telnet_connection dummy_conn
;
354 mncc_recv_app
= mncc_recv
;
360 dummy_conn
.priv
= NULL
;
362 if (config_file
!= NULL
) {
363 rc
= vty_read_config_file(config_file
, &dummy_conn
);
365 fprintf(stderr
, "Failed to parse the config file:"
366 " '%s'\n", config_file
);
367 fprintf(stderr
, "Please check or create config file"
368 " using: 'touch %s'\n", config_file
);
373 telnet_init(l23_ctx
, NULL
, vty_port
);
376 printf("VTY available on port %u.\n", vty_port
);
378 osmo_signal_register_handler(SS_GLOBAL
, &global_signal_cb
, NULL
);
379 osmo_signal_register_handler(SS_L1CTL
, &mobile_signal_cb
, NULL
);
380 osmo_signal_register_handler(SS_L1CTL
, &gsm322_l1_signal
, NULL
);
382 if (llist_empty(&ms_list
)) {
383 struct osmocom_ms
*ms
;
385 printf("No Mobile Station defined, creating: MS '1'\n");
386 ms
= mobile_new("1");