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"
Go to the source code of this file.
Functions | |
AST_MODULE_INFO_STANDARD (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" |
Applications to test connection and produce report in text file.
Definition in file app_test.c.
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"Interface Test Application" | ||||
) |
static int load_module | ( | void | ) | [static] |
Definition at line 485 of file app_test.c.
References ast_register_application_xml, testclient_exec(), and testserver_exec().
00486 { 00487 int res; 00488 00489 res = ast_register_application_xml(testc_app, testclient_exec); 00490 res |= ast_register_application_xml(tests_app, testserver_exec); 00491 00492 return res; 00493 }
static int measurenoise | ( | struct ast_channel * | chan, | |
int | ms, | |||
char * | who | |||
) | [static] |
Definition at line 84 of file app_test.c.
References ast_debug, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_set_read_format(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_frame_subclass::codec, ast_frame::data, f, ast_frame::frametype, LOG_NOTICE, ast_frame::ptr, ast_channel::readformat, ast_frame::samples, and ast_frame::subclass.
Referenced by testclient_exec(), and testserver_exec().
00085 { 00086 int res=0; 00087 int mssofar; 00088 int noise=0; 00089 int samples=0; 00090 int x; 00091 short *foo; 00092 struct timeval start; 00093 struct ast_frame *f; 00094 int rformat; 00095 rformat = chan->readformat; 00096 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) { 00097 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n"); 00098 return -1; 00099 } 00100 start = ast_tvnow(); 00101 for(;;) { 00102 mssofar = ast_tvdiff_ms(ast_tvnow(), start); 00103 if (mssofar > ms) 00104 break; 00105 res = ast_waitfor(chan, ms - mssofar); 00106 if (res < 1) 00107 break; 00108 f = ast_read(chan); 00109 if (!f) { 00110 res = -1; 00111 break; 00112 } 00113 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.codec == AST_FORMAT_SLINEAR)) { 00114 foo = (short *)f->data.ptr; 00115 for (x=0;x<f->samples;x++) { 00116 noise += abs(foo[x]); 00117 samples++; 00118 } 00119 } 00120 ast_frfree(f); 00121 } 00122 00123 if (rformat) { 00124 if (ast_set_read_format(chan, rformat)) { 00125 ast_log(LOG_NOTICE, "Unable to restore original format!\n"); 00126 return -1; 00127 } 00128 } 00129 if (res < 0) 00130 return res; 00131 if (!samples) { 00132 ast_log(LOG_NOTICE, "No samples were received from the other side!\n"); 00133 return -1; 00134 } 00135 ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples); 00136 return (noise / samples); 00137 }
static int sendnoise | ( | struct ast_channel * | chan, | |
int | ms | |||
) | [static] |
Definition at line 139 of file app_test.c.
References ast_tonepair_start(), ast_tonepair_stop(), and ast_waitfordigit().
Referenced by testclient_exec(), and testserver_exec().
00140 { 00141 int res; 00142 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192); 00143 if (!res) { 00144 res = ast_waitfordigit(chan, ms); 00145 ast_tonepair_stop(chan); 00146 } 00147 return res; 00148 }
static int testclient_exec | ( | struct ast_channel * | chan, | |
const char * | data | |||
) | [static] |
Definition at line 150 of file app_test.c.
References ast_channel::_state, ast_answer(), ast_app_getdata(), 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(), f, LOG_NOTICE, LOG_WARNING, measurenoise(), and sendnoise().
Referenced by load_module().
00151 { 00152 int res = 0; 00153 const char *testid=data; 00154 char fn[80]; 00155 char serverver[80]; 00156 FILE *f; 00157 00158 /* Check for test id */ 00159 if (ast_strlen_zero(testid)) { 00160 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n"); 00161 return -1; 00162 } 00163 00164 if (chan->_state != AST_STATE_UP) 00165 res = ast_answer(chan); 00166 00167 /* Wait a few just to be sure things get started */ 00168 res = ast_safe_sleep(chan, 3000); 00169 /* Transmit client version */ 00170 if (!res) 00171 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0); 00172 ast_debug(1, "Transmit client version\n"); 00173 00174 /* Read server version */ 00175 ast_debug(1, "Read server version\n"); 00176 if (!res) 00177 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0); 00178 if (res > 0) 00179 res = 0; 00180 ast_debug(1, "server version: %s\n", serverver); 00181 00182 if (res > 0) 00183 res = 0; 00184 00185 if (!res) 00186 res = ast_safe_sleep(chan, 1000); 00187 /* Send test id */ 00188 if (!res) 00189 res = ast_dtmf_stream(chan, NULL, testid, 0, 0); 00190 if (!res) 00191 res = ast_dtmf_stream(chan, NULL, "#", 0, 0); 00192 ast_debug(1, "send test identifier: %s\n", testid); 00193 00194 if ((res >=0) && (!ast_strlen_zero(testid))) { 00195 /* Make the directory to hold the test results in case it's not there */ 00196 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); 00197 ast_mkdir(fn, 0777); 00198 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid); 00199 if ((f = fopen(fn, "w+"))) { 00200 setlinebuf(f); 00201 fprintf(f, "CLIENTCHAN: %s\n", chan->name); 00202 fprintf(f, "CLIENTTEST ID: %s\n", testid); 00203 fprintf(f, "ANSWER: PASS\n"); 00204 res = 0; 00205 00206 if (!res) { 00207 /* Step 1: Wait for "1" */ 00208 ast_debug(1, "TestClient: 2. Wait DTMF 1\n"); 00209 res = ast_waitfordigit(chan, 3000); 00210 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS"); 00211 if (res == '1') 00212 res = 0; 00213 else 00214 res = -1; 00215 } 00216 if (!res) { 00217 res = ast_safe_sleep(chan, 1000); 00218 } 00219 if (!res) { 00220 /* Step 2: Send "2" */ 00221 ast_debug(1, "TestClient: 2. Send DTMF 2\n"); 00222 res = ast_dtmf_stream(chan, NULL, "2", 0, 0); 00223 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS"); 00224 if (res > 0) 00225 res = 0; 00226 } 00227 if (!res) { 00228 /* Step 3: Wait one second */ 00229 ast_debug(1, "TestClient: 3. Wait one second\n"); 00230 res = ast_safe_sleep(chan, 1000); 00231 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); 00232 if (res > 0) 00233 res = 0; 00234 } 00235 if (!res) { 00236 /* Step 4: Measure noise */ 00237 ast_debug(1, "TestClient: 4. Measure noise\n"); 00238 res = measurenoise(chan, 5000, "TestClient"); 00239 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); 00240 if (res > 0) 00241 res = 0; 00242 } 00243 if (!res) { 00244 /* Step 5: Wait for "4" */ 00245 ast_debug(1, "TestClient: 5. Wait DTMF 4\n"); 00246 res = ast_waitfordigit(chan, 3000); 00247 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS"); 00248 if (res == '4') 00249 res = 0; 00250 else 00251 res = -1; 00252 } 00253 if (!res) { 00254 /* Step 6: Transmit tone noise */ 00255 ast_debug(1, "TestClient: 6. Transmit tone\n"); 00256 res = sendnoise(chan, 6000); 00257 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS"); 00258 } 00259 if (!res || (res == '5')) { 00260 /* Step 7: Wait for "5" */ 00261 ast_debug(1, "TestClient: 7. Wait DTMF 5\n"); 00262 if (!res) 00263 res = ast_waitfordigit(chan, 3000); 00264 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS"); 00265 if (res == '5') 00266 res = 0; 00267 else 00268 res = -1; 00269 } 00270 if (!res) { 00271 /* Step 8: Wait one second */ 00272 ast_debug(1, "TestClient: 8. Wait one second\n"); 00273 res = ast_safe_sleep(chan, 1000); 00274 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); 00275 if (res > 0) 00276 res = 0; 00277 } 00278 if (!res) { 00279 /* Step 9: Measure noise */ 00280 ast_debug(1, "TestClient: 9. Measure tone\n"); 00281 res = measurenoise(chan, 4000, "TestClient"); 00282 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); 00283 if (res > 0) 00284 res = 0; 00285 } 00286 if (!res) { 00287 /* Step 10: Send "7" */ 00288 ast_debug(1, "TestClient: 10. Send DTMF 7\n"); 00289 res = ast_dtmf_stream(chan, NULL, "7", 0, 0); 00290 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS"); 00291 if (res > 0) 00292 res =0; 00293 } 00294 if (!res) { 00295 /* Step 11: Wait for "8" */ 00296 ast_debug(1, "TestClient: 11. Wait DTMF 8\n"); 00297 res = ast_waitfordigit(chan, 3000); 00298 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS"); 00299 if (res == '8') 00300 res = 0; 00301 else 00302 res = -1; 00303 } 00304 if (!res) { 00305 res = ast_safe_sleep(chan, 1000); 00306 } 00307 if (!res) { 00308 /* Step 12: Hangup! */ 00309 ast_debug(1, "TestClient: 12. Hangup\n"); 00310 } 00311 00312 ast_debug(1, "-- TEST COMPLETE--\n"); 00313 fprintf(f, "-- END TEST--\n"); 00314 fclose(f); 00315 res = -1; 00316 } else 00317 res = -1; 00318 } else { 00319 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name); 00320 res = -1; 00321 } 00322 return res; 00323 }
static int testserver_exec | ( | struct ast_channel * | chan, | |
const char * | data | |||
) | [static] |
Definition at line 325 of file app_test.c.
References ast_channel::_state, ast_answer(), ast_app_getdata(), 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(), f, LOG_NOTICE, measurenoise(), and sendnoise().
Referenced by load_module().
00326 { 00327 int res = 0; 00328 char testid[80]=""; 00329 char fn[80]; 00330 FILE *f; 00331 if (chan->_state != AST_STATE_UP) 00332 res = ast_answer(chan); 00333 /* Read version */ 00334 ast_debug(1, "Read client version\n"); 00335 if (!res) 00336 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0); 00337 if (res > 0) 00338 res = 0; 00339 00340 ast_debug(1, "client version: %s\n", testid); 00341 ast_debug(1, "Transmit server version\n"); 00342 00343 res = ast_safe_sleep(chan, 1000); 00344 if (!res) 00345 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0); 00346 if (res > 0) 00347 res = 0; 00348 00349 if (!res) 00350 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0); 00351 ast_debug(1, "read test identifier: %s\n", testid); 00352 /* Check for sneakyness */ 00353 if (strchr(testid, '/')) 00354 res = -1; 00355 if ((res >=0) && (!ast_strlen_zero(testid))) { 00356 /* Got a Test ID! Whoo hoo! */ 00357 /* Make the directory to hold the test results in case it's not there */ 00358 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); 00359 ast_mkdir(fn, 0777); 00360 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid); 00361 if ((f = fopen(fn, "w+"))) { 00362 setlinebuf(f); 00363 fprintf(f, "SERVERCHAN: %s\n", chan->name); 00364 fprintf(f, "SERVERTEST ID: %s\n", testid); 00365 fprintf(f, "ANSWER: PASS\n"); 00366 ast_debug(1, "Processing Test ID '%s'\n", testid); 00367 res = ast_safe_sleep(chan, 1000); 00368 if (!res) { 00369 /* Step 1: Send "1" */ 00370 ast_debug(1, "TestServer: 1. Send DTMF 1\n"); 00371 res = ast_dtmf_stream(chan, NULL, "1", 0,0 ); 00372 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS"); 00373 if (res > 0) 00374 res = 0; 00375 } 00376 if (!res) { 00377 /* Step 2: Wait for "2" */ 00378 ast_debug(1, "TestServer: 2. Wait DTMF 2\n"); 00379 res = ast_waitfordigit(chan, 3000); 00380 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS"); 00381 if (res == '2') 00382 res = 0; 00383 else 00384 res = -1; 00385 } 00386 if (!res) { 00387 /* Step 3: Measure noise */ 00388 ast_debug(1, "TestServer: 3. Measure noise\n"); 00389 res = measurenoise(chan, 6000, "TestServer"); 00390 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); 00391 if (res > 0) 00392 res = 0; 00393 } 00394 if (!res) { 00395 /* Step 4: Send "4" */ 00396 ast_debug(1, "TestServer: 4. Send DTMF 4\n"); 00397 res = ast_dtmf_stream(chan, NULL, "4", 0, 0); 00398 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS"); 00399 if (res > 0) 00400 res = 0; 00401 } 00402 if (!res) { 00403 /* Step 5: Wait one second */ 00404 ast_debug(1, "TestServer: 5. Wait one second\n"); 00405 res = ast_safe_sleep(chan, 1000); 00406 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); 00407 if (res > 0) 00408 res = 0; 00409 } 00410 if (!res) { 00411 /* Step 6: Measure noise */ 00412 ast_debug(1, "TestServer: 6. Measure tone\n"); 00413 res = measurenoise(chan, 4000, "TestServer"); 00414 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); 00415 if (res > 0) 00416 res = 0; 00417 } 00418 if (!res) { 00419 /* Step 7: Send "5" */ 00420 ast_debug(1, "TestServer: 7. Send DTMF 5\n"); 00421 res = ast_dtmf_stream(chan, NULL, "5", 0, 0); 00422 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS"); 00423 if (res > 0) 00424 res = 0; 00425 } 00426 if (!res) { 00427 /* Step 8: Transmit tone noise */ 00428 ast_debug(1, "TestServer: 8. Transmit tone\n"); 00429 res = sendnoise(chan, 6000); 00430 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS"); 00431 } 00432 00433 if (!res || (res == '7')) { 00434 /* Step 9: Wait for "7" */ 00435 ast_debug(1, "TestServer: 9. Wait DTMF 7\n"); 00436 if (!res) 00437 res = ast_waitfordigit(chan, 3000); 00438 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS"); 00439 if (res == '7') 00440 res = 0; 00441 else 00442 res = -1; 00443 } 00444 if (!res) { 00445 res = ast_safe_sleep(chan, 1000); 00446 } 00447 if (!res) { 00448 /* Step 10: Send "8" */ 00449 ast_debug(1, "TestServer: 10. Send DTMF 8\n"); 00450 res = ast_dtmf_stream(chan, NULL, "8", 0, 0); 00451 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS"); 00452 if (res > 0) 00453 res = 0; 00454 } 00455 if (!res) { 00456 /* Step 11: Wait for hangup to arrive! */ 00457 ast_debug(1, "TestServer: 11. Waiting for hangup\n"); 00458 res = ast_safe_sleep(chan, 10000); 00459 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL"); 00460 } 00461 00462 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n"); 00463 fprintf(f, "-- END TEST--\n"); 00464 fclose(f); 00465 res = -1; 00466 } else 00467 res = -1; 00468 } else { 00469 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name); 00470 res = -1; 00471 } 00472 return res; 00473 }
static int unload_module | ( | void | ) | [static] |
Definition at line 475 of file app_test.c.
References ast_unregister_application().
00476 { 00477 int res; 00478 00479 res = ast_unregister_application(testc_app); 00480 res |= ast_unregister_application(tests_app); 00481 00482 return res; 00483 }
char* testc_app = "TestClient" [static] |
Definition at line 82 of file app_test.c.
char* tests_app = "TestServer" [static] |
Definition at line 81 of file app_test.c.