move tools used during build into build_tools subdirectory
[asterisk/asterisk.git] / agi / eagi-test.c
1 /*
2  * Extended AGI test application
3  *
4  * This code is released into the public domain
5  * with no warranty of any kind
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <sys/select.h>
14 #ifdef SOLARIS
15 #include <solaris-compat/compat.h>
16 #endif
17
18 #define AUDIO_FILENO (STDERR_FILENO + 1)
19
20 static int read_environment(void)
21 {
22         char buf[256];
23         char *val;
24         /* Read environment */
25         for(;;) {
26                 fgets(buf, sizeof(buf), stdin);
27                 if (feof(stdin))
28                         return -1;
29                 buf[strlen(buf) - 1] = '\0';
30                 /* Check for end of environment */
31                 if (!strlen(buf))
32                         return 0;
33                 val = strchr(buf, ':');
34                 if (!val) {
35                         fprintf(stderr, "Invalid environment: '%s'\n", buf);
36                         return -1;
37                 }
38                 *val = '\0';
39                 val++;
40                 val++;
41                 /* Skip space */
42                 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
43
44                 /* Load into normal environment */
45                 setenv(buf, val, 1);
46                 
47         }
48         /* Never reached */
49         return 0;
50 }
51
52 static char *wait_result(void)
53 {
54         fd_set fds;
55         int res;
56         int bytes = 0;
57         static char astresp[256];
58         char audiobuf[4096];
59         for (;;) {
60                 FD_ZERO(&fds);
61                 FD_SET(STDIN_FILENO, &fds);
62                 FD_SET(AUDIO_FILENO, &fds);
63                 /* Wait for *some* sort of I/O */
64                 res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL);
65                 if (res < 0) {
66                         fprintf(stderr, "Error in select: %s\n", strerror(errno));
67                         return NULL;
68                 }
69                 if (FD_ISSET(STDIN_FILENO, &fds)) {
70                         fgets(astresp, sizeof(astresp), stdin);
71                         if (feof(stdin)) {
72                                 fprintf(stderr, "Got hungup on apparently\n");
73                                 return NULL;
74                         }
75                         astresp[strlen(astresp) - 1] = '\0';
76                         fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
77                         return astresp;
78                 }
79                 if (FD_ISSET(AUDIO_FILENO, &fds)) {
80                         res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
81                         if (res > 0) {
82                                 /* XXX Process the audio with sphinx here XXX */
83 #if 0
84                                 fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes);
85 #endif
86                                 bytes += res;
87                                 /* Prentend we detected some audio after 3 seconds */
88                                 if (bytes > 16000 * 3) {
89                                         return "Sample Message";
90                                         bytes = 0;
91                                 }
92                         }
93                 }
94         }
95                 
96 }
97
98 static char *run_command(char *command)
99 {
100         fprintf(stdout, "%s\n", command);
101         return wait_result();
102 }
103
104 static int run_script(void)
105 {
106         char *res;
107         res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
108         if (!res) {
109                 fprintf(stderr, "Failed to execute command\n");
110                 return -1;
111         }
112         fprintf(stderr, "1. Result is '%s'\n", res);
113         res = run_command("STREAM FILE demo-nomatch 0123456789*#");
114         if (!res) {
115                 fprintf(stderr, "Failed to execute command\n");
116                 return -1;
117         }
118         fprintf(stderr, "2. Result is '%s'\n", res);
119         res = run_command("SAY NUMBER 23452345 0123456789*#");
120         if (!res) {
121                 fprintf(stderr, "Failed to execute command\n");
122                 return -1;
123         }
124         fprintf(stderr, "3. Result is '%s'\n", res);
125         res = run_command("GET DATA demo-enterkeywords");
126         if (!res) {
127                 fprintf(stderr, "Failed to execute command\n");
128                 return -1;
129         }
130         fprintf(stderr, "4. Result is '%s'\n", res);
131         res = run_command("STREAM FILE auth-thankyou \"\"");
132         if (!res) {
133                 fprintf(stderr, "Failed to execute command\n");
134                 return -1;
135         }
136         fprintf(stderr, "5. Result is '%s'\n", res);
137         return 0;
138 }
139
140 int main(int argc, char *argv[])
141 {
142         char *tmp;
143         int ver = 0;
144         int subver = 0;
145         /* Setup stdin/stdout for line buffering */
146         setlinebuf(stdin);
147         setlinebuf(stdout);
148         if (read_environment()) {
149                 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
150                 exit(1);
151         }
152         tmp = getenv("agi_enhanced");
153         if (tmp) {
154                 if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
155                         ver = 0;
156         }
157         if (ver < 1) {
158                 fprintf(stderr, "No enhanced AGI services available.  Use EAGI, not AGI\n");
159                 exit(1);
160         }
161         if (run_script())
162                 return -1;
163         exit(0);
164 }