Sat Feb 11 06:34:03 2012

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, const char *data)
static int testserver_exec (struct ast_channel *chan, const char *data)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
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 496 of file app_test.c.

static void __unreg_module ( void   )  [static]

Definition at line 496 of file app_test.c.

static int load_module ( void   )  [static]

Definition at line 486 of file app_test.c.

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

00487 {
00488    int res;
00489 
00490    res = ast_register_application_xml(testc_app, testclient_exec);
00491    res |= ast_register_application_xml(tests_app, testserver_exec);
00492 
00493    return res;
00494 }

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

Definition at line 84 of file app_test.c.

References ast_debug, ast_format_copy(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_set_read_format(), ast_set_read_format_by_id(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_frame::data, f, ast_frame_subclass::format, ast_frame::frametype, ast_format::id, LOG_NOTICE, ast_frame::ptr, ast_channel::readformat, ast_frame::samples, and ast_frame::subclass.

Referenced by testclient_exec(), and testserver_exec().

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    struct ast_format rformat;
00095 
00096    ast_format_copy(&rformat, &chan->readformat);
00097    if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
00098       ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
00099       return -1;
00100    }
00101    start = ast_tvnow();
00102    for(;;) {
00103       mssofar = ast_tvdiff_ms(ast_tvnow(), start);
00104       if (mssofar > ms)
00105          break;
00106       res = ast_waitfor(chan, ms - mssofar);
00107       if (res < 1)
00108          break;
00109       f = ast_read(chan);
00110       if (!f) {
00111          res = -1;
00112          break;
00113       }
00114       if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
00115          foo = (short *)f->data.ptr;
00116          for (x=0;x<f->samples;x++) {
00117             noise += abs(foo[x]);
00118             samples++;
00119          }
00120       }
00121       ast_frfree(f);
00122    }
00123 
00124    if (rformat.id) {
00125       if (ast_set_read_format(chan, &rformat)) {
00126          ast_log(LOG_NOTICE, "Unable to restore original format!\n");
00127          return -1;
00128       }
00129    }
00130    if (res < 0)
00131       return res;
00132    if (!samples) {
00133       ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
00134       return -1;
00135    }
00136    ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
00137    return (noise / samples);
00138 }

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

Definition at line 140 of file app_test.c.

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

Referenced by testclient_exec(), and testserver_exec().

00141 {
00142    int res;
00143    res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
00144    if (!res) {
00145       res = ast_waitfordigit(chan, ms);
00146       ast_tonepair_stop(chan);
00147    }
00148    return res;
00149 }

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

Definition at line 151 of file app_test.c.

References ast_channel::_state, ast_answer(), ast_app_getdata(), ast_channel_name(), 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(), and sendnoise().

Referenced by load_module().

