2 * Asterisk -- A telephony toolkit for Linux.
4 * Applictions connected with CDR engine
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/module.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/lock.h>
20 #include <asterisk/app.h>
21 #include <asterisk/pbx.h>
22 #include <asterisk/utils.h>
27 #include "../astconf.h"
30 static char *tdesc = "Make sure asterisk doesn't save CDR for a certain call";
32 static char *tests_descrip = "TestServer(): Perform test server function and write call report"
33 "Results stored in /var/log/asterisk/testreports/<testid>.txt";
34 static char *tests_app = "TestServer";
35 static char *tests_synopsis = "Execute Interface Test Server";
37 static char *testc_descrip = "TestClient(testid): Executes test client with given testid.\n"
38 "Results stored in /var/log/asterisk/testreports/<testid>.txt";
40 static char *testc_app = "TestClient";
41 static char *testc_synopsis = "Execute Interface Test Client";
43 static int measurenoise(struct ast_channel *chan, int ms, char *who)
51 struct timeval start, tv;
54 rformat = chan->readformat;
55 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
56 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
59 gettimeofday(&start, NULL);
61 gettimeofday(&tv, NULL);
62 mssofar = (tv.tv_sec - start.tv_sec) * 1000;
63 mssofar += (tv.tv_usec - start.tv_usec) / 1000;
66 res = ast_waitfor(chan, ms - mssofar);
74 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
75 foo = (short *)f->data;
76 for (x=0;x<f->samples;x++) {
84 if (ast_set_read_format(chan, rformat)) {
85 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
91 ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
92 return (noise / samples);
95 static int sendnoise(struct ast_channel *chan, int ms)
98 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
100 res = ast_waitfordigit(chan, ms);
101 ast_tonepair_stop(chan);
110 static int testclient_exec(struct ast_channel *chan, void *data)
120 /* Check for test id */
121 if (!testid || ast_strlen_zero(testid)) {
122 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
126 if (chan->_state != AST_STATE_UP)
127 res = ast_answer(chan);
129 /* Wait a few just to be sure things get started */
130 res = ast_safe_sleep(chan, 3000);
131 /* Transmit client version */
133 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
134 ast_log(LOG_DEBUG, "Transmit client version\n");
136 /* Read server version */
137 ast_log(LOG_DEBUG, "Read server version\n");
139 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
142 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
148 res = ast_safe_sleep(chan, 1000);
151 res = ast_dtmf_stream(chan, NULL, testid, 0);
153 res = ast_dtmf_stream(chan, NULL, "#", 0);
154 ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
156 if ((res >=0) && (!ast_strlen_zero(testid))) {
157 /* Make the directory to hold the test results in case it's not there */
158 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
160 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
161 if ((f = fopen(fn, "w+"))) {
163 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
164 fprintf(f, "CLIENTTEST ID: %s\n", testid);
165 fprintf(f, "ANSWER: PASS\n");
169 /* Step 1: Wait for "1" */
170 ast_log(LOG_DEBUG, "TestClient: 2. Wait DTMF 1\n");
171 res = ast_waitfordigit(chan, 3000);
172 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
179 res = ast_safe_sleep(chan, 1000);
181 /* Step 2: Send "2" */
182 ast_log(LOG_DEBUG, "TestClient: 2. Send DTMF 2\n");
183 res = ast_dtmf_stream(chan, NULL, "2", 0);
184 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
189 /* Step 3: Wait one second */
190 ast_log(LOG_DEBUG, "TestClient: 3. Wait one second\n");
191 res = ast_safe_sleep(chan, 1000);
192 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
197 /* Step 4: Measure noise */
198 ast_log(LOG_DEBUG, "TestClient: 4. Measure noise\n");
199 res = measurenoise(chan, 5000, "TestClient");
200 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
205 /* Step 5: Wait for "4" */
206 ast_log(LOG_DEBUG, "TestClient: 5. Wait DTMF 4\n");
207 res = ast_waitfordigit(chan, 3000);
208 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
215 /* Step 6: Transmit tone noise */
216 ast_log(LOG_DEBUG, "TestClient: 6. Transmit tone\n");
217 res = sendnoise(chan, 6000);
218 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
220 if (!res || (res == '5')) {
221 /* Step 7: Wait for "5" */
222 ast_log(LOG_DEBUG, "TestClient: 7. Wait DTMF 5\n");
224 res = ast_waitfordigit(chan, 3000);
225 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
232 /* Step 8: Wait one second */
233 ast_log(LOG_DEBUG, "TestClient: 8. Wait one second\n");
234 res = ast_safe_sleep(chan, 1000);
235 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
240 /* Step 9: Measure noise */
241 ast_log(LOG_DEBUG, "TestClient: 6. Measure tone\n");
242 res = measurenoise(chan, 4000, "TestClient");
243 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
248 /* Step 10: Send "7" */
249 ast_log(LOG_DEBUG, "TestClient: 7. Send DTMF 7\n");
250 res = ast_dtmf_stream(chan, NULL, "7", 0);
251 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
256 /* Step 11: Wait for "8" */
257 ast_log(LOG_DEBUG, "TestClient: 11. Wait DTMF 8\n");
258 res = ast_waitfordigit(chan, 3000);
259 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
266 /* Step 12: Hangup! */
267 ast_log(LOG_DEBUG, "TestClient: 12. Hangup\n");
270 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
271 fprintf(f, "-- END TEST--\n");
277 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
280 LOCAL_USER_REMOVE(u);
284 static int testserver_exec(struct ast_channel *chan, void *data)
292 if (chan->_state != AST_STATE_UP)
293 res = ast_answer(chan);
295 ast_log(LOG_DEBUG, "Read client version\n");
297 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
300 ast_log(LOG_DEBUG, "client version: %s\n", testid);
301 ast_log(LOG_DEBUG, "Transmit server version\n");
302 res = ast_safe_sleep(chan, 1000);
304 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
309 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
310 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
311 /* Check for sneakyness */
312 if (strchr(testid, '/'))
314 if ((res >=0) && (!ast_strlen_zero(testid))) {
315 /* Got a Test ID! Whoo hoo! */
316 /* Make the directory to hold the test results in case it's not there */
317 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
319 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
320 if ((f = fopen(fn, "w+"))) {
322 fprintf(f, "SERVERCHAN: %s\n", chan->name);
323 fprintf(f, "SERVERTEST ID: %s\n", testid);
324 fprintf(f, "ANSWER: PASS\n");
325 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
326 res = ast_safe_sleep(chan, 1000);
328 /* Step 1: Send "1" */
329 ast_log(LOG_DEBUG, "TestServer: 1. Send DTMF 1\n");
330 res = ast_dtmf_stream(chan, NULL, "1", 0);
331 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
336 /* Step 2: Wait for "2" */
337 ast_log(LOG_DEBUG, "TestServer: 2. Wait DTMF 2\n");
338 res = ast_waitfordigit(chan, 3000);
339 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
346 /* Step 3: Measure noise */
347 ast_log(LOG_DEBUG, "TestServer: 3. Measure noise\n");
348 res = measurenoise(chan, 6000, "TestServer");
349 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
354 /* Step 4: Send "4" */
355 ast_log(LOG_DEBUG, "TestServer: 4. Send DTMF 4\n");
356 res = ast_dtmf_stream(chan, NULL, "4", 0);
357 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
363 /* Step 5: Wait one second */
364 ast_log(LOG_DEBUG, "TestServer: 5. Wait one second\n");
365 res = ast_safe_sleep(chan, 1000);
366 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
372 /* Step 6: Measure noise */
373 ast_log(LOG_DEBUG, "TestServer: 6. Measure tone\n");
374 res = measurenoise(chan, 4000, "TestServer");
375 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
381 /* Step 7: Send "5" */
382 ast_log(LOG_DEBUG, "TestServer: 7. Send DTMF 5\n");
383 res = ast_dtmf_stream(chan, NULL, "5", 0);
384 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
390 /* Step 8: Transmit tone noise */
391 ast_log(LOG_DEBUG, "TestServer: 8. Transmit tone\n");
392 res = sendnoise(chan, 6000);
393 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
396 if (!res || (res == '7')) {
397 /* Step 9: Wait for "7" */
398 ast_log(LOG_DEBUG, "TestServer: 9. Wait DTMF 7\n");
400 res = ast_waitfordigit(chan, 3000);
401 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
408 res = ast_safe_sleep(chan, 1000);
410 /* Step 10: Send "8" */
411 ast_log(LOG_DEBUG, "TestServer: 10. Send DTMF 8\n");
412 res = ast_dtmf_stream(chan, NULL, "8", 0);
413 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
418 /* Step 11: Wait for hangup to arrive! */
419 ast_log(LOG_DEBUG, "TestServer: 11. Waiting for hangup\n");
420 res = ast_safe_sleep(chan, 10000);
421 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
424 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
425 fprintf(f, "-- END TEST--\n");
431 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
434 LOCAL_USER_REMOVE(u);
438 int unload_module(void)
440 STANDARD_HANGUP_LOCALUSERS;
441 ast_unregister_application(testc_app);
442 return ast_unregister_application(tests_app);
445 int load_module(void)
447 ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
448 return ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
451 char *description(void)
459 STANDARD_USECOUNT(res);
465 return ASTERISK_GPL_KEY;