Conversions to ast_debug()
[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         ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
116         return (noise / samples);
117 }
118
119 static int sendnoise(struct ast_channel *chan, int ms) 
120 {
121         int res;
122         res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
123         if (!res) {
124                 res = ast_waitfordigit(chan, ms);
125                 ast_tonepair_stop(chan);
126         }
127         return res;     
128 }
129
130 static int testclient_exec(struct ast_channel *chan, void *data)
131 {
132         struct ast_module_user *u;
133         int res = 0;
134         char *testid=data;
135         char fn[80];
136         char serverver[80];
137         FILE *f;
138         
139         /* Check for test id */
140         if (ast_strlen_zero(testid)) {
141                 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
142                 return -1;
143         }
144         
145         u = ast_module_user_add(chan);
146
147         if (chan->_state != AST_STATE_UP)
148                 res = ast_answer(chan);
149         
150         /* Wait a few just to be sure things get started */
151         res = ast_safe_sleep(chan, 3000);
152         /* Transmit client version */
153         if (!res)
154                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
155         ast_debug(1, "Transmit client version\n");
156         
157         /* Read server version */
158         ast_debug(1, "Read server version\n");
159         if (!res) 
160                 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
161         if (res > 0)
162                 res = 0;
163         ast_debug(1, "server version: %s\n", serverver);
164                 
165         if (res > 0)
166                 res = 0;
167
168         if (!res)
169                 res = ast_safe_sleep(chan, 1000);
170         /* Send test id */
171         if (!res) 
172                 res = ast_dtmf_stream(chan, NULL, testid, 0);           
173         if (!res) 
174                 res = ast_dtmf_stream(chan, NULL, "#", 0);              
175         ast_debug(1, "send test identifier: %s\n", testid);
176
177         if ((res >=0) && (!ast_strlen_zero(testid))) {
178                 /* Make the directory to hold the test results in case it's not there */
179                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
180                 ast_mkdir(fn, 0777);
181                 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
182                 if ((f = fopen(fn, "w+"))) {
183                         setlinebuf(f);
184                         fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
185                         fprintf(f, "CLIENTTEST ID: %s\n", testid);
186                         fprintf(f, "ANSWER:        PASS\n");
187                         res = 0;
188                         
189                         if (!res) {
190                                 /* Step 1: Wait for "1" */
191                                 ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
192                                 res = ast_waitfordigit(chan, 3000);
193                                 fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
194                                 if (res == '1')
195                                         res = 0;
196                                 else
197                                         res = -1;
198                         }
199                         if (!res)
200                                 res = ast_safe_sleep(chan, 1000);
201                         if (!res) {
202                                 /* Step 2: Send "2" */
203                                 ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
204                                 res = ast_dtmf_stream(chan, NULL, "2", 0);
205                                 fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
206                                 if (res > 0)
207                                         res = 0;
208                         }
209                         if (!res) {
210                                 /* Step 3: Wait one second */
211                                 ast_debug(1, "TestClient: 3.  Wait one second\n");
212                                 res = ast_safe_sleep(chan, 1000);
213                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
214                                 if (res > 0)
215                                         res = 0;
216                         }                       
217                         if (!res) {
218                                 /* Step 4: Measure noise */
219                                 ast_debug(1, "TestClient: 4.  Measure noise\n");
220                                 res = measurenoise(chan, 5000, "TestClient");
221                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
222                                 if (res > 0)
223                                         res = 0;
224                         }
225                         if (!res) {
226                                 /* Step 5: Wait for "4" */
227                                 ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
228                                 res = ast_waitfordigit(chan, 3000);
229                                 fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
230                                 if (res == '4')
231                                         res = 0;
232                                 else
233                                         res = -1;
234                         }
235                         if (!res) {
236                                 /* Step 6: Transmit tone noise */
237                                 ast_debug(1, "TestClient: 6.  Transmit tone\n");
238                                 res = sendnoise(chan, 6000);
239                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
240                         }
241                         if (!res || (res == '5')) {
242                                 /* Step 7: Wait for "5" */
243                                 ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
244                                 if (!res)
245                                         res = ast_waitfordigit(chan, 3000);
246                                 fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
247                                 if (res == '5')
248                                         res = 0;
249                                 else
250                                         res = -1;
251                         }
252                         if (!res) {
253                                 /* Step 8: Wait one second */
254                                 ast_debug(1, "TestClient: 8.  Wait one second\n");
255                                 res = ast_safe_sleep(chan, 1000);
256                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
257                                 if (res > 0)
258                                         res = 0;
259                         }
260                         if (!res) {
261                                 /* Step 9: Measure noise */
262                                 ast_debug(1, "TestClient: 6.  Measure tone\n");
263                                 res = measurenoise(chan, 4000, "TestClient");
264                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
265                                 if (res > 0)
266                                         res = 0;
267                         }
268                         if (!res) {
269                                 /* Step 10: Send "7" */
270                                 ast_debug(1, "TestClient: 7.  Send DTMF 7\n");
271                                 res = ast_dtmf_stream(chan, NULL, "7", 0);
272                                 fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
273                                 if (res > 0)
274                                         res =0;
275                         }
276                         if (!res) {
277                                 /* Step 11: Wait for "8" */
278                                 ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
279                                 res = ast_waitfordigit(chan, 3000);
280                                 fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
281                                 if (res == '8')
282                                         res = 0;
283                                 else
284                                         res = -1;
285                         }
286                         if (!res) {
287                                 /* Step 12: Hangup! */
288                                 ast_debug(1, "TestClient: 12.  Hangup\n");
289                         }
290
291                         ast_debug(1, "-- TEST COMPLETE--\n");
292                         fprintf(f, "-- END TEST--\n");
293                         fclose(f);
294                         res = -1;
295                 } else
296                         res = -1;
297         } else {
298                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
299                 res = -1;
300         }
301         ast_module_user_remove(u);
302         return res;
303 }
304
305 static int testserver_exec(struct ast_channel *chan, void *data)
306 {
307         struct ast_module_user *u;
308         int res = 0;
309         char testid[80]="";
310         char fn[80];
311         FILE *f;
312         u = ast_module_user_add(chan);
313         if (chan->_state != AST_STATE_UP)
314                 res = ast_answer(chan);
315         /* Read version */
316         ast_debug(1, "Read client version\n");
317         if (!res) 
318                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
319         if (res > 0)
320                 res = 0;
321
322         ast_debug(1, "client version: %s\n", testid);
323         ast_debug(1, "Transmit server version\n");
324
325         res = ast_safe_sleep(chan, 1000);
326         if (!res)
327                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
328         if (res > 0)
329                 res = 0;
330
331         if (!res) 
332                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);               
333         ast_debug(1, "read test identifier: %s\n", testid);
334         /* Check for sneakyness */
335         if (strchr(testid, '/'))
336                 res = -1;
337         if ((res >=0) && (!ast_strlen_zero(testid))) {
338                 /* Got a Test ID!  Whoo hoo! */
339                 /* Make the directory to hold the test results in case it's not there */
340                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
341                 ast_mkdir(fn, 0777);
342                 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
343                 if ((f = fopen(fn, "w+"))) {
344                         setlinebuf(f);
345                         fprintf(f, "SERVERCHAN:    %s\n", chan->name);
346                         fprintf(f, "SERVERTEST ID: %s\n", testid);
347                         fprintf(f, "ANSWER:        PASS\n");
348                         ast_debug(1, "Processing Test ID '%s'\n", testid);
349                         res = ast_safe_sleep(chan, 1000);
350                         if (!res) {
351                                 /* Step 1: Send "1" */
352                                 ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
353                                 res = ast_dtmf_stream(chan, NULL, "1", 0);
354                                 fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
355                                 if (res > 0)
356                                         res = 0;
357                         }
358                         if (!res) {
359                                 /* Step 2: Wait for "2" */
360                                 ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
361                                 res = ast_waitfordigit(chan, 3000);
362                                 fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
363                                 if (res == '2')
364                                         res = 0;
365                                 else
366                                         res = -1;
367                         }
368                         if (!res) {
369                                 /* Step 3: Measure noise */
370                                 ast_debug(1, "TestServer: 3.  Measure noise\n");
371                                 res = measurenoise(chan, 6000, "TestServer");
372                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
373                                 if (res > 0)
374                                         res = 0;
375                         }
376                         if (!res) {
377                                 /* Step 4: Send "4" */
378                                 ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
379                                 res = ast_dtmf_stream(chan, NULL, "4", 0);
380                                 fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
381                                 if (res > 0)
382                                         res = 0;
383                         }
384                 
385                         if (!res) {
386                                 /* Step 5: Wait one second */
387                                 ast_debug(1, "TestServer: 5.  Wait one second\n");
388                                 res = ast_safe_sleep(chan, 1000);
389                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
390                                 if (res > 0)
391                                         res = 0;
392                         }
393                 
394                         if (!res) {
395                                 /* Step 6: Measure noise */
396                                 ast_debug(1, "TestServer: 6.  Measure tone\n");
397                                 res = measurenoise(chan, 4000, "TestServer");
398                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
399                                 if (res > 0)
400                                         res = 0;
401                         }
402
403                         if (!res) {
404                                 /* Step 7: Send "5" */
405                                 ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
406                                 res = ast_dtmf_stream(chan, NULL, "5", 0);
407                                 fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
408                                 if (res > 0)
409                                         res = 0;
410                         }
411
412                         if (!res) {
413                                 /* Step 8: Transmit tone noise */
414                                 ast_debug(1, "TestServer: 8.  Transmit tone\n");
415                                 res = sendnoise(chan, 6000);
416                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
417                         }
418                 
419                         if (!res || (res == '7')) {
420                                 /* Step 9: Wait for "7" */
421                                 ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
422                                 if (!res)
423                                         res = ast_waitfordigit(chan, 3000);
424                                 fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
425                                 if (res == '7')
426                                         res = 0;
427                                 else
428                                         res = -1;
429                         }
430                         if (!res)
431                                 res = ast_safe_sleep(chan, 1000);
432                         if (!res) {
433                                 /* Step 10: Send "8" */
434                                 ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
435                                 res = ast_dtmf_stream(chan, NULL, "8", 0);
436                                 fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
437                                 if (res > 0)
438                                         res = 0;
439                         }
440                         if (!res) {
441                                 /* Step 11: Wait for hangup to arrive! */
442                                 ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
443                                 res = ast_safe_sleep(chan, 10000);
444                                 fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
445                         }
446
447                         ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
448                         fprintf(f, "-- END TEST--\n");
449                         fclose(f);
450                         res = -1;
451                 } else
452                         res = -1;
453         } else {
454                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
455                 res = -1;
456         }
457         ast_module_user_remove(u);
458         return res;
459 }
460
461 static int unload_module(void)
462 {
463         int res;
464
465         res = ast_unregister_application(testc_app);
466         res |= ast_unregister_application(tests_app);
467
468         ast_module_user_hangup_all();
469
470         return res;     
471 }
472
473 static int load_module(void)
474 {
475         int res;
476
477         res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
478         res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
479
480         return res;
481 }
482
483 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");