Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 1 | /* Asterisk's AMI interface functions in TTCN-3 |
| 2 | * (C) 2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Released under the terms of GNU General Public License, Version 2 or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * https://docs.asterisk.org/Configuration/Interfaces/Asterisk-Manager-Interface-AMI/AMI-v2-Specification/ |
| 14 | */ |
| 15 | module AMI_Functions { |
| 16 | |
| 17 | import from Misc_Helpers all; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 18 | import from Osmocom_Types all; |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 19 | import from IPL4asp_Types all; |
| 20 | import from IPL4asp_PortType all; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 21 | import from Socket_API_Definitions all; |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 22 | import from TCCConversion_Functions all; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 23 | |
| 24 | modulepar { |
| 25 | float mp_ami_prompt_timeout := 10.0; |
| 26 | } |
| 27 | |
| 28 | const charstring AMI_FIELD_ACTION := "Action"; |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 29 | const charstring AMI_FIELD_ACTION_ID := "ActionId"; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 30 | const charstring AMI_FIELD_USERNAME := "Username"; |
| 31 | const charstring AMI_FIELD_SECRET := "Secret"; |
| 32 | const charstring AMI_FIELD_RESPONSE := "Response"; |
| 33 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 34 | /* Extensions: */ |
| 35 | const charstring AMI_FIELD_REGISTRATION := "Registration"; |
| 36 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 37 | type record AMI_Field { |
| 38 | charstring key, |
| 39 | charstring val |
Pau Espin Pedrol | de7a485 | 2024-04-19 16:20:45 +0200 | [diff] [blame] | 40 | } with { |
| 41 | encode "TEXT" |
| 42 | variant "SEPARATOR(': ', ':\s+')" |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 43 | }; |
Pau Espin Pedrol | de7a485 | 2024-04-19 16:20:45 +0200 | [diff] [blame] | 44 | |
| 45 | type set of AMI_Field AMI_Msg with { |
| 46 | encode "TEXT" |
| 47 | variant "SEPARATOR('\r\n', '(\r\n)|[\n]')" |
| 48 | variant "END('\r\n', '(\r\n)|[\n]')" |
| 49 | }; |
| 50 | |
| 51 | external function enc_AMI_Msg(in AMI_Msg msg) return charstring |
| 52 | with { extension "prototype(convert) encode(TEXT)" } |
| 53 | external function dec_AMI_Msg(in charstring stream) return AMI_Msg |
| 54 | with { extension "prototype(convert) decode(TEXT)" } |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 55 | |
| 56 | template (value) AMI_Field |
| 57 | ts_AMI_Field(template (value) charstring key, |
| 58 | template (value) charstring val) := { |
| 59 | key := key, |
| 60 | val := val |
| 61 | }; |
| 62 | |
| 63 | template (present) AMI_Field |
| 64 | tr_AMI_Field(template (present) charstring key := ?, |
| 65 | template (present) charstring val := ?) := { |
| 66 | key := key, |
| 67 | val := val |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * Field Templates: |
| 72 | */ |
| 73 | |
| 74 | template (value) AMI_Field |
| 75 | ts_AMI_Field_Action(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION, val); |
| 76 | template (value) AMI_Field |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 77 | ts_AMI_Field_ActionId(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION_ID, val); |
| 78 | template (value) AMI_Field |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 79 | ts_AMI_Field_Username(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_USERNAME, val); |
| 80 | template (value) AMI_Field |
| 81 | ts_AMI_Field_Secret(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_SECRET, val); |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 82 | /* Extensions: */ |
| 83 | template (value) AMI_Field |
| 84 | ts_AMI_Field_Registration(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_REGISTRATION, val); |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 85 | |
| 86 | template (present) AMI_Field |
| 87 | tr_AMI_Field_Action(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_ACTION, val); |
| 88 | template (present) AMI_Field |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 89 | tr_AMI_Field_ActionId(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_ACTION_ID, val); |
| 90 | template (present) AMI_Field |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 91 | tr_AMI_Field_Username(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_USERNAME, val); |
| 92 | template (present) AMI_Field |
| 93 | tr_AMI_Field_Secret(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_SECRET, val); |
| 94 | template (present) AMI_Field |
| 95 | tr_AMI_Field_Response(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_RESPONSE, val); |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 96 | /* Extensions: */ |
| 97 | template (present) AMI_Field |
| 98 | tr_AMI_Field_Registration(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_REGISTRATION, val); |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 99 | |
| 100 | |
| 101 | template (present) AMI_Field |
| 102 | tr_AMI_Field_ResponseSuccess := tr_AMI_Field(AMI_FIELD_RESPONSE, "Success"); |
| 103 | |
| 104 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 105 | /*********************** |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 106 | * Message Templates: |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 107 | ***********************/ |
| 108 | |
| 109 | /* |
| 110 | * ACTIONS |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 111 | */ |
| 112 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 113 | /* Action: Login |
| 114 | * Username: <value> |
| 115 | * Secret: <value> |
| 116 | */ |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 117 | template (value) AMI_Msg |
| 118 | ts_AMI_Action_Login(charstring username, charstring secret) := { |
| 119 | ts_AMI_Field_Action("Login"), |
| 120 | ts_AMI_Field_Username(username), |
| 121 | ts_AMI_Field_Secret(secret) |
| 122 | }; |
| 123 | |
| 124 | template (present) AMI_Msg |
| 125 | tr_AMI_Action_Login(template(present) charstring username := ?, |
| 126 | template(present) charstring secret := ?) := superset( |
| 127 | tr_AMI_Field_Action("Login"), |
| 128 | tr_AMI_Field_Username(username), |
| 129 | tr_AMI_Field_Secret(secret) |
| 130 | ); |
| 131 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 132 | /* Action: PJSIPRegister |
| 133 | * ActionID: <value> |
| 134 | * Registration: volte_ims |
| 135 | */ |
| 136 | template (value) AMI_Msg |
| 137 | ts_AMI_Action_PJSIPRegister(template (value) charstring registration := "volte_ims", |
| 138 | template (value) charstring action_id := "0001") := { |
| 139 | ts_AMI_Field_Action("PJSIPRegister"), |
| 140 | ts_AMI_Field_ActionId(action_id), |
| 141 | ts_AMI_Field_Registration(registration) |
| 142 | }; |
| 143 | template (present) AMI_Msg |
| 144 | tr_AMI_Action_PJSIPRegister(template (present) charstring registration := ?, |
| 145 | template (present) charstring action_id := ?) := { |
| 146 | tr_AMI_Field_Action("PJSIPRegister"), |
| 147 | tr_AMI_Field_ActionId(action_id), |
| 148 | tr_AMI_Field_Registration(registration) |
| 149 | }; |
| 150 | |
| 151 | /* |
| 152 | * RESPONSES |
| 153 | */ |
| 154 | |
| 155 | /* Response: Success |
| 156 | */ |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 157 | template (present) AMI_Msg |
| 158 | tr_AMI_Response_Success := superset( |
| 159 | tr_AMI_Field_ResponseSuccess |
| 160 | ); |
| 161 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 162 | /* Response: Success |
| 163 | * ActionId: <value> |
| 164 | */ |
| 165 | template (present) AMI_Msg |
| 166 | tr_AMI_Response_Success_ActionId(template (present) charstring action_id := ?) := superset( |
| 167 | tr_AMI_Field_ResponseSuccess, |
| 168 | tr_AMI_Field_ActionId(action_id) |
| 169 | ); |
| 170 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 171 | |
| 172 | /*********************** |
| 173 | * Adapter: |
| 174 | ***********************/ |
| 175 | |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 176 | type record AMI_Adapter_Parameters { |
| 177 | charstring remote_host, |
| 178 | IPL4asp_Types.PortNumber remote_port, |
| 179 | charstring local_host, |
| 180 | IPL4asp_Types.PortNumber local_port, |
| 181 | charstring welcome_str |
| 182 | } |
| 183 | |
| 184 | const AMI_Adapter_Parameters c_default_AMI_Adapter_pars := { |
| 185 | remote_host := "127.0.0.1", |
| 186 | remote_port := 5038, |
| 187 | local_host := "0.0.0.0", |
| 188 | local_port := 0, |
| 189 | welcome_str := "Asterisk Call Manager/9.0.0\r\n" |
| 190 | }; |
| 191 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 192 | type port AMI_Msg_PT message { |
| 193 | inout AMI_Msg; |
| 194 | } with { extension "internal" }; |
| 195 | |
| 196 | type component AMI_Adapter_CT { |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 197 | port IPL4asp_PT IPL4; |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 198 | port AMI_Msg_PT CLIENT; |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 199 | var AMI_Adapter_Parameters g_pars; |
| 200 | |
| 201 | /* Connection identifier of the client itself */ |
| 202 | var IPL4asp_Types.ConnectionId g_self_conn_id := -1; |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 205 | /* Function to use to connect as client to a remote IPA Server */ |
| 206 | private function f_AMI_Adapter_connect() runs on AMI_Adapter_CT { |
| 207 | var IPL4asp_Types.Result res; |
| 208 | map(self:IPL4, system:IPL4); |
| 209 | res := IPL4asp_PortType.f_IPL4_connect(IPL4, g_pars.remote_host, g_pars.remote_port, |
| 210 | g_pars.local_host, g_pars.local_port, 0, { tcp:={} }); |
| 211 | if (not ispresent(res.connId)) { |
| 212 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 213 | log2str("Could not connect AMI socket from ", g_pars.local_host, " port ", |
| 214 | g_pars.local_port, " to ", g_pars.remote_host, " port ", g_pars.remote_port, |
| 215 | "; check your configuration")); |
| 216 | } |
| 217 | g_self_conn_id := res.connId; |
| 218 | log("AMI connected, ConnId=", g_self_conn_id) |
| 219 | } |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 220 | |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 221 | private function f_ASP_RecvFrom_msg_to_charstring(ASP_RecvFrom rx_rf) return charstring { |
| 222 | return oct2char(rx_rf.msg); |
| 223 | } |
| 224 | |
| 225 | /* Function to use to connect as client to a remote IPA Server */ |
| 226 | private function f_AMI_Adapter_wait_rx_welcome_str() runs on AMI_Adapter_CT { |
| 227 | var ASP_RecvFrom rx_rf; |
| 228 | var charstring rx_str; |
| 229 | timer Twelcome := 3.0; |
| 230 | |
| 231 | Twelcome.start; |
| 232 | alt { |
| 233 | [] IPL4.receive(ASP_RecvFrom:?) -> value rx_rf { |
| 234 | rx_str := f_ASP_RecvFrom_msg_to_charstring(rx_rf); |
| 235 | if (g_pars.welcome_str != rx_str) { |
| 236 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 237 | log2str("AMI Welcome message mismatch: '", rx_str, |
| 238 | "' vs exp '", g_pars.welcome_str, "'")); |
| 239 | } |
| 240 | } |
| 241 | [] Twelcome.timeout { |
| 242 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 243 | log2str("AMI Welcome timeout")); |
| 244 | } |
| 245 | } |
| 246 | Twelcome.stop; |
| 247 | log("AMI Welcome message received: '", rx_str, "'"); |
| 248 | } |
| 249 | |
| 250 | private function dec_AMI_Msg_ext(charstring txt) return AMI_Msg { |
| 251 | log("AMI dec: '", txt, "'"); |
| 252 | /* TEXT Enc/dec is not happy with empty values, workaround it: */ |
| 253 | var charstring patched_txt := f_str_replace(txt, "Challenge: \r\n", ""); |
| 254 | patched_txt := f_str_replace(patched_txt, "AccountCode: \r\n", ""); |
| 255 | patched_txt := f_str_replace(patched_txt, "Value: \r\n", ""); |
| 256 | patched_txt := f_str_replace(patched_txt, "DestExten: \r\n", ""); |
| 257 | patched_txt := f_str_replace(patched_txt, "Exten: \r\n", ""); |
| 258 | patched_txt := f_str_replace(patched_txt, "Extension: \r\n", ""); |
| 259 | |
| 260 | /* "AppData" field sometimes has a value containing separator ": ", which makes |
| 261 | * TEXT dec not happy. Workaround it for now by removing the whole field line: |
| 262 | * "AppData: 5,0502: Call pjsip endpoint from 0501\r\n" |
| 263 | */ |
| 264 | var integer pos := f_strstr(patched_txt, "AppData: ", 0); |
| 265 | if (pos >= 0) { |
| 266 | var integer pos_end := f_strstr(patched_txt, "\r\n", pos) + lengthof("\r\n"); |
| 267 | var charstring to_remove := substr(patched_txt, pos, pos_end - pos); |
| 268 | patched_txt := f_str_replace(patched_txt, to_remove, ""); |
| 269 | } |
| 270 | |
| 271 | log("AMI patched dec: '", patched_txt, "'"); |
| 272 | return dec_AMI_Msg(patched_txt); |
| 273 | } |
| 274 | |
| 275 | function f_AMI_Adapter_main(AMI_Adapter_Parameters pars := c_default_AMI_Adapter_pars) |
| 276 | runs on AMI_Adapter_CT { |
| 277 | var AMI_Msg msg; |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 278 | var charstring rx, buf := ""; |
| 279 | var integer fd; |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 280 | var ASP_RecvFrom rx_rf; |
| 281 | var ASP_Event rx_ev; |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 282 | |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 283 | g_pars := pars; |
| 284 | |
| 285 | f_AMI_Adapter_connect(); |
| 286 | |
| 287 | f_AMI_Adapter_wait_rx_welcome_str(); |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 288 | |
| 289 | while (true) { |
| 290 | |
| 291 | alt { |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 292 | [] IPL4.receive(ASP_RecvFrom:?) -> value rx_rf { |
| 293 | var charstring rx_str := f_ASP_RecvFrom_msg_to_charstring(rx_rf); |
| 294 | log("AMI rx: '", rx_str, "'"); |
| 295 | buf := buf & rx_str; |
| 296 | log("AMI buf: '", buf, "'"); |
| 297 | |
| 298 | /* If several messages come together */ |
| 299 | var boolean last_is_complete := f_str_endswith(buf, "\r\n\r\n"); |
| 300 | var Misc_Helpers.ro_charstring msgs := f_str_split(buf, "\r\n\r\n"); |
| 301 | log("AMI split: ", msgs); |
| 302 | if (lengthof(msgs) > 0) { |
| 303 | for (var integer i := 0; i < lengthof(msgs) - 1; i := i + 1) { |
| 304 | var charstring txt := msgs[i] & "\r\n"; |
| 305 | msg := dec_AMI_Msg_ext(txt); |
| 306 | CLIENT.send(msg); |
| 307 | } |
| 308 | if (last_is_complete) { |
| 309 | var charstring txt := msgs[lengthof(msgs) - 1] & "\r\n"; |
| 310 | msg := dec_AMI_Msg_ext(txt); |
| 311 | CLIENT.send(msg); |
| 312 | buf := ""; |
| 313 | } else { |
| 314 | buf := msgs[lengthof(msgs) - 1]; |
| 315 | } |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 316 | } |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 317 | log("AMI remain buf: '", buf, "'"); |
| 318 | } |
| 319 | [] IPL4.receive(ASP_ConnId_ReadyToRelease:?) { |
| 320 | } |
| 321 | |
| 322 | [] IPL4.receive(ASP_Event:?) -> value rx_ev { |
| 323 | log("Rx AMI ASP_Event: ", rx_ev); |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 324 | } |
| 325 | [] CLIENT.receive(AMI_Msg:?) -> value msg { |
| 326 | /* TODO: in the future, queue Action if there's already one Action in transit, to fullfill AMI requirements. */ |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 327 | var charstring tx_txt := enc_AMI_Msg(msg) & "\r\n"; |
| 328 | |
| 329 | var ASP_SendTo tx := { |
| 330 | connId := g_self_conn_id, |
| 331 | remName := g_pars.remote_host, |
| 332 | remPort := g_pars.remote_port, |
| 333 | proto := { tcp := {} }, |
| 334 | msg := char2oct(tx_txt) |
| 335 | }; |
| 336 | IPL4.send(tx); |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 343 | /* |
| 344 | * Functions: |
| 345 | */ |
| 346 | |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 347 | /* Generate a random "ActionId" value: */ |
| 348 | function f_gen_action_id() return charstring { |
| 349 | return hex2str(f_rnd_hexstring(16)); |
| 350 | } |
| 351 | |
| 352 | function f_ami_msg_find(AMI_Msg msg, |
| 353 | template (present) charstring key := ?) |
| 354 | return template (omit) AMI_Field { |
| 355 | var integer i; |
| 356 | |
| 357 | for (i := 0; i < lengthof(msg); i := i + 1) { |
| 358 | if (not ispresent(msg[i])) { |
| 359 | continue; |
| 360 | } |
| 361 | if (match(msg[i].key, key)) { |
| 362 | return msg[i]; |
| 363 | } |
| 364 | } |
| 365 | return omit; |
| 366 | } |
| 367 | |
| 368 | function f_ami_msg_find_or_fail(AMI_Msg msg, |
| 369 | template (present) charstring key := ?) |
| 370 | return AMI_Field { |
| 371 | var template (omit) AMI_Field field; |
| 372 | field := f_ami_msg_find(msg, key); |
| 373 | if (istemplatekind(field, "omit")) { |
| 374 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 375 | log2str("Key ", key, " not found in ", msg)); |
| 376 | } |
| 377 | return valueof(field); |
| 378 | } |
| 379 | |
| 380 | function f_ami_msg_get_value(AMI_Msg msg, |
| 381 | template (present) charstring key := ?) |
| 382 | return template (omit) charstring { |
| 383 | var template (omit) AMI_Field field; |
| 384 | field := f_ami_msg_find(msg, key); |
| 385 | if (istemplatekind(field, "omit")) { |
| 386 | return omit; |
| 387 | } |
| 388 | return field.val; |
| 389 | } |
| 390 | |
| 391 | function f_ami_msg_get_value_or_fail(AMI_Msg msg, |
| 392 | template (present) charstring key := ?) |
| 393 | return template charstring { |
| 394 | var AMI_Field field; |
| 395 | field := f_ami_msg_find_or_fail(msg, key); |
| 396 | return field.val; |
| 397 | } |
| 398 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 399 | function f_ami_transceive_ret(AMI_Msg_PT pt, template (value) AMI_Msg tx_msg, float rx_timeout := 10.0) return AMI_Msg { |
| 400 | var AMI_Msg rx_msg; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 401 | timer T; |
| 402 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 403 | T.start(rx_timeout); |
| 404 | pt.send(tx_msg); |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 405 | alt { |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 406 | [] pt.receive(AMI_Msg:?) -> value rx_msg; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 407 | [] T.timeout { |
| 408 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 409 | log2str("AMI Response timeout: ", tx_msg)); |
| 410 | } |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 411 | } |
| 412 | T.stop; |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 413 | return rx_msg; |
| 414 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 417 | private altstep as_ami_rx_fail(AMI_Msg_PT pt, template AMI_Msg exp_msg := *) |
| 418 | { |
| 419 | var AMI_Msg msg; |
| 420 | [] pt.receive(AMI_Msg:?) -> value msg { |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 421 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 422 | log2str("Received unexpected AMI message := ", msg, "\nvs exp := ", exp_msg)); |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 426 | altstep as_ami_expect_msg(AMI_Msg_PT pt, template (present) AMI_Msg msg_expect, boolean fail_others := true) |
| 427 | { |
| 428 | [] pt.receive(msg_expect); |
| 429 | [fail_others] as_ami_rx_fail(pt, msg_expect); |
| 430 | } |
| 431 | |
| 432 | function f_ami_transceive_match(AMI_Msg_PT pt, |
| 433 | template (value) AMI_Msg tx_msg, |
| 434 | template (present) AMI_Msg exp_ret := ?, |
| 435 | boolean fail_others := true, |
| 436 | float rx_timeout := 10.0) return AMI_Msg { |
| 437 | var AMI_Msg rx_msg; |
| 438 | timer T; |
| 439 | |
| 440 | T.start(rx_timeout); |
| 441 | pt.send(tx_msg); |
| 442 | alt { |
| 443 | [] pt.receive(exp_ret) -> value rx_msg; |
| 444 | [not fail_others] pt.receive(AMI_Msg:?) -> value rx_msg { |
| 445 | log("AMI: Ignoring Rx msg ", rx_msg); |
| 446 | repeat; |
| 447 | } |
| 448 | [fail_others] as_ami_rx_fail(pt, exp_ret); |
| 449 | [] T.timeout { |
| 450 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 451 | log2str("AMI Response timeout: ", tx_msg)); |
| 452 | } |
| 453 | } |
| 454 | T.stop; |
| 455 | return rx_msg; |
| 456 | } |
| 457 | |
| 458 | function f_ami_transceive_match_response_success(AMI_Msg_PT pt, |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 459 | template (value) AMI_Msg tx_msg) { |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 460 | var template (present) AMI_Msg exp_resp; |
| 461 | var template (omit) charstring action_id := f_ami_msg_get_value(valueof(tx_msg), AMI_FIELD_ACTION_ID); |
| 462 | if (isvalue(action_id)) { |
| 463 | exp_resp := tr_AMI_Response_Success_ActionId(action_id); |
| 464 | } else { |
| 465 | exp_resp := tr_AMI_Response_Success; |
| 466 | } |
| 467 | f_ami_transceive_match(pt, tx_msg, exp_resp); |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 470 | function f_ami_action_login(AMI_Msg_PT pt, charstring username, charstring secret) { |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 471 | f_ami_transceive_match_response_success(pt, ts_AMI_Action_Login(username, secret)); |
| 472 | } |
| 473 | |
Pau Espin Pedrol | bcb4e82 | 2024-04-26 20:16:47 +0200 | [diff] [blame] | 474 | function f_ami_action_PJSIPRegister(AMI_Msg_PT pt, charstring register) { |
Pau Espin Pedrol | 4362dbd | 2024-04-26 19:31:27 +0200 | [diff] [blame] | 475 | var charstring reg_action_id := f_gen_action_id(); |
| 476 | f_ami_transceive_match_response_success(pt, ts_AMI_Action_PJSIPRegister(register, reg_action_id)); |
| 477 | } |
| 478 | |
Pau Espin Pedrol | 01f1df8 | 2024-05-08 16:55:55 +0200 | [diff] [blame] | 479 | private function f_ami_selftest_decode(charstring txt) { |
| 480 | log("Text to decode: '", txt, "'"); |
| 481 | var AMI_Msg msg := dec_AMI_Msg(txt); |
| 482 | log("AMI_Msg decoded: ", msg); |
| 483 | } |
| 484 | |
| 485 | function f_ami_selftest() { |
| 486 | f_ami_selftest_decode("AppData: 5,0502: Call pjsip endpoint from 0501\r\n"); |
| 487 | } |
| 488 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 489 | } |