aboutsummaryrefslogtreecommitdiffstats
path: root/asterisk/AMI_Functions.ttcn
blob: 9fbe9731356b9e6b8f2202efd3fa41b9d44bc5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* Asterisk's AMI interface functions in TTCN-3
 * (C) 2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * https://docs.asterisk.org/Configuration/Interfaces/Asterisk-Manager-Interface-AMI/AMI-v2-Specification/
 */
module AMI_Functions {

import from Misc_Helpers all;
import from Osmocom_Types all;
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
import from Socket_API_Definitions all;
import from TCCConversion_Functions all;

modulepar {
	float mp_ami_prompt_timeout := 10.0;
}

const charstring AMI_FIELD_ACTION := "Action";
const charstring AMI_FIELD_ACTION_ID := "ActionID";
const charstring AMI_FIELD_EVENT := "Event";
const charstring AMI_FIELD_USERNAME := "Username";
const charstring AMI_FIELD_SECRET := "Secret";
const charstring AMI_FIELD_RESPONSE := "Response";

/* Extensions: */
const charstring AMI_FIELD_REGISTRATION := "Registration";

type record AMI_Field {
	charstring	key,
	charstring	val
} with {
	encode "TEXT"
	variant "SEPARATOR(': ', ':\s+')"
};

type set of AMI_Field AMI_Msg with {
	encode "TEXT"
	variant "SEPARATOR('\r\n', '(\r\n)|[\n]')"
	variant "END('\r\n', '(\r\n)|[\n]')"
};

external function enc_AMI_Msg(in AMI_Msg msg) return charstring
	with { extension "prototype(convert) encode(TEXT)" }
external function dec_AMI_Msg(in charstring stream) return AMI_Msg
	with { extension "prototype(convert) decode(TEXT)" }

template (value) AMI_Field
ts_AMI_Field(template (value) charstring key,
	     template (value) charstring val) := {
	key := key,
	val := val
};

template (present) AMI_Field
tr_AMI_Field(template (present) charstring key := ?,
	     template (present) charstring val := ?) := {
	key := key,
	val := val
};

/*
 * Field Templates:
 */

template (value) AMI_Field
ts_AMI_Field_Action(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION, val);
template (value) AMI_Field
ts_AMI_Field_ActionId(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION_ID, val);
template (value) AMI_Field
ts_AMI_Field_Event(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_EVENT, val);
template (value) AMI_Field
ts_AMI_Field_Username(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_USERNAME, val);
template (value) AMI_Field
ts_AMI_Field_Secret(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_SECRET, val);
/* Extensions: */
template (value) AMI_Field
ts_AMI_Field_Registration(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_REGISTRATION, val);

template (present) AMI_Field
tr_AMI_Field_Action(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_ACTION, val);
template (present) AMI_Field
tr_AMI_Field_ActionId(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_ACTION_ID, val);
template (present) AMI_Field
tr_AMI_Field_Event(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_EVENT, val);
template (present) AMI_Field
tr_AMI_Field_Username(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_USERNAME, val);
template (present) AMI_Field
tr_AMI_Field_Secret(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_SECRET, val);
template (present) AMI_Field
tr_AMI_Field_Response(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_RESPONSE, val);
/* Extensions: */
template (present) AMI_Field
tr_AMI_Field_Registration(template (present) charstring val := ?) := tr_AMI_Field(pattern @nocase AMI_FIELD_REGISTRATION, val);


template (present) AMI_Field
tr_AMI_Field_ResponseSuccess := tr_AMI_Field(pattern @nocase AMI_FIELD_RESPONSE, "Success");


/***********************
 * Message Templates:
 ***********************/

/*
 * ACTIONS
 */

/* Action: Login
 * Username: <value>
 * Secret: <value>
 */
template (value) AMI_Msg
ts_AMI_Action_Login(charstring username,
		    charstring secret,
		    template (value) charstring action_id := "0001") := {
	ts_AMI_Field_Action("Login"),
	ts_AMI_Field_ActionId(action_id),
	ts_AMI_Field_Username(username),
	ts_AMI_Field_Secret(secret)
};

template (present) AMI_Msg
tr_AMI_Action_Login(template(present) charstring username := ?,
		    template(present) charstring secret := ?,
		    template (present) charstring action_id := ?) := superset(
	tr_AMI_Field_Action("Login"),
	tr_AMI_Field_ActionId(action_id),
	tr_AMI_Field_Username(username),
	tr_AMI_Field_Secret(secret)
);

/* Action: PJSIPRegister
 * ActionID: <value>
 * Registration: volte_ims
 */
template (value) AMI_Msg
ts_AMI_Action_PJSIPRegister(template (value) charstring registration := "volte_ims",
			    template (value) charstring action_id := "0001") := {
	ts_AMI_Field_Action("PJSIPRegister"),
	ts_AMI_Field_ActionId(action_id),
	ts_AMI_Field_Registration(registration)
};
template (present) AMI_Msg
tr_AMI_Action_PJSIPRegister(template (present) charstring registration := ?,
			    template (present) charstring action_id := ?) := {
	tr_AMI_Field_Action("PJSIPRegister"),
	tr_AMI_Field_ActionId(action_id),
	tr_AMI_Field_Registration(registration)
};

/*
 * RESPONSES
 */

/* Response: Success
 */
template (present) AMI_Msg
tr_AMI_Response_Success := superset(
	tr_AMI_Field_ResponseSuccess
);

/* Response: Success
 * ActionId: <value>
 */
template (present) AMI_Msg
tr_AMI_Response_Success_ActionId(template (present) charstring action_id := ?) := superset(
	tr_AMI_Field_ResponseSuccess,
	tr_AMI_Field_ActionId(action_id)
);

/*
 * EVENTS
 */
template (present) AMI_Msg
tr_AMI_Event(template (present) charstring ev_name := ?) := superset(
	tr_AMI_Field_Event(ev_name)
);

/* Event: FullyBooted
 * Privilege: system,all
 * Status: Fully Booted
 * Uptime: 4
 * LastReload: 4 *
 */
template (present) AMI_Msg
tr_AMI_Event_FullyBooted := tr_AMI_Event("FullyBooted");

/***********************
 * Adapter:
 ***********************/

type record AMI_Adapter_Parameters {
	charstring			remote_host,
	IPL4asp_Types.PortNumber	remote_port,
	charstring			local_host,
	IPL4asp_Types.PortNumber	local_port,
	charstring			welcome_str
}

const AMI_Adapter_Parameters c_default_AMI_Adapter_pars := {
	remote_host := "127.0.0.1",
	remote_port := 5038,
	local_host := "0.0.0.0",
	local_port := 0,
	welcome_str := "Asterisk Call Manager/9.0.0\r\n"
};

type port AMI_Msg_PT message {
	inout AMI_Msg;
} with { extension "internal" };

type component AMI_Adapter_CT {
	port IPL4asp_PT IPL4;
	port AMI_Msg_PT CLIENT;
	var AMI_Adapter_Parameters g_pars;

	/* Connection identifier of the client itself */
	var IPL4asp_Types.ConnectionId g_self_conn_id := -1;
}

/* Function to use to connect as client to a remote IPA Server */
private function f_AMI_Adapter_connect() runs on AMI_Adapter_CT {
	var IPL4asp_Types.Result res;
	map(self:IPL4, system:IPL4);
	res := IPL4asp_PortType.f_IPL4_connect(IPL4, g_pars.remote_host, g_pars.remote_port,
					       g_pars.local_host, g_pars.local_port, 0, { tcp:={} });
	if (not ispresent(res.connId)) {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
			log2str("Could not connect AMI socket from ", g_pars.local_host, " port ",
				g_pars.local_port, " to ", g_pars.remote_host, " port ", g_pars.remote_port,
				"; check your configuration"));
	}
	g_self_conn_id := res.connId;
	log("AMI connected, ConnId=", g_self_conn_id)
}

