2 * Extended AGI test application
4 * This code is released into public domain
5 * without any warranty of any kind.
14 #include <sys/select.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.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)
35 struct sockaddr_in sin
;
37 hp
= gethostbyname(SPHINX_HOST
);
39 fprintf(stderr
, "Unable to resolve '%s'\n", SPHINX_HOST
);
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
));
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
));
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
));
67 static int read_environment(void)
71 /* Read environment */
73 fgets(buf
, sizeof(buf
), stdin
);
76 buf
[strlen(buf
) - 1] = '\0';
77 /* Check for end of environment */
80 val
= strchr(buf
, ':');
82 fprintf(stderr
, "Invalid environment: '%s'\n", buf
);
89 fprintf(stderr
, "Environment: '%s' is '%s'\n", buf
, val
);
91 /* Load into normal environment */
99 static char *wait_result(void)
104 static char astresp
[256];
105 static char sphinxresp
[256];
109 FD_SET(STDIN_FILENO
, &fds
);
110 FD_SET(AUDIO_FILENO
, &fds
);
112 if (sphinx_sock
> -1) {
113 FD_SET(sphinx_sock
, &fds
);
114 if (sphinx_sock
> max
)
117 /* Wait for *some* sort of I/O */
118 res
= select(max
+ 1, &fds
, NULL
, NULL
, NULL
);
120 fprintf(stderr
, "Error in select: %s\n", strerror(errno
));
123 if (FD_ISSET(STDIN_FILENO
, &fds
)) {
124 fgets(astresp
, sizeof(astresp
), stdin
);
126 fprintf(stderr
, "Got hungup on apparently\n");
129 astresp
[strlen(astresp
) - 1] = '\0';
130 fprintf(stderr
, "Ooh, got a response from Asterisk: '%s'\n", astresp
);
133 if (FD_ISSET(AUDIO_FILENO
, &fds
)) {
134 res
= read(AUDIO_FILENO
, audiobuf
, sizeof(audiobuf
));
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
));
143 fprintf(stderr
, "Oooh, Sphinx found a token: '%s'\n", sphinxresp
);
145 } else if (res
== 0) {
146 fprintf(stderr
, "Hrm, lost sphinx, guess we're on our own\n");
155 static char *run_command(char *command
)
157 fprintf(stdout
, "%s\n", command
);
158 return wait_result();
161 static int run_script(void)
164 res
= run_command("STREAM FILE demo-enterkeywords 0123456789*#");
166 fprintf(stderr
, "Failed to execute command\n");
169 fprintf(stderr
, "1. Result is '%s'\n", res
);
170 res
= run_command("STREAM FILE demo-nomatch 0123456789*#");
172 fprintf(stderr
, "Failed to execute command\n");
175 fprintf(stderr
, "2. Result is '%s'\n", res
);
176 res
= run_command("SAY NUMBER 23452345 0123456789*#");
178 fprintf(stderr
, "Failed to execute command\n");
181 fprintf(stderr
, "3. Result is '%s'\n", res
);
182 res
= run_command("GET DATA demo-enterkeywords");
184 fprintf(stderr
, "Failed to execute command\n");
187 fprintf(stderr
, "4. Result is '%s'\n", res
);
188 res
= run_command("STREAM FILE auth-thankyou \"\"");
190 fprintf(stderr
, "Failed to execute command\n");
193 fprintf(stderr
, "5. Result is '%s'\n", res
);
197 int main(int argc
, char *argv
[])
202 /* Setup stdin/stdout for line buffering */
205 if (read_environment()) {
206 fprintf(stderr
, "Failed to read environment: %s\n", strerror(errno
));
210 tmp
= getenv("agi_enhanced");
212 if (sscanf(tmp
, "%d.%d", &ver
, &subver
) != 2)
216 fprintf(stderr
, "No enhanced AGI services available. Use EAGI, not AGI\n");