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