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