eagi-sphinx-test.c 5.0 KB

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