Sat Apr 26 2014 22:01:46

Asterisk developer's documentation


app_externalivr.c File Reference

External IVR application interface. More...

#include "asterisk.h"
#include <signal.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/linkedlists.h"
#include "asterisk/app.h"
#include "asterisk/utils.h"
#include "asterisk/tcptls.h"
#include "asterisk/astobj2.h"
Include dependency graph for app_externalivr.c:

Go to the source code of this file.

Data Structures

struct  ivr_localuser::finishlist
struct  gen_state
struct  ivr_localuser
struct  ivr_localuser::playlist
struct  playlist_entry

Defines

#define ast_chan_log(level, channel, format,...)   ast_log(level, "%s: " format, ast_channel_name(channel) , ## __VA_ARGS__)
#define EIVR_CMD_ANS   'T' /* answer channel */
#define EIVR_CMD_APND   'A' /* append to prompt queue */
#define EIVR_CMD_DTMF   'D' /* send DTMF */
#define EIVR_CMD_EXIT   'E' /* exit */
#define EIVR_CMD_GET   'G' /* get channel varable(s) */
#define EIVR_CMD_HGUP   'H' /* hangup */
#define EIVR_CMD_IRPT   'I' /* interrupt */
#define EIVR_CMD_LOG   'L' /* log message */
#define EIVR_CMD_OPT   'O' /* option */
#define EIVR_CMD_PARM   'P' /* return supplied params */
#define EIVR_CMD_SQUE   'S' /* (re)set prompt queue */
#define EIVR_CMD_SVAR   'V' /* set channel varable(s) */
#define EIVR_CMD_XIT   'X' /* exit **depricated** */
#define EXTERNALIVR_PORT   2949

Enumerations

enum  options_flags { noanswer = (1 << 0), ignore_hangup = (1 << 1), run_dead = (1 << 2) }

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int app_exec (struct ast_channel *chan, const char *data)
static void ast_eivr_getvariable (struct ast_channel *chan, char *data, char *outbuf, int outbuflen)
static void ast_eivr_senddtmf (struct ast_channel *chan, char *vdata)
static void ast_eivr_setvariable (struct ast_channel *chan, char *data)
static int eivr_comm (struct ast_channel *chan, struct ivr_localuser *u, int *eivr_events_fd, int *eivr_commands_fd, int *eivr_errors_fd, const struct ast_str *args, const struct ast_flags flags)
static void * gen_alloc (struct ast_channel *chan, void *params)
static void gen_closestream (struct gen_state *state)
static int gen_generate (struct ast_channel *chan, void *data, int len, int samples)
static int gen_nextfile (struct gen_state *state)
static struct ast_framegen_readframe (struct gen_state *state)
static void gen_release (struct ast_channel *chan, void *data)
static int load_module (void)
static struct playlist_entrymake_entry (const char *filename)
static void send_eivr_event (FILE *handle, const char event, const char *data, const struct ast_channel *chan)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "External IVR Interface Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static const char app [] = "ExternalIVR"
static struct ast_app_option app_opts [128] = { [ 'n' ] = { .flag = noanswer }, [ 'i' ] = { .flag = ignore_hangup }, [ 'd' ] = { .flag = run_dead },}
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_generator gen

Detailed Description

External IVR application interface.

Author:
Kevin P. Fleming <kpfleming@digium.com>
Note:
Portions taken from the file-based music-on-hold work created by Anthony Minessale II in res_musiconhold.c

Definition in file app_externalivr.c.


Define Documentation

