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