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