#define ast_chan_log (   level,
  channel,
  format,
  ... 
)    ast_log(level, "%s: " format, ast_channel_name(channel) , ## __VA_ARGS__)

Definition at line 100 of file app_externalivr.c.

Referenced by app_exec(), eivr_comm(), gen_generate(), and gen_nextfile().

#define EIVR_CMD_ANS   'T' /* answer channel */

Definition at line 113 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_APND   'A' /* append to prompt queue */

Definition at line 103 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_DTMF   'D' /* send DTMF */

Definition at line 104 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_EXIT   'E' /* exit */

Definition at line 105 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_GET   'G' /* get channel varable(s) */

Definition at line 106 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_HGUP   'H' /* hangup */

Definition at line 107 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_IRPT   'I' /* interrupt */

Definition at line 108 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_LOG   'L' /* log message */

Definition at line 109 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_OPT   'O' /* option */

Definition at line 110 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_PARM   'P' /* return supplied params */

Definition at line 111 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_SQUE   'S' /* (re)set prompt queue */

Definition at line 112 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_SVAR   'V' /* set channel varable(s) */

Definition at line 114 of file app_externalivr.c.

Referenced by eivr_comm().

#define EIVR_CMD_XIT   'X' /* exit **depricated** */

Definition at line 115 of file app_externalivr.c.

Referenced by eivr_comm().

#define EXTERNALIVR_PORT   2949

Definition at line 117 of file app_externalivr.c.

Referenced by app_exec().


Enumeration Type Documentation

Enumerator:
noanswer 
ignore_hangup 
run_dead 

Definition at line 119 of file app_externalivr.c.

                   {
   noanswer = (1 << 0),
   ignore_hangup = (1 << 1),
   run_dead = (1 << 2),
};

Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 918 of file app_externalivr.c.

static void __unreg_module ( void  ) [static]

Definition at line 918 of file app_externalivr.c.

static int app_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 390 of file app_externalivr.c.

References ivr_localuser::abort_current_sound, ast_tcptls_session_args::accept_fd, ao2_ref, app_opts, ast_activate_generator(), AST_AF_UNSPEC, ast_answer(), AST_APP_ARG, ast_app_parse_options(), ast_chan_log, ast_close_fds_above_n(), ast_deactivate_generator(), ast_debug, AST_DECLARE_APP_ARGS, ast_free, AST_LIST_HEAD_INIT_VALUE, AST_LIST_REMOVE_HEAD, ast_log(), ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), ast_sockaddr_copy(), ast_sockaddr_port, ast_sockaddr_resolve(), ast_sockaddr_set_port, AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_str_alloca, ast_str_append(), ast_str_reset(), ast_strlen_zero(), ast_tcptls_client_create(), ast_tcptls_client_start(), ast_test_flag, ast_verb, ivr_localuser::chan, eivr_comm(), errno, EXTERNALIVR_PORT, ast_tcptls_session_instance::fd, ivr_localuser::gen_active, ignore_hangup, LOG_ERROR, noanswer, ivr_localuser::playlist, ast_tcptls_session_args::remote_address, and run_dead.

Referenced by load_module().

