Mon Mar 31 07:39:13 2008

Asterisk developer's documentation


autoservice.c File Reference

Automatic channel service routines. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/sched.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/file.h"
#include "asterisk/translate.h"
#include "asterisk/manager.h"
#include "asterisk/chanvars.h"
#include "asterisk/linkedlists.h"
#include "asterisk/indications.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"

Include dependency graph for autoservice.c:

Go to the source code of this file.

Data Structures

struct  asent

Defines

#define MAX_AUTOMONS   1500

Functions

void ast_autoservice_init (void)
int ast_autoservice_start (struct ast_channel *chan)
 Automatically service a channel for us...
int ast_autoservice_stop (struct ast_channel *chan)
 Stop servicing a channel for us...
static AST_LIST_HEAD_STATIC (aslist, asent)
static void * autoservice_run (void *ign)
static void defer_frame (struct ast_channel *chan, struct ast_frame *f)

Variables

static int as_chan_list_state
static ast_cond_t as_cond
static pthread_t asthread = AST_PTHREADT_NULL


Detailed Description

Automatic channel service routines.

Author:
Mark Spencer <markster@digium.com>

Russell Bryant <russell@digium.com>

Definition in file autoservice.c.


Define Documentation

#define MAX_AUTOMONS   1500

Definition at line 55 of file autoservice.c.

Referenced by autoservice_run().


Function Documentation

void ast_autoservice_init ( void   ) 

Provided by astobj2.c Provided by autoservice.c

Definition at line 289 of file autoservice.c.

References as_cond, and ast_cond_init().

Referenced by main().

00290 {
00291    ast_cond_init(&as_cond, NULL);
00292 }

int ast_autoservice_start ( struct ast_channel chan  ) 

Automatically service a channel for us...

Return values:
0 success
-1 failure, or the channel is already being autoserviced

Definition at line 170 of file autoservice.c.

References as_cond, ast_calloc, ast_channel_lock, ast_channel_unlock, ast_cond_signal(), AST_FLAG_END_DTMF_ONLY, AST_LIST_EMPTY, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_pthread_create_background, AST_PTHREADT_NULL, ast_set_flag, ast_test_flag, asthread, autoservice_run(), asent::chan, free, LOG_WARNING, and asent::use_count.

Referenced by _macro_exec(), acf_curl_exec(), acf_cut_exec(), acf_odbc_read(), acf_odbc_write(), array(), ast_dtmf_stream(), ast_get_enum(), ast_get_srv(), ast_get_txt(), bridge_playfile(), builtin_atxfer(), builtin_automonitor(), builtin_blindtransfer(), conf_play(), feature_exec_app(), function_eval(), function_fieldqty(), function_realtime_read(), function_realtime_write(), osplookup_exec(), sla_station_exec(), smdi_msg_retrieve_read(), system_exec_helper(), and try_calling().

00171 {
00172    int res = 0;
00173    struct asent *as;
00174 
00175    /* Check if the channel already has autoservice */
00176    AST_LIST_LOCK(&aslist);
00177    AST_LIST_TRAVERSE(&aslist, as, list) {
00178       if (as->chan == chan) {
00179          as->use_count++;
00180          break;
00181       }
00182    }
00183    AST_LIST_UNLOCK(&aslist);
00184 
00185    if (as) {
00186       /* Entry exists, autoservice is already handling this channel */
00187       return 0;
00188    }
00189 
00190    if (!(as = ast_calloc(1, sizeof(*as))))
00191       return -1;
00192    
00193    /* New entry created */
00194    as->chan = chan;
00195    as->use_count = 1;
00196 
00197    ast_channel_lock(chan);
00198    as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
00199    if (!as->orig_end_dtmf_flag)
00200       ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
00201    ast_channel_unlock(chan);
00202 
00203    AST_LIST_LOCK(&aslist);
00204    if (AST_LIST_EMPTY(&aslist))
00205       ast_cond_signal(&as_cond);
00206    AST_LIST_INSERT_HEAD(&aslist, as, list);
00207    AST_LIST_UNLOCK(&aslist);
00208 
00209    if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
00210       if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
00211          ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
00212          /* There will only be a single member in the list at this point,
00213             the one we just added. */
00214          AST_LIST_LOCK(&aslist);
00215          AST_LIST_REMOVE(&aslist, as, list);
00216          AST_LIST_UNLOCK(&aslist);
00217          free(as);
00218          res = -1;
00219       } else
00220          pthread_kill(asthread, SIGURG);
00221    }
00222 
00223    return res;
00224 }

