eagi-sphinx-test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Extended AGI test application
  3. *
  4. * This code is released into public domain
  5. * without any warranty of any kind.
  6. *
  7. */
  8. /*! \file
  9. * Extended AGI test application
  10. *
  11. * This code is released into public domain
  12. * without any warranty of any kind.
  13. *
  14. * \ingroup agi
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <sys/select.h>
  22. #include <fcntl.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netdb.h>
  27. #include "asterisk.h"
  28. #include "asterisk/compat.h"
  29. #define AUDIO_FILENO (STDERR_FILENO + 1)
  30. #define SPHINX_HOST "192.168.1.108"
  31. #define SPHINX_PORT 3460
  32. static int sphinx_sock = -1;
  33. static int connect_sphinx(void)
  34. {
  35. struct hostent *hp;
  36. struct sockaddr_in sin;
  37. int res;
  38. hp = gethostbyname(SPHINX_HOST);
  39. if (!hp) {
  40. fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
  41. return -1;
  42. }
  43. sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
  44. if (sphinx_sock < 0) {
  45. fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
  46. return -1;
  47. }
  48. memset(&sin, 0, sizeof(sin));
  49. sin.sin_family = AF_INET;
  50. sin.sin_port = htons(SPHINX_PORT);
  51. memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
  52. if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
  53. fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
  54. close(sphinx_sock);
  55. sphinx_sock = -1;
  56. return -1;
  57. }
  58. res = fcntl(sphinx_sock, F_GETFL);
  59. if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
  60. fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
  61. close(sphinx_sock);
  62. sphinx_sock = -1;
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. static int read_environment(void)
  68. {
  69. char buf[256];
  70. char *val;
  71. /* Read environment */
  72. for(;;) {
  73. if (!fgets(buf, sizeof(buf), stdin)) {
  74. return -1;
  75. }
  76. if (feof(stdin))
  77. return -1;
  78. buf[strlen(buf) - 1] = '\0';
  79. /* Check for end of environment */
  80. if (!strlen(buf))
  81. return 0;
  82. val = strchr(buf, ':');
  83. if (!val) {
  84. fprintf(stderr, "Invalid environment: '%s'\n", buf);
  85. return -1;
  86. }
  87. *val = '\0';
  88. val++;
  89. val++;
  90. /* Skip space */
  91. fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
  92. /* Load into normal environment */
  93. setenv(buf, val, 1);
  94. }
  95. /* Never reached */
  96. return 0;
  97. }
  98. static char *wait_result(void)
  99. {
  100. fd_set fds;
  101. int res;
  102. int max;
  103. static char astresp[256];
  104. static char sphinxresp[256];
  105. char audiobuf[4096];
  106. for (;;) {
  107. FD_ZERO(&fds);
  108. FD_SET(STDIN_FILENO, &fds);
  109. FD_SET(AUDIO_FILENO, &fds);
  110. max = AUDIO_FILENO;
  111. if (sphinx_sock > -1) {
  112. FD_SET(sphinx_sock, &fds);
  113. if (sphinx_sock > max)
  114. max = sphinx_sock;
  115. }
  116. /* Wait for *some* sort of I/O */
  117. res = select(max + 1, &fds, NULL, NULL, NULL);
  118. if (res < 0) {
  119. fprintf(stderr, "Error in select: %s\n", strerror(errno));
  120. return NULL;
  121. }
  122. if (FD_ISSET(STDIN_FILENO, &fds)) {
  123. if (!fgets(astresp, sizeof(astresp), stdin)) {
  124. return NULL;
  125. }
  126. if (feof(stdin)) {
  127. fprintf(stderr, "Got hungup on apparently\n");
  128. return NULL;
  129. }
  130. astresp[strlen(astresp) - 1] = '\0';
  131. fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
  132. return astresp;
  133. }
  134. if (FD_ISSET(AUDIO_FILENO, &fds)) {
  135. res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
  136. if ((res > 0) && (sphinx_sock > -1)) {
  137. if (write(sphinx_sock, audiobuf, res) < 0) {
  138. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  139. }
  140. }
  141. }
  142. if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
  143. res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
  144. if (res > 0) {
  145. fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
  146. return sphinxresp;
  147. } else if (res == 0) {
  148. fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
  149. close(sphinx_sock);
  150. sphinx_sock = -1;
  151. }
  152. }
  153. }
  154. }
  155. static char *run_command(char *command)
  156. {
  157. fprintf(stdout, "%s\n", command);
  158. return wait_result();
  159. }
  160. static int run_script(void)
  161. {
  162. char *res;
  163. res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
  164. if (!res) {
  165. fprintf(stderr, "Failed to execute command\n");
  166. return -1;
  167. }
  168. fprintf(stderr, "1. Result is '%s'\n", res);
  169. res = run_command("STREAM FILE demo-nomatch 0123456789*#");
  170. if (!res) {
  171. fprintf(stderr, "Failed to execute command\n");
  172. return -1;
  173. }
  174. fprintf(stderr, "2. Result is '%s'\n", res);
  175. res = run_command("SAY NUMBER 23452345 0123456789*#");
  176. if (!res) {
  177. fprintf(stderr, "Failed to execute command\n");
  178. return -1;
  179. }
  180. fprintf(stderr, "3. Result is '%s'\n", res);
  181. res = run_command("GET DATA demo-enterkeywords");
  182. if (!res) {
  183. fprintf(stderr, "Failed to execute command\n");
  184. return -1;
  185. }
  186. fprintf(stderr, "4. Result is '%s'\n", res);
  187. res = run_command("STREAM FILE auth-thankyou \"\"");
  188. if (!res) {
  189. fprintf(stderr, "Failed to execute command\n");
  190. return -1;
  191. }
  192. fprintf(stderr, "5. Result is '%s'\n", res);
  193. return 0;
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. char *tmp;
  198. int ver = 0;
  199. int subver = 0;
  200. /* Setup stdin/stdout for line buffering */
  201. setlinebuf(stdin);
  202. setlinebuf(stdout);
  203. if (read_environment()) {
  204. fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
  205. exit(1);
  206. }
  207. connect_sphinx();
  208. tmp = getenv("agi_enhanced");
  209. if (tmp) {
  210. if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
  211. ver = 0;
  212. }
  213. if (ver < 1) {
  214. fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
  215. exit(1);
  216. }
  217. if (run_script())
  218. return -1;
  219. exit(0);
  220. }