{
   struct ast_flags flags = { 0, };
   char *opts[0];
   struct playlist_entry *entry;
   int child_stdin[2] = { -1, -1 };
   int child_stdout[2] = { -1, -1 };
   int child_stderr[2] = { -1, -1 };
   int res = -1;
   int pid;

   struct ast_tcptls_session_instance *ser = NULL;

   struct ivr_localuser foo = {
      .playlist = AST_LIST_HEAD_INIT_VALUE,
      .finishlist = AST_LIST_HEAD_INIT_VALUE,
      .gen_active = 0,
      .playing_silence = 1,
   };
   struct ivr_localuser *u = &foo;

   char *buf;
   int j;
   char *s, **app_args, *e; 
   struct ast_str *comma_delim_args = ast_str_alloca(100);

   AST_DECLARE_APP_ARGS(eivr_args,
      AST_APP_ARG(application);
      AST_APP_ARG(options);
   );
   AST_DECLARE_APP_ARGS(application_args,
      AST_APP_ARG(cmd)[32];
   );

   u->abort_current_sound = 0;
   u->chan = chan;

   if (ast_strlen_zero(data)) {
      ast_log(LOG_ERROR, "ExternalIVR requires a command to execute\n");
      goto exit;
   }

   buf = ast_strdupa(data);
   AST_STANDARD_APP_ARGS(eivr_args, buf);

   ast_verb(4, "ExternalIVR received application and arguments: %s\n", eivr_args.application);
   ast_verb(4, "ExternalIVR received options: %s\n", eivr_args.options);

   /* Parse out any application arguments */
   if ((s = strchr(eivr_args.application, '('))) {
      s[0] = ',';
      if ((e = strrchr(s, ')'))) {
         *e = '\0';
      } else {
         ast_log(LOG_ERROR, "Parse error, missing closing parenthesis\n");
         goto exit;
      }
   }

   AST_STANDARD_APP_ARGS(application_args, eivr_args.application);
   app_args = application_args.argv;

   /* Put the application + the arguments in a , delimited list */
   ast_str_reset(comma_delim_args);
   for (j = 0; application_args.cmd[j] != NULL; j++) {
      ast_str_append(&comma_delim_args, 0, "%s%s", j == 0 ? "" : ",", application_args.cmd[j]);
   }

   /* Get rid of any extraneous arguments */
   if (eivr_args.options && (s = strchr(eivr_args.options, ','))) {
      *s = '\0';
   }

   /* Parse the ExternalIVR() arguments */
   ast_verb(4, "Parsing options from: [%s]\n", eivr_args.options);
   ast_app_parse_options(app_opts, &flags, opts, eivr_args.options);
   if (ast_test_flag(&flags, noanswer)) {
      ast_verb(4, "noanswer is set\n");
   }
   if (ast_test_flag(&flags, ignore_hangup)) {
      ast_verb(4, "ignore_hangup is set\n");
   }
   if (ast_test_flag(&flags, run_dead)) {
      ast_verb(4, "run_dead is set\n");
   }
   
   if (!(ast_test_flag(&flags, noanswer))) {
      ast_verb(3, "Answering channel and starting generator\n");
      if (ast_channel_state(chan) != AST_STATE_UP) {
         if (ast_test_flag(&flags, run_dead)) {
            ast_chan_log(LOG_ERROR, chan, "Running ExternalIVR with 'd'ead flag on non-hungup channel isn't supported\n");
            goto exit;
         }
         ast_answer(chan);
      }
      if (ast_activate_generator(chan, &gen, u) < 0) {
         ast_chan_log(LOG_ERROR, chan, "Failed to activate generator\n");
         goto exit;
      } else {
         u->gen_active = 1;
      }
   }

   if (!strncmp(app_args[0], "ivr://", sizeof("ivr://") - 1)) {
      struct ast_tcptls_session_args ivr_desc = {
         .accept_fd = -1,
         .name = "IVR",
      };
      struct ast_sockaddr *addrs;
      int num_addrs = 0, i = 0;
      char *host = app_args[0] + sizeof("ivr://") - 1;

      /* Communicate through socket to server */
      ast_debug(1, "Parsing hostname/port for socket connect from \"%s\"\n", host);

      if (!(num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC))) {
         ast_chan_log(LOG_ERROR, chan, "Unable to locate host '%s'\n", host);
         goto exit;
      }

      for (i = 0; i < num_addrs; i++) {
         if (!ast_sockaddr_port(&addrs[i])) {
            /* Default port if not specified */
            ast_sockaddr_set_port(&addrs[i], EXTERNALIVR_PORT);
         }
         ast_sockaddr_copy(&ivr_desc.remote_address, &addrs[i]);
         if (!(ser = ast_tcptls_client_create(&ivr_desc)) || !(ser = ast_tcptls_client_start(ser))) {
            continue;
         }
         break;
      }

      if (i == num_addrs) {
         ast_chan_log(LOG_ERROR, chan, "Could not connect to any host.  ExternalIVR failed.\n");
         goto exit;
      }

      res = eivr_comm(chan, u, &ser->fd, &ser->fd, NULL, comma_delim_args, flags);

   } else {
      if (pipe(child_stdin)) {
         ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child input: %s\n", strerror(errno));
         goto exit;
      }
      if (pipe(child_stdout)) {
         ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child output: %s\n", strerror(errno));
         goto exit;
      }
      if (pipe(child_stderr)) {
         ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child errors: %s\n", strerror(errno));
         goto exit;
      }
   
      pid = ast_safe_fork(0);
      if (pid < 0) {
         ast_log(LOG_ERROR, "Failed to fork(): %s\n", strerror(errno));
         goto exit;
      }
   
      if (!pid) {
         /* child process */
         if (ast_opt_high_priority)
            ast_set_priority(0);
   
         dup2(child_stdin[0], STDIN_FILENO);
         dup2(child_stdout[1], STDOUT_FILENO);
         dup2(child_stderr[1], STDERR_FILENO);
         ast_close_fds_above_n(STDERR_FILENO);
         execv(app_args[0], app_args);
         fprintf(stderr, "Failed to execute '%s': %s\n", app_args[0], strerror(errno));
         _exit(1);
      } else {
         /* parent process */
         close(child_stdin[0]);
         child_stdin[0] = -1;
         close(child_stdout[1]);
         child_stdout[1] = -1;
         close(child_stderr[1]);
         child_stderr[1] = -1;
         res = eivr_comm(chan, u, &child_stdin[1], &child_stdout[0], &child_stderr[0], comma_delim_args, flags);
      }
   }

   exit:
   if (u->gen_active) {
      ast_deactivate_generator(chan);
   }
   if (child_stdin[0] > -1) {
      close(child_stdin[0]);
   }
   if (child_stdin[1] > -1) {
      close(child_stdin[1]);
   }
   if (child_stdout[0] > -1) {
      close(child_stdout[0]);
   }
   if (child_stdout[1] > -1) {
      close(child_stdout[1]);
   }
   if (child_stderr[0] > -1) {
      close(child_stderr[0]);
   }
   if (child_stderr[1] > -1) {
      close(child_stderr[1]);
   }
   if (ser) {
      ao2_ref(ser, -1);
   }
   while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
      ast_free(entry);
   }
   return res;
}
static void ast_eivr_getvariable ( struct ast_channel chan,
char *  data,
char *  outbuf,
int  outbuflen 
) [static]

