Asterisk - The Open Source Telephony Project  GIT-master-b7027de
Macros | Functions | Variables
eagi-sphinx-test.c File Reference
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "asterisk.h"
#include "asterisk/compat.h"

Go to the source code of this file.

Macros

#define AUDIO_FILENO   (STDERR_FILENO + 1)
 
#define SPHINX_HOST   "192.168.1.108"
 
#define SPHINX_PORT   3460
 

Functions

static int connect_sphinx (void)
 
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)
 

Variables

static int sphinx_sock = -1
 

Detailed Description

Extended AGI test application

This code is released into public domain without any warranty of any kind.

Definition in file eagi-sphinx-test.c.

Macro Definition Documentation

◆ AUDIO_FILENO

#define AUDIO_FILENO   (STDERR_FILENO + 1)

Definition at line 34 of file eagi-sphinx-test.c.

Referenced by wait_result().

◆ SPHINX_HOST

#define SPHINX_HOST   "192.168.1.108"

Definition at line 36 of file eagi-sphinx-test.c.

Referenced by connect_sphinx().

◆ SPHINX_PORT

#define SPHINX_PORT   3460

Definition at line 37 of file eagi-sphinx-test.c.

Referenced by connect_sphinx().

Function Documentation

◆ connect_sphinx()

static int connect_sphinx ( void  )
static

Definition at line 41 of file eagi-sphinx-test.c.

References errno, gethostbyname, hp, SPHINX_HOST, SPHINX_PORT, and sphinx_sock.

Referenced by main().

42 {
43  struct hostent *hp;
44  struct sockaddr_in sin;
45  int res;
47  if (!hp) {
48  fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
49  return -1;
50  }
51  sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
52  if (sphinx_sock < 0) {
53  fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
54  return -1;
55  }
56  memset(&sin, 0, sizeof(sin));
57  sin.sin_family = AF_INET;
58  sin.sin_port = htons(SPHINX_PORT);
59  memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
60  if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
61  fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
62  close(sphinx_sock);
63  sphinx_sock = -1;
64  return -1;
65  }
66  res = fcntl(sphinx_sock, F_GETFL);
67  if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
68  fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
69  close(sphinx_sock);
70  sphinx_sock = -1;
71  return -1;
72  }
73  return 0;
74 }
#define gethostbyname
Definition: lock.h:637
#define SPHINX_HOST
static int sphinx_sock
#define SPHINX_PORT
int errno
static struct hostent * hp
Definition: chan_skinny.c:1236

◆ main()

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

Definition at line 211 of file eagi-sphinx-test.c.

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

Referenced by compute_func(), and is_zero_or_null().

212 {
213  char *tmp;
214  int ver = 0;
215  int subver = 0;
216  /* Setup stdin/stdout for line buffering */
217  setlinebuf(stdin);
218  setlinebuf(stdout);
219  if (read_environment()) {
220  fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
221  exit(1);
222  }
223  connect_sphinx();
224  tmp = getenv("agi_enhanced");
225  if (tmp) {
226  if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
227  ver = 0;
228  }
229  if (ver < 1) {
230  fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
231  exit(1);
232  }
233  if (run_script())
234  return -1;
235  exit(0);
236 }
static int connect_sphinx(void)
static int tmp()
Definition: bt_open.c:389
int errno
static int read_environment(void)
static int run_script(void)

◆ read_environment()

static int read_environment ( void  )
static

Definition at line 76 of file eagi-sphinx-test.c.

References buf, and setenv().

Referenced by main().

77 {
78  char buf[256];
79  char *val;
80  /* Read environment */
81  for(;;) {
82  if (!fgets(buf, sizeof(buf), stdin)) {
83  return -1;
84  }
85  if (feof(stdin))
86  return -1;
87  buf[strlen(buf) - 1] = '\0';
88  /* Check for end of environment */
89  if (!strlen(buf))
90  return 0;
91  val = strchr(buf, ':');
92  if (!val) {
93  fprintf(stderr, "Invalid environment: '%s'\n", buf);
94  return -1;
95  }
96  *val = '\0';
97  val++;
98  val++;
99  /* Skip space */
100  fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
101 
102  /* Load into normal environment */
103  setenv(buf, val, 1);
104 
105  }
106  /* Never reached */
107  return 0;
108 }
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 169 of file eagi-sphinx-test.c.

