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