Thu Apr 28 2011 17:15:15

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