Fri Aug 24 02:25:15 2007

Asterisk developer's documentation


eagi-test.c File Reference

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include "asterisk.h"
#include "asterisk/compat.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 19 of file eagi-test.c.


Function Documentation

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

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

References read_environment(), and run_script().

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

static int read_environment ( void   )  [static]

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

References setenv().

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

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

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

References wait_result().

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

static int run_script ( void   )  [static]

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

References run_command().

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

static char* wait_result ( void   )  [static]

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

References AUDIO_FILENO.

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


Generated on Fri Aug 24 02:25:15 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1