Fri Nov 12 11:52:17 2010

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"
Include dependency graph for app_test.c:

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 = "0901e4e500243c855563a2d78b0c03e4" , .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static char * testc_app = "TestClient"
static char * tests_app = "TestServer"

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 493 of file app_test.c.

static void __unreg_module ( void   )  [static]

Definition at line 493 of file app_test.c.

static int load_module ( void   )  [static]

Definition at line 483 of file app_test.c.

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

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 }

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

Definition at line 80 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(), ast_frame::data, f, ast_frame::frametype, LOG_NOTICE, ast_frame::ptr, ast_channel::readformat, ast_frame::samples, and ast_frame::subclass.

Referenced by testclient_exec(), and testserver_exec().

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 }

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

Definition at line 135 of file app_test.c.

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

Referenced by testclient_exec(), and testserver_exec().

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 }

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

Definition at line 146 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(), f, LOG_NOTICE, LOG_WARNING, measurenoise(), ast_channel::name, and sendnoise().

Referenced by load_module().

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 }

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

Definition at line 320 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(), f, LOG_NOTICE, measurenoise(), ast_channel::name, and sendnoise().

Referenced by load_module().

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 }

static int unload_module ( void   )  [static]

Definition at line 473 of file app_test.c.

References ast_unregister_application().

00474 {
00475    int res;
00476 
00477    res = ast_unregister_application(testc_app);
00478    res |= ast_unregister_application(tests_app);
00479 
00480    return res; 
00481 }


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 = "0901e4e500243c855563a2d78b0c03e4" , .load = load_module, .unload = unload_module, } [static]

Definition at line 493 of file app_test.c.

Definition at line 493 of file app_test.c.

char* testc_app = "TestClient" [static]

Definition at line 78 of file app_test.c.

char* tests_app = "TestServer" [static]

Definition at line 77 of file app_test.c.


Generated by  doxygen 1.6.2