#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 |
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 errno, 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 211 of file eagi-sphinx-test.c.
References connect_sphinx(), errno, read_environment(), and run_script().
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 }
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 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 }
static char* run_command | ( | char * | command | ) | [static] |
Definition at line 169 of file eagi-sphinx-test.c.
References wait_result().
Referenced by run_script().
00170 { 00171 fprintf(stdout, "%s\n", command); 00172 return wait_result(); 00173 }
static int run_script | ( | void | ) | [static] |
Definition at line 175 of file eagi-sphinx-test.c.
References run_command().
Referenced by main().
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 }
static char* wait_result | ( | void | ) | [static] |
Definition at line 110 of file eagi-sphinx-test.c.
References AUDIO_FILENO, errno, FD_SET, FD_ZERO, and sphinx_sock.
Referenced by run_command().
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 }
int sphinx_sock = -1 [static] |
Definition at line 39 of file eagi-sphinx-test.c.
Referenced by connect_sphinx(), and wait_result().