private function f_ASP_RecvFrom_msg_to_charstring(ASP_RecvFrom rx_rf) return charstring {
	return oct2char(rx_rf.msg);
}

/* Function to use to connect as client to a remote IPA Server */
private function f_AMI_Adapter_wait_rx_welcome_str() runs on AMI_Adapter_CT {
	var ASP_RecvFrom rx_rf;
	var charstring rx_str;
	timer Twelcome := 3.0;

	Twelcome.start;
	alt {
	[] IPL4.receive(ASP_RecvFrom:?) -> value rx_rf {
		rx_str := f_ASP_RecvFrom_msg_to_charstring(rx_rf);
		if (g_pars.welcome_str != rx_str) {
					Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("AMI Welcome message mismatch: '", rx_str,
						"' vs exp '", g_pars.welcome_str, "'"));
		}
	}
	[] Twelcome.timeout {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("AMI Welcome timeout"));
	}
	}
	Twelcome.stop;
	log("AMI Welcome message received: '", rx_str, "'");
}

private function dec_AMI_Msg_ext(charstring txt) return AMI_Msg {
	log("AMI dec: '", txt, "'");
	/* TEXT Enc/dec is not happy with empty values, workaround it: */
	var charstring patched_txt := f_str_replace(txt, "Challenge: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "AccountCode: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "Value: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "DestExten: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "Exten: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "Extension: \r\n", "");
	patched_txt := f_str_replace(patched_txt, "Hint: \r\n", "");

	/* "AppData" field sometimes has a value containing separator ": ", which makes
	 * TEXT dec not happy. Workaround it for now by removing the whole field line:
	 * "AppData: 5,0502: Call pjsip endpoint from 0501\r\n"
	 */
	var integer pos := f_strstr(patched_txt, "AppData: ", 0);
	if (pos >= 0) {
		var integer pos_end := f_strstr(patched_txt, "\r\n", pos) + lengthof("\r\n");
		var charstring to_remove := substr(patched_txt, pos, pos_end - pos);
		patched_txt := f_str_replace(patched_txt, to_remove, "");
	}

	log("AMI patched dec: '", patched_txt, "'");
	return dec_AMI_Msg(patched_txt);
}

function f_AMI_Adapter_main(AMI_Adapter_Parameters pars := c_default_AMI_Adapter_pars)
		runs on AMI_Adapter_CT {
	var AMI_Msg msg;
	var charstring rx, buf := "";
	var integer fd;
	var ASP_RecvFrom rx_rf;
	var ASP_Event rx_ev;

	g_pars := pars;

	f_AMI_Adapter_connect();

	f_AMI_Adapter_wait_rx_welcome_str();

	while (true) {

		alt {
		[] IPL4.receive(ASP_RecvFrom:?) -> value rx_rf {
			var charstring rx_str := f_ASP_RecvFrom_msg_to_charstring(rx_rf);
			log("AMI rx: '", rx_str, "'");
			buf := buf & rx_str;
			log("AMI buf: '", buf, "'");

			/* If several messages come together */
			var boolean last_is_complete := f_str_endswith(buf, "\r\n\r\n");
			var Misc_Helpers.ro_charstring msgs := f_str_split(buf, "\r\n\r\n");
			log("AMI split: ", msgs);
			if (lengthof(msgs) > 0) {
				for (var integer i := 0; i < lengthof(msgs) - 1; i := i + 1) {
					var charstring txt := msgs[i] & "\r\n";
					msg := dec_AMI_Msg_ext(txt);
					CLIENT.send(msg);
				}
				if (last_is_complete) {
					var charstring txt := msgs[lengthof(msgs) - 1] & "\r\n";
					msg := dec_AMI_Msg_ext(txt);
					CLIENT.send(msg);
					buf := "";
				} else {
					buf := msgs[lengthof(msgs) - 1];
				}
			}
			log("AMI remain buf: '", buf, "'");
			}
		[] IPL4.receive(ASP_ConnId_ReadyToRelease:?) {
			}

		[] IPL4.receive(ASP_Event:?) -> value rx_ev {
			log("Rx AMI ASP_Event: ", rx_ev);
			}
		[] CLIENT.receive(AMI_Msg:?) -> value msg {
			/* TODO: in the future, queue Action if there's already one Action in transit, to fullfill AMI requirements. */
			var charstring tx_txt := enc_AMI_Msg(msg) & "\r\n";

			var ASP_SendTo tx := {
				connId := g_self_conn_id,
				remName := g_pars.remote_host,
				remPort := g_pars.remote_port,
				proto := { tcp := {} },
				msg := char2oct(tx_txt)
			};
			IPL4.send(tx);
			}
		}
	}
}


/*
 * Functions:
 */

/* Generate a random "ActionId" value: */
function f_gen_action_id() return charstring {
	return hex2str(f_rnd_hexstring(16));
}

function f_ami_msg_find(AMI_Msg msg,
			template (present) charstring key := ?)
return template (omit) AMI_Field {
	var integer i;

	for (i := 0; i < lengthof(msg); i := i + 1) {
		if (not ispresent(msg[i])) {
			continue;
		}
		if (match(msg[i].key, key)) {
			return msg[i];
		}
	}
	return omit;
}

function f_ami_msg_find_or_fail(AMI_Msg msg,
				template (present) charstring key := ?)
return AMI_Field {
	var template (omit) AMI_Field field;
	field := f_ami_msg_find(msg, key);
	if (istemplatekind(field, "omit")) {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
			log2str("Key ", key, " not found in ", msg));
	}
	return valueof(field);
}

