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