00152 {
00153    int res = 0;
00154    const char *testid=data;
00155    char fn[80];
00156    char serverver[80];
00157    FILE *f;
00158 
00159    /* Check for test id */
00160    if (ast_strlen_zero(testid)) {
00161       ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
00162       return -1;
00163    }
00164 
00165    if (chan->_state != AST_STATE_UP)
00166       res = ast_answer(chan);
00167 
00168    /* Wait a few just to be sure things get started */
00169    res = ast_safe_sleep(chan, 3000);
00170    /* Transmit client version */
00171    if (!res)
00172       res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
00173    ast_debug(1, "Transmit client version\n");
00174 
00175    /* Read server version */
00176    ast_debug(1, "Read server version\n");
00177    if (!res)
00178       res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
00179    if (res > 0)
00180       res = 0;
00181    ast_debug(1, "server version: %s\n", serverver);
00182 
00183    if (res > 0)
00184       res = 0;
00185 
00186    if (!res)
00187       res = ast_safe_sleep(chan, 1000);
00188    /* Send test id */
00189    if (!res)
00190       res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
00191    if (!res)
00192       res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
00193    ast_debug(1, "send test identifier: %s\n", testid);
00194 
00195    if ((res >=0) && (!ast_strlen_zero(testid))) {
00196       /* Make the directory to hold the test results in case it's not there */
00197       snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00198       ast_mkdir(fn, 0777);
00199       snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
00200       if ((f = fopen(fn, "w+"))) {
00201          setlinebuf(f);
00202          fprintf(f, "CLIENTCHAN:    %s\n", ast_channel_name(chan));
00203          fprintf(f, "CLIENTTEST ID: %s\n", testid);
00204          fprintf(f, "ANSWER:        PASS\n");
00205          res = 0;
00206 
00207          if (!res) {
00208             /* Step 1: Wait for "1" */
00209             ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
00210             res = ast_waitfordigit(chan, 3000);
00211             fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
00212             if (res == '1')
00213                res = 0;
00214             else
00215                res = -1;
00216          }
00217          if (!res) {
00218             res = ast_safe_sleep(chan, 1000);
00219          }
00220          if (!res) {
00221             /* Step 2: Send "2" */
00222             ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
00223             res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
00224             fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
00225             if (res > 0)
00226                res = 0;
00227          }
00228          if (!res) {
00229             /* Step 3: Wait one second */
00230             ast_debug(1, "TestClient: 3.  Wait one second\n");
00231             res = ast_safe_sleep(chan, 1000);
00232             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00233             if (res > 0)
00234                res = 0;
00235          }
00236          if (!res) {
00237             /* Step 4: Measure noise */
00238             ast_debug(1, "TestClient: 4.  Measure noise\n");
00239             res = measurenoise(chan, 5000, "TestClient");
00240             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00241             if (res > 0)
00242                res = 0;
00243          }
00244          if (!res) {
00245             /* Step 5: Wait for "4" */
00246             ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
00247             res = ast_waitfordigit(chan, 3000);
00248             fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
00249             if (res == '4')
00250                res = 0;
00251             else
00252                res = -1;
00253          }
00254          if (!res) {
00255             /* Step 6: Transmit tone noise */
00256             ast_debug(1, "TestClient: 6.  Transmit tone\n");
00257             res = sendnoise(chan, 6000);
00258             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00259          }
00260          if (!res || (res == '5')) {
00261             /* Step 7: Wait for "5" */
00262             ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
00263             if (!res)
00264                res = ast_waitfordigit(chan, 3000);
00265             fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
00266             if (res == '5')
00267                res = 0;
00268             else
00269                res = -1;
00270          }
00271          if (!res) {
00272             /* Step 8: Wait one second */
00273             ast_debug(1, "TestClient: 8.  Wait one second\n");
00274             res = ast_safe_sleep(chan, 1000);
00275             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00276             if (res > 0)
00277                res = 0;
00278          }
00279          if (!res) {
00280             /* Step 9: Measure noise */
00281             ast_debug(1, "TestClient: 9.  Measure tone\n");
00282             res = measurenoise(chan, 4000, "TestClient");
00283             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00284             if (res > 0)
00285                res = 0;
00286          }
00287          if (!res) {
00288             /* Step 10: Send "7" */
00289             ast_debug(1, "TestClient: 10.  Send DTMF 7\n");
00290             res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
00291             fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
00292             if (res > 0)
00293                res =0;
00294          }
00295          if (!res) {
00296             /* Step 11: Wait for "8" */
00297             ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
00298             res = ast_waitfordigit(chan, 3000);
00299             fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
00300             if (res == '8')
00301                res = 0;
00302             else
00303                res = -1;
00304          }
00305          if (!res) {
00306             res = ast_safe_sleep(chan, 1000);
00307          }
00308          if (!res) {
00309             /* Step 12: Hangup! */
00310             ast_debug(1, "TestClient: 12.  Hangup\n");
00311          }
00312 
00313          ast_debug(1, "-- TEST COMPLETE--\n");
00314          fprintf(f, "-- END TEST--\n");
00315          fclose(f);
00316          res = -1;
00317       } else
00318          res = -1;
00319    } else {
00320       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
00321       res = -1;
00322    }
00323    return res;
00324 }

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

Definition at line 326 of file app_test.c.

References ast_channel::_state, ast_answer(), ast_app_getdata(), ast_channel_name(), 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(), and sendnoise().

Referenced by load_module().