Definition at line 302 of file app_externalivr.c.

References ast_channel_lock, ast_channel_unlock, ast_copy_string(), ast_str_alloca, ast_str_append(), ast_str_buffer(), inbuf(), pbx_builtin_getvar_helper(), and value.

Referenced by eivr_comm().

{
   /* original input data: "G,var1,var2," */
   /* data passed as "data":  "var1,var2" */

   char *inbuf, *variable;
   const char *value;
   int j;
   struct ast_str *newstring = ast_str_alloca(outbuflen); 

   outbuf[0] = '\0';

   for (j = 1, inbuf = data; ; j++) {
      variable = strsep(&inbuf, ",");
      if (variable == NULL) {
         int outstrlen = strlen(outbuf);
         if (outstrlen && outbuf[outstrlen - 1] == ',') {
            outbuf[outstrlen - 1] = 0;
         }
         break;
      }
      
      ast_channel_lock(chan);
      if (!(value = pbx_builtin_getvar_helper(chan, variable))) {
         value = "";
      }

      ast_str_append(&newstring, 0, "%s=%s,", variable, value);
      ast_channel_unlock(chan);
      ast_copy_string(outbuf, ast_str_buffer(newstring), outbuflen);
   }
}
static void ast_eivr_senddtmf ( struct ast_channel chan,
char *  vdata 
) [static]

Definition at line 354 of file app_externalivr.c.

References args, AST_APP_ARG, ast_app_parse_timelen(), AST_DECLARE_APP_ARGS, ast_dtmf_stream(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_verb, and TIMELEN_MILLISECONDS.

Referenced by eivr_comm().

{

   char *data;
   int dinterval = 0, duration = 0;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(digits);
      AST_APP_ARG(dinterval);
      AST_APP_ARG(duration);
   );

   data = ast_strdupa(vdata);
   AST_STANDARD_APP_ARGS(args, data);

   if (!ast_strlen_zero(args.dinterval)) {
      ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS);
   }
   if (!ast_strlen_zero(args.duration)) {
      ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS);
   }
   ast_verb(4, "Sending DTMF: %s %d %d\n", args.digits, dinterval <= 0 ? 250 : dinterval, duration);
   ast_dtmf_stream(chan, NULL, args.digits, dinterval <= 0 ? 250 : dinterval, duration);
}
static void ast_eivr_setvariable ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 335 of file app_externalivr.c.

References ast_debug, inbuf(), pbx_builtin_setvar_helper(), and value.

Referenced by eivr_comm().

{
   char *value;

   char *inbuf = ast_strdupa(data), *variable;

   for (variable = strsep(&inbuf, ","); variable; variable = strsep(&inbuf, ",")) {
      ast_debug(1, "Setting up a variable: %s\n", variable);
      /* variable contains "varname=value" */
      value = strchr(variable, '=');
      if (!value) {
         value = "";
      } else {
         *value++ = '\0';
      }
      pbx_builtin_setvar_helper(chan, variable, value);
   }
}
static int eivr_comm ( struct ast_channel chan,
struct ivr_localuser u,
int *  eivr_events_fd,
int *  eivr_commands_fd,
int *  eivr_errors_fd,
const struct ast_str args,
const struct ast_flags  flags 
) [static]

Definition at line 604 of file app_externalivr.c.

References ivr_localuser::abort_current_sound, ast_activate_generator(), ast_answer(), ast_chan_log, ast_channel_flags(), ast_channel_hangupcause_set(), ast_channel_language(), ast_check_hangup(), AST_CONTROL_HANGUP, ast_eivr_getvariable(), ast_eivr_senddtmf(), ast_eivr_setvariable(), ast_fileexists(), AST_FLAG_ZOMBIE, AST_FRAME_CONTROL, AST_FRAME_DTMF, ast_free, ast_frfree, AST_LIST_EMPTY, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_read(), AST_STATE_UP, ast_str_buffer(), ast_strip(), ast_test_flag, ast_verb, ast_waitfor_nandfds(), ivr_localuser::chan, ast_frame::data, EIVR_CMD_ANS, EIVR_CMD_APND, EIVR_CMD_DTMF, EIVR_CMD_EXIT, EIVR_CMD_GET, EIVR_CMD_HGUP, EIVR_CMD_IRPT, EIVR_CMD_LOG, EIVR_CMD_OPT, EIVR_CMD_PARM, EIVR_CMD_SQUE, EIVR_CMD_SVAR, EIVR_CMD_XIT, errno, f, playlist_entry::filename, ivr_localuser::finishlist, ast_frame::frametype, ivr_localuser::gen_active, ignore_hangup, input(), ast_frame_subclass::integer, LOG_ERROR, LOG_NOTICE, LOG_WARNING, make_entry(), ivr_localuser::option_autoclear, ivr_localuser::playing_silence, ivr_localuser::playlist, run_dead, send_eivr_event(), ast_frame::subclass, and ast_frame::uint32.

