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