Asterisk media architecture conversion - no more format bitfields
[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         struct ast_format rformat;
91
92         ast_format_copy(&rformat, &chan->readformat);
93         if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
94                 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
95                 return -1;
96         }
97         start = ast_tvnow();
98         for(;;) {
99                 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
100                 if (mssofar > ms)
101                         break;
102                 res = ast_waitfor(chan, ms - mssofar);
103                 if (res < 1)
104                         break;
105                 f = ast_read(chan);
106                 if (!f) {
107                         res = -1;
108                         break;
109                 }
110                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
111                         foo = (short *)f->data.ptr;
112                         for (x=0;x<f->samples;x++) {
113                                 noise += abs(foo[x]);
114                                 samples++;
115                         }
116                 }
117                 ast_frfree(f);
118         }
119
120         if (rformat.id) {
121                 if (ast_set_read_format(chan, &rformat)) {
122                         ast_log(LOG_NOTICE, "Unable to restore original format!\n");
123                         return -1;
124                 }
125         }
126         if (res < 0)
127                 return res;
128         if (!samples) {
129                 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
130                 return -1;
131         }
132         ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
133         return (noise / samples);
134 }
135
136 static int sendnoise(struct ast_channel *chan, int ms)
137 {
138         int res;
139         res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
140         if (!res) {
141                 res = ast_waitfordigit(chan, ms);
142                 ast_tonepair_stop(chan);
143         }
144         return res;
145 }
146
147 static int testclient_exec(struct ast_channel *chan, const char *data)
148 {
149         int res = 0;
150         const char *testid=data;
151         char fn[80];
152         char serverver[80];
153         FILE *f;
154
155         /* Check for test id */
156         if (ast_strlen_zero(testid)) {
157                 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
158                 return -1;
159         }
160
161         if (chan->_state != AST_STATE_UP)
162                 res = ast_answer(chan);
163
164         /* Wait a few just to be sure things get started */
165         res = ast_safe_sleep(chan, 3000);
166         /* Transmit client version */
167         if (!res)
168                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
169         ast_debug(1, "Transmit client version\n");
170
171         /* Read server version */
172         ast_debug(1, "Read server version\n");
173         if (!res)
174                 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
175         if (res > 0)
176                 res = 0;
177         ast_debug(1, "server version: %s\n", serverver);
178
179         if (res > 0)
180                 res = 0;
181
182         if (!res)
183                 res = ast_safe_sleep(chan, 1000);
184         /* Send test id */
185         if (!res)
186                 res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
187         if (!res)
188                 res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
189         ast_debug(1, "send test identifier: %s\n", testid);
190
191         if ((res >=0) && (!ast_strlen_zero(testid))) {
192                 /* Make the directory to hold the test results in case it's not there */
193                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
194                 ast_mkdir(fn, 0777);
195                 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
196                 if ((f = fopen(fn, "w+"))) {
197                         setlinebuf(f);
198                         fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
199                         fprintf(f, "CLIENTTEST ID: %s\n", testid);
200                         fprintf(f, "ANSWER:        PASS\n");
201                         res = 0;
202
203                         if (!res) {
204                                 /* Step 1: Wait for "1" */
205                                 ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
206                                 res = ast_waitfordigit(chan, 3000);
207                                 fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
208                                 if (res == '1')
209                                         res = 0;
210                                 else
211                                         res = -1;
212                         }
213                         if (!res) {
214                                 res = ast_safe_sleep(chan, 1000);
215                         }
216                         if (!res) {
217                                 /* Step 2: Send "2" */
218                                 ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
219                                 res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
220                                 fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
221                                 if (res > 0)
222                                         res = 0;
223                         }
224                         if (!res) {
225                                 /* Step 3: Wait one second */
226                                 ast_debug(1, "TestClient: 3.  Wait one second\n");
227                                 res = ast_safe_sleep(chan, 1000);
228                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
229                                 if (res > 0)
230                                         res = 0;
231                         }
232                         if (!res) {
233                                 /* Step 4: Measure noise */
234                                 ast_debug(1, "TestClient: 4.  Measure noise\n");
235                                 res = measurenoise(chan, 5000, "TestClient");
236                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
237                                 if (res > 0)
238                                         res = 0;
239                         }
240                         if (!res) {
241                                 /* Step 5: Wait for "4" */
242                                 ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
243                                 res = ast_waitfordigit(chan, 3000);
244                                 fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
245                                 if (res == '4')
246                                         res = 0;
247                                 else
248                                         res = -1;
249                         }
250                         if (!res) {
251                                 /* Step 6: Transmit tone noise */
252                                 ast_debug(1, "TestClient: 6.  Transmit tone\n");
253                                 res = sendnoise(chan, 6000);
254                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
255                         }
256                         if (!res || (res == '5')) {
257                                 /* Step 7: Wait for "5" */
258                                 ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
259                                 if (!res)
260                                         res = ast_waitfordigit(chan, 3000);
261                                 fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
262                                 if (res == '5')
263                                         res = 0;
264                                 else
265                                         res = -1;
266                         }
267                         if (!res) {
268                                 /* Step 8: Wait one second */
269                                 ast_debug(1, "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                                 ast_debug(1, "TestClient: 9.  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                                 ast_debug(1, "TestClient: 10.  Send DTMF 7\n");
286                                 res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
287                                 fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
288                                 if (res > 0)
289                                         res =0;
290                         }
291                         if (!res) {
292                                 /* Step 11: Wait for "8" */
293                                 ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
294                                 res = ast_waitfordigit(chan, 3000);
295                                 fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
296                                 if (res == '8')
297                                         res = 0;
298                                 else
299                                         res = -1;
300                         }
301                         if (!res) {
302                                 res = ast_safe_sleep(chan, 1000);
303                         }
304                         if (!res) {
305                                 /* Step 12: Hangup! */
306                                 ast_debug(1, "TestClient: 12.  Hangup\n");
307                         }
308
309                         ast_debug(1, "-- 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         return res;
320 }
321
322 static int testserver_exec(struct ast_channel *chan, const char *data)
323 {
324         int res = 0;
325         char testid[80]="";
326         char fn[80];
327         FILE *f;
328         if (chan->_state != AST_STATE_UP)
329                 res = ast_answer(chan);
330         /* Read version */
331         ast_debug(1, "Read client version\n");
332         if (!res)
333                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
334         if (res > 0)
335                 res = 0;
336
337         ast_debug(1, "client version: %s\n", testid);
338         ast_debug(1, "Transmit server version\n");
339
340         res = ast_safe_sleep(chan, 1000);
341         if (!res)
342                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
343         if (res > 0)
344                 res = 0;
345
346         if (!res)
347                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
348         ast_debug(1, "read test identifier: %s\n", testid);
349         /* Check for sneakyness */
350         if (strchr(testid, '/'))
351                 res = -1;
352         if ((res >=0) && (!ast_strlen_zero(testid))) {
353                 /* Got a Test ID!  Whoo hoo! */
354                 /* Make the directory to hold the test results in case it's not there */
355                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
356                 ast_mkdir(fn, 0777);
357                 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
358                 if ((f = fopen(fn, "w+"))) {
359                         setlinebuf(f);
360                         fprintf(f, "SERVERCHAN:    %s\n", chan->name);
361                         fprintf(f, "SERVERTEST ID: %s\n", testid);
362                         fprintf(f, "ANSWER:        PASS\n");
363                         ast_debug(1, "Processing Test ID '%s'\n", testid);
364                         res = ast_safe_sleep(chan, 1000);
365                         if (!res) {
366                                 /* Step 1: Send "1" */
367                                 ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
368                                 res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
369                                 fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
370                                 if (res > 0)
371                                         res = 0;
372                         }
373                         if (!res) {
374                                 /* Step 2: Wait for "2" */
375                                 ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
376                                 res = ast_waitfordigit(chan, 3000);
377                                 fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
378                                 if (res == '2')
379                                         res = 0;
380                                 else
381                                         res = -1;
382                         }
383                         if (!res) {
384                                 /* Step 3: Measure noise */
385                                 ast_debug(1, "TestServer: 3.  Measure noise\n");
386                                 res = measurenoise(chan, 6000, "TestServer");
387                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
388                                 if (res > 0)
389                                         res = 0;
390                         }
391                         if (!res) {
392                                 /* Step 4: Send "4" */
393                                 ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
394                                 res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
395                                 fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
396                                 if (res > 0)
397                                         res = 0;
398                         }
399                         if (!res) {
400                                 /* Step 5: Wait one second */
401                                 ast_debug(1, "TestServer: 5.  Wait one second\n");
402                                 res = ast_safe_sleep(chan, 1000);
403                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
404                                 if (res > 0)
405                                         res = 0;
406                         }
407                         if (!res) {
408                                 /* Step 6: Measure noise */
409                                 ast_debug(1, "TestServer: 6.  Measure tone\n");
410                                 res = measurenoise(chan, 4000, "TestServer");
411                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
412                                 if (res > 0)
413                                         res = 0;
414                         }
415                         if (!res) {
416                                 /* Step 7: Send "5" */
417                                 ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
418                                 res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
419                                 fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
420                                 if (res > 0)
421                                         res = 0;
422                         }
423                         if (!res) {
424                                 /* Step 8: Transmit tone noise */
425                                 ast_debug(1, "TestServer: 8.  Transmit tone\n");
426                                 res = sendnoise(chan, 6000);
427                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
428                         }
429
430                         if (!res || (res == '7')) {
431                                 /* Step 9: Wait for "7" */
432                                 ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
433                                 if (!res)
434                                         res = ast_waitfordigit(chan, 3000);
435                                 fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
436                                 if (res == '7')
437                                         res = 0;
438                                 else
439                                         res = -1;
440                         }
441                         if (!res) {
442                                 res = ast_safe_sleep(chan, 1000);
443                         }
444                         if (!res) {
445                                 /* Step 10: Send "8" */
446                                 ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
447                                 res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
448                                 fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
449                                 if (res > 0)
450                                         res = 0;
451                         }
452                         if (!res) {
453                                 /* Step 11: Wait for hangup to arrive! */
454                                 ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
455                                 res = ast_safe_sleep(chan, 10000);
456                                 fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
457                         }
458
459                         ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
460                         fprintf(f, "-- END TEST--\n");
461                         fclose(f);
462                         res = -1;
463                 } else
464                         res = -1;
465         } else {
466                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
467                 res = -1;
468         }
469         return res;
470 }
471
472 static int unload_module(void)
473 {
474         int res;
475
476         res = ast_unregister_application(testc_app);
477         res |= ast_unregister_application(tests_app);
478
479         return res;
480 }
481
482 static int load_module(void)
483 {
484         int res;
485
486         res = ast_register_application_xml(testc_app, testclient_exec);
487         res |= ast_register_application_xml(tests_app, testserver_exec);
488
489         return res;
490 }
491
492 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");