eagi-test.c 3.7 KB

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