blob: da3b80de728d9ebaf6445afe66503b4eafb08727 [file] [log] [blame]
Harald Welte03c0e562017-12-09 02:55:12 +01001module Osmocom_CTRL_Functions {
Harald Welte35bb7162018-01-03 21:07:52 +01002
3/* Definition of helper functions for the Osmocom CTRL interface.
4 *
5 * As opposed to many other parts of the Osmocom TTCN-3 code base, this module
6 * implements blocking functions, instead of asynchronous functions. The
7 * rationale for this is simple: One normally wants to inquire a value or set
8 * a value and not continue the main program until that operation is complete.
9 *
10 * CTRL is a machine-type protocol on how external programs can interact with
11 * an Osmocom program in a structured way. It is intended for programmatic
12 * access (by other software), as opposed to the VTY interface intended for
13 * human consumption.
14 *
15 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
16 * All rights reserved.
17 *
18 * Released under the terms of GNU General Public License, Version 2 or
19 * (at your option) any later version.
20 */
21
22
Harald Welte03c0e562017-12-09 02:55:12 +010023 import from Osmocom_CTRL_Types all;
24 import from IPA_Emulation all;
25
26 private function f_gen_rand_id() return CtrlId {
27 return int2str(float2int(rnd()*999999999.0));
28 }
29
30 /* perform a given GET Operation */
31 function f_ctrl_get(IPA_CTRL_PT pt, CtrlVariable variable) return CtrlValue {
32 timer T := 2.0;
33 var CtrlMessage rx;
34 var CtrlId id := f_gen_rand_id();
35 pt.send(ts_CtrlMsgGet(id, variable));
36 T.start;
37 alt {
Harald Weltefdfa0462017-12-10 18:09:40 +010038 [] pt.receive(tr_CtrlMsgGetRepl(id, variable)) -> value rx {
39 }
Harald Welte95a47812017-12-09 14:19:03 +010040 [] pt.receive(tr_CtrlMsgTrap) { repeat; }
Harald Welte03c0e562017-12-09 02:55:12 +010041 [] pt.receive(tr_CtrlMsgError) -> value rx {
42 setverdict(fail, "Error in CTRL GET ", variable, ": ", rx.err.reason);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020043 mtc.stop;
Harald Weltefdfa0462017-12-10 18:09:40 +010044 return "FAIL";
Harald Welte03c0e562017-12-09 02:55:12 +010045 }
46 [] T.timeout {
47 setverdict(fail, "Timeout waiting for CTRL GET REPLY ", variable);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020048 mtc.stop;
Harald Weltefdfa0462017-12-10 18:09:40 +010049 return "TIMEOUT";
Harald Welte03c0e562017-12-09 02:55:12 +010050 }
51 }
52 return rx.resp.val;
53 }
54
55 /* perform a given SET Operation */
56 function f_ctrl_set(IPA_CTRL_PT pt, CtrlVariable variable, CtrlValue val) {
57 timer T := 2.0;
58 var CtrlMessage rx;
59 var CtrlId id := f_gen_rand_id();
60 pt.send(ts_CtrlMsgSet(id, variable, val));
61 T.start;
62 alt {
63 [] pt.receive(tr_CtrlMsgSetRepl(id, variable, val)) { }
Harald Welte95a47812017-12-09 14:19:03 +010064 [] pt.receive(tr_CtrlMsgTrap) { repeat; }
Harald Welte03c0e562017-12-09 02:55:12 +010065 [] pt.receive(tr_CtrlMsgError) -> value rx {
66 setverdict(fail, "Error in CTRL GET ", variable, ": ", rx.err.reason);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020067 mtc.stop;
Harald Welte03c0e562017-12-09 02:55:12 +010068 }
69 [] T.timeout {
70 setverdict(fail, "Timeout waiting for CTRL SET REPLY ", variable);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020071 mtc.stop;
Harald Welte03c0e562017-12-09 02:55:12 +010072 }
73 }
74 }
75
76 /* Expect a matching TRAP */
77 function f_ctrl_exp_trap(IPA_CTRL_PT pt, template CtrlVariable variable,
78 template CtrlValue val := ?) return CtrlValue {
79 timer T := 2.0;
80 var CtrlMessage rx;
81 T.start;
82 alt {
Harald Weltefdfa0462017-12-10 18:09:40 +010083 [] pt.receive(tr_CtrlMsgTrap(variable, val)) -> value rx {
84 }
Harald Welte03c0e562017-12-09 02:55:12 +010085 [] T.timeout {
86 setverdict(fail, "Timeout waiting for TRAP ", variable);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020087 mtc.stop;
Harald Weltefdfa0462017-12-10 18:09:40 +010088 return "TIMEOUT";
Harald Welte03c0e562017-12-09 02:55:12 +010089 }
90 }
91 return rx.trap.val;
92 }
Harald Welte852a3842017-12-09 14:19:36 +010093
94 /* Expect a matching GET result */
95 function f_ctrl_get_exp(IPA_CTRL_PT pt, CtrlVariable variable, template CtrlValue exp) {
96 var charstring ctrl_resp;
97 ctrl_resp := f_ctrl_get(pt, variable);
98 if (not match(ctrl_resp, exp)) {
99 setverdict(fail, "Unexpected " & variable & ":" & ctrl_resp);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200100 mtc.stop;
Harald Welte852a3842017-12-09 14:19:36 +0100101 }
102 }
103
Harald Welte3ec493f2017-12-09 16:25:29 +0100104 template charstring ts_ctrl_ratectr(CtrlVariable grp, integer instance, CtrlVariable name,
105 CtrlVariable kind := "abs") :=
106 "rate_ctr." & kind & "." & grp & "." & int2str(instance) & "." & name;
107
108 function f_ctrl_get_ratectr_abs(IPA_CTRL_PT pt, CtrlVariable grp, integer instance,
109 CtrlVariable name) return integer {
110 return str2int(f_ctrl_get(pt, valueof(ts_ctrl_ratectr(grp, instance, name))));
111 }
112
113 function f_ctrl_get_exp_ratectr_abs(IPA_CTRL_PT pt, CtrlVariable grp, integer instance,
114 CtrlVariable name, template integer exp) {
115 var charstring ctrl_resp;
116 var CtrlVariable variable := valueof(ts_ctrl_ratectr(grp, instance, name));
117 ctrl_resp := f_ctrl_get(pt, variable);
118 if (not match(str2int(ctrl_resp), exp)) {
119 setverdict(fail, variable & " value " & ctrl_resp & " didn't match ", exp);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200120 mtc.stop;
Harald Welte3ec493f2017-12-09 16:25:29 +0100121 }
122 }
123
Harald Welte852a3842017-12-09 14:19:36 +0100124
Harald Welte03c0e562017-12-09 02:55:12 +0100125}