23 #include <sys/select.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
34 #define AUDIO_FILENO (STDERR_FILENO + 1)
36 #define SPHINX_HOST "192.168.1.108"
37 #define SPHINX_PORT 3460
39 static int sphinx_sock = -1;
41 static int connect_sphinx(
void)
44 struct sockaddr_in sin;
46 hp = gethostbyname(SPHINX_HOST);
48 fprintf(stderr,
"Unable to resolve '%s'\n", SPHINX_HOST);
51 sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
52 if (sphinx_sock < 0) {
53 fprintf(stderr,
"Unable to allocate socket: %s\n", strerror(errno));
56 memset(&sin, 0,
sizeof(sin));
57 sin.sin_family = AF_INET;
58 sin.sin_port = htons(SPHINX_PORT);
59 memcpy(&sin.sin_addr, hp->h_addr,
sizeof(sin.sin_addr));
60 if (connect(sphinx_sock, (
struct sockaddr *)&sin,
sizeof(sin))) {
61 fprintf(stderr,
"Unable to connect on socket: %s\n", strerror(errno));
66 res = fcntl(sphinx_sock, F_GETFL);
67 if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
68 fprintf(stderr,
"Unable to set flags on socket: %s\n", strerror(errno));
76 static int read_environment(
void)
82 if (!fgets(buf,
sizeof(buf), stdin)) {
87 buf[strlen(buf) - 1] =
'\0';
91 val = strchr(buf,
':');
93 fprintf(stderr,
"Invalid environment: '%s'\n", buf);
100 fprintf(stderr,
"Environment: '%s' is '%s'\n", buf, val);
110 static char *wait_result(
void)
115 static char astresp[256];
116 static char sphinxresp[256];
120 FD_SET(STDIN_FILENO, &fds);
121 FD_SET(AUDIO_FILENO, &fds);
123 if (sphinx_sock > -1) {
124 FD_SET(sphinx_sock, &fds);
125 if (sphinx_sock > max)
129 res = select(max + 1, &fds, NULL, NULL, NULL);
131 fprintf(stderr,
"Error in select: %s\n", strerror(errno));
134 if (FD_ISSET(STDIN_FILENO, &fds)) {
135 if (!fgets(astresp,
sizeof(astresp), stdin)) {
139 fprintf(stderr,
"Got hungup on apparently\n");
142 astresp[strlen(astresp) - 1] =
'\0';
143 fprintf(stderr,
"Ooh, got a response from Asterisk: '%s'\n", astresp);
146 if (FD_ISSET(AUDIO_FILENO, &fds)) {
147 res = read(AUDIO_FILENO, audiobuf,
sizeof(audiobuf));
148 if ((res > 0) && (sphinx_sock > -1)) {
149 if (write(sphinx_sock, audiobuf, res) < 0) {
150 fprintf(stderr,
"write() failed: %s\n", strerror(errno));
154 if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
155 res = read(sphinx_sock, sphinxresp,
sizeof(sphinxresp));
157 fprintf(stderr,
"Oooh, Sphinx found a token: '%s'\n", sphinxresp);
159 }
else if (res == 0) {
160 fprintf(stderr,
"Hrm, lost sphinx, guess we're on our own\n");
169 static char *run_command(
char *command)
171 fprintf(stdout,
"%s\n", command);
172 return wait_result();
175 static int run_script(
void)
178 res = run_command(
"STREAM FILE demo-enterkeywords 0123456789*#");
180 fprintf(stderr,
"Failed to execute command\n");
183 fprintf(stderr,
"1. Result is '%s'\n", res);
184 res = run_command(
"STREAM FILE demo-nomatch 0123456789*#");
186 fprintf(stderr,
"Failed to execute command\n");
189 fprintf(stderr,
"2. Result is '%s'\n", res);
190 res = run_command(
"SAY NUMBER 23452345 0123456789*#");
192 fprintf(stderr,
"Failed to execute command\n");
195 fprintf(stderr,
"3. Result is '%s'\n", res);
196 res = run_command(
"GET DATA demo-enterkeywords");
198 fprintf(stderr,
"Failed to execute command\n");
201 fprintf(stderr,
"4. Result is '%s'\n", res);
202 res = run_command(
"STREAM FILE auth-thankyou \"\"");
204 fprintf(stderr,
"Failed to execute command\n");
207 fprintf(stderr,
"5. Result is '%s'\n", res);
211 int main(
int argc,
char *argv[])
219 if (read_environment()) {
220 fprintf(stderr,
"Failed to read environment: %s\n", strerror(errno));
224 tmp = getenv(
"agi_enhanced");
226 if (sscanf(tmp,
"%30d.%30d", &ver, &subver) != 2)
230 fprintf(stderr,
"No enhanced AGI services available. Use EAGI, not AGI\n");
Asterisk main include file. File version handling, generic pbx functions.
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
int main(int argc, char *argv[])