2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2005 Junghanns.NET GmbH
7 * Klaus-Peter Junghanns <kpj@junghanns.net>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
20 #include <sys/select.h>
22 #include <solaris-compat/compat.h>
25 #define AUDIO_FILENO_IN (STDERR_FILENO + 1)
26 #define AUDIO_FILENO_OUT (STDERR_FILENO + 2)
28 static int read_environment(void)
32 /* Read environment */
34 fgets(buf
, sizeof(buf
), stdin
);
37 buf
[strlen(buf
) - 1] = '\0';
38 /* Check for end of environment */
41 val
= strchr(buf
, ':');
43 fprintf(stderr
, "Invalid environment: '%s'\n", buf
);
50 // fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
52 /* Load into normal environment */
60 static void app_echo(void)
65 static char astresp
[256];
66 char audiobuf
[16000]; /* 1 second of audio */
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
);
74 fprintf(stderr
, "Error in select: %s\n", strerror(errno
));
77 if (FD_ISSET(STDIN_FILENO
, &fds
)) {
78 fgets(astresp
, sizeof(astresp
), stdin
);
82 astresp
[strlen(astresp
) - 1] = '\0';
83 fprintf(stderr
, "Ooh, got a response from Asterisk: '%s'\n", astresp
);
86 if (FD_ISSET(AUDIO_FILENO_IN
, &fds
)) {
87 /* what goes in.... */
88 res
= read(AUDIO_FILENO_IN
, audiobuf
, sizeof(audiobuf
));
92 write(AUDIO_FILENO_OUT
, audiobuf
, bytes
);
98 static char *wait_result(void)
102 static char astresp
[256];
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
);
111 fprintf(stderr
, "Error in select: %s\n", strerror(errno
));
114 if (FD_ISSET(STDIN_FILENO
, &fds
)) {
115 fgets(astresp
, sizeof(astresp
), stdin
);
117 fprintf(stderr
, "Got hungup on apparently\n");
120 astresp
[strlen(astresp
) - 1] = '\0';
121 fprintf(stderr
, "Ooh, got a response from Asterisk: '%s'\n", 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)
142 res
= run_command("STREAM FILE demo-echotest \"\"");
144 fprintf(stderr
, "Failed to execute command\n");
151 int main(int argc
, char *argv
[])
156 /* Setup stdin/stdout for line buffering */
159 if (read_environment()) {
160 fprintf(stderr
, "Failed to read environment: %s\n", strerror(errno
));
163 tmp
= getenv("agi_enhanced");
165 if (sscanf(tmp
, "%d.%d", &ver
, &subver
) != 2)
169 fprintf(stderr
, "No XAGI services available. Use XAGI, not AGI or EAGI\n");