Referenced by app_exec().

{
   struct playlist_entry *entry;
   struct ast_frame *f;
   int ms;
   int exception;
   int ready_fd;
   int waitfds[2] = { *eivr_commands_fd, (eivr_errors_fd) ? *eivr_errors_fd : -1 };
   struct ast_channel *rchan;
   int res = -1;
   int test_available_fd = -1;
   int hangup_info_sent = 0;
  
   FILE *eivr_commands = NULL;
   FILE *eivr_errors = NULL;
   FILE *eivr_events = NULL;

   if (!(eivr_events = fdopen(*eivr_events_fd, "w"))) {
      ast_chan_log(LOG_ERROR, chan, "Could not open stream to send events\n");
      goto exit;
   }
   if (!(eivr_commands = fdopen(*eivr_commands_fd, "r"))) {
      ast_chan_log(LOG_ERROR, chan, "Could not open stream to receive commands\n");
      goto exit;
   }
   if (eivr_errors_fd) {  /* if opening a socket connection, error stream will not be used */
      if (!(eivr_errors = fdopen(*eivr_errors_fd, "r"))) {
         ast_chan_log(LOG_ERROR, chan, "Could not open stream to receive errors\n");
         goto exit;
      }
   }

   test_available_fd = open("/dev/null", O_RDONLY);
 
   setvbuf(eivr_events, NULL, _IONBF, 0);
   setvbuf(eivr_commands, NULL, _IONBF, 0);
   if (eivr_errors) {
      setvbuf(eivr_errors, NULL, _IONBF, 0);
   }

   while (1) {
      if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
         ast_chan_log(LOG_ERROR, chan, "Is a zombie\n");
         break;
      }
      if (!hangup_info_sent && !(ast_test_flag(&flags, run_dead)) && ast_check_hangup(chan)) {
         if (ast_test_flag(&flags, ignore_hangup)) {
            ast_verb(3, "Got check_hangup, but ignore_hangup set so sending 'I' command\n");
            send_eivr_event(eivr_events, 'I', "HANGUP", chan);
            hangup_info_sent = 1;
         } else {
            ast_verb(3, "Got check_hangup\n");
            send_eivr_event(eivr_events, 'H', NULL, chan);
            break;
         }
      }
 
      ready_fd = 0;
      ms = 100;
      errno = 0;
      exception = 0;
 
      rchan = ast_waitfor_nandfds(&chan, 1, waitfds, (eivr_errors_fd) ? 2 : 1, &exception, &ready_fd, &ms);
 
      if (ast_channel_state(chan) == AST_STATE_UP && !AST_LIST_EMPTY(&u->finishlist)) {
         AST_LIST_LOCK(&u->finishlist);
         while ((entry = AST_LIST_REMOVE_HEAD(&u->finishlist, list))) {
            send_eivr_event(eivr_events, 'F', entry->filename, chan);
            ast_free(entry);
         }
         AST_LIST_UNLOCK(&u->finishlist);
      }
 
      if (ast_channel_state(chan) == AST_STATE_UP && !(ast_check_hangup(chan)) && rchan) {
         /* the channel has something */
         f = ast_read(chan);
         if (!f) {
            ast_verb(3, "Returned no frame\n");
            send_eivr_event(eivr_events, 'H', NULL, chan);
            break;
         }
         if (f->frametype == AST_FRAME_DTMF) {
            send_eivr_event(eivr_events, f->subclass.integer, NULL, chan);
            if (u->option_autoclear) {
               AST_LIST_LOCK(&u->playlist);
               if (!u->abort_current_sound && !u->playing_silence) {
                  /* send interrupted file as T data */
                  if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
                     send_eivr_event(eivr_events, 'T', entry->filename, chan);
                     ast_free(entry);
                  }
               }
               while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
                  send_eivr_event(eivr_events, 'D', entry->filename, chan);
                  ast_free(entry);
               }
               if (!u->playing_silence)
                  u->abort_current_sound = 1;
               AST_LIST_UNLOCK(&u->playlist);
            }
         } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_HANGUP)) {
            ast_verb(3, "Got AST_CONTROL_HANGUP\n");
            send_eivr_event(eivr_events, 'H', NULL, chan);
            if (f->data.uint32) {
               ast_channel_hangupcause_set(chan, f->data.uint32);
            }
            ast_frfree(f);
            break;
         }
         ast_frfree(f);
      } else if (ready_fd == *eivr_commands_fd) {
         char input[1024];
 
         if (exception || (dup2(*eivr_commands_fd, test_available_fd) == -1) || feof(eivr_commands)) {
            ast_chan_log(LOG_ERROR, chan, "Child process went away\n");
            break;
         }
  
         if (!fgets(input, sizeof(input), eivr_commands)) {
            continue;
         }

         ast_strip(input);
         ast_verb(4, "got command '%s'\n", input);

         if (strlen(input) < 3) {
            continue;
         }

         if (input[0] == EIVR_CMD_PARM) {
            struct ast_str *tmp = (struct ast_str *) args;
            send_eivr_event(eivr_events, 'P', ast_str_buffer(tmp), chan);
         } else if (input[0] == EIVR_CMD_DTMF) {
            ast_verb(4, "Sending DTMF: %s\n", &input[2]);
            ast_eivr_senddtmf(chan, &input[2]);
         } else if (input[0] == EIVR_CMD_ANS) {
            ast_verb(3, "Answering channel if needed and starting generator\n");
            if (ast_channel_state(chan) != AST_STATE_UP) {
               if (ast_test_flag(&flags, run_dead)) {
                  ast_chan_log(LOG_WARNING, chan, "Running ExternalIVR with 'd'ead flag on non-hungup channel isn't supported\n");
                  send_eivr_event(eivr_events, 'Z', "ANSWER_FAILURE", chan);
                  continue;
               }
               if (ast_answer(chan)) {
                  ast_chan_log(LOG_WARNING, chan, "Failed to answer channel\n");
                  send_eivr_event(eivr_events, 'Z', "ANSWER_FAILURE", chan);
                  continue;
               }
            }
            if (!(u->gen_active)) {
               if (ast_activate_generator(chan, &gen, u) < 0) {
                  ast_chan_log(LOG_WARNING, chan, "Failed to activate generator\n");
                  send_eivr_event(eivr_events, 'Z', "GENERATOR_FAILURE", chan);
               } else {
                  u->gen_active = 1;
               }
            }
         } else if (input[0] == EIVR_CMD_IRPT) {
            if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
               ast_chan_log(LOG_WARNING, chan, "Queue 'I'nterrupt called on unanswered channel\n");
               send_eivr_event(eivr_events, 'Z', NULL, chan);
               continue;
            }
            AST_LIST_LOCK(&u->playlist);
            if (!u->abort_current_sound && !u->playing_silence) {
               /* send interrupted file as T data */
               if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
                  send_eivr_event(eivr_events, 'T', entry->filename, chan);
                  ast_free(entry);
               }
            }
            while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
               send_eivr_event(eivr_events, 'D', entry->filename, chan);
               ast_free(entry);
            }
            if (!u->playing_silence) {
               u->abort_current_sound = 1;
            }
            AST_LIST_UNLOCK(&u->playlist);
         } else if (input[0] == EIVR_CMD_SQUE) {
            if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
               ast_chan_log(LOG_WARNING, chan, "Queue re'S'et called on unanswered channel\n");
               send_eivr_event(eivr_events, 'Z', NULL, chan);
               continue;
            }
            if (!ast_fileexists(&input[2], NULL, ast_channel_language(u->chan))) {
               ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
               send_eivr_event(eivr_events, 'Z', &input[2], chan);
            } else {
               AST_LIST_LOCK(&u->playlist);
               if (!u->abort_current_sound && !u->playing_silence) {
                  /* send interrupted file as T data */
                  if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
                     send_eivr_event(eivr_events, 'T', entry->filename, chan);
                     ast_free(entry);
                  }
               }
               while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
                  send_eivr_event(eivr_events, 'D', entry->filename, chan);
                  ast_free(entry);
               }
               if (!u->playing_silence) {
                  u->abort_current_sound = 1;
               }
               entry = make_entry(&input[2]);
               if (entry) {
                  AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
               }
               AST_LIST_UNLOCK(&u->playlist);
            }
         } else if (input[0] == EIVR_CMD_APND) {
            if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
               ast_chan_log(LOG_WARNING, chan, "Queue 'A'ppend called on unanswered channel\n");
               send_eivr_event(eivr_events, 'Z', NULL, chan);
               continue;
            }
            if (!ast_fileexists(&input[2], NULL, ast_channel_language(u->chan))) {
               ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
               send_eivr_event(eivr_events, 'Z', &input[2], chan);
            } else {
               entry = make_entry(&input[2]);
               if (entry) {
                  AST_LIST_LOCK(&u->playlist);
                  AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
                  AST_LIST_UNLOCK(&u->playlist);
               }
            }
         } else if (input[0] == EIVR_CMD_GET) {
            char response[2048];
            ast_verb(4, "Retriving Variables from channel: %s\n", &input[2]);
            ast_eivr_getvariable(chan, &input[2], response, sizeof(response));
            send_eivr_event(eivr_events, 'G', response, chan);
         } else if (input[0] == EIVR_CMD_SVAR) {
            ast_verb(4, "Setting Variables in channel: %s\n", &input[2]);
            ast_eivr_setvariable(chan, &input[2]);
         } else if (input[0] == EIVR_CMD_LOG) {
            ast_chan_log(LOG_NOTICE, chan, "Log message from EIVR: %s\n", &input[2]);
         } else if (input[0] == EIVR_CMD_XIT) {
            ast_chan_log(LOG_NOTICE, chan, "Exiting: %s\n", &input[2]);
            ast_chan_log(LOG_WARNING, chan, "e'X'it command is depricated, use 'E'xit instead\n");
            res = 0;
            break;
         } else if (input[0] == EIVR_CMD_EXIT) {
            ast_chan_log(LOG_NOTICE, chan, "Exiting: %s\n", &input[2]);
            send_eivr_event(eivr_events, 'E', NULL, chan);
            res = 0;
            break;
         } else if (input[0] == EIVR_CMD_HGUP) {
            ast_chan_log(LOG_NOTICE, chan, "Hanging up: %s\n", &input[2]);
            send_eivr_event(eivr_events, 'H', NULL, chan);
            break;
         } else if (input[0] == EIVR_CMD_OPT) {
            if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
               ast_chan_log(LOG_WARNING, chan, "Option called on unanswered channel\n");
               send_eivr_event(eivr_events, 'Z', NULL, chan);
               continue;
            }
            if (!strcasecmp(&input[2], "autoclear"))
               u->option_autoclear = 1;
            else if (!strcasecmp(&input[2], "noautoclear"))
               u->option_autoclear = 0;
            else
               ast_chan_log(LOG_WARNING, chan, "Unknown option requested: %s\n", &input[2]);
         }
      } else if (eivr_errors_fd && (ready_fd == *eivr_errors_fd)) {
         char input[1024];
  
         if (exception || feof(eivr_errors)) {
            ast_chan_log(LOG_ERROR, chan, "Child process went away\n");
            break;
         }
         if (fgets(input, sizeof(input), eivr_errors)) {
            ast_chan_log(LOG_NOTICE, chan, "stderr: %s\n", ast_strip(input));
         }
      } else if ((ready_fd < 0) && ms) { 
         if (errno == 0 || errno == EINTR)
            continue;
 
         ast_chan_log(LOG_ERROR, chan, "Wait failed (%s)\n", strerror(errno));
         break;
      }
   }
 
   exit:
   if (test_available_fd > -1) {
      close(test_available_fd);
   }
   if (eivr_events) {
      fclose(eivr_events);
      *eivr_events_fd = -1;
   }
   if (eivr_commands) {
      fclose(eivr_commands);
      *eivr_commands_fd = -1;
   }
   if (eivr_errors) {
      fclose(eivr_errors);
      *eivr_errors_fd = -1;
   }
   return res;
}
static void* gen_alloc ( struct ast_channel chan,
void *  params 
) [static]

