Added support for NMEA GPS receiver
[osmocom-bb.git] / src / host / layer23 / src / mobile / vty_interface.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include <osmocore/gsm48.h>
29
30 #include <osmocom/bb/common/osmocom_data.h>
31 #include <osmocom/bb/common/networks.h>
32 #include <osmocom/bb/mobile/mncc.h>
33 #include <osmocom/bb/mobile/transaction.h>
34 #include <osmocom/bb/mobile/vty.h>
35 #include <osmocom/bb/mobile/gps.h>
36 #include <osmocom/vty/telnet_interface.h>
37
38 int mncc_call(struct osmocom_ms *ms, char *number);
39 int mncc_hangup(struct osmocom_ms *ms);
40 int mncc_answer(struct osmocom_ms *ms);
41 int mncc_hold(struct osmocom_ms *ms);
42 int mncc_retrieve(struct osmocom_ms *ms, int number);
43
44 extern struct llist_head ms_list;
45 extern struct llist_head active_connections;
46
47 struct cmd_node ms_node = {
48         MS_NODE,
49         "%s(ms)#",
50         1
51 };
52
53 struct cmd_node testsim_node = {
54         TESTSIM_NODE,
55         "%s(test-sim)#",
56         1
57 };
58
59 static void print_vty(void *priv, const char *fmt, ...)
60 {
61         char buffer[1000];
62         struct vty *vty = priv;
63         va_list args;
64
65         va_start(args, fmt);
66         vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
67         buffer[sizeof(buffer) - 1] = '\0';
68         va_end(args);
69
70         if (buffer[0]) {
71                 if (buffer[strlen(buffer) - 1] == '\n') {
72                         buffer[strlen(buffer) - 1] = '\0';
73                         vty_out(vty, "%s%s", buffer, VTY_NEWLINE);
74                 } else
75                         vty_out(vty, "%s", buffer);
76         }
77 }
78
79 static struct osmocom_ms *get_ms(const char *name, struct vty *vty)
80 {
81         struct osmocom_ms *ms;
82
83         llist_for_each_entry(ms, &ms_list, entity) {
84                 if (!strcmp(ms->name, name))
85                         return ms;
86         }
87         vty_out(vty, "MS name '%s' does not exits.%s", name, VTY_NEWLINE);
88
89         return NULL;
90 }
91
92 DEFUN(show_ms, show_ms_cmd, "show ms",
93         SHOW_STR "Display available MS entities\n")
94 {
95         struct osmocom_ms *ms;
96
97         llist_for_each_entry(ms, &ms_list, entity) {
98                 struct gsm_settings *set = &ms->settings;
99
100                 vty_out(vty, "MS NAME: %s%s", ms->name, VTY_NEWLINE);
101                 vty_out(vty, " IMEI: %s%s", set->imei, VTY_NEWLINE);
102                 vty_out(vty, " IMEISV: %s%s", set->imeisv, VTY_NEWLINE);
103                 if (set->imei_random)
104                         vty_out(vty, " IMEI generation: random (%d trailing "
105                                 "digits)%s", set->imei_random, VTY_NEWLINE);
106                 else
107                         vty_out(vty, " IMEI generation: fixed%s", VTY_NEWLINE);
108                 vty_out(vty, " network selection mode: %s%s",
109                         (set->plmn_mode == PLMN_MODE_AUTO)
110                                 ? "automatic" : "manual", VTY_NEWLINE);
111         }
112
113         return CMD_SUCCESS;
114 }
115
116 DEFUN(show_support, show_support_cmd, "show support [ms_name]",
117         SHOW_STR "Display information about MS support\n"
118         "Name of MS (see \"show ms\")")
119 {
120         struct osmocom_ms *ms;
121
122         if (argc) {
123                 ms = get_ms(argv[0], vty);
124                 if (!ms)
125                         return CMD_WARNING;
126                 gsm_support_dump(&ms->support, print_vty, vty);
127         } else {
128                 llist_for_each_entry(ms, &ms_list, entity) {
129                         gsm_support_dump(&ms->support, print_vty, vty);
130                         vty_out(vty, "%s", VTY_NEWLINE);
131                 }
132         }
133
134         return CMD_SUCCESS;
135 }
136
137 static void gsm_states_dump(struct osmocom_ms *ms, struct vty *vty)
138 {
139         struct gsm_trans *trans;
140
141         vty_out(vty, "Current state of MS '%s'%s", ms->name, VTY_NEWLINE);
142         if (ms->settings.plmn_mode == PLMN_MODE_AUTO)
143                 vty_out(vty, " automatic network selection: %s%s", 
144                         plmn_a_state_names[ms->plmn.state], VTY_NEWLINE);
145         else
146                 vty_out(vty, " manual network selection: %s%s", 
147                         plmn_m_state_names[ms->plmn.state], VTY_NEWLINE);
148         vty_out(vty, " cell selection: %s%s", 
149                 cs_state_names[ms->cellsel.state], VTY_NEWLINE);
150         vty_out(vty, " radio ressource layer: %s%s", 
151                 gsm48_rr_state_names[ms->rrlayer.state], VTY_NEWLINE);
152         vty_out(vty, " mobility management layer: %s", 
153                 gsm48_mm_state_names[ms->mmlayer.state]);
154         if (ms->mmlayer.state == GSM48_MM_ST_MM_IDLE)
155                 vty_out(vty, ", %s", 
156                         gsm48_mm_substate_names[ms->mmlayer.substate]);
157         vty_out(vty, "%s", VTY_NEWLINE);
158         llist_for_each_entry(trans, &ms->trans_list, entry) {
159                 vty_out(vty, " call control: %s%s", 
160                         gsm48_cc_state_name(trans->cc.state), VTY_NEWLINE);
161         }
162 }
163
164 DEFUN(show_states, show_states_cmd, "show states [ms_name]",
165         SHOW_STR "Display current states of given MS\n"
166         "Name of MS (see \"show ms\")")
167 {
168         struct osmocom_ms *ms;
169
170         if (argc) {
171                 ms = get_ms(argv[0], vty);
172                 if (!ms)
173                         return CMD_WARNING;
174                 gsm_states_dump(ms, vty);
175         } else {
176                 llist_for_each_entry(ms, &ms_list, entity) {
177                         gsm_states_dump(ms, vty);
178                         vty_out(vty, "%s", VTY_NEWLINE);
179                 }
180         }
181
182         return CMD_SUCCESS;
183 }
184
185 DEFUN(show_subscr, show_subscr_cmd, "show subscriber [ms_name]",
186         SHOW_STR "Display information about subscriber\n"
187         "Name of MS (see \"show ms\")")
188 {
189         struct osmocom_ms *ms;
190
191         if (argc) {
192                 ms = get_ms(argv[0], vty);
193                 if (!ms)
194                         return CMD_WARNING;
195                 gsm_subscr_dump(&ms->subscr, print_vty, vty);
196         } else {
197                 llist_for_each_entry(ms, &ms_list, entity) {
198                         gsm_subscr_dump(&ms->subscr, print_vty, vty);
199                         vty_out(vty, "%s", VTY_NEWLINE);
200                 }
201         }
202
203         return CMD_SUCCESS;
204 }
205
206 DEFUN(show_cell, show_cell_cmd, "show cell MS_NAME",
207         SHOW_STR "Display information about received cells\n"
208         "Name of MS (see \"show ms\")")
209 {
210         struct osmocom_ms *ms;
211
212         ms = get_ms(argv[0], vty);
213         if (!ms)
214                 return CMD_WARNING;
215
216         gsm322_dump_cs_list(&ms->cellsel, GSM322_CS_FLAG_SYSINFO, print_vty,
217                 vty);
218
219         return CMD_SUCCESS;
220 }
221
222 DEFUN(show_cell_si, show_cell_si_cmd, "show cell MS_NAME <0-1023>",
223         SHOW_STR "Display information about received cell\n"
224         "Name of MS (see \"show ms\")\nRadio frequency number")
225 {
226         struct osmocom_ms *ms;
227         int i;
228         struct gsm48_sysinfo *s;
229
230         ms = get_ms(argv[0], vty);
231         if (!ms)
232                 return CMD_WARNING;
233
234         i = atoi(argv[1]);
235         if (i < 0 || i > 1023) {
236                 vty_out(vty, "Given ARFCN '%s' not in range (0..1023)%s",
237                         argv[1], VTY_NEWLINE);
238                 return CMD_WARNING;
239         }
240         s = ms->cellsel.list[i].sysinfo;
241         if (!s) {
242                 vty_out(vty, "Given ARFCN '%s' has no sysinfo available%s",
243                         argv[1], VTY_NEWLINE);
244                 return CMD_SUCCESS;
245         }
246
247         gsm48_sysinfo_dump(s, i, print_vty, vty);
248
249         return CMD_SUCCESS;
250 }
251
252 DEFUN(show_ba, show_ba_cmd, "show ba MS_NAME [mcc] [mnc]",
253         SHOW_STR "Display information about band allocations\n"
254         "Name of MS (see \"show ms\")\nMobile Country Code\n"
255         "Mobile Network Code")
256 {
257         struct osmocom_ms *ms;
258         uint16_t mcc = 0, mnc = 0;
259
260         ms = get_ms(argv[0], vty);
261         if (!ms)
262                 return CMD_WARNING;
263
264         if (argc >= 3) {
265                 mcc = gsm_input_mcc((char *)argv[1]);
266                 mnc = gsm_input_mnc((char *)argv[2]);
267                 if (!mcc) {
268                         vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
269                         return CMD_WARNING;
270                 }
271                 if (!mnc) {
272                         vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
273                         return CMD_WARNING;
274                 }
275         }
276
277         gsm322_dump_ba_list(&ms->cellsel, mcc, mnc, print_vty, vty);
278
279         return CMD_SUCCESS;
280 }
281
282 DEFUN(show_forb_plmn, show_forb_plmn_cmd, "show forbidden plmn MS_NAME",
283         SHOW_STR "Display information about forbidden cells / networks\n"
284         "Display forbidden PLMNs\nName of MS (see \"show ms\")")
285 {
286         struct osmocom_ms *ms;
287
288         ms = get_ms(argv[0], vty);
289         if (!ms)
290                 return CMD_WARNING;
291
292         gsm_subscr_dump_forbidden_plmn(ms, print_vty, vty);
293
294         return CMD_SUCCESS;
295 }
296
297 DEFUN(show_forb_la, show_forb_la_cmd, "show forbidden location-area MS_NAME",
298         SHOW_STR "Display information about forbidden cells / networks\n"
299         "Display forbidden location areas\nName of MS (see \"show ms\")")
300 {
301         struct osmocom_ms *ms;
302
303         ms = get_ms(argv[0], vty);
304         if (!ms)
305                 return CMD_WARNING;
306
307         gsm322_dump_forbidden_la(ms, print_vty, vty);
308
309         return CMD_SUCCESS;
310 }
311
312 DEFUN(monitor_network, monitor_network_cmd, "monitor network MS_NAME",
313         "Monitor...\nMonitor network information\nName of MS (see \"show ms\")")
314 {
315         struct osmocom_ms *ms;
316
317         ms = get_ms(argv[0], vty);
318         if (!ms)
319                 return CMD_WARNING;
320
321         gsm48_rr_start_monitor(ms);
322
323         return CMD_SUCCESS;
324 }
325
326 DEFUN(no_monitor_network, no_monitor_network_cmd, "no monitor network MS_NAME",
327         NO_STR "Monitor...\nDeactivate monitor of network information\n"
328         "Name of MS (see \"show ms\")")
329 {
330         struct osmocom_ms *ms;
331
332         ms = get_ms(argv[0], vty);
333         if (!ms)
334                 return CMD_WARNING;
335
336         gsm48_rr_stop_monitor(ms);
337
338         return CMD_SUCCESS;
339 }
340
341 DEFUN(sim_test, sim_test_cmd, "sim testcard MS_NAME [mcc] [mnc]",
342         "SIM actions\nInsert test card\nName of MS (see \"show ms\")\n"
343         "Mobile Country Code of RPLMN\nMobile Network Code of RPLMN")
344 {
345         struct osmocom_ms *ms;
346         uint16_t mcc = 0x001, mnc = 0x01f;
347
348         ms = get_ms(argv[0], vty);
349         if (!ms)
350                 return CMD_WARNING;
351
352         if (ms->subscr.sim_valid) {
353                 vty_out(vty, "Sim already presend, remove first!%s",
354                         VTY_NEWLINE);
355                 return CMD_WARNING;
356         }
357
358         if (argc >= 3) {
359                 mcc = gsm_input_mcc((char *)argv[1]);
360                 mnc = gsm_input_mnc((char *)argv[2]);
361                 if (!mcc) {
362                         vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
363                         return CMD_WARNING;
364                 }
365                 if (!mnc) {
366                         vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
367                         return CMD_WARNING;
368                 }
369         }
370
371         gsm_subscr_testcard(ms, mcc, mnc);
372
373         return CMD_SUCCESS;
374 }
375
376 DEFUN(sim_remove, sim_remove_cmd, "sim remove MS_NAME",
377         "SIM actions\nRemove SIM card\nName of MS (see \"show ms\")")
378 {
379         struct osmocom_ms *ms;
380
381         ms = get_ms(argv[0], vty);
382         if (!ms)
383                 return CMD_WARNING;
384
385         if (!ms->subscr.sim_valid) {
386                 vty_out(vty, "No Sim inserted!%s", VTY_NEWLINE);
387                 return CMD_WARNING;
388         }
389
390         gsm_subscr_remove(ms);
391
392         return CMD_SUCCESS;
393 }
394
395 DEFUN(network_select, network_select_cmd, "network select MS_NAME MCC MNC",
396         "Select ...\nSelect Network\nName of MS (see \"show ms\")\n"
397         "Mobile Country Code\nMobile Network Code")
398 {
399         struct osmocom_ms *ms;
400         struct gsm322_plmn *plmn;
401         struct msgb *nmsg;
402         struct gsm322_msg *ngm;
403         struct gsm322_plmn_list *temp;
404         uint16_t mcc = gsm_input_mcc((char *)argv[1]),
405                  mnc = gsm_input_mnc((char *)argv[2]);
406         int found = 0;
407
408         ms = get_ms(argv[0], vty);
409         if (!ms)
410                 return CMD_WARNING;
411         plmn = &ms->plmn;
412
413         if (!mcc) {
414                 vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
415                 return CMD_WARNING;
416         }
417         if (!mnc) {
418                 vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
419                 return CMD_WARNING;
420         }
421
422         llist_for_each_entry(temp, &plmn->sorted_plmn, entry)
423                 if (temp->mcc == mcc &&  temp->mnc == mnc)
424                         found = 1;
425         if (!found) {
426                 vty_out(vty, "Network not in list!%s", VTY_NEWLINE);
427                 return CMD_WARNING;
428         }
429
430         nmsg = gsm322_msgb_alloc(GSM322_EVENT_CHOOSE_PLMN);
431         if (!nmsg)
432                 return CMD_WARNING;
433         ngm = (struct gsm322_msg *) nmsg->data;
434         ngm->mcc = mcc;
435         ngm->mnc = mnc;
436         gsm322_plmn_sendmsg(ms, nmsg);
437
438         return CMD_SUCCESS;
439 }
440
441 DEFUN(call, call_cmd, "call MS_NAME (NUMBER|emergency|answer|hangup|hold)",
442         "Make a call\nName of MS (see \"show ms\")\nPhone number to call\n"
443         "Make an emergency call\nAnswer an incomming call\nHangup a call\n"
444         "Hold current active call\n")
445 {
446         struct osmocom_ms *ms;
447
448         ms = get_ms(argv[0], vty);
449         if (!ms)
450                 return CMD_WARNING;
451
452         switch (argv[1][0]) {
453         case 'a':
454                 mncc_answer(ms);
455                 break;
456         case 'h':
457                 if (argv[1][1] == 'a')
458                         mncc_hangup(ms);
459                 else
460                         mncc_hold(ms);
461                 break;
462         default:
463                 mncc_call(ms, (char *)argv[1]);
464         }
465
466         return CMD_SUCCESS;
467 }
468
469 DEFUN(call_retr, call_retr_cmd, "call MS_NAME retrieve [number]",
470         "Make a call\nName of MS (see \"show ms\")\n"
471         "Retrieve call on hold\nNumber of call to retrieve")
472 {
473         struct osmocom_ms *ms;
474
475         ms = get_ms(argv[0], vty);
476         if (!ms)
477                 return CMD_WARNING;
478
479         mncc_retrieve(ms, (argc > 1) ? atoi(argv[1]) : 0);
480
481         return CMD_SUCCESS;
482 }
483
484 DEFUN(network_show, network_show_cmd, "network show MS_NAME",
485         "Network ...\nShow results of network search (again)\n"
486         "Name of MS (see \"show ms\")")
487 {
488         struct osmocom_ms *ms;
489         struct gsm322_plmn *plmn;
490         struct gsm322_plmn_list *temp;
491
492         ms = get_ms(argv[0], vty);
493         if (!ms)
494                 return CMD_WARNING;
495         plmn = &ms->plmn;
496
497         if (ms->settings.plmn_mode != PLMN_MODE_AUTO
498          && plmn->state != GSM322_M3_NOT_ON_PLMN) {
499                 vty_out(vty, "Start network search first!%s", VTY_NEWLINE);
500                 return CMD_WARNING;
501         }
502
503         llist_for_each_entry(temp, &plmn->sorted_plmn, entry)
504                 vty_out(vty, " Network %s, %s (%s, %s)%s",
505                         gsm_print_mcc(temp->mcc), gsm_print_mnc(temp->mnc),
506                         gsm_get_mcc(temp->mcc),
507                         gsm_get_mnc(temp->mcc, temp->mnc), VTY_NEWLINE);
508
509         return CMD_SUCCESS;
510 }
511
512 DEFUN(network_search, network_search_cmd, "network search MS_NAME",
513         "Network ...\nTrigger network search\nName of MS (see \"show ms\")")
514 {
515         struct osmocom_ms *ms;
516         struct msgb *nmsg;
517
518         ms = get_ms(argv[0], vty);
519         if (!ms)
520                 return CMD_WARNING;
521
522         nmsg = gsm322_msgb_alloc(GSM322_EVENT_USER_RESEL);
523         if (!nmsg)
524                 return CMD_WARNING;
525         gsm322_plmn_sendmsg(ms, nmsg);
526
527         return CMD_SUCCESS;
528 }
529
530 DEFUN(cfg_gps_enable, cfg_gps_enable_cmd, "gps enable",
531         "GPS receiver")
532 {
533         if (gps_open()) {
534                 gps.enable = 1;
535                 vty_out(vty, "Failed to open GPS device!%s", VTY_NEWLINE);
536                 return CMD_WARNING;
537         }
538         gps.enable = 1;
539
540         return CMD_SUCCESS;
541 }
542
543 DEFUN(cfg_no_gps_enable, cfg_no_gps_enable_cmd, "no gps enable",
544         NO_STR "Disable GPS receiver")
545 {
546         if (gps.enable)
547                 gps_close();
548         gps.enable = 0;
549
550         return CMD_SUCCESS;
551 }
552
553 DEFUN(cfg_gps_device, cfg_gps_device_cmd, "gps device DEVICE",
554         "GPS receiver\nSelect serial device\n"
555         "Full path of serial device including /dev/")
556 {
557         strncpy(gps.device, argv[0], sizeof(gps.device));
558         gps.device[sizeof(gps.device) - 1] = '\0';
559         if (gps.enable) {
560                 gps_close();
561                 if (gps_open()) {
562                         vty_out(vty, "Failed to open GPS device!%s",
563                                 VTY_NEWLINE);
564                         return CMD_WARNING;
565                 }
566         }
567
568         return CMD_SUCCESS;
569 }
570
571 DEFUN(cfg_gps_baud, cfg_gps_baud_cmd, "gps baudrate "
572         "(default|4800|""9600|19200|38400|57600|115200)",
573         "GPS receiver\nSelect baud rate\nDefault, don't modify\n\n\n\n\n\n")
574 {
575         if (argv[0][0] == 'd')
576                 gps.baud = 0;
577         else
578                 gps.baud = atoi(argv[0]);
579         if (gps.enable) {
580                 gps_close();
581                 if (gps_open()) {
582                         gps.enable = 0;
583                         vty_out(vty, "Failed to open GPS device!%s",
584                                 VTY_NEWLINE);
585                         return CMD_WARNING;
586                 }
587         }
588
589         return CMD_SUCCESS;
590 }
591
592 /* per MS config */
593 DEFUN(cfg_ms, cfg_ms_cmd, "ms MS_NAME",
594         "Select a mobile station to configure\nName of MS (see \"show ms\")")
595 {
596         struct osmocom_ms *ms;
597
598         ms = get_ms(argv[0], vty);
599         if (!ms)
600                 return CMD_WARNING;
601
602         vty->index = ms;
603         vty->node = MS_NODE;
604
605         return CMD_SUCCESS;
606 }
607
608 static void config_write_ms_single(struct vty *vty, struct osmocom_ms *ms)
609 {
610         struct gsm_settings *set = &ms->settings;
611
612         vty_out(vty, "ms %s%s", ms->name, VTY_NEWLINE);
613         switch(set->simtype) {
614                 case GSM_SIM_TYPE_NONE:
615                 vty_out(vty, " sim none%s", VTY_NEWLINE);
616                 break;
617                 case GSM_SIM_TYPE_SLOT:
618                 vty_out(vty, " sim slot%s", VTY_NEWLINE);
619                 break;
620                 case GSM_SIM_TYPE_TEST:
621                 vty_out(vty, " sim test%s", VTY_NEWLINE);
622                 break;
623         }
624         vty_out(vty, " network-selection-mode %s%s", (set->plmn_mode
625                         == PLMN_MODE_AUTO) ? "auto" : "manual", VTY_NEWLINE);
626         vty_out(vty, " imei %s %s%s", set->imei,
627                 set->imeisv + strlen(set->imei), VTY_NEWLINE);
628         if (set->imei_random)
629                 vty_out(vty, " imei-random %d%s", set->imei_random,
630                         VTY_NEWLINE);
631         else
632                 vty_out(vty, " imei-fixed%s", VTY_NEWLINE);
633         if (set->emergency_imsi[0])
634                 vty_out(vty, " emergency-imsi %s%s", set->emergency_imsi,
635                         VTY_NEWLINE);
636         else
637                 vty_out(vty, " no emergency-imsi%s", VTY_NEWLINE);
638         vty_out(vty, " %scall-waiting%s", (set->cw) ? "" : "no ", VTY_NEWLINE);
639         vty_out(vty, " %sclip%s", (set->clip) ? "" : "no ", VTY_NEWLINE);
640         vty_out(vty, " %sclir%s", (set->clir) ? "" : "no ", VTY_NEWLINE);
641         vty_out(vty, " test-sim%s", VTY_NEWLINE);
642         vty_out(vty, "  imsi %s%s", set->test_imsi, VTY_NEWLINE);
643         vty_out(vty, "  %sbarred-access%s", (set->test_barr) ? "" : "no ",
644                 VTY_NEWLINE);
645         if (set->test_rplmn_valid)
646                 vty_out(vty, "  rplmn %s %s%s",
647                         gsm_print_mcc(set->test_rplmn_mcc),
648                         gsm_print_mnc(set->test_rplmn_mnc),
649                         VTY_NEWLINE);
650         else
651                 vty_out(vty, "  no rplmn%s", VTY_NEWLINE);
652         vty_out(vty, "  hplmn-search %s%s", (set->test_always) ? "everywhere"
653                         : "foreign-country", VTY_NEWLINE);
654         vty_out(vty, " exit%s", VTY_NEWLINE);
655         if (set->alter_tx_power)
656                 if (set->alter_tx_power_value)
657                         vty_out(vty, " tx-power %d%s",
658                                 set->alter_tx_power_value, VTY_NEWLINE);
659                 else
660                         vty_out(vty, " tx-power full%s", VTY_NEWLINE);
661         else
662                 vty_out(vty, " tx-power auto%s", VTY_NEWLINE);
663         if (set->alter_delay)
664                 vty_out(vty, " simulated-delay %d%s", set->alter_delay,
665                         VTY_NEWLINE);
666         else
667                 vty_out(vty, " no simulated-delay%s", VTY_NEWLINE);
668         if (set->stick)
669                 vty_out(vty, " stick %d%s", set->stick_arfcn,
670                         VTY_NEWLINE);
671         else
672                 vty_out(vty, " no stick%s", VTY_NEWLINE);
673         if (set->no_lupd)
674                 vty_out(vty, " no location-updating%s", VTY_NEWLINE);
675         else
676                 vty_out(vty, " location-updating%s", VTY_NEWLINE);
677         vty_out(vty, "exit%s", VTY_NEWLINE);
678         vty_out(vty, "!%s", VTY_NEWLINE);
679 }
680
681 static int config_write_ms(struct vty *vty)
682 {
683         struct osmocom_ms *ms;
684
685         vty_out(vty, "gps device %s%s", gps.device, VTY_NEWLINE);
686         if (gps.baud)
687                 vty_out(vty, "gps baudrate %d%s", gps.baud, VTY_NEWLINE);
688         else
689                 vty_out(vty, "gps baudrate default%s", VTY_NEWLINE);
690         vty_out(vty, "%sgps enable%s", (gps.enable) ? "" : "no ", VTY_NEWLINE);
691         vty_out(vty, "!%s", VTY_NEWLINE);
692
693         llist_for_each_entry(ms, &ms_list, entity)
694                 config_write_ms_single(vty, ms);
695
696         return CMD_SUCCESS;
697 }
698
699 DEFUN(cfg_ms_sim, cfg_ms_sim_cmd, "sim (none|test)",
700         "Set sim card type when powering on\nNo sim interted\n"
701         "Test sim inserted")
702 {
703         struct osmocom_ms *ms = vty->index;
704
705         switch (argv[0][0]) {
706         case 'n':
707                 ms->settings.simtype = GSM_SIM_TYPE_NONE;
708                 break;
709         case 's':
710                 ms->settings.simtype = GSM_SIM_TYPE_SLOT;
711                 break;
712         case 't':
713                 ms->settings.simtype = GSM_SIM_TYPE_TEST;
714                 break;
715         }
716
717         return CMD_SUCCESS;
718 }
719
720 DEFUN(cfg_ms_mode, cfg_ms_mode_cmd, "network-selection-mode (auto|manual)",
721         "Set network selection mode\nAutomatic network selection\n"
722         "Manual network selection")
723 {
724         struct osmocom_ms *ms = vty->index;
725         struct msgb *nmsg;
726
727         if (!ms->plmn.state) {
728                 if (argv[0][0] == 'a')
729                         ms->settings.plmn_mode = PLMN_MODE_AUTO;
730                 else
731                         ms->settings.plmn_mode = PLMN_MODE_MANUAL;
732
733                 return CMD_SUCCESS;
734         }
735         if (argv[0][0] == 'a')
736                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SEL_AUTO);
737         else
738                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SEL_MANUAL);
739         if (!nmsg)
740                 return CMD_WARNING;
741         gsm322_plmn_sendmsg(ms, nmsg);
742
743         return CMD_SUCCESS;
744 }
745
746 DEFUN(cfg_ms_imei, cfg_ms_imei_cmd, "imei IMEI [SV]",
747         "Set IMEI (enter without control digit)\n15 Digits IMEI\n"
748         "Software version digit")
749 {
750         struct osmocom_ms *ms = vty->index;
751         char *error, *sv = "0";
752
753         if (argc >= 2)
754                 sv = (char *)argv[1];
755
756         error = gsm_check_imei(argv[0], sv);
757         if (error) {
758                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
759                 return CMD_WARNING;
760         }
761
762         strcpy(ms->settings.imei, argv[0]);
763         strcpy(ms->settings.imeisv, argv[0]);
764         strcpy(ms->settings.imeisv + 15, sv);
765
766         return CMD_SUCCESS;
767 }
768
769 DEFUN(cfg_ms_imei_fixed, cfg_ms_imei_fixed_cmd, "imei-fixed",
770         "Use fixed IMEI on every power on")
771 {
772         struct osmocom_ms *ms = vty->index;
773
774         ms->settings.imei_random = 0;
775
776         return CMD_SUCCESS;
777 }
778
779 DEFUN(cfg_ms_imei_random, cfg_ms_imei_random_cmd, "imei-random <0-15>",
780         "Use random IMEI on every power on\n"
781         "Number of trailing digits to randomize")
782 {
783         struct osmocom_ms *ms = vty->index;
784
785         ms->settings.imei_random = atoi(argv[0]);
786
787         return CMD_SUCCESS;
788 }
789
790 DEFUN(cfg_ms_emerg_imsi, cfg_ms_emerg_imsi_cmd, "emergency-imsi IMSI",
791         "Use special IMSI for emergency calls\n15 digits IMSI")
792 {
793         struct osmocom_ms *ms = vty->index;
794         char *error;
795
796         error = gsm_check_imsi(argv[0]);
797         if (error) {
798                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
799                 return CMD_WARNING;
800         }
801         strcpy(ms->settings.emergency_imsi, argv[0]);
802
803         return CMD_SUCCESS;
804 }
805
806 DEFUN(cfg_ms_no_emerg_imsi, cfg_ms_no_emerg_imsi_cmd, "no emergency-imsi",
807         NO_STR "Use IMSI of SIM or IMEI for emergency calls")
808 {
809         struct osmocom_ms *ms = vty->index;
810
811         ms->settings.emergency_imsi[0] = '\0';
812
813         return CMD_SUCCESS;
814 }
815
816 DEFUN(cfg_no_cw, cfg_ms_no_cw_cmd, "no call-waiting",
817         NO_STR "Disallow waiting calls")
818 {
819         struct osmocom_ms *ms = vty->index;
820
821         ms->settings.cw = 0;
822
823         return CMD_SUCCESS;
824 }
825
826 DEFUN(cfg_cw, cfg_ms_cw_cmd, "call-waiting",
827         "Allow waiting calls")
828 {
829         struct osmocom_ms *ms = vty->index;
830
831         ms->settings.cw = 1;
832
833         return CMD_SUCCESS;
834 }
835
836 DEFUN(cfg_clip, cfg_ms_clip_cmd, "clip",
837         "Force caller ID presentation")
838 {
839         struct osmocom_ms *ms = vty->index;
840
841         ms->settings.clip = 1;
842         ms->settings.clir = 0;
843
844         return CMD_SUCCESS;
845 }
846
847 DEFUN(cfg_clir, cfg_ms_clir_cmd, "clir",
848         "Force caller ID restriction")
849 {
850         struct osmocom_ms *ms = vty->index;
851
852         ms->settings.clip = 0;
853         ms->settings.clir = 1;
854
855         return CMD_SUCCESS;
856 }
857
858 DEFUN(cfg_no_clip, cfg_ms_no_clip_cmd, "no clip",
859         NO_STR "Disable forcing of caller ID presentation")
860 {
861         struct osmocom_ms *ms = vty->index;
862
863         ms->settings.clip = 0;
864
865         return CMD_SUCCESS;
866 }
867
868 DEFUN(cfg_no_clir, cfg_ms_no_clir_cmd, "no clir",
869         NO_STR "Disable forcing of caller ID restriction")
870 {
871         struct osmocom_ms *ms = vty->index;
872
873         ms->settings.clir = 0;
874
875         return CMD_SUCCESS;
876 }
877
878 DEFUN(cfg_ms_tx_power, cfg_ms_tx_power_cmd, "tx-power (auto|full)",
879         "Set the way to choose transmit power\nControlled by BTS\n"
880         "Always full power\nFixed GSM power value if supported")
881 {
882         struct osmocom_ms *ms = vty->index;
883
884         switch (argv[0][0]) {
885         case 'a':
886                 ms->settings.alter_tx_power = 0;
887                 break;
888         case 'f':
889                 ms->settings.alter_tx_power = 1;
890                 ms->settings.alter_tx_power_value = 0;
891                 break;
892         }
893
894         return CMD_SUCCESS;
895 }
896
897 DEFUN(cfg_ms_tx_power_val, cfg_ms_tx_power_val_cmd, "tx-power <0-31>",
898         "Set the way to choose transmit power\n"
899         "Fixed GSM power value if supported")
900 {
901         struct osmocom_ms *ms = vty->index;
902
903         ms->settings.alter_tx_power = 1;
904         ms->settings.alter_tx_power_value = atoi(argv[0]);
905
906         return CMD_SUCCESS;
907 }
908
909 DEFUN(cfg_ms_sim_delay, cfg_ms_sim_delay_cmd, "simulated-delay <-128-127>",
910         "Simulate a lower or higher distance from the BTS\n"
911         "Delay in half bits (distance in 553.85 meter steps)")
912 {
913         struct osmocom_ms *ms = vty->index;
914
915         ms->settings.alter_delay = atoi(argv[0]);
916
917         return CMD_SUCCESS;
918 }
919
920 DEFUN(cfg_ms_no_sim_delay, cfg_ms_no_sim_delay_cmd, "no simulated-delay",
921         NO_STR "Do not simulate a lower or higher distance from the BTS")
922 {
923         struct osmocom_ms *ms = vty->index;
924
925         ms->settings.alter_delay = 0;
926
927         return CMD_SUCCESS;
928 }
929
930 DEFUN(cfg_ms_stick, cfg_ms_stick_cmd, "stick <0-1023>",
931         "Stick to the given cell\nARFCN of the cell to stick to")
932 {
933         struct osmocom_ms *ms = vty->index;
934
935         ms->settings.stick = 1;
936         ms->settings.stick_arfcn = atoi(argv[0]);
937
938         return CMD_SUCCESS;
939 }
940
941 DEFUN(cfg_ms_no_stick, cfg_ms_no_stick_cmd, "no stick",
942         NO_STR "Do not stick to any cell")
943 {
944         struct osmocom_ms *ms = vty->index;
945
946         ms->settings.stick = 0;
947
948         return CMD_SUCCESS;
949 }
950
951 DEFUN(cfg_ms_lupd, cfg_ms_lupd_cmd, "location-updating",
952         "Allow location updating")
953 {
954         struct osmocom_ms *ms = vty->index;
955
956         ms->settings.no_lupd = 0;
957
958         return CMD_SUCCESS;
959 }
960
961 DEFUN(cfg_ms_no_lupd, cfg_ms_no_lupd_cmd, "no location-updating",
962         NO_STR "Do not allow location updating")
963 {
964         struct osmocom_ms *ms = vty->index;
965
966         ms->settings.no_lupd = 1;
967
968         return CMD_SUCCESS;
969 }
970
971 /* per testsim config */
972 DEFUN(cfg_ms_testsim, cfg_ms_testsim_cmd, "test-sim",
973         "Configure test SIM emulation")
974 {
975         vty->node = TESTSIM_NODE;
976
977         return CMD_SUCCESS;
978 }
979
980 static int config_write_dummy(struct vty *vty)
981 {
982         return CMD_SUCCESS;
983 }
984
985 DEFUN(cfg_test_imsi, cfg_test_imsi_cmd, "imsi IMSI",
986         "Set IMSI on test card\n15 digits IMSI")
987 {
988         struct osmocom_ms *ms = vty->index;
989         char *error = gsm_check_imsi(argv[0]);
990
991         if (error) {
992                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
993                 return CMD_WARNING;
994         }
995
996         strcpy(ms->settings.test_imsi, argv[0]);
997
998         return CMD_SUCCESS;
999 }
1000
1001 DEFUN(cfg_test_barr, cfg_test_barr_cmd, "barred-access",
1002         "Allow access to barred cells")
1003 {
1004         struct osmocom_ms *ms = vty->index;
1005
1006         ms->settings.test_barr = 1;
1007
1008         return CMD_SUCCESS;
1009 }
1010
1011 DEFUN(cfg_test_no_barr, cfg_test_no_barr_cmd, "no barred-access",
1012         NO_STR "Deny access to barred cells")
1013 {
1014         struct osmocom_ms *ms = vty->index;
1015
1016         ms->settings.test_barr = 0;
1017
1018         return CMD_SUCCESS;
1019 }
1020
1021 DEFUN(cfg_test_no_rplmn, cfg_test_no_rplmn_cmd, "no rplmn",
1022         NO_STR "Unset Registered PLMN")
1023 {
1024         struct osmocom_ms *ms = vty->index;
1025
1026         ms->settings.test_rplmn_valid = 0;
1027
1028         return CMD_SUCCESS;
1029 }
1030
1031 DEFUN(cfg_test_rplmn, cfg_test_rplmn_cmd, "rplmn MCC MNC",
1032         "Set Registered PLMN\nMobile Country Code\nMobile Network Code")
1033 {
1034         struct osmocom_ms *ms = vty->index;
1035         uint16_t mcc = gsm_input_mcc((char *)argv[0]),
1036                  mnc = gsm_input_mnc((char *)argv[1]);
1037
1038         if (!mcc) {
1039                 vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
1040                 return CMD_WARNING;
1041         }
1042         if (!mnc) {
1043                 vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
1044                 return CMD_WARNING;
1045         }
1046         ms->settings.test_rplmn_valid = 1;
1047         ms->settings.test_rplmn_mcc = mcc;
1048         ms->settings.test_rplmn_mnc = mnc;
1049
1050         return CMD_SUCCESS;
1051 }
1052
1053 DEFUN(cfg_test_hplmn, cfg_test_hplmn_cmd, "hplmn-search (everywhere|foreign-country)",
1054         "Set Home PLMN search mode\n"
1055         "Search for HPLMN when on any other network\n"
1056         "Search for HPLMN when in a different country")
1057 {
1058         struct osmocom_ms *ms = vty->index;
1059
1060         switch (argv[0][0]) {
1061         case 'e':
1062                 ms->settings.test_always = 1;
1063                 break;
1064         case 'f':
1065                 ms->settings.test_always = 0;
1066                 break;
1067         }
1068
1069         return CMD_SUCCESS;
1070 }
1071
1072 enum node_type ms_vty_go_parent(struct vty *vty)
1073 {
1074         switch (vty->node) {
1075         case MS_NODE:
1076                 vty->node = CONFIG_NODE;
1077                 vty->index = NULL;
1078                 break;
1079         case TESTSIM_NODE:
1080                 vty->node = MS_NODE;
1081                 break;
1082         default:
1083                 vty->node = CONFIG_NODE;
1084         }
1085
1086         return vty->node;
1087 }
1088
1089 /* Down vty node level. */
1090 gDEFUN(ournode_exit,
1091        ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
1092 {
1093         switch (vty->node) {
1094         case MS_NODE:
1095                 vty->node = CONFIG_NODE;
1096                 vty->index = NULL;
1097                 break;
1098         case TESTSIM_NODE:
1099                 vty->node = MS_NODE;
1100                 break;
1101         default:
1102                 break;
1103         }
1104         return CMD_SUCCESS;
1105 }
1106
1107 /* End of configuration. */
1108 gDEFUN(ournode_end,
1109        ournode_end_cmd, "end", "End current mode and change to enable mode.")
1110 {
1111         switch (vty->node) {
1112         case VIEW_NODE:
1113         case ENABLE_NODE:
1114                 /* Nothing to do. */
1115                 break;
1116         case CONFIG_NODE:
1117         case VTY_NODE:
1118         case MS_NODE:
1119         case TESTSIM_NODE:
1120                 vty_config_unlock(vty);
1121                 vty->node = ENABLE_NODE;
1122                 vty->index = NULL;
1123                 vty->index_sub = NULL;
1124                 break;
1125         default:
1126                 break;
1127         }
1128         return CMD_SUCCESS;
1129 }
1130
1131 int ms_vty_init(void)
1132 {
1133         install_element_ve(&show_ms_cmd);
1134         install_element_ve(&show_subscr_cmd);
1135         install_element_ve(&show_support_cmd);
1136         install_element_ve(&show_states_cmd);
1137         install_element_ve(&show_cell_cmd);
1138         install_element_ve(&show_cell_si_cmd);
1139         install_element_ve(&show_ba_cmd);
1140         install_element_ve(&show_forb_la_cmd);
1141         install_element_ve(&show_forb_plmn_cmd);
1142         install_element_ve(&monitor_network_cmd);
1143         install_element_ve(&no_monitor_network_cmd);
1144
1145         install_element(ENABLE_NODE, &sim_test_cmd);
1146         install_element(ENABLE_NODE, &sim_remove_cmd);
1147         install_element(ENABLE_NODE, &network_search_cmd);
1148         install_element(ENABLE_NODE, &network_show_cmd);
1149         install_element(ENABLE_NODE, &network_select_cmd);
1150         install_element(ENABLE_NODE, &call_cmd);
1151         install_element(ENABLE_NODE, &call_retr_cmd);
1152
1153         install_element(CONFIG_NODE, &cfg_gps_device_cmd);
1154         install_element(CONFIG_NODE, &cfg_gps_baud_cmd);
1155         install_element(CONFIG_NODE, &cfg_gps_enable_cmd);
1156         install_element(CONFIG_NODE, &cfg_no_gps_enable_cmd);
1157
1158         install_element(CONFIG_NODE, &cfg_ms_cmd);
1159         install_element(CONFIG_NODE, &ournode_end_cmd);
1160         install_node(&ms_node, config_write_ms);
1161         install_default(MS_NODE);
1162         install_element(MS_NODE, &ournode_exit_cmd);
1163         install_element(MS_NODE, &ournode_end_cmd);
1164         install_element(MS_NODE, &cfg_ms_sim_cmd);
1165         install_element(MS_NODE, &cfg_ms_mode_cmd);
1166         install_element(MS_NODE, &cfg_ms_imei_cmd);
1167         install_element(MS_NODE, &cfg_ms_imei_fixed_cmd);
1168         install_element(MS_NODE, &cfg_ms_imei_random_cmd);
1169         install_element(MS_NODE, &cfg_ms_no_emerg_imsi_cmd);
1170         install_element(MS_NODE, &cfg_ms_emerg_imsi_cmd);
1171         install_element(MS_NODE, &cfg_ms_cw_cmd);
1172         install_element(MS_NODE, &cfg_ms_no_cw_cmd);
1173         install_element(MS_NODE, &cfg_ms_clip_cmd);
1174         install_element(MS_NODE, &cfg_ms_clir_cmd);
1175         install_element(MS_NODE, &cfg_ms_no_clip_cmd);
1176         install_element(MS_NODE, &cfg_ms_no_clir_cmd);
1177         install_element(MS_NODE, &cfg_ms_testsim_cmd);
1178         install_element(MS_NODE, &cfg_ms_tx_power_cmd);
1179         install_element(MS_NODE, &cfg_ms_tx_power_val_cmd);
1180         install_element(MS_NODE, &cfg_ms_sim_delay_cmd);
1181         install_element(MS_NODE, &cfg_ms_no_sim_delay_cmd);
1182         install_element(MS_NODE, &cfg_ms_stick_cmd);
1183         install_element(MS_NODE, &cfg_ms_no_stick_cmd);
1184         install_element(MS_NODE, &cfg_ms_lupd_cmd);
1185         install_element(MS_NODE, &cfg_ms_no_lupd_cmd);
1186
1187         install_node(&testsim_node, config_write_dummy);
1188         install_default(TESTSIM_NODE);
1189         install_element(TESTSIM_NODE, &ournode_exit_cmd);
1190         install_element(TESTSIM_NODE, &ournode_end_cmd);
1191         install_element(TESTSIM_NODE, &cfg_test_imsi_cmd);
1192         install_element(TESTSIM_NODE, &cfg_test_barr_cmd);
1193         install_element(TESTSIM_NODE, &cfg_test_no_barr_cmd);
1194         install_element(TESTSIM_NODE, &cfg_test_no_rplmn_cmd);
1195         install_element(TESTSIM_NODE, &cfg_test_rplmn_cmd);
1196         install_element(TESTSIM_NODE, &cfg_test_hplmn_cmd);
1197
1198         return 0;
1199 }
1200
1201 void vty_notify(struct osmocom_ms *ms, const char *fmt, ...)
1202 {
1203         struct telnet_connection *connection;
1204         char buffer[1000];
1205         va_list args;
1206         struct vty *vty;
1207
1208         if (fmt) {
1209                 va_start(args, fmt);
1210                 vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
1211                 buffer[sizeof(buffer) - 1] = '\0';
1212                 va_end(args);
1213
1214                 if (!buffer[0])
1215                         return;
1216         }
1217
1218         llist_for_each_entry(connection, &active_connections, entry) {
1219                 vty = connection->vty;
1220                 if (!vty)
1221                         continue;
1222                 if (!fmt) {
1223                         vty_out(vty, "%s%% (MS %s)%s", VTY_NEWLINE, ms->name,
1224                                 VTY_NEWLINE);
1225                         continue;
1226                 }
1227                 if (buffer[strlen(buffer) - 1] == '\n') {
1228                         buffer[strlen(buffer) - 1] = '\0';
1229                         vty_out(vty, "%% %s%s", buffer, VTY_NEWLINE);
1230                         buffer[strlen(buffer)] = '\n';
1231                 } else
1232                         vty_out(vty, "%% %s", buffer);
1233         }
1234 }
1235