#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"
Include dependency graph for eagi-sphinx-test.c:
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 |
This code is released into public domain without any warranty of any kind.
Definition in file eagi-sphinx-test.c.
#define AUDIO_FILENO (STDERR_FILENO + 1) |
#define SPHINX_HOST "192.168.1.108" |
#define SPHINX_PORT 3460 |
static int connect_sphinx | ( | void | ) | [static] |
Definition at line 41 of file eagi-sphinx-test.c.
References gethostbyname, hp, SPHINX_HOST, SPHINX_PORT, and sphinx_sock.
Referenced by main().
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 }
int main | ( | int | argc, | |
char * | argv[] | |||
) |
Definition at line 206 of file eagi-sphinx-test.c.
References connect_sphinx(), read_environment(), and run_script().
00207 { 00208 char *tmp; 00209 int ver = 0; 00210 int subver = 0; 00211 /* Setup stdin/stdout for line buffering */ 00212 setlinebuf(stdin); 00213 setlinebuf(stdout); 00214 if (read_environment()) { 00215 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno)); 00216 exit(1); 00217 } 00218 connect_sphinx(); 00219 tmp = getenv("agi_enhanced"); 00220 if (tmp) { 00221 if (sscanf(tmp, "%d.%d", &ver, &subver) != 2) 00222 ver = 0; 00223 } 00224 if (ver < 1) { 00225 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n"); 00226 exit(1); 00227 } 00228 if (run_script()) 00229 return -1; 00230 exit(0); 00231 }
static int read_environment | ( | void | ) | [static] |
Definition at line 76 of file eagi-sphinx-test.c.
References setenv().
Referenced by main().
00077 { 00078 char buf[256]; 00079 char *val; 00080 /* Read environment */ 00081 for(;;) { 00082 fgets(buf, sizeof(buf), stdin); 00083 if (feof(stdin)) 00084 return -1; 00085 buf[strlen(buf) - 1] = '\0'; 00086 /* Check for end of environment */ 00087 if (!strlen(buf)) 00088 return 0; 00089 val = strchr(buf, ':'); 00090 if (!val) { 00091 fprintf(stderr, "Invalid environment: '%s'\n", buf); 00092 return -1; 00093 } 00094 *val = '\0'; 00095 val++; 00096 val++; 00097 /* Skip space */ 00098 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val); 00099 00100 /* Load into normal environment */ 00101 setenv(buf, val, 1); 00102 00103 } 00104 /* Never reached */ 00105 return 0; 00106 }
static char* run_command | ( | char * | command | ) | [static] |
Definition at line 164 of file eagi-sphinx-test.c.
References wait_result().
Referenced by run_script().
00165 { 00166 fprintf(stdout, "%s\n", command); 00167 return wait_result(); 00168 }
static int run_script | ( | void | ) | [static] |
Definition at line 170 of file eagi-sphinx-test.c.
References run_command().
Referenced by main().
00171 { 00172 char *res; 00173 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#"); 00174 if (!res) { 00175 fprintf(stderr, "Failed to execute command\n"); 00176 return -1; 00177 } 00178 fprintf(stderr, "1. Result is '%s'\n", res); 00179 res = run_command("STREAM FILE demo-nomatch 0123456789*#"); 00180 if (!res) { 00181 fprintf(stderr, "Failed to execute command\n"); 00182 return -1; 00183 } 00184 fprintf(stderr, "2. Result is '%s'\n", res); 00185 res = run_command("SAY NUMBER 23452345 0123456789*#"); 00186 if (!res) { 00187 fprintf(stderr, "Failed to execute command\n"); 00188 return -1; 00189 } 00190 fprintf(stderr, "3. Result is '%s'\n", res); 00191 res = run_command("GET DATA demo-enterkeywords"); 00192 if (!res) { 00193 fprintf(stderr, "Failed to execute command\n"); 00194 return -1; 00195 } 00196 fprintf(stderr, "4. Result is '%s'\n", res); 00197 res = run_command("STREAM FILE auth-thankyou \"\""); 00198 if (!res) { 00199 fprintf(stderr, "Failed to execute command\n"); 00200 return -1; 00201 } 00202 fprintf(stderr, "5. Result is '%s'\n", res); 00203 return 0; 00204 }
static char* wait_result | ( | void | ) | [static] |
Definition at line 108 of file eagi-sphinx-test.c.
References AUDIO_FILENO, and sphinx_sock.
Referenced by run_command().
00109 { 00110 fd_set fds; 00111 int res; 00112 int max; 00113 static char astresp[256]; 00114 static char sphinxresp[256]; 00115 char audiobuf[4096]; 00116 for (;;) { 00117 FD_ZERO(&fds); 00118 FD_SET(STDIN_FILENO, &fds); 00119 FD_SET(AUDIO_FILENO, &fds); 00120 max = AUDIO_FILENO; 00121 if (sphinx_sock > -1) { 00122 FD_SET(sphinx_sock, &fds); 00123 if (sphinx_sock > max) 00124 max = sphinx_sock; 00125 } 00126 /* Wait for *some* sort of I/O */ 00127 res = select(max + 1, &fds, NULL, NULL, NULL); 00128 if (res < 0) { 00129 fprintf(stderr, "Error in select: %s\n", strerror(errno)); 00130 return NULL; 00131 } 00132 if (FD_ISSET(STDIN_FILENO, &fds)) { 00133 fgets(astresp, sizeof(astresp), stdin); 00134 if (feof(stdin)) { 00135 fprintf(stderr, "Got hungup on apparently\n"); 00136 return NULL; 00137 } 00138 astresp[strlen(astresp) - 1] = '\0'; 00139 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp); 00140 return astresp; 00141 } 00142 if (FD_ISSET(AUDIO_FILENO, &fds)) { 00143 res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf)); 00144 if (res > 0) { 00145 if (sphinx_sock > -1) 00146 write(sphinx_sock, audiobuf, res); 00147 } 00148 } 00149 if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) { 00150 res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp)); 00151 if (res > 0) { 00152 fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp); 00153 return sphinxresp; 00154 } else if (res == 0) { 00155 fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n"); 00156 close(sphinx_sock); 00157 sphinx_sock = -1; 00158 } 00159 } 00160 } 00161 00162 }
int sphinx_sock = -1 [static] |
Definition at line 39 of file eagi-sphinx-test.c.
Referenced by connect_sphinx(), and wait_result().