Fri Jul 24 00:41:14 2009

Asterisk developer's documentation


app_test.c File Reference

Applications to test connection and produce report in text file. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static int measurenoise (struct ast_channel *chan, int ms, char *who)
static int sendnoise (struct ast_channel *chan, int ms)
static int testclient_exec (struct ast_channel *chan, void *data)
static int testserver_exec (struct ast_channel *chan, void *data)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Interface Test Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static char * testc_app = "TestClient"
static char * testc_descrip
static char * testc_synopsis = "Execute Interface Test Client"
static char * tests_app = "TestServer"
static char * tests_descrip
static char * tests_synopsis = "Execute Interface Test Server"


Detailed Description

Applications to test connection and produce report in text file.

Author:
Mark Spencer <markster@digium.com>

Russell Bryant <russelb@clemson.edu>

Definition in file app_test.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 468 of file app_test.c.

static void __unreg_module ( void   )  [static]

Definition at line 468 of file app_test.c.

static int load_module ( void   )  [static]

Definition at line 458 of file app_test.c.

References ast_register_application, testclient_exec(), and testserver_exec().

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 }

static int measurenoise ( struct ast_channel chan,
int  ms,
char *  who 
) [static]

Definition at line 57 of file app_test.c.

References ast_debug, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_set_read_format(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), chan, f, LOG_NOTICE, and ast_channel::readformat.

Referenced by testclient_exec(), and testserver_exec().

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.ptr;
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 }

static int sendnoise ( struct ast_channel chan,
int  ms 
) [static]

Definition at line 112 of file app_test.c.

References ast_tonepair_start(), ast_tonepair_stop(), ast_waitfordigit(), and chan.

Referenced by testclient_exec(), and testserver_exec().

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 }

static int testclient_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 123 of file app_test.c.

References ast_channel::_state, ast_answer(), ast_app_getdata(), ast_config_AST_LOG_DIR, ast_debug, ast_dtmf_stream(), ast_log(), ast_mkdir(), ast_safe_sleep(), AST_STATE_UP, ast_strlen_zero(), ast_waitfordigit(), chan, f, LOG_NOTICE, LOG_WARNING, measurenoise(), ast_channel::name, and sendnoise().

Referenced by load_module().

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 }

static int testserver_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 298 of file app_test.c.

References ast_channel::_state, ast_answer(), ast_app_getdata(), ast_config_AST_LOG_DIR, ast_debug, ast_dtmf_stream(), ast_log(), ast_mkdir(), ast_safe_sleep(), AST_STATE_UP, ast_strlen_zero(), ast_waitfordigit(), chan, f, LOG_NOTICE, measurenoise(), ast_channel::name, and sendnoise().

Referenced by load_module().

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 }

static int unload_module ( void   )  [static]

Definition at line 448 of file app_test.c.

References ast_unregister_application().

00449 {
00450    int res;
00451 
00452    res = ast_unregister_application(testc_app);
00453    res |= ast_unregister_application(tests_app);
00454 
00455    return res;
00456 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Interface Test Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 468 of file app_test.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 468 of file app_test.c.

char* testc_app = "TestClient" [static]

Definition at line 54 of file app_test.c.

char* testc_descrip [static]

Initial value:

 
    "  TestClient(testid): Executes test client with given testid.\n"
    "Results stored in /var/log/asterisk/testreports/<testid>-client.txt"

Definition at line 50 of file app_test.c.

char* testc_synopsis = "Execute Interface Test Client" [static]

Definition at line 55 of file app_test.c.

char* tests_app = "TestServer" [static]

Definition at line 47 of file app_test.c.

char* tests_descrip [static]

Initial value:

 
    "  TestServer(): Perform test server function and write call report.\n"
    "Results stored in /var/log/asterisk/testreports/<testid>-server.txt"

Definition at line 44 of file app_test.c.

char* tests_synopsis = "Execute Interface Test Server" [static]

Definition at line 48 of file app_test.c.


Generated on Fri Jul 24 00:41:14 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7