The XAGI feature.
[asterisk-bristuff.git] / agi / xagi-test.c
blob39e28cba768675eef32f6432691a107cc7878da0
1 /*
2 * Asterisk -- A telephony toolkit for Linux.
4 * XAGI sample script
6 * Copyright (C) 2005 Junghanns.NET GmbH
7 * Klaus-Peter Junghanns <kpj@junghanns.net>
9 * based on eagi-test.c
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <sys/select.h>
21 #ifdef SOLARIS
22 #include <solaris-compat/compat.h>
23 #endif
25 #define AUDIO_FILENO_IN (STDERR_FILENO + 1)
26 #define AUDIO_FILENO_OUT (STDERR_FILENO + 2)
28 static int read_environment(void)
30 char buf[256];
31 char *val;
32 /* Read environment */
33 for(;;) {
34 fgets(buf, sizeof(buf), stdin);
35 if (feof(stdin))
36 return -1;
37 buf[strlen(buf) - 1] = '\0';
38 /* Check for end of environment */
39 if (!strlen(buf))
40 return 0;
41 val = strchr(buf, ':');
42 if (!val) {
43 fprintf(stderr, "Invalid environment: '%s'\n", buf);
44 return -1;
46 *val = '\0';
47 val++;
48 val++;
49 /* Skip space */
50 // fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
52 /* Load into normal environment */
53 setenv(buf, val, 1);
56 /* Never reached */
57 return 0;
60 static void app_echo(void)
62 fd_set fds;
63 int res;
64 int bytes = 0;
65 static char astresp[256];
66 char audiobuf[16000]; /* 1 second of audio */
67 for (;;) {
68 FD_ZERO(&fds);
69 FD_SET(STDIN_FILENO, &fds);
70 FD_SET(AUDIO_FILENO_IN, &fds);
71 /* Wait for *some* sort of I/O */
72 res = select(AUDIO_FILENO_IN + 1, &fds, NULL, NULL, NULL);
73 if (res < 0) {
74 fprintf(stderr, "Error in select: %s\n", strerror(errno));
75 return;
77 if (FD_ISSET(STDIN_FILENO, &fds)) {
78 fgets(astresp, sizeof(astresp), stdin);
79 if (feof(stdin)) {
80 return;
82 astresp[strlen(astresp) - 1] = '\0';
83 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
84 return;
86 if (FD_ISSET(AUDIO_FILENO_IN, &fds)) {
87 /* what goes in.... */
88 res = read(AUDIO_FILENO_IN, audiobuf, sizeof(audiobuf));
89 if (res > 0) {
90 bytes = res;
91 /* must come out */
92 write(AUDIO_FILENO_OUT, audiobuf, bytes);
98 static char *wait_result(void)
100 fd_set fds;
101 int res;
102 static char astresp[256];
103 char audiobuf[4096];
104 for (;;) {
105 FD_ZERO(&fds);
106 FD_SET(STDIN_FILENO, &fds);
107 FD_SET(AUDIO_FILENO_IN, &fds);
108 /* Wait for *some* sort of I/O */
109 res = select(AUDIO_FILENO_IN + 1, &fds, NULL, NULL, NULL);
110 if (res < 0) {
111 fprintf(stderr, "Error in select: %s\n", strerror(errno));
112 return NULL;
114 if (FD_ISSET(STDIN_FILENO, &fds)) {
115 fgets(astresp, sizeof(astresp), stdin);
116 if (feof(stdin)) {
117 fprintf(stderr, "Got hungup on apparently\n");
118 return NULL;
120 astresp[strlen(astresp) - 1] = '\0';
121 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
122 return astresp;
124 if (FD_ISSET(AUDIO_FILENO_IN, &fds)) {
125 res = read(AUDIO_FILENO_IN, audiobuf, sizeof(audiobuf));
126 /* drop it, like it's hot */
132 static char *run_command(char *command)
134 fprintf(stdout, "%s\n", command);
135 return wait_result();
139 static int run_script(void)
141 char *res;
142 res = run_command("STREAM FILE demo-echotest \"\"");
143 if (!res) {
144 fprintf(stderr, "Failed to execute command\n");
145 return -1;
147 app_echo();
148 return 0;
151 int main(int argc, char *argv[])
153 char *tmp;
154 int ver = 0;
155 int subver = 0;
156 /* Setup stdin/stdout for line buffering */
157 setlinebuf(stdin);
158 setlinebuf(stdout);
159 if (read_environment()) {
160 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
161 exit(1);
163 tmp = getenv("agi_enhanced");
164 if (tmp) {
165 if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
166 ver = 0;
168 if (ver < 2) {
169 fprintf(stderr, "No XAGI services available. Use XAGI, not AGI or EAGI\n");
170 exit(1);
172 if (run_script())
173 return -1;
174 exit(0);