Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 1 | module Osmocom_CTRL_Functions { |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 2 | |
| 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. |
Pau Espin Pedrol | 76ba541 | 2019-06-10 11:00:33 +0200 | [diff] [blame] | 9 | * |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 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 Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 23 | 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 Welte | fdfa046 | 2017-12-10 18:09:40 +0100 | [diff] [blame] | 38 | [] pt.receive(tr_CtrlMsgGetRepl(id, variable)) -> value rx { |
| 39 | } |
Harald Welte | 95a4781 | 2017-12-09 14:19:03 +0100 | [diff] [blame] | 40 | [] pt.receive(tr_CtrlMsgTrap) { repeat; } |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 41 | [] pt.receive(tr_CtrlMsgError) -> value rx { |
| 42 | setverdict(fail, "Error in CTRL GET ", variable, ": ", rx.err.reason); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 43 | mtc.stop; |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 44 | } |
| 45 | [] T.timeout { |
| 46 | setverdict(fail, "Timeout waiting for CTRL GET REPLY ", variable); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 47 | mtc.stop; |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return rx.resp.val; |
| 51 | } |
| 52 | |
| 53 | /* perform a given SET Operation */ |
| 54 | function f_ctrl_set(IPA_CTRL_PT pt, CtrlVariable variable, CtrlValue val) { |
| 55 | timer T := 2.0; |
| 56 | var CtrlMessage rx; |
| 57 | var CtrlId id := f_gen_rand_id(); |
| 58 | pt.send(ts_CtrlMsgSet(id, variable, val)); |
| 59 | T.start; |
| 60 | alt { |
| 61 | [] pt.receive(tr_CtrlMsgSetRepl(id, variable, val)) { } |
Harald Welte | 95a4781 | 2017-12-09 14:19:03 +0100 | [diff] [blame] | 62 | [] pt.receive(tr_CtrlMsgTrap) { repeat; } |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 63 | [] pt.receive(tr_CtrlMsgError) -> value rx { |
| 64 | setverdict(fail, "Error in CTRL GET ", variable, ": ", rx.err.reason); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 65 | mtc.stop; |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 66 | } |
| 67 | [] T.timeout { |
| 68 | setverdict(fail, "Timeout waiting for CTRL SET REPLY ", variable); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 69 | mtc.stop; |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Expect a matching TRAP */ |
| 75 | function f_ctrl_exp_trap(IPA_CTRL_PT pt, template CtrlVariable variable, |
| 76 | template CtrlValue val := ?) return CtrlValue { |
| 77 | timer T := 2.0; |
| 78 | var CtrlMessage rx; |
| 79 | T.start; |
| 80 | alt { |
Harald Welte | fdfa046 | 2017-12-10 18:09:40 +0100 | [diff] [blame] | 81 | [] pt.receive(tr_CtrlMsgTrap(variable, val)) -> value rx { |
| 82 | } |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 83 | [] T.timeout { |
| 84 | setverdict(fail, "Timeout waiting for TRAP ", variable); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 85 | mtc.stop; |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return rx.trap.val; |
| 89 | } |
Harald Welte | 852a384 | 2017-12-09 14:19:36 +0100 | [diff] [blame] | 90 | |
| 91 | /* Expect a matching GET result */ |
| 92 | function f_ctrl_get_exp(IPA_CTRL_PT pt, CtrlVariable variable, template CtrlValue exp) { |
| 93 | var charstring ctrl_resp; |
| 94 | ctrl_resp := f_ctrl_get(pt, variable); |
| 95 | if (not match(ctrl_resp, exp)) { |
| 96 | setverdict(fail, "Unexpected " & variable & ":" & ctrl_resp); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 97 | mtc.stop; |
Harald Welte | 852a384 | 2017-12-09 14:19:36 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Harald Welte | 3ec493f | 2017-12-09 16:25:29 +0100 | [diff] [blame] | 101 | template charstring ts_ctrl_ratectr(CtrlVariable grp, integer instance, CtrlVariable name, |
| 102 | CtrlVariable kind := "abs") := |
| 103 | "rate_ctr." & kind & "." & grp & "." & int2str(instance) & "." & name; |
| 104 | |
| 105 | function f_ctrl_get_ratectr_abs(IPA_CTRL_PT pt, CtrlVariable grp, integer instance, |
| 106 | CtrlVariable name) return integer { |
| 107 | return str2int(f_ctrl_get(pt, valueof(ts_ctrl_ratectr(grp, instance, name)))); |
| 108 | } |
| 109 | |
| 110 | function f_ctrl_get_exp_ratectr_abs(IPA_CTRL_PT pt, CtrlVariable grp, integer instance, |
| 111 | CtrlVariable name, template integer exp) { |
| 112 | var charstring ctrl_resp; |
| 113 | var CtrlVariable variable := valueof(ts_ctrl_ratectr(grp, instance, name)); |
| 114 | ctrl_resp := f_ctrl_get(pt, variable); |
| 115 | if (not match(str2int(ctrl_resp), exp)) { |
| 116 | setverdict(fail, variable & " value " & ctrl_resp & " didn't match ", exp); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 117 | mtc.stop; |
Harald Welte | 3ec493f | 2017-12-09 16:25:29 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Harald Welte | 852a384 | 2017-12-09 14:19:36 +0100 | [diff] [blame] | 121 | |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 122 | } |