References wait_result().

Referenced by run_script().

170 {
171  fprintf(stdout, "%s\n", command);
172  return wait_result();
173 }
static char * wait_result(void)

◆ run_script()

static int run_script ( void  )
static

Definition at line 175 of file eagi-sphinx-test.c.

References run_command().

Referenced by main().

176 {
177  char *res;
178  res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
179  if (!res) {
180  fprintf(stderr, "Failed to execute command\n");
181  return -1;
182  }
183  fprintf(stderr, "1. Result is '%s'\n", res);
184  res = run_command("STREAM FILE demo-nomatch 0123456789*#");
185  if (!res) {
186  fprintf(stderr, "Failed to execute command\n");
187  return -1;
188  }
189  fprintf(stderr, "2. Result is '%s'\n", res);
190  res = run_command("SAY NUMBER 23452345 0123456789*#");
191  if (!res) {
192  fprintf(stderr, "Failed to execute command\n");
193  return -1;
194  }
195  fprintf(stderr, "3. Result is '%s'\n", res);
196  res = run_command("GET DATA demo-enterkeywords");
197  if (!res) {
198  fprintf(stderr, "Failed to execute command\n");
199  return -1;
200  }
201  fprintf(stderr, "4. Result is '%s'\n", res);
202  res = run_command("STREAM FILE auth-thankyou \"\"");
203  if (!res) {
204  fprintf(stderr, "Failed to execute command\n");
205  return -1;
206  }
207  fprintf(stderr, "5. Result is '%s'\n", res);
208  return 0;
209 }
static char * run_command(char *command)

◆ wait_result()

static char* wait_result ( void  )
static

Definition at line 110 of file eagi-sphinx-test.c.

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

Referenced by run_command().

111 {
112  fd_set fds;
113  int res;
114  int max;
115  static char astresp[256];
116  static char sphinxresp[256];
117  char audiobuf[4096];
118  for (;;) {
119  FD_ZERO(&fds);
120  FD_SET(STDIN_FILENO, &fds);
121  FD_SET(AUDIO_FILENO, &fds);
122  max = AUDIO_FILENO;
123  if (sphinx_sock > -1) {
124  FD_SET(sphinx_sock, &fds);
125  if (sphinx_sock > max)
126  max = sphinx_sock;
127  }
128  /* Wait for *some* sort of I/O */
129  res = select(max + 1, &fds, NULL, NULL, NULL);
130  if (res < 0) {
131  fprintf(stderr, "Error in select: %s\n", strerror(errno));
132  return NULL;
133  }
134  if (FD_ISSET(STDIN_FILENO, &fds)) {
135  if (!fgets(astresp, sizeof(astresp), stdin)) {
136  return NULL;
137  }
138  if (feof(stdin)) {
139  fprintf(stderr, "Got hungup on apparently\n");
140  return NULL;
141  }
142  astresp[strlen(astresp) - 1] = '\0';
143  fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
144  return astresp;
145  }
146  if (FD_ISSET(AUDIO_FILENO, &fds)) {
147  res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
148  if ((res > 0) && (sphinx_sock > -1)) {
149  if (write(sphinx_sock, audiobuf, res) < 0) {
150  fprintf(stderr, "write() failed: %s\n", strerror(errno));
151  }
152  }
153  }
154  if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
155  res = read(sphinx_sock, sphinxresp, sizeof(sphinxresp));
156  if (res > 0) {
157  fprintf(stderr, "Oooh, Sphinx found a token: '%s'\n", sphinxresp);
158  return sphinxresp;
159  } else if (res == 0) {
160  fprintf(stderr, "Hrm, lost sphinx, guess we're on our own\n");
161  close(sphinx_sock);
162  sphinx_sock = -1;
163  }
164  }
165  }
166 
167 }
#define NULL
Definition: resample.c:96
#define FD_ZERO(a)
Definition: select.h:49
#define FD_SET(fd, fds)
Definition: select.h:58
static int sphinx_sock
#define AUDIO_FILENO
int errno
#define max(a, b)
Definition: f2c.h:198

Variable Documentation

◆ sphinx_sock

int sphinx_sock = -1
static

Definition at line 39 of file eagi-sphinx-test.c.

Referenced by connect_sphinx(), and wait_result().