#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include "asterisk.h"
#include "asterisk/compat.h"
Go to the source code of this file.
Defines | |
#define | AUDIO_FILENO (STDERR_FILENO + 1) |
Functions | |
int | main (int argc, char *argv[]) |
static int | read_environment (void) |
static char * | run_command (char *command) |
static int | run_script (void) |
static char * | wait_result (void) |
#define AUDIO_FILENO (STDERR_FILENO + 1) |
Definition at line 19 of file eagi-test.c.
int main | ( | int | argc, | |
char * | argv[] | |||
) |
Definition at line 145 of file eagi-test.c.
References errno, read_environment(), and run_script().
00146 { 00147 char *tmp; 00148 int ver = 0; 00149 int subver = 0; 00150 /* Setup stdin/stdout for line buffering */ 00151 setlinebuf(stdin); 00152 setlinebuf(stdout); 00153 if (read_environment()) { 00154 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno)); 00155 exit(1); 00156 } 00157 tmp = getenv("agi_enhanced"); 00158 if (tmp) { 00159 if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2) 00160 ver = 0; 00161 } 00162 if (ver < 1) { 00163 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n"); 00164 exit(1); 00165 } 00166 if (run_script()) 00167 return -1; 00168 exit(0); 00169 }
static int read_environment | ( | void | ) | [static] |
Definition at line 21 of file eagi-test.c.
00022 { 00023 char buf[256]; 00024 char *val; 00025 /* Read environment */ 00026 for(;;) { 00027 if (!fgets(buf, sizeof(buf), stdin)) { 00028 return -1; 00029 } 00030 if (feof(stdin)) 00031 return -1; 00032 buf[strlen(buf) - 1] = '\0'; 00033 /* Check for end of environment */ 00034 if (!strlen(buf)) 00035 return 0; 00036 val = strchr(buf, ':'); 00037 if (!val) { 00038 fprintf(stderr, "Invalid environment: '%s'\n", buf); 00039 return -1; 00040 } 00041 *val = '\0'; 00042 val++; 00043 val++; 00044 /* Skip space */ 00045 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val); 00046 00047 /* Load into normal environment */ 00048 setenv(buf, val, 1); 00049 00050 } 00051 /* Never reached */ 00052 return 0; 00053 }
static char* run_command | ( | char * | command | ) | [static] |
Definition at line 103 of file eagi-test.c.
References wait_result().
00104 { 00105 fprintf(stdout, "%s\n", command); 00106 return wait_result(); 00107 }
static int run_script | ( | void | ) | [static] |
Definition at line 109 of file eagi-test.c.
References run_command().
00110 { 00111 char *res; 00112 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#"); 00113 if (!res) { 00114 fprintf(stderr, "Failed to execute command\n"); 00115 return -1; 00116 } 00117 fprintf(stderr, "1. Result is '%s'\n", res); 00118 res = run_command("STREAM FILE demo-nomatch 0123456789*#"); 00119 if (!res) { 00120 fprintf(stderr, "Failed to execute command\n"); 00121 return -1; 00122 } 00123 fprintf(stderr, "2. Result is '%s'\n", res); 00124 res = run_command("SAY NUMBER 23452345 0123456789*#"); 00125 if (!res) { 00126 fprintf(stderr, "Failed to execute command\n"); 00127 return -1; 00128 } 00129 fprintf(stderr, "3. Result is '%s'\n", res); 00130 res = run_command("GET DATA demo-enterkeywords"); 00131 if (!res) { 00132 fprintf(stderr, "Failed to execute command\n"); 00133 return -1; 00134 } 00135 fprintf(stderr, "4. Result is '%s'\n", res); 00136 res = run_command("STREAM FILE auth-thankyou \"\""); 00137 if (!res) { 00138 fprintf(stderr, "Failed to execute command\n"); 00139 return -1; 00140 } 00141 fprintf(stderr, "5. Result is '%s'\n", res); 00142 return 0; 00143 }
static char* wait_result | ( | void | ) | [static] |
Definition at line 55 of file eagi-test.c.
References AUDIO_FILENO, errno, FD_SET, and FD_ZERO.
00056 { 00057 fd_set fds; 00058 int res; 00059 int bytes = 0; 00060 static char astresp[256]; 00061 char audiobuf[4096]; 00062 for (;;) { 00063 FD_ZERO(&fds); 00064 FD_SET(STDIN_FILENO, &fds); 00065 FD_SET(AUDIO_FILENO, &fds); 00066 /* Wait for *some* sort of I/O */ 00067 res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL); 00068 if (res < 0) { 00069 fprintf(stderr, "Error in select: %s\n", strerror(errno)); 00070 return NULL; 00071 } 00072 if (FD_ISSET(STDIN_FILENO, &fds)) { 00073 if (!fgets(astresp, sizeof(astresp), stdin)) { 00074 return NULL; 00075 } 00076 if (feof(stdin)) { 00077 fprintf(stderr, "Got hungup on apparently\n"); 00078 return NULL; 00079 } 00080 astresp[strlen(astresp) - 1] = '\0'; 00081 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp); 00082 return astresp; 00083 } 00084 if (FD_ISSET(AUDIO_FILENO, &fds)) { 00085 res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf)); 00086 if (res > 0) { 00087 /* XXX Process the audio with sphinx here XXX */ 00088 #if 0 00089 fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes); 00090 #endif 00091 bytes += res; 00092 /* Prentend we detected some audio after 3 seconds */ 00093 if (bytes > 16000 * 3) { 00094 return "Sample Message"; 00095 bytes = 0; 00096 } 00097 } 00098 } 00099 } 00100 00101 }