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