Asterisk - The Open Source Telephony Project GIT-master-a358458
eagi-sphinx-test.c
Go to the documentation of this file.
1/*
2 * Extended AGI test application
3 *
4 * This code is released into public domain
5 * without any warranty of any kind.
6 *
7 */
8
9/*! \file
10 * Extended AGI test application
11 *
12 * This code is released into public domain
13 * without any warranty of any kind.
14 *
15 * \ingroup agi
16 */
17
18#include <stdio.h>
19#include <unistd.h>
20#include <stdlib.h>
21#include <errno.h>
22#include <string.h>
23#include <sys/select.h>
24#include <fcntl.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <netdb.h>
29
30#include "asterisk.h"
31
32#include "asterisk/compat.h"
33
34#define AUDIO_FILENO (STDERR_FILENO + 1)
35
36#define SPHINX_HOST "192.168.1.108"
37#define SPHINX_PORT 3460
38
39static int sphinx_sock = -1;
40
41static int connect_sphinx(void)
42{
43 struct hostent *hp;
44 struct sockaddr_in sin;
45 int res;
47 if (!hp) {
48 fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
49 return -1;
50 }
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));
54 return -1;
55 }
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));
62 close(sphinx_sock);
63 sphinx_sock = -1;
64 return -1;
65 }
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));
69 close(sphinx_sock);
70 sphinx_sock = -1;
71 return -1;
72 }
73 return 0;
74}
75
76static int read_environment(void)
77{
78 char buf[256];
79 char *val;
80 /* Read environment */
81 for(;;) {
82 if (!fgets(buf, sizeof(buf), stdin)) {
83 return -1;
84 }
85 if (feof(stdin))
86 return -1;
87 buf[strlen(buf) - 1] = '\0';
88 /* Check for end of environment */
89 if (!strlen(buf))
90 return 0;
91 val = strchr(buf, ':');
92 if (!val) {
93 fprintf(stderr, "Invalid environment: '%s'\n", buf);
94 return -1;
95 }
96 *val = '\0';
97 val++;
98 val++;
99 /* Skip space */
100 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
101
102 /* Load into normal environment */
103 setenv(buf, val, 1);
104
105 }
106 /* Never reached */
107 return 0;
108}
109
110static char *wait_result(void)
111{
112 fd_set fds;
113 int res;
114 int max;
115 static char astresp[256];
116 static char sphinxresp[256];
117 char audiobuf[4096];
118 for (;;) {
119 FD_ZERO(&fds);
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)
127 }
128 /* Wait for *some* sort of I/O */
129 res = select(max + 1, &fds, NULL, NULL, NULL);
130 if (res < 0) {
131 fprintf(stderr, "Error in select: %s\n", strerror(errno));
132 return NULL;
133 }
134 if (FD_ISSET(STDIN_FILENO, &fds)) {
135 if (!fgets(astresp, sizeof(astresp), stdin)) {
136 return NULL;
137 }
138 if (feof(stdin)) {
139 fprintf(stderr, "Got hungup on apparently\n");
140 return NULL;
141 }
142 astresp[strlen(astresp) - 1] = '\0';
143 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
144 return astresp;
145 }
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));
151 }
152 }
153 }
154 if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
155 res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
156 if (res > 0) {
157 fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
158 return sphinxresp;
159 } else if (res == 0) {
160 fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
161 close(sphinx_sock);
162 sphinx_sock = -1;
163 }
164 }
165 }
166
167}
168
169static char *run_command(char *command)
170{
171 fprintf(stdout, "%s\n", command);
172 return wait_result();
173}
174
175static int run_script(void)
176{
177 char *res;
178 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
179 if (!res) {
180 fprintf(stderr, "Failed to execute command\n");
181 return -1;
182 }
183 fprintf(stderr, "1. Result is '%s'\n", res);
184 res = run_command("STREAM FILE demo-nomatch 0123456789*#");
185 if (!res) {
186 fprintf(stderr, "Failed to execute command\n");
187 return -1;
188 }
189 fprintf(stderr, "2. Result is '%s'\n", res);
190 res = run_command("SAY NUMBER 23452345 0123456789*#");
191 if (!res) {
192 fprintf(stderr, "Failed to execute command\n");
193 return -1;
194 }
195 fprintf(stderr, "3. Result is '%s'\n", res);
196 res = run_command("GET DATA demo-enterkeywords");
197 if (!res) {
198 fprintf(stderr, "Failed to execute command\n");
199 return -1;
200 }
201 fprintf(stderr, "4. Result is '%s'\n", res);
202 res = run_command("STREAM FILE auth-thankyou \"\"");
203 if (!res) {
204 fprintf(stderr, "Failed to execute command\n");
205 return -1;
206 }
207 fprintf(stderr, "5. Result is '%s'\n", res);
208 return 0;
209}
210
211int main(int argc, char *argv[])
212{
213 char *tmp;
214 int ver = 0;
215 int subver = 0;
216 /* Setup stdin/stdout for line buffering */
217 setlinebuf(stdin);
218 setlinebuf(stdout);
219 if (read_environment()) {
220 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
221 exit(1);
222 }
224 tmp = getenv("agi_enhanced");
225 if (tmp) {
226 if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
227 ver = 0;
228 }
229 if (ver < 1) {
230 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
231 exit(1);
232 }
233 if (run_script())
234 return -1;
235 exit(0);
236}
Asterisk main include file. File version handling, generic pbx functions.
static int tmp()
Definition: bt_open.c:389
static int sphinx_sock
int main(int argc, char *argv[])
#define AUDIO_FILENO
static int run_script(void)
#define SPHINX_PORT
static int read_environment(void)
static int connect_sphinx(void)
static char * run_command(char *command)
#define SPHINX_HOST
static char * wait_result(void)
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define max(a, b)
Definition: f2c.h:198
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
int setenv(const char *name, const char *value, int overwrite)
#define gethostbyname
Definition: lock.h:639
int errno
#define NULL
Definition: resample.c:96
#define FD_SET(fd, fds)
Definition: select.h:58
#define FD_ZERO(a)
Definition: select.h:49
Definition: ast_expr2.c:325