Asterisk - The Open Source Telephony Project GIT-master-a358458
Functions | Variables
app_test.c File Reference

Applications to test connection and produce report in text file. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/format_cache.h"
Include dependency graph for app_test.c:

Go to the source code of this file.

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Interface Test Application")
 
static int load_module (void)
 
static int measurenoise (struct ast_channel *chan, int ms, char *who)
 
static int sendnoise (struct ast_channel *chan, int ms)
 
static int testclient_exec (struct ast_channel *chan, const char *data)
 
static int testserver_exec (struct ast_channel *chan, const char *data)
 
static int unload_module (void)
 

Variables

static char * testc_app = "TestClient"
 
static char * tests_app = "TestServer"
 

Detailed Description

Applications to test connection and produce report in text file.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Russell Bryant russe.nosp@m.lb@c.nosp@m.lemso.nosp@m.n.ed.nosp@m.u

Definition in file app_test.c.

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Interface Test Application"   
)

◆ load_module()

static int load_module ( void  )
static

Definition at line 490 of file app_test.c.

491{
492 int res;
493
496
497 return res;
498}
static int testserver_exec(struct ast_channel *chan, const char *data)
Definition: app_test.c:329
static char * testc_app
Definition: app_test.c:81
static char * tests_app
Definition: app_test.c:80
static int testclient_exec(struct ast_channel *chan, const char *data)
Definition: app_test.c:154
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

References ast_register_application_xml, testc_app, testclient_exec(), tests_app, and testserver_exec().

◆ measurenoise()

static int measurenoise ( struct ast_channel chan,
int  ms,
char *  who 
)
static

Definition at line 83 of file app_test.c.

84{
85 int res=0;
86 int mssofar;
87 int noise=0;
88 int samples=0;
89 int x;
90 short *foo;
91 struct timeval start;
92 struct ast_frame *f;
93 struct ast_format *rformat;
94
95 rformat = ao2_bump(ast_channel_readformat(chan));
97 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
98 ao2_cleanup(rformat);
99 return -1;
100 }
101 start = ast_tvnow();
102 for(;;) {
103 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
104 if (mssofar > ms)
105 break;
106 res = ast_waitfor(chan, ms - mssofar);
107 if (res < 1)
108 break;
109 f = ast_read(chan);
110 if (!f) {
111 res = -1;
112 break;
113 }
114 if ((f->frametype == AST_FRAME_VOICE) &&
116 foo = (short *)f->data.ptr;
117 for (x=0;x<f->samples;x++) {
118 noise += abs(foo[x]);
119 samples++;
120 }
121 }
122 ast_frfree(f);
123 }
124
125 if (rformat) {
126 if (ast_set_read_format(chan, rformat)) {
127 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
128 ao2_ref(rformat, -1);
129 return -1;
130 }
131 ao2_ref(rformat, -1);
132 }
133 if (res < 0)
134 return res;
135 if (!samples) {
136 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
137 return -1;
138 }
139 ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
140 return (noise / samples);
141}
#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
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5762
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
#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
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
#define ast_frfree(fr)
@ AST_FRAME_VOICE
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_NOTICE
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
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

