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