- revert change to ast_queue_hangup and create ast_queue_hangup_with_cause
[asterisk/asterisk.git] / apps / app_test.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * Russell Bryant <russelb@clemson.edu>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Applications to test connection and produce report in text file
23  *
24  * \author Mark Spencer <markster@digium.com>
25  * \author Russell Bryant <russelb@clemson.edu>
26  * 
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/stat.h>
35
36 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
37 #include "asterisk/channel.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/app.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/utils.h"
43
44 static char *tests_descrip = 
45          "  TestServer(): Perform test server function and write call report.\n"
46          "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
47 static char *tests_app = "TestServer";
48 static char *tests_synopsis = "Execute Interface Test Server";
49
50 static char *testc_descrip = 
51          "  TestClient(testid): Executes test client with given testid.\n"
52          "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
53
54 static char *testc_app = "TestClient";
55 static char *testc_synopsis = "Execute Interface Test Client";
56
57 static int measurenoise(struct ast_channel *chan, int ms, char *who)
58 {
59         int res=0;
60         int mssofar;
61         int noise=0;
62         int samples=0;
63         int x;
64         short *foo;
65         struct timeval start;
66         struct ast_frame *f;
67         int rformat;
68         rformat = chan->readformat;
69         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
70                 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
71                 return -1;
72         }
73         start = ast_tvnow();
74         for(;;) {
75                 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
76                 if (mssofar > ms)
77                         break;
78                 res = ast_waitfor(chan, ms - mssofar);
79                 if (res < 1)
80                         break;
81                 f = ast_read(chan);
82                 if (!f) {
83                         res = -1;
84                         break;
85                 }
86                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
87                         foo = (short *)f->data.ptr;
88                         for (x=0;x<f->samples;x++) {
89                                 noise += abs(foo[x]);
90                                 samples++;
91                         }
92                 }
93                 ast_frfree(f);
94         }
95
96         if (rformat) {
97                 if (ast_set_read_format(chan, rformat)) {
98                         ast_log(LOG_NOTICE, "Unable to restore original format!\n");
99                         return -1;
100                 }
101         }
102         if (res < 0)
103                 return res;
104         if (!samples) {
105                 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
106                 return -1;
107         }
108         ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
109         return (noise / samples);
110 }
111
112 static int sendnoise(struct ast_channel *chan, int ms) 
113 {
114         int res;
115         res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
116         if (!res) {
117                 res = ast_waitfordigit(chan, ms);
118                 ast_tonepair_stop(chan);
119         }
120         return res;     
121 }
122
123 static int testclient_exec(struct ast_channel *chan, void *data)
124 {
125         int res = 0;
126         char *testid=data;
127         char fn[80];
128         char serverver[80];
129         FILE *f;
130         
131         /* Check for test id */
132         if (ast_strlen_zero(testid)) {
133                 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
134                 return -1;
135         }
136         
137         if (chan->_state != AST_STATE_UP)
138                 res = ast_answer(chan);
139         
140         /* Wait a few just to be sure things get started */
141         res = ast_safe_sleep(chan, 3000);
142         /* Transmit client version */
143         if (!res)
144                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
145         ast_debug(1, "Transmit client version\n");
146         
147         /* Read server version */
148         ast_debug(1, "Read server version\n");
149         if (!res) 
150                 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
151         if (res > 0)
152                 res = 0;
153         ast_debug(1, "server version: %s\n", serverver);
154                 
155         if (res > 0)
156                 res = 0;
157
158         if (!res)
159                 res = ast_safe_sleep(chan, 1000);
160         /* Send test id */
161         if (!res) 
162                 res = ast_dtmf_stream(chan, NULL, testid, 0, 0);                
163         if (!res) 
164                 res = ast_dtmf_stream(chan, NULL, "#", 0, 0);           
165         ast_debug(1, "send test identifier: %s\n", testid);
166
167         if ((res >=0) && (!ast_strlen_zero(testid))) {
168                 /* Make the directory to hold the test results in case it's not there */
169                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
170                 ast_mkdir(fn, 0777);
171                 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
172                 if ((f = fopen(fn, "w+"))) {
173                         setlinebuf(f);
174                         fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
175                         fprintf(f, "CLIENTTEST ID: %s\n", testid);
176                         fprintf(f, "ANSWER:        PASS\n");
177                         res = 0;
178                         
179                         if (!res) {
180                                 /* Step 1: Wait for "1" */
181                                 ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
182                                 res = ast_waitfordigit(chan, 3000);
183                                 fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
184                                 if (res == '1')
185                                         res = 0;
186                                 else
187                                         res = -1;
188                         }
189                         if (!res)
190                                 res = ast_safe_sleep(chan, 1000);
191                         if (!res) {
192                                 /* Step 2: Send "2" */
193                                 ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
194                                 res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
195                                 fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
196                                 if (res > 0)
197                                         res = 0;
198                         }
199                         if (!res) {
200                                 /* Step 3: Wait one second */
201                                 ast_debug(1, "TestClient: 3.  Wait one second\n");
202                                 res = ast_safe_sleep(chan, 1000);
203                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
204                                 if (res > 0)
205                                         res = 0;
206                         }                       
207                         if (!res) {
208                                 /* Step 4: Measure noise */
209                                 ast_debug(1, "TestClient: 4.  Measure noise\n");
210                                 res = measurenoise(chan, 5000, "TestClient");
211                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
212                                 if (res > 0)
213                                         res = 0;
214                         }
215                         if (!res) {
216                                 /* Step 5: Wait for "4" */
217                                 ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
218                                 res = ast_waitfordigit(chan, 3000);
219                                 fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
220                                 if (res == '4')
221                                         res = 0;
222                                 else
223                                         res = -1;
224                         }
225                         if (!res) {
226                                 /* Step 6: Transmit tone noise */
227                                 ast_debug(1, "TestClient: 6.  Transmit tone\n");
228                                 res = sendnoise(chan, 6000);
229                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
230                         }
231                         if (!res || (res == '5')) {
232                                 /* Step 7: Wait for "5" */
233                                 ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
234                                 if (!res)
235                                         res = ast_waitfordigit(chan, 3000);
236                                 fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
237                                 if (res == '5')
238                                         res = 0;
239                                 else
240                                         res = -1;
241                         }
242                         if (!res) {
243                                 /* Step 8: Wait one second */
244                                 ast_debug(1, "TestClient: 8.  Wait one second\n");
245                                 res = ast_safe_sleep(chan, 1000);
246                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
247                                 if (res > 0)
248                                         res = 0;
249                         }
250                         if (!res) {
251                                 /* Step 9: Measure noise */
252                                 ast_debug(1, "TestClient: 6.  Measure tone\n");
253                                 res = measurenoise(chan, 4000, "TestClient");
254                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
255                                 if (res > 0)
256                                         res = 0;
257                         }
258                         if (!res) {
259                                 /* Step 10: Send "7" */
260                                 ast_debug(1, "TestClient: 7.  Send DTMF 7\n");
261                                 res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
262                                 fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
263                                 if (res > 0)
264                                         res =0;
265                         }
266                         if (!res) {
267                                 /* Step 11: Wait for "8" */
268                                 ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
269                                 res = ast_waitfordigit(chan, 3000);
270                                 fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
271                                 if (res == '8')
272                                         res = 0;
273                                 else
274                                         res = -1;
275                         }
276                         if (!res) {
277                                 /* Step 12: Hangup! */
278                                 ast_debug(1, "TestClient: 12.  Hangup\n");
279                         }
280
281                         ast_debug(1, "-- TEST COMPLETE--\n");
282                         fprintf(f, "-- END TEST--\n");
283                         fclose(f);
284                         res = -1;
285                 } else
286                         res = -1;
287         } else {
288                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
289                 res = -1;
290         }
291         return res;
292 }
293
294 static int testserver_exec(struct ast_channel *chan, void *data)
295 {
296         int res = 0;
297         char testid[80]="";
298         char fn[80];
299         FILE *f;
300         if (chan->_state != AST_STATE_UP)
301                 res = ast_answer(chan);
302         /* Read version */
303         ast_debug(1, "Read client version\n");
304         if (!res) 
305                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
306         if (res > 0)
307                 res = 0;
308
309         ast_debug(1, "client version: %s\n", testid);
310         ast_debug(1, "Transmit server version\n");
311
312         res = ast_safe_sleep(chan, 1000);
313         if (!res)
314                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
315         if (res > 0)
316                 res = 0;
317
318         if (!res) 
319                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);               
320         ast_debug(1, "read test identifier: %s\n", testid);
321         /* Check for sneakyness */
322         if (strchr(testid, '/'))
323                 res = -1;
324         if ((res >=0) && (!ast_strlen_zero(testid))) {
325                 /* Got a Test ID!  Whoo hoo! */
326                 /* Make the directory to hold the test results in case it's not there */
327                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
328                 ast_mkdir(fn, 0777);
329                 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
330                 if ((f = fopen(fn, "w+"))) {
331                         setlinebuf(f);
332                         fprintf(f, "SERVERCHAN:    %s\n", chan->name);
333                         fprintf(f, "SERVERTEST ID: %s\n", testid);
334                         fprintf(f, "ANSWER:        PASS\n");
335                         ast_debug(1, "Processing Test ID '%s'\n", testid);
336                         res = ast_safe_sleep(chan, 1000);
337                         if (!res) {
338                                 /* Step 1: Send "1" */
339                                 ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
340                                 res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
341                                 fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
342                                 if (res > 0)
343                                         res = 0;
344                         }
345                         if (!res) {
346                                 /* Step 2: Wait for "2" */
347                                 ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
348                                 res = ast_waitfordigit(chan, 3000);
349                                 fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
350                                 if (res == '2')
351                                         res = 0;
352                                 else
353                                         res = -1;
354                         }
355                         if (!res) {
356                                 /* Step 3: Measure noise */
357                                 ast_debug(1, "TestServer: 3.  Measure noise\n");
358                                 res = measurenoise(chan, 6000, "TestServer");
359                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
360                                 if (res > 0)
361                                         res = 0;
362                         }
363                         if (!res) {
364                                 /* Step 4: Send "4" */
365                                 ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
366                                 res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
367                                 fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
368                                 if (res > 0)
369                                         res = 0;
370                         }
371                 
372                         if (!res) {
373                                 /* Step 5: Wait one second */
374                                 ast_debug(1, "TestServer: 5.  Wait one second\n");
375                                 res = ast_safe_sleep(chan, 1000);
376                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
377                                 if (res > 0)
378                                         res = 0;
379                         }
380                 
381                         if (!res) {
382                                 /* Step 6: Measure noise */
383                                 ast_debug(1, "TestServer: 6.  Measure tone\n");
384                                 res = measurenoise(chan, 4000, "TestServer");
385                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
386                                 if (res > 0)
387                                         res = 0;
388                         }
389
390                         if (!res) {
391                                 /* Step 7: Send "5" */
392                                 ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
393                                 res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
394                                 fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
395                                 if (res > 0)
396                                         res = 0;
397                         }
398
399                         if (!res) {
400                                 /* Step 8: Transmit tone noise */
401                                 ast_debug(1, "TestServer: 8.  Transmit tone\n");
402                                 res = sendnoise(chan, 6000);
403                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
404                         }
405                 
406                         if (!res || (res == '7')) {
407                                 /* Step 9: Wait for "7" */
408                                 ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
409                                 if (!res)
410                                         res = ast_waitfordigit(chan, 3000);
411                                 fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
412                                 if (res == '7')
413                                         res = 0;
414                                 else
415                                         res = -1;
416                         }
417                         if (!res)
418                                 res = ast_safe_sleep(chan, 1000);
419                         if (!res) {
420                                 /* Step 10: Send "8" */
421                                 ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
422                                 res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
423                                 fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
424                                 if (res > 0)
425                                         res = 0;
426                         }
427                         if (!res) {
428                                 /* Step 11: Wait for hangup to arrive! */
429                                 ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
430                                 res = ast_safe_sleep(chan, 10000);
431                                 fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
432                         }
433
434                         ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
435                         fprintf(f, "-- END TEST--\n");
436                         fclose(f);
437                         res = -1;
438                 } else
439                         res = -1;
440         } else {
441                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
442                 res = -1;
443         }
444         return res;
445 }
446
447 static int unload_module(void)
448 {
449         int res;
450
451         res = ast_unregister_application(testc_app);
452         res |= ast_unregister_application(tests_app);
453
454         return res;     
455 }
456
457 static int load_module(void)
458 {
459         int res;
460
461         res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
462         res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
463
464         return res;
465 }
466
467 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");