References abs, ao2_bump, ao2_cleanup, ao2_ref, ast_channel_readformat(), ast_debug, ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_format_slin, AST_FRAME_VOICE, ast_frfree, ast_log, ast_read(), ast_set_read_format(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_frame::data, ast_frame_subclass::format, ast_frame::frametype, LOG_NOTICE, ast_frame::ptr, ast_frame::samples, and ast_frame::subclass.

Referenced by testclient_exec(), and testserver_exec().

◆ sendnoise()

static int sendnoise ( struct ast_channel chan,
int  ms 
)
static

Definition at line 143 of file app_test.c.

144{
145 int res;
146 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
147 if (!res) {
148 res = ast_waitfordigit(chan, ms);
149 ast_tonepair_stop(chan);
150 }
151 return res;
152}
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3175
int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7582
void ast_tonepair_stop(struct ast_channel *chan)
Definition: channel.c:7595

References ast_tonepair_start(), ast_tonepair_stop(), and ast_waitfordigit().

Referenced by testclient_exec(), and testserver_exec().

◆ testclient_exec()

static int testclient_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 154 of file app_test.c.

155{
156 int res = 0;
157 const char *testid=data;
158 char fn[80];
159 char serverver[80];
160 FILE *f;
161
162 /* Check for test id */
163 if (ast_strlen_zero(testid)) {
164 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
165 return -1;
166 }
167
168 if (ast_channel_state(chan) != AST_STATE_UP)
169 res = ast_answer(chan);
170
171 /* Wait a few just to be sure things get started */
172 res = ast_safe_sleep(chan, 3000);
173 /* Transmit client version */
174 if (!res)
175 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
176 ast_debug(1, "Transmit client version\n");
177
178 /* Read server version */
179 ast_debug(1, "Read server version\n");
180 if (!res)
181 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
182 if (res > 0)
183 res = 0;
184 ast_debug(1, "server version: %s\n", serverver);
185
186 if (res > 0)
187 res = 0;
188
189 if (!res)
190 res = ast_safe_sleep(chan, 1000);
191 /* Send test id */
192 if (!res)
193 res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
194 if (!res)
195 res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
196 ast_debug(1, "send test identifier: %s\n", testid);
197
198 if ((res >=0) && (!ast_strlen_zero(testid))) {
199 /* Make the directory to hold the test results in case it's not there */
200 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
201 ast_mkdir(fn, 0777);
202 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
203 if ((f = fopen(fn, "w+"))) {
204 setlinebuf(f);
205 fprintf(f, "CLIENTCHAN: %s\n", ast_channel_name(chan));
206 fprintf(f, "CLIENTTEST ID: %s\n", testid);
207 fprintf(f, "ANSWER: PASS\n");
208 res = 0;
209
210 if (!res) {
211 /* Step 1: Wait for "1" */
212 ast_debug(1, "TestClient: 2. Wait DTMF 1\n");
213 res = ast_waitfordigit(chan, 3000);
214 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
215 if (res == '1')
216 res = 0;
217 else
218 res = -1;
219 }
220 if (!res) {
221 res = ast_safe_sleep(chan, 1000);
222 }
223 if (!res) {
224 /* Step 2: Send "2" */
225 ast_debug(1, "TestClient: 2. Send DTMF 2\n");
226 res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
227 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
228 if (res > 0)
229 res = 0;
230 }
231 if (!res) {
232 /* Step 3: Wait one second */
233 ast_debug(1, "TestClient: 3. Wait one second\n");
234 res = ast_safe_sleep(chan, 1000);
235 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
236 if (res > 0)
237 res = 0;
238 }
239 if (!res) {
240 /* Step 4: Measure noise */
241 ast_debug(1, "TestClient: 4. Measure noise\n");
242 res = measurenoise(chan, 5000, "TestClient");
243 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
244 if (res > 0)
245 res = 0;
246 }
247 if (!res) {
248 /* Step 5: Wait for "4" */
249 ast_debug(1, "TestClient: 5. Wait DTMF 4\n");
250 res = ast_waitfordigit(chan, 3000);
251 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
252 if (res == '4')
253 res = 0;
254 else
255 res = -1;
256 }
257 if (!res) {
258 /* Step 6: Transmit tone noise */
259 ast_debug(1, "TestClient: 6. Transmit tone\n");
260 res = sendnoise(chan, 6000);
261 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
262 }
263 if (!res || (res == '5')) {
264 /* Step 7: Wait for "5" */
265 ast_debug(1, "TestClient: 7. Wait DTMF 5\n");
266 if (!res)
267 res = ast_waitfordigit(chan, 3000);
268 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
269 if (res == '5')
270 res = 0;
271 else
272 res = -1;
273 }
274 if (!res) {
275 /* Step 8: Wait one second */
276 ast_debug(1, "TestClient: 8. Wait one second\n");
277 res = ast_safe_sleep(chan, 1000);
278 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
279 if (res > 0)
280 res = 0;
281 }
282 if (!res) {
283 /* Step 9: Measure noise */
284 ast_debug(1, "TestClient: 9. Measure tone\n");
285 res = measurenoise(chan, 4000, "TestClient");
286 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
287 if (res > 0)
288 res = 0;
289 }
290 if (!res) {
291 /* Step 10: Send "7" */
292 ast_debug(1, "TestClient: 10. Send DTMF 7\n");
293 res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
294 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
295 if (res > 0)
296 res =0;
297 }
298 if (!res) {
299 /* Step 11: Wait for "8" */
300 ast_debug(1, "TestClient: 11. Wait DTMF 8\n");
301 res = ast_waitfordigit(chan, 3000);
302 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
303 if (res == '8')
304 res = 0;
305 else
306 res = -1;
307 }
308 if (!res) {
309 res = ast_safe_sleep(chan, 1000);
310 }
311 if (!res) {
312 /* Step 12: Hangup! */
313 ast_debug(1, "TestClient: 12. Hangup\n");
314 }
315
316 ast_debug(1, "-- TEST COMPLETE--\n");
317 fprintf(f, "-- END TEST--\n");
318 fclose(f);
319 res = -1;
320 } else
321 res = -1;
322 } else {
323 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
324 res = -1;
325 }
326 return res;
327}
static int sendnoise(struct ast_channel *chan, int ms)
Definition: app_test.c:143
static int measurenoise(struct ast_channel *chan, int ms, char *who)
Definition: app_test.c:83
const char * ast_channel_name(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2805
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
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 LOG_WARNING
const char * ast_config_AST_LOG_DIR
Definition: options.c:159
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479

References ast_answer(), ast_app_getdata(), ast_channel_name(), ast_config_AST_LOG_DIR, ast_debug, ast_dtmf_stream(), ast_log, ast_mkdir(), ast_safe_sleep(), AST_STATE_UP, ast_strlen_zero(), ast_waitfordigit(), LOG_NOTICE, LOG_WARNING, measurenoise(), NULL, and sendnoise().

Referenced by load_module().

◆ testserver_exec()

static int testserver_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 329 of file app_test.c.

330{
331 int res = 0;
332 char testid[80]="";
333 FILE *f;
334 if (ast_channel_state(chan) != AST_STATE_UP)
335 res = ast_answer(chan);
336 /* Read version */
337 ast_debug(1, "Read client version\n");
338 if (!res)
339 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
340 if (res > 0)
341 res = 0;
342
343 ast_debug(1, "client version: %s\n", testid);
344 ast_debug(1, "Transmit server version\n");
345
346 res = ast_safe_sleep(chan, 1000);
347 if (!res)
348 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
349 if (res > 0)
350 res = 0;
351
352 if (!res)
353 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
354 ast_debug(1, "read test identifier: %s\n", testid);
355 /* Check for sneakiness */
356 if (strchr(testid, '/'))
357 res = -1;
358 if ((res >=0) && (!ast_strlen_zero(testid))) {
359 char fn[PATH_MAX];
360
361 /* Got a Test ID! Whoo hoo! */
362 /* Make the directory to hold the test results in case it's not there */
363 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
364 ast_mkdir(fn, 0777);
365 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
366 if ((f = fopen(fn, "w+"))) {
367 setlinebuf(f);
368 fprintf(f, "SERVERCHAN: %s\n", ast_channel_name(chan));
369 fprintf(f, "SERVERTEST ID: %s\n", testid);
370 fprintf(f, "ANSWER: PASS\n");
371 ast_debug(1, "Processing Test ID '%s'\n", testid);
372 res = ast_safe_sleep(chan, 1000);
373 if (!res) {
374 /* Step 1: Send "1" */
375 ast_debug(1, "TestServer: 1. Send DTMF 1\n");
376 res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
377 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
378 if (res > 0)
379 res = 0;
380 }
381 if (!res) {
382 /* Step 2: Wait for "2" */
383 ast_debug(1, "TestServer: 2. Wait DTMF 2\n");
384 res = ast_waitfordigit(chan, 3000);
385 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
386 if (res == '2')
387 res = 0;
388 else
389 res = -1;
390 }
391 if (!res) {
392 /* Step 3: Measure noise */
393 ast_debug(1, "TestServer: 3. Measure noise\n");
394 res = measurenoise(chan, 6000, "TestServer");
395 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
396 if (res > 0)
397 res = 0;
398 }
399 if (!res) {
400 /* Step 4: Send "4" */
401 ast_debug(1, "TestServer: 4. Send DTMF 4\n");
402 res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
403 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
404 if (res > 0)
405 res = 0;
406 }
407 if (!res) {
408 /* Step 5: Wait one second */
409 ast_debug(1, "TestServer: 5. Wait one second\n");
410 res = ast_safe_sleep(chan, 1000);
411 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
412 if (res > 0)
413 res = 0;
414 }
415 if (!res) {
416 /* Step 6: Measure noise */
417 ast_debug(1, "TestServer: 6. Measure tone\n");
418 res = measurenoise(chan, 4000, "TestServer");
419 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
420 if (res > 0)
421 res = 0;
422 }
423 if (!res) {
424 /* Step 7: Send "5" */
425 ast_debug(1, "TestServer: 7. Send DTMF 5\n");
426 res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
427 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
428 if (res > 0)
429 res = 0;
430 }
431 if (!res) {
432 /* Step 8: Transmit tone noise */
433 ast_debug(1, "TestServer: 8. Transmit tone\n");
434 res = sendnoise(chan, 6000);
435 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
436 }
437
438 if (!res || (res == '7')) {
439 /* Step 9: Wait for "7" */
440 ast_debug(1, "TestServer: 9. Wait DTMF 7\n");
441 if (!res)
442 res = ast_waitfordigit(chan, 3000);
443 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
444 if (res == '7')
445 res = 0;
446 else
447 res = -1;
448 }
449 if (!res) {
450 res = ast_safe_sleep(chan, 1000);
451 }
452 if (!res) {
453 /* Step 10: Send "8" */
454 ast_debug(1, "TestServer: 10. Send DTMF 8\n");
455 res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
456 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
457 if (res > 0)
458 res = 0;
459 }
460 if (!res) {
461 /* Step 11: Wait for hangup to arrive! */
462 ast_debug(1, "TestServer: 11. Waiting for hangup\n");
463 res = ast_safe_sleep(chan, 10000);
464 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
465 }
466
467 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
468 fprintf(f, "-- END TEST--\n");
469 fclose(f);
470 res = -1;
471 } else
472 res = -1;
473 } else {
474 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
475 res = -1;
476 }
477 return res;
478}
#define PATH_MAX
Definition: asterisk.h:40

References ast_answer(), ast_app_getdata(), ast_channel_name(), ast_config_AST_LOG_DIR, ast_debug, ast_dtmf_stream(), ast_log, ast_mkdir(), ast_safe_sleep(), AST_STATE_UP, ast_strlen_zero(), ast_waitfordigit(), LOG_NOTICE, measurenoise(), NULL, PATH_MAX, and sendnoise().

Referenced by load_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 480 of file app_test.c.

481{
482 int res;
483
486
487 return res;
488}
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References ast_unregister_application(), testc_app, and tests_app.

Variable Documentation

◆ testc_app

char* testc_app = "TestClient"
static

Definition at line 81 of file app_test.c.

Referenced by load_module(), and unload_module().

◆ tests_app

char* tests_app = "TestServer"
static

Definition at line 80 of file app_test.c.

Referenced by load_module(), and unload_module().