make app_queue 1.2 jump compliant (issue #5580)
[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  * \ingroup applications
25  */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/channel.h"
40 #include "asterisk/options.h"
41 #include "asterisk/module.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/app.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/utils.h"
47
48 STANDARD_LOCAL_USER;
49
50 LOCAL_USER_DECL;
51
52 static char *tdesc = "Interface Test Application";
53
54 static char *tests_descrip = 
55          "TestServer(): Perform test server function and write call report.\n"
56          "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
57 static char *tests_app = "TestServer";
58 static char *tests_synopsis = "Execute Interface Test Server";
59
60 static char *testc_descrip = 
61          "TestClient(testid): Executes test client with given testid.\n"
62          "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
63
64 static char *testc_app = "TestClient";
65 static char *testc_synopsis = "Execute Interface Test Client";
66
67 static int measurenoise(struct ast_channel *chan, int ms, char *who)
68 {
69         int res=0;
70         int mssofar;
71         int noise=0;
72         int samples=0;
73         int x;
74         short *foo;
75         struct timeval start;
76         struct ast_frame *f;
77         int rformat;
78         rformat = chan->readformat;
79         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
80                 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
81                 return -1;
82         }
83         start = ast_tvnow();
84         for(;;) {
85                 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
86                 if (mssofar > ms)
87                         break;
88                 res = ast_waitfor(chan, ms - mssofar);
89                 if (res < 1)
90                         break;
91                 f = ast_read(chan);
92                 if (!f) {
93                         res = -1;
94                         break;
95                 }
96                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
97                         foo = (short *)f->data;
98                         for (x=0;x<f->samples;x++) {
99                                 noise += abs(foo[x]);
100                                 samples++;
101                         }
102                 }
103         }
104
105         if (rformat) {
106                 if (ast_set_read_format(chan, rformat)) {
107                         ast_log(LOG_NOTICE, "Unable to restore original format!\n");
108                         return -1;
109                 }
110         }
111         if (res < 0)
112                 return res;
113         if (!samples) {
114                 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
115                 return -1;
116         }
117         ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
118         return (noise / samples);
119 }
120
121 static int sendnoise(struct ast_channel *chan, int ms) 
122 {
123         int res;
124         res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
125         if (!res) {
126                 res = ast_waitfordigit(chan, ms);
127                 ast_tonepair_stop(chan);
128         }
129         return res;     
130 }
131
132 static int testclient_exec(struct ast_channel *chan, void *data)
133 {
134         struct localuser *u;
135         int res = 0;
136         char *testid=data;
137         char fn[80];
138         char serverver[80];
139         FILE *f;
140         
141         /* Check for test id */
142         if (ast_strlen_zero(testid)) {
143                 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
144                 return -1;
145         }
146         
147         LOCAL_USER_ADD(u);
148
149         if (chan->_state != AST_STATE_UP)
150                 res = ast_answer(chan);
151         
152         /* Wait a few just to be sure things get started */
153         res = ast_safe_sleep(chan, 3000);
154         /* Transmit client version */
155         if (!res)
156                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
157         if (option_debug)
158                 ast_log(LOG_DEBUG, "Transmit client version\n");
159         
160         /* Read server version */
161         if (option_debug)
162                 ast_log(LOG_DEBUG, "Read server version\n");
163         if (!res) 
164                 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
165         if (res > 0)
166                 res = 0;
167         if (option_debug)
168                 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
169                 
170         if (res > 0)
171                 res = 0;
172
173         if (!res)
174                 res = ast_safe_sleep(chan, 1000);
175         /* Send test id */
176         if (!res) 
177                 res = ast_dtmf_stream(chan, NULL, testid, 0);           
178         if (!res) 
179                 res = ast_dtmf_stream(chan, NULL, "#", 0);              
180         if (option_debug)
181                 ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
182
183         if ((res >=0) && (!ast_strlen_zero(testid))) {
184                 /* Make the directory to hold the test results in case it's not there */
185                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
186                 mkdir(fn, 0777);
187                 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
188                 if ((f = fopen(fn, "w+"))) {
189                         setlinebuf(f);
190                         fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
191                         fprintf(f, "CLIENTTEST ID: %s\n", testid);
192                         fprintf(f, "ANSWER:        PASS\n");
193                         res = 0;
194                         
195                         if (!res) {
196                                 /* Step 1: Wait for "1" */
197                                 if (option_debug)
198                                         ast_log(LOG_DEBUG, "TestClient: 2.  Wait DTMF 1\n");
199                                 res = ast_waitfordigit(chan, 3000);
200                                 fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
201                                 if (res == '1')
202                                         res = 0;
203                                 else
204                                         res = -1;
205                         }
206                         if (!res)
207                                 res = ast_safe_sleep(chan, 1000);
208                         if (!res) {
209                                 /* Step 2: Send "2" */
210                                 if (option_debug)
211                                         ast_log(LOG_DEBUG, "TestClient: 2.  Send DTMF 2\n");
212                                 res = ast_dtmf_stream(chan, NULL, "2", 0);
213                                 fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
214                                 if (res > 0)
215                                         res = 0;
216                         }
217                         if (!res) {
218                                 /* Step 3: Wait one second */
219                                 if (option_debug)
220                                         ast_log(LOG_DEBUG, "TestClient: 3.  Wait one second\n");
221                                 res = ast_safe_sleep(chan, 1000);
222                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
223                                 if (res > 0)
224                                         res = 0;
225                         }                       
226                         if (!res) {
227                                 /* Step 4: Measure noise */
228                                 if (option_debug)
229                                         ast_log(LOG_DEBUG, "TestClient: 4.  Measure noise\n");
230                                 res = measurenoise(chan, 5000, "TestClient");
231                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
232                                 if (res > 0)
233                                         res = 0;
234                         }
235                         if (!res) {
236                                 /* Step 5: Wait for "4" */
237                                 if (option_debug)
238                                         ast_log(LOG_DEBUG, "TestClient: 5.  Wait DTMF 4\n");
239                                 res = ast_waitfordigit(chan, 3000);
240                                 fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
241                                 if (res == '4')
242                                         res = 0;
243                                 else
244                                         res = -1;
245                         }
246                         if (!res) {
247                                 /* Step 6: Transmit tone noise */
248                                 if (option_debug)
249                                         ast_log(LOG_DEBUG, "TestClient: 6.  Transmit tone\n");
250                                 res = sendnoise(chan, 6000);
251                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
252                         }
253                         if (!res || (res == '5')) {
254                                 /* Step 7: Wait for "5" */
255                                 if (option_debug)
256                                         ast_log(LOG_DEBUG, "TestClient: 7.  Wait DTMF 5\n");
257                                 if (!res)
258                                         res = ast_waitfordigit(chan, 3000);
259                                 fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
260                                 if (res == '5')
261                                         res = 0;
262                                 else
263                                         res = -1;
264                         }
265                         if (!res) {
266                                 /* Step 8: Wait one second */
267                                 if (option_debug)
268                                         ast_log(LOG_DEBUG, "TestClient: 8.  Wait one second\n");
269                                 res = ast_safe_sleep(chan, 1000);
270                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
271                                 if (res > 0)
272                                         res = 0;
273                         }
274                         if (!res) {
275                                 /* Step 9: Measure noise */
276                                 if (option_debug)
277                                         ast_log(LOG_DEBUG, "TestClient: 6.  Measure tone\n");
278                                 res = measurenoise(chan, 4000, "TestClient");
279                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
280                                 if (res > 0)
281                                         res = 0;
282                         }
283                         if (!res) {
284                                 /* Step 10: Send "7" */
285                                 if (option_debug)
286                                         ast_log(LOG_DEBUG, "TestClient: 7.  Send DTMF 7\n");
287                                 res = ast_dtmf_stream(chan, NULL, "7", 0);
288                                 fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
289                                 if (res > 0)
290                                         res =0;
291                         }
292                         if (!res) {
293                                 /* Step 11: Wait for "8" */
294                                 if (option_debug)
295                                         ast_log(LOG_DEBUG, "TestClient: 11.  Wait DTMF 8\n");
296                                 res = ast_waitfordigit(chan, 3000);
297                                 fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
298                                 if (res == '8')
299                                         res = 0;
300                                 else
301                                         res = -1;
302                         }
303                         if (option_debug && !res ) {
304                                 /* Step 12: Hangup! */
305                                 ast_log(LOG_DEBUG, "TestClient: 12.  Hangup\n");
306                         }
307
308                         if (option_debug)
309                                 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
310                         fprintf(f, "-- END TEST--\n");
311                         fclose(f);
312                         res = -1;
313                 } else
314                         res = -1;
315         } else {
316                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
317                 res = -1;
318         }
319         LOCAL_USER_REMOVE(u);
320         return res;
321 }
322
323 static int testserver_exec(struct ast_channel *chan, void *data)
324 {
325         struct localuser *u;
326         int res = 0;
327         char testid[80]="";
328         char fn[80];
329         FILE *f;
330         LOCAL_USER_ADD(u);
331         if (chan->_state != AST_STATE_UP)
332                 res = ast_answer(chan);
333         /* Read version */
334         if (option_debug)
335                 ast_log(LOG_DEBUG, "Read client version\n");
336         if (!res) 
337                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
338         if (res > 0)
339                 res = 0;
340         if (option_debug) {
341                 ast_log(LOG_DEBUG, "client version: %s\n", testid);
342                 ast_log(LOG_DEBUG, "Transmit server version\n");
343         }
344         res = ast_safe_sleep(chan, 1000);
345         if (!res)
346                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
347         if (res > 0)
348                 res = 0;
349
350         if (!res) 
351                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);               
352         if (option_debug) 
353                 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
354         /* Check for sneakyness */
355         if (strchr(testid, '/'))
356                 res = -1;
357         if ((res >=0) && (!ast_strlen_zero(testid))) {
358                 /* Got a Test ID!  Whoo hoo! */
359                 /* Make the directory to hold the test results in case it's not there */
360                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
361                 mkdir(fn, 0777);
362                 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
363                 if ((f = fopen(fn, "w+"))) {
364                         setlinebuf(f);
365                         fprintf(f, "SERVERCHAN:    %s\n", chan->name);
366                         fprintf(f, "SERVERTEST ID: %s\n", testid);
367                         fprintf(f, "ANSWER:        PASS\n");
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         LOCAL_USER_REMOVE(u);
489         return res;
490 }
491
492 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         STANDARD_HANGUP_LOCALUSERS;
500
501         return res;     
502 }
503
504 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 char *description(void)
515 {
516         return tdesc;
517 }
518
519 int usecount(void)
520 {
521         int res;
522         STANDARD_USECOUNT(res);
523         return res;
524 }
525
526 char *key(void)
527 {
528         return ASTERISK_GPL_KEY;
529 }