| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <sys/stat.h> |
| 37 |
|
| 38 |
#include "asterisk/paths.h" |
| 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" |
| 45 |
#include "asterisk/format_cache.h" |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static char *tests_app = "TestServer"; |
| 87 |
static char *testc_app = "TestClient"; |
| 88 |
|
| 89 |
static 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)); |
| 102 |
if (ast_set_read_format(chan, ast_format_slin)) { |
| 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) && |
| 121 |
(ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) { |
| 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 |
|
| 149 |
static 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 |
|
| 160 |
static 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 |
|
| 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 |
|
| 178 |
res = ast_safe_sleep(chan, 3000); |
| 179 |
|
| 180 |
if (!res) |
| 181 |
res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0); |
| 182 |
ast_debug(1, "Transmit client version\n"); |
| 183 |
|
| 184 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 335 |
static 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 |
|
| 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 |
|
| 362 |
if (strchr(testid, '/')) |
| 363 |
res = -1; |
| 364 |
if ((res >=0) && (!ast_strlen_zero(testid))) { |
| 365 |
char fn[PATH_MAX]; |
| 366 |
|
| 367 |
|
| 368 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 486 |
static int unload_module(void) |
| 487 |
{ |
| 488 |
int res; |
| 489 |
|
| 490 |
res = ast_unregister_application(testc_app); |
| 491 |
res |= ast_unregister_application(tests_app); |
| 492 |
|
| 493 |
return res; |
| 494 |
} |
| 495 |
|
| 496 |
static int load_module(void) |
| 497 |
{ |
| 498 |
int res; |
| 499 |
|
| 500 |
res = ast_register_application_xml(testc_app, testclient_exec); |
| 501 |
res |= ast_register_application_xml(tests_app, testserver_exec); |
| 502 |
|
| 503 |
return res; |
| 504 |
} |
| 505 |
|
| 506 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Interface Test Application"); |