int ast_autoservice_stop ( struct ast_channel chan  ) 

Stop servicing a channel for us...

Return values:
0 success
-1 error, or the channel has been hungup

Definition at line 226 of file autoservice.c.

References ast_channel::_softhangup, as_chan_list_state, ast_clear_flag, AST_FLAG_BLOCKING, AST_FLAG_END_DTMF_ONLY, ast_frfree, AST_LIST_APPEND_LIST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_HEAD_NOLOCK, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, AST_PTHREADT_NULL, ast_queue_frame(), ast_test_flag, asthread, asent::chan, f, free, asent::orig_end_dtmf_flag, and asent::use_count.

Referenced by _macro_exec(), acf_curl_exec(), acf_cut_exec(), acf_odbc_read(), acf_odbc_write(), ast_dtmf_stream(), ast_get_srv(), ast_get_txt(), ast_hangup(), bridge_playfile(), builtin_atxfer(), builtin_automonitor(), conf_play(), feature_exec_app(), finishup(), function_eval(), function_fieldqty(), function_realtime_read(), function_realtime_write(), osplookup_exec(), sla_station_exec(), smdi_msg_retrieve_read(), system_exec_helper(), and try_calling().

00227 {
00228    int res = -1;
00229    struct asent *as;
00230    AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
00231    struct ast_frame *f;
00232    int removed = 0;
00233    int orig_end_dtmf_flag = 0;
00234    int chan_list_state;
00235 
00236    AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
00237 
00238    AST_LIST_LOCK(&aslist);
00239 
00240    /* Save the autoservice channel list state.  We _must_ verify that the channel
00241     * list has been rebuilt before we return.  Because, after we return, the channel
00242     * could get destroyed and we don't want our poor autoservice thread to step on
00243     * it after its gone! */
00244    chan_list_state = as_chan_list_state;
00245 
00246    AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {  
00247       if (as->chan == chan) {
00248          as->use_count--;
00249          if (as->use_count)
00250             break;
00251          AST_LIST_REMOVE_CURRENT(&aslist, list);
00252          AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
00253          orig_end_dtmf_flag = as->orig_end_dtmf_flag;
00254          free(as);
00255          removed = 1;
00256          if (!chan->_softhangup)
00257             res = 0;
00258          break;
00259       }
00260    }
00261    AST_LIST_TRAVERSE_SAFE_END
00262 
00263    if (removed && asthread != AST_PTHREADT_NULL) 
00264       pthread_kill(asthread, SIGURG);
00265 
00266    AST_LIST_UNLOCK(&aslist);
00267 
00268    if (!removed)
00269       return 0;
00270 
00271    if (!orig_end_dtmf_flag)
00272       ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
00273 
00274    /* Wait for it to un-block */
00275    while (ast_test_flag(chan, AST_FLAG_BLOCKING))
00276       usleep(1000);
00277 
00278    while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
00279       ast_queue_frame(chan, f);
00280       ast_frfree(f);
00281    }
00282 
00283    while (chan_list_state == as_chan_list_state)
00284       usleep(1000);
00285 
00286    return res;
00287 }

static AST_LIST_HEAD_STATIC ( aslist  ,
asent   
) [static]

static void* autoservice_run ( void *  ign  )  [static]

Definition at line 90 of file autoservice.c.

References ast_channel::_softhangup, as_chan_list_state, as_cond, ast_cond_wait(), AST_CONTROL_HANGUP, AST_FRAME_CNG, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_HTML, AST_FRAME_IAX, AST_FRAME_IMAGE, AST_FRAME_MODEM, AST_FRAME_NULL, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, AST_LIST_EMPTY, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), AST_PTHREADT_NULL, ast_read(), ast_waitfor_n(), asthread, asent::chan, defer_frame(), f, ast_frame::frametype, LOG_WARNING, MAX_AUTOMONS, and ast_frame::subclass.

