Thu Jul 9 13:40:20 2009

Asterisk developer's documentation


app_test.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Russell Bryant <russelb@clemson.edu>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Applications to test connection and produce report in text file
00023  *
00024  * \author Mark Spencer <markster@digium.com>
00025  * \author Russell Bryant <russelb@clemson.edu>
00026  * 
00027  * \ingroup applications
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 184391 $")
00033 
00034 #include <sys/stat.h>
00035 
00036 #include "asterisk/paths.h"   /* use ast_config_AST_LOG_DIR */
00037 #include "asterisk/channel.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/lock.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/utils.h"
00043 
00044 static char *tests_descrip = 
00045     "  TestServer(): Perform test server function and write call report.\n"
00046     "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
00047 static char *tests_app = "TestServer";
00048 static char *tests_synopsis = "Execute Interface Test Server";
00049 
00050 static char *testc_descrip = 
00051     "  TestClient(testid): Executes test client with given testid.\n"
00052     "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
00053 
00054 static char *testc_app = "TestClient";
00055 static char *testc_synopsis = "Execute Interface Test Client";
00056 
00057 static int measurenoise(struct ast_channel *chan, int ms, char *who)
00058 {
00059    int res=0;
00060    int mssofar;
00061    int noise=0;
00062    int samples=0;
00063    int x;
00064    short *foo;
00065    struct timeval start;
00066    struct ast_frame *f;
00067    int rformat;
00068    rformat = chan->readformat;
00069    if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
00070       ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
00071       return -1;
00072    }
00073    start = ast_tvnow();
00074    for(;;) {
00075       mssofar = ast_tvdiff_ms(ast_tvnow(), start);
00076       if (mssofar > ms)
00077          break;
00078       res = ast_waitfor(chan, ms - mssofar);
00079       if (res < 1)
00080          break;
00081       f = ast_read(chan);
00082       if (!f) {
00083          res = -1;
00084          break;
00085       }
00086       if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
00087          foo = (short *)f->data;
00088          for (x=0;x<f->samples;x++) {
00089             noise += abs(foo[x]);
00090             samples++;
00091          }
00092       }
00093       ast_frfree(f);
00094    }
00095 
00096    if (rformat) {
00097       if (ast_set_read_format(chan, rformat)) {
00098          ast_log(LOG_NOTICE, "Unable to restore original format!\n");
00099          return -1;
00100       }
00101    }
00102    if (res < 0)
00103       return res;
00104    if (!samples) {
00105       ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
00106       return -1;
00107    }
00108    ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
00109    return (noise / samples);
00110 }
00111 
00112 static int sendnoise(struct ast_channel *chan, int ms)
00113 {
00114    int res;
00115    res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
00116    if (!res) {
00117       res = ast_waitfordigit(chan, ms);
00118       ast_tonepair_stop(chan);
00119    }
00120    return res;
00121 }
00122 
00123 static int testclient_exec(struct ast_channel *chan, void *data)
00124 {
00125    int res = 0;
00126    char *testid=data;
00127    char fn[80];
00128    char serverver[80];
00129    FILE *f;
00130 
00131    /* Check for test id */
00132    if (ast_strlen_zero(testid)) {
00133       ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
00134       return -1;
00135    }
00136 
00137    if (chan->_state != AST_STATE_UP)
00138       res = ast_answer(chan);
00139 
00140    /* Wait a few just to be sure things get started */
00141    res = ast_safe_sleep(chan, 3000);
00142    /* Transmit client version */
00143    if (!res)
00144       res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
00145    ast_debug(1, "Transmit client version\n");
00146 
00147    /* Read server version */
00148    ast_debug(1, "Read server version\n");
00149    if (!res)
00150       res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
00151    if (res > 0)
00152       res = 0;
00153    ast_debug(1, "server version: %s\n", serverver);
00154 
00155    if (res > 0)
00156       res = 0;
00157 
00158    if (!res)
00159       res = ast_safe_sleep(chan, 1000);
00160    /* Send test id */
00161    if (!res)
00162       res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
00163    if (!res)
00164       res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
00165    ast_debug(1, "send test identifier: %s\n", testid);
00166 
00167    if ((res >=0) && (!ast_strlen_zero(testid))) {
00168       /* Make the directory to hold the test results in case it's not there */
00169       snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00170       ast_mkdir(fn, 0777);
00171       snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
00172       if ((f = fopen(fn, "w+"))) {
00173          setlinebuf(f);
00174          fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
00175          fprintf(f, "CLIENTTEST ID: %s\n", testid);
00176          fprintf(f, "ANSWER:        PASS\n");
00177          res = 0;
00178 
00179          if (!res) {
00180             /* Step 1: Wait for "1" */
00181             ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
00182             res = ast_waitfordigit(chan, 3000);
00183             fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
00184             if (res == '1')
00185                res = 0;
00186             else
00187                res = -1;
00188          }
00189          if (!res) {
00190             res = ast_safe_sleep(chan, 1000);
00191          }
00192          if (!res) {
00193             /* Step 2: Send "2" */
00194             ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
00195             res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
00196             fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
00197             if (res > 0)
00198                res = 0;
00199          }
00200          if (!res) {
00201             /* Step 3: Wait one second */
00202             ast_debug(1, "TestClient: 3.  Wait one second\n");
00203             res = ast_safe_sleep(chan, 1000);
00204             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00205             if (res > 0)
00206                res = 0;
00207          }
00208          if (!res) {
00209             /* Step 4: Measure noise */
00210             ast_debug(1, "TestClient: 4.  Measure noise\n");
00211             res = measurenoise(chan, 5000, "TestClient");
00212             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00213             if (res > 0)
00214                res = 0;
00215          }
00216          if (!res) {
00217             /* Step 5: Wait for "4" */
00218             ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
00219             res = ast_waitfordigit(chan, 3000);
00220             fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
00221             if (res == '4')
00222                res = 0;
00223             else
00224                res = -1;
00225          }
00226          if (!res) {
00227             /* Step 6: Transmit tone noise */
00228             ast_debug(1, "TestClient: 6.  Transmit tone\n");
00229             res = sendnoise(chan, 6000);
00230             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00231          }
00232          if (!res || (res == '5')) {
00233             /* Step 7: Wait for "5" */
00234             ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
00235             if (!res)
00236                res = ast_waitfordigit(chan, 3000);
00237             fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
00238             if (res == '5')
00239                res = 0;
00240             else
00241                res = -1;
00242          }
00243          if (!res) {
00244             /* Step 8: Wait one second */
00245             ast_debug(1, "TestClient: 8.  Wait one second\n");
00246             res = ast_safe_sleep(chan, 1000);
00247             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00248             if (res > 0)
00249                res = 0;
00250          }
00251          if (!res) {
00252             /* Step 9: Measure noise */
00253             ast_debug(1, "TestClient: 9.  Measure tone\n");
00254             res = measurenoise(chan, 4000, "TestClient");
00255             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00256             if (res > 0)
00257                res = 0;
00258          }
00259          if (!res) {
00260             /* Step 10: Send "7" */
00261             ast_debug(1, "TestClient: 10.  Send DTMF 7\n");
00262             res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
00263             fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
00264             if (res > 0)
00265                res =0;
00266          }
00267          if (!res) {
00268             /* Step 11: Wait for "8" */
00269             ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
00270             res = ast_waitfordigit(chan, 3000);
00271             fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
00272             if (res == '8')
00273                res = 0;
00274             else
00275                res = -1;
00276          }
00277          if (!res) {
00278             res = ast_safe_sleep(chan, 1000);
00279          }
00280          if (!res) {
00281             /* Step 12: Hangup! */
00282             ast_debug(1, "TestClient: 12.  Hangup\n");
00283          }
00284 
00285          ast_debug(1, "-- TEST COMPLETE--\n");
00286          fprintf(f, "-- END TEST--\n");
00287          fclose(f);
00288          res = -1;
00289       } else
00290          res = -1;
00291    } else {
00292       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00293       res = -1;
00294    }
00295    return res;
00296 }
00297 
00298 static int testserver_exec(struct ast_channel *chan, void *data)
00299 {
00300    int res = 0;
00301    char testid[80]="";
00302    char fn[80];
00303    FILE *f;
00304    if (chan->_state != AST_STATE_UP)
00305       res = ast_answer(chan);
00306    /* Read version */
00307    ast_debug(1, "Read client version\n");
00308    if (!res)
00309       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00310    if (res > 0)
00311       res = 0;
00312 
00313    ast_debug(1, "client version: %s\n", testid);
00314    ast_debug(1, "Transmit server version\n");
00315 
00316    res = ast_safe_sleep(chan, 1000);
00317    if (!res)
00318       res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
00319    if (res > 0)
00320       res = 0;
00321 
00322    if (!res)
00323       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00324    ast_debug(1, "read test identifier: %s\n", testid);
00325    /* Check for sneakyness */
00326    if (strchr(testid, '/'))
00327       res = -1;
00328    if ((res >=0) && (!ast_strlen_zero(testid))) {
00329       /* Got a Test ID!  Whoo hoo! */
00330       /* Make the directory to hold the test results in case it's not there */
00331       snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00332       ast_mkdir(fn, 0777);
00333       snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
00334       if ((f = fopen(fn, "w+"))) {
00335          setlinebuf(f);
00336          fprintf(f, "SERVERCHAN:    %s\n", chan->name);
00337          fprintf(f, "SERVERTEST ID: %s\n", testid);
00338          fprintf(f, "ANSWER:        PASS\n");
00339          ast_debug(1, "Processing Test ID '%s'\n", testid);
00340          res = ast_safe_sleep(chan, 1000);
00341          if (!res) {
00342             /* Step 1: Send "1" */
00343             ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
00344             res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
00345             fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
00346             if (res > 0)
00347                res = 0;
00348          }
00349          if (!res) {
00350             /* Step 2: Wait for "2" */
00351             ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
00352             res = ast_waitfordigit(chan, 3000);
00353             fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
00354             if (res == '2')
00355                res = 0;
00356             else
00357                res = -1;
00358          }
00359          if (!res) {
00360             /* Step 3: Measure noise */
00361             ast_debug(1, "TestServer: 3.  Measure noise\n");
00362             res = measurenoise(chan, 6000, "TestServer");
00363             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00364             if (res > 0)
00365                res = 0;
00366          }
00367          if (!res) {
00368             /* Step 4: Send "4" */
00369             ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
00370             res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
00371             fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
00372             if (res > 0)
00373                res = 0;
00374          }
00375          if (!res) {
00376             /* Step 5: Wait one second */
00377             ast_debug(1, "TestServer: 5.  Wait one second\n");
00378             res = ast_safe_sleep(chan, 1000);
00379             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00380             if (res > 0)
00381                res = 0;
00382          }
00383          if (!res) {
00384             /* Step 6: Measure noise */
00385             ast_debug(1, "TestServer: 6.  Measure tone\n");
00386             res = measurenoise(chan, 4000, "TestServer");
00387             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00388             if (res > 0)
00389                res = 0;
00390          }
00391          if (!res) {
00392             /* Step 7: Send "5" */
00393             ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
00394             res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
00395             fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
00396             if (res > 0)
00397                res = 0;
00398          }
00399          if (!res) {
00400             /* Step 8: Transmit tone noise */
00401             ast_debug(1, "TestServer: 8.  Transmit tone\n");
00402             res = sendnoise(chan, 6000);
00403             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00404          }
00405 
00406          if (!res || (res == '7')) {
00407             /* Step 9: Wait for "7" */
00408             ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
00409             if (!res)
00410                res = ast_waitfordigit(chan, 3000);
00411             fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
00412             if (res == '7')
00413                res = 0;
00414             else
00415                res = -1;
00416          }
00417          if (!res) {
00418             res = ast_safe_sleep(chan, 1000);
00419          }
00420          if (!res) {
00421             /* Step 10: Send "8" */
00422             ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
00423             res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
00424             fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
00425             if (res > 0)
00426                res = 0;
00427          }
00428          if (!res) {
00429             /* Step 11: Wait for hangup to arrive! */
00430             ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
00431             res = ast_safe_sleep(chan, 10000);
00432             fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
00433          }
00434 
00435          ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
00436          fprintf(f, "-- END TEST--\n");
00437          fclose(f);
00438          res = -1;
00439       } else
00440          res = -1;
00441    } else {
00442       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00443       res = -1;
00444    }
00445    return res;
00446 }
00447 
00448 static int unload_module(void)
00449 {
00450    int res;
00451 
00452    res = ast_unregister_application(testc_app);
00453    res |= ast_unregister_application(tests_app);
00454 
00455    return res;
00456 }
00457 
00458 static int load_module(void)
00459 {
00460    int res;
00461 
00462    res = ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
00463    res |= ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
00464 
00465    return res;
00466 }
00467 
00468 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");

Generated on Thu Jul 9 13:40:20 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7