Wed Oct 28 11:51:03 2009

Asterisk developer's documentation


eagi-sphinx-test.c

Go to the documentation of this file.
00001 /*
00002  * Extended AGI test application
00003  *
00004  * This code is released into public domain
00005  * without any warranty of any kind.
00006  *
00007  */
00008 
00009 /*! \file
00010  * Extended AGI test application
00011  *
00012  * This code is released into public domain
00013  * without any warranty of any kind.
00014  *
00015  * \ingroup agi
00016  */
00017 
00018 #include <stdio.h>
00019 #include <unistd.h>
00020 #include <stdlib.h>
00021 #include <errno.h>
00022 #include <string.h>
00023 #include <sys/select.h>
00024 #include <fcntl.h>
00025 #include <sys/socket.h>
00026 #include <netinet/in.h>
00027 #include <arpa/inet.h>
00028 #include <netdb.h>
00029 
00030 #include "asterisk.h"
00031 
00032 #include "asterisk/compat.h"
00033 
00034 #define AUDIO_FILENO (STDERR_FILENO + 1)
00035 
00036 #define SPHINX_HOST "192.168.1.108"
00037 #define SPHINX_PORT 3460
00038 
00039 static int sphinx_sock = -1;
00040 
00041 static int connect_sphinx(void)
00042 {
00043    struct hostent *hp;
00044    struct sockaddr_in sin;
00045    int res;
00046    hp = gethostbyname(SPHINX_HOST);
00047    if (!hp) {
00048       fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
00049       return -1;
00050    }
00051    sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
00052    if (sphinx_sock < 0) {
00053       fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
00054       return -1;
00055    }
00056    memset(&sin, 0, sizeof(sin));
00057    sin.sin_family = AF_INET;
00058    sin.sin_port = htons(SPHINX_PORT);
00059    memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
00060    if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
00061       fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
00062       close(sphinx_sock);
00063       sphinx_sock = -1;
00064       return -1;
00065    }
00066    res = fcntl(sphinx_sock, F_GETFL);
00067    if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
00068       fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
00069       close(sphinx_sock);
00070       sphinx_sock = -1;
00071       return -1;
00072    }
00073    return 0;
00074 }
00075 
00076 static int read_environment(void)
00077 {
00078    char buf[256];
00079    char *val;
00080    /* Read environment */
00081    for(;;) {
00082       if (!fgets(buf, sizeof(buf), stdin)) {
00083          return -1;
00084       }
00085       if (feof(stdin))
00086          return -1;
00087       buf[strlen(buf) - 1] = '\0';
00088       /* Check for end of environment */
00089       if (!strlen(buf))
00090          return 0;
00091       val = strchr(buf, ':');
00092       if (!val) {
00093          fprintf(stderr, "Invalid environment: '%s'\n", buf);
00094          return -1;
00095       }
00096       *val = '\0';
00097       val++;
00098       val++;
00099       /* Skip space */
00100       fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
00101 
00102       /* Load into normal environment */
00103       setenv(buf, val, 1);
00104       
00105    }
00106    /* Never reached */
00107    return 0;
00108 }
00109 
00110 static char *wait_result(void)
00111 {
00112    fd_set fds;
00113    int res;
00114    int max;
00115    static char astresp[256];
00116    static char sphinxresp[256];
00117    char audiobuf[4096];
00118    for (;;) {
00119       FD_ZERO(&fds);
00120       FD_SET(STDIN_FILENO, &fds);
00121       FD_SET(AUDIO_FILENO, &fds);
00122       max = AUDIO_FILENO;
00123       if (sphinx_sock > -1) {
00124          FD_SET(sphinx_sock, &fds);
00125          if (sphinx_sock > max)
00126             max = sphinx_sock;
00127       }
00128       /* Wait for *some* sort of I/O */
00129       res = select(max + 1, &fds, NULL, NULL, NULL);
00130       if (res < 0) {
00131          fprintf(stderr, "Error in select: %s\n", strerror(errno));
00132          return NULL;
00133       }
00134       if (FD_ISSET(STDIN_FILENO, &fds)) {
00135          if (!fgets(astresp, sizeof(astresp), stdin)) {
00136             return NULL;
00137          }
00138          if (feof(stdin)) {
00139             fprintf(stderr, "Got hungup on apparently\n");
00140             return NULL;
00141          }
00142          astresp[strlen(astresp) - 1] = '\0';
00143          fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
00144          return astresp;
00145       }
00146       if (FD_ISSET(AUDIO_FILENO, &fds)) {
00147          res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
00148          if ((res > 0) && (sphinx_sock > -1)) {
00149             if (write(sphinx_sock, audiobuf, res) < 0) {
00150                fprintf(stderr, "write() failed: %s\n", strerror(errno));
00151             }
00152          }
00153       }
00154       if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
00155          res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
00156          if (res > 0) {
00157             fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
00158             return sphinxresp;
00159          } else if (res == 0) {
00160             fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
00161             close(sphinx_sock);
00162             sphinx_sock = -1;
00163          }
00164       }
00165    }
00166       
00167 }
00168 
00169 static char *run_command(char *command)
00170 {
00171    fprintf(stdout, "%s\n", command);
00172    return wait_result();
00173 }
00174 
00175 static int run_script(void)
00176 {
00177    char *res;
00178    res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
00179    if (!res) {
00180       fprintf(stderr, "Failed to execute command\n");
00181       return -1;
00182    }
00183    fprintf(stderr, "1. Result is '%s'\n", res);
00184    res = run_command("STREAM FILE demo-nomatch 0123456789*#");
00185    if (!res) {
00186       fprintf(stderr, "Failed to execute command\n");
00187       return -1;
00188    }
00189    fprintf(stderr, "2. Result is '%s'\n", res);
00190    res = run_command("SAY NUMBER 23452345 0123456789*#");
00191    if (!res) {
00192       fprintf(stderr, "Failed to execute command\n");
00193       return -1;
00194    }
00195    fprintf(stderr, "3. Result is '%s'\n", res);
00196    res = run_command("GET DATA demo-enterkeywords");
00197    if (!res) {
00198       fprintf(stderr, "Failed to execute command\n");
00199       return -1;
00200    }
00201    fprintf(stderr, "4. Result is '%s'\n", res);
00202    res = run_command("STREAM FILE auth-thankyou \"\"");
00203    if (!res) {
00204       fprintf(stderr, "Failed to execute command\n");
00205       return -1;
00206    }
00207    fprintf(stderr, "5. Result is '%s'\n", res);
00208    return 0;
00209 }
00210 
00211 int main(int argc, char *argv[])
00212 {
00213    char *tmp;
00214    int ver = 0;
00215    int subver = 0;
00216    /* Setup stdin/stdout for line buffering */
00217    setlinebuf(stdin);
00218    setlinebuf(stdout);
00219    if (read_environment()) {
00220       fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
00221       exit(1);
00222    }
00223    connect_sphinx();
00224    tmp = getenv("agi_enhanced");
00225    if (tmp) {
00226       if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
00227          ver = 0;
00228    }
00229    if (ver < 1) {
00230       fprintf(stderr, "No enhanced AGI services available.  Use EAGI, not AGI\n");
00231       exit(1);
00232    }
00233    if (run_script())
00234       return -1;
00235    exit(0);
00236 }

Generated on Wed Oct 28 11:51:03 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6