00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <unistd.h>
00020 #include <stdlib.h>
00021 #include <errno.h>
00022 #include <string.h>
00023 #include <sys/select.h>
00024 #include <fcntl.h>
00025 #include <sys/socket.h>
00026 #include <netinet/in.h>
00027 #include <arpa/inet.h>
00028 #include <netdb.h>
00029
00030 #include "asterisk.h"
00031
00032 #include "asterisk/compat.h"
00033
00034 #define AUDIO_FILENO (STDERR_FILENO + 1)
00035
00036 #define SPHINX_HOST "192.168.1.108"
00037 #define SPHINX_PORT 3460
00038
00039 static int sphinx_sock = -1;
00040
00041 static int connect_sphinx(void)
00042 {
00043 struct hostent *hp;
00044 struct sockaddr_in sin;
00045 int res;
00046 hp = gethostbyname(SPHINX_HOST);
00047 if (!hp) {
00048 fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
00049 return -1;
00050 }
00051 sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
00052 if (sphinx_sock < 0) {
00053 fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
00054 return -1;
00055 }
00056 memset(&sin, 0, sizeof(sin));
00057 sin.sin_family = AF_INET;
00058 sin.sin_port = htons(SPHINX_PORT);
00059 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
00060 if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
00061 fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
00062 close(sphinx_sock);
00063 sphinx_sock = -1;
00064 return -1;
00065 }
00066 res = fcntl(sphinx_sock, F_GETFL);
00067 if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
00068 fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
00069 close(sphinx_sock);
00070 sphinx_sock = -1;
00071 return -1;
00072 }
00073 return 0;
00074 }
00075
00076 static int read_environment(void)
00077 {
00078 char buf[256];
00079 char *val;
00080
00081 for(;;) {
00082 fgets(buf, sizeof(buf), stdin);
00083 if (feof(stdin))
00084 return -1;
00085 buf[strlen(buf) - 1] = '\0';
00086
00087 if (!strlen(buf))
00088 return 0;
00089 val = strchr(buf, ':');
00090 if (!val) {
00091 fprintf(stderr, "Invalid environment: '%s'\n", buf);
00092 return -1;
00093 }
00094 *val = '\0';
00095 val++;
00096 val++;
00097
00098 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
00099
00100
00101 setenv(buf, val, 1);
00102
00103 }
00104
00105 return 0;
00106 }
00107
00108 static char *wait_result(void)
00109 {
00110 fd_set fds;
00111 int res;
00112 int max;
00113 static char astresp[256];
00114 static char sphinxresp[256];
00115 char audiobuf[4096];
00116 for (;;) {
00117 FD_ZERO(&fds);
00118 FD_SET(STDIN_FILENO, &fds);
00119 FD_SET(AUDIO_FILENO, &fds);
00120 max = AUDIO_FILENO;
00121 if (sphinx_sock > -1) {
00122 FD_SET(sphinx_sock, &fds);
00123 if (sphinx_sock > max)
00124 max = sphinx_sock;
00125 }
00126
00127 res = select(max + 1, &fds, NULL, NULL, NULL);
00128 if (res < 0) {
00129 fprintf(stderr, "Error in select: %s\n", strerror(errno));
00130 return NULL;
00131 }
00132 if (FD_ISSET(STDIN_FILENO, &fds)) {
00133 fgets(astresp, sizeof(astresp), stdin);
00134 if (feof(stdin)) {
00135 fprintf(stderr, "Got hungup on apparently\n");
00136 return NULL;
00137 }
00138 astresp[strlen(astresp) - 1] = '\0';
00139 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
00140 return astresp;
00141 }
00142 if (FD_ISSET(AUDIO_FILENO, &fds)) {
00143 res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
00144 if (res > 0) {
00145 if (sphinx_sock > -1)
00146 write(sphinx_sock, audiobuf, res);
00147 }
00148 }
00149 if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
00150 res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
00151 if (res > 0) {
00152 fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
00153 return sphinxresp;
00154 } else if (res == 0) {
00155 fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
00156 close(sphinx_sock);
00157 sphinx_sock = -1;
00158 }
00159 }
00160 }
00161
00162 }
00163
00164 static char *run_command(char *command)
00165 {
00166 fprintf(stdout, "%s\n", command);
00167 return wait_result();
00168 }
00169
00170 static int run_script(void)
00171 {
00172 char *res;
00173 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
00174 if (!res) {
00175 fprintf(stderr, "Failed to execute command\n");
00176 return -1;
00177 }
00178 fprintf(stderr, "1. Result is '%s'\n", res);
00179 res = run_command("STREAM FILE demo-nomatch 0123456789*#");
00180 if (!res) {
00181 fprintf(stderr, "Failed to execute command\n");
00182 return -1;
00183 }
00184 fprintf(stderr, "2. Result is '%s'\n", res);
00185 res = run_command("SAY NUMBER 23452345 0123456789*#");
00186 if (!res) {
00187 fprintf(stderr, "Failed to execute command\n");
00188 return -1;
00189 }
00190 fprintf(stderr, "3. Result is '%s'\n", res);
00191 res = run_command("GET DATA demo-enterkeywords");
00192 if (!res) {
00193 fprintf(stderr, "Failed to execute command\n");
00194 return -1;
00195 }
00196 fprintf(stderr, "4. Result is '%s'\n", res);
00197 res = run_command("STREAM FILE auth-thankyou \"\"");
00198 if (!res) {
00199 fprintf(stderr, "Failed to execute command\n");
00200 return -1;
00201 }
00202 fprintf(stderr, "5. Result is '%s'\n", res);
00203 return 0;
00204 }
00205
00206 int main(int argc, char *argv[])
00207 {
00208 char *tmp;
00209 int ver = 0;
00210 int subver = 0;
00211
00212 setlinebuf(stdin);
00213 setlinebuf(stdout);
00214 if (read_environment()) {
00215 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
00216 exit(1);
00217 }
00218 connect_sphinx();
00219 tmp = getenv("agi_enhanced");
00220 if (tmp) {
00221 if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
00222 ver = 0;
00223 }
00224 if (ver < 1) {
00225 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
00226 exit(1);
00227 }
00228 if (run_script())
00229 return -1;
00230 exit(0);
00231 }