00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00033
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <unistd.h>
00038 #include <fcntl.h>
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041
00042 #include "asterisk/channel.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/lock.h"
00047 #include "asterisk/app.h"
00048 #include "asterisk/pbx.h"
00049 #include "asterisk/utils.h"
00050
00051 #define TEST_VERSION_NUMBER "8378*"
00052
00053 #define HIGHEST_TEST '2'
00054
00055 static char *tests_descrip =
00056 "TestServer(): Perform test server function and write call report.\n"
00057 "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
00058 static char *tests_app = "TestServer";
00059 static char *tests_synopsis = "Execute Interface Test Server";
00060
00061 static char *testc_descrip =
00062 "TestClient(testid[|testnumber]): Executes test client with given testid.\n"
00063 "Different test can be selected by passing the testnumber, as follows:\n"
00064 " 1: set of tests to try confirm that the connection is generally working\n"
00065
00066 "\n"
00067 "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
00068
00069 static char *testc_app = "TestClient";
00070 static char *testc_synopsis = "Execute Interface Test Client";
00071
00072 static int measurenoise(struct ast_channel *chan, int ms, char *who)
00073 {
00074 int res=0;
00075 int mssofar;
00076 int noise=0;
00077 int samples=0;
00078 int x;
00079 short *foo;
00080 struct timeval start;
00081 struct ast_frame *f;
00082 int rformat;
00083 rformat = chan->readformat;
00084 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
00085 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
00086 return -1;
00087 }
00088 start = ast_tvnow();
00089 for(;;) {
00090 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
00091 if (mssofar > ms)
00092 break;
00093 res = ast_waitfor(chan, ms - mssofar);
00094 if (res < 1)
00095 break;
00096 f = ast_read(chan);
00097 if (!f) {
00098 res = -1;
00099 break;
00100 }
00101 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
00102 foo = (short *)f->data;
00103 for (x=0;x<f->samples;x++) {
00104 noise += abs(foo[x]);
00105 samples++;
00106 }
00107 }
00108 ast_frfree(f);
00109 }
00110
00111 if (rformat) {
00112 if (ast_set_read_format(chan, rformat)) {
00113 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
00114 return -1;
00115 }
00116 }
00117 if (res < 0)
00118 return res;
00119 if (!samples) {
00120 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
00121 return -1;
00122 }
00123 ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
00124 return (noise / samples);
00125 }
00126
00127 static int sendnoise(struct ast_channel *chan, int ms)
00128 {
00129 int res;
00130 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
00131 if (!res) {
00132 res = ast_waitfordigit(chan, ms);
00133 ast_tonepair_stop(chan);
00134 }
00135 return res;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 static int test1_client(int res, struct ast_channel *chan, FILE *f)
00147 {
00148 if (!res) {
00149
00150 if (option_debug)
00151 ast_log(LOG_DEBUG, "TestClient: 2. Wait DTMF 1\n");
00152 res = ast_waitfordigit(chan, 3000);
00153 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
00154 if (res == '1')
00155 res = 0;
00156 else
00157 res = -1;
00158 }
00159 if (!res)
00160 res = ast_safe_sleep(chan, 1000);
00161
00162 if (!res) {
00163
00164 if (option_debug)
00165 ast_log(LOG_DEBUG, "TestClient: 2. Send DTMF 2\n");
00166 res = ast_dtmf_stream(chan, NULL, "2", 0);
00167 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
00168 if (res > 0)
00169 res = 0;
00170 }
00171 if (!res) {
00172
00173 if (option_debug)
00174 ast_log(LOG_DEBUG, "TestClient: 3. Wait one second\n");
00175 res = ast_safe_sleep(chan, 1000);
00176 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
00177 if (res > 0)
00178 res = 0;
00179 }
00180 if (!res) {
00181
00182 if (option_debug)
00183 ast_log(LOG_DEBUG, "TestClient: 4. Measure noise\n");
00184 res = measurenoise(chan, 5000, "TestClient");
00185 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00186 if (res > 0)
00187 res = 0;
00188 }
00189 if (!res) {
00190
00191 if (option_debug)
00192 ast_log(LOG_DEBUG, "TestClient: 5. Wait DTMF 4\n");
00193 res = ast_waitfordigit(chan, 3000);
00194 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
00195 if (res == '4')
00196 res = 0;
00197 else
00198 res = -1;
00199 }
00200 if (!res) {
00201
00202 if (option_debug)
00203 ast_log(LOG_DEBUG, "TestClient: 6. Transmit tone\n");
00204 res = sendnoise(chan, 6000);
00205 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
00206 }
00207 if (!res || (res == '5')) {
00208
00209 if (option_debug)
00210 ast_log(LOG_DEBUG, "TestClient: 7. Wait DTMF 5\n");
00211 if (!res)
00212 res = ast_waitfordigit(chan, 3000);
00213 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
00214 if (res == '5')
00215 res = 0;
00216 else
00217 res = -1;
00218 }
00219 if (!res) {
00220
00221 if (option_debug)
00222 ast_log(LOG_DEBUG, "TestClient: 8. Wait one second\n");
00223 res = ast_safe_sleep(chan, 1000);
00224 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
00225 if (res > 0)
00226 res = 0;
00227 }
00228 if (!res) {
00229
00230 if (option_debug)
00231 ast_log(LOG_DEBUG, "TestClient: 6. Measure tone\n");
00232 res = measurenoise(chan, 4000, "TestClient");
00233 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00234 if (res > 0)
00235 res = 0;
00236 }
00237 if (!res) {
00238
00239 if (option_debug)
00240 ast_log(LOG_DEBUG, "TestClient: 7. Send DTMF 7\n");
00241 res = ast_dtmf_stream(chan, NULL, "7", 0);
00242 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
00243 if (res > 0)
00244 res =0;
00245 }
00246 if (!res) {
00247
00248 if (option_debug)
00249 ast_log(LOG_DEBUG, "TestClient: 11. Wait DTMF 8\n");
00250 res = ast_waitfordigit(chan, 3000);
00251 fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
00252 if (res == '8')
00253 res = 0;
00254 else
00255 res = -1;
00256 }
00257 if (option_debug && !res ) {
00258
00259 ast_log(LOG_DEBUG, "TestClient: 12. Hangup\n");
00260 }
00261 return res;
00262 }
00263
00264 static int test1_server(int res, struct ast_channel *chan, FILE *f)
00265 {
00266 if (!res) {
00267
00268 if (option_debug)
00269 ast_log(LOG_DEBUG, "TestServer: 1. Send DTMF 1\n");
00270 res = ast_dtmf_stream(chan, NULL, "1", 0);
00271 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
00272 if (res > 0)
00273 res = 0;
00274 }
00275 if (!res) {
00276
00277 if (option_debug)
00278 ast_log(LOG_DEBUG, "TestServer: 2. Wait DTMF 2\n");
00279 res = ast_waitfordigit(chan, 3000);
00280 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
00281 if (res == '2')
00282 res = 0;
00283 else
00284 res = -1;
00285 }
00286 if (!res) {
00287
00288 if (option_debug)
00289 ast_log(LOG_DEBUG, "TestServer: 3. Measure noise\n");
00290
00291 res = ast_safe_sleep(chan, 1000);
00292 if (!res)
00293 res = measurenoise(chan, 5000, "TestServer");
00294 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00295 if (res > 0)
00296 res = 0;
00297 }
00298 if (!res) {
00299
00300 if (option_debug)
00301 ast_log(LOG_DEBUG, "TestServer: 4. Send DTMF 4\n");
00302
00303 res = ast_safe_sleep(chan, 1000);
00304
00305 if (!res)
00306 res = ast_dtmf_stream(chan, NULL, "4", 0);
00307 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
00308 if (res > 0)
00309 res = 0;
00310 }
00311
00312 if (!res) {
00313
00314 if (option_debug)
00315 ast_log(LOG_DEBUG, "TestServer: 5. Wait one second\n");
00316 res = ast_safe_sleep(chan, 1000);
00317 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
00318 if (res > 0)
00319 res = 0;
00320 }
00321
00322 if (!res) {
00323
00324 if (option_debug)
00325 ast_log(LOG_DEBUG, "TestServer: 6. Measure tone\n");
00326 res = measurenoise(chan, 4000, "TestServer");
00327 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00328 if (res > 0)
00329 res = 0;
00330 }
00331
00332 if (!res) {
00333
00334 if (option_debug)
00335 ast_log(LOG_DEBUG, "TestServer: 7. Send DTMF 5\n");
00336
00337 res = ast_safe_sleep(chan, 1000);
00338
00339 if (!res)
00340 res = ast_dtmf_stream(chan, NULL, "5", 0);
00341 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
00342 if (res > 0)
00343 res = 0;
00344 }
00345
00346 if (!res) {
00347
00348 if (option_debug)
00349 ast_log(LOG_DEBUG, "TestServer: 8. Transmit tone\n");
00350 res = sendnoise(chan, 6000);
00351 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
00352 }
00353
00354 if (!res || (res == '7')) {
00355
00356 if (option_debug)
00357 ast_log(LOG_DEBUG, "TestServer: 9. Wait DTMF 7\n");
00358 if (!res)
00359 res = ast_waitfordigit(chan, 3000);
00360 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
00361 if (res == '7')
00362 res = 0;
00363 else
00364 res = -1;
00365 }
00366 if (!res)
00367 res = ast_safe_sleep(chan, 1000);
00368 if (!res) {
00369
00370 if (option_debug)
00371 ast_log(LOG_DEBUG, "TestServer: 10. Send DTMF 8\n");
00372 res = ast_dtmf_stream(chan, NULL, "8", 0);
00373 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
00374 if (res > 0)
00375 res = 0;
00376 }
00377 if (!res) {
00378
00379 if (option_debug)
00380 ast_log(LOG_DEBUG, "TestServer: 11. Waiting for hangup\n");
00381 res = ast_safe_sleep(chan, 10000);
00382 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
00383 }
00384 return res;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 static int test2_client(int res, struct ast_channel *chan, FILE *f)
00398 {
00399 return -1;
00400 }
00401
00402 static int test2_server(int res, struct ast_channel *chan, FILE *f)
00403 {
00404 return -1;
00405 }
00406
00407
00408 static int testclient_exec(struct ast_channel *chan, void *data)
00409 {
00410 struct ast_module_user *u;
00411 int res = 0;
00412 char testid[256], *testnumber, *stringp;
00413 char fn[80];
00414 char serverver[80];
00415 FILE *f;
00416
00417
00418 strncpy(testid, (char *)data, sizeof(testid)-1);
00419 stringp = testid;
00420 strsep(&stringp, "|");
00421 testnumber = strsep(&stringp, "|");
00422
00423
00424 if (ast_strlen_zero(testnumber))
00425 testnumber = "1";
00426
00427
00428 if (ast_strlen_zero(testid)) {
00429 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
00430 return -1;
00431 }
00432
00433 if (strlen(testnumber) != 1 || (*testnumber < '1') || (*testnumber > HIGHEST_TEST)) {
00434 ast_log(LOG_WARNING, "TestClient only knows test numbers from 1 to 2\n");
00435 return -1;
00436 }
00437
00438 u = ast_module_user_add(chan);
00439
00440 if (chan->_state != AST_STATE_UP)
00441 res = ast_answer(chan);
00442
00443
00444 res = ast_safe_sleep(chan, 3000);
00445
00446
00447 if (!res) {
00448 res = ast_dtmf_stream(chan, NULL, TEST_VERSION_NUMBER, 0);
00449 res = ast_dtmf_stream(chan, NULL, testnumber, 0);
00450 res = ast_dtmf_stream(chan, NULL, "#", 0);
00451 }
00452 if (option_debug)
00453 ast_log(LOG_DEBUG, "Transmit client version and test number\n");
00454
00455
00456 if (option_debug)
00457 ast_log(LOG_DEBUG, "Read server version\n");
00458 if (!res)
00459 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
00460 if (res > 0)
00461 res = 0;
00462 if (option_debug)
00463 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
00464
00465
00466 if (!res) {
00467 stringp = strchr(serverver, '*');
00468 if (stringp) {
00469 if (*++stringp != *testnumber) {
00470 ast_log(LOG_WARNING, "TestClient: server doesn't know test \'%s\'\n", testnumber);
00471 ast_module_user_remove(u);
00472 return -1;
00473 }
00474 }
00475 else {
00476 ast_log(LOG_WARNING, "TestClient couldn't find server test number in \'%s\'\n", serverver);
00477 ast_module_user_remove(u);
00478 return -1;
00479 }
00480 }
00481 else {
00482 ast_log(LOG_WARNING, "TestClient failed to read server version\n");
00483 ast_module_user_remove(u);
00484 return -1;
00485 }
00486
00487 if (res > 0)
00488 res = 0;
00489
00490 if (!res)
00491 res = ast_safe_sleep(chan, 1000);
00492
00493
00494 if (!res)
00495 res = ast_dtmf_stream(chan, NULL, testid, 0);
00496 if (!res)
00497 res = ast_dtmf_stream(chan, NULL, "#", 0);
00498 if (option_debug)
00499 ast_log(LOG_DEBUG, "sent test identifier: %s\n", testid);
00500
00501 if ((res >=0) && (!ast_strlen_zero(testid))) {
00502
00503 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00504 mkdir(fn, 0777);
00505 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
00506 if ((f = fopen(fn, "w+"))) {
00507 setlinebuf(f);
00508 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
00509 fprintf(f, "CLIENTTEST ID: %s\n", testid);
00510 fprintf(f, "CLIENTTESTNUM: %s\n", testnumber);
00511 fprintf(f, "ANSWER: PASS\n");
00512
00513 res = ast_safe_sleep(chan, 1000); res = 0;
00514
00515 switch (*testnumber) {
00516 case '1':
00517 res = test1_client(res, chan, f);
00518 break;
00519 case '2':
00520 res = test2_client(res, chan, f);
00521 break;
00522 }
00523
00524 if (option_debug)
00525 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
00526
00527 fprintf(f, "-- END TEST--\n");
00528 fclose(f);
00529 res = -1;
00530 } else
00531 res = -1;
00532 } else {
00533 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00534 res = -1;
00535 }
00536 ast_module_user_remove(u);
00537 return res;
00538 }
00539
00540 static int testserver_exec(struct ast_channel *chan, void *data)
00541 {
00542 struct ast_module_user *u;
00543 int res = 0;
00544 char testid[80]="";
00545 char clientversion[80]="";
00546 char *testnumber;
00547 char fn[80];
00548 FILE *f;
00549 u = ast_module_user_add(chan);
00550
00551 if (chan->_state != AST_STATE_UP)
00552 res = ast_answer(chan);
00553
00554
00555 if (option_debug)
00556 ast_log(LOG_DEBUG, "Read client version\n");
00557 if (!res)
00558 res = ast_app_getdata(chan, NULL, clientversion, sizeof(clientversion) - 1, 0);
00559 if (res > 0)
00560 res = 0;
00561 if (option_debug)
00562 ast_log(LOG_DEBUG, "client version: %s\n", clientversion);
00563
00564
00565 testnumber = strchr(clientversion, '*');
00566 if (testnumber) {
00567 ++testnumber;
00568 if (strlen(testnumber) != 1 || (*testnumber < '1') || (*testnumber > HIGHEST_TEST)) {
00569 ast_log(LOG_WARNING, "TestServer: we don\'t know requested test \'%s\'\n", testnumber);
00570 testnumber = "*";
00571 }
00572 else {
00573 ast_log(LOG_WARNING, "TestServer: client requested test \'%s\'\n", testnumber);
00574 }
00575 }
00576 else {
00577 ast_log(LOG_WARNING, "TestServer couldn\'t find desired test number in \'%s\'\n", clientversion);
00578 testnumber = "*";
00579 }
00580
00581
00582 ast_log(LOG_DEBUG, "Transmit server version\n");
00583 res = ast_safe_sleep(chan, 1000);
00584 if (!res) {
00585 ast_log(LOG_DEBUG, "testserver replying with %s%s#\n", TEST_VERSION_NUMBER, testnumber);
00586 res = ast_dtmf_stream(chan, NULL, TEST_VERSION_NUMBER, 0);
00587 res = ast_dtmf_stream(chan, NULL, testnumber, 0);
00588 res = ast_dtmf_stream(chan, NULL, "#", 0);
00589 }
00590 if (res > 0)
00591 res = 0;
00592
00593
00594 if (*testnumber == '*') {
00595 ast_log(LOG_DEBUG, "TestServer: stopping cos I don't know the test they want\n");
00596
00597 res = ast_safe_sleep(chan, 1000);
00598 ast_module_user_remove(u);
00599 return -1;
00600 }
00601
00602 if (!res)
00603 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00604 if (option_debug)
00605 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
00606
00607 if (strchr(testid, '/'))
00608 res = -1;
00609 if ((res >=0) && (!ast_strlen_zero(testid))) {
00610
00611
00612 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00613 mkdir(fn, 0777);
00614 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
00615 if ((f = fopen(fn, "w+"))) {
00616 setlinebuf(f);
00617 fprintf(f, "SERVERCHAN: %s\n", chan->name);
00618 fprintf(f, "SERVERTEST ID: %s\n", testid);
00619 fprintf(f, "SERVERTESTNUM: %s\n", testnumber);
00620 fprintf(f, "ANSWER: PASS\n");
00621 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
00622 res = ast_safe_sleep(chan, 1000); res = 0;
00623
00624 switch (*testnumber) {
00625 case '1':
00626 res = test1_server(res, chan, f);
00627 break;
00628 case '2':
00629 res = test2_server(res, chan, f);
00630 break;
00631 }
00632 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
00633 fprintf(f, "-- END TEST--\n");
00634 fclose(f);
00635 res = -1;
00636 } else
00637 res = -1;
00638 } else {
00639 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00640 res = -1;
00641 }
00642 ast_module_user_remove(u);
00643 return res;
00644 }
00645
00646 static int unload_module(void)
00647 {
00648 int res;
00649
00650 res = ast_unregister_application(testc_app);
00651 res |= ast_unregister_application(tests_app);
00652
00653 ast_module_user_hangup_all();
00654
00655 return res;
00656 }
00657
00658 static int load_module(void)
00659 {
00660 int res;
00661
00662 res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
00663 res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
00664
00665 return res;
00666 }
00667
00668 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");