Definition at line 173 of file app_externalivr.c.

References ast_calloc, state, and gen_state::u.

{
   struct ivr_localuser *u = params;
   struct gen_state *state;

   if (!(state = ast_calloc(1, sizeof(*state))))
      return NULL;

   state->u = u;

   return state;
}
static void gen_closestream ( struct gen_state state) [static]

Definition at line 186 of file app_externalivr.c.

References ast_channel_stream_set(), ast_closestream(), ivr_localuser::chan, gen_state::stream, and gen_state::u.

Referenced by gen_nextfile(), gen_readframe(), and gen_release().

{
   if (!state->stream)
      return;

   ast_closestream(state->stream);
   ast_channel_stream_set(state->u->chan, NULL);
   state->stream = NULL;
}
static int gen_generate ( struct ast_channel chan,
void *  data,
int  len,
int  samples 
) [static]

Definition at line 271 of file app_externalivr.c.

References ast_chan_log, ast_frfree, ast_write(), errno, f, gen_readframe(), LOG_WARNING, gen_state::sample_queue, and ast_frame::samples.

{
   struct gen_state *state = data;
   struct ast_frame *f = NULL;
   int res = 0;

   state->sample_queue += samples;

   while (state->sample_queue > 0) {
      if (!(f = gen_readframe(state)))
         return -1;

      res = ast_write(chan, f);
      ast_frfree(f);
      if (res < 0) {
         ast_chan_log(LOG_WARNING, chan, "Failed to write frame: %s\n", strerror(errno));
         return -1;
      }
      state->sample_queue -= f->samples;
   }

   return res;
}
static int gen_nextfile ( struct gen_state state) [static]

