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 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 33466 $")
00038
00039 #include "asterisk/channel.h"
00040 #include "asterisk/options.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/logger.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/utils.h"
00047
00048 STANDARD_LOCAL_USER;
00049
00050 LOCAL_USER_DECL;
00051
00052 static char *tdesc = "Interface Test Application";
00053
00054 static char *tests_descrip =
00055 "TestServer(): Perform test server function and write call report.\n"
00056 "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
00057 static char *tests_app = "TestServer";
00058 static char *tests_synopsis = "Execute Interface Test Server";
00059
00060 static char *testc_descrip =
00061 "TestClient(testid): Executes test client with given testid.\n"
00062 "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
00063
00064 static char *testc_app = "TestClient";
00065 static char *testc_synopsis = "Execute Interface Test Client";
00066
00067 static int measurenoise(struct ast_channel *chan, int ms, char *who)
00068 {
00069 int res=0;
00070 int mssofar;
00071 int noise=0;
00072 int samples=0;
00073 int x;
00074 short *foo;
00075 struct timeval start;
00076 struct ast_frame *f;
00077 int rformat;
00078 rformat = chan->readformat;
00079 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
00080 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
00081 return -1;
00082 }
00083 start = ast_tvnow();
00084 for(;;) {
00085 mssofar = ast_tvdiff_ms(ast_tvnow(), start);
00086 if (mssofar > ms)
00087 break;
00088 res = ast_waitfor(chan, ms - mssofar);
00089 if (res < 1)
00090 break;
00091 f = ast_read(chan);
00092 if (!f) {
00093 res = -1;
00094 break;
00095 }
00096 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
00097 foo = (short *)f->data;
00098 for (x=0;x<f->samples;x++) {
00099 noise += abs(foo[x]);
00100 samples++;
00101 }
00102 }
00103 ast_frfree(f);
00104 }
00105
00106 if (rformat) {
00107 if (ast_set_read_format(chan, rformat)) {
00108 ast_log(LOG_NOTICE, "Unable to restore original format!\n");
00109 return -1;
00110 }
00111 }
00112 if (res < 0)
00113 return res;
00114 if (!samples) {
00115 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
00116 return -1;
00117 }
00118 ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
00119 return (noise / samples);
00120 }
00121
00122 static int sendnoise(struct ast_channel *chan, int ms)
00123 {
00124 int res;
00125 res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
00126 if (!res) {
00127 res = ast_waitfordigit(chan, ms);
00128 ast_tonepair_stop(chan);
00129 }
00130 return res;
00131 }
00132
00133 static int testclient_exec(struct ast_channel *chan, void *data)
00134 {
00135 struct localuser *u;
00136 int res = 0;
00137 char *testid=data;
00138 char fn[80];
00139 char serverver[80];
00140 FILE *f;
00141
00142
00143 if (ast_strlen_zero(testid)) {
00144 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
00145 return -1;
00146 }
00147
00148 LOCAL_USER_ADD(u);
00149
00150 if (chan->_state != AST_STATE_UP)
00151 res = ast_answer(chan);
00152
00153
00154 res = ast_safe_sleep(chan, 3000);
00155
00156 if (!res)
00157 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
00158 if (option_debug)
00159 ast_log(LOG_DEBUG, "Transmit client version\n");
00160
00161
00162 if (option_debug)
00163 ast_log(LOG_DEBUG, "Read server version\n");
00164 if (!res)
00165 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
00166 if (res > 0)
00167 res = 0;
00168 if (option_debug)
00169 ast_log(LOG_DEBUG, "server version: %s\n", serverver);
00170
00171 if (res > 0)
00172 res = 0;
00173
00174 if (!res)
00175 res = ast_safe_sleep(chan, 1000);
00176
00177 if (!res)
00178 res = ast_dtmf_stream(chan, NULL, testid, 0);
00179 if (!res)
00180 res = ast_dtmf_stream(chan, NULL, "#", 0);
00181 if (option_debug)
00182 ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
00183
00184 if ((res >=0) && (!ast_strlen_zero(testid))) {
00185
00186 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00187 mkdir(fn, 0777);
00188 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
00189 if ((f = fopen(fn, "w+"))) {
00190 setlinebuf(f);
00191 fprintf(f, "CLIENTCHAN: %s\n", chan->name);
00192 fprintf(f, "CLIENTTEST ID: %s\n", testid);
00193 fprintf(f, "ANSWER: PASS\n");
00194 res = 0;
00195
00196 if (!res) {
00197
00198 if (option_debug)
00199 ast_log(LOG_DEBUG, "TestClient: 2. Wait DTMF 1\n");
00200 res = ast_waitfordigit(chan, 3000);
00201 fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
00202 if (res == '1')
00203 res = 0;
00204 else
00205 res = -1;
00206 }
00207 if (!res)
00208 res = ast_safe_sleep(chan, 1000);
00209 if (!res) {
00210
00211 if (option_debug)
00212 ast_log(LOG_DEBUG, "TestClient: 2. Send DTMF 2\n");
00213 res = ast_dtmf_stream(chan, NULL, "2", 0);
00214 fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
00215 if (res > 0)
00216 res = 0;
00217 }
00218 if (!res) {
00219
00220 if (option_debug)
00221 ast_log(LOG_DEBUG, "TestClient: 3. Wait one second\n");
00222 res = ast_safe_sleep(chan, 1000);
00223 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
00224 if (res > 0)
00225 res = 0;
00226 }
00227 if (!res) {
00228
00229 if (option_debug)
00230 ast_log(LOG_DEBUG, "TestClient: 4. Measure noise\n");
00231 res = measurenoise(chan, 5000, "TestClient");
00232 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00233 if (res > 0)
00234 res = 0;
00235 }
00236 if (!res) {
00237
00238 if (option_debug)
00239 ast_log(LOG_DEBUG, "TestClient: 5. Wait DTMF 4\n");
00240 res = ast_waitfordigit(chan, 3000);
00241 fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
00242 if (res == '4')
00243 res = 0;
00244 else
00245 res = -1;
00246 }
00247 if (!res) {
00248
00249 if (option_debug)
00250 ast_log(LOG_DEBUG, "TestClient: 6. Transmit tone\n");
00251 res = sendnoise(chan, 6000);
00252 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
00253 }
00254 if (!res || (res == '5')) {
00255
00256 if (option_debug)
00257 ast_log(LOG_DEBUG, "TestClient: 7. Wait DTMF 5\n");
00258 if (!res)
00259 res = ast_waitfordigit(chan, 3000);
00260 fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
00261 if (res == '5')
00262 res = 0;
00263 else
00264 res = -1;
00265 }
00266 if (!res) {
00267
00268 if (option_debug)
00269 ast_log(LOG_DEBUG, "TestClient: 8. Wait one second\n");
00270 res = ast_safe_sleep(chan, 1000);
00271 fprintf(f, "WAIT 1 SEC: %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, "TestClient: 6. Measure tone\n");
00279 res = measurenoise(chan, 4000, "TestClient");
00280 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00281 if (res > 0)
00282 res = 0;
00283 }
00284 if (!res) {
00285
00286 if (option_debug)
00287 ast_log(LOG_DEBUG, "TestClient: 7. Send DTMF 7\n");
00288 res = ast_dtmf_stream(chan, NULL, "7", 0);
00289 fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
00290 if (res > 0)
00291 res =0;
00292 }
00293 if (!res) {
00294
00295 if (option_debug)
00296 ast_log(LOG_DEBUG, "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 (option_debug && !res ) {
00305
00306 ast_log(LOG_DEBUG, "TestClient: 12. Hangup\n");
00307 }
00308
00309 if (option_debug)
00310 ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
00311 fprintf(f, "-- END TEST--\n");
00312 fclose(f);
00313 res = -1;
00314 } else
00315 res = -1;
00316 } else {
00317 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00318 res = -1;
00319 }
00320 LOCAL_USER_REMOVE(u);
00321 return res;
00322 }
00323
00324 static int testserver_exec(struct ast_channel *chan, void *data)
00325 {
00326 struct localuser *u;
00327 int res = 0;
00328 char testid[80]="";
00329 char fn[80];
00330 FILE *f;
00331 LOCAL_USER_ADD(u);
00332 if (chan->_state != AST_STATE_UP)
00333 res = ast_answer(chan);
00334
00335 if (option_debug)
00336 ast_log(LOG_DEBUG, "Read client version\n");
00337 if (!res)
00338 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00339 if (res > 0)
00340 res = 0;
00341 if (option_debug) {
00342 ast_log(LOG_DEBUG, "client version: %s\n", testid);
00343 ast_log(LOG_DEBUG, "Transmit server version\n");
00344 }
00345 res = ast_safe_sleep(chan, 1000);
00346 if (!res)
00347 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
00348 if (res > 0)
00349 res = 0;
00350
00351 if (!res)
00352 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00353 if (option_debug)
00354 ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
00355
00356 if (strchr(testid, '/'))
00357 res = -1;
00358 if ((res >=0) && (!ast_strlen_zero(testid))) {
00359
00360
00361 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00362 mkdir(fn, 0777);
00363 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
00364 if ((f = fopen(fn, "w+"))) {
00365 setlinebuf(f);
00366 fprintf(f, "SERVERCHAN: %s\n", chan->name);
00367 fprintf(f, "SERVERTEST ID: %s\n", testid);
00368 fprintf(f, "ANSWER: PASS\n");
00369 ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
00370 res = ast_safe_sleep(chan, 1000);
00371 if (!res) {
00372
00373 if (option_debug)
00374 ast_log(LOG_DEBUG, "TestServer: 1. Send DTMF 1\n");
00375 res = ast_dtmf_stream(chan, NULL, "1", 0);
00376 fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
00377 if (res > 0)
00378 res = 0;
00379 }
00380 if (!res) {
00381
00382 if (option_debug)
00383 ast_log(LOG_DEBUG, "TestServer: 2. Wait DTMF 2\n");
00384 res = ast_waitfordigit(chan, 3000);
00385 fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
00386 if (res == '2')
00387 res = 0;
00388 else
00389 res = -1;
00390 }
00391 if (!res) {
00392
00393 if (option_debug)
00394 ast_log(LOG_DEBUG, "TestServer: 3. Measure noise\n");
00395 res = measurenoise(chan, 6000, "TestServer");
00396 fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00397 if (res > 0)
00398 res = 0;
00399 }
00400 if (!res) {
00401
00402 if (option_debug)
00403 ast_log(LOG_DEBUG, "TestServer: 4. Send DTMF 4\n");
00404 res = ast_dtmf_stream(chan, NULL, "4", 0);
00405 fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
00406 if (res > 0)
00407 res = 0;
00408 }
00409
00410 if (!res) {
00411
00412 if (option_debug)
00413 ast_log(LOG_DEBUG, "TestServer: 5. Wait one second\n");
00414 res = ast_safe_sleep(chan, 1000);
00415 fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
00416 if (res > 0)
00417 res = 0;
00418 }
00419
00420 if (!res) {
00421
00422 if (option_debug)
00423 ast_log(LOG_DEBUG, "TestServer: 6. Measure tone\n");
00424 res = measurenoise(chan, 4000, "TestServer");
00425 fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00426 if (res > 0)
00427 res = 0;
00428 }
00429
00430 if (!res) {
00431
00432 if (option_debug)
00433 ast_log(LOG_DEBUG, "TestServer: 7. Send DTMF 5\n");
00434 res = ast_dtmf_stream(chan, NULL, "5", 0);
00435 fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
00436 if (res > 0)
00437 res = 0;
00438 }
00439
00440 if (!res) {
00441
00442 if (option_debug)
00443 ast_log(LOG_DEBUG, "TestServer: 8. Transmit tone\n");
00444 res = sendnoise(chan, 6000);
00445 fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
00446 }
00447
00448 if (!res || (res == '7')) {
00449
00450 if (option_debug)
00451 ast_log(LOG_DEBUG, "TestServer: 9. Wait DTMF 7\n");
00452 if (!res)
00453 res = ast_waitfordigit(chan, 3000);
00454 fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
00455 if (res == '7')
00456 res = 0;
00457 else
00458 res = -1;
00459 }
00460 if (!res)
00461 res = ast_safe_sleep(chan, 1000);
00462 if (!res) {
00463
00464 if (option_debug)
00465 ast_log(LOG_DEBUG, "TestServer: 10. Send DTMF 8\n");
00466 res = ast_dtmf_stream(chan, NULL, "8", 0);
00467 fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
00468 if (res > 0)
00469 res = 0;
00470 }
00471 if (!res) {
00472
00473 if (option_debug)
00474 ast_log(LOG_DEBUG, "TestServer: 11. Waiting for hangup\n");
00475 res = ast_safe_sleep(chan, 10000);
00476 fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
00477 }
00478
00479 ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
00480 fprintf(f, "-- END TEST--\n");
00481 fclose(f);
00482 res = -1;
00483 } else
00484 res = -1;
00485 } else {
00486 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00487 res = -1;
00488 }
00489 LOCAL_USER_REMOVE(u);
00490 return res;
00491 }
00492
00493 int unload_module(void)
00494 {
00495 int res;
00496
00497 res = ast_unregister_application(testc_app);
00498 res |= ast_unregister_application(tests_app);
00499
00500 STANDARD_HANGUP_LOCALUSERS;
00501
00502 return res;
00503 }
00504
00505 int load_module(void)
00506 {
00507 int res;
00508
00509 res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
00510 res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
00511
00512 return res;
00513 }
00514
00515 char *description(void)
00516 {
00517 return tdesc;
00518 }
00519
00520 int usecount(void)
00521 {
00522 int res;
00523 STANDARD_USECOUNT(res);
00524 return res;
00525 }
00526
00527 char *key(void)
00528 {
00529 return ASTERISK_GPL_KEY;
00530 }