2 * Asterisk -- A telephony toolkit for Linux.
4 * Applications to test connection and produce report in text file
6 * Copyright (C) 2004 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
9 * Russell Bryant <russelb@clemson.edu>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
15 #include <sys/types.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/options.h>
19 #include <asterisk/module.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/lock.h>
22 #include <asterisk/app.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/utils.h>
29 #include "../astconf.h"
32 static char *tdesc = "Interface Test Application";
34 static char *tests_descrip =
35 "TestServer(): Perform test server function and write call report.\n"
36 "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
37 static char *tests_app = "TestServer";
38 static char *tests_synopsis = "Execute Interface Test Server";
40 static char *testc_descrip =
41 "TestClient(testid): Executes test client with given testid.\n"
42 "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
44 static char *testc_app = "TestClient";
45 static char *testc_synopsis = "Execute Interface Test Client";
47 static int measurenoise(struct ast_channel *chan, int ms, char *who)
55 struct timeval start, tv;
58 rformat = chan->readformat;
59 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
60 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
63 gettimeofday(&start, NULL);
65 gettimeofday(&tv, NULL);
66 mssofar = (tv.tv_sec - start.tv_sec) * 1000;
67 mssofar += (tv.tv_usec - start.tv_usec) / 1000;
70 res = ast_waitfor(chan, ms - mssofar);
78 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
79 foo = (short *)f->data;
80 for (x=0;x<f->samples;x++) {
88 if (ast_set_read_format(chan, rformat)) {
89 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
96 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
99 ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
100 return (noise / samples);
103 static int sendnoise(struct ast_channel *chan, int ms)
106 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
108 res = ast_waitfordigit(chan, ms);
109 ast_tonepair_stop(chan);
118 static int testclient_exec(struct ast_channel *chan, void *data)
128 /* Check for test id */
129 if (!testid || ast_strlen_zero(testid)) {
130 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
134 if (chan->_state != AST_STATE_UP)
135 res = ast_answer(chan);
137 /* Wait a few just to be sure things get started */
138 res = ast_safe_sleep(chan, 3000);
139 /* Transmit client version */
141 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
143 ast_log(LOG_DEBUG, "Transmit client version\n");
145 /* Read server version */
147 ast_log(LOG_DEBUG, "Read server version\n");
149 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
153 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
159 res = ast_safe_sleep(chan, 1000);
162 res = ast_dtmf_stream(chan, NULL, testid, 0);
164 res = ast_dtmf_stream(chan, NULL, "#", 0);
166 ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
168 if ((res >=0) && (!ast_strlen_zero(testid))) {
169 /* Make the directory to hold the test results in case it's not there */
170 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
172 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
173 if ((f = fopen(fn, "w+"))) {
175 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
176 fprintf(f, "CLIENTTEST ID: %s\n", testid);
177 fprintf(f, "ANSWER: PASS\n");
181 /* Step 1: Wait for "1" */
183 ast_log(LOG_DEBUG, "TestClient: 2. Wait DTMF 1\n");
184 res = ast_waitfordigit(chan, 3000);
185 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
192 res = ast_safe_sleep(chan, 1000);
194 /* Step 2: Send "2" */
196 ast_log(LOG_DEBUG, "TestClient: 2. Send DTMF 2\n");
197 res = ast_dtmf_stream(chan, NULL, "2", 0);
198 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
203 /* Step 3: Wait one second */
205 ast_log(LOG_DEBUG, "TestClient: 3. Wait one second\n");
206 res = ast_safe_sleep(chan, 1000);
207 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
212 /* Step 4: Measure noise */
214 ast_log(LOG_DEBUG, "TestClient: 4. Measure noise\n");
215 res = measurenoise(chan, 5000, "TestClient");
216 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
221 /* Step 5: Wait for "4" */
223 ast_log(LOG_DEBUG, "TestClient: 5. Wait DTMF 4\n");
224 res = ast_waitfordigit(chan, 3000);
225 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
232 /* Step 6: Transmit tone noise */
234 ast_log(LOG_DEBUG, "TestClient: 6. Transmit tone\n");
235 res = sendnoise(chan, 6000);
236 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
238 if (!res || (res == '5')) {
239 /* Step 7: Wait for "5" */
241 ast_log(LOG_DEBUG, "TestClient: 7. Wait DTMF 5\n");
243 res = ast_waitfordigit(chan, 3000);
244 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
251 /* Step 8: Wait one second */
253 ast_log(LOG_DEBUG, "TestClient: 8. Wait one second\n");
254 res = ast_safe_sleep(chan, 1000);
255 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
260 /* Step 9: Measure noise */
262 ast_log(LOG_DEBUG, "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" */
271 ast_log(LOG_DEBUG, "TestClient: 7. Send DTMF 7\n");
272 res = ast_dtmf_stream(chan, NULL, "7", 0);
273 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
278 /* Step 11: Wait for "8" */
280 ast_log(LOG_DEBUG, "TestClient: 11. Wait DTMF 8\n");
281 res = ast_waitfordigit(chan, 3000);
282 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
288 if (option_debug && !res ) {
289 /* Step 12: Hangup! */
290 ast_log(LOG_DEBUG, "TestClient: 12. Hangup\n");
294 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
295 fprintf(f, "-- END TEST--\n");
301 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
304 LOCAL_USER_REMOVE(u);
308 static int testserver_exec(struct ast_channel *chan, void *data)
316 if (chan->_state != AST_STATE_UP)
317 res = ast_answer(chan);
320 ast_log(LOG_DEBUG, "Read client version\n");
322 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
326 ast_log(LOG_DEBUG, "client version: %s\n", testid);
327 ast_log(LOG_DEBUG, "Transmit server version\n");
329 res = ast_safe_sleep(chan, 1000);
331 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
336 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
338 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
339 /* Check for sneakyness */
340 if (strchr(testid, '/'))
342 if ((res >=0) && (!ast_strlen_zero(testid))) {
343 /* Got a Test ID! Whoo hoo! */
344 /* Make the directory to hold the test results in case it's not there */
345 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
347 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
348 if ((f = fopen(fn, "w+"))) {
350 fprintf(f, "SERVERCHAN: %s\n", chan->name);
351 fprintf(f, "SERVERTEST ID: %s\n", testid);
352 fprintf(f, "ANSWER: PASS\n");
353 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
354 res = ast_safe_sleep(chan, 1000);
356 /* Step 1: Send "1" */
358 ast_log(LOG_DEBUG, "TestServer: 1. Send DTMF 1\n");
359 res = ast_dtmf_stream(chan, NULL, "1", 0);
360 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
365 /* Step 2: Wait for "2" */
367 ast_log(LOG_DEBUG, "TestServer: 2. Wait DTMF 2\n");
368 res = ast_waitfordigit(chan, 3000);
369 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
376 /* Step 3: Measure noise */
378 ast_log(LOG_DEBUG, "TestServer: 3. Measure noise\n");
379 res = measurenoise(chan, 6000, "TestServer");
380 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
385 /* Step 4: Send "4" */
387 ast_log(LOG_DEBUG, "TestServer: 4. Send DTMF 4\n");
388 res = ast_dtmf_stream(chan, NULL, "4", 0);
389 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
395 /* Step 5: Wait one second */
397 ast_log(LOG_DEBUG, "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");
405 /* Step 6: Measure noise */
407 ast_log(LOG_DEBUG, "TestServer: 6. Measure tone\n");
408 res = measurenoise(chan, 4000, "TestServer");
409 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
415 /* Step 7: Send "5" */
417 ast_log(LOG_DEBUG, "TestServer: 7. Send DTMF 5\n");
418 res = ast_dtmf_stream(chan, NULL, "5", 0);
419 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
425 /* Step 8: Transmit tone noise */
427 ast_log(LOG_DEBUG, "TestServer: 8. Transmit tone\n");
428 res = sendnoise(chan, 6000);
429 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
432 if (!res || (res == '7')) {
433 /* Step 9: Wait for "7" */
435 ast_log(LOG_DEBUG, "TestServer: 9. Wait DTMF 7\n");
437 res = ast_waitfordigit(chan, 3000);
438 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
445 res = ast_safe_sleep(chan, 1000);
447 /* Step 10: Send "8" */
449 ast_log(LOG_DEBUG, "TestServer: 10. Send DTMF 8\n");
450 res = ast_dtmf_stream(chan, NULL, "8", 0);
451 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
456 /* Step 11: Wait for hangup to arrive! */
458 ast_log(LOG_DEBUG, "TestServer: 11. Waiting for hangup\n");
459 res = ast_safe_sleep(chan, 10000);
460 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
463 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
464 fprintf(f, "-- END TEST--\n");
470 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
473 LOCAL_USER_REMOVE(u);
477 int unload_module(void)
479 STANDARD_HANGUP_LOCALUSERS;
480 ast_unregister_application(testc_app);
481 return ast_unregister_application(tests_app);
484 int load_module(void)
486 ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
487 return ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
490 char *description(void)
498 STANDARD_USECOUNT(res);
504 return ASTERISK_GPL_KEY;