[layer23] Moving sysinfo.c and gps.c (.h) to liblayer23
[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/utils.h>
29 #include <osmocore/gsm48.h>
30 #include <osmocore/talloc.h>
31
32 #include <osmocom/bb/common/osmocom_data.h>
33 #include <osmocom/bb/common/networks.h>
34 #include <osmocom/bb/common/gps.h>
35 #include <osmocom/bb/mobile/mncc.h>
36 #include <osmocom/bb/mobile/transaction.h>
37 #include <osmocom/bb/mobile/vty.h>
38 #include <osmocom/vty/telnet_interface.h>
39
40 void *l23_ctx;
41
42 int mncc_call(struct osmocom_ms *ms, char *number);
43 int mncc_hangup(struct osmocom_ms *ms);
44 int mncc_answer(struct osmocom_ms *ms);
45 int mncc_hold(struct osmocom_ms *ms);
46 int mncc_retrieve(struct osmocom_ms *ms, int number);
47 int mncc_dtmf(struct osmocom_ms *ms, char *dtmf);
48
49 extern struct llist_head ms_list;
50 extern struct llist_head active_connections;
51
52 struct cmd_node ms_node = {
53         MS_NODE,
54         "%s(ms)#",
55         1
56 };
57
58 struct cmd_node testsim_node = {
59         TESTSIM_NODE,
60         "%s(test-sim)#",
61         1
62 };
63
64 struct cmd_node support_node = {
65         SUPPORT_NODE,
66         "%s(support)#",
67         1
68 };
69
70 static void print_vty(void *priv, const char *fmt, ...)
71 {
72         char buffer[1000];
73         struct vty *vty = priv;
74         va_list args;
75
76         va_start(args, fmt);
77         vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
78         buffer[sizeof(buffer) - 1] = '\0';
79         va_end(args);
80
81         if (buffer[0]) {
82                 if (buffer[strlen(buffer) - 1] == '\n') {
83                         buffer[strlen(buffer) - 1] = '\0';
84                         vty_out(vty, "%s%s", buffer, VTY_NEWLINE);
85                 } else
86                         vty_out(vty, "%s", buffer);
87         }
88 }
89
90 int vty_check_number(struct vty *vty, const char *number)
91 {
92         int i;
93
94         for (i = 0; i < strlen(number); i++) {
95                 /* allow international notation with + */
96                 if (i == 0 && number[i] == '+')
97                         continue;
98                 if (!(number[i] >= '0' && number[i] <= '9')
99                  && number[i] != '*'
100                  && number[i] != '#'
101                  && !(number[i] >= 'a' && number[i] <= 'c')) {
102                         vty_out(vty, "Invalid digit '%c' of number!%s",
103                                 number[i], VTY_NEWLINE);
104                         return -EINVAL;
105                 }
106         }
107         if (number[0] == '\0' || (number[0] == '+' && number[1] == '\0')) {
108                 vty_out(vty, "Given number has no digits!%s", VTY_NEWLINE);
109                 return -EINVAL;
110         }
111
112         return 0;
113 }
114
115 int vty_reading = 0;
116
117 static void vty_restart(struct vty *vty)
118 {
119         if (vty_reading)
120                 return;
121         vty_out(vty, "You must restart for change to take effect!%s",
122                 VTY_NEWLINE);
123 }
124
125 static struct osmocom_ms *get_ms(const char *name, struct vty *vty)
126 {
127         struct osmocom_ms *ms;
128
129         llist_for_each_entry(ms, &ms_list, entity) {
130                 if (!strcmp(ms->name, name))
131                         return ms;
132         }
133         vty_out(vty, "MS name '%s' does not exits.%s", name, VTY_NEWLINE);
134
135         return NULL;
136 }
137
138 DEFUN(show_ms, show_ms_cmd, "show ms",
139         SHOW_STR "Display available MS entities\n")
140 {
141         struct osmocom_ms *ms;
142
143         llist_for_each_entry(ms, &ms_list, entity) {
144                 struct gsm_settings *set = &ms->settings;
145
146                 vty_out(vty, "MS NAME: %s%s", ms->name, VTY_NEWLINE);
147                 vty_out(vty, " IMEI: %s%s", set->imei, VTY_NEWLINE);
148                 vty_out(vty, " IMEISV: %s%s", set->imeisv, VTY_NEWLINE);
149                 if (set->imei_random)
150                         vty_out(vty, " IMEI generation: random (%d trailing "
151                                 "digits)%s", set->imei_random, VTY_NEWLINE);
152                 else
153                         vty_out(vty, " IMEI generation: fixed%s", VTY_NEWLINE);
154                 vty_out(vty, " network selection mode: %s%s",
155                         (set->plmn_mode == PLMN_MODE_AUTO)
156                                 ? "automatic" : "manual", VTY_NEWLINE);
157         }
158
159         return CMD_SUCCESS;
160 }
161
162 DEFUN(show_support, show_support_cmd, "show support [ms_name]",
163         SHOW_STR "Display information about MS support\n"
164         "Name of MS (see \"show ms\")")
165 {
166         struct osmocom_ms *ms;
167
168         if (argc) {
169                 ms = get_ms(argv[0], vty);
170                 if (!ms)
171                         return CMD_WARNING;
172                 gsm_support_dump(ms, print_vty, vty);
173         } else {
174                 llist_for_each_entry(ms, &ms_list, entity) {
175                         gsm_support_dump(ms, print_vty, vty);
176                         vty_out(vty, "%s", VTY_NEWLINE);
177                 }
178         }
179
180         return CMD_SUCCESS;
181 }
182
183 static void gsm_states_dump(struct osmocom_ms *ms, struct vty *vty)
184 {
185         struct gsm_settings *set = &ms->settings;
186         struct gsm_trans *trans;
187
188         vty_out(vty, "Current state of MS '%s'%s", ms->name, VTY_NEWLINE);
189         if (set->plmn_mode == PLMN_MODE_AUTO)
190                 vty_out(vty, " automatic network selection: %s%s", 
191                         plmn_a_state_names[ms->plmn.state], VTY_NEWLINE);
192         else
193                 vty_out(vty, " manual network selection: %s%s", 
194                         plmn_m_state_names[ms->plmn.state], VTY_NEWLINE);
195         vty_out(vty, " cell selection: %s",
196                 cs_state_names[ms->cellsel.state]);
197         if (ms->rrlayer.state == GSM48_RR_ST_IDLE && ms->cellsel.selected)
198                 vty_out(vty, " (ARFCN %d)", ms->cellsel.sel_arfcn);
199         vty_out(vty, "%s", VTY_NEWLINE);
200         vty_out(vty, " radio ressource layer: %s%s", 
201                 gsm48_rr_state_names[ms->rrlayer.state], VTY_NEWLINE);
202         vty_out(vty, " mobility management layer: %s", 
203                 gsm48_mm_state_names[ms->mmlayer.state]);
204         if (ms->mmlayer.state == GSM48_MM_ST_MM_IDLE)
205                 vty_out(vty, ", %s", 
206                         gsm48_mm_substate_names[ms->mmlayer.substate]);
207         vty_out(vty, "%s", VTY_NEWLINE);
208         llist_for_each_entry(trans, &ms->trans_list, entry) {
209                 vty_out(vty, " call control: %s%s", 
210                         gsm48_cc_state_name(trans->cc.state), VTY_NEWLINE);
211         }
212 }
213
214 DEFUN(show_states, show_states_cmd, "show states [ms_name]",
215         SHOW_STR "Display current states of given MS\n"
216         "Name of MS (see \"show ms\")")
217 {
218         struct osmocom_ms *ms;
219
220         if (argc) {
221                 ms = get_ms(argv[0], vty);
222                 if (!ms)
223                         return CMD_WARNING;
224                 gsm_states_dump(ms, vty);
225         } else {
226                 llist_for_each_entry(ms, &ms_list, entity) {
227                         gsm_states_dump(ms, vty);
228                         vty_out(vty, "%s", VTY_NEWLINE);
229                 }
230         }
231
232         return CMD_SUCCESS;
233 }
234
235 DEFUN(show_subscr, show_subscr_cmd, "show subscriber [ms_name]",
236         SHOW_STR "Display information about subscriber\n"
237         "Name of MS (see \"show ms\")")
238 {
239         struct osmocom_ms *ms;
240
241         if (argc) {
242                 ms = get_ms(argv[0], vty);
243                 if (!ms)
244                         return CMD_WARNING;
245                 gsm_subscr_dump(&ms->subscr, print_vty, vty);
246         } else {
247                 llist_for_each_entry(ms, &ms_list, entity) {
248                         gsm_subscr_dump(&ms->subscr, print_vty, vty);
249                         vty_out(vty, "%s", VTY_NEWLINE);
250                 }
251         }
252
253         return CMD_SUCCESS;
254 }
255
256 DEFUN(show_cell, show_cell_cmd, "show cell MS_NAME",
257         SHOW_STR "Display information about received cells\n"
258         "Name of MS (see \"show ms\")")
259 {
260         struct osmocom_ms *ms;
261
262         ms = get_ms(argv[0], vty);
263         if (!ms)
264                 return CMD_WARNING;
265
266         gsm322_dump_cs_list(&ms->cellsel, GSM322_CS_FLAG_SYSINFO, print_vty,
267                 vty);
268
269         return CMD_SUCCESS;
270 }
271
272 DEFUN(show_cell_si, show_cell_si_cmd, "show cell MS_NAME <0-1023>",
273         SHOW_STR "Display information about received cell\n"
274         "Name of MS (see \"show ms\")\nRadio frequency number")
275 {
276         struct osmocom_ms *ms;
277         int i;
278         struct gsm48_sysinfo *s;
279
280         ms = get_ms(argv[0], vty);
281         if (!ms)
282                 return CMD_WARNING;
283
284         i = atoi(argv[1]);
285         if (i < 0 || i > 1023) {
286                 vty_out(vty, "Given ARFCN '%s' not in range (0..1023)%s",
287                         argv[1], VTY_NEWLINE);
288                 return CMD_WARNING;
289         }
290         s = ms->cellsel.list[i].sysinfo;
291         if (!s) {
292                 vty_out(vty, "Given ARFCN '%s' has no sysinfo available%s",
293                         argv[1], VTY_NEWLINE);
294                 return CMD_SUCCESS;
295         }
296
297         gsm48_sysinfo_dump(s, i, print_vty, vty);
298
299         return CMD_SUCCESS;
300 }
301
302 DEFUN(show_ba, show_ba_cmd, "show ba MS_NAME [mcc] [mnc]",
303         SHOW_STR "Display information about band allocations\n"
304         "Name of MS (see \"show ms\")\nMobile Country Code\n"
305         "Mobile Network Code")
306 {
307         struct osmocom_ms *ms;
308         uint16_t mcc = 0, mnc = 0;
309
310         ms = get_ms(argv[0], vty);
311         if (!ms)
312                 return CMD_WARNING;
313
314         if (argc >= 3) {
315                 mcc = gsm_input_mcc((char *)argv[1]);
316                 mnc = gsm_input_mnc((char *)argv[2]);
317                 if (!mcc) {
318                         vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
319                         return CMD_WARNING;
320                 }
321                 if (!mnc) {
322                         vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
323                         return CMD_WARNING;
324                 }
325         }
326
327         gsm322_dump_ba_list(&ms->cellsel, mcc, mnc, print_vty, vty);
328
329         return CMD_SUCCESS;
330 }
331
332 DEFUN(show_forb_plmn, show_forb_plmn_cmd, "show forbidden plmn MS_NAME",
333         SHOW_STR "Display information about forbidden cells / networks\n"
334         "Display forbidden PLMNs\nName of MS (see \"show ms\")")
335 {
336         struct osmocom_ms *ms;
337
338         ms = get_ms(argv[0], vty);
339         if (!ms)
340                 return CMD_WARNING;
341
342         gsm_subscr_dump_forbidden_plmn(ms, print_vty, vty);
343
344         return CMD_SUCCESS;
345 }
346
347 DEFUN(show_forb_la, show_forb_la_cmd, "show forbidden location-area MS_NAME",
348         SHOW_STR "Display information about forbidden cells / networks\n"
349         "Display forbidden location areas\nName of MS (see \"show ms\")")
350 {
351         struct osmocom_ms *ms;
352
353         ms = get_ms(argv[0], vty);
354         if (!ms)
355                 return CMD_WARNING;
356
357         gsm322_dump_forbidden_la(ms, print_vty, vty);
358
359         return CMD_SUCCESS;
360 }
361
362 DEFUN(monitor_network, monitor_network_cmd, "monitor network MS_NAME",
363         "Monitor...\nMonitor network information\nName of MS (see \"show ms\")")
364 {
365         struct osmocom_ms *ms;
366
367         ms = get_ms(argv[0], vty);
368         if (!ms)
369                 return CMD_WARNING;
370
371         gsm48_rr_start_monitor(ms);
372
373         return CMD_SUCCESS;
374 }
375
376 DEFUN(no_monitor_network, no_monitor_network_cmd, "no monitor network MS_NAME",
377         NO_STR "Monitor...\nDeactivate monitor of network information\n"
378         "Name of MS (see \"show ms\")")
379 {
380         struct osmocom_ms *ms;
381
382         ms = get_ms(argv[0], vty);
383         if (!ms)
384                 return CMD_WARNING;
385
386         gsm48_rr_stop_monitor(ms);
387
388         return CMD_SUCCESS;
389 }
390
391 DEFUN(sim_test, sim_test_cmd, "sim testcard MS_NAME [mcc] [mnc]",
392         "SIM actions\nInsert test card\nName of MS (see \"show ms\")\n"
393         "Mobile Country Code of RPLMN\nMobile Network Code of RPLMN")
394 {
395         struct osmocom_ms *ms;
396         uint16_t mcc = 0x001, mnc = 0x01f;
397
398         ms = get_ms(argv[0], vty);
399         if (!ms)
400                 return CMD_WARNING;
401
402         if (ms->subscr.sim_valid) {
403                 vty_out(vty, "SIM already presend, remove first!%s",
404                         VTY_NEWLINE);
405                 return CMD_WARNING;
406         }
407
408         if (argc >= 3) {
409                 mcc = gsm_input_mcc((char *)argv[1]);
410                 mnc = gsm_input_mnc((char *)argv[2]);
411                 if (!mcc) {
412                         vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
413                         return CMD_WARNING;
414                 }
415                 if (!mnc) {
416                         vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
417                         return CMD_WARNING;
418                 }
419         }
420
421         gsm_subscr_testcard(ms, mcc, mnc);
422
423         return CMD_SUCCESS;
424 }
425
426 DEFUN(sim_reader, sim_reader_cmd, "sim reader MS_NAME",
427         "SIM actions\nSelect SIM from reader\nName of MS (see \"show ms\")")
428 {
429         struct osmocom_ms *ms;
430
431         ms = get_ms(argv[0], vty);
432         if (!ms)
433                 return CMD_WARNING;
434
435         if (ms->subscr.sim_valid) {
436                 vty_out(vty, "SIM already presend, remove first!%s",
437                         VTY_NEWLINE);
438                 return CMD_WARNING;
439         }
440
441         gsm_subscr_simcard(ms);
442
443         return CMD_SUCCESS;
444 }
445
446 DEFUN(sim_remove, sim_remove_cmd, "sim remove MS_NAME",
447         "SIM actions\nRemove SIM card\nName of MS (see \"show ms\")")
448 {
449         struct osmocom_ms *ms;
450
451         ms = get_ms(argv[0], vty);
452         if (!ms)
453                 return CMD_WARNING;
454
455         if (!ms->subscr.sim_valid) {
456                 vty_out(vty, "No Sim inserted!%s", VTY_NEWLINE);
457                 return CMD_WARNING;
458         }
459
460         gsm_subscr_remove(ms);
461
462         return CMD_SUCCESS;
463 }
464
465 DEFUN(sim_pin, sim_pin_cmd, "sim pin MS_NAME PIN",
466         "SIM actions\nEnter PIN for SIM card\nName of MS (see \"show ms\")\n"
467         "PIN number")
468 {
469         struct osmocom_ms *ms;
470
471         ms = get_ms(argv[0], vty);
472         if (!ms)
473                 return CMD_WARNING;
474
475         if (strlen(argv[1]) < 4 || strlen(argv[1]) > 8) {
476                 vty_out(vty, "PIN must be in range 4..8!%s", VTY_NEWLINE);
477                 return CMD_WARNING;
478         }
479
480         if (!ms->subscr.sim_pin_required) {
481                 vty_out(vty, "No PIN is required at this time!%s", VTY_NEWLINE);
482                 return CMD_WARNING;
483         }
484
485         gsm_subscr_sim_pin(ms, (char *)argv[1], "", 0);
486
487         return CMD_SUCCESS;
488 }
489
490 DEFUN(sim_disable_pin, sim_disable_pin_cmd, "sim disable-pin MS_NAME PIN",
491         "SIM actions\nDisable PIN of SIM card\nName of MS (see \"show ms\")\n"
492         "PIN number")
493 {
494         struct osmocom_ms *ms;
495
496         ms = get_ms(argv[0], vty);
497         if (!ms)
498                 return CMD_WARNING;
499
500         if (strlen(argv[1]) < 4 || strlen(argv[1]) > 8) {
501                 vty_out(vty, "PIN must be in range 4..8!%s", VTY_NEWLINE);
502                 return CMD_WARNING;
503         }
504
505         gsm_subscr_sim_pin(ms, (char *)argv[1], "", -1);
506
507         return CMD_SUCCESS;
508 }
509
510 DEFUN(sim_enable_pin, sim_enable_pin_cmd, "sim enable-pin MS_NAME PIN",
511         "SIM actions\nEnable PIN of SIM card\nName of MS (see \"show ms\")\n"
512         "PIN number")
513 {
514         struct osmocom_ms *ms;
515
516         ms = get_ms(argv[0], vty);
517         if (!ms)
518                 return CMD_WARNING;
519
520         if (strlen(argv[1]) < 4 || strlen(argv[1]) > 8) {
521                 vty_out(vty, "PIN must be in range 4..8!%s", VTY_NEWLINE);
522                 return CMD_WARNING;
523         }
524
525         gsm_subscr_sim_pin(ms, (char *)argv[1], "", 1);
526
527         return CMD_SUCCESS;
528 }
529
530 DEFUN(sim_change_pin, sim_change_pin_cmd, "sim change-pin MS_NAME OLD NEW",
531         "SIM actions\nChange PIN of SIM card\nName of MS (see \"show ms\")\n"
532         "Old PIN number\nNew PIN number")
533 {
534         struct osmocom_ms *ms;
535
536         ms = get_ms(argv[0], vty);
537         if (!ms)
538                 return CMD_WARNING;
539
540         if (strlen(argv[1]) < 4 || strlen(argv[1]) > 8) {
541                 vty_out(vty, "Old PIN must be in range 4..8!%s", VTY_NEWLINE);
542                 return CMD_WARNING;
543         }
544         if (strlen(argv[2]) < 4 || strlen(argv[2]) > 8) {
545                 vty_out(vty, "New PIN must be in range 4..8!%s", VTY_NEWLINE);
546                 return CMD_WARNING;
547         }
548
549         gsm_subscr_sim_pin(ms, (char *)argv[1], (char *)argv[2], 2);
550
551         return CMD_SUCCESS;
552 }
553
554 DEFUN(sim_unblock_pin, sim_unblock_pin_cmd, "sim unblock-pin MS_NAME PUC NEW",
555         "SIM actions\nChange PIN of SIM card\nName of MS (see \"show ms\")\n"
556         "Personal Unblock Key\nNew PIN number")
557 {
558         struct osmocom_ms *ms;
559
560         ms = get_ms(argv[0], vty);
561         if (!ms)
562                 return CMD_WARNING;
563
564         if (strlen(argv[1]) != 8) {
565                 vty_out(vty, "PUC must be 8 digits!%s", VTY_NEWLINE);
566                 return CMD_WARNING;
567         }
568         if (strlen(argv[2]) < 4 || strlen(argv[2]) > 8) {
569                 vty_out(vty, "PIN must be in range 4..8!%s", VTY_NEWLINE);
570                 return CMD_WARNING;
571         }
572
573         gsm_subscr_sim_pin(ms, (char *)argv[1], (char *)argv[2], 99);
574
575         return CMD_SUCCESS;
576 }
577
578 DEFUN(sim_lai, sim_lai_cmd, "sim lai MS_NAME MCC MNC LAC",
579         "SIM actions\nChange LAI of SIM card\nName of MS (see \"show ms\")\n"
580         "Mobile Country Code\nMobile Network Code\nLocation Area Code "
581         " (use 0000 to remove LAI)")
582 {
583         struct osmocom_ms *ms;
584         uint16_t mcc = gsm_input_mcc((char *)argv[1]),
585                  mnc = gsm_input_mnc((char *)argv[2]),
586                  lac = strtoul(argv[3], NULL, 16);
587
588         ms = get_ms(argv[0], vty);
589         if (!ms)
590                 return CMD_WARNING;
591
592         if (!mcc) {
593                 vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
594                 return CMD_WARNING;
595         }
596         if (!mnc) {
597                 vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
598                 return CMD_WARNING;
599         }
600
601         ms->subscr.mcc = mcc;
602         ms->subscr.mnc = mnc;
603         ms->subscr.lac = lac;
604         ms->subscr.tmsi = 0xffffffff;
605
606         gsm_subscr_write_loci(ms);
607
608         return CMD_SUCCESS;
609 }
610
611 DEFUN(network_select, network_select_cmd, "network select MS_NAME MCC MNC",
612         "Select ...\nSelect Network\nName of MS (see \"show ms\")\n"
613         "Mobile Country Code\nMobile Network Code")
614 {
615         struct osmocom_ms *ms;
616         struct gsm322_plmn *plmn;
617         struct msgb *nmsg;
618         struct gsm322_msg *ngm;
619         struct gsm322_plmn_list *temp;
620         uint16_t mcc = gsm_input_mcc((char *)argv[1]),
621                  mnc = gsm_input_mnc((char *)argv[2]);
622         int found = 0;
623
624         ms = get_ms(argv[0], vty);
625         if (!ms)
626                 return CMD_WARNING;
627         plmn = &ms->plmn;
628
629         if (!mcc) {
630                 vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
631                 return CMD_WARNING;
632         }
633         if (!mnc) {
634                 vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
635                 return CMD_WARNING;
636         }
637
638         llist_for_each_entry(temp, &plmn->sorted_plmn, entry)
639                 if (temp->mcc == mcc &&  temp->mnc == mnc)
640                         found = 1;
641         if (!found) {
642                 vty_out(vty, "Network not in list!%s", VTY_NEWLINE);
643                 return CMD_WARNING;
644         }
645
646         nmsg = gsm322_msgb_alloc(GSM322_EVENT_CHOOSE_PLMN);
647         if (!nmsg)
648                 return CMD_WARNING;
649         ngm = (struct gsm322_msg *) nmsg->data;
650         ngm->mcc = mcc;
651         ngm->mnc = mnc;
652         gsm322_plmn_sendmsg(ms, nmsg);
653
654         return CMD_SUCCESS;
655 }
656
657 DEFUN(call, call_cmd, "call MS_NAME (NUMBER|emergency|answer|hangup|hold)",
658         "Make a call\nName of MS (see \"show ms\")\nPhone number to call "
659         "(Use digits '0123456789*#abc', and '+' to dial international)\n"
660         "Make an emergency call\nAnswer an incomming call\nHangup a call\n"
661         "Hold current active call\n")
662 {
663         struct osmocom_ms *ms;
664         struct gsm_settings *set;
665         struct gsm_settings_abbrev *abbrev;
666         char *number;
667
668         ms = get_ms(argv[0], vty);
669         if (!ms)
670                 return CMD_WARNING;
671         set = &ms->settings;
672
673         if (set->ch_cap == GSM_CAP_SDCCH) {
674                 vty_out(vty, "Basic call is not supported for SDCCH only "
675                         "mobile%s", VTY_NEWLINE);
676                 return CMD_WARNING;
677         }
678
679         number = (char *)argv[1];
680         if (!strcmp(number, "emergency"))
681                 mncc_call(ms, number);
682         else if (!strcmp(number, "answer"))
683                 mncc_answer(ms);
684         else if (!strcmp(number, "hangup"))
685                 mncc_hangup(ms);
686         else if (!strcmp(number, "hold"))
687                 mncc_hold(ms);
688         else {
689                 llist_for_each_entry(abbrev, &set->abbrev, list) {
690                         if (!strcmp(number, abbrev->abbrev)) {
691                                 number = abbrev->number;
692                                 vty_out(vty, "Dialing number '%s'%s", number,
693                                         VTY_NEWLINE);
694                                 break;
695                         }
696                 }
697                 if (vty_check_number(vty, number))
698                         return CMD_WARNING;
699                 mncc_call(ms, number);
700         }
701
702         return CMD_SUCCESS;
703 }
704
705 DEFUN(call_retr, call_retr_cmd, "call MS_NAME retrieve [number]",
706         "Make a call\nName of MS (see \"show ms\")\n"
707         "Retrieve call on hold\nNumber of call to retrieve")
708 {
709         struct osmocom_ms *ms;
710
711         ms = get_ms(argv[0], vty);
712         if (!ms)
713                 return CMD_WARNING;
714
715         mncc_retrieve(ms, (argc > 1) ? atoi(argv[1]) : 0);
716
717         return CMD_SUCCESS;
718 }
719
720 DEFUN(call_dtmf, call_dtmf_cmd, "call MS_NAME dtmf DIGITS",
721         "Make a call\nName of MS (see \"show ms\")\n"
722         "One or more DTMF digits to transmit")
723 {
724         struct osmocom_ms *ms;
725         struct gsm_settings *set;
726
727         ms = get_ms(argv[0], vty);
728         if (!ms)
729                 return CMD_WARNING;
730         set = &ms->settings;
731
732         if (!set->cc_dtmf) {
733                 vty_out(vty, "DTMF not supported, please enable!%s",
734                         VTY_NEWLINE);
735                 return CMD_WARNING;
736         }
737
738         mncc_dtmf(ms, (char *)argv[1]);
739
740         return CMD_SUCCESS;
741 }
742
743 DEFUN(network_show, network_show_cmd, "network show MS_NAME",
744         "Network ...\nShow results of network search (again)\n"
745         "Name of MS (see \"show ms\")")
746 {
747         struct osmocom_ms *ms;
748         struct gsm_settings *set;
749         struct gsm322_plmn *plmn;
750         struct gsm322_plmn_list *temp;
751
752         ms = get_ms(argv[0], vty);
753         if (!ms)
754                 return CMD_WARNING;
755         set = &ms->settings;
756         plmn = &ms->plmn;
757
758         if (set->plmn_mode != PLMN_MODE_AUTO
759          && plmn->state != GSM322_M3_NOT_ON_PLMN) {
760                 vty_out(vty, "Start network search first!%s", VTY_NEWLINE);
761                 return CMD_WARNING;
762         }
763
764         llist_for_each_entry(temp, &plmn->sorted_plmn, entry)
765                 vty_out(vty, " Network %s, %s (%s, %s)%s",
766                         gsm_print_mcc(temp->mcc), gsm_print_mnc(temp->mnc),
767                         gsm_get_mcc(temp->mcc),
768                         gsm_get_mnc(temp->mcc, temp->mnc), VTY_NEWLINE);
769
770         return CMD_SUCCESS;
771 }
772
773 DEFUN(network_search, network_search_cmd, "network search MS_NAME",
774         "Network ...\nTrigger network search\nName of MS (see \"show ms\")")
775 {
776         struct osmocom_ms *ms;
777         struct msgb *nmsg;
778
779         ms = get_ms(argv[0], vty);
780         if (!ms)
781                 return CMD_WARNING;
782
783         nmsg = gsm322_msgb_alloc(GSM322_EVENT_USER_RESEL);
784         if (!nmsg)
785                 return CMD_WARNING;
786         gsm322_plmn_sendmsg(ms, nmsg);
787
788         return CMD_SUCCESS;
789 }
790
791 DEFUN(cfg_gps_enable, cfg_gps_enable_cmd, "gps enable",
792         "GPS receiver")
793 {
794         if (gps_open()) {
795                 gps.enable = 1;
796                 vty_out(vty, "Failed to open GPS device!%s", VTY_NEWLINE);
797                 return CMD_WARNING;
798         }
799         gps.enable = 1;
800
801         return CMD_SUCCESS;
802 }
803
804 DEFUN(cfg_no_gps_enable, cfg_no_gps_enable_cmd, "no gps enable",
805         NO_STR "Disable GPS receiver")
806 {
807         if (gps.enable)
808                 gps_close();
809         gps.enable = 0;
810
811         return CMD_SUCCESS;
812 }
813
814 DEFUN(cfg_gps_device, cfg_gps_device_cmd, "gps device DEVICE",
815         "GPS receiver\nSelect serial device\n"
816         "Full path of serial device including /dev/")
817 {
818         strncpy(gps.device, argv[0], sizeof(gps.device));
819         gps.device[sizeof(gps.device) - 1] = '\0';
820         if (gps.enable) {
821                 gps_close();
822                 if (gps_open()) {
823                         vty_out(vty, "Failed to open GPS device!%s",
824                                 VTY_NEWLINE);
825                         return CMD_WARNING;
826                 }
827         }
828
829         return CMD_SUCCESS;
830 }
831
832 DEFUN(cfg_gps_baud, cfg_gps_baud_cmd, "gps baudrate "
833         "(default|4800|""9600|19200|38400|57600|115200)",
834         "GPS receiver\nSelect baud rate\nDefault, don't modify\n\n\n\n\n\n")
835 {
836         if (argv[0][0] == 'd')
837                 gps.baud = 0;
838         else
839                 gps.baud = atoi(argv[0]);
840         if (gps.enable) {
841                 gps_close();
842                 if (gps_open()) {
843                         gps.enable = 0;
844                         vty_out(vty, "Failed to open GPS device!%s",
845                                 VTY_NEWLINE);
846                         return CMD_WARNING;
847                 }
848         }
849
850         return CMD_SUCCESS;
851 }
852
853 /* per MS config */
854 DEFUN(cfg_ms, cfg_ms_cmd, "ms MS_NAME",
855         "Select a mobile station to configure\nName of MS (see \"show ms\")")
856 {
857         struct osmocom_ms *ms;
858
859         ms = get_ms(argv[0], vty);
860         if (!ms)
861                 return CMD_WARNING;
862
863         vty->index = ms;
864         vty->node = MS_NODE;
865
866         return CMD_SUCCESS;
867 }
868
869 #define SUP_WRITE(item, cmd) \
870         if (sup->item) \
871                 vty_out(vty, "  %s%s%s", (set->item) ? "" : "no ", cmd, \
872                         VTY_NEWLINE);
873
874 static void config_write_ms_single(struct vty *vty, struct osmocom_ms *ms)
875 {
876         struct gsm_settings *set = &ms->settings;
877         struct gsm_support *sup = &ms->support;
878         struct gsm_settings_abbrev *abbrev;
879
880         vty_out(vty, "ms %s%s", ms->name, VTY_NEWLINE);
881         switch(set->sim_type) {
882                 case GSM_SIM_TYPE_NONE:
883                 vty_out(vty, " sim none%s", VTY_NEWLINE);
884                 break;
885                 case GSM_SIM_TYPE_READER:
886                 vty_out(vty, " sim reader%s", VTY_NEWLINE);
887                 break;
888                 case GSM_SIM_TYPE_TEST:
889                 vty_out(vty, " sim test%s", VTY_NEWLINE);
890                 break;
891         }
892         vty_out(vty, " network-selection-mode %s%s", (set->plmn_mode
893                         == PLMN_MODE_AUTO) ? "auto" : "manual", VTY_NEWLINE);
894         vty_out(vty, " imei %s %s%s", set->imei,
895                 set->imeisv + strlen(set->imei), VTY_NEWLINE);
896         if (set->imei_random)
897                 vty_out(vty, " imei-random %d%s", set->imei_random,
898                         VTY_NEWLINE);
899         else
900                 vty_out(vty, " imei-fixed%s", VTY_NEWLINE);
901         if (set->emergency_imsi[0])
902                 vty_out(vty, " emergency-imsi %s%s", set->emergency_imsi,
903                         VTY_NEWLINE);
904         else
905                 vty_out(vty, " no emergency-imsi%s", VTY_NEWLINE);
906         vty_out(vty, " %scall-waiting%s", (set->cw) ? "" : "no ", VTY_NEWLINE);
907         vty_out(vty, " %sclip%s", (set->clip) ? "" : "no ", VTY_NEWLINE);
908         vty_out(vty, " %sclir%s", (set->clir) ? "" : "no ", VTY_NEWLINE);
909         if (set->alter_tx_power)
910                 if (set->alter_tx_power_value)
911                         vty_out(vty, " tx-power %d%s",
912                                 set->alter_tx_power_value, VTY_NEWLINE);
913                 else
914                         vty_out(vty, " tx-power full%s", VTY_NEWLINE);
915         else
916                 vty_out(vty, " tx-power auto%s", VTY_NEWLINE);
917         if (set->alter_delay)
918                 vty_out(vty, " simulated-delay %d%s", set->alter_delay,
919                         VTY_NEWLINE);
920         else
921                 vty_out(vty, " no simulated-delay%s", VTY_NEWLINE);
922         if (set->stick)
923                 vty_out(vty, " stick %d%s", set->stick_arfcn,
924                         VTY_NEWLINE);
925         else
926                 vty_out(vty, " no stick%s", VTY_NEWLINE);
927         if (set->no_lupd)
928                 vty_out(vty, " no location-updating%s", VTY_NEWLINE);
929         else
930                 vty_out(vty, " location-updating%s", VTY_NEWLINE);
931         if (set->full_v1 || set->full_v2 || set->full_v3) {
932                 /* mandatory anyway */
933                 vty_out(vty, " codec full-speed%s%s",
934                         (!set->half_prefer) ? " prefer" : "",
935                         VTY_NEWLINE);
936         }
937         if (set->half_v1 || set->half_v3) {
938                 if (set->half)
939                         vty_out(vty, " codec half-speed%s%s",
940                                 (set->half_prefer) ? " prefer" : "",
941                                 VTY_NEWLINE);
942                 else
943                         vty_out(vty, " no codec half-speed%s", VTY_NEWLINE);
944         }
945         if (llist_empty(&set->abbrev))
946                 vty_out(vty, " no abbrev%s", VTY_NEWLINE);
947         else {
948                 llist_for_each_entry(abbrev, &set->abbrev, list)
949                         vty_out(vty, " abbrev %s %s%s%s%s", abbrev->abbrev,
950                                 abbrev->number, (abbrev->name[0]) ? " " : "",
951                                 abbrev->name, VTY_NEWLINE);
952         }
953         vty_out(vty, " support%s", VTY_NEWLINE);
954         SUP_WRITE(sms_ptp, "sms");
955         SUP_WRITE(a5_1, "a5/1");
956         SUP_WRITE(a5_2, "a5/2");
957         SUP_WRITE(a5_3, "a5/3");
958         SUP_WRITE(a5_4, "a5/4");
959         SUP_WRITE(a5_5, "a5/5");
960         SUP_WRITE(a5_6, "a5/6");
961         SUP_WRITE(a5_7, "a5/7");
962         SUP_WRITE(p_gsm, "p-gsm");
963         SUP_WRITE(e_gsm, "e-gsm");
964         SUP_WRITE(r_gsm, "r-gsm");
965         SUP_WRITE(dcs, "dcs");
966         vty_out(vty, "  class-900 %d%s", set->class_900, VTY_NEWLINE);
967         vty_out(vty, "  class-dcs %d%s", set->class_dcs, VTY_NEWLINE);
968         switch (set->ch_cap) {
969         case GSM_CAP_SDCCH:
970                 vty_out(vty, "  channel-capability sdcch%s", VTY_NEWLINE);
971                 break;
972         case GSM_CAP_SDCCH_TCHF:
973                 vty_out(vty, "  channel-capability sdcch+tchf%s", VTY_NEWLINE);
974                 break;
975         case GSM_CAP_SDCCH_TCHF_TCHH:
976                 vty_out(vty, "  channel-capability sdcch+tchf+tchh%s",
977                         VTY_NEWLINE);
978                 break;
979         }
980         SUP_WRITE(full_v1, "full-speech-v1");
981         SUP_WRITE(full_v2, "full-speech-v2");
982         SUP_WRITE(full_v3, "full-speech-v3");
983         SUP_WRITE(half_v1, "half-speech-v1");
984         SUP_WRITE(half_v3, "half-speech-v3");
985         vty_out(vty, "  min-rxlev %d%s", set->min_rxlev_db, VTY_NEWLINE);
986         vty_out(vty, "  dsc-max %d%s", set->dsc_max, VTY_NEWLINE);
987         vty_out(vty, " exit%s", VTY_NEWLINE);
988         vty_out(vty, " test-sim%s", VTY_NEWLINE);
989         vty_out(vty, "  imsi %s%s", set->test_imsi, VTY_NEWLINE);
990         switch (set->test_ki_type) {
991         case GSM_SIM_KEY_XOR:
992                 vty_out(vty, "  ki xor %s%s", hexdump(set->test_ki, 12),
993                         VTY_NEWLINE);
994                 break;
995         case GSM_SIM_KEY_COMP128:
996                 vty_out(vty, "  ki comp128 %s%s", hexdump(set->test_ki, 16),
997                         VTY_NEWLINE);
998                 break;
999         }
1000         vty_out(vty, "  %sbarred-access%s", (set->test_barr) ? "" : "no ",
1001                 VTY_NEWLINE);
1002         if (set->test_rplmn_valid)
1003                 vty_out(vty, "  rplmn %s %s%s",
1004                         gsm_print_mcc(set->test_rplmn_mcc),
1005                         gsm_print_mnc(set->test_rplmn_mnc),
1006                         VTY_NEWLINE);
1007         else
1008                 vty_out(vty, "  no rplmn%s", VTY_NEWLINE);
1009         vty_out(vty, "  hplmn-search %s%s", (set->test_always) ? "everywhere"
1010                         : "foreign-country", VTY_NEWLINE);
1011         vty_out(vty, " exit%s", VTY_NEWLINE);
1012         vty_out(vty, "exit%s", VTY_NEWLINE);
1013         vty_out(vty, "!%s", VTY_NEWLINE);
1014 }
1015
1016 static int config_write_ms(struct vty *vty)
1017 {
1018         struct osmocom_ms *ms;
1019
1020         vty_out(vty, "gps device %s%s", gps.device, VTY_NEWLINE);
1021         if (gps.baud)
1022                 vty_out(vty, "gps baudrate %d%s", gps.baud, VTY_NEWLINE);
1023         else
1024                 vty_out(vty, "gps baudrate default%s", VTY_NEWLINE);
1025         vty_out(vty, "%sgps enable%s", (gps.enable) ? "" : "no ", VTY_NEWLINE);
1026         vty_out(vty, "!%s", VTY_NEWLINE);
1027
1028         llist_for_each_entry(ms, &ms_list, entity)
1029                 config_write_ms_single(vty, ms);
1030
1031         return CMD_SUCCESS;
1032 }
1033
1034 DEFUN(cfg_ms_sim, cfg_ms_sim_cmd, "sim (none|reader|test)",
1035         "Set SIM card type when powering on\nNo SIM interted\n"
1036         "Use SIM from reader\nTest SIM inserted")
1037 {
1038         struct osmocom_ms *ms = vty->index;
1039         struct gsm_settings *set = &ms->settings;
1040
1041         switch (argv[0][0]) {
1042         case 'n':
1043                 set->sim_type = GSM_SIM_TYPE_NONE;
1044                 break;
1045         case 'r':
1046                 set->sim_type = GSM_SIM_TYPE_READER;
1047                 break;
1048         case 't':
1049                 set->sim_type = GSM_SIM_TYPE_TEST;
1050                 break;
1051         default:
1052                 vty_out(vty, "unknown SIM type%s", VTY_NEWLINE);
1053                 return CMD_WARNING;
1054         }
1055
1056         vty_restart(vty);
1057         return CMD_SUCCESS;
1058 }
1059
1060 DEFUN(cfg_ms_mode, cfg_ms_mode_cmd, "network-selection-mode (auto|manual)",
1061         "Set network selection mode\nAutomatic network selection\n"
1062         "Manual network selection")
1063 {
1064         struct osmocom_ms *ms = vty->index;
1065         struct gsm_settings *set = &ms->settings;
1066         struct msgb *nmsg;
1067
1068         if (!ms->plmn.state) {
1069                 if (argv[0][0] == 'a')
1070                         set->plmn_mode = PLMN_MODE_AUTO;
1071                 else
1072                         set->plmn_mode = PLMN_MODE_MANUAL;
1073
1074                 return CMD_SUCCESS;
1075         }
1076         if (argv[0][0] == 'a')
1077                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SEL_AUTO);
1078         else
1079                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SEL_MANUAL);
1080         if (!nmsg)
1081                 return CMD_WARNING;
1082         gsm322_plmn_sendmsg(ms, nmsg);
1083
1084         return CMD_SUCCESS;
1085 }
1086
1087 DEFUN(cfg_ms_imei, cfg_ms_imei_cmd, "imei IMEI [SV]",
1088         "Set IMEI (enter without control digit)\n15 Digits IMEI\n"
1089         "Software version digit")
1090 {
1091         struct osmocom_ms *ms = vty->index;
1092         struct gsm_settings *set = &ms->settings;
1093         char *error, *sv = "0";
1094
1095         if (argc >= 2)
1096                 sv = (char *)argv[1];
1097
1098         error = gsm_check_imei(argv[0], sv);
1099         if (error) {
1100                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
1101                 return CMD_WARNING;
1102         }
1103
1104         strcpy(set->imei, argv[0]);
1105         strcpy(set->imeisv, argv[0]);
1106         strcpy(set->imeisv + 15, sv);
1107
1108         return CMD_SUCCESS;
1109 }
1110
1111 DEFUN(cfg_ms_imei_fixed, cfg_ms_imei_fixed_cmd, "imei-fixed",
1112         "Use fixed IMEI on every power on")
1113 {
1114         struct osmocom_ms *ms = vty->index;
1115         struct gsm_settings *set = &ms->settings;
1116
1117         set->imei_random = 0;
1118
1119         vty_restart(vty);
1120         return CMD_SUCCESS;
1121 }
1122
1123 DEFUN(cfg_ms_imei_random, cfg_ms_imei_random_cmd, "imei-random <0-15>",
1124         "Use random IMEI on every power on\n"
1125         "Number of trailing digits to randomize")
1126 {
1127         struct osmocom_ms *ms = vty->index;
1128         struct gsm_settings *set = &ms->settings;
1129
1130         set->imei_random = atoi(argv[0]);
1131
1132         vty_restart(vty);
1133         return CMD_SUCCESS;
1134 }
1135
1136 DEFUN(cfg_ms_emerg_imsi, cfg_ms_emerg_imsi_cmd, "emergency-imsi IMSI",
1137         "Use special IMSI for emergency calls\n15 digits IMSI")
1138 {
1139         struct osmocom_ms *ms = vty->index;
1140         struct gsm_settings *set = &ms->settings;
1141         char *error;
1142
1143         error = gsm_check_imsi(argv[0]);
1144         if (error) {
1145                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
1146                 return CMD_WARNING;
1147         }
1148         strcpy(set->emergency_imsi, argv[0]);
1149
1150         return CMD_SUCCESS;
1151 }
1152
1153 DEFUN(cfg_ms_no_emerg_imsi, cfg_ms_no_emerg_imsi_cmd, "no emergency-imsi",
1154         NO_STR "Use IMSI of SIM or IMEI for emergency calls")
1155 {
1156         struct osmocom_ms *ms = vty->index;
1157         struct gsm_settings *set = &ms->settings;
1158
1159         set->emergency_imsi[0] = '\0';
1160
1161         return CMD_SUCCESS;
1162 }
1163
1164 DEFUN(cfg_no_cw, cfg_ms_no_cw_cmd, "no call-waiting",
1165         NO_STR "Disallow waiting calls")
1166 {
1167         struct osmocom_ms *ms = vty->index;
1168         struct gsm_settings *set = &ms->settings;
1169
1170         set->cw = 0;
1171
1172         return CMD_SUCCESS;
1173 }
1174
1175 DEFUN(cfg_cw, cfg_ms_cw_cmd, "call-waiting",
1176         "Allow waiting calls")
1177 {
1178         struct osmocom_ms *ms = vty->index;
1179         struct gsm_settings *set = &ms->settings;
1180
1181         set->cw = 1;
1182
1183         return CMD_SUCCESS;
1184 }
1185
1186 DEFUN(cfg_clip, cfg_ms_clip_cmd, "clip",
1187         "Force caller ID presentation")
1188 {
1189         struct osmocom_ms *ms = vty->index;
1190         struct gsm_settings *set = &ms->settings;
1191
1192         set->clip = 1;
1193         set->clir = 0;
1194
1195         return CMD_SUCCESS;
1196 }
1197
1198 DEFUN(cfg_clir, cfg_ms_clir_cmd, "clir",
1199         "Force caller ID restriction")
1200 {
1201         struct osmocom_ms *ms = vty->index;
1202         struct gsm_settings *set = &ms->settings;
1203
1204         set->clip = 0;
1205         set->clir = 1;
1206
1207         return CMD_SUCCESS;
1208 }
1209
1210 DEFUN(cfg_no_clip, cfg_ms_no_clip_cmd, "no clip",
1211         NO_STR "Disable forcing of caller ID presentation")
1212 {
1213         struct osmocom_ms *ms = vty->index;
1214         struct gsm_settings *set = &ms->settings;
1215
1216         set->clip = 0;
1217
1218         return CMD_SUCCESS;
1219 }
1220
1221 DEFUN(cfg_no_clir, cfg_ms_no_clir_cmd, "no clir",
1222         NO_STR "Disable forcing of caller ID restriction")
1223 {
1224         struct osmocom_ms *ms = vty->index;
1225         struct gsm_settings *set = &ms->settings;
1226
1227         set->clir = 0;
1228
1229         return CMD_SUCCESS;
1230 }
1231
1232 DEFUN(cfg_ms_tx_power, cfg_ms_tx_power_cmd, "tx-power (auto|full)",
1233         "Set the way to choose transmit power\nControlled by BTS\n"
1234         "Always full power\nFixed GSM power value if supported")
1235 {
1236         struct osmocom_ms *ms = vty->index;
1237         struct gsm_settings *set = &ms->settings;
1238
1239         switch (argv[0][0]) {
1240         case 'a':
1241                 set->alter_tx_power = 0;
1242                 break;
1243         case 'f':
1244                 set->alter_tx_power = 1;
1245                 set->alter_tx_power_value = 0;
1246                 break;
1247         }
1248
1249         return CMD_SUCCESS;
1250 }
1251
1252 DEFUN(cfg_ms_tx_power_val, cfg_ms_tx_power_val_cmd, "tx-power <0-31>",
1253         "Set the way to choose transmit power\n"
1254         "Fixed GSM power value if supported")
1255 {
1256         struct osmocom_ms *ms = vty->index;
1257         struct gsm_settings *set = &ms->settings;
1258
1259         set->alter_tx_power = 1;
1260         set->alter_tx_power_value = atoi(argv[0]);
1261
1262         return CMD_SUCCESS;
1263 }
1264
1265 DEFUN(cfg_ms_sim_delay, cfg_ms_sim_delay_cmd, "simulated-delay <-128-127>",
1266         "Simulate a lower or higher distance from the BTS\n"
1267         "Delay in half bits (distance in 553.85 meter steps)")
1268 {
1269         struct osmocom_ms *ms = vty->index;
1270         struct gsm_settings *set = &ms->settings;
1271
1272         set->alter_delay = atoi(argv[0]);
1273         gsm48_rr_alter_delay(ms);
1274
1275         return CMD_SUCCESS;
1276 }
1277
1278 DEFUN(cfg_ms_no_sim_delay, cfg_ms_no_sim_delay_cmd, "no simulated-delay",
1279         NO_STR "Do not simulate a lower or higher distance from the BTS")
1280 {
1281         struct osmocom_ms *ms = vty->index;
1282         struct gsm_settings *set = &ms->settings;
1283
1284         set->alter_delay = 0;
1285         gsm48_rr_alter_delay(ms);
1286
1287         return CMD_SUCCESS;
1288 }
1289
1290 DEFUN(cfg_ms_stick, cfg_ms_stick_cmd, "stick <0-1023>",
1291         "Stick to the given cell\nARFCN of the cell to stick to")
1292 {
1293         struct osmocom_ms *ms = vty->index;
1294         struct gsm_settings *set = &ms->settings;
1295
1296         set->stick = 1;
1297         set->stick_arfcn = atoi(argv[0]);
1298
1299         return CMD_SUCCESS;
1300 }
1301
1302 DEFUN(cfg_ms_no_stick, cfg_ms_no_stick_cmd, "no stick",
1303         NO_STR "Do not stick to any cell")
1304 {
1305         struct osmocom_ms *ms = vty->index;
1306         struct gsm_settings *set = &ms->settings;
1307
1308         set->stick = 0;
1309
1310         return CMD_SUCCESS;
1311 }
1312
1313 DEFUN(cfg_ms_lupd, cfg_ms_lupd_cmd, "location-updating",
1314         "Allow location updating")
1315 {
1316         struct osmocom_ms *ms = vty->index;
1317         struct gsm_settings *set = &ms->settings;
1318
1319         set->no_lupd = 0;
1320
1321         return CMD_SUCCESS;
1322 }
1323
1324 DEFUN(cfg_ms_no_lupd, cfg_ms_no_lupd_cmd, "no location-updating",
1325         NO_STR "Do not allow location updating")
1326 {
1327         struct osmocom_ms *ms = vty->index;
1328         struct gsm_settings *set = &ms->settings;
1329
1330         set->no_lupd = 1;
1331
1332         return CMD_SUCCESS;
1333 }
1334
1335 DEFUN(cfg_codec_full, cfg_ms_codec_full_cmd, "codec full-speed",
1336         "Enable codec\nFull speed speech codec")
1337 {
1338         struct osmocom_ms *ms = vty->index;
1339         struct gsm_settings *set = &ms->settings;
1340
1341         if (!set->full_v1 && !set->full_v2 && !set->full_v3) {
1342                 vty_out(vty, "Full-rate codec not supported%s", VTY_NEWLINE);
1343                 return CMD_WARNING;
1344         }
1345
1346         return CMD_SUCCESS;
1347 }
1348
1349 DEFUN(cfg_codec_full_pref, cfg_ms_codec_full_pref_cmd, "codec full-speed "
1350         "prefer",
1351         "Enable codec\nFull speed speech codec\nPrefer this codec")
1352 {
1353         struct osmocom_ms *ms = vty->index;
1354         struct gsm_settings *set = &ms->settings;
1355
1356         if (!set->full_v1 && !set->full_v2 && !set->full_v3) {
1357                 vty_out(vty, "Full-rate codec not supported%s", VTY_NEWLINE);
1358                 return CMD_WARNING;
1359         }
1360
1361         set->half_prefer = 0;
1362
1363         return CMD_SUCCESS;
1364 }
1365
1366 DEFUN(cfg_codec_half, cfg_ms_codec_half_cmd, "codec half-speed",
1367         "Enable codec\nHalf speed speech codec")
1368 {
1369         struct osmocom_ms *ms = vty->index;
1370         struct gsm_settings *set = &ms->settings;
1371
1372         if (!set->half_v1 && !set->half_v3) {
1373                 vty_out(vty, "Half-rate codec not supported%s", VTY_NEWLINE);
1374                 return CMD_WARNING;
1375         }
1376
1377         set->half = 1;
1378
1379         return CMD_SUCCESS;
1380 }
1381
1382 DEFUN(cfg_codec_half_pref, cfg_ms_codec_half_pref_cmd, "codec half-speed "
1383         "prefer",
1384         "Enable codec\nHalf speed speech codec\nPrefer this codec")
1385 {
1386         struct osmocom_ms *ms = vty->index;
1387         struct gsm_settings *set = &ms->settings;
1388
1389         if (!set->half_v1 && !set->half_v3) {
1390                 vty_out(vty, "Half-rate codec not supported%s", VTY_NEWLINE);
1391                 return CMD_WARNING;
1392         }
1393
1394         set->half = 1;
1395         set->half_prefer = 1;
1396
1397         return CMD_SUCCESS;
1398 }
1399
1400 DEFUN(cfg_no_codec_half, cfg_ms_no_codec_half_cmd, "no codec half-speed",
1401         NO_STR "Disable codec\nHalf speed speech codec")
1402 {
1403         struct osmocom_ms *ms = vty->index;
1404         struct gsm_settings *set = &ms->settings;
1405
1406         if (!set->half_v1 && !set->half_v3) {
1407                 vty_out(vty, "Half-rate codec not supported%s", VTY_NEWLINE);
1408                 return CMD_WARNING;
1409         }
1410
1411         set->half = 0;
1412         set->half_prefer = 0;
1413
1414         return CMD_SUCCESS;
1415 }
1416
1417 DEFUN(cfg_abbrev, cfg_ms_abbrev_cmd, "abbrev ABBREVIATION NUMBER [name]",
1418         "Store given abbreviation number\n1-3 digits abbreviation\n"
1419         "Number to store for the abbreviation "
1420         "(Use digits '0123456789*#abc', and '+' to dial international)\n"
1421         "Name of the abbreviation")
1422 {
1423         struct osmocom_ms *ms = vty->index;
1424         struct gsm_settings *set = &ms->settings;
1425         struct gsm_settings_abbrev *abbrev;
1426         int i;
1427
1428         llist_for_each_entry(abbrev, &set->abbrev, list) {
1429                 if (!strcmp(argv[0], abbrev->abbrev)) {
1430                         vty_out(vty, "Given abbreviation '%s' already stored, "
1431                                 "delete first!%s", argv[0], VTY_NEWLINE);
1432                         return CMD_WARNING;
1433                 }
1434         }
1435
1436         if (strlen(argv[0]) >= sizeof(abbrev->abbrev)) {
1437                 vty_out(vty, "Given abbreviation too long%s", VTY_NEWLINE);
1438                 return CMD_WARNING;
1439         }
1440
1441         for (i = 0; i < strlen(argv[0]); i++) {
1442                 if (argv[0][i] < '0' || argv[0][i] > '9') {
1443                         vty_out(vty, "Given abbreviation must have digits "
1444                                 "0..9 only!%s", VTY_NEWLINE);
1445                         return CMD_WARNING;
1446                 }
1447         }
1448
1449         if (vty_check_number(vty, argv[1]))
1450                 return CMD_WARNING;
1451
1452         abbrev = talloc_zero(l23_ctx, struct gsm_settings_abbrev);
1453         if (!abbrev) {
1454                 vty_out(vty, "No Memory!%s", VTY_NEWLINE);
1455                 return CMD_WARNING;
1456         }
1457         llist_add_tail(&abbrev->list, &set->abbrev);
1458         strncpy(abbrev->abbrev, argv[0], sizeof(abbrev->abbrev) - 1);
1459         strncpy(abbrev->number, argv[1], sizeof(abbrev->number) - 1);
1460         if (argc >= 3)
1461                 strncpy(abbrev->name, argv[2], sizeof(abbrev->name) - 1);
1462
1463         return CMD_SUCCESS;
1464 }
1465
1466 DEFUN(cfg_no_abbrev, cfg_ms_no_abbrev_cmd, "no abbrev [abbreviation]",
1467         NO_STR "Remove given abbreviation number or all numbers\n"
1468         "Abbreviation number to remove")
1469 {
1470         struct osmocom_ms *ms = vty->index;
1471         struct gsm_settings *set = &ms->settings;
1472         struct gsm_settings_abbrev *abbrev, *abbrev2;
1473         uint8_t deleted = 0;
1474
1475         llist_for_each_entry_safe(abbrev, abbrev2, &set->abbrev, list) {
1476                 if (argc < 1 || !strcmp(argv[0], abbrev->abbrev)) {
1477                         llist_del(&abbrev->list);
1478                         deleted = 1;
1479                 }
1480         }
1481
1482         if (argc >= 1 && !deleted) {
1483                 vty_out(vty, "Given abbreviation '%s' not found!%s",
1484                         argv[0], VTY_NEWLINE);
1485                 return CMD_WARNING;
1486         }
1487
1488         return CMD_SUCCESS;
1489 }
1490
1491 static int config_write_dummy(struct vty *vty)
1492 {
1493         return CMD_SUCCESS;
1494 }
1495
1496 /* per support config */
1497 DEFUN(cfg_ms_support, cfg_ms_support_cmd, "support",
1498         "Define supported features")
1499 {
1500         vty->node = SUPPORT_NODE;
1501
1502         return CMD_SUCCESS;
1503 }
1504
1505 #define SUP_EN(cfg, cfg_cmd, item, cmd, desc, restart) \
1506 DEFUN(cfg, cfg_cmd, cmd, "Enable " desc "support") \
1507 { \
1508         struct osmocom_ms *ms = vty->index; \
1509         struct gsm_settings *set = &ms->settings; \
1510         struct gsm_support *sup = &ms->support; \
1511         if (!sup->item) { \
1512                 vty_out(vty, desc " not supported%s", VTY_NEWLINE); \
1513                 if (vty_reading) \
1514                         return CMD_SUCCESS; \
1515                 return CMD_WARNING; \
1516         } \
1517         if (restart) \
1518                 vty_restart(vty); \
1519         set->item = 1; \
1520         return CMD_SUCCESS; \
1521 }
1522
1523 #define SUP_DI(cfg, cfg_cmd, item, cmd, desc, restart) \
1524 DEFUN(cfg, cfg_cmd, "no " cmd, NO_STR "Disable " desc " support") \
1525 { \
1526         struct osmocom_ms *ms = vty->index; \
1527         struct gsm_settings *set = &ms->settings; \
1528         struct gsm_support *sup = &ms->support; \
1529         if (!sup->item) { \
1530                 vty_out(vty, desc " not supported%s", VTY_NEWLINE); \
1531                 if (vty_reading) \
1532                         return CMD_SUCCESS; \
1533                 return CMD_WARNING; \
1534         } \
1535         if (restart) \
1536                 vty_restart(vty); \
1537         set->item = 0; \
1538         return CMD_SUCCESS; \
1539 }
1540
1541 #define SET_EN(cfg, cfg_cmd, item, cmd, desc, restart) \
1542 DEFUN(cfg, cfg_cmd, cmd, "Enable " desc "support") \
1543 { \
1544         struct osmocom_ms *ms = vty->index; \
1545         struct gsm_settings *set = &ms->settings; \
1546         if (restart) \
1547                 vty_restart(vty); \
1548         set->item = 1; \
1549         return CMD_SUCCESS; \
1550 }
1551
1552 #define SET_DI(cfg, cfg_cmd, item, cmd, desc, restart) \
1553 DEFUN(cfg, cfg_cmd, "no " cmd, NO_STR "Disable " desc " support") \
1554 { \
1555         struct osmocom_ms *ms = vty->index; \
1556         struct gsm_settings *set = &ms->settings; \
1557         if (restart) \
1558                 vty_restart(vty); \
1559         set->item = 0; \
1560         return CMD_SUCCESS; \
1561 }
1562
1563 SET_EN(cfg_ms_sup_dtmf, cfg_ms_sup_dtmf_cmd, cc_dtmf, "dtmf", "DTMF", 0);
1564 SET_DI(cfg_ms_sup_no_dtmf, cfg_ms_sup_no_dtmf_cmd, cc_dtmf, "dtmf", "DTMF", 0);
1565 SUP_EN(cfg_ms_sup_sms, cfg_ms_sup_sms_cmd, sms_ptp, "sms", "SMS", 0);
1566 SUP_DI(cfg_ms_sup_no_sms, cfg_ms_sup_no_sms_cmd, sms_ptp, "sms", "SMS", 0);
1567 SUP_EN(cfg_ms_sup_a5_1, cfg_ms_sup_a5_1_cmd, a5_1, "a5/1", "A5/1", 0);
1568 SUP_DI(cfg_ms_sup_no_a5_1, cfg_ms_sup_no_a5_1_cmd, a5_1, "a5/1", "A5/1", 0);
1569 SUP_EN(cfg_ms_sup_a5_2, cfg_ms_sup_a5_2_cmd, a5_2, "a5/2", "A5/2", 0);
1570 SUP_DI(cfg_ms_sup_no_a5_2, cfg_ms_sup_no_a5_2_cmd, a5_2, "a5/2", "A5/2", 0);
1571 SUP_EN(cfg_ms_sup_a5_3, cfg_ms_sup_a5_3_cmd, a5_3, "a5/3", "A5/3", 0);
1572 SUP_DI(cfg_ms_sup_no_a5_3, cfg_ms_sup_no_a5_3_cmd, a5_3, "a5/3", "A5/3", 0);
1573 SUP_EN(cfg_ms_sup_a5_4, cfg_ms_sup_a5_4_cmd, a5_4, "a5/4", "A5/4", 0);
1574 SUP_DI(cfg_ms_sup_no_a5_4, cfg_ms_sup_no_a5_4_cmd, a5_4, "a5/4", "A5/4", 0);
1575 SUP_EN(cfg_ms_sup_a5_5, cfg_ms_sup_a5_5_cmd, a5_5, "a5/5", "A5/5", 0);
1576 SUP_DI(cfg_ms_sup_no_a5_5, cfg_ms_sup_no_a5_5_cmd, a5_5, "a5/5", "A5/5", 0);
1577 SUP_EN(cfg_ms_sup_a5_6, cfg_ms_sup_a5_6_cmd, a5_6, "a5/6", "A5/6", 0);
1578 SUP_DI(cfg_ms_sup_no_a5_6, cfg_ms_sup_no_a5_6_cmd, a5_6, "a5/6", "A5/6", 0);
1579 SUP_EN(cfg_ms_sup_a5_7, cfg_ms_sup_a5_7_cmd, a5_7, "a5/7", "A5/7", 0);
1580 SUP_DI(cfg_ms_sup_no_a5_7, cfg_ms_sup_no_a5_7_cmd, a5_7, "a5/7", "A5/7", 1);
1581 SUP_EN(cfg_ms_sup_p_gsm, cfg_ms_sup_p_gsm_cmd, p_gsm, "p-gsm", "P-GSM (900)",
1582         1);
1583 SUP_DI(cfg_ms_sup_no_p_gsm, cfg_ms_sup_no_p_gsm_cmd, p_gsm, "p-gsm",
1584         "P-GSM (900)", 1);
1585 SUP_EN(cfg_ms_sup_e_gsm, cfg_ms_sup_e_gsm_cmd, e_gsm, "e-gsm", "E-GSM (850)",
1586         1);
1587 SUP_DI(cfg_ms_sup_no_e_gsm, cfg_ms_sup_no_e_gsm_cmd, e_gsm, "e-gsm",
1588         "E-GSM (850)", 1);
1589 SUP_EN(cfg_ms_sup_r_gsm, cfg_ms_sup_r_gsm_cmd, r_gsm, "r-gsm", "R-GSM (850)",
1590         1);
1591 SUP_DI(cfg_ms_sup_no_r_gsm, cfg_ms_sup_no_r_gsm_cmd, r_gsm, "r-gsm",
1592         "R-GSM (850)", 1);
1593 SUP_EN(cfg_ms_sup_dcs, cfg_ms_sup_dcs_cmd, dcs, "dcs", "DCS (1800)", 0);
1594 SUP_DI(cfg_ms_sup_no_dcs, cfg_ms_sup_no_dcs_cmd, dcs, "dcs", "DCS (1800)", 0);
1595
1596 DEFUN(cfg_ms_sup_class_900, cfg_ms_sup_class_900_cmd, "class-900 (1|2|3|4|5)",
1597         "Select power class for GSM 850/900\n"
1598         "20 Watts\n"
1599         "8 Watts\n"
1600         "5 Watts\n"
1601         "2 Watts\n"
1602         "0.8 Watts")
1603 {
1604         struct osmocom_ms *ms = vty->index;
1605         struct gsm_settings *set = &ms->settings;
1606         struct gsm_support *sup = &ms->support;
1607
1608         set->class_900 = atoi(argv[0]);
1609
1610         if (set->class_900 < sup->class_900 && !vty_reading)
1611                 vty_out(vty, "You selected an higher class than supported "
1612                         " by hardware!%s", VTY_NEWLINE);
1613
1614         return CMD_SUCCESS;
1615 }
1616
1617 DEFUN(cfg_ms_sup_class_dcs, cfg_ms_sup_class_dcs_cmd, "class-dcs (1|2|3)",
1618         "Select power class for DCS 1800\n"
1619         "1 Watt\n"
1620         "0.25 Watts\n"
1621         "4 Watts")
1622 {
1623         struct osmocom_ms *ms = vty->index;
1624         struct gsm_settings *set = &ms->settings;
1625         struct gsm_support *sup = &ms->support;
1626
1627         set->class_dcs = atoi(argv[0]);
1628
1629         if (((set->class_dcs + 1) & 3) < ((sup->class_dcs + 1) & 3)
1630          && !vty_reading)
1631                 vty_out(vty, "You selected an higher class than supported "
1632                         " by hardware!%s", VTY_NEWLINE);
1633
1634         return CMD_SUCCESS;
1635 }
1636
1637 DEFUN(cfg_ms_sup_ch_cap, cfg_ms_sup_ch_cap_cmd, "channel-capability "
1638         "(sdcch|sdcch+tchf|sdcch+tchf+tchh)",
1639         "Select channel capability\nSDCCH only\nSDCCH + TCH/F\nSDCCH + TCH/H")
1640 {
1641         struct osmocom_ms *ms = vty->index;
1642         struct gsm_settings *set = &ms->settings;
1643         struct gsm_support *sup = &ms->support;
1644         uint8_t ch_cap;
1645
1646         if (!strcmp(argv[0], "sdcch+tchf+tchh"))
1647                 ch_cap = GSM_CAP_SDCCH_TCHF_TCHH;
1648         else if (!strcmp(argv[0], "sdcch+tchf"))
1649                 ch_cap = GSM_CAP_SDCCH_TCHF;
1650         else
1651                 ch_cap = GSM_CAP_SDCCH;
1652
1653         if (ch_cap > sup->ch_cap && !vty_reading) {
1654                 vty_out(vty, "You selected an higher capability than supported "
1655                         " by hardware!%s", VTY_NEWLINE);
1656                 return CMD_WARNING;
1657         }
1658
1659         if (ch_cap != set->ch_cap
1660          && (ch_cap == GSM_CAP_SDCCH || set->ch_cap == GSM_CAP_SDCCH))
1661                 vty_restart(vty);
1662
1663         set->ch_cap = ch_cap;
1664
1665         return CMD_SUCCESS;
1666 }
1667
1668 SUP_EN(cfg_ms_sup_full_v1, cfg_ms_sup_full_v1_cmd, full_v1, "full-speech-v1",
1669         "Full rate speech V1", 0);
1670 SUP_DI(cfg_ms_sup_no_full_v1, cfg_ms_sup_no_full_v1_cmd, full_v1,
1671         "full-speech-v1", "Full rate speech V1", 0);
1672 SUP_EN(cfg_ms_sup_full_v2, cfg_ms_sup_full_v2_cmd, full_v2, "full-speech-v2",
1673         "Full rate speech V2 (EFR)", 0);
1674 SUP_DI(cfg_ms_sup_no_full_v2, cfg_ms_sup_no_full_v2_cmd, full_v2,
1675         "full-speech-v2", "Full rate speech V2 (EFR)", 0);
1676 SUP_EN(cfg_ms_sup_full_v3, cfg_ms_sup_full_v3_cmd, full_v3, "full-speech-v3",
1677         "Full rate speech V3 (AMR)", 0);
1678 SUP_DI(cfg_ms_sup_no_full_v3, cfg_ms_sup_no_full_v3_cmd, full_v3,
1679         "full-speech-v3", "Full rate speech V3 (AMR)", 0);
1680 SUP_EN(cfg_ms_sup_half_v1, cfg_ms_sup_half_v1_cmd, half_v1, "half-speech-v1",
1681         "Half rate speech V1 (AMR)", 0);
1682 SUP_DI(cfg_ms_sup_no_half_v1, cfg_ms_sup_no_half_v1_cmd, half_v1,
1683         "half-speech-v1", "Half rate speech V1", 0);
1684 SUP_EN(cfg_ms_sup_half_v3, cfg_ms_sup_half_v3_cmd, half_v3, "half-speech-v3",
1685         "Half rate speech V3 (AMR)", 0);
1686 SUP_DI(cfg_ms_sup_no_half_v3, cfg_ms_sup_no_half_v3_cmd, half_v3,
1687         "half-speech-v3", "Half rate speech V3", 0);
1688
1689 DEFUN(cfg_ms_sup_min_rxlev, cfg_ms_sup_min_rxlev_cmd, "min-rxlev <-110--47>",
1690         "Set the minimum receive level to select a cell\n"
1691         "Minimum receive level from -110 dBm to -47 dBm")
1692 {
1693         struct osmocom_ms *ms = vty->index;
1694         struct gsm_settings *set = &ms->settings;
1695
1696         set->min_rxlev_db = atoi(argv[0]);
1697
1698         return CMD_SUCCESS;
1699 }
1700
1701 DEFUN(cfg_ms_sup_dsc_max, cfg_ms_sup_dsc_max_cmd, "dsc-max <90-500>",
1702         "Set the maximum DSC value. Standard is 90. Increase to make mobile "
1703         "more reliable against bad RX signal. This increase the propability "
1704         "of missing a paging requests\n"
1705         "DSC initial and maximum value (standard is 90)")
1706 {
1707         struct osmocom_ms *ms = vty->index;
1708         struct gsm_settings *set = &ms->settings;
1709
1710         set->dsc_max = atoi(argv[0]);
1711
1712         return CMD_SUCCESS;
1713 }
1714
1715 /* per testsim config */
1716 DEFUN(cfg_ms_testsim, cfg_ms_testsim_cmd, "test-sim",
1717         "Configure test SIM emulation")
1718 {
1719         vty->node = TESTSIM_NODE;
1720
1721         return CMD_SUCCESS;
1722 }
1723
1724 DEFUN(cfg_test_imsi, cfg_test_imsi_cmd, "imsi IMSI",
1725         "Set IMSI on test card\n15 digits IMSI")
1726 {
1727         struct osmocom_ms *ms = vty->index;
1728         struct gsm_settings *set = &ms->settings;
1729         char *error = gsm_check_imsi(argv[0]);
1730
1731         if (error) {
1732                 vty_out(vty, "%s%s", error, VTY_NEWLINE);
1733                 return CMD_WARNING;
1734         }
1735
1736         strcpy(set->test_imsi, argv[0]);
1737
1738         vty_restart(vty);
1739         return CMD_SUCCESS;
1740 }
1741
1742 #define HEX_STR "\nByte as two digits hexadecimal"
1743 DEFUN(cfg_test_ki_xor, cfg_test_ki_xor_cmd, "ki xor HEX HEX HEX HEX HEX HEX "
1744         "HEX HEX HEX HEX HEX HEX",
1745         "Set Key (Kc) on test card\nUse XOR algorithm" HEX_STR HEX_STR HEX_STR
1746         HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR)
1747 {
1748         struct osmocom_ms *ms = vty->index;
1749         struct gsm_settings *set = &ms->settings;
1750         uint8_t ki[12];
1751         const char *p;
1752         int i;
1753
1754         for (i = 0; i < 12; i++) {
1755                 p = argv[i];
1756                 if (!strncmp(p, "0x", 2))
1757                         p += 2;
1758                 if (strlen(p) != 2) {
1759                         vty_out(vty, "Expecting two digits hex value (with or "
1760                                 "without 0x in front)%s", VTY_NEWLINE);
1761                         return CMD_WARNING;
1762                 }
1763                 ki[i] = strtoul(p, NULL, 16);
1764         }
1765
1766         set->test_ki_type = GSM_SIM_KEY_XOR;
1767         memcpy(set->test_ki, ki, 12);
1768         return CMD_SUCCESS;
1769 }
1770
1771 DEFUN(cfg_test_ki_comp128, cfg_test_ki_comp128_cmd, "ki comp128 HEX HEX HEX "
1772         "HEX HEX HEX HEX HEX HEX HEX HEX HEX HEX HEX HEX HEX",
1773         "Set Key (Kc) on test card\nUse XOR algorithm" HEX_STR HEX_STR HEX_STR
1774         HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR HEX_STR
1775         HEX_STR HEX_STR HEX_STR HEX_STR)
1776 {
1777         struct osmocom_ms *ms = vty->index;
1778         struct gsm_settings *set = &ms->settings;
1779         uint8_t ki[16];
1780         const char *p;
1781         int i;
1782
1783         for (i = 0; i < 16; i++) {
1784                 p = argv[i];
1785                 if (!strncmp(p, "0x", 2))
1786                         p += 2;
1787                 if (strlen(p) != 2) {
1788                         vty_out(vty, "Expecting two digits hex value (with or "
1789                                 "without 0x in front)%s", VTY_NEWLINE);
1790                         return CMD_WARNING;
1791                 }
1792                 ki[i] = strtoul(p, NULL, 16);
1793         }
1794
1795         set->test_ki_type = GSM_SIM_KEY_COMP128;
1796         memcpy(set->test_ki, ki, 16);
1797         return CMD_SUCCESS;
1798 }
1799
1800 DEFUN(cfg_test_barr, cfg_test_barr_cmd, "barred-access",
1801         "Allow access to barred cells")
1802 {
1803         struct osmocom_ms *ms = vty->index;
1804         struct gsm_settings *set = &ms->settings;
1805
1806         set->test_barr = 1;
1807
1808         return CMD_SUCCESS;
1809 }
1810
1811 DEFUN(cfg_test_no_barr, cfg_test_no_barr_cmd, "no barred-access",
1812         NO_STR "Deny access to barred cells")
1813 {
1814         struct osmocom_ms *ms = vty->index;
1815         struct gsm_settings *set = &ms->settings;
1816
1817         set->test_barr = 0;
1818
1819         return CMD_SUCCESS;
1820 }
1821
1822 DEFUN(cfg_test_no_rplmn, cfg_test_no_rplmn_cmd, "no rplmn",
1823         NO_STR "Unset Registered PLMN")
1824 {
1825         struct osmocom_ms *ms = vty->index;
1826         struct gsm_settings *set = &ms->settings;
1827
1828         set->test_rplmn_valid = 0;
1829
1830         vty_restart(vty);
1831         return CMD_SUCCESS;
1832 }
1833
1834 DEFUN(cfg_test_rplmn, cfg_test_rplmn_cmd, "rplmn MCC MNC",
1835         "Set Registered PLMN\nMobile Country Code\nMobile Network Code")
1836 {
1837         struct osmocom_ms *ms = vty->index;
1838         struct gsm_settings *set = &ms->settings;
1839         uint16_t mcc = gsm_input_mcc((char *)argv[0]),
1840                  mnc = gsm_input_mnc((char *)argv[1]);
1841
1842         if (!mcc) {
1843                 vty_out(vty, "Given MCC invalid%s", VTY_NEWLINE);
1844                 return CMD_WARNING;
1845         }
1846         if (!mnc) {
1847                 vty_out(vty, "Given MNC invalid%s", VTY_NEWLINE);
1848                 return CMD_WARNING;
1849         }
1850         set->test_rplmn_valid = 1;
1851         set->test_rplmn_mcc = mcc;
1852         set->test_rplmn_mnc = mnc;
1853
1854         vty_restart(vty);
1855         return CMD_SUCCESS;
1856 }
1857
1858 DEFUN(cfg_test_hplmn, cfg_test_hplmn_cmd, "hplmn-search (everywhere|foreign-country)",
1859         "Set Home PLMN search mode\n"
1860         "Search for HPLMN when on any other network\n"
1861         "Search for HPLMN when in a different country")
1862 {
1863         struct osmocom_ms *ms = vty->index;
1864         struct gsm_settings *set = &ms->settings;
1865
1866         switch (argv[0][0]) {
1867         case 'e':
1868                 set->test_always = 1;
1869                 break;
1870         case 'f':
1871                 set->test_always = 0;
1872                 break;
1873         }
1874
1875         vty_restart(vty);
1876         return CMD_SUCCESS;
1877 }
1878
1879 enum node_type ms_vty_go_parent(struct vty *vty)
1880 {
1881         switch (vty->node) {
1882         case MS_NODE:
1883                 vty->node = CONFIG_NODE;
1884                 vty->index = NULL;
1885                 break;
1886         case TESTSIM_NODE:
1887         case SUPPORT_NODE:
1888                 vty->node = MS_NODE;
1889                 break;
1890         default:
1891                 vty->node = CONFIG_NODE;
1892         }
1893
1894         return vty->node;
1895 }
1896
1897 /* Down vty node level. */
1898 gDEFUN(ournode_exit,
1899        ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
1900 {
1901         switch (vty->node) {
1902         case MS_NODE:
1903                 vty->node = CONFIG_NODE;
1904                 vty->index = NULL;
1905                 break;
1906         case TESTSIM_NODE:
1907         case SUPPORT_NODE:
1908                 vty->node = MS_NODE;
1909                 break;
1910         default:
1911                 break;
1912         }
1913         return CMD_SUCCESS;
1914 }
1915
1916 /* End of configuration. */
1917 gDEFUN(ournode_end,
1918        ournode_end_cmd, "end", "End current mode and change to enable mode.")
1919 {
1920         switch (vty->node) {
1921         case VIEW_NODE:
1922         case ENABLE_NODE:
1923                 /* Nothing to do. */
1924                 break;
1925         case CONFIG_NODE:
1926         case VTY_NODE:
1927         case MS_NODE:
1928         case TESTSIM_NODE:
1929         case SUPPORT_NODE:
1930                 vty_config_unlock(vty);
1931                 vty->node = ENABLE_NODE;
1932                 vty->index = NULL;
1933                 vty->index_sub = NULL;
1934                 break;
1935         default:
1936                 break;
1937         }
1938         return CMD_SUCCESS;
1939 }
1940
1941 #define SUP_NODE(item) \
1942         install_element(SUPPORT_NODE, &cfg_ms_sup_item_cmd);
1943
1944 int ms_vty_init(void)
1945 {
1946         install_element_ve(&show_ms_cmd);
1947         install_element_ve(&show_subscr_cmd);
1948         install_element_ve(&show_support_cmd);
1949         install_element_ve(&show_states_cmd);
1950         install_element_ve(&show_cell_cmd);
1951         install_element_ve(&show_cell_si_cmd);
1952         install_element_ve(&show_ba_cmd);
1953         install_element_ve(&show_forb_la_cmd);
1954         install_element_ve(&show_forb_plmn_cmd);
1955         install_element_ve(&monitor_network_cmd);
1956         install_element_ve(&no_monitor_network_cmd);
1957
1958         install_element(ENABLE_NODE, &sim_test_cmd);
1959         install_element(ENABLE_NODE, &sim_reader_cmd);
1960         install_element(ENABLE_NODE, &sim_remove_cmd);
1961         install_element(ENABLE_NODE, &sim_pin_cmd);
1962         install_element(ENABLE_NODE, &sim_disable_pin_cmd);
1963         install_element(ENABLE_NODE, &sim_enable_pin_cmd);
1964         install_element(ENABLE_NODE, &sim_change_pin_cmd);
1965         install_element(ENABLE_NODE, &sim_unblock_pin_cmd);
1966         install_element(ENABLE_NODE, &sim_lai_cmd);
1967         install_element(ENABLE_NODE, &network_search_cmd);
1968         install_element(ENABLE_NODE, &network_show_cmd);
1969         install_element(ENABLE_NODE, &network_select_cmd);
1970         install_element(ENABLE_NODE, &call_cmd);
1971         install_element(ENABLE_NODE, &call_retr_cmd);
1972         install_element(ENABLE_NODE, &call_dtmf_cmd);
1973
1974         install_element(CONFIG_NODE, &cfg_gps_device_cmd);
1975         install_element(CONFIG_NODE, &cfg_gps_baud_cmd);
1976         install_element(CONFIG_NODE, &cfg_gps_enable_cmd);
1977         install_element(CONFIG_NODE, &cfg_no_gps_enable_cmd);
1978
1979         install_element(CONFIG_NODE, &cfg_ms_cmd);
1980         install_element(CONFIG_NODE, &ournode_end_cmd);
1981         install_node(&ms_node, config_write_ms);
1982         install_default(MS_NODE);
1983         install_element(MS_NODE, &ournode_exit_cmd);
1984         install_element(MS_NODE, &ournode_end_cmd);
1985         install_element(MS_NODE, &cfg_ms_sim_cmd);
1986         install_element(MS_NODE, &cfg_ms_mode_cmd);
1987         install_element(MS_NODE, &cfg_ms_imei_cmd);
1988         install_element(MS_NODE, &cfg_ms_imei_fixed_cmd);
1989         install_element(MS_NODE, &cfg_ms_imei_random_cmd);
1990         install_element(MS_NODE, &cfg_ms_no_emerg_imsi_cmd);
1991         install_element(MS_NODE, &cfg_ms_emerg_imsi_cmd);
1992         install_element(MS_NODE, &cfg_ms_cw_cmd);
1993         install_element(MS_NODE, &cfg_ms_no_cw_cmd);
1994         install_element(MS_NODE, &cfg_ms_clip_cmd);
1995         install_element(MS_NODE, &cfg_ms_clir_cmd);
1996         install_element(MS_NODE, &cfg_ms_no_clip_cmd);
1997         install_element(MS_NODE, &cfg_ms_no_clir_cmd);
1998         install_element(MS_NODE, &cfg_ms_tx_power_cmd);
1999         install_element(MS_NODE, &cfg_ms_tx_power_val_cmd);
2000         install_element(MS_NODE, &cfg_ms_sim_delay_cmd);
2001         install_element(MS_NODE, &cfg_ms_no_sim_delay_cmd);
2002         install_element(MS_NODE, &cfg_ms_stick_cmd);
2003         install_element(MS_NODE, &cfg_ms_no_stick_cmd);
2004         install_element(MS_NODE, &cfg_ms_lupd_cmd);
2005         install_element(MS_NODE, &cfg_ms_no_lupd_cmd);
2006         install_element(MS_NODE, &cfg_ms_codec_full_cmd);
2007         install_element(MS_NODE, &cfg_ms_codec_full_pref_cmd);
2008         install_element(MS_NODE, &cfg_ms_codec_half_cmd);
2009         install_element(MS_NODE, &cfg_ms_codec_half_pref_cmd);
2010         install_element(MS_NODE, &cfg_ms_no_codec_half_cmd);
2011         install_element(MS_NODE, &cfg_ms_abbrev_cmd);
2012         install_element(MS_NODE, &cfg_ms_no_abbrev_cmd);
2013         install_element(MS_NODE, &cfg_ms_testsim_cmd);
2014         install_element(MS_NODE, &cfg_ms_support_cmd);
2015         install_node(&support_node, config_write_dummy);
2016         install_default(SUPPORT_NODE);
2017         install_element(SUPPORT_NODE, &ournode_exit_cmd);
2018         install_element(SUPPORT_NODE, &ournode_end_cmd);
2019         install_element(SUPPORT_NODE, &cfg_ms_sup_dtmf_cmd);
2020         install_element(SUPPORT_NODE, &cfg_ms_sup_no_dtmf_cmd);
2021         install_element(SUPPORT_NODE, &cfg_ms_sup_sms_cmd);
2022         install_element(SUPPORT_NODE, &cfg_ms_sup_no_sms_cmd);
2023         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_1_cmd);
2024         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_1_cmd);
2025         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_2_cmd);
2026         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_2_cmd);
2027         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_3_cmd);
2028         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_3_cmd);
2029         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_4_cmd);
2030         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_4_cmd);
2031         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_5_cmd);
2032         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_5_cmd);
2033         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_6_cmd);
2034         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_6_cmd);
2035         install_element(SUPPORT_NODE, &cfg_ms_sup_a5_7_cmd);
2036         install_element(SUPPORT_NODE, &cfg_ms_sup_no_a5_7_cmd);
2037         install_element(SUPPORT_NODE, &cfg_ms_sup_p_gsm_cmd);
2038         install_element(SUPPORT_NODE, &cfg_ms_sup_no_p_gsm_cmd);
2039         install_element(SUPPORT_NODE, &cfg_ms_sup_e_gsm_cmd);
2040         install_element(SUPPORT_NODE, &cfg_ms_sup_no_e_gsm_cmd);
2041         install_element(SUPPORT_NODE, &cfg_ms_sup_r_gsm_cmd);
2042         install_element(SUPPORT_NODE, &cfg_ms_sup_no_r_gsm_cmd);
2043         install_element(SUPPORT_NODE, &cfg_ms_sup_dcs_cmd);
2044         install_element(SUPPORT_NODE, &cfg_ms_sup_no_dcs_cmd);
2045         install_element(SUPPORT_NODE, &cfg_ms_sup_class_900_cmd);
2046         install_element(SUPPORT_NODE, &cfg_ms_sup_class_dcs_cmd);
2047         install_element(SUPPORT_NODE, &cfg_ms_sup_ch_cap_cmd);
2048         install_element(SUPPORT_NODE, &cfg_ms_sup_full_v1_cmd);
2049         install_element(SUPPORT_NODE, &cfg_ms_sup_no_full_v1_cmd);
2050         install_element(SUPPORT_NODE, &cfg_ms_sup_full_v2_cmd);
2051         install_element(SUPPORT_NODE, &cfg_ms_sup_no_full_v2_cmd);
2052         install_element(SUPPORT_NODE, &cfg_ms_sup_full_v3_cmd);
2053         install_element(SUPPORT_NODE, &cfg_ms_sup_no_full_v3_cmd);
2054         install_element(SUPPORT_NODE, &cfg_ms_sup_half_v1_cmd);
2055         install_element(SUPPORT_NODE, &cfg_ms_sup_no_half_v1_cmd);
2056         install_element(SUPPORT_NODE, &cfg_ms_sup_half_v3_cmd);
2057         install_element(SUPPORT_NODE, &cfg_ms_sup_no_half_v3_cmd);
2058         install_element(SUPPORT_NODE, &cfg_ms_sup_min_rxlev_cmd);
2059         install_element(SUPPORT_NODE, &cfg_ms_sup_dsc_max_cmd);
2060         install_node(&testsim_node, config_write_dummy);
2061         install_default(TESTSIM_NODE);
2062         install_element(TESTSIM_NODE, &ournode_exit_cmd);
2063         install_element(TESTSIM_NODE, &ournode_end_cmd);
2064         install_element(TESTSIM_NODE, &cfg_test_imsi_cmd);
2065         install_element(TESTSIM_NODE, &cfg_test_ki_xor_cmd);
2066         install_element(TESTSIM_NODE, &cfg_test_ki_comp128_cmd);
2067         install_element(TESTSIM_NODE, &cfg_test_barr_cmd);
2068         install_element(TESTSIM_NODE, &cfg_test_no_barr_cmd);
2069         install_element(TESTSIM_NODE, &cfg_test_no_rplmn_cmd);
2070         install_element(TESTSIM_NODE, &cfg_test_rplmn_cmd);
2071         install_element(TESTSIM_NODE, &cfg_test_hplmn_cmd);
2072
2073         return 0;
2074 }
2075
2076 void vty_notify(struct osmocom_ms *ms, const char *fmt, ...)
2077 {
2078         struct telnet_connection *connection;
2079         char buffer[1000];
2080         va_list args;
2081         struct vty *vty;
2082
2083         if (fmt) {
2084                 va_start(args, fmt);
2085                 vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
2086                 buffer[sizeof(buffer) - 1] = '\0';
2087                 va_end(args);
2088
2089                 if (!buffer[0])
2090                         return;
2091         }
2092
2093         llist_for_each_entry(connection, &active_connections, entry) {
2094                 vty = connection->vty;
2095                 if (!vty)
2096                         continue;
2097                 if (!fmt) {
2098                         vty_out(vty, "%s%% (MS %s)%s", VTY_NEWLINE, ms->name,
2099                                 VTY_NEWLINE);
2100                         continue;
2101                 }
2102                 if (buffer[strlen(buffer) - 1] == '\n') {
2103                         buffer[strlen(buffer) - 1] = '\0';
2104                         vty_out(vty, "%% %s%s", buffer, VTY_NEWLINE);
2105                         buffer[strlen(buffer)] = '\n';
2106                 } else
2107                         vty_out(vty, "%% %s", buffer);
2108         }
2109 }
2110