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