eagi-test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Extended AGI test application
  3. *
  4. * This code is released into the public domain
  5. * with no warranty of any kind
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <sys/select.h>
  13. #define AUDIO_FILENO (STDERR_FILENO + 1)
  14. static int read_environment(void)
  15. {
  16. char buf[256];
  17. char *val;
  18. /* Read environment */
  19. for(;;) {
  20. fgets(buf, sizeof(buf), stdin);
  21. if (feof(stdin))
  22. return -1;
  23. buf[strlen(buf) - 1] = '\0';
  24. /* Check for end of environment */
  25. if (!strlen(buf))
  26. return 0;
  27. val = strchr(buf, ':');
  28. if (!val) {
  29. fprintf(stderr, "Invalid environment: '%s'\n", buf);
  30. return -1;
  31. }
  32. *val = '\0';
  33. val++;
  34. val++;
  35. /* Skip space */
  36. fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
  37. /* Load into normal environment */
  38. setenv(buf, val, 1);
  39. }
  40. /* Never reached */
  41. return 0;
  42. }
  43. static char *wait_result(void)
  44. {
  45. fd_set fds;
  46. int res;
  47. int bytes = 0;
  48. static char astresp[256];
  49. char audiobuf[4096];
  50. for (;;) {
  51. FD_ZERO(&fds);
  52. FD_SET(STDIN_FILENO, &fds);
  53. FD_SET(AUDIO_FILENO, &fds);
  54. /* Wait for *some* sort of I/O */
  55. res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL);
  56. if (res < 0) {
  57. fprintf(stderr, "Error in select: %s\n", strerror(errno));
  58. return NULL;
  59. }
  60. if (FD_ISSET(STDIN_FILENO, &fds)) {
  61. fgets(astresp, sizeof(astresp), stdin);
  62. if (feof(stdin)) {
  63. fprintf(stderr, "Got hungup on apparently\n");
  64. return NULL;
  65. }
  66. astresp[strlen(astresp) - 1] = '\0';
  67. fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
  68. return astresp;
  69. }
  70. if (FD_ISSET(AUDIO_FILENO, &fds)) {
  71. res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
  72. if (res > 0) {
  73. /* XXX Process the audio with sphinx here XXX */
  74. #if 0
  75. fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes);
  76. #endif
  77. bytes += res;
  78. /* Prentend we detected some audio after 3 seconds */
  79. if (bytes > 16000 * 3) {
  80. return "Sample Message";
  81. bytes = 0;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. static char *run_command(char *command)
  88. {
  89. fprintf(stdout, "%s\n", command);
  90. return wait_result();
  91. }
  92. static int run_script(void)
  93. {
  94. char *res;
  95. res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
  96. if (!res) {
  97. fprintf(stderr, "Failed to execute command\n");
  98. return -1;
  99. }
  100. fprintf(stderr, "1. Result is '%s'\n", res);
  101. res = run_command("STREAM FILE demo-nomatch 0123456789*#");
  102. if (!res) {
  103. fprintf(stderr, "Failed to execute command\n");
  104. return -1;
  105. }
  106. fprintf(stderr, "2. Result is '%s'\n", res);
  107. res = run_command("SAY NUMBER 23452345 0123456789*#");
  108. if (!res) {
  109. fprintf(stderr, "Failed to execute command\n");
  110. return -1;
  111. }
  112. fprintf(stderr, "3. Result is '%s'\n", res);
  113. res = run_command("GET DATA demo-enterkeywords");
  114. if (!res) {
  115. fprintf(stderr, "Failed to execute command\n");
  116. return -1;
  117. }
  118. fprintf(stderr, "4. Result is '%s'\n", res);
  119. res = run_command("STREAM FILE auth-thankyou \"\"");
  120. if (!res) {
  121. fprintf(stderr, "Failed to execute command\n");
  122. return -1;
  123. }
  124. fprintf(stderr, "5. Result is '%s'\n", res);
  125. return 0;
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129. char *tmp;
  130. int ver = 0;
  131. int subver = 0;
  132. /* Setup stdin/stdout for line buffering */
  133. setlinebuf(stdin);
  134. setlinebuf(stdout);
  135. if (read_environment()) {
  136. fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
  137. exit(1);
  138. }
  139. tmp = getenv("agi_enhanced");
  140. if (tmp) {
  141. if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
  142. ver = 0;
  143. }
  144. if (ver < 1) {
  145. fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
  146. exit(1);
  147. }
  148. if (run_script())
  149. return -1;
  150. exit(0);
  151. }