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

Generated on Thu Apr 16 06:27:12 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6