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