#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) |
Variables | |
static int | as_chan_list_state |
static ast_cond_t | as_cond |
static pthread_t | asthread = AST_PTHREADT_NULL |
Definition in file autoservice.c.
#define MAX_AUTOMONS 1500 |
void ast_autoservice_init | ( | void | ) |
Provided by astobj2.c Provided by autoservice.c
Definition at line 311 of file autoservice.c.
References as_cond, and ast_cond_init().
Referenced by main().
00312 { 00313 ast_cond_init(&as_cond, NULL); 00314 }
int ast_autoservice_start | ( | struct ast_channel * | chan | ) |
Automatically service a channel for us...
0 | success | |
-1 | failure, or the channel is already being autoserviced |
Definition at line 186 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().
00187 { 00188 int res = 0; 00189 struct asent *as; 00190 00191 /* Check if the channel already has autoservice */ 00192 AST_LIST_LOCK(&aslist); 00193 AST_LIST_TRAVERSE(&aslist, as, list) { 00194 if (as->chan == chan) { 00195 as->use_count++; 00196 break; 00197 } 00198 } 00199 AST_LIST_UNLOCK(&aslist); 00200 00201 if (as) { 00202 /* Entry exists, autoservice is already handling this channel */ 00203 return 0; 00204 } 00205 00206 if (!(as = ast_calloc(1, sizeof(*as)))) 00207 return -1; 00208 00209 /* New entry created */ 00210 as->chan = chan; 00211 as->use_count = 1; 00212 00213 ast_channel_lock(chan); 00214 as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0; 00215 if (!as->orig_end_dtmf_flag) 00216 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY); 00217 ast_channel_unlock(chan); 00218 00219 AST_LIST_LOCK(&aslist); 00220 00221 if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) { 00222 ast_cond_signal(&as_cond); 00223 } 00224 00225 AST_LIST_INSERT_HEAD(&aslist, as, list); 00226 00227 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */ 00228 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) { 00229 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n"); 00230 /* There will only be a single member in the list at this point, 00231 the one we just added. */ 00232 AST_LIST_REMOVE(&aslist, as, list); 00233 free(as); 00234 asthread = AST_PTHREADT_NULL; 00235 res = -1; 00236 } else { 00237 pthread_kill(asthread, SIGURG); 00238 } 00239 } 00240 00241 AST_LIST_UNLOCK(&aslist); 00242 00243 return res; 00244 }
int ast_autoservice_stop | ( | struct ast_channel * | chan | ) |
Stop servicing a channel for us...
0 | success | |
-1 | error, or the channel has been hungup |
Definition at line 246 of file autoservice.c.
References ast_channel::_softhangup, as_chan_list_state, ast_clear_flag, AST_FLAG_END_DTMF_ONLY, ast_frfree, 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(), 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().
00247 { 00248 int res = -1; 00249 struct asent *as, *removed = NULL; 00250 struct ast_frame *f; 00251 int chan_list_state; 00252 00253 AST_LIST_LOCK(&aslist); 00254 00255 /* Save the autoservice channel list state. We _must_ verify that the channel 00256 * list has been rebuilt before we return. Because, after we return, the channel 00257 * could get destroyed and we don't want our poor autoservice thread to step on 00258 * it after its gone! */ 00259 chan_list_state = as_chan_list_state; 00260 00261 /* Find the entry, but do not free it because it still can be in the 00262 autoservice thread array */ 00263 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) { 00264 if (as->chan == chan) { 00265 as->use_count--; 00266 if (as->use_count < 1) { 00267 AST_LIST_REMOVE_CURRENT(&aslist, list); 00268 removed = as; 00269 } 00270 break; 00271 } 00272 } 00273 AST_LIST_TRAVERSE_SAFE_END 00274 00275 if (removed && asthread != AST_PTHREADT_NULL) { 00276 pthread_kill(asthread, SIGURG); 00277 } 00278 00279 AST_LIST_UNLOCK(&aslist); 00280 00281 if (!removed) { 00282 return 0; 00283 } 00284 00285 /* Wait while autoservice thread rebuilds its list. */ 00286 while (chan_list_state == as_chan_list_state) { 00287 usleep(1000); 00288 } 00289 00290 /* Now autoservice thread should have no references to our entry 00291 and we can safely destroy it */ 00292 00293 if (!chan->_softhangup) { 00294 res = 0; 00295 } 00296 00297 if (!as->orig_end_dtmf_flag) { 00298 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY); 00299 } 00300 00301 while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) { 00302 ast_queue_frame(chan, f); 00303 ast_frfree(f); 00304 } 00305 00306 free(as); 00307 00308 return res; 00309 }
static AST_LIST_HEAD_STATIC | ( | aslist | , | |
asent | ||||
) | [static] |
static void* autoservice_run | ( | void * | ign | ) | [static] |
Definition at line 75 of file autoservice.c.
References ast_channel::_softhangup, 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_frdup(), ast_frfree, AST_LIST_EMPTY, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_read(), ast_waitfor_n(), asthread, asent::chan, f, ast_frame::frametype, LOG_WARNING, MAX_AUTOMONS, and ast_frame::subclass.
Referenced by ast_autoservice_start().
00076 { 00077 for (;;) { 00078 struct ast_channel *mons[MAX_AUTOMONS]; 00079 struct asent *ents[MAX_AUTOMONS]; 00080 struct ast_channel *chan; 00081 struct asent *as; 00082 int i, x = 0, ms = 50; 00083 struct ast_frame *f = NULL; 00084 struct ast_frame *defer_frame = NULL; 00085 00086 AST_LIST_LOCK(&aslist); 00087 00088 /* At this point, we know that no channels that have been removed are going 00089 * to get used again. */ 00090 as_chan_list_state++; 00091 00092 if (AST_LIST_EMPTY(&aslist)) { 00093 ast_cond_wait(&as_cond, &aslist.lock); 00094 } 00095 00096 AST_LIST_TRAVERSE(&aslist, as, list) { 00097 if (!as->chan->_softhangup) { 00098 if (x < MAX_AUTOMONS) { 00099 ents[x] = as; 00100 mons[x++] = as->chan; 00101 } else { 00102 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n"); 00103 } 00104 } 00105 } 00106 00107 AST_LIST_UNLOCK(&aslist); 00108 00109 chan = ast_waitfor_n(mons, x, &ms); 00110 if (!chan) { 00111 continue; 00112 } 00113 00114 f = ast_read(chan); 00115 00116 if (!f) { 00117 struct ast_frame hangup_frame = { 0, }; 00118 /* No frame means the channel has been hung up. 00119 * A hangup frame needs to be queued here as ast_waitfor() may 00120 * never return again for the condition to be detected outside 00121 * of autoservice. So, we'll leave a HANGUP queued up so the 00122 * thread in charge of this channel will know. */ 00123 00124 hangup_frame.frametype = AST_FRAME_CONTROL; 00125 hangup_frame.subclass = AST_CONTROL_HANGUP; 00126 00127 defer_frame = &hangup_frame; 00128 } else { 00129 00130 /* Do not add a default entry in this switch statement. Each new 00131 * frame type should be addressed directly as to whether it should 00132 * be queued up or not. */ 00133 00134 switch (f->frametype) { 00135 /* Save these frames */ 00136 case AST_FRAME_DTMF_END: 00137 case AST_FRAME_CONTROL: 00138 case AST_FRAME_TEXT: 00139 case AST_FRAME_IMAGE: 00140 case AST_FRAME_HTML: 00141 defer_frame = f; 00142 break; 00143 00144 /* Throw these frames away */ 00145 case AST_FRAME_DTMF_BEGIN: 00146 case AST_FRAME_VOICE: 00147 case AST_FRAME_VIDEO: 00148 case AST_FRAME_NULL: 00149 case AST_FRAME_IAX: 00150 case AST_FRAME_CNG: 00151 case AST_FRAME_MODEM: 00152 break; 00153 } 00154 } 00155 00156 if (!defer_frame) { 00157 if (f) { 00158 ast_frfree(f); 00159 } 00160 continue; 00161 } 00162 00163 if (f) { 00164 for (i = 0; i < x; i++) { 00165 struct ast_frame *dup_f; 00166 00167 if (mons[i] != chan) { 00168 continue; 00169 } 00170 00171 if ((dup_f = ast_frdup(f))) { 00172 AST_LIST_INSERT_TAIL(&ents[i]->deferred_frames, dup_f, frame_list); 00173 } 00174 00175 break; 00176 } 00177 ast_frfree(f); 00178 } 00179 } 00180 00181 asthread = AST_PTHREADT_NULL; 00182 00183 return NULL; 00184 }
int as_chan_list_state [static] |
ast_cond_t as_cond [static] |
Definition at line 69 of file autoservice.c.
Referenced by ast_autoservice_init(), and ast_autoservice_start().
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().