function f_ami_msg_get_value(AMI_Msg msg,
			     template (present) charstring key := ?)
return template (omit) charstring {
	var template (omit) AMI_Field field;
	field := f_ami_msg_find(msg, key);
	if (istemplatekind(field, "omit")) {
		return omit;
	}
	return field.val;
}

function f_ami_msg_get_value_or_fail(AMI_Msg msg,
				     template (present) charstring key := ?)
return template charstring {
	var AMI_Field field;
	field := f_ami_msg_find_or_fail(msg, key);
	return field.val;
}

function f_ami_transceive_ret(AMI_Msg_PT pt, template (value) AMI_Msg tx_msg, float rx_timeout := 10.0) return AMI_Msg {
	var AMI_Msg rx_msg;
	timer T;

	T.start(rx_timeout);
	pt.send(tx_msg);
	alt {
	[] pt.receive(AMI_Msg:?) -> value rx_msg;
	[] T.timeout {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("AMI Response timeout: ", tx_msg));
		}
	}
	T.stop;
	return rx_msg;

}

altstep as_ami_rx_ignore(AMI_Msg_PT pt)
{
	var AMI_Msg msg;
	[] pt.receive(AMI_Msg:?) -> value msg {
		log("Ignoring AMI message := ", msg);
		repeat;
	}
}

