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