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$")
39 #include <sys/types.h>
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"
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";
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";
61 static char *testc_app = "TestClient";
62 static char *testc_synopsis = "Execute Interface Test Client";
64 static int measurenoise(struct ast_channel *chan, int ms, char *who)
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");
82 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
85 res = ast_waitfor(chan, ms - mssofar);
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++) {
104 if (ast_set_read_format(chan, rformat)) {
105 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
112 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
115 ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
116 return (noise / samples);
119 static int sendnoise(struct ast_channel *chan, int ms)
122 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
124 res = ast_waitfordigit(chan, ms);
125 ast_tonepair_stop(chan);
130 static int testclient_exec(struct ast_channel *chan, void *data)
132 struct ast_module_user *u;
139 /* Check for test id */
140 if (ast_strlen_zero(testid)) {
141 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
145 u = ast_module_user_add(chan);
147 if (chan->_state != AST_STATE_UP)
148 res = ast_answer(chan);
150 /* Wait a few just to be sure things get started */
151 res = ast_safe_sleep(chan, 3000);
152 /* Transmit client version */
154 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
155 ast_debug(1, "Transmit client version\n");
157 /* Read server version */
158 ast_debug(1, "Read server version\n");
160 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
163 ast_debug(1, "server version: %s\n", serverver);
169 res = ast_safe_sleep(chan, 1000);
172 res = ast_dtmf_stream(chan, NULL, testid, 0);
174 res = ast_dtmf_stream(chan, NULL, "#", 0);
175 ast_debug(1, "send test identifier: %s\n", testid);
177 if ((res >=0) && (!ast_strlen_zero(testid))) {
178 /* Make the directory to hold the test results in case it's not there */
179 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
181 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
182 if ((f = fopen(fn, "w+"))) {
184 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
185 fprintf(f, "CLIENTTEST ID: %s\n", testid);
186 fprintf(f, "ANSWER: PASS\n");
190 /* Step 1: Wait for "1" */
191 ast_debug(1, "TestClient: 2. Wait DTMF 1\n");
192 res = ast_waitfordigit(chan, 3000);
193 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
200 res = ast_safe_sleep(chan, 1000);
202 /* Step 2: Send "2" */
203 ast_debug(1, "TestClient: 2. Send DTMF 2\n");
204 res = ast_dtmf_stream(chan, NULL, "2", 0);
205 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
210 /* Step 3: Wait one second */
211 ast_debug(1, "TestClient: 3. Wait one second\n");
212 res = ast_safe_sleep(chan, 1000);
213 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
218 /* Step 4: Measure noise */
219 ast_debug(1, "TestClient: 4. Measure noise\n");
220 res = measurenoise(chan, 5000, "TestClient");
221 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
226 /* Step 5: Wait for "4" */
227 ast_debug(1, "TestClient: 5. Wait DTMF 4\n");
228 res = ast_waitfordigit(chan, 3000);
229 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
236 /* Step 6: Transmit tone noise */
237 ast_debug(1, "TestClient: 6. Transmit tone\n");
238 res = sendnoise(chan, 6000);
239 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
241 if (!res || (res == '5')) {
242 /* Step 7: Wait for "5" */
243 ast_debug(1, "TestClient: 7. Wait DTMF 5\n");
245 res = ast_waitfordigit(chan, 3000);
246 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
253 /* Step 8: Wait one second */
254 ast_debug(1, "TestClient: 8. Wait one second\n");
255 res = ast_safe_sleep(chan, 1000);
256 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
261 /* Step 9: Measure noise */
262 ast_debug(1, "TestClient: 6. Measure tone\n");
263 res = measurenoise(chan, 4000, "TestClient");
264 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
269 /* Step 10: Send "7" */
270 ast_debug(1, "TestClient: 7. Send DTMF 7\n");
271 res = ast_dtmf_stream(chan, NULL, "7", 0);
272 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
277 /* Step 11: Wait for "8" */
278 ast_debug(1, "TestClient: 11. Wait DTMF 8\n");
279 res = ast_waitfordigit(chan, 3000);
280 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
287 /* Step 12: Hangup! */
288 ast_debug(1, "TestClient: 12. Hangup\n");
291 ast_debug(1, "-- TEST COMPLETE--\n");
292 fprintf(f, "-- END TEST--\n");
298 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
301 ast_module_user_remove(u);
305 static int testserver_exec(struct ast_channel *chan, void *data)
307 struct ast_module_user *u;
312 u = ast_module_user_add(chan);
313 if (chan->_state != AST_STATE_UP)
314 res = ast_answer(chan);
316 ast_debug(1, "Read client version\n");
318 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
322 ast_debug(1, "client version: %s\n", testid);
323 ast_debug(1, "Transmit server version\n");
325 res = ast_safe_sleep(chan, 1000);
327 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
332 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
333 ast_debug(1, "read test identifier: %s\n", testid);
334 /* Check for sneakyness */
335 if (strchr(testid, '/'))
337 if ((res >=0) && (!ast_strlen_zero(testid))) {
338 /* Got a Test ID! Whoo hoo! */
339 /* Make the directory to hold the test results in case it's not there */
340 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
342 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
343 if ((f = fopen(fn, "w+"))) {
345 fprintf(f, "SERVERCHAN: %s\n", chan->name);
346 fprintf(f, "SERVERTEST ID: %s\n", testid);
347 fprintf(f, "ANSWER: PASS\n");
348 ast_debug(1, "Processing Test ID '%s'\n", testid);
349 res = ast_safe_sleep(chan, 1000);
351 /* Step 1: Send "1" */
352 ast_debug(1, "TestServer: 1. Send DTMF 1\n");
353 res = ast_dtmf_stream(chan, NULL, "1", 0);
354 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
359 /* Step 2: Wait for "2" */
360 ast_debug(1, "TestServer: 2. Wait DTMF 2\n");
361 res = ast_waitfordigit(chan, 3000);
362 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
369 /* Step 3: Measure noise */
370 ast_debug(1, "TestServer: 3. Measure noise\n");
371 res = measurenoise(chan, 6000, "TestServer");
372 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
377 /* Step 4: Send "4" */
378 ast_debug(1, "TestServer: 4. Send DTMF 4\n");
379 res = ast_dtmf_stream(chan, NULL, "4", 0);
380 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
386 /* Step 5: Wait one second */
387 ast_debug(1, "TestServer: 5. Wait one second\n");
388 res = ast_safe_sleep(chan, 1000);
389 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
395 /* Step 6: Measure noise */
396 ast_debug(1, "TestServer: 6. Measure tone\n");
397 res = measurenoise(chan, 4000, "TestServer");
398 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
404 /* Step 7: Send "5" */
405 ast_debug(1, "TestServer: 7. Send DTMF 5\n");
406 res = ast_dtmf_stream(chan, NULL, "5", 0);
407 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
413 /* Step 8: Transmit tone noise */
414 ast_debug(1, "TestServer: 8. Transmit tone\n");
415 res = sendnoise(chan, 6000);
416 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
419 if (!res || (res == '7')) {
420 /* Step 9: Wait for "7" */
421 ast_debug(1, "TestServer: 9. Wait DTMF 7\n");
423 res = ast_waitfordigit(chan, 3000);
424 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
431 res = ast_safe_sleep(chan, 1000);
433 /* Step 10: Send "8" */
434 ast_debug(1, "TestServer: 10. Send DTMF 8\n");
435 res = ast_dtmf_stream(chan, NULL, "8", 0);
436 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
441 /* Step 11: Wait for hangup to arrive! */
442 ast_debug(1, "TestServer: 11. Waiting for hangup\n");
443 res = ast_safe_sleep(chan, 10000);
444 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
447 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
448 fprintf(f, "-- END TEST--\n");
454 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
457 ast_module_user_remove(u);
461 static int unload_module(void)
465 res = ast_unregister_application(testc_app);
466 res |= ast_unregister_application(tests_app);
468 ast_module_user_hangup_all();
473 static int load_module(void)
477 res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
478 res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
483 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");