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