Definition at line 205 of file app_externalivr.c.

References ivr_localuser::abort_current_sound, ast_chan_log, ast_channel_language(), AST_LIST_FIRST, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_openstream_full(), ivr_localuser::chan, gen_state::current, errno, playlist_entry::filename, gen_closestream(), LOG_WARNING, ivr_localuser::playing_silence, ivr_localuser::playlist, gen_state::stream, and gen_state::u.

Referenced by gen_readframe().

{
   struct ivr_localuser *u = state->u;
   char *file_to_stream;

   u->abort_current_sound = 0;
   u->playing_silence = 0;
   gen_closestream(state);

   while (!state->stream) {
      state->current = AST_LIST_FIRST(&u->playlist);
      if (state->current) {
         file_to_stream = state->current->filename;
      } else {
         file_to_stream = "silence/10";
         u->playing_silence = 1;
      }

      if (!(state->stream = ast_openstream_full(u->chan, file_to_stream, ast_channel_language(u->chan), 1))) {
         ast_chan_log(LOG_WARNING, u->chan, "File '%s' could not be opened: %s\n", file_to_stream, strerror(errno));
         AST_LIST_LOCK(&u->playlist);
         AST_LIST_REMOVE_HEAD(&u->playlist, list);
         AST_LIST_UNLOCK(&u->playlist);
         if (!u->playing_silence) {
            continue;
         } else {
            break;
         }
      }
   }

   return (!state->stream);
}
static struct ast_frame* gen_readframe ( struct gen_state state) [static, read]
static void gen_release ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 196 of file app_externalivr.c.

