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