Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_test.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 * Russell Bryant <russelb@clemson.edu>
8 *
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.
14 *
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.
18 */
19
20/*! \file
21 *
22 * \brief Applications to test connection and produce report in text file
23 *
24 * \author Mark Spencer <markster@digium.com>
25 * \author Russell Bryant <russelb@clemson.edu>
26 *
27 * \ingroup applications
28 */
29
30/*** MODULEINFO
31 <support_level>extended</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include <sys/stat.h>
37
38#include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
39#include "asterisk/channel.h"
40#include "asterisk/module.h"
41#include "asterisk/lock.h"
42#include "asterisk/app.h"
43#include "asterisk/pbx.h"
44#include "asterisk/utils.h"
46
47/*** DOCUMENTATION
48 <application name="TestServer" language="en_US">
49 <since>
50 <version>1.0.0</version>
51 </since>
52 <synopsis>
53 Execute Interface Test Server.
54 </synopsis>
55 <syntax />
56 <description>
57 <para>Perform test server function and write call report. Results stored in
58 <filename>/var/log/asterisk/testreports/&lt;testid&gt;-server.txt</filename></para>
59 </description>
60 <see-also>
61 <ref type="application">TestClient</ref>
62 </see-also>
63 </application>
64 <application name="TestClient" language="en_US">
65 <since>
66 <version>1.0.0</version>
67 </since>
68 <synopsis>
69 Execute Interface Test Client.
70 </synopsis>
71 <syntax>
72 <parameter name="testid" required="true">
73 <para>An ID to identify this test.</para>
74 </parameter>
75 </syntax>
76 <description>
77 <para>Executes test client with given <replaceable>testid</replaceable>. Results stored in
78 <filename>/var/log/asterisk/testreports/&lt;testid&gt;-client.txt</filename></para>
79 </description>
80 <see-also>
81 <ref type="application">TestServer</ref>
82 </see-also>
83 </application>
84 ***/
85
86static char *tests_app = "TestServer";
87static char *testc_app = "TestClient";
88
89static int measurenoise(struct ast_channel *chan, int ms, char *who)
90{
91 int res=0;
92 int mssofar;
93 int noise=0;
94 int samples=0;
95 int x;
96 short *foo;
97 struct timeval start;
98 struct ast_frame *f;
99 struct ast_format *rformat;
100
101 rformat = ao2_bump(ast_channel_readformat(chan));
103 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
104 ao2_cleanup(rformat);
105 return -1;
106 }
107 start = ast_tvnow();
108 for(;;) {
109 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
110 if (mssofar > ms)
111 break;
112 res = ast_waitfor(chan, ms - mssofar);
113 if (res < 1)
114 break;
115 f = ast_read(chan);
116 if (!f) {
117 res = -1;
118 break;
119 }
120 if ((f->frametype == AST_FRAME_VOICE) &&
122 foo = (short *)f->data.ptr;
123 for (x=0;x<f->samples;x++) {
124 noise += abs(foo[x]);
125 samples++;
126 }
127 }
128 ast_frfree(f);
129 }
130
131 if (rformat) {
132 if (ast_set_read_format(chan, rformat)) {
133 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
134 ao2_ref(rformat, -1);
135 return -1;
136 }
137 ao2_ref(rformat, -1);
138 }
139 if (res < 0)
140 return res;
141 if (!samples) {
142 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
143 return -1;
144 }
145 ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
146 return (noise / samples);
147}
148
149static int sendnoise(struct ast_channel *chan, int ms)
150{
151 int res;
152 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
153 if (!res) {
154 res = ast_waitfordigit(chan, ms);
155 ast_tonepair_stop(chan);
156 }
157 return res;
158}
159
160static int testclient_exec(struct ast_channel *chan, const char *data)
161{
162 int res = 0;
163 const char *testid=data;
164 char fn[80];
165 char serverver[80];
166 FILE *f;
167
168 /* Check for test id */
169 if (ast_strlen_zero(testid)) {
170 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
171 return -1;
172 }
173
174 if (ast_channel_state(chan) != AST_STATE_UP)
175 res = ast_answer(chan);
176
177 /* Wait a few just to be sure things get started */
178 res = ast_safe_sleep(chan, 3000);
179 /* Transmit client version */
180 if (!res)
181 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
182 ast_debug(1, "Transmit client version\n");
183
184 /* Read server version */
185 ast_debug(1, "Read server version\n");
186 if (!res)
187 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
188 if (res > 0)
189 res = 0;
190 ast_debug(1, "server version: %s\n", serverver);
191
192 if (res > 0)
193 res = 0;
194
195 if (!res)
196 res = ast_safe_sleep(chan, 1000);
197 /* Send test id */
198 if (!res)
199 res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
200 if (!res)
201 res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
202 ast_debug(1, "send test identifier: %s\n", testid);
203
204 if ((res >=0) && (!ast_strlen_zero(testid))) {
205 /* Make the directory to hold the test results in case it's not there */
206 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
207 ast_mkdir(fn, 0777);
208 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
209 if ((f = fopen(fn, "w+"))) {
210 setlinebuf(f);
211 fprintf(f, "CLIENTCHAN: %s\n", ast_channel_name(chan));
212 fprintf(f, "CLIENTTEST ID: %s\n", testid);
213 fprintf(f, "ANSWER: PASS\n");
214 res = 0;
215
216 if (!res) {
217 /* Step 1: Wait for "1" */
218 ast_debug(1, "TestClient: 2. Wait DTMF 1\n");
219 res = ast_waitfordigit(chan, 3000);
220 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
221 if (res == '1')
222 res = 0;
223 else
224 res = -1;
225 }
226 if (!res) {
227 res = ast_safe_sleep(chan, 1000);
228 }
229 if (!res) {
230 /* Step 2: Send "2" */
231 ast_debug(1, "TestClient: 2. Send DTMF 2\n");
232 res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
233 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
234 if (res > 0)
235 res = 0;
236 }
237 if (!res) {
238 /* Step 3: Wait one second */
239 ast_debug(1, "TestClient: 3. Wait one second\n");
240 res = ast_safe_sleep(chan, 1000);
241 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
242 if (res > 0)
243 res = 0;
244 }
245 if (!res) {
246 /* Step 4: Measure noise */
247 ast_debug(1, "TestClient: 4. Measure noise\n");
248 res = measurenoise(chan, 5000, "TestClient");
249 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
250 if (res > 0)
251 res = 0;
252 }
253 if (!res) {
254 /* Step 5: Wait for "4" */
255 ast_debug(1, "TestClient: 5. Wait DTMF 4\n");
256 res = ast_waitfordigit(chan, 3000);
257 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
258 if (res == '4')
259 res = 0;
260 else
261 res = -1;
262 }
263 if (!res) {
264 /* Step 6: Transmit tone noise */
265 ast_debug(1, "TestClient: 6. Transmit tone\n");
266 res = sendnoise(chan, 6000);
267 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
268 }
269 if (!res || (res == '5')) {
270 /* Step 7: Wait for "5" */
271 ast_debug(1, "TestClient: 7. Wait DTMF 5\n");
272 if (!res)
273 res = ast_waitfordigit(chan, 3000);
274 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
275 if (res == '5')
276 res = 0;
277 else
278 res = -1;
279 }
280 if (!res) {
281 /* Step 8: Wait one second */
282 ast_debug(1, "TestClient: 8. Wait one second\n");
283 res = ast_safe_sleep(chan, 1000);
284 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
285 if (res > 0)
286 res = 0;
287 }
288 if (!res) {
289 /* Step 9: Measure noise */
290 ast_debug(1, "TestClient: 9. Measure tone\n");
291 res = measurenoise(chan, 4000, "TestClient");
292 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
293 if (res > 0)
294 res = 0;
295 }
296 if (!res) {
297 /* Step 10: Send "7" */
298 ast_debug(1, "TestClient: 10. Send DTMF 7\n");
299 res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
300 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
301 if (res > 0)
302 res =0;
303 }
304 if (!res) {
305 /* Step 11: Wait for "8" */
306 ast_debug(1, "TestClient: 11. Wait DTMF 8\n");
307 res = ast_waitfordigit(chan, 3000);
308 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
309 if (res == '8')
310 res = 0;
311 else
312 res = -1;
313 }
314 if (!res) {
315 res = ast_safe_sleep(chan, 1000);
316 }
317 if (!res) {
318 /* Step 12: Hangup! */
319 ast_debug(1, "TestClient: 12. Hangup\n");
320 }
321
322 ast_debug(1, "-- TEST COMPLETE--\n");
323 fprintf(f, "-- END TEST--\n");
324 fclose(f);
325 res = -1;
326 } else
327 res = -1;
328 } else {
329 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
330 res = -1;
331 }
332 return res;
333}
334
335static int testserver_exec(struct ast_channel *chan, const char *data)
336{
337 int res = 0;
338 char testid[80]="";
339 FILE *f;
340 if (ast_channel_state(chan) != AST_STATE_UP)
341 res = ast_answer(chan);
342 /* Read version */
343 ast_debug(1, "Read client version\n");
344 if (!res)
345 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
346 if (res > 0)
347 res = 0;
348
349 ast_debug(1, "client version: %s\n", testid);
350 ast_debug(1, "Transmit server version\n");
351
352 res = ast_safe_sleep(chan, 1000);
353 if (!res)
354 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
355 if (res > 0)
356 res = 0;
357
358 if (!res)
359 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
360 ast_debug(1, "read test identifier: %s\n", testid);
361 /* Check for sneakiness */
362 if (strchr(testid, '/'))
363 res = -1;
364 if ((res >=0) && (!ast_strlen_zero(testid))) {
365 char fn[PATH_MAX];
366
367 /* Got a Test ID! Whoo hoo! */
368 /* Make the directory to hold the test results in case it's not there */
369 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
370 ast_mkdir(fn, 0777);
371 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
372 if ((f = fopen(fn, "w+"))) {
373 setlinebuf(f);
374 fprintf(f, "SERVERCHAN: %s\n", ast_channel_name(chan));
375 fprintf(f, "SERVERTEST ID: %s\n", testid);
376 fprintf(f, "ANSWER: PASS\n");
377 ast_debug(1, "Processing Test ID '%s'\n", testid);
378 res = ast_safe_sleep(chan, 1000);
379 if (!res) {
380 /* Step 1: Send "1" */
381 ast_debug(1, "TestServer: 1. Send DTMF 1\n");
382 res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
383 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
384 if (res > 0)
385 res = 0;
386 }
387 if (!res) {
388 /* Step 2: Wait for "2" */
389 ast_debug(1, "TestServer: 2. Wait DTMF 2\n");
390 res = ast_waitfordigit(chan, 3000);
391 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
392 if (res == '2')
393 res = 0;
394 else
395 res = -1;
396 }
397 if (!res) {
398 /* Step 3: Measure noise */
399 ast_debug(1, "TestServer: 3. Measure noise\n");
400 res = measurenoise(chan, 6000, "TestServer");
401 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
402 if (res > 0)
403 res = 0;
404 }
405 if (!res) {
406 /* Step 4: Send "4" */
407 ast_debug(1, "TestServer: 4. Send DTMF 4\n");
408 res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
409 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
410 if (res > 0)
411 res = 0;
412 }
413 if (!res) {
414 /* Step 5: Wait one second */
415 ast_debug(1, "TestServer: 5. Wait one second\n");
416 res = ast_safe_sleep(chan, 1000);
417 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
418 if (res > 0)
419 res = 0;
420 }
421 if (!res) {
422 /* Step 6: Measure noise */
423 ast_debug(1, "TestServer: 6. Measure tone\n");
424 res = measurenoise(chan, 4000, "TestServer");
425 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
426 if (res > 0)
427 res = 0;
428 }
429 if (!res) {
430 /* Step 7: Send "5" */
431 ast_debug(1, "TestServer: 7. Send DTMF 5\n");
432 res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
433 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
434 if (res > 0)
435 res = 0;
436 }
437 if (!res) {
438 /* Step 8: Transmit tone noise */
439 ast_debug(1, "TestServer: 8. Transmit tone\n");
440 res = sendnoise(chan, 6000);
441 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
442 }
443
444 if (!res || (res == '7')) {
445 /* Step 9: Wait for "7" */
446 ast_debug(1, "TestServer: 9. Wait DTMF 7\n");
447 if (!res)
448 res = ast_waitfordigit(chan, 3000);
449 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
450 if (res == '7')
451 res = 0;
452 else
453 res = -1;
454 }
455 if (!res) {
456 res = ast_safe_sleep(chan, 1000);
457 }
458 if (!res) {
459 /* Step 10: Send "8" */
460 ast_debug(1, "TestServer: 10. Send DTMF 8\n");
461 res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
462 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
463 if (res > 0)
464 res = 0;
465 }
466 if (!res) {
467 /* Step 11: Wait for hangup to arrive! */
468 ast_debug(1, "TestServer: 11. Waiting for hangup\n");
469 res = ast_safe_sleep(chan, 10000);
470 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
471 }
472
473 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
474 fprintf(f, "-- END TEST--\n");
475 fclose(f);
476 res = -1;
477 } else
478 res = -1;
479 } else {
480 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
481 res = -1;
482 }
483 return res;
484}
485
486static int unload_module(void)
487{
488 int res;
489
492
493 return res;
494}
495
496static int load_module(void)
497{
498 int res;
499
502
503 return res;
504}
505
static int testserver_exec(struct ast_channel *chan, const char *data)
Definition: app_test.c:335
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Interface Test Application")
static int sendnoise(struct ast_channel *chan, int ms)
Definition: app_test.c:149
static int measurenoise(struct ast_channel *chan, int ms, char *who)
Definition: app_test.c:89
static char * testc_app
Definition: app_test.c:87
static int load_module(void)
Definition: app_test.c:496
static char * tests_app
Definition: app_test.c:86
static int testclient_exec(struct ast_channel *chan, const char *data)
Definition: app_test.c:160
static int unload_module(void)
Definition: app_test.c:486
Asterisk main include file. File version handling, generic pbx functions.
#define PATH_MAX
Definition: asterisk.h:40
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
General Asterisk PBX channel definitions.
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3203
const char * ast_channel_name(const struct ast_channel *chan)
int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7603
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3190
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4274
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5779
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1601
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
void ast_tonepair_stop(struct ast_channel *chan)
Definition: channel.c:7616
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
#define abs(x)
Definition: f2c.h:195
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
enum ast_getdata_result ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:188
int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration)
Send a string of DTMF digits to a channel.
Definition: main/app.c:1127
#define ast_frfree(fr)
@ AST_FRAME_VOICE
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_NOTICE
#define LOG_WARNING
Asterisk locking-related definitions:
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_LOG_DIR
Definition: options.c:159
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
union ast_frame::@228 data
struct ast_frame_subclass subclass
enum ast_frame_type frametype
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
Utility functions.
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479