eagi-test.c File Reference

#include "asterisk.h"

Include dependency graph for eagi-test.c:

Go to the source code of this file.

Defines

#define AUDIO_FILENO   (STDERR_FILENO + 1)

Functions

int main (int argc, char *argv[])
static int read_environment (void)
static char * run_command (char *command)
static int run_script (void)
static char * wait_result (void)


Detailed Description

Extended AGI test application

This code is released into the public domain with no warranty of any kind

Definition in file eagi-test.c.


Define Documentation

#define AUDIO_FILENO   (STDERR_FILENO + 1)

Definition at line 14 of file eagi-test.c.


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 149 of file eagi-test.c.

References errno, read_environment(), run_script(), and tmp().

00150 {
00151    char *tmp;
00152    int ver = 0;
00153    int subver = 0;
00154    /* Setup stdin/stdout for line buffering */
00155    setlinebuf(stdin);
00156    setlinebuf(stdout);
00157    if (read_environment()) {
00158       fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
00159       exit(1);
00160    }
00161    tmp = getenv("agi_enhanced");
00162    if (tmp) {
00163       if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
00164          ver = 0;
00165    }
00166    if (ver < 1) {
00167       fprintf(stderr, "No enhanced AGI services available.  Use EAGI, not AGI\n");
00168       exit(1);
00169    }
00170    if (run_script())
00171       return -1;
00172    exit(0);
00173 }

static int read_environment ( void   )  [static]

Definition at line 25 of file eagi-test.c.

References buf, and setenv().

00026 {
00027    char buf[256];
00028    char *val;
00029    /* Read environment */
00030    for(;;) {
00031       if (!fgets(buf, sizeof(buf), stdin)) {
00032          return -1;
00033       }
00034       if (feof(stdin))
00035          return -1;
00036       buf[strlen(buf) - 1] = '\0';
00037       /* Check for end of environment */
00038       if (!strlen(buf))
00039          return 0;
00040       val = strchr(buf, ':');
00041       if (!val) {
00042          fprintf(stderr, "Invalid environment: '%s'\n", buf);
00043          return -1;
00044       }
00045       *val = '\0';
00046       val++;
00047       val++;
00048       /* Skip space */
00049       fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
00050 
00051       /* Load into normal environment */
00052       setenv(buf, val, 1);
00053       
00054    }
00055    /* Never reached */
00056    return 0;
00057 }

static char* run_command ( char *  command  )  [static]

Definition at line 107 of file eagi-test.c.

References wait_result().

00108 {
00109    fprintf(stdout, "%s\n", command);
00110    return wait_result();
00111 }

static int run_script ( void   )  [static]

Definition at line 113 of file eagi-test.c.

References run_command().

00114 {
00115    char *res;
00116    res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
00117    if (!res) {
00118       fprintf(stderr, "Failed to execute command\n");
00119       return -1;
00120    }
00121    fprintf(stderr, "1. Result is '%s'\n", res);
00122    res = run_command("STREAM FILE demo-nomatch 0123456789*#");
00123    if (!res) {
00124       fprintf(stderr, "Failed to execute command\n");
00125       return -1;
00126    }
00127    fprintf(stderr, "2. Result is '%s'\n", res);
00128    res = run_command("SAY NUMBER 23452345 0123456789*#");
00129    if (!res) {
00130       fprintf(stderr, "Failed to execute command\n");
00131       return -1;
00132    }
00133    fprintf(stderr, "3. Result is '%s'\n", res);
00134    res = run_command("GET DATA demo-enterkeywords");
00135    if (!res) {
00136       fprintf(stderr, "Failed to execute command\n");
00137       return -1;
00138    }
00139    fprintf(stderr, "4. Result is '%s'\n", res);
00140    res = run_command("STREAM FILE auth-thankyou \"\"");
00141    if (!res) {
00142       fprintf(stderr, "Failed to execute command\n");
00143       return -1;
00144    }
00145    fprintf(stderr, "5. Result is '%s'\n", res);
00146    return 0;
00147 }

static char* wait_result ( void   )  [static]

Definition at line 59 of file eagi-test.c.

References AUDIO_FILENO, errno, FD_SET, FD_ZERO, and NULL.

00060 {
00061    fd_set fds;
00062    int res;
00063    int bytes = 0;
00064    static char astresp[256];
00065    char audiobuf[4096];
00066    for (;;) {
00067       FD_ZERO(&fds);
00068       FD_SET(STDIN_FILENO, &fds);
00069       FD_SET(AUDIO_FILENO, &fds);
00070       /* Wait for *some* sort of I/O */
00071       res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL);
00072       if (res < 0) {
00073          fprintf(stderr, "Error in select: %s\n", strerror(errno));
00074          return NULL;
00075       }
00076       if (FD_ISSET(STDIN_FILENO, &fds)) {
00077          if (!fgets(astresp, sizeof(astresp), stdin)) {
00078             return NULL;
00079          }
00080          if (feof(stdin)) {
00081             fprintf(stderr, "Got hungup on apparently\n");
00082             return NULL;
00083          }
00084          astresp[strlen(astresp) - 1] = '\0';
00085          fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
00086          return astresp;
00087       }
00088       if (FD_ISSET(AUDIO_FILENO, &fds)) {
00089          res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
00090          if (res > 0) {
00091             /* XXX Process the audio with sphinx here XXX */
00092 #if 0
00093             fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes);
00094 #endif
00095             bytes += res;
00096             /* Prentend we detected some audio after 3 seconds */
00097             if (bytes > 16000 * 3) {
00098                return "Sample Message";
00099                bytes = 0;
00100             }
00101          }
00102       }
00103    }
00104       
00105 }


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