eagi-test.c 3.6 KB

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