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