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