blob: a375df6112706896818648b22987d8dfe0173229 [file] [log] [blame]
Harald Welte8542cef2017-07-19 20:06:26 +02001module Osmocom_VTY_Functions {
2 import from TELNETasp_PortType all;
3
Harald Welte553f6552018-01-24 18:50:53 +01004 modulepar {
5 charstring mp_prompt_prefix := "OpenBSC";
6 }
Harald Welte8542cef2017-07-19 20:06:26 +02007
Harald Welte553f6552018-01-24 18:50:53 +01008 const charstring VTY_VIEW_SUFFIX := "> ";
9 const charstring VTY_ENABLE_SUFFIX := "# ";
10 const charstring VTY_CFG_SUFFIX := "(*)";
11
12 template charstring t_vty_unknown := pattern "*% Unknown command.";
Harald Welte8542cef2017-07-19 20:06:26 +020013
14 /* configure prompts in TELNETasp module */
15 function f_vty_set_prompts(TELNETasp_PT pt) {
Harald Welte553f6552018-01-24 18:50:53 +010016 var ASP_TelnetDynamicConfig vty_prompt[3] := {
17 {
18 prompt := {
19 id := 1,
20 prompt := mp_prompt_prefix & VTY_VIEW_SUFFIX,
21 has_wildcards := false
22 }
23 }, {
24 prompt := {
25 id := 2,
26 prompt := mp_prompt_prefix & VTY_ENABLE_SUFFIX,
27 has_wildcards := false
28 }
29 }, {
30 prompt := {
31 id := 3,
32 prompt := mp_prompt_prefix & VTY_CFG_SUFFIX,
33 has_wildcards := true
34 }
35 }
36 };
37
Harald Welte8542cef2017-07-19 20:06:26 +020038 /* set some configuration that isn't possible to express
39 * in the config file due to syntactic restrictions (Who invents config
40 * files that don't permit regular expressions? */
41 for (var integer i := 0; i < sizeof(vty_prompt); i:= i + 1) {
Harald Welte553f6552018-01-24 18:50:53 +010042 log(vty_prompt[i]);
43 pt.send(vty_prompt[i]);
Harald Welte8542cef2017-07-19 20:06:26 +020044 }
45 }
46
47 /* wait for any of the permitted prompts; buffer + return all intermediate output */
48 function f_vty_wait_for_prompt(TELNETasp_PT pt) return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +020049 var charstring rx, buf := "";
50 timer T := 2.0;
51
52 T.start;
53 alt {
Harald Welte553f6552018-01-24 18:50:53 +010054 [] pt.receive(mp_prompt_prefix & VTY_VIEW_SUFFIX) { };
55 [] pt.receive(mp_prompt_prefix & VTY_ENABLE_SUFFIX) { };
56 [] pt.receive(pattern mp_prompt_prefix & VTY_CFG_SUFFIX) { };
57 [] pt.receive(t_vty_unknown) {
58 testcase.stop(fail, "VTY: Unknown Command");
59 };
Harald Welte8542cef2017-07-19 20:06:26 +020060 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
Harald Welte553f6552018-01-24 18:50:53 +010061 [] T.timeout {
62 setverdict(fail, "VTY Timeout for prompt");
63 self.stop;
64 };
Harald Welte8542cef2017-07-19 20:06:26 +020065 }
66 T.stop;
67 return buf;
68 }
69
70 /* send a VTY command and obtain response until prompt is received */
71 function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx) return charstring {
72 pt.send(tx);
73 return f_vty_wait_for_prompt(pt);
74 }
75
76 /* send a VTY command and obtain response until prompt is received */
77 function f_vty_transceive(TELNETasp_PT pt, charstring tx) {
78 f_vty_transceive_ret(pt, tx);
79 }
80
81 type integer BtsNr (0..255);
82 type integer BtsTrxNr (0..255);
83 type integer BtsTimeslotNr (0..7);
84
85 type charstring BtsGprsMode ("none", "gprs", "egrps");
86
87 /* enter the'confiugration' mode of the VTY */
88 function f_vty_enter_config(TELNETasp_PT pt) {
89 f_vty_transceive(pt, "configure terminal")
90 }
91
92 function f_vty_enter_cfg_network(TELNETasp_PT pt) {
93 f_vty_enter_config(pt);
94 f_vty_transceive(pt, "network")
95 }
96
97 function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) {
98 f_vty_enter_cfg_network(pt);
99 f_vty_transceive(pt, "bts " & int2str(bts));
100 }
101
102 function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) {
103 f_vty_enter_cfg_bts(pt, bts);
104 f_vty_transceive(pt, "trx " & int2str(trx));
105 }
106
107 function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) {
108 f_vty_enter_cfg_trx(pt, bts, trx);
109 f_vty_transceive(pt, "timeslot " & int2str(ts));
110 }
111
Harald Welte872ce172018-02-16 22:10:33 +0100112function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd)
113{
114 /* enter config mode; enter node */
115 f_vty_enter_config(pt);
116 f_vty_transceive(pt, config_node);
117 /* execute command */
118 f_vty_transceive(pt, cmd);
119 /* leave config mode */
120 f_vty_transceive(pt, "end");
121}
122
123
Harald Welte8542cef2017-07-19 20:06:26 +0200124}