2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Russell Bryant <russelb@clemson.edu>
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.
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.
22 * \brief Applications to test connection and produce report in text file
24 * \author Mark Spencer <markster@digium.com>
25 * \author Russell Bryant <russelb@clemson.edu>
27 * \ingroup applications
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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"
44 static char *tests_descrip
=
45 " TestServer(): Perform test server function and write call report.\n"
46 "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
47 static char *tests_app
= "TestServer";
48 static char *tests_synopsis
= "Execute Interface Test Server";
50 static char *testc_descrip
=
51 " TestClient(testid): Executes test client with given testid.\n"
52 "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
54 static char *testc_app
= "TestClient";
55 static char *testc_synopsis
= "Execute Interface Test Client";
57 static int measurenoise(struct ast_channel
*chan
, int ms
, char *who
)
68 rformat
= chan
->readformat
;
69 if (ast_set_read_format(chan
, AST_FORMAT_SLINEAR
)) {
70 ast_log(LOG_NOTICE
, "Unable to set to linear mode!\n");
75 mssofar
= ast_tvdiff_ms(ast_tvnow(), start
);
78 res
= ast_waitfor(chan
, ms
- mssofar
);
86 if ((f
->frametype
== AST_FRAME_VOICE
) && (f
->subclass
== AST_FORMAT_SLINEAR
)) {
87 foo
= (short *)f
->data
.ptr
;
88 for (x
=0;x
<f
->samples
;x
++) {
97 if (ast_set_read_format(chan
, rformat
)) {
98 ast_log(LOG_NOTICE
, "Unable to restore original format!\n");
105 ast_log(LOG_NOTICE
, "No samples were received from the other side!\n");
108 ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who
, noise
, samples
, noise
/ samples
);
109 return (noise
/ samples
);
112 static int sendnoise(struct ast_channel
*chan
, int ms
)
115 res
= ast_tonepair_start(chan
, 1537, 2195, ms
, 8192);
117 res
= ast_waitfordigit(chan
, ms
);
118 ast_tonepair_stop(chan
);
123 static int testclient_exec(struct ast_channel
*chan
, void *data
)
131 /* Check for test id */
132 if (ast_strlen_zero(testid
)) {
133 ast_log(LOG_WARNING
, "TestClient requires an argument - the test id\n");
137 if (chan
->_state
!= AST_STATE_UP
)
138 res
= ast_answer(chan
);
140 /* Wait a few just to be sure things get started */
141 res
= ast_safe_sleep(chan
, 3000);
142 /* Transmit client version */
144 res
= ast_dtmf_stream(chan
, NULL
, "8378*1#", 0, 0);
145 ast_debug(1, "Transmit client version\n");
147 /* Read server version */
148 ast_debug(1, "Read server version\n");
150 res
= ast_app_getdata(chan
, NULL
, serverver
, sizeof(serverver
) - 1, 0);
153 ast_debug(1, "server version: %s\n", serverver
);
159 res
= ast_safe_sleep(chan
, 1000);
162 res
= ast_dtmf_stream(chan
, NULL
, testid
, 0, 0);
164 res
= ast_dtmf_stream(chan
, NULL
, "#", 0, 0);
165 ast_debug(1, "send test identifier: %s\n", testid
);
167 if ((res
>=0) && (!ast_strlen_zero(testid
))) {
168 /* Make the directory to hold the test results in case it's not there */
169 snprintf(fn
, sizeof(fn
), "%s/testresults", ast_config_AST_LOG_DIR
);
171 snprintf(fn
, sizeof(fn
), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR
, testid
);
172 if ((f
= fopen(fn
, "w+"))) {
174 fprintf(f
, "CLIENTCHAN: %s\n", chan
->name
);
175 fprintf(f
, "CLIENTTEST ID: %s\n", testid
);
176 fprintf(f
, "ANSWER: PASS\n");
180 /* Step 1: Wait for "1" */
181 ast_debug(1, "TestClient: 2. Wait DTMF 1\n");
182 res
= ast_waitfordigit(chan
, 3000);
183 fprintf(f
, "WAIT DTMF 1: %s\n", (res
!= '1') ? "FAIL" : "PASS");
190 res
= ast_safe_sleep(chan
, 1000);
192 /* Step 2: Send "2" */
193 ast_debug(1, "TestClient: 2. Send DTMF 2\n");
194 res
= ast_dtmf_stream(chan
, NULL
, "2", 0, 0);
195 fprintf(f
, "SEND DTMF 2: %s\n", (res
< 0) ? "FAIL" : "PASS");
200 /* Step 3: Wait one second */
201 ast_debug(1, "TestClient: 3. Wait one second\n");
202 res
= ast_safe_sleep(chan
, 1000);
203 fprintf(f
, "WAIT 1 SEC: %s\n", (res
< 0) ? "FAIL" : "PASS");
208 /* Step 4: Measure noise */
209 ast_debug(1, "TestClient: 4. Measure noise\n");
210 res
= measurenoise(chan
, 5000, "TestClient");
211 fprintf(f
, "MEASURENOISE: %s (%d)\n", (res
< 0) ? "FAIL" : "PASS", res
);
216 /* Step 5: Wait for "4" */
217 ast_debug(1, "TestClient: 5. Wait DTMF 4\n");
218 res
= ast_waitfordigit(chan
, 3000);
219 fprintf(f
, "WAIT DTMF 4: %s\n", (res
!= '4') ? "FAIL" : "PASS");
226 /* Step 6: Transmit tone noise */
227 ast_debug(1, "TestClient: 6. Transmit tone\n");
228 res
= sendnoise(chan
, 6000);
229 fprintf(f
, "SENDTONE: %s\n", (res
< 0) ? "FAIL" : "PASS");
231 if (!res
|| (res
== '5')) {
232 /* Step 7: Wait for "5" */
233 ast_debug(1, "TestClient: 7. Wait DTMF 5\n");
235 res
= ast_waitfordigit(chan
, 3000);
236 fprintf(f
, "WAIT DTMF 5: %s\n", (res
!= '5') ? "FAIL" : "PASS");
243 /* Step 8: Wait one second */
244 ast_debug(1, "TestClient: 8. Wait one second\n");
245 res
= ast_safe_sleep(chan
, 1000);
246 fprintf(f
, "WAIT 1 SEC: %s\n", (res
< 0) ? "FAIL" : "PASS");
251 /* Step 9: Measure noise */
252 ast_debug(1, "TestClient: 6. Measure tone\n");
253 res
= measurenoise(chan
, 4000, "TestClient");
254 fprintf(f
, "MEASURETONE: %s (%d)\n", (res
< 0) ? "FAIL" : "PASS", res
);
259 /* Step 10: Send "7" */
260 ast_debug(1, "TestClient: 7. Send DTMF 7\n");
261 res
= ast_dtmf_stream(chan
, NULL
, "7", 0, 0);
262 fprintf(f
, "SEND DTMF 7: %s\n", (res
< 0) ? "FAIL" : "PASS");
267 /* Step 11: Wait for "8" */
268 ast_debug(1, "TestClient: 11. Wait DTMF 8\n");
269 res
= ast_waitfordigit(chan
, 3000);
270 fprintf(f
, "WAIT DTMF 8: %s\n", (res
!= '8') ? "FAIL" : "PASS");
277 /* Step 12: Hangup! */
278 ast_debug(1, "TestClient: 12. Hangup\n");
281 ast_debug(1, "-- TEST COMPLETE--\n");
282 fprintf(f
, "-- END TEST--\n");
288 ast_log(LOG_NOTICE
, "Did not read a test ID on '%s'\n", chan
->name
);
294 static int testserver_exec(struct ast_channel
*chan
, void *data
)
300 if (chan
->_state
!= AST_STATE_UP
)
301 res
= ast_answer(chan
);
303 ast_debug(1, "Read client version\n");
305 res
= ast_app_getdata(chan
, NULL
, testid
, sizeof(testid
) - 1, 0);
309 ast_debug(1, "client version: %s\n", testid
);
310 ast_debug(1, "Transmit server version\n");
312 res
= ast_safe_sleep(chan
, 1000);
314 res
= ast_dtmf_stream(chan
, NULL
, "8378*1#", 0, 0);
319 res
= ast_app_getdata(chan
, NULL
, testid
, sizeof(testid
) - 1, 0);
320 ast_debug(1, "read test identifier: %s\n", testid
);
321 /* Check for sneakyness */
322 if (strchr(testid
, '/'))
324 if ((res
>=0) && (!ast_strlen_zero(testid
))) {
325 /* Got a Test ID! Whoo hoo! */
326 /* Make the directory to hold the test results in case it's not there */
327 snprintf(fn
, sizeof(fn
), "%s/testresults", ast_config_AST_LOG_DIR
);
329 snprintf(fn
, sizeof(fn
), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR
, testid
);
330 if ((f
= fopen(fn
, "w+"))) {
332 fprintf(f
, "SERVERCHAN: %s\n", chan
->name
);
333 fprintf(f
, "SERVERTEST ID: %s\n", testid
);
334 fprintf(f
, "ANSWER: PASS\n");
335 ast_debug(1, "Processing Test ID '%s'\n", testid
);
336 res
= ast_safe_sleep(chan
, 1000);
338 /* Step 1: Send "1" */
339 ast_debug(1, "TestServer: 1. Send DTMF 1\n");
340 res
= ast_dtmf_stream(chan
, NULL
, "1", 0,0 );
341 fprintf(f
, "SEND DTMF 1: %s\n", (res
< 0) ? "FAIL" : "PASS");
346 /* Step 2: Wait for "2" */
347 ast_debug(1, "TestServer: 2. Wait DTMF 2\n");
348 res
= ast_waitfordigit(chan
, 3000);
349 fprintf(f
, "WAIT DTMF 2: %s\n", (res
!= '2') ? "FAIL" : "PASS");
356 /* Step 3: Measure noise */
357 ast_debug(1, "TestServer: 3. Measure noise\n");
358 res
= measurenoise(chan
, 6000, "TestServer");
359 fprintf(f
, "MEASURENOISE: %s (%d)\n", (res
< 0) ? "FAIL" : "PASS", res
);
364 /* Step 4: Send "4" */
365 ast_debug(1, "TestServer: 4. Send DTMF 4\n");
366 res
= ast_dtmf_stream(chan
, NULL
, "4", 0, 0);
367 fprintf(f
, "SEND DTMF 4: %s\n", (res
< 0) ? "FAIL" : "PASS");
373 /* Step 5: Wait one second */
374 ast_debug(1, "TestServer: 5. Wait one second\n");
375 res
= ast_safe_sleep(chan
, 1000);
376 fprintf(f
, "WAIT 1 SEC: %s\n", (res
< 0) ? "FAIL" : "PASS");
382 /* Step 6: Measure noise */
383 ast_debug(1, "TestServer: 6. Measure tone\n");
384 res
= measurenoise(chan
, 4000, "TestServer");
385 fprintf(f
, "MEASURETONE: %s (%d)\n", (res
< 0) ? "FAIL" : "PASS", res
);
391 /* Step 7: Send "5" */
392 ast_debug(1, "TestServer: 7. Send DTMF 5\n");
393 res
= ast_dtmf_stream(chan
, NULL
, "5", 0, 0);
394 fprintf(f
, "SEND DTMF 5: %s\n", (res
< 0) ? "FAIL" : "PASS");
400 /* Step 8: Transmit tone noise */
401 ast_debug(1, "TestServer: 8. Transmit tone\n");
402 res
= sendnoise(chan
, 6000);
403 fprintf(f
, "SENDTONE: %s\n", (res
< 0) ? "FAIL" : "PASS");
406 if (!res
|| (res
== '7')) {
407 /* Step 9: Wait for "7" */
408 ast_debug(1, "TestServer: 9. Wait DTMF 7\n");
410 res
= ast_waitfordigit(chan
, 3000);
411 fprintf(f
, "WAIT DTMF 7: %s\n", (res
!= '7') ? "FAIL" : "PASS");
418 res
= ast_safe_sleep(chan
, 1000);
420 /* Step 10: Send "8" */
421 ast_debug(1, "TestServer: 10. Send DTMF 8\n");
422 res
= ast_dtmf_stream(chan
, NULL
, "8", 0, 0);
423 fprintf(f
, "SEND DTMF 8: %s\n", (res
< 0) ? "FAIL" : "PASS");
428 /* Step 11: Wait for hangup to arrive! */
429 ast_debug(1, "TestServer: 11. Waiting for hangup\n");
430 res
= ast_safe_sleep(chan
, 10000);
431 fprintf(f
, "WAIT HANGUP: %s\n", (res
< 0) ? "PASS" : "FAIL");
434 ast_log(LOG_NOTICE
, "-- TEST COMPLETE--\n");
435 fprintf(f
, "-- END TEST--\n");
441 ast_log(LOG_NOTICE
, "Did not read a test ID on '%s'\n", chan
->name
);
447 static int unload_module(void)
451 res
= ast_unregister_application(testc_app
);
452 res
|= ast_unregister_application(tests_app
);
457 static int load_module(void)
461 res
= ast_register_application(testc_app
, testclient_exec
, testc_synopsis
, testc_descrip
);
462 res
|= ast_register_application(tests_app
, testserver_exec
, tests_synopsis
, tests_descrip
);
467 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Interface Test Application");