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