00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00031
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <sys/time.h>
00036 #include <signal.h>
00037 #include <errno.h>
00038 #include <unistd.h>
00039
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/frame.h"
00042 #include "asterisk/sched.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/file.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/manager.h"
00049 #include "asterisk/chanvars.h"
00050 #include "asterisk/linkedlists.h"
00051 #include "asterisk/indications.h"
00052 #include "asterisk/lock.h"
00053 #include "asterisk/utils.h"
00054
00055 #define MAX_AUTOMONS 1500
00056
00057 struct asent {
00058 struct ast_channel *chan;
00059
00060
00061
00062 unsigned int use_count;
00063 unsigned int orig_end_dtmf_flag:1;
00064 AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
00065 AST_LIST_ENTRY(asent) list;
00066 };
00067
00068 static AST_LIST_HEAD_STATIC(aslist, asent);
00069 static ast_cond_t as_cond;
00070
00071 static pthread_t asthread = AST_PTHREADT_NULL;
00072
00073 static int as_chan_list_state;
00074
00075 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
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 }
00089
00090 static void *autoservice_run(void *ign)
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
00101
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
00125
00126
00127
00128
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
00139
00140
00141 switch (f->frametype) {
00142
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
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 }
00169
00170 int ast_autoservice_start(struct ast_channel *chan)
00171 {
00172 int res = 0;
00173 struct asent *as;
00174
00175
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
00187 return 0;
00188 }
00189
00190 if (!(as = ast_calloc(1, sizeof(*as))))
00191 return -1;
00192
00193
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) {
00210 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
00211 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
00212
00213
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 }
00225
00226 int ast_autoservice_stop(struct ast_channel *chan)
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
00241
00242
00243
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
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 }
00288
00289 void ast_autoservice_init(void)
00290 {
00291 ast_cond_init(&as_cond, NULL);
00292 }