Tue Aug 20 16:34:23 2013

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

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1