Let's also include aclocal.m4
[asterisk-bristuff.git] / agi / eagi-sphinx-test.c
blob0ad12c787f86dbf99c7ec2515db4a77bb604ca4f
1 /*
2 * Extended AGI test application
4 * This code is released into public domain
5 * without any warranty of any kind.
7 */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <sys/select.h>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
21 #include "asterisk.h"
23 #include "asterisk/compat.h"
25 #define AUDIO_FILENO (STDERR_FILENO + 1)
27 #define SPHINX_HOST "192.168.1.108"
28 #define SPHINX_PORT 3460
30 static int sphinx_sock = -1;
32 static int connect_sphinx(void)
34 struct hostent *hp;
35 struct sockaddr_in sin;
36 int res;
37 hp = gethostbyname(SPHINX_HOST);
38 if (!hp) {
39 fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
40 return -1;
42 sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
43 if (sphinx_sock < 0) {
44 fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
45 return -1;
47 memset(&sin, 0, sizeof(sin));
48 sin.sin_family = AF_INET;
49 sin.sin_port = htons(SPHINX_PORT);
50 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
51 if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
52 fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
53 close(sphinx_sock);
54 sphinx_sock = -1;
55 return -1;
57 res = fcntl(sphinx_sock, F_GETFL);
58 if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
59 fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
60 close(sphinx_sock);
61 sphinx_sock = -1;
62 return -1;
64 return 0;
67 static int read_environment(void)
69 char buf[256];
70 char *val;
71 /* Read environment */
72 for(;;) {
73 fgets(buf, sizeof(buf), stdin);
74 if (feof(stdin))
75 return -1;
76 buf[strlen(buf) - 1] = '\0';
77 /* Check for end of environment */
78 if (!strlen(buf))
79 return 0;
80 val = strchr(buf, ':');
81 if (!val) {
82 fprintf(stderr, "Invalid environment: '%s'\n", buf);
83 return -1;
85 *val = '\0';
86 val++;
87 val++;
88 /* Skip space */
89 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
91 /* Load into normal environment */
92 setenv(buf, val, 1);
95 /* Never reached */
96 return 0;
99 static char *wait_result(void)
101 fd_set fds;
102 int res;
103 int max;
104 static char astresp[256];
105 static char sphinxresp[256];
106 char audiobuf[4096];
107 for (;;) {
108 FD_ZERO(&fds);
109 FD_SET(STDIN_FILENO, &fds);
110 FD_SET(AUDIO_FILENO, &fds);
111 max = AUDIO_FILENO;
112 if (sphinx_sock > -1) {
113 FD_SET(sphinx_sock, &fds);
114 if (sphinx_sock > max)
115 max = sphinx_sock;
117 /* Wait for *some* sort of I/O */
118 res = select(max + 1, &fds, NULL, NULL, NULL);
119 if (res < 0) {
120 fprintf(stderr, "Error in select: %s\n", strerror(errno));
121 return NULL;
123 if (FD_ISSET(STDIN_FILENO, &fds)) {
124 fgets(astresp, sizeof(astresp), stdin);
125 if (feof(stdin)) {
126 fprintf(stderr, "Got hungup on apparently\n");
127 return NULL;
129 astresp[strlen(astresp) - 1] = '\0';
130 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
131 return astresp;
133 if (FD_ISSET(AUDIO_FILENO, &fds)) {
134 res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
135 if (res > 0) {
136 if (sphinx_sock > -1)
137 write(sphinx_sock, audiobuf, res);
140 if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
141 res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
142 if (res > 0) {
143 fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
144 return sphinxresp;
145 } else if (res == 0) {
146 fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
147 close(sphinx_sock);
148 sphinx_sock = -1;
155 static char *run_command(char *command)
157 fprintf(stdout, "%s\n", command);
158 return wait_result();
161 static int run_script(void)
163 char *res;
164 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
165 if (!res) {
166 fprintf(stderr, "Failed to execute command\n");
167 return -1;
169 fprintf(stderr, "1. Result is '%s'\n", res);
170 res = run_command("STREAM FILE demo-nomatch 0123456789*#");
171 if (!res) {
172 fprintf(stderr, "Failed to execute command\n");
173 return -1;
175 fprintf(stderr, "2. Result is '%s'\n", res);
176 res = run_command("SAY NUMBER 23452345 0123456789*#");
177 if (!res) {
178 fprintf(stderr, "Failed to execute command\n");
179 return -1;
181 fprintf(stderr, "3. Result is '%s'\n", res);
182 res = run_command("GET DATA demo-enterkeywords");
183 if (!res) {
184 fprintf(stderr, "Failed to execute command\n");
185 return -1;
187 fprintf(stderr, "4. Result is '%s'\n", res);
188 res = run_command("STREAM FILE auth-thankyou \"\"");
189 if (!res) {
190 fprintf(stderr, "Failed to execute command\n");
191 return -1;
193 fprintf(stderr, "5. Result is '%s'\n", res);
194 return 0;
197 int main(int argc, char *argv[])
199 char *tmp;
200 int ver = 0;
201 int subver = 0;
202 /* Setup stdin/stdout for line buffering */
203 setlinebuf(stdin);
204 setlinebuf(stdout);
205 if (read_environment()) {
206 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
207 exit(1);
209 connect_sphinx();
210 tmp = getenv("agi_enhanced");
211 if (tmp) {
212 if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
213 ver = 0;
215 if (ver < 1) {
216 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
217 exit(1);
219 if (run_script())
220 return -1;
221 exit(0);