00327 {
00328    int res = 0;
00329    char testid[80]="";
00330    char fn[80];
00331    FILE *f;
00332    if (chan->_state != AST_STATE_UP)
00333       res = ast_answer(chan);
00334    /* Read version */
00335    ast_debug(1, "Read client version\n");
00336    if (!res)
00337       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00338    if (res > 0)
00339       res = 0;
00340 
00341    ast_debug(1, "client version: %s\n", testid);
00342    ast_debug(1, "Transmit server version\n");
00343 
00344    res = ast_safe_sleep(chan, 1000);
00345    if (!res)
00346       res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
00347    if (res > 0)
00348       res = 0;
00349 
00350    if (!res)
00351       res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
00352    ast_debug(1, "read test identifier: %s\n", testid);
00353    /* Check for sneakyness */
00354    if (strchr(testid, '/'))
00355       res = -1;
00356    if ((res >=0) && (!ast_strlen_zero(testid))) {
00357       /* Got a Test ID!  Whoo hoo! */
00358       /* Make the directory to hold the test results in case it's not there */
00359       snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
00360       ast_mkdir(fn, 0777);
00361       snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
00362       if ((f = fopen(fn, "w+"))) {
00363          setlinebuf(f);
00364          fprintf(f, "SERVERCHAN:    %s\n", ast_channel_name(chan));
00365          fprintf(f, "SERVERTEST ID: %s\n", testid);
00366          fprintf(f, "ANSWER:        PASS\n");
00367          ast_debug(1, "Processing Test ID '%s'\n", testid);
00368          res = ast_safe_sleep(chan, 1000);
00369          if (!res) {
00370             /* Step 1: Send "1" */
00371             ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
00372             res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
00373             fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
00374             if (res > 0)
00375                res = 0;
00376          }
00377          if (!res) {
00378             /* Step 2: Wait for "2" */
00379             ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
00380             res = ast_waitfordigit(chan, 3000);
00381             fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
00382             if (res == '2')
00383                res = 0;
00384             else
00385                res = -1;
00386          }
00387          if (!res) {
00388             /* Step 3: Measure noise */
00389             ast_debug(1, "TestServer: 3.  Measure noise\n");
00390             res = measurenoise(chan, 6000, "TestServer");
00391             fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00392             if (res > 0)
00393                res = 0;
00394          }
00395          if (!res) {
00396             /* Step 4: Send "4" */
00397             ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
00398             res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
00399             fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
00400             if (res > 0)
00401                res = 0;
00402          }
00403          if (!res) {
00404             /* Step 5: Wait one second */
00405             ast_debug(1, "TestServer: 5.  Wait one second\n");
00406             res = ast_safe_sleep(chan, 1000);
00407             fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
00408             if (res > 0)
00409                res = 0;
00410          }
00411          if (!res) {
00412             /* Step 6: Measure noise */
00413             ast_debug(1, "TestServer: 6.  Measure tone\n");
00414             res = measurenoise(chan, 4000, "TestServer");
00415             fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
00416             if (res > 0)
00417                res = 0;
00418          }
00419          if (!res) {
00420             /* Step 7: Send "5" */
00421             ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
00422             res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
00423             fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
00424             if (res > 0)
00425                res = 0;
00426          }
00427          if (!res) {
00428             /* Step 8: Transmit tone noise */
00429             ast_debug(1, "TestServer: 8.  Transmit tone\n");
00430             res = sendnoise(chan, 6000);
00431             fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
00432          }
00433 
00434          if (!res || (res == '7')) {
00435             /* Step 9: Wait for "7" */
00436             ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
00437             if (!res)
00438                res = ast_waitfordigit(chan, 3000);
00439             fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
00440             if (res == '7')
00441                res = 0;
00442             else
00443                res = -1;
00444          }
00445          if (!res) {
00446             res = ast_safe_sleep(chan, 1000);
00447          }
00448          if (!res) {
00449             /* Step 10: Send "8" */
00450             ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
00451             res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
00452             fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
00453             if (res > 0)
00454                res = 0;
00455          }
00456          if (!res) {
00457             /* Step 11: Wait for hangup to arrive! */
00458             ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
00459             res = ast_safe_sleep(chan, 10000);
00460             fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
00461          }
00462 
00463          ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
00464          fprintf(f, "-- END TEST--\n");
00465          fclose(f);
00466          res = -1;
00467       } else
00468          res = -1;
00469    } else {
00470       ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
00471       res = -1;
00472    }
00473    return res;
00474 }

static int unload_module ( void   )  [static]

Definition at line 476 of file app_test.c.

References ast_unregister_application().

00477 {
00478    int res;
00479 
00480    res = ast_unregister_application(testc_app);
00481    res |= ast_unregister_application(tests_app);
00482 
00483    return res;
00484 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 496 of file app_test.c.

Definition at line 496 of file app_test.c.

char* testc_app = "TestClient" [static]

Definition at line 82 of file app_test.c.

char* tests_app = "TestServer" [static]

Definition at line 81 of file app_test.c.


Generated on Sat Feb 11 06:34:03 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6