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