References ast_free, and gen_closestream().

{
   struct gen_state *state = data;

   gen_closestream(state);
   ast_free(data);
}
static int load_module ( void  ) [static]

Definition at line 913 of file app_externalivr.c.

References app_exec(), and ast_register_application_xml.

static struct playlist_entry* make_entry ( const char *  filename) [static, read]

Definition at line 378 of file app_externalivr.c.

References ast_calloc, and playlist_entry::filename.

Referenced by eivr_comm().

{
   struct playlist_entry *entry;

   if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(filename) + 10))) /* XXX why 10 ? */
      return NULL;

   strcpy(entry->filename, filename);

   return entry;
}
static void send_eivr_event ( FILE *  handle,
const char  event,
const char *  data,
const struct ast_channel chan 
) [static]

Definition at line 158 of file app_externalivr.c.

References ast_debug, ast_free, ast_str_append(), ast_str_buffer(), and ast_str_create().

Referenced by eivr_comm().

{
   struct ast_str *tmp = ast_str_create(12);

   ast_str_append(&tmp, 0, "%c,%10d", event, (int)time(NULL));
   if (data) {
      ast_str_append(&tmp, 0, ",%s", data);
   }

   fprintf(handle, "%s\n", ast_str_buffer(tmp));
   ast_debug(1, "sent '%s'\n", ast_str_buffer(tmp));
   ast_free(tmp);
}
static int unload_module ( void  ) [static]

Definition at line 908 of file app_externalivr.c.

References ast_unregister_application().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "External IVR Interface Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 918 of file app_externalivr.c.

const char app[] = "ExternalIVR" [static]

Definition at line 97 of file app_externalivr.c.

struct ast_app_option app_opts[128] = { [ 'n' ] = { .flag = noanswer }, [ 'i' ] = { .flag = ignore_hangup }, [ 'd' ] = { .flag = run_dead },} [static]

Definition at line 129 of file app_externalivr.c.

Referenced by app_exec().

Definition at line 918 of file app_externalivr.c.

struct ast_generator gen [static]

Definition at line 295 of file app_externalivr.c.

Referenced by reload_config(), and set_config().