blob: 494b1714cf75e84c8582868ee85a6a910bdc79a3 [file] [log] [blame]
Daniel Willmannfa870f52018-01-17 12:37:14 +01001module MGCP_Emulation {
2
Harald Weltea02515f2018-01-26 10:33:23 +01003/* MGCP Emulation, runs on top of MGCP_CodecPort. It multiplexes/demultiplexes
4 * the individual connections, so there can be separate TTCN-3 components handling
5 * each of the connections.
6 *
7 * The MGCP_Emulation.main() function processes MGCP primitives from the MGCP
8 * socket via the MGCP_CodecPort, and dispatches them to the per-connection components.
9 *
10 * For each new inbound connection, the MgcpOps.create_cb() is called. It can create
11 * or resolve a TTCN-3 component, and returns a component reference to which that inbound
12 * connection is routed/dispatched.
13 *
14 * If a pre-existing component wants to register to handle a future inbound call, it can
15 * do so by registering an "expect" with the expected destination phone number. This is e.g. useful
16 * if you are simulating BSC + MGCP, and first trigger a connection from BSC side in a
17 * component which then subsequently should also handle the MGCP emulation.
18 *
19 * Inbound Unit Data messages (such as are dispatched to the MgcpOps.unitdata_cb() callback,
20 * which is registered with an argument to the main() function below.
21 *
22 * (C) 2017-2018 by Harald Welte <laforge@gnumonks.org>
23 * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Daniel Willmann
24 * All rights reserved.
25 *
26 * Released under the terms of GNU General Public License, Version 2 or
27 * (at your option) any later version.
Harald Welte34b5a952019-05-27 11:54:11 +020028 *
29 * SPDX-License-Identifier: GPL-2.0-or-later
Harald Weltea02515f2018-01-26 10:33:23 +010030 */
31
Daniel Willmannfa870f52018-01-17 12:37:14 +010032import from MGCP_CodecPort all;
33import from MGCP_CodecPort_CtrlFunct all;
34import from MGCP_Types all;
35import from MGCP_Templates all;
36import from Osmocom_Types all;
37import from IPL4asp_Types all;
38
39type component MGCP_ConnHdlr {
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +020040 /* Simple send/recv without caring about peer addr+port. Used with multi_conn_mode=false. */
Daniel Willmannfa870f52018-01-17 12:37:14 +010041 port MGCP_Conn_PT MGCP;
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +020042 /* Handle multiple connections concurrently. Used with multi_conn_mode=true. */
43 port MGCP_Conn_Multi_PT MGCP_MULTI;
44 /* procedure based port to register for incoming connections. */
Harald Weltebb5a1212018-01-26 10:34:44 +010045 port MGCPEM_PROC_PT MGCP_PROC;
Daniel Willmannfa870f52018-01-17 12:37:14 +010046}
47
48/* port between individual per-connection components and this dispatcher */
49type port MGCP_Conn_PT message {
50 inout MgcpCommand, MgcpResponse;
51} with { extension "internal" };
52
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +020053/* port between individual per-connection components and this dispatcher */
54type port MGCP_Conn_Multi_PT message {
55 inout MGCP_RecvFrom, MGCP_SendTo;
56} with { extension "internal" };
57
Harald Welte9601c812018-01-26 18:58:20 +010058/* represents a single MGCP Endpoint */
59type record EndpointData {
Harald Weltebb5a1212018-01-26 10:34:44 +010060 MGCP_ConnHdlr comp_ref,
Harald Welte9601c812018-01-26 18:58:20 +010061 MgcpEndpoint endpoint optional
Harald Weltebb5a1212018-01-26 10:34:44 +010062};
Daniel Willmannfa870f52018-01-17 12:37:14 +010063
Harald Welte9601c812018-01-26 18:58:20 +010064/* pending CRCX with their transaction ID */
65type set of MgcpTransId MgcpTransIds;
66
Daniel Willmannfa870f52018-01-17 12:37:14 +010067type component MGCP_Emulation_CT {
68 /* Port facing to the UDP SUT */
69 port MGCP_CODEC_PT MGCP;
70 /* All MGCP_ConnHdlr MGCP ports connect here
71 * MGCP_Emulation_CT.main needs to figure out what messages
72 * to send where with CLIENT.send() to vc_conn */
Harald Weltebb5a1212018-01-26 10:34:44 +010073 port MGCP_Conn_PT MGCP_CLIENT;
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +020074 /* This one is used with multi_conn_mode=true and allows differentiating UDP sockets */
75 port MGCP_Conn_Multi_PT MGCP_CLIENT_MULTI;
Daniel Willmannfa870f52018-01-17 12:37:14 +010076 /* currently tracked connections */
Harald Welte9601c812018-01-26 18:58:20 +010077 var EndpointData MgcpEndpointTable[16];
78 var MgcpTransIds MgcpPendingTrans := {};
Daniel Willmannfa870f52018-01-17 12:37:14 +010079 /* pending expected CRCX */
Harald Weltebb5a1212018-01-26 10:34:44 +010080 var ExpectData MgcpExpectTable[8];
Daniel Willmannfa870f52018-01-17 12:37:14 +010081 /* procedure based port to register for incoming connections */
Harald Weltebb5a1212018-01-26 10:34:44 +010082 port MGCPEM_PROC_PT MGCP_PROC;
Daniel Willmannfa870f52018-01-17 12:37:14 +010083
84 var charstring g_mgcp_id;
Daniel Willmann166bbb32018-01-17 15:28:04 +010085 var integer g_mgcp_conn_id := -1;
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +020086
87 var MGCP_conn_parameters g_pars;
Daniel Willmannfa870f52018-01-17 12:37:14 +010088}
89
90type function MGCPCreateCallback(MgcpCommand cmd, charstring id)
91runs on MGCP_Emulation_CT return MGCP_ConnHdlr;
92
Harald Weltebb5a1212018-01-26 10:34:44 +010093type function MGCPUnitdataCallback(MgcpMessage msg)
94runs on MGCP_Emulation_CT return template MgcpMessage;
95
Daniel Willmannfa870f52018-01-17 12:37:14 +010096type record MGCPOps {
Harald Weltebb5a1212018-01-26 10:34:44 +010097 MGCPCreateCallback create_cb,
98 MGCPUnitdataCallback unitdata_cb
Daniel Willmannfa870f52018-01-17 12:37:14 +010099}
100
101type record MGCP_conn_parameters {
Harald Weltebb5a1212018-01-26 10:34:44 +0100102 HostName callagent_ip,
103 PortNumber callagent_udp_port,
104 HostName mgw_ip,
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200105 PortNumber mgw_udp_port,
106 boolean multi_conn_mode
Daniel Willmannfa870f52018-01-17 12:37:14 +0100107}
108
Daniel Willmann166bbb32018-01-17 15:28:04 +0100109function tr_MGCP_RecvFrom_R(template MgcpMessage msg)
110runs on MGCP_Emulation_CT return template MGCP_RecvFrom {
111 var template MGCP_RecvFrom mrf := {
112 connId := g_mgcp_conn_id,
113 remName := ?,
114 remPort := ?,
115 locName := ?,
116 locPort := ?,
117 msg := msg
118 }
119 return mrf;
120}
121
Harald Welte9601c812018-01-26 18:58:20 +0100122private function f_ep_known(MgcpEndpoint ep)
Harald Weltebb5a1212018-01-26 10:34:44 +0100123runs on MGCP_Emulation_CT return boolean {
124 var integer i;
Harald Welte9601c812018-01-26 18:58:20 +0100125 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
126 if (MgcpEndpointTable[i].endpoint == ep) {
Harald Weltebb5a1212018-01-26 10:34:44 +0100127 return true;
128 }
129 }
130 return false;
131}
132
133private function f_comp_known(MGCP_ConnHdlr client)
134runs on MGCP_Emulation_CT return boolean {
135 var integer i;
Harald Welte9601c812018-01-26 18:58:20 +0100136 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
137 if (MgcpEndpointTable[i].comp_ref == client) {
Harald Weltebb5a1212018-01-26 10:34:44 +0100138 return true;
139 }
140 }
141 return false;
142}
143
Harald Welte9601c812018-01-26 18:58:20 +0100144private function f_comp_by_ep(MgcpEndpoint ep)
Harald Weltebb5a1212018-01-26 10:34:44 +0100145runs on MGCP_Emulation_CT return MGCP_ConnHdlr {
146 var integer i;
Harald Welte9601c812018-01-26 18:58:20 +0100147 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
148 if (MgcpEndpointTable[i].endpoint == ep) {
149 return MgcpEndpointTable[i].comp_ref;
Harald Weltebb5a1212018-01-26 10:34:44 +0100150 }
151 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200152 setverdict(fail, "MGCP Endpoint Table not found by Endpoint", ep);
153 mtc.stop;
Harald Weltebb5a1212018-01-26 10:34:44 +0100154}
155
Harald Welte9601c812018-01-26 18:58:20 +0100156private function f_ep_by_comp(MGCP_ConnHdlr client)
157runs on MGCP_Emulation_CT return MgcpEndpoint {
Harald Weltebb5a1212018-01-26 10:34:44 +0100158 var integer i;
Harald Welte9601c812018-01-26 18:58:20 +0100159 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
160 if (MgcpEndpointTable[i].comp_ref == client) {
161 return MgcpEndpointTable[i].endpoint;
Harald Weltebb5a1212018-01-26 10:34:44 +0100162 }
163 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200164 setverdict(fail, "MGCP Endpoint Table not found by component ", client);
165 mtc.stop;
Harald Weltebb5a1212018-01-26 10:34:44 +0100166}
167
Harald Welte9601c812018-01-26 18:58:20 +0100168private function f_ep_table_add(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep)
169runs on MGCP_Emulation_CT {
170 var integer i;
171 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
172 if (not isvalue(MgcpEndpointTable[i].endpoint)) {
173 MgcpEndpointTable[i].endpoint := ep;
174 MgcpEndpointTable[i].comp_ref := comp_ref;
175 return;
176 }
177 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200178 testcase.stop("MGCP Endpoint Table full!");
Harald Welte9601c812018-01-26 18:58:20 +0100179}
180
181private function f_ep_table_del(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep)
182runs on MGCP_Emulation_CT {
183 var integer i;
184 for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
185 if (MgcpEndpointTable[i].comp_ref == comp_ref and
186 MgcpEndpointTable[i].endpoint == ep) {
187 MgcpEndpointTable[i].endpoint := omit;
188 MgcpEndpointTable[i].comp_ref := null;
189 return;
190 }
191 }
192 setverdict(fail, "MGCP Endpoint Table: Couldn't find to-be-deleted entry!");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200193 mtc.stop;
Harald Welte9601c812018-01-26 18:58:20 +0100194}
195
196
197/* Check if the given transaction ID is a pending CRCX. If yes, return true + remove */
198private function f_trans_id_was_pending(MgcpTransId trans_id)
199runs on MGCP_Emulation_CT return boolean {
200 for (var integer i := 0; i < lengthof(MgcpPendingTrans); i := i+1) {
201 if (MgcpPendingTrans[i] == trans_id) {
202 /* Remove from list */
203 var MgcpTransIds OldPendingTrans := MgcpPendingTrans;
204 MgcpPendingTrans := {}
205 for (var integer j := 0; j < lengthof(OldPendingTrans); j := j+1) {
206 if (j != i) {
207 MgcpPendingTrans := MgcpPendingTrans & {OldPendingTrans[j]};
208 }
209 }
210 return true;
211 }
212 }
213 return false;
214}
215
Harald Weltebb5a1212018-01-26 10:34:44 +0100216/* TODO: move this to MGCP_Types? */
Harald Welte9601c812018-01-26 18:58:20 +0100217function f_mgcp_ep(MgcpMessage msg) return MgcpEndpoint {
Harald Weltebb5a1212018-01-26 10:34:44 +0100218 var MgcpParameterList params;
219 var integer i;
220 if (ischosen(msg.command)) {
Harald Welte9601c812018-01-26 18:58:20 +0100221 return msg.command.line.ep;
Harald Weltebb5a1212018-01-26 10:34:44 +0100222 } else {
Harald Welte363cb0a2018-01-30 19:35:53 +0100223 var MgcpEndpoint ep;
224 if (f_mgcp_find_param(msg, "Z", ep) == false) {
225 setverdict(fail, "No SpecificEndpointName in MGCP response", msg);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200226 mtc.stop;
Harald Welte363cb0a2018-01-30 19:35:53 +0100227 }
228 return ep;
Harald Weltebb5a1212018-01-26 10:34:44 +0100229 }
Harald Weltebb5a1212018-01-26 10:34:44 +0100230}
231
Harald Welte9601c812018-01-26 18:58:20 +0100232private function f_ep_table_init()
Harald Weltebb5a1212018-01-26 10:34:44 +0100233runs on MGCP_Emulation_CT {
Harald Welte9601c812018-01-26 18:58:20 +0100234 for (var integer i := 0; i < sizeof(MgcpEndpointTable); i := i+1) {
235 MgcpEndpointTable[i].comp_ref := null;
236 MgcpEndpointTable[i].endpoint := omit;
Harald Weltebb5a1212018-01-26 10:34:44 +0100237 }
238}
239
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200240private function f_forward_to_client(MGCP_RecvFrom mrf, MGCP_ConnHdlr vc_conn) runs on MGCP_Emulation_CT {
241 if (g_pars.multi_conn_mode) {
242 MGCP_CLIENT_MULTI.send(mrf) to vc_conn;
243 } else {
244 MGCP_CLIENT.send(mrf.msg.command) to vc_conn;
245 }
246}
247
Daniel Willmann955627a2018-01-17 15:22:32 +0100248function main(MGCPOps ops, MGCP_conn_parameters p, charstring id) runs on MGCP_Emulation_CT {
Daniel Willmannfa870f52018-01-17 12:37:14 +0100249 var Result res;
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200250 g_pars := p;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100251 g_mgcp_id := id;
Harald Welte9601c812018-01-26 18:58:20 +0100252 f_ep_table_init();
Daniel Willmann955627a2018-01-17 15:22:32 +0100253 f_expect_table_init();
Daniel Willmannfa870f52018-01-17 12:37:14 +0100254
255 map(self:MGCP, system:MGCP_CODEC_PT);
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200256 if (p.multi_conn_mode or p.callagent_udp_port == -1) {
Harald Weltebb5a1212018-01-26 10:34:44 +0100257 res := MGCP_CodecPort_CtrlFunct.f_IPL4_listen(MGCP, p.mgw_ip, p.mgw_udp_port, { udp:={} });
258 } else {
259 res := MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, -1, { udp:={} });
260 }
Harald Welte9220f632018-05-23 20:27:02 +0200261 if (not ispresent(res.connId)) {
262 setverdict(fail, "Could not connect MGCP socket, check your configuration");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200263 mtc.stop;
Harald Welte9220f632018-05-23 20:27:02 +0200264 }
Daniel Willmann166bbb32018-01-17 15:28:04 +0100265 g_mgcp_conn_id := res.connId;
Harald Weltebb5a1212018-01-26 10:34:44 +0100266
Daniel Willmannfa870f52018-01-17 12:37:14 +0100267 while (true) {
Daniel Willmann166bbb32018-01-17 15:28:04 +0100268 var MGCP_ConnHdlr vc_conn;
269 var ExpectCriteria crit;
270 var MGCP_RecvFrom mrf;
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200271 var MGCP_SendTo mst;
Daniel Willmann166bbb32018-01-17 15:28:04 +0100272 var MgcpMessage msg;
273 var MgcpCommand cmd;
274 var MgcpResponse resp;
Harald Welte9601c812018-01-26 18:58:20 +0100275 var MgcpEndpoint ep;
Daniel Willmann166bbb32018-01-17 15:28:04 +0100276
Daniel Willmannfa870f52018-01-17 12:37:14 +0100277 alt {
Daniel Willmann166bbb32018-01-17 15:28:04 +0100278 /* MGCP from client */
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200279 [not p.multi_conn_mode] MGCP_CLIENT.receive(MgcpResponse:?) -> value resp sender vc_conn {
Harald Welte9601c812018-01-26 18:58:20 +0100280 msg := {
281 response := resp
282 };
283 /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */
284 if (f_trans_id_was_pending(resp.line.trans_id)) {
285 f_ep_table_add(vc_conn, f_mgcp_ep(msg));
286 }
Daniel Willmann166bbb32018-01-17 15:28:04 +0100287 /* Pass message through */
Harald Weltebb5a1212018-01-26 10:34:44 +0100288 /* TODO: check which ConnectionID client has allocated + store in table? */
Daniel Willmann166bbb32018-01-17 15:28:04 +0100289 MGCP.send(t_MGCP_Send(g_mgcp_conn_id, msg));
Daniel Willmannfa870f52018-01-17 12:37:14 +0100290 }
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200291
292 /* MGCP from client in Multi Conn mode */
293 [p.multi_conn_mode] MGCP_CLIENT_MULTI.receive(MGCP_SendTo:?) -> value mst sender vc_conn {
294 /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */
295 if (f_trans_id_was_pending(mst.msg.response.line.trans_id)) {
296 f_ep_table_add(vc_conn, f_mgcp_ep(mst.msg));
297 }
298 /* Pass message through */
299 /* TODO: check which ConnectionID client has allocated + store in table? */
300 MGCP.send(mst);
301 }
Daniel Willmann166bbb32018-01-17 15:28:04 +0100302 [] MGCP.receive(tr_MGCP_RecvFrom_R(?)) -> value mrf {
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200303 if (not p.multi_conn_mode and p.callagent_udp_port == -1) {
304 /* we aren't yet connected to the remote side
305 port, let's fix this. This way upper layers
306 can use Send/Recv without caring about UDP
307 src/dst addr + port */
Harald Weltebb5a1212018-01-26 10:34:44 +0100308 p.callagent_udp_port := mrf.remPort;
Harald Welte930d0a72018-03-22 22:08:40 +0100309 res := MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, g_mgcp_conn_id, { udp:={} });
Harald Welte9220f632018-05-23 20:27:02 +0200310 if (not ispresent(res.connId)) {
311 setverdict(fail, "Could not connect MGCP socket, check your configuration");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200312 mtc.stop;
Harald Welte9220f632018-05-23 20:27:02 +0200313 }
Harald Weltebb5a1212018-01-26 10:34:44 +0100314 }
Daniel Willmann166bbb32018-01-17 15:28:04 +0100315 if (ischosen(mrf.msg.command)) {
316 cmd := mrf.msg.command;
Harald Welte9601c812018-01-26 18:58:20 +0100317 if (f_ep_known(cmd.line.ep)) {
318 vc_conn := f_comp_by_ep(cmd.line.ep);
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200319 f_forward_to_client(mrf, vc_conn);
Harald Weltebb5a1212018-01-26 10:34:44 +0100320 } else {
Harald Welte9601c812018-01-26 18:58:20 +0100321 if (cmd.line.verb == "CRCX") {
322 vc_conn := ops.create_cb.apply(cmd, id);
Harald Welte363cb0a2018-01-30 19:35:53 +0100323 if (not match(cmd.line.ep, t_MGCP_EP_wildcard)) {
324 /* non-wildcard EP, use directly */
Harald Welte9601c812018-01-26 18:58:20 +0100325 f_ep_table_add(vc_conn, cmd.line.ep);
326 } else {
327 /* add this transaction to list of pending transactions */
328 MgcpPendingTrans := MgcpPendingTrans & {cmd.line.trans_id};
329 }
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200330 f_forward_to_client(mrf, vc_conn);
Harald Welte9601c812018-01-26 18:58:20 +0100331 } else {
332 /* connectionless MGCP, i.e. messages without ConnectionId */
333 var template MgcpMessage r := ops.unitdata_cb.apply(mrf.msg);
334 if (isvalue(r)) {
Pau Espin Pedrol1a026a52019-06-18 17:21:52 +0200335 MGCP.send(t_MGCP_SendToMrf(mrf, r));
Harald Welte9601c812018-01-26 18:58:20 +0100336 }
Harald Weltebb5a1212018-01-26 10:34:44 +0100337 }
338 }
Daniel Willmann166bbb32018-01-17 15:28:04 +0100339 } else {
340 setverdict(fail, "Received unexpected MGCP response: ", mrf.msg.response);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200341 mtc.stop;
Daniel Willmann166bbb32018-01-17 15:28:04 +0100342 }
Daniel Willmannfa870f52018-01-17 12:37:14 +0100343 }
Harald Weltebb5a1212018-01-26 10:34:44 +0100344 [] MGCP_PROC.getcall(MGCPEM_register:{?,?}) -> param(crit, vc_conn) {
Daniel Willmann955627a2018-01-17 15:22:32 +0100345 f_create_expect(crit, vc_conn);
Harald Weltee32ad992018-05-31 22:17:46 +0200346 MGCP_PROC.reply(MGCPEM_register:{crit, vc_conn}) to vc_conn;
Daniel Willmann955627a2018-01-17 15:22:32 +0100347 }
Harald Welte9601c812018-01-26 18:58:20 +0100348 [] MGCP_PROC.getcall(MGCPEM_delete_ep:{?,?}) -> param(ep, vc_conn) {
349 f_ep_table_del(vc_conn, ep);
Harald Weltee32ad992018-05-31 22:17:46 +0200350 MGCP_PROC.reply(MGCPEM_delete_ep:{ep, vc_conn}) to vc_conn;
Harald Welte9601c812018-01-26 18:58:20 +0100351 }
Daniel Willmannfa870f52018-01-17 12:37:14 +0100352 }
Harald Welte9601c812018-01-26 18:58:20 +0100353
Daniel Willmannfa870f52018-01-17 12:37:14 +0100354 }
355}
356
357/* "Expect" Handling */
358
359/* */
Daniel Willmann955627a2018-01-17 15:22:32 +0100360type record ExpectCriteria {
361 MgcpConnectionId connid optional,
362 MgcpEndpoint endpoint optional,
363 MgcpTransId transid optional
Daniel Willmannfa870f52018-01-17 12:37:14 +0100364}
365
366type record ExpectData {
367 ExpectCriteria crit optional,
Daniel Willmann955627a2018-01-17 15:22:32 +0100368 MGCP_ConnHdlr vc_conn
Daniel Willmannfa870f52018-01-17 12:37:14 +0100369}
370
Daniel Willmann955627a2018-01-17 15:22:32 +0100371signature MGCPEM_register(in ExpectCriteria cmd, in MGCP_ConnHdlr hdlr);
Harald Welte9601c812018-01-26 18:58:20 +0100372signature MGCPEM_delete_ep(in MgcpEndpoint ep, in MGCP_ConnHdlr hdlr);
Daniel Willmannfa870f52018-01-17 12:37:14 +0100373
374type port MGCPEM_PROC_PT procedure {
Harald Welte9601c812018-01-26 18:58:20 +0100375 inout MGCPEM_register, MGCPEM_delete_ep;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100376} with { extension "internal" };
377
378function f_get_mgcp_by_crit(ExpectCriteria crit)
379return template MgcpCommand {
Harald Weltebb5a1212018-01-26 10:34:44 +0100380 var template MgcpCommand ret := {
381 line := {
382 verb := ?,
383 trans_id := ?,
384 ep := ?,
385 ver := ?
386 },
387 params := *,
388 sdp := *
389 }
390 if (ispresent(crit.connid)) {
391 ret.params := { *, ts_MgcpParConnectionId(crit.connid), * };
392 }
393 if (ispresent(crit.endpoint)) {
394 ret.line.ep := crit.endpoint;
395 }
396 if (ispresent(crit.transid)) {
397 ret.line.trans_id := crit.transid;
398 }
Daniel Willmannfa870f52018-01-17 12:37:14 +0100399
400 return ret;
401}
402
403/* Function that can be used as create_cb and will usse the expect table */
404function ExpectedCreateCallback(MgcpCommand cmd, charstring id)
405runs on MGCP_Emulation_CT return MGCP_ConnHdlr {
406 var MGCP_ConnHdlr ret := null;
407 var template MgcpCommand mgcpcmd;
408 var integer i;
409
Harald Weltebb5a1212018-01-26 10:34:44 +0100410 for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) {
411 if (not ispresent(MgcpExpectTable[i].crit)) {
Daniel Willmannfa870f52018-01-17 12:37:14 +0100412 continue;
413 }
Daniel Willmann955627a2018-01-17 15:22:32 +0100414 /* FIXME: Ignore criteria for now */
Harald Weltebb5a1212018-01-26 10:34:44 +0100415 mgcpcmd := f_get_mgcp_by_crit(MgcpExpectTable[i].crit);
416 if (match(cmd, mgcpcmd)) {
417 ret := MgcpExpectTable[i].vc_conn;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100418 /* Release this entry */
Harald Weltebb5a1212018-01-26 10:34:44 +0100419 MgcpExpectTable[i].crit := omit;
420 MgcpExpectTable[i].vc_conn := null;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100421 log("Found Expect[", i, "] for ", cmd, " handled at ", ret);
422 return ret;
Harald Weltebb5a1212018-01-26 10:34:44 +0100423 }
Daniel Willmannfa870f52018-01-17 12:37:14 +0100424 }
425 setverdict(fail, "Couldn't find Expect for CRCX", cmd);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200426 mtc.stop;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100427}
428
429private function f_create_expect(ExpectCriteria crit, MGCP_ConnHdlr hdlr)
430runs on MGCP_Emulation_CT {
431 var integer i;
432
433 /* Check an entry like this is not already presnt */
Harald Weltebb5a1212018-01-26 10:34:44 +0100434 for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) {
435 if (crit == MgcpExpectTable[i].crit) {
Daniel Willmannfa870f52018-01-17 12:37:14 +0100436 setverdict(fail, "Crit already present", crit);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200437 mtc.stop;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100438 }
439 }
Harald Weltebb5a1212018-01-26 10:34:44 +0100440 for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) {
441 if (not ispresent(MgcpExpectTable[i].crit)) {
442 MgcpExpectTable[i].crit := crit;
443 MgcpExpectTable[i].vc_conn := hdlr;
Daniel Willmannfa870f52018-01-17 12:37:14 +0100444 log("Created Expect[", i, "] for ", crit, " to be handled at ", hdlr);
445 return;
446 }
447 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200448 testcase.stop("No space left in MgcpExpectTable")
Harald Weltebb5a1212018-01-26 10:34:44 +0100449}
450
451/* client/conn_hdlr side function to use procedure port to create expect in emulation */
452function f_create_mgcp_expect(ExpectCriteria dest_number) runs on MGCP_ConnHdlr {
453 MGCP_PROC.call(MGCPEM_register:{dest_number, self}) {
454 [] MGCP_PROC.getreply(MGCPEM_register:{?,?}) {};
455 }
Daniel Willmannfa870f52018-01-17 12:37:14 +0100456}
457
Harald Welte9601c812018-01-26 18:58:20 +0100458/* client/conn_hdlr side function to use procedure port to create expect in emulation */
459function f_create_mgcp_delete_ep(MgcpEndpoint ep) runs on MGCP_ConnHdlr {
460 MGCP_PROC.call(MGCPEM_delete_ep:{ep, self}) {
461 [] MGCP_PROC.getreply(MGCPEM_delete_ep:{?,?}) {};
462 }
463}
464
465
Daniel Willmann955627a2018-01-17 15:22:32 +0100466private function f_expect_table_init()
467runs on MGCP_Emulation_CT {
468 var integer i;
Harald Weltebb5a1212018-01-26 10:34:44 +0100469 for (i := 0; i < sizeof(MgcpExpectTable); i := i + 1) {
470 MgcpExpectTable[i].crit := omit;
Daniel Willmann955627a2018-01-17 15:22:32 +0100471 }
472}
473
Harald Weltebb5a1212018-01-26 10:34:44 +0100474function DummyUnitdataCallback(MgcpMessage msg)
475runs on MGCP_Emulation_CT return template MgcpMessage {
476 log("Ignoring MGCP ", msg);
477 return omit;
478}
479
Philipp Maier11a58942018-06-25 16:40:48 +0200480/* Determine encoding name for a specified payload type number */
481function f_encoding_name_from_pt(SDP_FIELD_PayloadType pt) return charstring {
482 if (pt == PT_PCMU) {
483 return "PCMU";
484 } else if (pt == PT_GSM) {
485 return "GSM";
486 } else if (pt == PT_PCMA) {
487 return "PCMA";
488 } else if (pt == PT_GSMEFR) {
489 return "GSM-EFR";
490 } else if (pt == PT_GSMHR) {
491 return "GSM-HR-08";
492 } else if (pt == PT_AMR) {
493 return "AMR";
494 } else if (pt == PT_AMRWB) {
495 return "AMR-WB";
496 }
497
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200498 setverdict(fail, "Unknown payload type ", pt);
499 mtc.stop;
Philipp Maier11a58942018-06-25 16:40:48 +0200500}
Harald Weltebb5a1212018-01-26 10:34:44 +0100501
Daniel Willmannfa870f52018-01-17 12:37:14 +0100502}