bug #8076 check option_debug before printing to debug channel.
[asterisk/asterisk.git] / apps / app_test.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * Russell Bryant <russelb@clemson.edu>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Applications to test connection and produce report in text file
23  *
24  * \author Mark Spencer <markster@digium.com>
25  * \author Russell Bryant <russelb@clemson.edu>
26  * 
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/channel.h"
43 #include "asterisk/options.h"
44 #include "asterisk/module.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/app.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/utils.h"
50
51 static char *tests_descrip = 
52          "TestServer(): Perform test server function and write call report.\n"
53          "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
54 static char *tests_app = "TestServer";
55 static char *tests_synopsis = "Execute Interface Test Server";
56
57 static char *testc_descrip = 
58          "TestClient(testid): Executes test client with given testid.\n"
59          "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
60
61 static char *testc_app = "TestClient";
62 static char *testc_synopsis = "Execute Interface Test Client";
63
64 static int measurenoise(struct ast_channel *chan, int ms, char *who)
65 {
66         int res=0;
67         int mssofar;
68         int noise=0;
69         int samples=0;
70         int x;
71         short *foo;
72         struct timeval start;
73         struct ast_frame *f;
74         int rformat;
75         rformat = chan->readformat;
76         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
77                 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
78                 return -1;
79         }
80         start = ast_tvnow();
81         for(;;) {
82                 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
83                 if (mssofar > ms)
84                         break;
85                 res = ast_waitfor(chan, ms - mssofar);
86                 if (res < 1)
87                         break;
88                 f = ast_read(chan);
89                 if (!f) {
90                         res = -1;
91                         break;
92                 }
93                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
94                         foo = (short *)f->data;
95                         for (x=0;x<f->samples;x++) {
96                                 noise += abs(foo[x]);
97                                 samples++;
98                         }
99                 }
100                 ast_frfree(f);
101         }
102
103         if (rformat) {
104                 if (ast_set_read_format(chan, rformat)) {
105                         ast_log(LOG_NOTICE, "Unable to restore original format!\n");
106                         return -1;
107                 }
108         }
109         if (res < 0)
110                 return res;
111         if (!samples) {
112                 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
113                 return -1;
114         }
115         if (option_debug)
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 ast_module_user *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         u = ast_module_user_add(chan);
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         ast_module_user_remove(u);
319         return res;
320 }
321
322 static int testserver_exec(struct ast_channel *chan, void *data)
323 {
324         struct ast_module_user *u;
325         int res = 0;
326         char testid[80]="";
327         char fn[80];
328         FILE *f;
329         u = ast_module_user_add(chan);
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                         if (option_debug)
368                                 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
369                         res = ast_safe_sleep(chan, 1000);
370                         if (!res) {
371                                 /* Step 1: Send "1" */
372                                 if (option_debug) 
373                                         ast_log(LOG_DEBUG, "TestServer: 1.  Send DTMF 1\n");
374                                 res = ast_dtmf_stream(chan, NULL, "1", 0);
375                                 fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
376                                 if (res > 0)
377                                         res = 0;
378                         }
379                         if (!res) {
380                                 /* Step 2: Wait for "2" */
381                                 if (option_debug) 
382                                         ast_log(LOG_DEBUG, "TestServer: 2.  Wait DTMF 2\n");
383                                 res = ast_waitfordigit(chan, 3000);
384                                 fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
385                                 if (res == '2')
386                                         res = 0;
387                                 else
388                                         res = -1;
389                         }
390                         if (!res) {
391                                 /* Step 3: Measure noise */
392                                 if (option_debug) 
393                                         ast_log(LOG_DEBUG, "TestServer: 3.  Measure noise\n");
394                                 res = measurenoise(chan, 6000, "TestServer");
395                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
396                                 if (res > 0)
397                                         res = 0;
398                         }
399                         if (!res) {
400                                 /* Step 4: Send "4" */
401                                 if (option_debug) 
402                                         ast_log(LOG_DEBUG, "TestServer: 4.  Send DTMF 4\n");
403                                 res = ast_dtmf_stream(chan, NULL, "4", 0);
404                                 fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
405                                 if (res > 0)
406                                         res = 0;
407                         }
408                 
409                         if (!res) {
410                                 /* Step 5: Wait one second */
411                                 if (option_debug) 
412                                         ast_log(LOG_DEBUG, "TestServer: 5.  Wait one second\n");
413                                 res = ast_safe_sleep(chan, 1000);
414                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
415                                 if (res > 0)
416                                         res = 0;
417                         }
418                 
419                         if (!res) {
420                                 /* Step 6: Measure noise */
421                                 if (option_debug) 
422                                         ast_log(LOG_DEBUG, "TestServer: 6.  Measure tone\n");
423                                 res = measurenoise(chan, 4000, "TestServer");
424                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
425                                 if (res > 0)
426                                         res = 0;
427                         }
428
429                         if (!res) {
430                                 /* Step 7: Send "5" */
431                                 if (option_debug) 
432                                         ast_log(LOG_DEBUG, "TestServer: 7.  Send DTMF 5\n");
433                                 res = ast_dtmf_stream(chan, NULL, "5", 0);
434                                 fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
435                                 if (res > 0)
436                                         res = 0;
437                         }
438
439                         if (!res) {
440                                 /* Step 8: Transmit tone noise */
441                                 if (option_debug) 
442                                         ast_log(LOG_DEBUG, "TestServer: 8.  Transmit tone\n");
443                                 res = sendnoise(chan, 6000);
444                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
445                         }
446                 
447                         if (!res || (res == '7')) {
448                                 /* Step 9: Wait for "7" */
449                                 if (option_debug) 
450                                         ast_log(LOG_DEBUG, "TestServer: 9.  Wait DTMF 7\n");
451                                 if (!res)
452                                         res = ast_waitfordigit(chan, 3000);
453                                 fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
454                                 if (res == '7')
455                                         res = 0;
456                                 else
457                                         res = -1;
458                         }
459                         if (!res)
460                                 res = ast_safe_sleep(chan, 1000);
461                         if (!res) {
462                                 /* Step 10: Send "8" */
463                                 if (option_debug) 
464                                         ast_log(LOG_DEBUG, "TestServer: 10.  Send DTMF 8\n");
465                                 res = ast_dtmf_stream(chan, NULL, "8", 0);
466                                 fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
467                                 if (res > 0)
468                                         res = 0;
469                         }
470                         if (!res) {
471                                 /* Step 11: Wait for hangup to arrive! */
472                                 if (option_debug) 
473                                         ast_log(LOG_DEBUG, "TestServer: 11.  Waiting for hangup\n");
474                                 res = ast_safe_sleep(chan, 10000);
475                                 fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
476                         }
477
478                         ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
479                         fprintf(f, "-- END TEST--\n");
480                         fclose(f);
481                         res = -1;
482                 } else
483                         res = -1;
484         } else {
485                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
486                 res = -1;
487         }
488         ast_module_user_remove(u);
489         return res;
490 }
491
492 static int unload_module(void)
493 {
494         int res;
495
496         res = ast_unregister_application(testc_app);
497         res |= ast_unregister_application(tests_app);
498
499         ast_module_user_hangup_all();
500
501         return res;     
502 }
503
504 static int load_module(void)
505 {
506         int res;
507
508         res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
509         res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
510
511         return res;
512 }
513
514 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");