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