Mon Jun 27 16:50:47 2011

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: 227580 $")
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.codec == 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, const char *data)
00147 {
00148    int res = 0;
00149    const 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          }
00215          if (!res) {
00216             /* Step 2: Send "2" */
00217             ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
00218             res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
00219             fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
00220             if (res > 0)
00221                res = 0;
00222          }
00223          if (!res) {
00224             /* Step 3: Wait one second */
00225             ast_debug(1, "TestClient: 3.  Wait one second\n");
00226             res = ast_safe_sleep(chan, 1000);
00227             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00228             if (res > 0)
00229                res = 0;
00230          }
00231          if (!res) {
00232             /* Step 4: Measure noise */
00233             ast_debug(1, "TestClient: 4.  Measure noise\n");
00234             res = measurenoise(chan, 5000, "TestClient");
00235             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00236             if (res > 0)
00237                res = 0;
00238          }
00239          if (!res) {
00240             /* Step 5: Wait for "4" */
00241             ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
00242             res = ast_waitfordigit(chan, 3000);
00243             fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
00244             if (res == '4')
00245                res = 0;
00246             else
00247                res = -1;
00248          }
00249          if (!res) {
00250             /* Step 6: Transmit tone noise */
00251             ast_debug(1, "TestClient: 6.  Transmit tone\n");
00252             res = sendnoise(chan, 6000);
00253             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00254          }
00255          if (!res || (res == '5')) {
00256             /* Step 7: Wait for "5" */
00257             ast_debug(1, "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             /* Step 8: Wait one second */
00268             ast_debug(1, "TestClient: 8.  Wait one second\n");
00269             res = ast_safe_sleep(chan, 1000);
00270             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00271             if (res > 0)
00272                res = 0;
00273          }
00274          if (!res) {
00275             /* Step 9: Measure noise */
00276             ast_debug(1, "TestClient: 9.  Measure tone\n");
00277             res = measurenoise(chan, 4000, "TestClient");
00278             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00279             if (res > 0)
00280                res = 0;
00281          }
00282          if (!res) {
00283             /* Step 10: Send "7" */
00284             ast_debug(1, "TestClient: 10.  Send DTMF 7\n");
00285             res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
00286             fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
00287             if (res > 0)
00288                res =0;
00289          }
00290          if (!res) {
00291             /* Step 11: Wait for "8" */
00292             ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
00293             res = ast_waitfordigit(chan, 3000);
00294             fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
00295             if (res == '8')
00296                res = 0;
00297             else
00298                res = -1;
00299          }
00300          if (!res) {
00301             res = ast_safe_sleep(chan, 1000);
00302          }
00303          if (!res) {
00304             /* Step 12: Hangup! */
00305             ast_debug(1, "TestClient: 12.  Hangup\n");
00306          }
00307 
00308          ast_debug(1, "-- TEST COMPLETE--\n");
00309          fprintf(f, "-- END TEST--\n");
00310          fclose(f);
00311          res = -1;
00312       } else
00313          res = -1;
00314    } else {
00315       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00316       res = -1;
00317    }
00318    return res;
00319 }
00320 
00321 static int testserver_exec(struct ast_channel *chan, const char *data)
00322 {
00323    int res = 0;
00324    char testid[80]="";
00325    char fn[80];
00326    FILE *f;
00327    if (chan->_state != AST_STATE_UP)
00328       res = ast_answer(chan);
00329    /* Read version */
00330    ast_debug(1, "Read client version\n");
00331    if (!res)
00332       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00333    if (res > 0)
00334       res = 0;
00335 
00336    ast_debug(1, "client version: %s\n", testid);
00337    ast_debug(1, "Transmit server version\n");
00338 
00339    res = ast_safe_sleep(chan, 1000);
00340    if (!res)
00341       res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
00342    if (res > 0)
00343       res = 0;
00344 
00345    if (!res)
00346       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00347    ast_debug(1, "read test identifier: %s\n", testid);
00348    /* Check for sneakyness */
00349    if (strchr(testid, '/'))
00350       res = -1;
00351    if ((res >=0) && (!ast_strlen_zero(testid))) {
00352       /* Got a Test ID!  Whoo hoo! */
00353       /* Make the directory to hold the test results in case it's not there */
00354       snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00355       ast_mkdir(fn, 0777);
00356       snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
00357       if ((f = fopen(fn, "w+"))) {
00358          setlinebuf(f);
00359          fprintf(f, "SERVERCHAN:    %s\n", chan->name);
00360          fprintf(f, "SERVERTEST ID: %s\n", testid);
00361          fprintf(f, "ANSWER:        PASS\n");
00362          ast_debug(1, "Processing Test ID '%s'\n", testid);
00363          res = ast_safe_sleep(chan, 1000);
00364          if (!res) {
00365             /* Step 1: Send "1" */
00366             ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
00367             res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
00368             fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
00369             if (res > 0)
00370                res = 0;
00371          }
00372          if (!res) {
00373             /* Step 2: Wait for "2" */
00374             ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
00375             res = ast_waitfordigit(chan, 3000);
00376             fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
00377             if (res == '2')
00378                res = 0;
00379             else
00380                res = -1;
00381          }
00382          if (!res) {
00383             /* Step 3: Measure noise */
00384             ast_debug(1, "TestServer: 3.  Measure noise\n");
00385             res = measurenoise(chan, 6000, "TestServer");
00386             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00387             if (res > 0)
00388                res = 0;
00389          }
00390          if (!res) {
00391             /* Step 4: Send "4" */
00392             ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
00393             res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
00394             fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
00395             if (res > 0)
00396                res = 0;
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          if (!res) {
00407             /* Step 6: Measure noise */
00408             ast_debug(1, "TestServer: 6.  Measure tone\n");
00409             res = measurenoise(chan, 4000, "TestServer");
00410             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00411             if (res > 0)
00412                res = 0;
00413          }
00414          if (!res) {
00415             /* Step 7: Send "5" */
00416             ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
00417             res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
00418             fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
00419             if (res > 0)
00420                res = 0;
00421          }
00422          if (!res) {
00423             /* Step 8: Transmit tone noise */
00424             ast_debug(1, "TestServer: 8.  Transmit tone\n");
00425             res = sendnoise(chan, 6000);
00426             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00427          }
00428 
00429          if (!res || (res == '7')) {
00430             /* Step 9: Wait for "7" */
00431             ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
00432             if (!res)
00433                res = ast_waitfordigit(chan, 3000);
00434             fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
00435             if (res == '7')
00436                res = 0;
00437             else
00438                res = -1;
00439          }
00440          if (!res) {
00441             res = ast_safe_sleep(chan, 1000);
00442          }
00443          if (!res) {
00444             /* Step 10: Send "8" */
00445             ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
00446             res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
00447             fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
00448             if (res > 0)
00449                res = 0;
00450          }
00451          if (!res) {
00452             /* Step 11: Wait for hangup to arrive! */
00453             ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
00454             res = ast_safe_sleep(chan, 10000);
00455             fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
00456          }
00457 
00458          ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
00459          fprintf(f, "-- END TEST--\n");
00460          fclose(f);
00461          res = -1;
00462       } else
00463          res = -1;
00464    } else {
00465       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
00466       res = -1;
00467    }
00468    return res;
00469 }
00470 
00471 static int unload_module(void)
00472 {
00473    int res;
00474 
00475    res = ast_unregister_application(testc_app);
00476    res |= ast_unregister_application(tests_app);
00477 
00478    return res;
00479 }
00480 
00481 static int load_module(void)
00482 {
00483    int res;
00484 
00485    res = ast_register_application_xml(testc_app, testclient_exec);
00486    res |= ast_register_application_xml(tests_app, testserver_exec);
00487 
00488    return res;
00489 }
00490 
00491 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");

Generated on Mon Jun 27 16:50:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7