Make all of the various rtpqos parameters in this branch available from the CHANNEL...
[asterisk/asterisk.git] / channels / sip / dialplan_functions.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*!
18  * \file
19  * \brief sip channel dialplan functions and unit tests
20  */
21
22 #include "asterisk.h"
23
24 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
25
26 #include <math.h>
27
28 #include "asterisk/channel.h"
29 #include "asterisk/rtp_engine.h"
30 #include "asterisk/pbx.h"
31
32 #include "include/sip.h"
33 #include "include/globals.h"
34 #include "include/dialog.h"
35 #include "include/dialplan_functions.h"
36 #include "include/sip_utils.h"
37
38
39 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
40 {
41         struct sip_pvt *p = chan->tech_pvt;
42         char *parse = ast_strdupa(preparse);
43         int res = 0;
44         AST_DECLARE_APP_ARGS(args,
45                 AST_APP_ARG(param);
46                 AST_APP_ARG(type);
47                 AST_APP_ARG(field);
48         );
49         AST_STANDARD_APP_ARGS(args, parse);
50
51         /* Sanity check */
52         if (!IS_SIP_TECH(chan->tech)) {
53                 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
54                 return 0;
55         }
56
57         memset(buf, 0, buflen);
58
59         if (p == NULL) {
60                 return -1;
61         }
62
63         if (!strcasecmp(args.param, "peerip")) {
64                 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", buflen);
65         } else if (!strcasecmp(args.param, "recvip")) {
66                 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", buflen);
67         } else if (!strcasecmp(args.param, "from")) {
68                 ast_copy_string(buf, p->from, buflen);
69         } else if (!strcasecmp(args.param, "uri")) {
70                 ast_copy_string(buf, p->uri, buflen);
71         } else if (!strcasecmp(args.param, "useragent")) {
72                 ast_copy_string(buf, p->useragent, buflen);
73         } else if (!strcasecmp(args.param, "peername")) {
74                 ast_copy_string(buf, p->peername, buflen);
75         } else if (!strcasecmp(args.param, "t38passthrough")) {
76                 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
77         } else if (!strcasecmp(args.param, "rtpdest")) {
78                 struct sockaddr_in sin;
79
80                 if (ast_strlen_zero(args.type))
81                         args.type = "audio";
82
83                 if (!strcasecmp(args.type, "audio"))
84                         ast_rtp_instance_get_remote_address(p->rtp, &sin);
85                 else if (!strcasecmp(args.type, "video"))
86                         ast_rtp_instance_get_remote_address(p->vrtp, &sin);
87                 else if (!strcasecmp(args.type, "text"))
88                         ast_rtp_instance_get_remote_address(p->trtp, &sin);
89                 else
90                         return -1;
91
92                 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
93         } else if (!strcasecmp(args.param, "rtpqos")) {
94                 struct ast_rtp_instance *rtp = NULL;
95
96                 if (ast_strlen_zero(args.type)) {
97                         args.type = "audio";
98                 }
99
100                 if (!strcasecmp(args.type, "audio")) {
101                         rtp = p->rtp;
102                 } else if (!strcasecmp(args.type, "video")) {
103                         rtp = p->vrtp;
104                 } else if (!strcasecmp(args.type, "text")) {
105                         rtp = p->trtp;
106                 } else {
107                         return -1;
108                 }
109
110                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
111                         char quality_buf[AST_MAX_USER_FIELD], *quality;
112
113                         if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
114                                 return -1;
115                         }
116
117                         ast_copy_string(buf, quality_buf, buflen);
118                         return res;
119                 } else {
120                         struct ast_rtp_instance_stats stats;
121                         int i;
122                         struct {
123                                 const char *name;
124                                 enum { INT, DBL } type;
125                                 union {
126                                         unsigned int *i4;
127                                         double *d8;
128                                 };
129                         } lookup[] = {
130                                 { "txcount",               INT, { .i4 = &stats.txcount, }, },
131                                 { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
132                                 { "txjitter",              INT, { .i4 = &stats.txjitter, }, },
133                                 { "rxjitter",              INT, { .i4 = &stats.rxjitter, }, },
134                                 { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
135                                 { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
136                                 { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
137                                 { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
138                                 { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
139                                 { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
140                                 { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
141                                 { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
142                                 { "txploss",               INT, { .i4 = &stats.txploss, }, },
143                                 { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
144                                 { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
145                                 { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
146                                 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
147                                 { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
148                                 { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
149                                 { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
150                                 { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
151                                 { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
152                                 { "rtt",                   INT, { .i4 = &stats.rtt, }, },
153                                 { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
154                                 { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
155                                 { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
156                                 { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
157                                 { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
158                                 { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
159                                 { NULL, },
160                         };
161
162                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
163                                 return -1;
164                         }
165
166                         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
167                                 if (!strcasecmp(args.field, lookup[i].name)) {
168                                         if (lookup[i].type == INT) {
169                                                 snprintf(buf, buflen, "%u", *lookup[i].i4);
170                                         } else {
171                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
172                                         }
173                                         return 0;
174                                 }
175                         }
176                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
177                         return -1;
178                 }
179         } else {
180                 res = -1;
181         }
182         return res;
183 }
184
185 #ifdef TEST_FRAMEWORK
186 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct sockaddr_in *sin, void *data)
187 {
188         /* Needed to pass sanity checks */
189         ast_rtp_instance_set_data(instance, data);
190         return 0;
191 }
192
193 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
194 {
195         /* Needed to pass sanity checks */
196         return 0;
197 }
198
199 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
200 {
201         /* Needed to pass sanity checks */
202         return &ast_null_frame;
203 }
204
205 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
206 {
207         /* Needed to pass sanity checks */
208         return 0;
209 }
210
211 static int test_sip_rtpqos_1_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
212 {
213         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
214         memcpy(stats, s, sizeof(*stats));
215         return 0;
216 }
217
218 AST_TEST_DEFINE(test_sip_rtpqos_1)
219 {
220         int i, res = AST_TEST_PASS;
221         struct ast_rtp_engine test_engine = {
222                 .name = "test",
223                 .new = test_sip_rtpqos_1_new,
224                 .destroy = test_sip_rtpqos_1_destroy,
225                 .read = test_sip_rtpqos_1_read,
226                 .write = test_sip_rtpqos_1_write,
227                 .get_stat = test_sip_rtpqos_1_get_stat,
228         };
229         struct sockaddr_in sin = { .sin_port = 31337, .sin_addr = { 4 * 16777216 + 3 * 65536 + 2 * 256 + 1 } };
230         struct ast_rtp_instance_stats mine = { 0, };
231         struct sip_pvt *p = NULL;
232         struct ast_channel *chan = NULL;
233         struct ast_str *varstr = NULL, *buffer = NULL;
234         struct {
235                 const char *name;
236                 enum { INT, DBL } type;
237                 union {
238                         unsigned int *i4;
239                         double *d8;
240                 };
241         } lookup[] = {
242                 { "txcount",               INT, { .i4 = &mine.txcount, }, },
243                 { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
244                 { "txjitter",              INT, { .i4 = &mine.txjitter, }, },
245                 { "rxjitter",              INT, { .i4 = &mine.rxjitter, }, },
246                 { "remote_maxjitter",      DBL, { .d8 = &mine.remote_maxjitter, }, },
247                 { "remote_minjitter",      DBL, { .d8 = &mine.remote_minjitter, }, },
248                 { "remote_normdevjitter",  DBL, { .d8 = &mine.remote_normdevjitter, }, },
249                 { "remote_stdevjitter",    DBL, { .d8 = &mine.remote_stdevjitter, }, },
250                 { "local_maxjitter",       DBL, { .d8 = &mine.local_maxjitter, }, },
251                 { "local_minjitter",       DBL, { .d8 = &mine.local_minjitter, }, },
252                 { "local_normdevjitter",   DBL, { .d8 = &mine.local_normdevjitter, }, },
253                 { "local_stdevjitter",     DBL, { .d8 = &mine.local_stdevjitter, }, },
254                 { "txploss",               INT, { .i4 = &mine.txploss, }, },
255                 { "rxploss",               INT, { .i4 = &mine.rxploss, }, },
256                 { "remote_maxrxploss",     DBL, { .d8 = &mine.remote_maxrxploss, }, },
257                 { "remote_minrxploss",     DBL, { .d8 = &mine.remote_minrxploss, }, },
258                 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
259                 { "remote_stdevrxploss",   DBL, { .d8 = &mine.remote_stdevrxploss, }, },
260                 { "local_maxrxploss",      DBL, { .d8 = &mine.local_maxrxploss, }, },
261                 { "local_minrxploss",      DBL, { .d8 = &mine.local_minrxploss, }, },
262                 { "local_normdevrxploss",  DBL, { .d8 = &mine.local_normdevrxploss, }, },
263                 { "local_stdevrxploss",    DBL, { .d8 = &mine.local_stdevrxploss, }, },
264                 { "rtt",                   INT, { .i4 = &mine.rtt, }, },
265                 { "maxrtt",                DBL, { .d8 = &mine.maxrtt, }, },
266                 { "minrtt",                DBL, { .d8 = &mine.minrtt, }, },
267                 { "normdevrtt",            DBL, { .d8 = &mine.normdevrtt, }, },
268                 { "stdevrtt",              DBL, { .d8 = &mine.stdevrtt, }, },
269                 { "local_ssrc",            INT, { .i4 = &mine.local_ssrc, }, },
270                 { "remote_ssrc",           INT, { .i4 = &mine.remote_ssrc, }, },
271                 { NULL, },
272         };
273
274         switch (cmd) {
275         case TEST_INIT:
276                 info->name = "test_sip_rtpqos";
277                 info->category = "channels/chan_sip/";
278                 info->summary = "Test retrieval of SIP RTP QOS stats";
279                 info->description =
280                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
281                 return AST_TEST_NOT_RUN;
282         case TEST_EXECUTE:
283                 break;
284         }
285
286         ast_rtp_engine_register2(&test_engine, NULL);
287         /* Have to associate this with a SIP pvt and an ast_channel */
288         if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
289                 res = AST_TEST_NOT_RUN;
290                 goto done;
291         }
292         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
293                 res = AST_TEST_NOT_RUN;
294                 goto done;
295         }
296         ast_rtp_instance_set_remote_address(p->rtp, &sin);
297         if (!(chan = ast_dummy_channel_alloc())) {
298                 res = AST_TEST_NOT_RUN;
299                 goto done;
300         }
301         chan->tech = &sip_tech;
302         chan->tech_pvt = p;
303         p->owner = chan;
304
305         varstr = ast_str_create(16);
306         buffer = ast_str_create(16);
307         if (!varstr || !buffer) {
308                 res = AST_TEST_NOT_RUN;
309                 goto done;
310         }
311
312         /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
313         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
314                 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
315                 if (lookup[i].type == INT) {
316                         int j;
317                         char cmpstr[256];
318                         for (j = 1; j < 25; j++) {
319                                 *lookup[i].i4 = j;
320                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
321                                 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
322                                 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
323                                         res = AST_TEST_FAIL;
324                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
325                                         break;
326                                 }
327                         }
328                 } else {
329                         double j, cmpdbl = 0.0;
330                         for (j = 1.0; j < 10.0; j += 0.3) {
331                                 *lookup[i].d8 = j;
332                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
333                                 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
334                                         res = AST_TEST_FAIL;
335                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
336                                         break;
337                                 }
338                         }
339                 }
340         }
341
342 done:
343         ast_free(varstr);
344         ast_free(buffer);
345
346         /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
347         if (p) {
348                 dialog_unref(p, "Destroy test object");
349         }
350         ast_rtp_engine_unregister(&test_engine);
351         return res;
352 }
353 #endif
354
355 /*! \brief SIP test registration */
356 void sip_dialplan_function_register_tests(void)
357 {
358         AST_TEST_REGISTER(test_sip_rtpqos_1);
359 }
360
361 /*! \brief SIP test registration */
362 void sip_dialplan_function_unregister_tests(void)
363 {
364         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
365 }
366