Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
eagi-test.c File Reference
#include "asterisk.h"
Include dependency graph for eagi-test.c:

Go to the source code of this file.

Macros

#define AUDIO_FILENO   (STDERR_FILENO + 1)
 

Functions

int main (int argc, char *argv[])
 
static int read_environment (void)
 
static char * run_command (char *command)
 
static int run_script (void)
 
static char * wait_result (void)
 

Detailed Description

Extended AGI test application

This code is released into the public domain with no warranty of any kind

Definition in file eagi-test.c.

Macro Definition Documentation

◆ AUDIO_FILENO

#define AUDIO_FILENO   (STDERR_FILENO + 1)

Definition at line 14 of file eagi-test.c.

Referenced by wait_result().

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 149 of file eagi-test.c.

References errno, read_environment(), run_script(), and tmp().

150 {
151  char *tmp;
152  int ver = 0;
153  int subver = 0;
154  /* Setup stdin/stdout for line buffering */
155  setlinebuf(stdin);
156  setlinebuf(stdout);
157  if (read_environment()) {
158  fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
159  exit(1);
160  }
161  tmp = getenv("agi_enhanced");
162  if (tmp) {
163  if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
164  ver = 0;
165  }
166  if (ver < 1) {
167  fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
168  exit(1);
169  }
170  if (run_script())
171  return -1;
172  exit(0);
173 }
static int read_environment(void)
Definition: eagi-test.c:25
static int tmp()
Definition: bt_open.c:389
static int run_script(void)
Definition: eagi-test.c:113
int errno

◆ read_environment()

static int read_environment ( void  )
static

Definition at line 25 of file eagi-test.c.

References buf, and setenv().

Referenced by main().

26 {
27  char buf[256];
28  char *val;
29  /* Read environment */
30  for(;;) {
31  if (!fgets(buf, sizeof(buf), stdin)) {
32  return -1;
33  }
34  if (feof(stdin))
35  return -1;
36  buf[strlen(buf) - 1] = '\0';
37  /* Check for end of environment */
38  if (!strlen(buf))
39  return 0;
40  val = strchr(buf, ':');
41  if (!val) {
42  fprintf(stderr, "Invalid environment: '%s'\n", buf);
43  return -1;
44  }
45  *val = '\0';
46  val++;
47  val++;
48  /* Skip space */
49  fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
50 
51  /* Load into normal environment */
52  setenv(buf, val, 1);
53 
54  }
55  /* Never reached */
56  return 0;
57 }
Definition: ast_expr2.c:325
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int setenv(const char *name, const char *value, int overwrite)

◆ run_command()

static char* run_command ( char *  command)
static

Definition at line 107 of file eagi-test.c.

References wait_result().

Referenced by run_script().

108 {
109  fprintf(stdout, "%s\n", command);
110  return wait_result();
111 }
static char * wait_result(void)
Definition: eagi-test.c:59

◆ run_script()

static int run_script ( void  )
static

Definition at line 113 of file eagi-test.c.

References run_command().

Referenced by main().

114 {
115  char *res;
116  res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
117  if (!res) {
118  fprintf(stderr, "Failed to execute command\n");
119  return -1;
120  }
121  fprintf(stderr, "1. Result is '%s'\n", res);
122  res = run_command("STREAM FILE demo-nomatch 0123456789*#");
123  if (!res) {
124  fprintf(stderr, "Failed to execute command\n");
125  return -1;
126  }
127  fprintf(stderr, "2. Result is '%s'\n", res);
128  res = run_command("SAY NUMBER 23452345 0123456789*#");
129  if (!res) {
130  fprintf(stderr, "Failed to execute command\n");
131  return -1;
132  }
133  fprintf(stderr, "3. Result is '%s'\n", res);
134  res = run_command("GET DATA demo-enterkeywords");
135  if (!res) {
136  fprintf(stderr, "Failed to execute command\n");
137  return -1;
138  }
139  fprintf(stderr, "4. Result is '%s'\n", res);
140  res = run_command("STREAM FILE auth-thankyou \"\"");
141  if (!res) {
142  fprintf(stderr, "Failed to execute command\n");
143  return -1;
144  }
145  fprintf(stderr, "5. Result is '%s'\n", res);
146  return 0;
147 }
static char * run_command(char *command)
Definition: eagi-test.c:107

◆ wait_result()

static char* wait_result ( void  )
static

Definition at line 59 of file eagi-test.c.

References AUDIO_FILENO, errno, FD_SET, FD_ZERO, and NULL.

Referenced by run_command().

60 {
61  fd_set fds;
62  int res;
63  int bytes = 0;
64  static char astresp[256];
65  char audiobuf[4096];
66  for (;;) {
67  FD_ZERO(&fds);
68  FD_SET(STDIN_FILENO, &fds);
69  FD_SET(AUDIO_FILENO, &fds);
70  /* Wait for *some* sort of I/O */
71  res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL);
72  if (res < 0) {
73  fprintf(stderr, "Error in select: %s\n", strerror(errno));
74  return NULL;
75  }
76  if (FD_ISSET(STDIN_FILENO, &fds)) {
77  if (!fgets(astresp, sizeof(astresp), stdin)) {
78  return NULL;
79  }
80  if (feof(stdin)) {
81  fprintf(stderr, "Got hungup on apparently\n");
82  return NULL;
83  }
84  astresp[strlen(astresp) - 1] = '\0';
85  fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
86  return astresp;
87  }
88  if (FD_ISSET(AUDIO_FILENO, &fds)) {
89  res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
90  if (res > 0) {
91  /* XXX Process the audio with sphinx here XXX */
92 #if 0
93  fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes);
94 #endif
95  bytes += res;
96  /* Prentend we detected some audio after 3 seconds */
97  if (bytes > 16000 * 3) {
98  return "Sample Message";
99  bytes = 0;
100  }
101  }
102  }
103  }
104 
105 }
#define NULL
Definition: resample.c:96
#define FD_ZERO(a)
Definition: select.h:49
#define FD_SET(fd, fds)
Definition: select.h:58
#define AUDIO_FILENO
Definition: eagi-test.c:14
int errno