private altstep as_ami_rx_fail(AMI_Msg_PT pt, template AMI_Msg exp_msg := *)
{
	var AMI_Msg msg;
	[] pt.receive(AMI_Msg:?) -> value msg {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("Received unexpected AMI message := ", msg, "\nvs exp := ", exp_msg));
	}
}

altstep as_ami_expect_msg(AMI_Msg_PT pt, template (present) AMI_Msg msg_expect, boolean fail_others := true)
{
	[] pt.receive(msg_expect);
	[fail_others] as_ami_rx_fail(pt, msg_expect);
}

function f_ami_transceive_match(AMI_Msg_PT pt,
				template (value) AMI_Msg tx_msg,
				template (present) AMI_Msg exp_ret := ?,
				boolean fail_others := true,
				float rx_timeout := 10.0) return AMI_Msg {
	var AMI_Msg rx_msg;
	timer T;

	T.start(rx_timeout);
	pt.send(tx_msg);
	alt {
	[] pt.receive(exp_ret) -> value rx_msg;
	[not fail_others] pt.receive(AMI_Msg:?) -> value rx_msg {
		log("AMI: Ignoring Rx msg ", rx_msg);
		repeat;
	}
	[fail_others] as_ami_rx_fail(pt, exp_ret);
	[] T.timeout {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("AMI Response timeout: ", tx_msg));
		}
	}
	T.stop;
	return rx_msg;
}

function f_ami_transceive_match_response_success(AMI_Msg_PT pt,
						 template (value) AMI_Msg tx_msg) {
	var template (present) AMI_Msg exp_resp;
	var template (omit) charstring action_id := f_ami_msg_get_value(valueof(tx_msg), AMI_FIELD_ACTION_ID);
	if (isvalue(action_id)) {
		exp_resp := tr_AMI_Response_Success_ActionId(action_id);
	} else {
		exp_resp := tr_AMI_Response_Success;
	}
	f_ami_transceive_match(pt, tx_msg, exp_resp);
}

function f_ami_action_login(AMI_Msg_PT pt, charstring username, charstring secret) {
	var charstring reg_action_id := f_gen_action_id();
	f_ami_transceive_match_response_success(pt, ts_AMI_Action_Login(username, secret, reg_action_id));
}

function f_ami_action_PJSIPRegister(AMI_Msg_PT pt, charstring register) {
	var charstring reg_action_id := f_gen_action_id();
	f_ami_transceive_match_response_success(pt, ts_AMI_Action_PJSIPRegister(register, reg_action_id));
}

private function f_ami_selftest_decode(charstring txt) {
	log("Text to decode: '", txt, "'");
	var AMI_Msg msg := dec_AMI_Msg(txt);
	log("AMI_Msg decoded: ", msg);
}

function f_ami_selftest() {
	f_ami_selftest_decode("AppData: 5,0502: Call pjsip endpoint from 0501\r\n");
}

}