blob: 1dacf3ab48fc0748a8b6ace764129d8fa666da89 [file] [log] [blame]
Harald Welte4029e8c2017-11-23 22:00:42 +01001module MGCP_Templates {
2
3 import from MGCP_Types all;
4 import from SDP_Types all;
5
6 function f_mgcp_par_append(inout template MgcpParameterList list, template MgcpParameter par) {
7 var integer len := lengthof(list);
8 list[len] := par;
9 }
10
11 /* 3.2.2.6 Connection Mode (sendonly, recvonly, sendrecv, confrnce, inactive, loopback,
12 * conttest, netwloop, netwtest) */
13 template MgcpParameter t_MgcpParConnMode(template MgcpConnectionMode mode) := { "M", mode };
14
15 /* 3.2.2.2 CallId: maximum 32 hex chars */
16 template MgcpParameter ts_MgcpParCallId(MgcpCallId cid) := {
17 code := "C",
18 val := hex2str(cid)
19 };
20
21 /* 3.2.2.18 RequestIdentifier: Maximum 32 hex chars */
22 template MgcpParameter ts_MgcpParReqId(MgcpRequestId rid) := {
23 code := "X",
24 val := hex2str(rid)
25 };
26
27 /* 3.2.2.10: LocalConnectionOptions (codec, packetization, bandwidth, ToS, eco, gain, silence, ...) */
28 template MgcpParameter t_MgcpParLocConnOpt(template charstring lco) := { "L", lco };
29
30 /* 3.2.2.5: ConnectionId: maximum 32 hex chars */
31 template MgcpParameter ts_MgcpParConnectionId(MgcpConnectionId cid) := {
32 code := "I",
33 val := hex2str(cid)
34 };
35
36 /* osmo-bsc_mgcp implements L/C/M/X only, osmo-mgw adds 'I' */
37 /* SDP: osmo-bsc_mgcp implements Tx of v,o,s,c,t,m,a */
38
39 template MgcpResponse tr_MgcpResp_Err(template MgcpResponseCode code) := {
40 line := {
41 code := code,
42 trans_id := ?,
43 string := ?
44 },
45 params := {},
46 sdp := omit
47 }
48
49 template MgcpCommandLine t_MgcpCmdLine(template charstring verb, template MgcpTransId trans_id, template charstring ep) := {
50 verb := verb,
51 trans_id := trans_id,
52 ep := ep,
53 ver := "1.0"
54 };
55
56 template MgcpCommand ts_CRCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, template SDP_Message sdp := omit) := {
57 line := t_MgcpCmdLine("CRCX", trans_id, ep),
58 params := {
59 t_MgcpParConnMode(mode),
60 ts_MgcpParCallId(call_id),
61 //t_MgcpParReqId(omit),
62 t_MgcpParLocConnOpt("p:20, a:PCMU")
63 },
64 sdp := sdp
65 }
66
Harald Welte90029572017-11-24 23:39:50 +010067 template MgcpCommand tr_CRCX := {
68 line := t_MgcpCmdLine("CRCX", ?, ?),
69 params := *,
70 sdp := *
71 }
72
Harald Welte4029e8c2017-11-23 22:00:42 +010073 template MgcpResponse tr_CRCX_ACK := {
74 line := {
75 code := "200",
Harald Welte51f34ad2017-11-24 20:40:43 +010076 trans_id := ?,
Harald Welte4029e8c2017-11-23 22:00:42 +010077 string := "OK"
78 },
79 params:= { { "I", ? }, *},
80 sdp := ?
81 }
82
Harald Welte90029572017-11-24 23:39:50 +010083 template MgcpResponse ts_CRCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := {
84 line := {
85 code := "200",
86 trans_id := trans_id,
87 string := "OK"
88 },
89 params:= { ts_MgcpParConnectionId(conn_id) },
90 sdp := sdp
91 }
92
Harald Welte4029e8c2017-11-23 22:00:42 +010093 template MgcpCommand ts_MDCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := {
94 line := t_MgcpCmdLine("MDCX", trans_id, ep),
95 params := {
96 t_MgcpParConnMode(mode),
97 ts_MgcpParCallId(call_id),
98 ts_MgcpParConnectionId(conn_id),
99 //t_MgcpParReqId(omit),
100 t_MgcpParLocConnOpt("p:20, a:PCMU")
101 },
102 sdp := sdp
103 }
104
Harald Welte90029572017-11-24 23:39:50 +0100105 template MgcpCommand tr_MDCX := {
106 line := t_MgcpCmdLine("MDCX", ?, ?),
107 params := *,
108 sdp := *
109 }
110
111 template MgcpResponse ts_MDCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp);
112
Harald Welte4029e8c2017-11-23 22:00:42 +0100113 /* have a function that generates a template, rather than a template in order to handle
114 * optional parameters */
115 function ts_DLCX(MgcpTransId trans_id, charstring ep, template MgcpCallId call_id := omit,
116 template MgcpConnectionId conn_id := omit) return template MgcpCommand {
117 var template MgcpCommand cmd;
118 cmd.line := t_MgcpCmdLine("DLCX", trans_id, ep);
119 cmd.params := {};
120 cmd.sdp := omit;
121 if (isvalue(call_id)) {
122 f_mgcp_par_append(cmd.params, ts_MgcpParCallId(valueof(call_id)));
123 if (isvalue(conn_id)) {
124 f_mgcp_par_append(cmd.params, ts_MgcpParConnectionId(valueof(conn_id)));
125 }
126 }
127 return cmd;
128 }
129
Harald Welte90029572017-11-24 23:39:50 +0100130 template MgcpCommand tr_DLCX := {
131 line := t_MgcpCmdLine("DLCX", ?, ?),
132 params := *,
133 sdp := *
134 }
135
Harald Weltec82eef42017-11-24 20:40:12 +0100136 template MgcpResponse tr_DLCX_ACK := {
137 line := {
138 code := "200",
139 trans_id := ?,
140 string := "OK"
141 },
142 params:= *,
143 sdp := *
144 }
145
Harald Welte90029572017-11-24 23:39:50 +0100146 template MgcpResponse ts_DLCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp);
147
Harald Welte4029e8c2017-11-23 22:00:42 +0100148 /* SDP Templates */
149 template SDP_Origin ts_SDP_origin(charstring addr, charstring session_id,
150 charstring session_version := "1",
151 charstring addr_type := "IP4",
152 charstring user_name := "-") := {
153 user_name := user_name,
154 session_id := session_id,
155 session_version := session_version,
156 net_type := "IN",
157 addr_type := addr_type,
158 addr := addr
159 }
160
161 template SDP_connection ts_SDP_connection_IP(charstring addr, charstring addr_type := "IP4",
162 template integer ttl := omit,
163 template integer num_of_addr := omit) :={
164 net_type := "IN",
165 addr_type := addr_type,
166 conn_addr := {
167 addr := addr,
168 ttl := ttl,
169 num_of_addr := num_of_addr
170 }
171 }
172
173 template SDP_time ts_SDP_time(charstring beg, charstring end) := {
174 time_field := {
175 start_time := beg,
176 stop_time := end
177 },
178 time_repeat := omit
179 }
180
181 template SDP_media_desc ts_SDP_media_desc(integer port_number, SDP_fmt_list fmts,
182 SDP_attribute_list attributes) := {
183 media_field := {
184 media := "audio",
185 ports := {
186 port_number := port_number,
187 num_of_ports := omit
188 },
189 transport := "RTP/AVP",
190 fmts := fmts
191 },
192 information := omit,
193 connections := omit,
194 bandwidth := omit,
195 key := omit,
196 attributes := attributes
197 }
198
199 /* master template for generating SDP based in template arguments */
200 template SDP_Message ts_SDP(charstring local_addr, charstring remote_addr,
201 charstring session_id, charstring session_version,
202 integer rtp_port, SDP_fmt_list fmts,
203 SDP_attribute_list attributes) := {
204 protocol_version := 0,
205 origin := ts_SDP_origin(local_addr, session_id, session_version),
206 session_name := "-",
207 information := omit,
208 uri := omit,
209 emails := omit,
210 phone_numbers := omit,
211 connection := ts_SDP_connection_IP(remote_addr),
212 bandwidth := omit,
213 times := { ts_SDP_time("0","0") },
214 timezone_adjustments := omit,
215 key := omit,
216 attributes := omit,
217 media_list := { ts_SDP_media_desc(rtp_port, fmts, attributes) }
218 }
219
220 template SDP_attribute ts_SDP_rtpmap(integer fmt, charstring val) := {
221 rtpmap := {
222 attr_value := int2str(fmt) & " " & val
223 }
224 }
225 template SDP_attribute ts_SDP_ptime(integer p) := {
226 ptime := {
227 attr_value := int2str(p)
228 }
229 }
230
Harald Welte4c11d562017-11-24 23:39:00 +0100231 function f_MgcpResp_extract_conn_id(MgcpResponse resp) return MgcpConnectionId {
232 var integer i;
233 for (i := 0; i < lengthof(resp.params); i := i + 1) {
234 var MgcpParameter par := resp.params[i];
235 if (par.code == "I") {
236 return str2hex(par.val);
237 }
238 }
239 setverdict(fail);
240 return '00000000'H;
241 }
242
243 function f_mgcp_alloc_tid() return MgcpTransId {
244 return int2str(float2int(rnd()*2147483647.0));
245 }
246
247 function f_mgcp_alloc_call_id() return MgcpCallId {
248 return int2hex(float2int(rnd()*2147483647.0), 8);
249 }
250
251 function f_mgcp_alloc_conn_id() return MgcpConnectionId {
252 return int2hex(float2int(rnd()*2147483647.0), 8);
253 }
254
Harald Welte4029e8c2017-11-23 22:00:42 +0100255}