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