Sat Aug 6 00:39:55 2011

Asterisk developer's documentation


eagi-sphinx-test.c File Reference

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "asterisk.h"
#include "asterisk/compat.h"

Go to the source code of this file.

Defines

#define AUDIO_FILENO   (STDERR_FILENO + 1)
#define SPHINX_HOST   "192.168.1.108"
#define SPHINX_PORT   3460

Functions

static int connect_sphinx (void)
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)

Variables

static int sphinx_sock = -1


Define Documentation

#define AUDIO_FILENO   (STDERR_FILENO + 1)

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

Referenced by wait_result().

#define SPHINX_HOST   "192.168.1.108"

Definition at line 27 of file eagi-sphinx-test.c.

Referenced by connect_sphinx().

#define SPHINX_PORT   3460

Definition at line 28 of file eagi-sphinx-test.c.

Referenced by connect_sphinx().


Function Documentation

static int connect_sphinx ( void   )  [static]

Definition at line 32 of file eagi-sphinx-test.c.

References errno, gethostbyname, hp, SPHINX_HOST, SPHINX_PORT, and sphinx_sock.

Referenced by main().

00033 {
00034    struct hostent *hp;
00035    struct sockaddr_in sin;
00036    int res;
00037    hp = gethostbyname(SPHINX_HOST);
00038    if (!hp) {
00039       fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
00040       return -1;
00041    }
00042    sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
00043    if (sphinx_sock < 0) {
00044       fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
00045       return -1;
00046    }
00047    memset(&sin, 0, sizeof(sin));
00048    sin.sin_family = AF_INET;
00049    sin.sin_port = htons(SPHINX_PORT);
00050    memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
00051    if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
00052       fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
00053       close(sphinx_sock);
00054       sphinx_sock = -1;
00055       return -1;
00056    }
00057    res = fcntl(sphinx_sock, F_GETFL);
00058    if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
00059       fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
00060       close(sphinx_sock);
00061       sphinx_sock = -1;
00062       return -1;
00063    }
00064    return 0;
00065 }

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

Definition at line 202 of file eagi-sphinx-test.c.

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

00203 {
00204    char *tmp;
00205    int ver = 0;
00206    int subver = 0;
00207    /* Setup stdin/stdout for line buffering */
00208    setlinebuf(stdin);
00209    setlinebuf(stdout);
00210    if (read_environment()) {
00211       fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
00212       exit(1);
00213    }
00214    connect_sphinx();
00215    tmp = getenv("agi_enhanced");
00216    if (tmp) {
00217       if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
00218          ver = 0;
00219    }
00220    if (ver < 1) {
00221       fprintf(stderr, "No enhanced AGI services available.  Use EAGI, not AGI\n");
00222       exit(1);
00223    }
00224    if (run_script())
00225       return -1;
00226    exit(0);
00227 }

static int read_environment ( void   )  [static]

Definition at line 67 of file eagi-sphinx-test.c.

Referenced by main().

00068 {
00069    char buf[256];
00070    char *val;
00071    /* Read environment */
00072    for(;;) {
00073       if (!fgets(buf, sizeof(buf), stdin)) {
00074          return -1;
00075       }
00076       if (feof(stdin))
00077          return -1;
00078       buf[strlen(buf) - 1] = '\0';
00079       /* Check for end of environment */
00080       if (!strlen(buf))
00081          return 0;
00082       val = strchr(buf, ':');
00083       if (!val) {
00084          fprintf(stderr, "Invalid environment: '%s'\n", buf);
00085          return -1;
00086       }
00087       *val = '\0';
00088       val++;
00089       val++;
00090       /* Skip space */
00091       fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
00092 
00093       /* Load into normal environment */
00094       setenv(buf, val, 1);
00095       
00096    }
00097    /* Never reached */
00098    return 0;
00099 }

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

Definition at line 160 of file eagi-sphinx-test.c.

References wait_result().

Referenced by run_script().

00161 {
00162    fprintf(stdout, "%s\n", command);
00163    return wait_result();
00164 }

static int run_script ( void   )  [static]

Definition at line 166 of file eagi-sphinx-test.c.

References run_command().

Referenced by main().

00167 {
00168    char *res;
00169    res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
00170    if (!res) {
00171       fprintf(stderr, "Failed to execute command\n");
00172       return -1;
00173    }
00174    fprintf(stderr, "1. Result is '%s'\n", res);
00175    res = run_command("STREAM FILE demo-nomatch 0123456789*#");
00176    if (!res) {
00177       fprintf(stderr, "Failed to execute command\n");
00178       return -1;
00179    }
00180    fprintf(stderr, "2. Result is '%s'\n", res);
00181    res = run_command("SAY NUMBER 23452345 0123456789*#");
00182    if (!res) {
00183       fprintf(stderr, "Failed to execute command\n");
00184       return -1;
00185    }
00186    fprintf(stderr, "3. Result is '%s'\n", res);
00187    res = run_command("GET DATA demo-enterkeywords");
00188    if (!res) {
00189       fprintf(stderr, "Failed to execute command\n");
00190       return -1;
00191    }
00192    fprintf(stderr, "4. Result is '%s'\n", res);
00193    res = run_command("STREAM FILE auth-thankyou \"\"");
00194    if (!res) {
00195       fprintf(stderr, "Failed to execute command\n");
00196       return -1;
00197    }
00198    fprintf(stderr, "5. Result is '%s'\n", res);
00199    return 0;
00200 }

static char* wait_result ( void   )  [static]

Definition at line 101 of file eagi-sphinx-test.c.

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

Referenced by run_command().

00102 {
00103    fd_set fds;
00104    int res;
00105    int max;
00106    static char astresp[256];
00107    static char sphinxresp[256];
00108    char audiobuf[4096];
00109    for (;;) {
00110       FD_ZERO(&fds);
00111       FD_SET(STDIN_FILENO, &fds);
00112       FD_SET(AUDIO_FILENO, &fds);
00113       max = AUDIO_FILENO;
00114       if (sphinx_sock > -1) {
00115          FD_SET(sphinx_sock, &fds);
00116          if (sphinx_sock > max)
00117             max = sphinx_sock;
00118       }
00119       /* Wait for *some* sort of I/O */
00120       res = select(max + 1, &fds, NULL, NULL, NULL);
00121       if (res < 0) {
00122          fprintf(stderr, "Error in select: %s\n", strerror(errno));
00123          return NULL;
00124       }
00125       if (FD_ISSET(STDIN_FILENO, &fds)) {
00126          if (!fgets(astresp, sizeof(astresp), stdin)) {
00127             return NULL;
00128          }
00129          if (feof(stdin)) {
00130             fprintf(stderr, "Got hungup on apparently\n");
00131             return NULL;
00132          }
00133          astresp[strlen(astresp) - 1] = '\0';
00134          fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
00135          return astresp;
00136       }
00137       if (FD_ISSET(AUDIO_FILENO, &fds)) {
00138          res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
00139          if ((res > 0) && (sphinx_sock > -1)) {
00140             if (write(sphinx_sock, audiobuf, res) < 0) {
00141                fprintf(stderr, "write() failed: %s\n", strerror(errno));
00142             }
00143          }
00144       }
00145       if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
00146          res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
00147          if (res > 0) {
00148             fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
00149             return sphinxresp;
00150          } else if (res == 0) {
00151             fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
00152             close(sphinx_sock);
00153             sphinx_sock = -1;
00154          }
00155       }
00156    }
00157       
00158 }


Variable Documentation

int sphinx_sock = -1 [static]

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

Referenced by connect_sphinx(), and wait_result().


Generated on Sat Aug 6 00:39:55 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7