2 * Asterisk -- A telephony toolkit for Linux.
4 * Applications to test connection and produce report in text file
6 * Copyright (C) 2004, 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>
16 #include <asterisk/channel.h>
17 #include <asterisk/options.h>
18 #include <asterisk/module.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/lock.h>
21 #include <asterisk/app.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/utils.h>
28 #include "../astconf.h"
31 static char *tdesc = "Interface Test Application";
33 static char *tests_descrip =
34 "TestServer(): Perform test server function and write call report.\n"
35 "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
36 static char *tests_app = "TestServer";
37 static char *tests_synopsis = "Execute Interface Test Server";
39 static char *testc_descrip =
40 "TestClient(testid): Executes test client with given testid.\n"
41 "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
43 static char *testc_app = "TestClient";
44 static char *testc_synopsis = "Execute Interface Test Client";
46 static int measurenoise(struct ast_channel *chan, int ms, char *who)
54 struct timeval start, tv;
57 rformat = chan->readformat;
58 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
59 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
62 gettimeofday(&start, NULL);
64 gettimeofday(&tv, NULL);
65 mssofar = (tv.tv_sec - start.tv_sec) * 1000;
66 mssofar += (tv.tv_usec - start.tv_usec) / 1000;
69 res = ast_waitfor(chan, ms - mssofar);
77 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
78 foo = (short *)f->data;
79 for (x=0;x<f->samples;x++) {
87 if (ast_set_read_format(chan, rformat)) {
88 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
95 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
98 ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
99 return (noise / samples);
102 static int sendnoise(struct ast_channel *chan, int ms)
105 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
107 res = ast_waitfordigit(chan, ms);
108 ast_tonepair_stop(chan);
117 static int testclient_exec(struct ast_channel *chan, void *data)
127 /* Check for test id */
128 if (!testid || ast_strlen_zero(testid)) {
129 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
133 if (chan->_state != AST_STATE_UP)
134 res = ast_answer(chan);
136 /* Wait a few just to be sure things get started */
137 res = ast_safe_sleep(chan, 3000);
138 /* Transmit client version */
140 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
142 ast_log(LOG_DEBUG, "Transmit client version\n");
144 /* Read server version */
146 ast_log(LOG_DEBUG, "Read server version\n");
148 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
152 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
158 res = ast_safe_sleep(chan, 1000);
161 res = ast_dtmf_stream(chan, NULL, testid, 0);
163 res = ast_dtmf_stream(chan, NULL, "#", 0);
165 ast_log(LOG_DEBUG, "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" */
182 ast_log(LOG_DEBUG, "TestClient: 2. Wait DTMF 1\n");
183 res = ast_waitfordigit(chan, 3000);
184 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
191 res = ast_safe_sleep(chan, 1000);
193 /* Step 2: Send "2" */
195 ast_log(LOG_DEBUG, "TestClient: 2. Send DTMF 2\n");
196 res = ast_dtmf_stream(chan, NULL, "2", 0);
197 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
202 /* Step 3: Wait one second */
204 ast_log(LOG_DEBUG, "TestClient: 3. Wait one second\n");
205 res = ast_safe_sleep(chan, 1000);
206 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
211 /* Step 4: Measure noise */
213 ast_log(LOG_DEBUG, "TestClient: 4. Measure noise\n");
214 res = measurenoise(chan, 5000, "TestClient");
215 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
220 /* Step 5: Wait for "4" */
222 ast_log(LOG_DEBUG, "TestClient: 5. Wait DTMF 4\n");
223 res = ast_waitfordigit(chan, 3000);
224 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
231 /* Step 6: Transmit tone noise */
233 ast_log(LOG_DEBUG, "TestClient: 6. Transmit tone\n");
234 res = sendnoise(chan, 6000);
235 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
237 if (!res || (res == '5')) {
238 /* Step 7: Wait for "5" */
240 ast_log(LOG_DEBUG, "TestClient: 7. Wait DTMF 5\n");
242 res = ast_waitfordigit(chan, 3000);
243 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
250 /* Step 8: Wait one second */
252 ast_log(LOG_DEBUG, "TestClient: 8. Wait one second\n");
253 res = ast_safe_sleep(chan, 1000);
254 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
259 /* Step 9: Measure noise */
261 ast_log(LOG_DEBUG, "TestClient: 6. Measure tone\n");
262 res = measurenoise(chan, 4000, "TestClient");
263 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
268 /* Step 10: Send "7" */
270 ast_log(LOG_DEBUG, "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" */
279 ast_log(LOG_DEBUG, "TestClient: 11. Wait DTMF 8\n");
280 res = ast_waitfordigit(chan, 3000);
281 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
287 if (option_debug && !res ) {
288 /* Step 12: Hangup! */
289 ast_log(LOG_DEBUG, "TestClient: 12. Hangup\n");
293 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
294 fprintf(f, "-- END TEST--\n");
300 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
303 LOCAL_USER_REMOVE(u);
307 static int testserver_exec(struct ast_channel *chan, void *data)
315 if (chan->_state != AST_STATE_UP)
316 res = ast_answer(chan);
319 ast_log(LOG_DEBUG, "Read client version\n");
321 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
325 ast_log(LOG_DEBUG, "client version: %s\n", testid);
326 ast_log(LOG_DEBUG, "Transmit server version\n");
328 res = ast_safe_sleep(chan, 1000);
330 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
335 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
337 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
338 /* Check for sneakyness */
339 if (strchr(testid, '/'))
341 if ((res >=0) && (!ast_strlen_zero(testid))) {
342 /* Got a Test ID! Whoo hoo! */
343 /* Make the directory to hold the test results in case it's not there */
344 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
346 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
347 if ((f = fopen(fn, "w+"))) {
349 fprintf(f, "SERVERCHAN: %s\n", chan->name);
350 fprintf(f, "SERVERTEST ID: %s\n", testid);
351 fprintf(f, "ANSWER: PASS\n");
352 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
353 res = ast_safe_sleep(chan, 1000);
355 /* Step 1: Send "1" */
357 ast_log(LOG_DEBUG, "TestServer: 1. Send DTMF 1\n");
358 res = ast_dtmf_stream(chan, NULL, "1", 0);
359 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
364 /* Step 2: Wait for "2" */
366 ast_log(LOG_DEBUG, "TestServer: 2. Wait DTMF 2\n");
367 res = ast_waitfordigit(chan, 3000);
368 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
375 /* Step 3: Measure noise */
377 ast_log(LOG_DEBUG, "TestServer: 3. Measure noise\n");
378 res = measurenoise(chan, 6000, "TestServer");
379 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
384 /* Step 4: Send "4" */
386 ast_log(LOG_DEBUG, "TestServer: 4. Send DTMF 4\n");
387 res = ast_dtmf_stream(chan, NULL, "4", 0);
388 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
394 /* Step 5: Wait one second */
396 ast_log(LOG_DEBUG, "TestServer: 5. Wait one second\n");
397 res = ast_safe_sleep(chan, 1000);
398 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
404 /* Step 6: Measure noise */
406 ast_log(LOG_DEBUG, "TestServer: 6. Measure tone\n");
407 res = measurenoise(chan, 4000, "TestServer");
408 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
414 /* Step 7: Send "5" */
416 ast_log(LOG_DEBUG, "TestServer: 7. Send DTMF 5\n");
417 res = ast_dtmf_stream(chan, NULL, "5", 0);
418 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
424 /* Step 8: Transmit tone noise */
426 ast_log(LOG_DEBUG, "TestServer: 8. Transmit tone\n");
427 res = sendnoise(chan, 6000);
428 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
431 if (!res || (res == '7')) {
432 /* Step 9: Wait for "7" */
434 ast_log(LOG_DEBUG, "TestServer: 9. Wait DTMF 7\n");
436 res = ast_waitfordigit(chan, 3000);
437 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
444 res = ast_safe_sleep(chan, 1000);
446 /* Step 10: Send "8" */
448 ast_log(LOG_DEBUG, "TestServer: 10. Send DTMF 8\n");
449 res = ast_dtmf_stream(chan, NULL, "8", 0);
450 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
455 /* Step 11: Wait for hangup to arrive! */
457 ast_log(LOG_DEBUG, "TestServer: 11. Waiting for hangup\n");
458 res = ast_safe_sleep(chan, 10000);
459 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
462 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
463 fprintf(f, "-- END TEST--\n");
469 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
472 LOCAL_USER_REMOVE(u);
476 int unload_module(void)
478 STANDARD_HANGUP_LOCALUSERS;
479 ast_unregister_application(testc_app);
480 return ast_unregister_application(tests_app);
483 int load_module(void)
485 ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
486 return ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
489 char *description(void)
497 STANDARD_USECOUNT(res);
503 return ASTERISK_GPL_KEY;