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