Merged revisions 126573 via svnmerge from
[asterisk-bristuff.git] / apps / app_test.c
blobe873777e41be4c4bdd531d6ed77d4407717af5a7
1 /*
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.
20 /*! \file
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
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <sys/stat.h>
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)
59 int res=0;
60 int mssofar;
61 int noise=0;
62 int samples=0;
63 int x;
64 short *foo;
65 struct timeval start;
66 struct ast_frame *f;
67 int rformat;
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");
71 return -1;
73 start = ast_tvnow();
74 for(;;) {
75 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
76 if (mssofar > ms)
77 break;
78 res = ast_waitfor(chan, ms - mssofar);
79 if (res < 1)
80 break;
81 f = ast_read(chan);
82 if (!f) {
83 res = -1;
84 break;
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++) {
89 noise += abs(foo[x]);
90 samples++;
93 ast_frfree(f);
96 if (rformat) {
97 if (ast_set_read_format(chan, rformat)) {
98 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
99 return -1;
102 if (res < 0)
103 return res;
104 if (!samples) {
105 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
106 return -1;
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)
114 int res;
115 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
116 if (!res) {
117 res = ast_waitfordigit(chan, ms);
118 ast_tonepair_stop(chan);
120 return res;
123 static int testclient_exec(struct ast_channel *chan, void *data)
125 int res = 0;
126 char *testid=data;
127 char fn[80];
128 char serverver[80];
129 FILE *f;
131 /* Check for test id */
132 if (ast_strlen_zero(testid)) {
133 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
134 return -1;
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 */
143 if (!res)
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");
149 if (!res)
150 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
151 if (res > 0)
152 res = 0;
153 ast_debug(1, "server version: %s\n", serverver);
155 if (res > 0)
156 res = 0;
158 if (!res)
159 res = ast_safe_sleep(chan, 1000);
160 /* Send test id */
161 if (!res)
162 res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
163 if (!res)
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);
170 ast_mkdir(fn, 0777);
171 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
172 if ((f = fopen(fn, "w+"))) {
173 setlinebuf(f);
174 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
175 fprintf(f, "CLIENTTEST ID: %s\n", testid);
176 fprintf(f, "ANSWER: PASS\n");
177 res = 0;
179 if (!res) {
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");
184 if (res == '1')
185 res = 0;
186 else
187 res = -1;
189 if (!res)
190 res = ast_safe_sleep(chan, 1000);
191 if (!res) {
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");
196 if (res > 0)
197 res = 0;
199 if (!res) {
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");
204 if (res > 0)
205 res = 0;
207 if (!res) {
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);
212 if (res > 0)
213 res = 0;
215 if (!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");
220 if (res == '4')
221 res = 0;
222 else
223 res = -1;
225 if (!res) {
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");
234 if (!res)
235 res = ast_waitfordigit(chan, 3000);
236 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
237 if (res == '5')
238 res = 0;
239 else
240 res = -1;
242 if (!res) {
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");
247 if (res > 0)
248 res = 0;
250 if (!res) {
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);
255 if (res > 0)
256 res = 0;
258 if (!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");
263 if (res > 0)
264 res =0;
266 if (!res) {
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");
271 if (res == '8')
272 res = 0;
273 else
274 res = -1;
276 if (!res) {
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");
283 fclose(f);
284 res = -1;
285 } else
286 res = -1;
287 } else {
288 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
289 res = -1;
291 return res;
294 static int testserver_exec(struct ast_channel *chan, void *data)
296 int res = 0;
297 char testid[80]="";
298 char fn[80];
299 FILE *f;
300 if (chan->_state != AST_STATE_UP)
301 res = ast_answer(chan);
302 /* Read version */
303 ast_debug(1, "Read client version\n");
304 if (!res)
305 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
306 if (res > 0)
307 res = 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);
313 if (!res)
314 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
315 if (res > 0)
316 res = 0;
318 if (!res)
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, '/'))
323 res = -1;
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);
328 ast_mkdir(fn, 0777);
329 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
330 if ((f = fopen(fn, "w+"))) {
331 setlinebuf(f);
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);
337 if (!res) {
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");
342 if (res > 0)
343 res = 0;
345 if (!res) {
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");
350 if (res == '2')
351 res = 0;
352 else
353 res = -1;
355 if (!res) {
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);
360 if (res > 0)
361 res = 0;
363 if (!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");
368 if (res > 0)
369 res = 0;
372 if (!res) {
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");
377 if (res > 0)
378 res = 0;
381 if (!res) {
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);
386 if (res > 0)
387 res = 0;
390 if (!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");
395 if (res > 0)
396 res = 0;
399 if (!res) {
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");
409 if (!res)
410 res = ast_waitfordigit(chan, 3000);
411 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
412 if (res == '7')
413 res = 0;
414 else
415 res = -1;
417 if (!res)
418 res = ast_safe_sleep(chan, 1000);
419 if (!res) {
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");
424 if (res > 0)
425 res = 0;
427 if (!res) {
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");
436 fclose(f);
437 res = -1;
438 } else
439 res = -1;
440 } else {
441 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
442 res = -1;
444 return res;
447 static int unload_module(void)
449 int res;
451 res = ast_unregister_application(testc_app);
452 res |= ast_unregister_application(tests_app);
454 return res;
457 static int load_module(void)
459 int res;
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);
464 return res;
467 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");