Referenced by ast_autoservice_start().

00091 {
00092    for (;;) {
00093       struct ast_channel *mons[MAX_AUTOMONS];
00094       struct ast_channel *chan;
00095       struct asent *as;
00096       int x = 0, ms = 50;
00097 
00098       AST_LIST_LOCK(&aslist);
00099 
00100       /* At this point, we know that no channels that have been removed are going
00101        * to get used again. */
00102       as_chan_list_state++;
00103 
00104       if (AST_LIST_EMPTY(&aslist))
00105          ast_cond_wait(&as_cond, &aslist.lock);
00106 
00107       AST_LIST_TRAVERSE(&aslist, as, list) {
00108          if (!as->chan->_softhangup) {
00109             if (x < MAX_AUTOMONS)
00110                mons[x++] = as->chan;
00111             else
00112                ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
00113          }
00114       }
00115 
00116       AST_LIST_UNLOCK(&aslist);
00117 
00118       chan = ast_waitfor_n(mons, x, &ms);
00119       if (chan) {
00120          struct ast_frame *f = ast_read(chan);
00121    
00122          if (!f) {
00123             struct ast_frame hangup_frame = { 0, };
00124             /* No frame means the channel has been hung up.
00125              * A hangup frame needs to be queued here as ast_waitfor() may
00126              * never return again for the condition to be detected outside
00127              * of autoservice.  So, we'll leave a HANGUP queued up so the
00128              * thread in charge of this channel will know. */
00129 
00130             hangup_frame.frametype = AST_FRAME_CONTROL;
00131             hangup_frame.subclass = AST_CONTROL_HANGUP;
00132 
00133             defer_frame(chan, &hangup_frame);
00134 
00135             continue;
00136          }
00137          
00138          /* Do not add a default entry in this switch statement.  Each new
00139           * frame type should be addressed directly as to whether it should
00140           * be queued up or not. */
00141          switch (f->frametype) {
00142          /* Save these frames */
00143          case AST_FRAME_DTMF_END:
00144          case AST_FRAME_CONTROL:
00145          case AST_FRAME_TEXT:
00146          case AST_FRAME_IMAGE:
00147          case AST_FRAME_HTML:
00148             defer_frame(chan, f);
00149             break;
00150 
00151          /* Throw these frames away */
00152          case AST_FRAME_DTMF_BEGIN:
00153          case AST_FRAME_VOICE:
00154          case AST_FRAME_VIDEO:
00155          case AST_FRAME_NULL:
00156          case AST_FRAME_IAX:
00157          case AST_FRAME_CNG:
00158          case AST_FRAME_MODEM:
00159             break;
00160          }
00161 
00162          if (f)
00163             ast_frfree(f);
00164       }
00165    }
00166    asthread = AST_PTHREADT_NULL;
00167    return NULL;
00168 }

static void defer_frame ( struct ast_channel chan,
struct ast_frame f 
) [static]

Definition at line 75 of file autoservice.c.

References ast_frdup(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, and asent::chan.

Referenced by autoservice_run().

00076 {
00077    struct ast_frame *dup_f;
00078    struct asent *as;
00079 
00080    AST_LIST_LOCK(&aslist);
00081    AST_LIST_TRAVERSE(&aslist, as, list) {
00082       if (as->chan != chan)
00083          continue;
00084       if ((dup_f = ast_frdup(f)))
00085          AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
00086    }
00087    AST_LIST_UNLOCK(&aslist);
00088 }


Variable Documentation

int as_chan_list_state [static]

Definition at line 73 of file autoservice.c.

Referenced by ast_autoservice_stop(), and autoservice_run().

ast_cond_t as_cond [static]

Definition at line 69 of file autoservice.c.

Referenced by ast_autoservice_init(), ast_autoservice_start(), and autoservice_run().

pthread_t asthread = AST_PTHREADT_NULL [static]

Definition at line 71 of file autoservice.c.

Referenced by ast_autoservice_start(), ast_autoservice_stop(), and autoservice_run().


Generated on Mon Mar 31 07:39:14 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1