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 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <sys/time.h>
00029 #include <signal.h>
00030 #include <errno.h>
00031 #include <unistd.h>
00032 #include <math.h>
00033
00034 #ifdef ZAPTEL_OPTIMIZATIONS
00035 #include <sys/ioctl.h>
00036 #ifdef __linux__
00037 #include <linux/zaptel.h>
00038 #else
00039 #include <zaptel.h>
00040 #endif
00041 #ifndef ZT_TIMERPING
00042 #error "You need newer zaptel! Please cvs update zaptel"
00043 #endif
00044 #endif
00045
00046 #include "asterisk.h"
00047
00048 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 43778 $")
00049
00050 #include "asterisk/pbx.h"
00051 #include "asterisk/frame.h"
00052 #include "asterisk/sched.h"
00053 #include "asterisk/options.h"
00054 #include "asterisk/channel.h"
00055 #include "asterisk/chanspy.h"
00056 #include "asterisk/musiconhold.h"
00057 #include "asterisk/logger.h"
00058 #include "asterisk/say.h"
00059 #include "asterisk/file.h"
00060 #include "asterisk/cli.h"
00061 #include "asterisk/translate.h"
00062 #include "asterisk/manager.h"
00063 #include "asterisk/chanvars.h"
00064 #include "asterisk/linkedlists.h"
00065 #include "asterisk/indications.h"
00066 #include "asterisk/monitor.h"
00067 #include "asterisk/causes.h"
00068 #include "asterisk/callerid.h"
00069 #include "asterisk/utils.h"
00070 #include "asterisk/lock.h"
00071 #include "asterisk/app.h"
00072 #include "asterisk/transcap.h"
00073 #include "asterisk/devicestate.h"
00074
00075 struct channel_spy_trans {
00076 int last_format;
00077 struct ast_trans_pvt *path;
00078 };
00079
00080 struct ast_channel_spy_list {
00081 struct channel_spy_trans read_translator;
00082 struct channel_spy_trans write_translator;
00083 AST_LIST_HEAD_NOLOCK(, ast_channel_spy) list;
00084 };
00085
00086
00087 #if 0
00088 #define MONITOR_CONSTANT_DELAY
00089 #define MONITOR_DELAY 150 * 8
00090 #endif
00091
00092
00093
00094
00095 static int shutting_down = 0;
00096
00097 AST_MUTEX_DEFINE_STATIC(uniquelock);
00098 static int uniqueint = 0;
00099
00100 unsigned long global_fin = 0, global_fout = 0;
00101
00102
00103
00104 struct chanlist {
00105 const struct ast_channel_tech *tech;
00106 struct chanlist *next;
00107 };
00108
00109 static struct chanlist *backends = NULL;
00110
00111
00112
00113
00114 static struct ast_channel *channels = NULL;
00115
00116
00117
00118 AST_MUTEX_DEFINE_STATIC(chlock);
00119
00120 const struct ast_cause {
00121 int cause;
00122 const char *desc;
00123 } causes[] = {
00124 { AST_CAUSE_UNALLOCATED, "Unallocated (unassigned) number" },
00125 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "No route to specified transmit network" },
00126 { AST_CAUSE_NO_ROUTE_DESTINATION, "No route to destination" },
00127 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "Channel unacceptable" },
00128 { AST_CAUSE_CALL_AWARDED_DELIVERED, "Call awarded and being delivered in an established channel" },
00129 { AST_CAUSE_NORMAL_CLEARING, "Normal Clearing" },
00130 { AST_CAUSE_USER_BUSY, "User busy" },
00131 { AST_CAUSE_NO_USER_RESPONSE, "No user responding" },
00132 { AST_CAUSE_NO_ANSWER, "User alerting, no answer" },
00133 { AST_CAUSE_CALL_REJECTED, "Call Rejected" },
00134 { AST_CAUSE_NUMBER_CHANGED, "Number changed" },
00135 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "Destination out of order" },
00136 { AST_CAUSE_INVALID_NUMBER_FORMAT, "Invalid number format" },
00137 { AST_CAUSE_FACILITY_REJECTED, "Facility rejected" },
00138 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "Response to STATus ENQuiry" },
00139 { AST_CAUSE_NORMAL_UNSPECIFIED, "Normal, unspecified" },
00140 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "Circuit/channel congestion" },
00141 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "Network out of order" },
00142 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "Temporary failure" },
00143 { AST_CAUSE_SWITCH_CONGESTION, "Switching equipment congestion" },
00144 { AST_CAUSE_ACCESS_INFO_DISCARDED, "Access information discarded" },
00145 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "Requested channel not available" },
00146 { AST_CAUSE_PRE_EMPTED, "Pre-empted" },
00147 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "Facility not subscribed" },
00148 { AST_CAUSE_OUTGOING_CALL_BARRED, "Outgoing call barred" },
00149 { AST_CAUSE_INCOMING_CALL_BARRED, "Incoming call barred" },
00150 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "Bearer capability not authorized" },
00151 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "Bearer capability not available" },
00152 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "Bearer capability not implemented" },
00153 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "Channel not implemented" },
00154 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "Facility not implemented" },
00155 { AST_CAUSE_INVALID_CALL_REFERENCE, "Invalid call reference value" },
00156 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "Incompatible destination" },
00157 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "Invalid message unspecified" },
00158 { AST_CAUSE_MANDATORY_IE_MISSING, "Mandatory information element is missing" },
00159 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "Message type nonexist." },
00160 { AST_CAUSE_WRONG_MESSAGE, "Wrong message" },
00161 { AST_CAUSE_IE_NONEXIST, "Info. element nonexist or not implemented" },
00162 { AST_CAUSE_INVALID_IE_CONTENTS, "Invalid information element contents" },
00163 { AST_CAUSE_WRONG_CALL_STATE, "Message not compatible with call state" },
00164 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "Recover on timer expiry" },
00165 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "Mandatory IE length error" },
00166 { AST_CAUSE_PROTOCOL_ERROR, "Protocol error, unspecified" },
00167 { AST_CAUSE_INTERWORKING, "Interworking, unspecified" },
00168 };
00169
00170
00171 static int show_channeltypes(int fd, int argc, char *argv[])
00172 {
00173 #define FORMAT "%-10.10s %-30.30s %-12.12s %-12.12s %-12.12s\n"
00174 struct chanlist *cl = backends;
00175 ast_cli(fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
00176 ast_cli(fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
00177 if (ast_mutex_lock(&chlock)) {
00178 ast_log(LOG_WARNING, "Unable to lock channel list\n");
00179 return -1;
00180 }
00181 while (cl) {
00182 ast_cli(fd, FORMAT, cl->tech->type, cl->tech->description,
00183 (cl->tech->devicestate) ? "yes" : "no",
00184 (cl->tech->indicate) ? "yes" : "no",
00185 (cl->tech->transfer) ? "yes" : "no");
00186 cl = cl->next;
00187 }
00188 ast_mutex_unlock(&chlock);
00189 return RESULT_SUCCESS;
00190
00191 #undef FORMAT
00192
00193 }
00194
00195 static char show_channeltypes_usage[] =
00196 "Usage: show channeltypes\n"
00197 " Shows available channel types registered in your Asterisk server.\n";
00198
00199 static struct ast_cli_entry cli_show_channeltypes =
00200 { { "show", "channeltypes", NULL }, show_channeltypes, "Show available channel types", show_channeltypes_usage };
00201
00202
00203 int ast_check_hangup(struct ast_channel *chan)
00204 {
00205 time_t myt;
00206
00207
00208 if (chan->_softhangup)
00209 return 1;
00210
00211 if (!chan->tech_pvt)
00212 return 1;
00213
00214 if (!chan->whentohangup)
00215 return 0;
00216 time(&myt);
00217
00218 if (chan->whentohangup > myt)
00219 return 0;
00220 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
00221 return 1;
00222 }
00223
00224 static int ast_check_hangup_locked(struct ast_channel *chan)
00225 {
00226 int res;
00227 ast_mutex_lock(&chan->lock);
00228 res = ast_check_hangup(chan);
00229 ast_mutex_unlock(&chan->lock);
00230 return res;
00231 }
00232
00233
00234 void ast_begin_shutdown(int hangup)
00235 {
00236 struct ast_channel *c;
00237 shutting_down = 1;
00238 if (hangup) {
00239 ast_mutex_lock(&chlock);
00240 c = channels;
00241 while(c) {
00242 ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
00243 c = c->next;
00244 }
00245 ast_mutex_unlock(&chlock);
00246 }
00247 }
00248
00249
00250 int ast_active_channels(void)
00251 {
00252 struct ast_channel *c;
00253 int cnt = 0;
00254 ast_mutex_lock(&chlock);
00255 c = channels;
00256 while(c) {
00257 cnt++;
00258 c = c->next;
00259 }
00260 ast_mutex_unlock(&chlock);
00261 return cnt;
00262 }
00263
00264
00265 void ast_cancel_shutdown(void)
00266 {
00267 shutting_down = 0;
00268 }
00269
00270
00271 int ast_shutting_down(void)
00272 {
00273 return shutting_down;
00274 }
00275
00276
00277 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
00278 {
00279 time_t myt;
00280 struct ast_frame fr = { AST_FRAME_NULL, };
00281
00282 time(&myt);
00283 if (offset)
00284 chan->whentohangup = myt + offset;
00285 else
00286 chan->whentohangup = 0;
00287 ast_queue_frame(chan, &fr);
00288 return;
00289 }
00290
00291
00292 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
00293 {
00294 time_t whentohangup;
00295
00296 if (chan->whentohangup == 0) {
00297 if (offset == 0)
00298 return (0);
00299 else
00300 return (-1);
00301 } else {
00302 if (offset == 0)
00303 return (1);
00304 else {
00305 whentohangup = offset + time (NULL);
00306 if (chan->whentohangup < whentohangup)
00307 return (1);
00308 else if (chan->whentohangup == whentohangup)
00309 return (0);
00310 else
00311 return (-1);
00312 }
00313 }
00314 }
00315
00316
00317 int ast_channel_register(const struct ast_channel_tech *tech)
00318 {
00319 struct chanlist *chan;
00320
00321 ast_mutex_lock(&chlock);
00322
00323 chan = backends;
00324 while (chan) {
00325 if (!strcasecmp(tech->type, chan->tech->type)) {
00326 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
00327 ast_mutex_unlock(&chlock);
00328 return -1;
00329 }
00330 chan = chan->next;
00331 }
00332
00333 chan = malloc(sizeof(*chan));
00334 if (!chan) {
00335 ast_log(LOG_WARNING, "Out of memory\n");
00336 ast_mutex_unlock(&chlock);
00337 return -1;
00338 }
00339 chan->tech = tech;
00340 chan->next = backends;
00341 backends = chan;
00342
00343 if (option_debug)
00344 ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
00345
00346 if (option_verbose > 1)
00347 ast_verbose(VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->tech->type,
00348 chan->tech->description);
00349
00350 ast_mutex_unlock(&chlock);
00351 return 0;
00352 }
00353
00354 void ast_channel_unregister(const struct ast_channel_tech *tech)
00355 {
00356 struct chanlist *chan, *last=NULL;
00357
00358 if (option_debug)
00359 ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", tech->type);
00360
00361 ast_mutex_lock(&chlock);
00362
00363 chan = backends;
00364 while (chan) {
00365 if (chan->tech == tech) {
00366 if (last)
00367 last->next = chan->next;
00368 else
00369 backends = backends->next;
00370 free(chan);
00371 ast_mutex_unlock(&chlock);
00372
00373 if (option_verbose > 1)
00374 ast_verbose( VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", tech->type);
00375
00376 return;
00377 }
00378 last = chan;
00379 chan = chan->next;
00380 }
00381
00382 ast_mutex_unlock(&chlock);
00383 }
00384
00385 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
00386 {
00387 struct chanlist *chanls;
00388
00389 if (ast_mutex_lock(&chlock)) {
00390 ast_log(LOG_WARNING, "Unable to lock channel tech list\n");
00391 return NULL;
00392 }
00393
00394 for (chanls = backends; chanls; chanls = chanls->next) {
00395 if (strcasecmp(name, chanls->tech->type))
00396 continue;
00397
00398 ast_mutex_unlock(&chlock);
00399 return chanls->tech;
00400 }
00401
00402 ast_mutex_unlock(&chlock);
00403 return NULL;
00404 }
00405
00406
00407 const char *ast_cause2str(int cause)
00408 {
00409 int x;
00410
00411 for (x=0; x < sizeof(causes) / sizeof(causes[0]); x++)
00412 if (causes[x].cause == cause)
00413 return causes[x].desc;
00414
00415 return "Unknown";
00416 }
00417
00418
00419 char *ast_state2str(int state)
00420 {
00421
00422 static char localtmp[256];
00423 switch(state) {
00424 case AST_STATE_DOWN:
00425 return "Down";
00426 case AST_STATE_RESERVED:
00427 return "Rsrvd";
00428 case AST_STATE_OFFHOOK:
00429 return "OffHook";
00430 case AST_STATE_DIALING:
00431 return "Dialing";
00432 case AST_STATE_RING:
00433 return "Ring";
00434 case AST_STATE_RINGING:
00435 return "Ringing";
00436 case AST_STATE_UP:
00437 return "Up";
00438 case AST_STATE_BUSY:
00439 return "Busy";
00440 default:
00441 snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
00442 return localtmp;
00443 }
00444 }
00445
00446
00447 char *ast_transfercapability2str(int transfercapability)
00448 {
00449 switch(transfercapability) {
00450 case AST_TRANS_CAP_SPEECH:
00451 return "SPEECH";
00452 case AST_TRANS_CAP_DIGITAL:
00453 return "DIGITAL";
00454 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
00455 return "RESTRICTED_DIGITAL";
00456 case AST_TRANS_CAP_3_1K_AUDIO:
00457 return "3K1AUDIO";
00458 case AST_TRANS_CAP_DIGITAL_W_TONES:
00459 return "DIGITAL_W_TONES";
00460 case AST_TRANS_CAP_VIDEO:
00461 return "VIDEO";
00462 default:
00463 return "UNKNOWN";
00464 }
00465 }
00466
00467
00468 int ast_best_codec(int fmts)
00469 {
00470
00471
00472 int x;
00473 static int prefs[] =
00474 {
00475
00476 AST_FORMAT_ULAW,
00477
00478 AST_FORMAT_ALAW,
00479
00480 AST_FORMAT_SLINEAR,
00481
00482 AST_FORMAT_G726,
00483
00484 AST_FORMAT_ADPCM,
00485
00486
00487 AST_FORMAT_GSM,
00488
00489 AST_FORMAT_ILBC,
00490
00491 AST_FORMAT_SPEEX,
00492
00493
00494 AST_FORMAT_LPC10,
00495
00496 AST_FORMAT_G729A,
00497
00498 AST_FORMAT_G723_1,
00499 };
00500
00501
00502
00503 for (x=0; x < (sizeof(prefs) / sizeof(prefs[0]) ); x++)
00504 if (fmts & prefs[x])
00505 return prefs[x];
00506 ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
00507 return 0;
00508 }
00509
00510 static const struct ast_channel_tech null_tech = {
00511 .type = "NULL",
00512 .description = "Null channel (should not see this)",
00513 };
00514
00515
00516 struct ast_channel *ast_channel_alloc(int needqueue)
00517 {
00518 struct ast_channel *tmp;
00519 int x;
00520 int flags;
00521 struct varshead *headp;
00522
00523
00524
00525 if (shutting_down) {
00526 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
00527 return NULL;
00528 }
00529
00530 tmp = malloc(sizeof(struct ast_channel));
00531 if (!tmp) {
00532 ast_log(LOG_WARNING, "Channel allocation failed: Out of memory\n");
00533 return NULL;
00534 }
00535
00536 memset(tmp, 0, sizeof(struct ast_channel));
00537 tmp->sched = sched_context_create();
00538 if (!tmp->sched) {
00539 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
00540 free(tmp);
00541 return NULL;
00542 }
00543
00544 for (x=0; x<AST_MAX_FDS - 1; x++)
00545 tmp->fds[x] = -1;
00546
00547 #ifdef ZAPTEL_OPTIMIZATIONS
00548 tmp->timingfd = open("/dev/zap/timer", O_RDWR);
00549 if (tmp->timingfd > -1) {
00550
00551
00552 flags = 1;
00553 if (!ioctl(tmp->timingfd, ZT_TIMERPONG, &flags))
00554 needqueue = 0;
00555 }
00556 #else
00557 tmp->timingfd = -1;
00558 #endif
00559
00560 if (needqueue) {
00561 if (pipe(tmp->alertpipe)) {
00562 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe!\n");
00563 free(tmp);
00564 return NULL;
00565 } else {
00566 flags = fcntl(tmp->alertpipe[0], F_GETFL);
00567 fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
00568 flags = fcntl(tmp->alertpipe[1], F_GETFL);
00569 fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
00570 }
00571 } else
00572
00573 tmp->alertpipe[0] = tmp->alertpipe[1] = -1;
00574
00575
00576 tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
00577
00578 tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
00579 strcpy(tmp->name, "**Unknown**");
00580
00581 tmp->_state = AST_STATE_DOWN;
00582 tmp->streamid = -1;
00583 tmp->appl = NULL;
00584 tmp->data = NULL;
00585 tmp->fin = global_fin;
00586 tmp->fout = global_fout;
00587 ast_mutex_lock(&uniquelock);
00588 snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", (long) time(NULL), uniqueint++);
00589 ast_mutex_unlock(&uniquelock);
00590 headp = &tmp->varshead;
00591 ast_mutex_init(&tmp->lock);
00592 AST_LIST_HEAD_INIT_NOLOCK(headp);
00593 strcpy(tmp->context, "default");
00594 ast_copy_string(tmp->language, defaultlanguage, sizeof(tmp->language));
00595 strcpy(tmp->exten, "s");
00596 tmp->priority = 1;
00597 tmp->amaflags = ast_default_amaflags;
00598 ast_copy_string(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode));
00599
00600 tmp->tech = &null_tech;
00601
00602 ast_mutex_lock(&chlock);
00603 tmp->next = channels;
00604 channels = tmp;
00605
00606 ast_mutex_unlock(&chlock);
00607 return tmp;
00608 }
00609
00610
00611 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
00612 {
00613 struct ast_frame *f;
00614 struct ast_frame *prev, *cur;
00615 int blah = 1;
00616 int qlen = 0;
00617
00618
00619 f = ast_frdup(fin);
00620 if (!f) {
00621 ast_log(LOG_WARNING, "Unable to duplicate frame\n");
00622 return -1;
00623 }
00624 ast_mutex_lock(&chan->lock);
00625 prev = NULL;
00626 cur = chan->readq;
00627 while(cur) {
00628 if ((cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
00629
00630 ast_frfree(f);
00631 ast_mutex_unlock(&chan->lock);
00632 return 0;
00633 }
00634 prev = cur;
00635 cur = cur->next;
00636 qlen++;
00637 }
00638
00639 if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
00640 if (fin->frametype != AST_FRAME_VOICE) {
00641 ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
00642 CRASH;
00643 } else {
00644 ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
00645 ast_frfree(f);
00646 ast_mutex_unlock(&chan->lock);
00647 return 0;
00648 }
00649 }
00650 if (prev)
00651 prev->next = f;
00652 else
00653 chan->readq = f;
00654 if (chan->alertpipe[1] > -1) {
00655 if (write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
00656 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
00657 chan->name, f->frametype, f->subclass, qlen, strerror(errno));
00658 #ifdef ZAPTEL_OPTIMIZATIONS
00659 } else if (chan->timingfd > -1) {
00660 ioctl(chan->timingfd, ZT_TIMERPING, &blah);
00661 #endif
00662 } else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
00663 pthread_kill(chan->blocker, SIGURG);
00664 }
00665 ast_mutex_unlock(&chan->lock);
00666 return 0;
00667 }
00668
00669
00670 int ast_queue_hangup(struct ast_channel *chan)
00671 {
00672 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
00673
00674 if (!ast_mutex_trylock(&chan->lock)) {
00675 chan->_softhangup |= AST_SOFTHANGUP_DEV;
00676 ast_mutex_unlock(&chan->lock);
00677 }
00678 return ast_queue_frame(chan, &f);
00679 }
00680
00681
00682 int ast_queue_control(struct ast_channel *chan, int control)
00683 {
00684 struct ast_frame f = { AST_FRAME_CONTROL, };
00685 f.subclass = control;
00686 return ast_queue_frame(chan, &f);
00687 }
00688
00689
00690 int ast_channel_defer_dtmf(struct ast_channel *chan)
00691 {
00692 int pre = 0;
00693
00694 if (chan) {
00695 pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF);
00696 ast_set_flag(chan, AST_FLAG_DEFER_DTMF);
00697 }
00698 return pre;
00699 }
00700
00701
00702 void ast_channel_undefer_dtmf(struct ast_channel *chan)
00703 {
00704 if (chan)
00705 ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
00706 }
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730 static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
00731 const char *name, const int namelen,
00732 const char *context, const char *exten)
00733 {
00734 const char *msg = prev ? "deadlock" : "initial deadlock";
00735 int retries, done;
00736 struct ast_channel *c;
00737
00738 for (retries = 0; retries < 10; retries++) {
00739 ast_mutex_lock(&chlock);
00740 for (c = channels; c; c = c->next) {
00741 if (!prev) {
00742
00743 if (!name && !exten)
00744 break;
00745 if (name) {
00746
00747 if (!namelen) {
00748 if (!strcasecmp(c->name, name))
00749 break;
00750 else
00751 continue;
00752 }
00753
00754 if (!strncasecmp(c->name, name, namelen))
00755 break;
00756 } else if (exten) {
00757
00758 if (context && (strcasecmp(c->context, context) &&
00759 strcasecmp(c->macrocontext, context)))
00760 continue;
00761
00762 if (strcasecmp(c->exten, exten) &&
00763 strcasecmp(c->macroexten, exten))
00764 continue;
00765 else
00766 break;
00767 }
00768 } else if (c == prev) {
00769 c = c->next;
00770 break;
00771 }
00772 }
00773
00774 done = (c == NULL) || (ast_mutex_trylock(&c->lock) == 0);
00775
00776 if (!done && c)
00777 ast_log(LOG_DEBUG, "Avoiding %s for '%s'\n", msg, c->name);
00778 ast_mutex_unlock(&chlock);
00779 if (done)
00780 return c;
00781 usleep(1);
00782 }
00783
00784
00785
00786
00787 ast_log(LOG_WARNING, "Avoided %s for '%p', %d retries!\n",
00788 msg, c, retries);
00789
00790 return NULL;
00791 }
00792
00793
00794 struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev)
00795 {
00796 return channel_find_locked(prev, NULL, 0, NULL, NULL);
00797 }
00798
00799
00800 struct ast_channel *ast_get_channel_by_name_locked(const char *name)
00801 {
00802 return channel_find_locked(NULL, name, 0, NULL, NULL);
00803 }
00804
00805
00806 struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen)
00807 {
00808 return channel_find_locked(NULL, name, namelen, NULL, NULL);
00809 }
00810
00811
00812 struct ast_channel *ast_walk_channel_by_name_prefix_locked(struct ast_channel *chan, const char *name, const int namelen)
00813 {
00814 return channel_find_locked(chan, name, namelen, NULL, NULL);
00815 }
00816
00817
00818 struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context)
00819 {
00820 return channel_find_locked(NULL, NULL, 0, context, exten);
00821 }
00822
00823
00824 int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
00825 int (*cond)(void*), void *data )
00826 {
00827 struct ast_frame *f;
00828
00829 while(ms > 0) {
00830 if( cond && ((*cond)(data) == 0 ) )
00831 return 0;
00832 ms = ast_waitfor(chan, ms);
00833 if (ms <0)
00834 return -1;
00835 if (ms > 0) {
00836 f = ast_read(chan);
00837 if (!f)
00838 return -1;
00839 ast_frfree(f);
00840 }
00841 }
00842 return 0;
00843 }
00844
00845
00846 int ast_safe_sleep(struct ast_channel *chan, int ms)
00847 {
00848 struct ast_frame *f;
00849 while(ms > 0) {
00850 ms = ast_waitfor(chan, ms);
00851 if (ms <0)
00852 return -1;
00853 if (ms > 0) {
00854 f = ast_read(chan);
00855 if (!f)
00856 return -1;
00857 ast_frfree(f);
00858 }
00859 }
00860 return 0;
00861 }
00862
00863 static void free_cid(struct ast_callerid *cid)
00864 {
00865 if (cid->cid_dnid)
00866 free(cid->cid_dnid);
00867 if (cid->cid_num)
00868 free(cid->cid_num);
00869 if (cid->cid_name)
00870 free(cid->cid_name);
00871 if (cid->cid_ani)
00872 free(cid->cid_ani);
00873 if (cid->cid_rdnis)
00874 free(cid->cid_rdnis);
00875 }
00876
00877
00878 void ast_channel_free(struct ast_channel *chan)
00879 {
00880 struct ast_channel *last=NULL, *cur;
00881 int fd;
00882 struct ast_var_t *vardata;
00883 struct ast_frame *f, *fp;
00884 struct varshead *headp;
00885 char name[AST_CHANNEL_NAME];
00886
00887 headp=&chan->varshead;
00888
00889 ast_mutex_lock(&chlock);
00890 cur = channels;
00891 while(cur) {
00892 if (cur == chan) {
00893 if (last)
00894 last->next = cur->next;
00895 else
00896 channels = cur->next;
00897 break;
00898 }
00899 last = cur;
00900 cur = cur->next;
00901 }
00902 if (!cur)
00903 ast_log(LOG_WARNING, "Unable to find channel in list\n");
00904 else {
00905
00906
00907 ast_mutex_lock(&cur->lock);
00908 ast_mutex_unlock(&cur->lock);
00909 }
00910 if (chan->tech_pvt) {
00911 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
00912 free(chan->tech_pvt);
00913 }
00914
00915 if (chan->sched)
00916 sched_context_destroy(chan->sched);
00917
00918 ast_copy_string(name, chan->name, sizeof(name));
00919
00920
00921 if (chan->monitor) {
00922 chan->monitor->stop( chan, 0 );
00923 }
00924
00925
00926 if(chan->music_state)
00927 ast_moh_cleanup(chan);
00928
00929
00930 if (chan->readtrans)
00931 ast_translator_free_path(chan->readtrans);
00932 if (chan->writetrans)
00933 ast_translator_free_path(chan->writetrans);
00934 if (chan->pbx)
00935 ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
00936 free_cid(&chan->cid);
00937 ast_mutex_destroy(&chan->lock);
00938
00939 if ((fd = chan->alertpipe[0]) > -1)
00940 close(fd);
00941 if ((fd = chan->alertpipe[1]) > -1)
00942 close(fd);
00943 if ((fd = chan->timingfd) > -1)
00944 close(fd);
00945 f = chan->readq;
00946 chan->readq = NULL;
00947 while(f) {
00948 fp = f;
00949 f = f->next;
00950 ast_frfree(fp);
00951 }
00952
00953
00954
00955
00956 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
00957 ast_var_delete(vardata);
00958
00959 free(chan);
00960 ast_mutex_unlock(&chlock);
00961
00962 ast_device_state_changed_literal(name);
00963 }
00964
00965 int ast_channel_spy_add(struct ast_channel *chan, struct ast_channel_spy *spy)
00966 {
00967
00968 spy->chan = chan;
00969
00970 if (!ast_test_flag(spy, CHANSPY_FORMAT_AUDIO)) {
00971 ast_log(LOG_WARNING, "Could not add channel spy '%s' to channel '%s', only audio format spies are supported.\n",
00972 spy->type, chan->name);
00973 return -1;
00974 }
00975
00976 if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST) && (spy->read_queue.format != AST_FORMAT_SLINEAR)) {
00977 ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
00978 ast_getformatname(spy->read_queue.format));
00979 return -1;
00980 }
00981
00982 if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST) && (spy->write_queue.format != AST_FORMAT_SLINEAR)) {
00983 ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
00984 ast_getformatname(spy->write_queue.format));
00985 return -1;
00986 }
00987
00988 if (ast_test_flag(spy, CHANSPY_MIXAUDIO) &&
00989 ((spy->read_queue.format != AST_FORMAT_SLINEAR) ||
00990 (spy->write_queue.format != AST_FORMAT_SLINEAR))) {
00991 ast_log(LOG_WARNING, "Cannot provide audio mixing on '%s'-'%s' format spies\n",
00992 ast_getformatname(spy->read_queue.format), ast_getformatname(spy->write_queue.format));
00993 return -1;
00994 }
00995
00996 if (!chan->spies) {
00997 if (!(chan->spies = calloc(1, sizeof(*chan->spies)))) {
00998 ast_log(LOG_WARNING, "Memory allocation failure\n");
00999 return -1;
01000 }
01001
01002 AST_LIST_HEAD_INIT_NOLOCK(&chan->spies->list);
01003 AST_LIST_INSERT_HEAD(&chan->spies->list, spy, list);
01004 } else {
01005 AST_LIST_INSERT_TAIL(&chan->spies->list, spy, list);
01006 }
01007
01008 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
01009 ast_cond_init(&spy->trigger, NULL);
01010 ast_set_flag(spy, CHANSPY_TRIGGER_READ);
01011 ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
01012 }
01013
01014 ast_log(LOG_DEBUG, "Spy %s added to channel %s\n",
01015 spy->type, chan->name);
01016
01017 return 0;
01018 }
01019
01020
01021 static void spy_cleanup(struct ast_channel *chan)
01022 {
01023 if (AST_LIST_FIRST(&chan->spies->list))
01024 return;
01025 if (chan->spies->read_translator.path)
01026 ast_translator_free_path(chan->spies->read_translator.path);
01027 if (chan->spies->write_translator.path)
01028 ast_translator_free_path(chan->spies->write_translator.path);
01029 free(chan->spies);
01030 chan->spies = NULL;
01031 return;
01032 }
01033
01034
01035 static void spy_detach(struct ast_channel_spy *spy, struct ast_channel *chan)
01036 {
01037 ast_mutex_lock(&spy->lock);
01038
01039
01040 if (spy->status != CHANSPY_DONE) {
01041 spy->status = CHANSPY_STOP;
01042 spy->chan = NULL;
01043 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
01044 ast_cond_signal(&spy->trigger);
01045 }
01046
01047
01048 ast_log(LOG_DEBUG, "Spy %s removed from channel %s\n", spy->type, chan->name);
01049
01050 ast_mutex_unlock(&spy->lock);
01051
01052 return;
01053 }
01054
01055 void ast_channel_spy_stop_by_type(struct ast_channel *chan, const char *type)
01056 {
01057 struct ast_channel_spy *spy = NULL;
01058
01059 if (!chan->spies)
01060 return;
01061
01062 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) {
01063 ast_mutex_lock(&spy->lock);
01064 if ((spy->type == type) && (spy->status == CHANSPY_RUNNING)) {
01065 ast_mutex_unlock(&spy->lock);
01066 AST_LIST_REMOVE_CURRENT(&chan->spies->list, list);
01067 spy_detach(spy, chan);
01068 } else
01069 ast_mutex_unlock(&spy->lock);
01070 }
01071 AST_LIST_TRAVERSE_SAFE_END
01072 spy_cleanup(chan);
01073 }
01074
01075 void ast_channel_spy_trigger_wait(struct ast_channel_spy *spy)
01076 {
01077 struct timeval tv;
01078 struct timespec ts;
01079
01080 tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
01081 ts.tv_sec = tv.tv_sec;
01082 ts.tv_nsec = tv.tv_usec * 1000;
01083
01084 ast_cond_timedwait(&spy->trigger, &spy->lock, &ts);
01085 }
01086
01087 void ast_channel_spy_remove(struct ast_channel *chan, struct ast_channel_spy *spy)
01088 {
01089 if (!chan->spies)
01090 return;
01091
01092 AST_LIST_REMOVE(&chan->spies->list, spy, list);
01093
01094 spy_detach(spy, chan);
01095 spy_cleanup(chan);
01096 }
01097
01098 void ast_channel_spy_free(struct ast_channel_spy *spy)
01099 {
01100 struct ast_frame *f = NULL;
01101
01102 if (spy->status == CHANSPY_DONE)
01103 return;
01104
01105
01106 spy->status = CHANSPY_DONE;
01107
01108
01109 for (f = spy->write_queue.head; f; f = spy->write_queue.head) {
01110 spy->write_queue.head = f->next;
01111 ast_frfree(f);
01112 }
01113 for (f = spy->read_queue.head; f; f= spy->read_queue.head) {
01114 spy->read_queue.head = f->next;
01115 ast_frfree(f);
01116 }
01117
01118
01119 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
01120 ast_cond_destroy(&spy->trigger);
01121
01122
01123 ast_mutex_destroy(&spy->lock);
01124
01125 return;
01126 }
01127
01128 static void detach_spies(struct ast_channel *chan)
01129 {
01130 struct ast_channel_spy *spy = NULL;
01131
01132 if (!chan->spies)
01133 return;
01134
01135 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) {
01136 AST_LIST_REMOVE_CURRENT(&chan->spies->list, list);
01137 spy_detach(spy, chan);
01138 }
01139 AST_LIST_TRAVERSE_SAFE_END
01140
01141 spy_cleanup(chan);
01142 }
01143
01144
01145 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
01146 {
01147 int res = 0;
01148 struct ast_frame f = { AST_FRAME_NULL };
01149 if (option_debug)
01150 ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
01151
01152 chan->_softhangup |= cause;
01153 ast_queue_frame(chan, &f);
01154
01155 if (ast_test_flag(chan, AST_FLAG_BLOCKING))
01156 pthread_kill(chan->blocker, SIGURG);
01157 return res;
01158 }
01159
01160
01161 int ast_softhangup(struct ast_channel *chan, int cause)
01162 {
01163 int res;
01164 ast_mutex_lock(&chan->lock);
01165 res = ast_softhangup_nolock(chan, cause);
01166 ast_mutex_unlock(&chan->lock);
01167 return res;
01168 }
01169
01170 enum spy_direction {
01171 SPY_READ,
01172 SPY_WRITE,
01173 };
01174
01175 #define SPY_QUEUE_SAMPLE_LIMIT 4000
01176
01177 static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f, enum spy_direction dir)
01178 {
01179 struct ast_frame *translated_frame = NULL;
01180 struct ast_channel_spy *spy;
01181 struct ast_channel_spy_queue *queue;
01182 struct channel_spy_trans *trans;
01183 struct ast_frame *last;
01184
01185 trans = (dir == SPY_READ) ? &chan->spies->read_translator : &chan->spies->write_translator;
01186
01187 AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
01188 ast_mutex_lock(&spy->lock);
01189
01190 queue = (dir == SPY_READ) ? &spy->read_queue : &spy->write_queue;
01191
01192 if ((queue->format == AST_FORMAT_SLINEAR) && (f->subclass != AST_FORMAT_SLINEAR)) {
01193 if (!translated_frame) {
01194 if (trans->path && (trans->last_format != f->subclass)) {
01195 ast_translator_free_path(trans->path);
01196 trans->path = NULL;
01197 }
01198 if (!trans->path) {
01199 ast_log(LOG_DEBUG, "Building translator from %s to SLINEAR for spies on channel %s\n",
01200 ast_getformatname(f->subclass), chan->name);
01201 if ((trans->path = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
01202 ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n",
01203 ast_getformatname(f->subclass), ast_getformatname(AST_FORMAT_SLINEAR));
01204 ast_mutex_unlock(&spy->lock);
01205 continue;
01206 } else {
01207 trans->last_format = f->subclass;
01208 }
01209 }
01210 if (!(translated_frame = ast_translate(trans->path, f, 0))) {
01211 ast_log(LOG_ERROR, "Translation to %s failed, dropping frame for spies\n",
01212 ast_getformatname(AST_FORMAT_SLINEAR));
01213 ast_mutex_unlock(&spy->lock);
01214 break;
01215 }
01216 }
01217
01218 for (last = queue->head; last && last->next; last = last->next);
01219 if (last)
01220 last->next = ast_frdup(translated_frame);
01221 else
01222 queue->head = ast_frdup(translated_frame);
01223 } else {
01224 if (f->subclass != queue->format) {
01225 ast_log(LOG_WARNING, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n",
01226 spy->type, chan->name,
01227 ast_getformatname(queue->format), ast_getformatname(f->subclass));
01228 ast_mutex_unlock(&spy->lock);
01229 continue;
01230 }
01231
01232 for (last = queue->head; last && last->next; last = last->next);
01233 if (last)
01234 last->next = ast_frdup(f);
01235 else
01236 queue->head = ast_frdup(f);
01237 }
01238
01239 queue->samples += f->samples;
01240
01241 if (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
01242 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
01243 switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
01244 case CHANSPY_TRIGGER_READ:
01245 if (dir == SPY_WRITE) {
01246 ast_set_flag(spy, CHANSPY_TRIGGER_WRITE);
01247 ast_clear_flag(spy, CHANSPY_TRIGGER_READ);
01248 if (option_debug)
01249 ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to write-trigger mode\n",
01250 spy->type, chan->name);
01251 }
01252 break;
01253 case CHANSPY_TRIGGER_WRITE:
01254 if (dir == SPY_READ) {
01255 ast_set_flag(spy, CHANSPY_TRIGGER_READ);
01256 ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
01257 if (option_debug)
01258 ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to read-trigger mode\n",
01259 spy->type, chan->name);
01260 }
01261 break;
01262 }
01263 if (option_debug)
01264 ast_log(LOG_DEBUG, "Triggering queue flush for spy '%s' on '%s'\n",
01265 spy->type, chan->name);
01266 ast_set_flag(spy, CHANSPY_TRIGGER_FLUSH);
01267 ast_cond_signal(&spy->trigger);
01268 } else {
01269 if (option_debug)
01270 ast_log(LOG_DEBUG, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n",
01271 spy->type, chan->name, (dir == SPY_READ) ? "read" : "write");
01272 while (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
01273 struct ast_frame *drop = queue->head;
01274
01275 queue->samples -= drop->samples;
01276 queue->head = drop->next;
01277 ast_frfree(drop);
01278 }
01279 }
01280 } else {
01281 switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
01282 case CHANSPY_TRIGGER_READ:
01283 if (dir == SPY_READ)
01284 ast_cond_signal(&spy->trigger);
01285 break;
01286 case CHANSPY_TRIGGER_WRITE:
01287 if (dir == SPY_WRITE)
01288 ast_cond_signal(&spy->trigger);
01289 break;
01290 }
01291 }
01292
01293 ast_mutex_unlock(&spy->lock);
01294 }
01295
01296 if (translated_frame)
01297 ast_frfree(translated_frame);
01298 }
01299
01300 static void free_translation(struct ast_channel *clone)
01301 {
01302 if (clone->writetrans)
01303 ast_translator_free_path(clone->writetrans);
01304 if (clone->readtrans)
01305 ast_translator_free_path(clone->readtrans);
01306 clone->writetrans = NULL;
01307 clone->readtrans = NULL;
01308 clone->rawwriteformat = clone->nativeformats;
01309 clone->rawreadformat = clone->nativeformats;
01310 }
01311
01312
01313 int ast_hangup(struct ast_channel *chan)
01314 {
01315 int res = 0;
01316
01317
01318
01319 ast_mutex_lock(&chan->lock);
01320
01321 detach_spies(chan);
01322
01323 if (chan->masq) {
01324 if (ast_do_masquerade(chan))
01325 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
01326 }
01327
01328 if (chan->masq) {
01329 ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
01330 ast_mutex_unlock(&chan->lock);
01331 return 0;
01332 }
01333
01334
01335 if (chan->masqr) {
01336 ast_set_flag(chan, AST_FLAG_ZOMBIE);
01337 ast_mutex_unlock(&chan->lock);
01338 return 0;
01339 }
01340 free_translation(chan);
01341
01342 if (chan->stream) {
01343 ast_closestream(chan->stream);
01344 chan->stream = NULL;
01345 }
01346
01347 if (chan->vstream) {
01348 ast_closestream(chan->vstream);
01349 chan->vstream = NULL;
01350 }
01351 if (chan->sched) {
01352 sched_context_destroy(chan->sched);
01353 chan->sched = NULL;
01354 }
01355
01356 if (chan->generatordata)
01357 chan->generator->release(chan, chan->generatordata);
01358 chan->generatordata = NULL;
01359 chan->generator = NULL;
01360 if (chan->cdr) {
01361 ast_cdr_end(chan->cdr);
01362 ast_cdr_detach(chan->cdr);
01363 chan->cdr = NULL;
01364 }
01365 if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
01366 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
01367 "is blocked by thread %ld in procedure %s! Expect a failure\n",
01368 (long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
01369 CRASH;
01370 }
01371 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE)) {
01372 if (option_debug)
01373 ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
01374 if (chan->tech->hangup)
01375 res = chan->tech->hangup(chan);
01376 } else {
01377 if (option_debug)
01378 ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
01379 }
01380
01381 ast_mutex_unlock(&chan->lock);
01382 manager_event(EVENT_FLAG_CALL, "Hangup",
01383 "Channel: %s\r\n"
01384 "Uniqueid: %s\r\n"
01385 "Cause: %d\r\n"
01386 "Cause-txt: %s\r\n",
01387 chan->name,
01388 chan->uniqueid,
01389 chan->hangupcause,
01390 ast_cause2str(chan->hangupcause)
01391 );
01392 ast_channel_free(chan);
01393 return res;
01394 }
01395
01396 int ast_answer(struct ast_channel *chan)
01397 {
01398 int res = 0;
01399 ast_mutex_lock(&chan->lock);
01400
01401 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
01402 ast_mutex_unlock(&chan->lock);
01403 return -1;
01404 }
01405 switch(chan->_state) {
01406 case AST_STATE_RINGING:
01407 case AST_STATE_RING:
01408 if (chan->tech->answer)
01409 res = chan->tech->answer(chan);
01410 ast_setstate(chan, AST_STATE_UP);
01411 if (chan->cdr)
01412 ast_cdr_answer(chan->cdr);
01413 ast_mutex_unlock(&chan->lock);
01414 return res;
01415 break;
01416 case AST_STATE_UP:
01417 if (chan->cdr)
01418 ast_cdr_answer(chan->cdr);
01419 break;
01420 }
01421 ast_mutex_unlock(&chan->lock);
01422 return 0;
01423 }
01424
01425
01426
01427 void ast_deactivate_generator(struct ast_channel *chan)
01428 {
01429 ast_mutex_lock(&chan->lock);
01430 if (chan->generatordata) {
01431 if (chan->generator && chan->generator->release)
01432 chan->generator->release(chan, chan->generatordata);
01433 chan->generatordata = NULL;
01434 chan->generator = NULL;
01435 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
01436 ast_settimeout(chan, 0, NULL, NULL);
01437 }
01438 ast_mutex_unlock(&chan->lock);
01439 }
01440
01441 static int generator_force(void *data)
01442 {
01443
01444 void *tmp;
01445 int res;
01446 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
01447 struct ast_channel *chan = data;
01448 tmp = chan->generatordata;
01449 chan->generatordata = NULL;
01450 generate = chan->generator->generate;
01451 res = generate(chan, tmp, 0, 160);
01452 chan->generatordata = tmp;
01453 if (res) {
01454 ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
01455 ast_deactivate_generator(chan);
01456 }
01457 return 0;
01458 }
01459
01460 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
01461 {
01462 int res = 0;
01463
01464 ast_mutex_lock(&chan->lock);
01465
01466 if (chan->generatordata) {
01467 if (chan->generator && chan->generator->release)
01468 chan->generator->release(chan, chan->generatordata);
01469 chan->generatordata = NULL;
01470 }
01471
01472 ast_prod(chan);
01473 if (gen->alloc) {
01474 if (!(chan->generatordata = gen->alloc(chan, params)))
01475 res = -1;
01476 }
01477
01478 if (!res) {
01479 ast_settimeout(chan, 160, generator_force, chan);
01480 chan->generator = gen;
01481 }
01482
01483 ast_mutex_unlock(&chan->lock);
01484
01485 return res;
01486 }
01487
01488
01489 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
01490 {
01491 struct timeval start = { 0 , 0 };
01492 int res;
01493 int x, y;
01494 int winner = -1;
01495 int spoint;
01496 struct pollfd *pfds;
01497
01498 pfds = alloca(sizeof(struct pollfd) * n);
01499 if (!pfds) {
01500 ast_log(LOG_ERROR, "Out of memory\n");
01501 return -1;
01502 }
01503 if (*ms > 0)
01504 start = ast_tvnow();
01505 y = 0;
01506 for (x=0; x < n; x++) {
01507 if (fds[x] > -1) {
01508 pfds[y].fd = fds[x];
01509 pfds[y].events = POLLIN | POLLPRI;
01510 y++;
01511 }
01512 }
01513 res = poll(pfds, y, *ms);
01514 if (res < 0) {
01515
01516 if (errno != EINTR)
01517 *ms = -1;
01518 else
01519 *ms = 0;
01520 return -1;
01521 }
01522 spoint = 0;
01523 for (x=0; x < n; x++) {
01524 if (fds[x] > -1) {
01525 if ((res = ast_fdisset(pfds, fds[x], y, &spoint))) {
01526 winner = fds[x];
01527 if (exception) {
01528 if (res & POLLPRI)
01529 *exception = -1;
01530 else
01531 *exception = 0;
01532 }
01533 }
01534 }
01535 }
01536 if (*ms > 0) {
01537 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
01538 if (*ms < 0)
01539 *ms = 0;
01540 }
01541 return winner;
01542 }
01543
01544
01545 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
01546 int *exception, int *outfd, int *ms)
01547 {
01548 struct timeval start = { 0 , 0 };
01549 struct pollfd *pfds;
01550 int res;
01551 long rms;
01552 int x, y, max;
01553 int spoint;
01554 time_t now = 0;
01555 long whentohangup = 0, havewhen = 0, diff;
01556 struct ast_channel *winner = NULL;
01557
01558 pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
01559 if (!pfds) {
01560 ast_log(LOG_ERROR, "Out of memory\n");
01561 *outfd = -1;
01562 return NULL;
01563 }
01564
01565 if (outfd)
01566 *outfd = -99999;
01567 if (exception)
01568 *exception = 0;
01569
01570
01571 for (x=0; x < n; x++) {
01572 ast_mutex_lock(&c[x]->lock);
01573 if (c[x]->whentohangup) {
01574 if (!havewhen)
01575 time(&now);
01576 diff = c[x]->whentohangup - now;
01577 if (!havewhen || (diff < whentohangup)) {
01578 havewhen++;
01579 whentohangup = diff;
01580 }
01581 }
01582 if (c[x]->masq) {
01583 if (ast_do_masquerade(c[x])) {
01584 ast_log(LOG_WARNING, "Masquerade failed\n");
01585 *ms = -1;
01586 ast_mutex_unlock(&c[x]->lock);
01587 return NULL;
01588 }
01589 }
01590 ast_mutex_unlock(&c[x]->lock);
01591 }
01592
01593 rms = *ms;
01594
01595 if (havewhen) {
01596 if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
01597 rms = whentohangup * 1000;
01598 }
01599 }
01600 max = 0;
01601 for (x=0; x < n; x++) {
01602 for (y=0; y< AST_MAX_FDS; y++) {
01603 if (c[x]->fds[y] > -1) {
01604 pfds[max].fd = c[x]->fds[y];
01605 pfds[max].events = POLLIN | POLLPRI;
01606 pfds[max].revents = 0;
01607 max++;
01608 }
01609 }
01610 CHECK_BLOCKING(c[x]);
01611 }
01612 for (x=0; x < nfds; x++) {
01613 if (fds[x] > -1) {
01614 pfds[max].fd = fds[x];
01615 pfds[max].events = POLLIN | POLLPRI;
01616 pfds[max].revents = 0;
01617 max++;
01618 }
01619 }
01620 if (*ms > 0)
01621 start = ast_tvnow();
01622
01623 if (sizeof(int) == 4) {
01624 do {
01625 int kbrms = rms;
01626 if (kbrms > 600000)
01627 kbrms = 600000;
01628 res = poll(pfds, max, kbrms);
01629 if (!res)
01630 rms -= kbrms;
01631 } while (!res && (rms > 0));
01632 } else {
01633 res = poll(pfds, max, rms);
01634 }
01635
01636 if (res < 0) {
01637 for (x=0; x < n; x++)
01638 ast_clear_flag(c[x], AST_FLAG_BLOCKING);
01639
01640 if (errno != EINTR)
01641 *ms = -1;
01642 else {
01643
01644 #if 0
01645 *ms = 0;
01646 #endif
01647 }
01648 return NULL;
01649 } else {
01650
01651
01652
01653 if (res == 0)
01654 *ms = 0;
01655 }
01656
01657 if (havewhen)
01658 time(&now);
01659 spoint = 0;
01660 for (x=0; x < n; x++) {
01661 ast_clear_flag(c[x], AST_FLAG_BLOCKING);
01662 if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
01663 c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
01664 if (!winner)
01665 winner = c[x];
01666 }
01667 for (y=0; y < AST_MAX_FDS; y++) {
01668 if (c[x]->fds[y] > -1) {
01669 if ((res = ast_fdisset(pfds, c[x]->fds[y], max, &spoint))) {
01670 if (res & POLLPRI)
01671 ast_set_flag(c[x], AST_FLAG_EXCEPTION);
01672 else
01673 ast_clear_flag(c[x], AST_FLAG_EXCEPTION);
01674 c[x]->fdno = y;
01675 winner = c[x];
01676 }
01677 }
01678 }
01679 }
01680 for (x=0; x < nfds; x++) {
01681 if (fds[x] > -1) {
01682 if ((res = ast_fdisset(pfds, fds[x], max, &spoint))) {
01683 if (outfd)
01684 *outfd = fds[x];
01685 if (exception) {
01686 if (res & POLLPRI)
01687 *exception = -1;
01688 else
01689 *exception = 0;
01690 }
01691 winner = NULL;
01692 }
01693 }
01694 }
01695 if (*ms > 0) {
01696 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
01697 if (*ms < 0)
01698 *ms = 0;
01699 }
01700 return winner;
01701 }
01702
01703 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
01704 {
01705 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
01706 }
01707
01708 int ast_waitfor(struct ast_channel *c, int ms)
01709 {
01710 struct ast_channel *chan;
01711 int oldms = ms;
01712
01713 chan = ast_waitfor_n(&c, 1, &ms);
01714 if (ms < 0) {
01715 if (oldms < 0)
01716 return 0;
01717 else
01718 return -1;
01719 }
01720 return ms;
01721 }
01722
01723 int ast_waitfordigit(struct ast_channel *c, int ms)
01724 {
01725
01726 struct ast_frame *f;
01727 int result = 0;
01728
01729
01730 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
01731 return -1;
01732
01733
01734 while(ms && !result) {
01735 ms = ast_waitfor(c, ms);
01736 if (ms < 0)
01737 result = -1;
01738 else if (ms > 0) {
01739
01740 f = ast_read(c);
01741 if (f) {
01742 if (f->frametype == AST_FRAME_DTMF)
01743 result = f->subclass;
01744 ast_frfree(f);
01745 } else
01746 result = -1;
01747 }
01748 }
01749 return result;
01750 }
01751
01752 int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
01753 {
01754 int res = -1;
01755 #ifdef ZAPTEL_OPTIMIZATIONS
01756 if (c->timingfd > -1) {
01757 if (!func) {
01758 samples = 0;
01759 data = 0;
01760 }
01761 ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
01762 res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
01763 c->timingfunc = func;
01764 c->timingdata = data;
01765 }
01766 #endif
01767 return res;
01768 }
01769
01770 int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd)
01771 {
01772 struct ast_frame *f;
01773 struct ast_channel *rchan;
01774 int outfd;
01775 int res;
01776
01777
01778 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
01779 return -1;
01780
01781 while(ms) {
01782 errno = 0;
01783 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
01784 if ((!rchan) && (outfd < 0) && (ms)) {
01785 if (errno == 0 || errno == EINTR)
01786 continue;
01787 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
01788 return -1;
01789 } else if (outfd > -1) {
01790
01791 return 1;
01792 } else if (rchan) {
01793 f = ast_read(c);
01794 if(!f) {
01795 return -1;
01796 }
01797
01798 switch(f->frametype) {
01799 case AST_FRAME_DTMF:
01800 res = f->subclass;
01801 ast_frfree(f);
01802 return res;
01803 case AST_FRAME_CONTROL:
01804 switch(f->subclass) {
01805 case AST_CONTROL_HANGUP:
01806 ast_frfree(f);
01807 return -1;
01808 case AST_CONTROL_RINGING:
01809 case AST_CONTROL_ANSWER:
01810
01811 break;
01812 default:
01813 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass);
01814 break;
01815 }
01816 break;
01817 case AST_FRAME_VOICE:
01818
01819 if (audiofd > -1)
01820 write(audiofd, f->data, f->datalen);
01821 }
01822
01823 ast_frfree(f);
01824 }
01825 }
01826 return 0;
01827 }
01828
01829 struct ast_frame *ast_read(struct ast_channel *chan)
01830 {
01831 struct ast_frame *f = NULL;
01832 int blah;
01833 int prestate;
01834 #ifdef ZAPTEL_OPTIMIZATIONS
01835 int (*func)(void *);
01836 void *data;
01837 int res;
01838 #endif
01839 static struct ast_frame null_frame = {
01840 AST_FRAME_NULL,
01841 };
01842
01843 ast_mutex_lock(&chan->lock);
01844 if (chan->masq) {
01845 if (ast_do_masquerade(chan)) {
01846 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
01847 f = NULL;
01848 } else
01849 f = &null_frame;
01850 ast_mutex_unlock(&chan->lock);
01851 return f;
01852 }
01853
01854
01855 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
01856 if (chan->generator)
01857 ast_deactivate_generator(chan);
01858 ast_mutex_unlock(&chan->lock);
01859 return NULL;
01860 }
01861 prestate = chan->_state;
01862
01863 if (!ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && !ast_strlen_zero(chan->dtmfq)) {
01864
01865 chan->dtmff.frametype = AST_FRAME_DTMF;
01866 chan->dtmff.subclass = chan->dtmfq[0];
01867
01868 memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
01869 ast_mutex_unlock(&chan->lock);
01870 return &chan->dtmff;
01871 }
01872
01873
01874
01875 if (chan->alertpipe[0] > -1) {
01876 read(chan->alertpipe[0], &blah, sizeof(blah));
01877 }
01878 #ifdef ZAPTEL_OPTIMIZATIONS
01879 if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
01880 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
01881 blah = -1;
01882
01883 res = ioctl(chan->timingfd, ZT_GETEVENT, &blah);
01884 if (res)
01885 blah = ZT_EVENT_TIMER_EXPIRED;
01886
01887 if (blah == ZT_EVENT_TIMER_PING) {
01888 #if 0
01889 ast_log(LOG_NOTICE, "Oooh, there's a PING!\n");
01890 #endif
01891 if (!chan->readq || !chan->readq->next) {
01892
01893 #if 0
01894 ast_log(LOG_NOTICE, "Sending a PONG!\n");
01895 #endif
01896 if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) {
01897 ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno));
01898 }
01899 }
01900 } else if (blah == ZT_EVENT_TIMER_EXPIRED) {
01901 ioctl(chan->timingfd, ZT_TIMERACK, &blah);
01902 func = chan->timingfunc;
01903 data = chan->timingdata;
01904 ast_mutex_unlock(&chan->lock);
01905 if (func) {
01906 #if 0
01907 ast_log(LOG_DEBUG, "Calling private function\n");
01908 #endif
01909 func(data);
01910 } else {
01911 blah = 0;
01912 ast_mutex_lock(&chan->lock);
01913 ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
01914 chan->timingdata = NULL;
01915 ast_mutex_unlock(&chan->lock);
01916 }
01917 f = &null_frame;
01918 return f;
01919 } else
01920 ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name);
01921 }
01922 #endif
01923
01924 if (chan->readq) {
01925 f = chan->readq;
01926 chan->readq = f->next;
01927
01928 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
01929 ast_frfree(f);
01930 f = NULL;
01931 }
01932 } else {
01933 chan->blocker = pthread_self();
01934 if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
01935 if (chan->tech->exception)
01936 f = chan->tech->exception(chan);
01937 else {
01938 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
01939 f = &null_frame;
01940 }
01941
01942 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
01943 } else {
01944 if (chan->tech->read)
01945 f = chan->tech->read(chan);
01946 else
01947 ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
01948 }
01949 }
01950
01951
01952 if (f && (f->frametype == AST_FRAME_VOICE)) {
01953 if (!(f->subclass & chan->nativeformats)) {
01954
01955
01956 ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n", chan->name, ast_getformatname(f->subclass), ast_getformatname(chan->nativeformats));
01957 ast_frfree(f);
01958 f = &null_frame;
01959 } else {
01960 if (chan->spies)
01961 queue_frame_to_spies(chan, f, SPY_READ);
01962
01963 if (chan->monitor && chan->monitor->read_stream ) {
01964 #ifndef MONITOR_CONSTANT_DELAY
01965 int jump = chan->outsmpl - chan->insmpl - 4 * f->samples;
01966 if (jump >= 0) {
01967 if (ast_seekstream(chan->monitor->read_stream, jump + f->samples, SEEK_FORCECUR) == -1)
01968 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
01969 chan->insmpl += jump + 4 * f->samples;
01970 } else
01971 chan->insmpl+= f->samples;
01972 #else
01973 int jump = chan->outsmpl - chan->insmpl;
01974 if (jump - MONITOR_DELAY >= 0) {
01975 if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
01976 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
01977 chan->insmpl += jump;
01978 } else
01979 chan->insmpl += f->samples;
01980 #endif
01981 if (ast_writestream(chan->monitor->read_stream, f) < 0)
01982 ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
01983 }
01984 if (chan->readtrans) {
01985 f = ast_translate(chan->readtrans, f, 1);
01986 if (!f)
01987 f = &null_frame;
01988 }
01989 }
01990 }
01991
01992
01993 if (!f) {
01994 chan->_softhangup |= AST_SOFTHANGUP_DEV;
01995 if (chan->generator)
01996 ast_deactivate_generator(chan);
01997
01998 if (chan->cdr)
01999 ast_cdr_end(chan->cdr);
02000 } else if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && f->frametype == AST_FRAME_DTMF) {
02001 if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
02002 chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
02003 else
02004 ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
02005 ast_frfree(f);
02006 f = &null_frame;
02007 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
02008 if (prestate == AST_STATE_UP) {
02009 ast_log(LOG_DEBUG, "Dropping duplicate answer!\n");
02010 ast_frfree(f);
02011 f = &null_frame;
02012 }
02013
02014 ast_setstate(chan, AST_STATE_UP);
02015 ast_cdr_answer(chan->cdr);
02016 }
02017
02018
02019 if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
02020
02021
02022 void *tmp;
02023 int res;
02024 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
02025
02026 if (chan->timingfunc) {
02027 ast_log(LOG_DEBUG, "Generator got voice, switching to phase locked mode\n");
02028 ast_settimeout(chan, 0, NULL, NULL);
02029 }
02030 tmp = chan->generatordata;
02031 chan->generatordata = NULL;
02032 generate = chan->generator->generate;
02033 res = generate(chan, tmp, f->datalen, f->samples);
02034 chan->generatordata = tmp;
02035 if (res) {
02036 ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
02037 ast_deactivate_generator(chan);
02038 }
02039 } else if (f && (f->frametype == AST_FRAME_CNG)) {
02040 if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) {
02041 ast_log(LOG_DEBUG, "Generator got CNG, switching to zap timed mode\n");
02042 ast_settimeout(chan, 160, generator_force, chan);
02043 }
02044 }
02045
02046 if (chan->fin & 0x80000000)
02047 ast_frame_dump(chan->name, f, "<<");
02048 if ((chan->fin & 0x7fffffff) == 0x7fffffff)
02049 chan->fin &= 0x80000000;
02050 else
02051 chan->fin++;
02052 ast_mutex_unlock(&chan->lock);
02053 return f;
02054 }
02055
02056 int ast_indicate(struct ast_channel *chan, int condition)
02057 {
02058 int res = -1;
02059
02060 ast_mutex_lock(&chan->lock);
02061
02062 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
02063 ast_mutex_unlock(&chan->lock);
02064 return -1;
02065 }
02066 if (chan->tech->indicate)
02067 res = chan->tech->indicate(chan, condition);
02068 ast_mutex_unlock(&chan->lock);
02069 if (!chan->tech->indicate || res) {
02070
02071
02072
02073
02074 if (condition >= 0) {
02075 const struct tone_zone_sound *ts = NULL;
02076 switch (condition) {
02077 case AST_CONTROL_RINGING:
02078 ts = ast_get_indication_tone(chan->zone, "ring");
02079 break;
02080 case AST_CONTROL_BUSY:
02081 ts = ast_get_indication_tone(chan->zone, "busy");
02082 break;
02083 case AST_CONTROL_CONGESTION:
02084 ts = ast_get_indication_tone(chan->zone, "congestion");
02085 break;
02086 }
02087 if (ts && ts->data[0]) {
02088 ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
02089 ast_playtones_start(chan,0,ts->data, 1);
02090 res = 0;
02091 } else if (condition == AST_CONTROL_PROGRESS) {
02092
02093 } else if (condition == AST_CONTROL_PROCEEDING) {
02094
02095 } else if (condition == AST_CONTROL_HOLD) {
02096
02097 } else if (condition == AST_CONTROL_UNHOLD) {
02098
02099 } else if (condition == AST_CONTROL_VIDUPDATE) {
02100
02101 } else {
02102
02103 ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
02104 res = -1;
02105 }
02106 }
02107 else ast_playtones_stop(chan);
02108 }
02109 return res;
02110 }
02111
02112 int ast_recvchar(struct ast_channel *chan, int timeout)
02113 {
02114 int c;
02115 char *buf = ast_recvtext(chan, timeout);
02116 if (buf == NULL)
02117 return -1;
02118 c = *(unsigned char *)buf;
02119 free(buf);
02120 return c;
02121 }
02122
02123 char *ast_recvtext(struct ast_channel *chan, int timeout)
02124 {
02125 int res, done = 0;
02126 char *buf = NULL;
02127
02128 while (!done) {
02129 struct ast_frame *f;
02130 if (ast_check_hangup(chan))
02131 break;
02132 res = ast_waitfor(chan, timeout);
02133 if (res <= 0)
02134 break;
02135 timeout = res;
02136 f = ast_read(chan);
02137 if (f == NULL)
02138 break;
02139 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP)
02140 done = 1;
02141 else if (f->frametype == AST_FRAME_TEXT) {
02142 buf = strndup((char *) f->data, f->datalen);
02143 done = 1;
02144 }
02145 ast_frfree(f);
02146 }
02147 return buf;
02148 }
02149
02150 int ast_sendtext(struct ast_channel *chan, const char *text)
02151 {
02152 int res = 0;
02153
02154 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
02155 return -1;
02156 CHECK_BLOCKING(chan);
02157 if (chan->tech->send_text)
02158 res = chan->tech->send_text(chan, text);
02159 ast_clear_flag(chan, AST_FLAG_BLOCKING);
02160 return res;
02161 }
02162
02163 static int do_senddigit(struct ast_channel *chan, char digit)
02164 {
02165 int res = -1;
02166
02167 if (chan->tech->send_digit)
02168 res = chan->tech->send_digit(chan, digit);
02169 if (!chan->tech->send_digit || res) {
02170
02171
02172
02173
02174 static const char* dtmf_tones[] = {
02175 "!941+1336/100,!0/100",
02176 "!697+1209/100,!0/100",
02177 "!697+1336/100,!0/100",
02178 "!697+1477/100,!0/100",
02179 "!770+1209/100,!0/100",
02180 "!770+1336/100,!0/100",
02181 "!770+1477/100,!0/100",
02182 "!852+1209/100,!0/100",
02183 "!852+1336/100,!0/100",
02184 "!852+1477/100,!0/100",
02185 "!697+1633/100,!0/100",
02186 "!770+1633/100,!0/100",
02187 "!852+1633/100,!0/100",
02188 "!941+1633/100,!0/100",
02189 "!941+1209/100,!0/100",
02190 "!941+1477/100,!0/100" };
02191 if (digit >= '0' && digit <='9')
02192 ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
02193 else if (digit >= 'A' && digit <= 'D')
02194 ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
02195 else if (digit == '*')
02196 ast_playtones_start(chan, 0, dtmf_tones[14], 0);
02197 else if (digit == '#')
02198 ast_playtones_start(chan, 0, dtmf_tones[15], 0);
02199 else {
02200
02201 ast_log(LOG_DEBUG, "Unable to generate DTMF tone '%c' for '%s'\n", digit, chan->name);
02202 }
02203 }
02204 return 0;
02205 }
02206
02207 int ast_senddigit(struct ast_channel *chan, char digit)
02208 {
02209 return do_senddigit(chan, digit);
02210 }
02211
02212 int ast_prod(struct ast_channel *chan)
02213 {
02214 struct ast_frame a = { AST_FRAME_VOICE };
02215 char nothing[128];
02216
02217
02218 if (chan->_state != AST_STATE_UP) {
02219 ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
02220 a.subclass = chan->rawwriteformat;
02221 a.data = nothing + AST_FRIENDLY_OFFSET;
02222 a.src = "ast_prod";
02223 if (ast_write(chan, &a))
02224 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
02225 }
02226 return 0;
02227 }
02228
02229 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
02230 {
02231 int res;
02232 if (!chan->tech->write_video)
02233 return 0;
02234 res = ast_write(chan, fr);
02235 if (!res)
02236 res = 1;
02237 return res;
02238 }
02239
02240 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
02241 {
02242 int res = -1;
02243 struct ast_frame *f = NULL;
02244
02245 ast_mutex_lock(&chan->lock);
02246 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
02247 ast_mutex_unlock(&chan->lock);
02248 return -1;
02249 }
02250
02251 if (chan->masq) {
02252 if (ast_do_masquerade(chan)) {
02253 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
02254 ast_mutex_unlock(&chan->lock);
02255 return -1;
02256 }
02257 }
02258 if (chan->masqr) {
02259 ast_mutex_unlock(&chan->lock);
02260 return 0;
02261 }
02262 if (chan->generatordata) {
02263 if (ast_test_flag(chan, AST_FLAG_WRITE_INT))
02264 ast_deactivate_generator(chan);
02265 else {
02266 ast_mutex_unlock(&chan->lock);
02267 return 0;
02268 }
02269 }
02270
02271 if (chan->fout & 0x80000000)
02272 ast_frame_dump(chan->name, fr, ">>");
02273 CHECK_BLOCKING(chan);
02274 switch(fr->frametype) {
02275 case AST_FRAME_CONTROL:
02276
02277 ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
02278 break;
02279 case AST_FRAME_DTMF:
02280 ast_clear_flag(chan, AST_FLAG_BLOCKING);
02281 ast_mutex_unlock(&chan->lock);
02282 res = do_senddigit(chan,fr->subclass);
02283 ast_mutex_lock(&chan->lock);
02284 CHECK_BLOCKING(chan);
02285 break;
02286 case AST_FRAME_TEXT:
02287 if (chan->tech->send_text)
02288 res = chan->tech->send_text(chan, (char *) fr->data);
02289 else
02290 res = 0;
02291 break;
02292 case AST_FRAME_HTML:
02293 if (chan->tech->send_html)
02294 res = chan->tech->send_html(chan, fr->subclass, (char *) fr->data, fr->datalen);
02295 else
02296 res = 0;
02297 break;
02298 case AST_FRAME_VIDEO:
02299
02300 if (chan->tech->write_video)
02301 res = chan->tech->write_video(chan, fr);
02302 else
02303 res = 0;
02304 break;
02305 default:
02306 if (chan->tech->write) {
02307 f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
02308 if (f) {
02309 if (f->frametype == AST_FRAME_VOICE && chan->spies)
02310 queue_frame_to_spies(chan, f, SPY_WRITE);
02311
02312 if( chan->monitor && chan->monitor->write_stream &&
02313 f && ( f->frametype == AST_FRAME_VOICE ) ) {
02314 #ifndef MONITOR_CONSTANT_DELAY
02315 int jump = chan->insmpl - chan->outsmpl - 4 * f->samples;
02316 if (jump >= 0) {
02317 if (ast_seekstream(chan->monitor->write_stream, jump + f->samples, SEEK_FORCECUR) == -1)
02318 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
02319 chan->outsmpl += jump + 4 * f->samples;
02320 } else
02321 chan->outsmpl += f->samples;
02322 #else
02323 int jump = chan->insmpl - chan->outsmpl;
02324 if (jump - MONITOR_DELAY >= 0) {
02325 if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1)
02326 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
02327 chan->outsmpl += jump;
02328 } else
02329 chan->outsmpl += f->samples;
02330 #endif
02331 if (ast_writestream(chan->monitor->write_stream, f) < 0)
02332 ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
02333 }
02334
02335 res = chan->tech->write(chan, f);
02336 } else
02337 res = 0;
02338 }
02339 }
02340
02341
02342 if (f && f->frametype == AST_FRAME_DTMF) {
02343 ast_log(LOG_DTMF, "%s : %c\n", chan->name, f->subclass);
02344 } else if (fr->frametype == AST_FRAME_DTMF) {
02345 ast_log(LOG_DTMF, "%s : %c\n", chan->name, fr->subclass);
02346 }
02347
02348 if (f && (f != fr))
02349 ast_frfree(f);
02350 ast_clear_flag(chan, AST_FLAG_BLOCKING);
02351
02352 if (res < 0)
02353 chan->_softhangup |= AST_SOFTHANGUP_DEV;
02354 else {
02355 if ((chan->fout & 0x7fffffff) == 0x7fffffff)
02356 chan->fout &= 0x80000000;
02357 else
02358 chan->fout++;
02359 }
02360 ast_mutex_unlock(&chan->lock);
02361 return res;
02362 }
02363
02364 static int set_format(struct ast_channel *chan, int fmt, int *rawformat, int *format,
02365 struct ast_trans_pvt **trans, const int direction)
02366 {
02367 int native;
02368 int res;
02369
02370 native = chan->nativeformats;
02371
02372 if (!direction)
02373
02374 res = ast_translator_best_choice(&fmt, &native);
02375 else
02376
02377 res = ast_translator_best_choice(&native, &fmt);
02378
02379 if (res < 0) {
02380 ast_log(LOG_WARNING, "Unable to find a codec translation path from %s to %s\n",
02381 ast_getformatname(native), ast_getformatname(fmt));
02382 return -1;
02383 }
02384
02385
02386 ast_mutex_lock(&chan->lock);
02387
02388 if ((*rawformat == native) && (*format == fmt) && ((*rawformat == *format) || (*trans))) {
02389
02390 ast_mutex_unlock(&chan->lock);
02391 return 0;
02392 }
02393
02394 *rawformat = native;
02395
02396 *format = fmt;
02397
02398 if (*trans)
02399 ast_translator_free_path(*trans);
02400
02401 if (!direction)
02402
02403 *trans = ast_translator_build_path(*format, *rawformat);
02404 else
02405
02406 *trans = ast_translator_build_path(*rawformat, *format);
02407 ast_mutex_unlock(&chan->lock);
02408 if (option_debug)
02409 ast_log(LOG_DEBUG, "Set channel %s to %s format %s\n", chan->name,
02410 direction ? "write" : "read", ast_getformatname(fmt));
02411 return 0;
02412 }
02413
02414 int ast_set_read_format(struct ast_channel *chan, int fmt)
02415 {
02416 return set_format(chan, fmt, &chan->rawreadformat, &chan->readformat,
02417 &chan->readtrans, 0);
02418 }
02419
02420 int ast_set_write_format(struct ast_channel *chan, int fmt)
02421 {
02422 return set_format(chan, fmt, &chan->rawwriteformat, &chan->writeformat,
02423 &chan->writetrans, 1);
02424 }
02425
02426 struct ast_channel *__ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
02427 {
02428 int state = 0;
02429 int cause = 0;
02430 struct ast_channel *chan;
02431 struct ast_frame *f;
02432 int res = 0;
02433
02434 chan = ast_request(type, format, data, &cause);
02435 if (chan) {
02436 if (oh) {
02437 if (oh->vars)
02438 ast_set_variables(chan, oh->vars);
02439 if (oh->cid_num && *oh->cid_num && oh->cid_name && *oh->cid_name)
02440 ast_set_callerid(chan, oh->cid_num, oh->cid_name, oh->cid_num);
02441 if (oh->parent_channel)
02442 ast_channel_inherit_variables(oh->parent_channel, chan);
02443 if (oh->account)
02444 ast_cdr_setaccount(chan, oh->account);
02445 }
02446 ast_set_callerid(chan, cid_num, cid_name, cid_num);
02447
02448 if (!ast_call(chan, data, 0)) {
02449 res = 1;
02450 while (timeout && (chan->_state != AST_STATE_UP)) {
02451 res = ast_waitfor(chan, timeout);
02452 if (res < 0) {
02453
02454 break;
02455 }
02456
02457 if (!res)
02458 break;
02459 if (timeout > -1)
02460 timeout = res;
02461 f = ast_read(chan);
02462 if (!f) {
02463 state = AST_CONTROL_HANGUP;
02464 res = 0;
02465 break;
02466 }
02467 if (f->frametype == AST_FRAME_CONTROL) {
02468 if (f->subclass == AST_CONTROL_RINGING)
02469 state = AST_CONTROL_RINGING;
02470 else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
02471 state = f->subclass;
02472 ast_frfree(f);
02473 break;
02474 } else if (f->subclass == AST_CONTROL_ANSWER) {
02475 state = f->subclass;
02476 ast_frfree(f);
02477 break;
02478 } else if (f->subclass == AST_CONTROL_PROGRESS) {
02479
02480 } else if (f->subclass == -1) {
02481
02482 } else {
02483 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
02484 }
02485 }
02486 ast_frfree(f);
02487 }
02488 } else
02489 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
02490 } else {
02491 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
02492 switch(cause) {
02493 case AST_CAUSE_BUSY:
02494 state = AST_CONTROL_BUSY;
02495 break;
02496 case AST_CAUSE_CONGESTION:
02497 state = AST_CONTROL_CONGESTION;
02498 break;
02499 }
02500 }
02501 if (chan) {
02502
02503 if (oh) {
02504 if (oh->context && *oh->context)
02505 ast_copy_string(chan->context, oh->context, sizeof(chan->context));
02506 if (oh->exten && *oh->exten)
02507 ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten));
02508 if (oh->priority)
02509 chan->priority = oh->priority;
02510 }
02511 if (chan->_state == AST_STATE_UP)
02512 state = AST_CONTROL_ANSWER;
02513 }
02514 if (outstate)
02515 *outstate = state;
02516 if (chan && res <= 0) {
02517 if (!chan->cdr) {
02518 chan->cdr = ast_cdr_alloc();
02519 if (chan->cdr)
02520 ast_cdr_init(chan->cdr, chan);
02521 }
02522 if (chan->cdr) {
02523 char tmp[256];
02524 snprintf(tmp, 256, "%s/%s", type, (char *)data);
02525 ast_cdr_setapp(chan->cdr,"Dial",tmp);
02526 ast_cdr_update(chan);
02527 ast_cdr_start(chan->cdr);
02528 ast_cdr_end(chan->cdr);
02529
02530 if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
02531 ast_cdr_failed(chan->cdr);
02532 } else
02533 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
02534 ast_hangup(chan);
02535 chan = NULL;
02536 }
02537 return chan;
02538 }
02539
02540 struct ast_channel *ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname)
02541 {
02542 return __ast_request_and_dial(type, format, data, timeout, outstate, cidnum, cidname, NULL);
02543 }
02544
02545 struct ast_channel *ast_request(const char *type, int format, void *data, int *cause)
02546 {
02547 struct chanlist *chan;
02548 struct ast_channel *c;
02549 int capabilities;
02550 int fmt;
02551 int res;
02552 int foo;
02553
02554 if (!cause)
02555 cause = &foo;
02556 *cause = AST_CAUSE_NOTDEFINED;
02557
02558 if (ast_mutex_lock(&chlock)) {
02559 ast_log(LOG_WARNING, "Unable to lock channel list\n");
02560 return NULL;
02561 }
02562
02563 for (chan = backends; chan; chan = chan->next) {
02564 if (strcasecmp(type, chan->tech->type))
02565 continue;
02566
02567 capabilities = chan->tech->capabilities;
02568 fmt = format;
02569 res = ast_translator_best_choice(&fmt, &capabilities);
02570 if (res < 0) {
02571 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->tech->capabilities, format);
02572 ast_mutex_unlock(&chlock);
02573 return NULL;
02574 }
02575 ast_mutex_unlock(&chlock);
02576 if (!chan->tech->requester)
02577 return NULL;
02578
02579 if (!(c = chan->tech->requester(type, capabilities, data, cause)))
02580 return NULL;
02581
02582 if (c->_state == AST_STATE_DOWN) {
02583 manager_event(EVENT_FLAG_CALL, "Newchannel",
02584 "Channel: %s\r\n"
02585 "State: %s\r\n"
02586 "CallerID: %s\r\n"
02587 "CallerIDName: %s\r\n"
02588 "Uniqueid: %s\r\n",
02589 c->name, ast_state2str(c->_state),
02590 c->cid.cid_num ? c->cid.cid_num : "<unknown>",
02591 c->cid.cid_name ? c->cid.cid_name : "<unknown>",
02592 c->uniqueid);
02593 }
02594 return c;
02595 }
02596
02597 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
02598 *cause = AST_CAUSE_NOSUCHDRIVER;
02599 ast_mutex_unlock(&chlock);
02600
02601 return NULL;
02602 }
02603
02604 int ast_call(struct ast_channel *chan, char *addr, int timeout)
02605 {
02606
02607
02608
02609 int res = -1;
02610
02611 ast_mutex_lock(&chan->lock);
02612 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan))
02613 if (chan->tech->call)
02614 res = chan->tech->call(chan, addr, timeout);
02615 ast_mutex_unlock(&chan->lock);
02616 return res;
02617 }
02618
02619
02620
02621 int ast_transfer(struct ast_channel *chan, char *dest)
02622 {
02623 int res = -1;
02624
02625
02626 ast_mutex_lock(&chan->lock);
02627 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
02628 if (chan->tech->transfer) {
02629 res = chan->tech->transfer(chan, dest);
02630 if (!res)
02631 res = 1;
02632 } else
02633 res = 0;
02634 }
02635 ast_mutex_unlock(&chan->lock);
02636 return res;
02637 }
02638
02639 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
02640 {
02641 int pos=0;
02642 int to = ftimeout;
02643 int d;
02644
02645
02646
02647 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
02648 return -1;
02649 if (!len)
02650 return -1;
02651 do {
02652 if (c->stream) {
02653 d = ast_waitstream(c, AST_DIGIT_ANY);
02654 ast_stopstream(c);
02655 usleep(1000);
02656 if (!d)
02657 d = ast_waitfordigit(c, to);
02658 } else {
02659 d = ast_waitfordigit(c, to);
02660 }
02661 if (d < 0)
02662 return -1;
02663 if (d == 0) {
02664 s[pos]='\0';
02665 return 1;
02666 }
02667 if (!strchr(enders, d))
02668 s[pos++] = d;
02669 if (strchr(enders, d) || (pos >= len)) {
02670 s[pos]='\0';
02671 return 0;
02672 }
02673 to = timeout;
02674 } while(1);
02675
02676 return 0;
02677 }
02678
02679 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
02680 {
02681 int pos=0;
02682 int to = ftimeout;
02683 int d;
02684
02685
02686 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
02687 return -1;
02688 if (!len)
02689 return -1;
02690 do {
02691 if (c->stream) {
02692 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
02693 ast_stopstream(c);
02694 usleep(1000);
02695 if (!d)
02696 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
02697 } else {
02698 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
02699 }
02700 if (d < 0)
02701 return -1;
02702 if (d == 0) {
02703 s[pos]='\0';
02704 return 1;
02705 }
02706 if (d == 1) {
02707 s[pos]='\0';
02708 return 2;
02709 }
02710 if (!strchr(enders, d))
02711 s[pos++] = d;
02712 if (strchr(enders, d) || (pos >= len)) {
02713 s[pos]='\0';
02714 return 0;
02715 }
02716 to = timeout;
02717 } while(1);
02718
02719 return 0;
02720 }
02721
02722 int ast_channel_supports_html(struct ast_channel *chan)
02723 {
02724 if (chan->tech->send_html)
02725 return 1;
02726 return 0;
02727 }
02728
02729 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
02730 {
02731 if (chan->tech->send_html)
02732 return chan->tech->send_html(chan, subclass, data, datalen);
02733 return -1;
02734 }
02735
02736 int ast_channel_sendurl(struct ast_channel *chan, const char *url)
02737 {
02738 if (chan->tech->send_html)
02739 return chan->tech->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
02740 return -1;
02741 }
02742
02743 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
02744 {
02745 int src;
02746 int dst;
02747
02748
02749 src = chan->nativeformats;
02750 dst = peer->nativeformats;
02751 if (ast_translator_best_choice(&dst, &src) < 0) {
02752 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, src, peer->name, dst);
02753 return -1;
02754 }
02755
02756
02757
02758
02759 if ((src != dst) && option_transcode_slin)
02760 dst = AST_FORMAT_SLINEAR;
02761 if (ast_set_read_format(chan, dst) < 0) {
02762 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, dst);
02763 return -1;
02764 }
02765 if (ast_set_write_format(peer, dst) < 0) {
02766 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, dst);
02767 return -1;
02768 }
02769
02770
02771 src = peer->nativeformats;
02772 dst = chan->nativeformats;
02773 if (ast_translator_best_choice(&dst, &src) < 0) {
02774 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, src, chan->name, dst);
02775 return -1;
02776 }
02777
02778
02779
02780 if ((src != dst) && option_transcode_slin)
02781 dst = AST_FORMAT_SLINEAR;
02782 if (ast_set_read_format(peer, dst) < 0) {
02783 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, dst);
02784 return -1;
02785 }
02786 if (ast_set_write_format(chan, dst) < 0) {
02787 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, dst);
02788 return -1;
02789 }
02790 return 0;
02791 }
02792
02793 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
02794 {
02795 struct ast_frame null = { AST_FRAME_NULL, };
02796 int res = -1;
02797 struct ast_channel *final_orig = original, *final_clone = clone;
02798
02799 ast_mutex_lock(&original->lock);
02800 while(ast_mutex_trylock(&clone->lock)) {
02801 ast_mutex_unlock(&original->lock);
02802 usleep(1);
02803 ast_mutex_lock(&original->lock);
02804 }
02805
02806
02807
02808 if (original->_bridge && (original->_bridge != ast_bridged_channel(original)))
02809 final_orig = original->_bridge;
02810
02811 if (clone->_bridge && (clone->_bridge != ast_bridged_channel(clone)))
02812 final_clone = clone->_bridge;
02813
02814 if ((final_orig != original) || (final_clone != clone)) {
02815 ast_mutex_lock(&final_orig->lock);
02816 while(ast_mutex_trylock(&final_clone->lock)) {
02817 ast_mutex_unlock(&final_orig->lock);
02818 usleep(1);
02819 ast_mutex_lock(&final_orig->lock);
02820 }
02821 ast_mutex_unlock(&clone->lock);
02822 ast_mutex_unlock(&original->lock);
02823 original = final_orig;
02824 clone = final_clone;
02825 }
02826
02827 if (original == clone) {
02828 ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name);
02829 ast_mutex_unlock(&clone->lock);
02830 ast_mutex_unlock(&original->lock);
02831 return -1;
02832 }
02833
02834 ast_log(LOG_DEBUG, "Planning to masquerade channel %s into the structure of %s\n",
02835 clone->name, original->name);
02836 if (original->masq) {
02837 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
02838 original->masq->name, original->name);
02839 } else if (clone->masqr) {
02840 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
02841 clone->name, clone->masqr->name);
02842 } else {
02843 original->masq = clone;
02844 clone->masqr = original;
02845 ast_queue_frame(original, &null);
02846 ast_queue_frame(clone, &null);
02847 ast_log(LOG_DEBUG, "Done planning to masquerade channel %s into the structure of %s\n", clone->name, original->name);
02848 res = 0;
02849 }
02850
02851 ast_mutex_unlock(&clone->lock);
02852 ast_mutex_unlock(&original->lock);
02853
02854 return res;
02855 }
02856
02857 void ast_change_name(struct ast_channel *chan, char *newname)
02858 {
02859 char tmp[256];
02860 ast_copy_string(tmp, chan->name, sizeof(tmp));
02861 ast_copy_string(chan->name, newname, sizeof(chan->name));
02862 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
02863 }
02864
02865 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
02866 {
02867 struct ast_var_t *current, *newvar;
02868 char *varname;
02869
02870 AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
02871 int vartype = 0;
02872
02873 varname = ast_var_full_name(current);
02874 if (!varname)
02875 continue;
02876
02877 if (varname[0] == '_') {
02878 vartype = 1;
02879 if (varname[1] == '_')
02880 vartype = 2;
02881 }
02882
02883 switch (vartype) {
02884 case 1:
02885 newvar = ast_var_assign(&varname[1], ast_var_value(current));
02886 if (newvar) {
02887 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
02888 if (option_debug)
02889 ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", ast_var_name(newvar));
02890 }
02891 break;
02892 case 2:
02893 newvar = ast_var_assign(ast_var_full_name(current), ast_var_value(current));
02894 if (newvar) {
02895 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
02896 if (option_debug)
02897 ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", ast_var_name(newvar));
02898 }
02899 break;
02900 default:
02901 if (option_debug)
02902 ast_log(LOG_DEBUG, "Not copying variable %s.\n", ast_var_name(current));
02903 break;
02904 }
02905 }
02906 }
02907
02908
02909
02910
02911
02912
02913
02914
02915 static void clone_variables(struct ast_channel *original, struct ast_channel *clone)
02916 {
02917 struct ast_var_t *varptr;
02918
02919
02920
02921
02922
02923
02924
02925 AST_LIST_TRAVERSE_SAFE_BEGIN(&original->varshead, varptr, entries) {
02926 if (!strncmp(ast_var_name(varptr), GROUP_CATEGORY_PREFIX, strlen(GROUP_CATEGORY_PREFIX))) {
02927 AST_LIST_REMOVE_CURRENT(&original->varshead, entries);
02928 ast_var_delete(varptr);
02929 }
02930 }
02931 AST_LIST_TRAVERSE_SAFE_END;
02932
02933
02934
02935 if (AST_LIST_FIRST(&clone->varshead))
02936 AST_LIST_APPEND_LIST(&original->varshead, &clone->varshead, entries);
02937 }
02938
02939
02940
02941 int ast_do_masquerade(struct ast_channel *original)
02942 {
02943 int x,i;
02944 int res=0;
02945 int origstate;
02946 struct ast_frame *cur, *prev;
02947 const struct ast_channel_tech *t;
02948 void *t_pvt;
02949 struct ast_callerid tmpcid;
02950 struct ast_channel *clone = original->masq;
02951 struct ast_channel_spy_list *spy_list;
02952 struct ast_channel_spy *spy = NULL;
02953 int rformat = original->readformat;
02954 int wformat = original->writeformat;
02955 char newn[100];
02956 char orig[100];
02957 char masqn[100];
02958 char zombn[100];
02959
02960 if (option_debug > 3)
02961 ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
02962 clone->name, clone->_state, original->name, original->_state);
02963
02964
02965
02966
02967
02968
02969
02970 ast_mutex_lock(&clone->lock);
02971
02972 ast_log(LOG_DEBUG, "Got clone lock for masquerade on '%s' at %p\n", clone->name, &clone->lock);
02973
02974
02975
02976 free_translation(clone);
02977 free_translation(original);
02978
02979
02980
02981 original->masq = NULL;
02982 clone->masqr = NULL;
02983
02984
02985 ast_copy_string(orig, original->name, sizeof(orig));
02986
02987 ast_copy_string(newn, clone->name, sizeof(newn));
02988
02989 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
02990
02991
02992 ast_copy_string(original->name, newn, sizeof(original->name));
02993
02994
02995 ast_copy_string(clone->name, masqn, sizeof(clone->name));
02996
02997
02998 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
02999 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig, newn, original->uniqueid);
03000
03001
03002 t = original->tech;
03003 original->tech = clone->tech;
03004 clone->tech = t;
03005
03006 t_pvt = original->tech_pvt;
03007 original->tech_pvt = clone->tech_pvt;
03008 clone->tech_pvt = t_pvt;
03009
03010
03011 cur = original->readq;
03012 original->readq = clone->readq;
03013 clone->readq = cur;
03014
03015
03016 for (i = 0; i < 2; i++) {
03017 x = original->alertpipe[i];
03018 original->alertpipe[i] = clone->alertpipe[i];
03019 clone->alertpipe[i] = x;
03020 }
03021
03022
03023 x = original->rawreadformat;
03024 original->rawreadformat = clone->rawreadformat;
03025 clone->rawreadformat = x;
03026 x = original->rawwriteformat;
03027 original->rawwriteformat = clone->rawwriteformat;
03028 clone->rawwriteformat = x;
03029
03030
03031 spy_list = original->spies;
03032 original->spies = clone->spies;
03033 clone->spies = spy_list;
03034
03035
03036 if (original->spies) {
03037 AST_LIST_TRAVERSE(&original->spies->list, spy, list) {
03038 ast_mutex_lock(&spy->lock);
03039 spy->chan = original;
03040 ast_mutex_unlock(&spy->lock);
03041 }
03042 }
03043 if (clone->spies) {
03044 AST_LIST_TRAVERSE(&clone->spies->list, spy, list) {
03045 ast_mutex_lock(&spy->lock);
03046 spy->chan = clone;
03047 ast_mutex_unlock(&spy->lock);
03048 }
03049 }
03050
03051
03052
03053 prev = NULL;
03054 cur = clone->readq;
03055 x = 0;
03056 while(cur) {
03057 x++;
03058 prev = cur;
03059 cur = cur->next;
03060 }
03061
03062
03063 if (prev) {
03064 prev->next = original->readq;
03065 original->readq = clone->readq;
03066 clone->readq = NULL;
03067 if (original->alertpipe[1] > -1) {
03068 for (i = 0; i < x; i++)
03069 write(original->alertpipe[1], &x, sizeof(x));
03070 }
03071 }
03072 clone->_softhangup = AST_SOFTHANGUP_DEV;
03073
03074
03075
03076
03077
03078
03079 origstate = original->_state;
03080 original->_state = clone->_state;
03081 clone->_state = origstate;
03082
03083 if (clone->tech->fixup){
03084 res = clone->tech->fixup(original, clone);
03085 if (res)
03086 ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
03087 }
03088
03089
03090 if (clone->tech->hangup)
03091 res = clone->tech->hangup(clone);
03092 if (res) {
03093 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
03094 ast_mutex_unlock(&clone->lock);
03095 return -1;
03096 }
03097
03098 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
03099
03100 ast_copy_string(clone->name, zombn, sizeof(clone->name));
03101 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
03102
03103
03104 original->type = clone->type;
03105 t_pvt = original->monitor;
03106 original->monitor = clone->monitor;
03107 clone->monitor = t_pvt;
03108
03109
03110 ast_copy_string(original->language, clone->language, sizeof(original->language));
03111
03112 for (x = 0; x < AST_MAX_FDS; x++) {
03113 original->fds[x] = clone->fds[x];
03114 }
03115 clone_variables(original, clone);
03116 AST_LIST_HEAD_INIT_NOLOCK(&clone->varshead);
03117
03118 original->adsicpe = clone->adsicpe;
03119
03120
03121
03122
03123
03124 ast_copy_flags(original, clone, AST_FLAG_EXCEPTION);
03125 original->fdno = clone->fdno;
03126
03127
03128
03129
03130
03131
03132 tmpcid = original->cid;
03133 original->cid = clone->cid;
03134 clone->cid = tmpcid;
03135
03136
03137 original->fds[AST_MAX_FDS - 2] = original->timingfd;
03138
03139
03140 original->nativeformats = clone->nativeformats;
03141
03142
03143
03144
03145
03146 ast_set_write_format(original, wformat);
03147
03148
03149 ast_set_read_format(original, rformat);
03150
03151
03152 ast_copy_string(original->musicclass, clone->musicclass, sizeof(original->musicclass));
03153
03154 ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
03155
03156
03157
03158 if (original->tech->fixup) {
03159 res = original->tech->fixup(clone, original);
03160 if (res) {
03161 ast_log(LOG_WARNING, "Channel for type '%s' could not fixup channel %s\n",
03162 original->type, original->name);
03163 ast_mutex_unlock(&clone->lock);
03164 return -1;
03165 }
03166 } else
03167 ast_log(LOG_WARNING, "Channel type '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
03168 original->type, original->name);
03169
03170
03171
03172
03173 if (ast_test_flag(clone, AST_FLAG_ZOMBIE)) {
03174 ast_log(LOG_DEBUG, "Destroying channel clone '%s'\n", clone->name);
03175 ast_mutex_unlock(&clone->lock);
03176 manager_event(EVENT_FLAG_CALL, "Hangup",
03177 "Channel: %s\r\n"
03178 "Uniqueid: %s\r\n"
03179 "Cause: %d\r\n"
03180 "Cause-txt: %s\r\n",
03181 clone->name,
03182 clone->uniqueid,
03183 clone->hangupcause,
03184 ast_cause2str(clone->hangupcause)
03185 );
03186 ast_channel_free(clone);
03187 } else {
03188 struct ast_frame null_frame = { AST_FRAME_NULL, };
03189 ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
03190 ast_set_flag(clone, AST_FLAG_ZOMBIE);
03191 ast_queue_frame(clone, &null_frame);
03192 ast_mutex_unlock(&clone->lock);
03193 }
03194
03195
03196 if (ast_test_flag(original, AST_FLAG_BLOCKING))
03197 pthread_kill(original->blocker, SIGURG);
03198 ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n", original->name, original->_state);
03199 return 0;
03200 }
03201
03202 void ast_set_callerid(struct ast_channel *chan, const char *callerid, const char *calleridname, const char *ani)
03203 {
03204 if (callerid) {
03205 if (chan->cid.cid_num)
03206 free(chan->cid.cid_num);
03207 if (ast_strlen_zero(callerid))
03208 chan->cid.cid_num = NULL;
03209 else
03210 chan->cid.cid_num = strdup(callerid);
03211 }
03212 if (calleridname) {
03213 if (chan->cid.cid_name)
03214 free(chan->cid.cid_name);
03215 if (ast_strlen_zero(calleridname))
03216 chan->cid.cid_name = NULL;
03217 else
03218 chan->cid.cid_name = strdup(calleridname);
03219 }
03220 if (ani) {
03221 if (chan->cid.cid_ani)
03222 free(chan->cid.cid_ani);
03223 if (ast_strlen_zero(ani))
03224 chan->cid.cid_ani = NULL;
03225 else
03226 chan->cid.cid_ani = strdup(ani);
03227 }
03228 if (chan->cdr)
03229 ast_cdr_setcid(chan->cdr, chan);
03230 manager_event(EVENT_FLAG_CALL, "Newcallerid",
03231 "Channel: %s\r\n"
03232 "CallerID: %s\r\n"
03233 "CallerIDName: %s\r\n"
03234 "Uniqueid: %s\r\n"
03235 "CID-CallingPres: %d (%s)\r\n",
03236 chan->name, chan->cid.cid_num ?
03237 chan->cid.cid_num : "<Unknown>",
03238 chan->cid.cid_name ?
03239 chan->cid.cid_name : "<Unknown>",
03240 chan->uniqueid,
03241 chan->cid.cid_pres,
03242 ast_describe_caller_presentation(chan->cid.cid_pres)
03243 );
03244 }
03245
03246 int ast_setstate(struct ast_channel *chan, int state)
03247 {
03248 int oldstate = chan->_state;
03249
03250 if (oldstate == state)
03251 return 0;
03252
03253 chan->_state = state;
03254 ast_device_state_changed_literal(chan->name);
03255 manager_event(EVENT_FLAG_CALL,
03256 (oldstate == AST_STATE_DOWN) ? "Newchannel" : "Newstate",
03257 "Channel: %s\r\n"
03258 "State: %s\r\n"
03259 "CallerID: %s\r\n"
03260 "CallerIDName: %s\r\n"
03261 "Uniqueid: %s\r\n",
03262 chan->name, ast_state2str(chan->_state),
03263 chan->cid.cid_num ? chan->cid.cid_num : "<unknown>",
03264 chan->cid.cid_name ? chan->cid.cid_name : "<unknown>",
03265 chan->uniqueid);
03266
03267 return 0;
03268 }
03269
03270
03271 struct ast_channel *ast_bridged_channel(struct ast_channel *chan)
03272 {
03273 struct ast_channel *bridged;
03274 bridged = chan->_bridge;
03275 if (bridged && bridged->tech->bridged_channel)
03276 bridged = bridged->tech->bridged_channel(chan, bridged);
03277 return bridged;
03278 }
03279
03280 static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, char *sound, int remain)
03281 {
03282 int res=0, min=0, sec=0,check=0;
03283
03284 check = ast_autoservice_start(peer);
03285 if(check)
03286 return;
03287
03288 if (remain > 0) {
03289 if (remain / 60 > 1) {
03290 min = remain / 60;
03291 sec = remain % 60;
03292 } else {
03293 sec = remain;
03294 }
03295 }
03296
03297 if (!strcmp(sound,"timeleft")) {
03298 res = ast_streamfile(chan, "vm-youhave", chan->language);
03299 res = ast_waitstream(chan, "");
03300 if (min) {
03301 res = ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, (char *) NULL);
03302 res = ast_streamfile(chan, "queue-minutes", chan->language);
03303 res = ast_waitstream(chan, "");
03304 }
03305 if (sec) {
03306 res = ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, (char *) NULL);
03307 res = ast_streamfile(chan, "queue-seconds", chan->language);
03308 res = ast_waitstream(chan, "");
03309 }
03310 } else {
03311 res = ast_streamfile(chan, sound, chan->language);
03312 res = ast_waitstream(chan, "");
03313 }
03314
03315 check = ast_autoservice_stop(peer);
03316 }
03317
03318 static enum ast_bridge_result ast_generic_bridge(struct ast_channel *c0, struct ast_channel *c1,
03319 struct ast_bridge_config *config, struct ast_frame **fo,
03320 struct ast_channel **rc, struct timeval bridge_end)
03321 {
03322
03323 struct ast_channel *cs[3];
03324 struct ast_frame *f;
03325 struct ast_channel *who = NULL;
03326 enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
03327 int o0nativeformats;
03328 int o1nativeformats;
03329 int watch_c0_dtmf;
03330 int watch_c1_dtmf;
03331 void *pvt0, *pvt1;
03332 int to;
03333
03334 cs[0] = c0;
03335 cs[1] = c1;
03336 pvt0 = c0->tech_pvt;
03337 pvt1 = c1->tech_pvt;
03338 o0nativeformats = c0->nativeformats;
03339 o1nativeformats = c1->nativeformats;
03340 watch_c0_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_0;
03341 watch_c1_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_1;
03342
03343 for (;;) {
03344 if ((c0->tech_pvt != pvt0) || (c1->tech_pvt != pvt1) ||
03345 (o0nativeformats != c0->nativeformats) ||
03346 (o1nativeformats != c1->nativeformats)) {
03347
03348 res = AST_BRIDGE_RETRY;
03349 break;
03350 }
03351 if (bridge_end.tv_sec) {
03352 to = ast_tvdiff_ms(bridge_end, ast_tvnow());
03353 if (to <= 0) {
03354 res = AST_BRIDGE_COMPLETE;
03355 break;
03356 }
03357 } else
03358 to = -1;
03359 who = ast_waitfor_n(cs, 2, &to);
03360 if (!who) {
03361 ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
03362 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) {
03363 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
03364 c0->_softhangup = 0;
03365 if (c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
03366 c1->_softhangup = 0;
03367 c0->_bridge = c1;
03368 c1->_bridge = c0;
03369 }
03370 continue;
03371 }
03372 f = ast_read(who);
03373 if (!f) {
03374 *fo = NULL;
03375 *rc = who;
03376 res = AST_BRIDGE_COMPLETE;
03377 ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
03378 break;
03379 }
03380
03381 if ((f->frametype == AST_FRAME_CONTROL) && !(config->flags & AST_BRIDGE_IGNORE_SIGS)) {
03382 if ((f->subclass == AST_CONTROL_HOLD) || (f->subclass == AST_CONTROL_UNHOLD) ||
03383 (f->subclass == AST_CONTROL_VIDUPDATE)) {
03384 ast_indicate(who == c0 ? c1 : c0, f->subclass);
03385 } else {
03386 *fo = f;
03387 *rc = who;
03388 res = AST_BRIDGE_COMPLETE;
03389 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass, who->name);
03390 break;
03391 }
03392 }
03393 if ((f->frametype == AST_FRAME_VOICE) ||
03394 (f->frametype == AST_FRAME_DTMF) ||
03395 (f->frametype == AST_FRAME_VIDEO) ||
03396 (f->frametype == AST_FRAME_IMAGE) ||
03397 (f->frametype == AST_FRAME_HTML) ||
03398 (f->frametype == AST_FRAME_TEXT)) {
03399 if (f->frametype == AST_FRAME_DTMF) {
03400 if (((who == c0) && watch_c0_dtmf) ||
03401 ((who == c1) && watch_c1_dtmf)) {
03402 *rc = who;
03403 *fo = f;
03404 res = AST_BRIDGE_COMPLETE;
03405 ast_log(LOG_DEBUG, "Got DTMF on channel (%s)\n", who->name);
03406 break;
03407 } else {
03408 goto tackygoto;
03409 }
03410 } else {
03411 #if 0
03412 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
03413 if (who == last)
03414 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
03415 last = who;
03416 #endif
03417 tackygoto:
03418 ast_write((who == c0) ? c1 : c0, f);
03419 }
03420 }
03421 ast_frfree(f);
03422
03423
03424 cs[2] = cs[0];
03425 cs[0] = cs[1];
03426 cs[1] = cs[2];
03427 }
03428 return res;
03429 }
03430
03431
03432 enum ast_bridge_result ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1,
03433 struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
03434 {
03435 struct ast_channel *who = NULL;
03436 enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
03437 int nativefailed=0;
03438 int firstpass;
03439 int o0nativeformats;
03440 int o1nativeformats;
03441 long time_left_ms=0;
03442 struct timeval nexteventts = { 0, };
03443 char caller_warning = 0;
03444 char callee_warning = 0;
03445
03446 if (c0->_bridge) {
03447 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
03448 c0->name, c0->_bridge->name);
03449 return -1;
03450 }
03451 if (c1->_bridge) {
03452 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
03453 c1->name, c1->_bridge->name);
03454 return -1;
03455 }
03456
03457
03458 if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
03459 ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1))
03460 return -1;
03461
03462 *fo = NULL;
03463 firstpass = config->firstpass;
03464 config->firstpass = 0;
03465
03466 if (ast_tvzero(config->start_time))
03467 config->start_time = ast_tvnow();
03468 time_left_ms = config->timelimit;
03469
03470 caller_warning = ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING);
03471 callee_warning = ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING);
03472
03473 if (config->start_sound && firstpass) {
03474 if (caller_warning)
03475 bridge_playfile(c0, c1, config->start_sound, time_left_ms / 1000);
03476 if (callee_warning)
03477 bridge_playfile(c1, c0, config->start_sound, time_left_ms / 1000);
03478 }
03479
03480
03481 c0->_bridge = c1;
03482 c1->_bridge = c0;
03483
03484 manager_event(EVENT_FLAG_CALL, "Link",
03485 "Channel1: %s\r\n"
03486 "Channel2: %s\r\n"
03487 "Uniqueid1: %s\r\n"
03488 "Uniqueid2: %s\r\n"
03489 "CallerID1: %s\r\n"
03490 "CallerID2: %s\r\n",
03491 c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
03492
03493 o0nativeformats = c0->nativeformats;
03494 o1nativeformats = c1->nativeformats;
03495
03496 if (config->feature_timer) {
03497 nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->feature_timer, 1000));
03498 } else if (config->timelimit) {
03499 nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
03500 if (caller_warning || callee_warning)
03501 nexteventts = ast_tvsub(nexteventts, ast_samp2tv(config->play_warning, 1000));
03502 }
03503
03504 for (;;) {
03505 struct timeval now = { 0, };
03506 int to;
03507
03508 to = -1;
03509
03510 if (!ast_tvzero(nexteventts)) {
03511 now = ast_tvnow();
03512 to = ast_tvdiff_ms(nexteventts, now);
03513 if (to <= 0) {
03514 res = AST_BRIDGE_COMPLETE;
03515 break;
03516 }
03517 }
03518
03519 if (config->timelimit) {
03520 time_left_ms = config->timelimit - ast_tvdiff_ms(now, config->start_time);
03521 if (time_left_ms < to)
03522 to = time_left_ms;
03523
03524 if (time_left_ms <= 0) {
03525 if (caller_warning && config->end_sound)
03526 bridge_playfile(c0, c1, config->end_sound, 0);
03527 if (callee_warning && config->end_sound)
03528 bridge_playfile(c1, c0, config->end_sound, 0);
03529 *fo = NULL;
03530 if (who)
03531 *rc = who;
03532 res = 0;
03533 break;
03534 }
03535
03536 if (!to) {
03537 if (time_left_ms >= 5000) {
03538
03539 if (caller_warning && config->warning_sound && config->play_warning)
03540 bridge_playfile(c0, c1, config->warning_sound,
03541 (time_left_ms + 500) / 1000);
03542 if (callee_warning && config->warning_sound && config->play_warning)
03543 bridge_playfile(c1, c0, config->warning_sound,
03544 (time_left_ms + 500) / 1000);
03545 }
03546 if (config->warning_freq) {
03547 nexteventts = ast_tvadd(nexteventts, ast_samp2tv(config->warning_freq, 1000));
03548 } else
03549 nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
03550 }
03551 }
03552
03553 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) {
03554 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
03555 c0->_softhangup = 0;
03556 if (c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
03557 c1->_softhangup = 0;
03558 c0->_bridge = c1;
03559 c1->_bridge = c0;
03560 ast_log(LOG_DEBUG, "Unbridge signal received. Ending native bridge.\n");
03561 continue;
03562 }
03563
03564
03565 if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
03566 ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1)) {
03567 *fo = NULL;
03568 if (who)
03569 *rc = who;
03570 res = 0;
03571 ast_log(LOG_DEBUG, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",
03572 c0->name, c1->name,
03573 ast_test_flag(c0, AST_FLAG_ZOMBIE) ? "Yes" : "No",
03574 ast_check_hangup(c0) ? "Yes" : "No",
03575 ast_test_flag(c1, AST_FLAG_ZOMBIE) ? "Yes" : "No",
03576 ast_check_hangup(c1) ? "Yes" : "No");
03577 break;
03578 }
03579
03580 if (c0->tech->bridge &&
03581 (config->timelimit == 0) &&
03582 (c0->tech->bridge == c1->tech->bridge) &&
03583 !nativefailed && !c0->monitor && !c1->monitor &&
03584 !c0->spies && !c1->spies && !ast_test_flag(&(config->features_callee),AST_FEATURE_REDIRECT) &&
03585 !ast_test_flag(&(config->features_caller),AST_FEATURE_REDIRECT) ) {
03586
03587 if (option_verbose > 2)
03588 ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
03589 ast_set_flag(c0, AST_FLAG_NBRIDGE);
03590 ast_set_flag(c1, AST_FLAG_NBRIDGE);
03591 if ((res = c0->tech->bridge(c0, c1, config->flags, fo, rc, to)) == AST_BRIDGE_COMPLETE) {
03592 manager_event(EVENT_FLAG_CALL, "Unlink",
03593 "Channel1: %s\r\n"
03594 "Channel2: %s\r\n"
03595 "Uniqueid1: %s\r\n"
03596 "Uniqueid2: %s\r\n"
03597 "CallerID1: %s\r\n"
03598 "CallerID2: %s\r\n",
03599 c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
03600 ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n", c0->name, c1->name);
03601
03602 ast_clear_flag(c0, AST_FLAG_NBRIDGE);
03603 ast_clear_flag(c1, AST_FLAG_NBRIDGE);
03604
03605 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
03606 continue;
03607
03608 c0->_bridge = NULL;
03609 c1->_bridge = NULL;
03610
03611 return res;
03612 } else {
03613 ast_clear_flag(c0, AST_FLAG_NBRIDGE);
03614 ast_clear_flag(c1, AST_FLAG_NBRIDGE);
03615 }
03616 switch (res) {
03617 case AST_BRIDGE_RETRY:
03618 continue;
03619 default:
03620 ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
03621
03622 case AST_BRIDGE_FAILED_NOWARN:
03623 nativefailed++;
03624 break;
03625 }
03626 }
03627
03628 if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) ||
03629 (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
03630 !(c0->generator || c1->generator)) {
03631 if (ast_channel_make_compatible(c0, c1)) {
03632 ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
03633 manager_event(EVENT_FLAG_CALL, "Unlink",
03634 "Channel1: %s\r\n"
03635 "Channel2: %s\r\n"
03636 "Uniqueid1: %s\r\n"
03637 "Uniqueid2: %s\r\n"
03638 "CallerID1: %s\r\n"
03639 "CallerID2: %s\r\n",
03640 c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
03641 return AST_BRIDGE_FAILED;
03642 }
03643 o0nativeformats = c0->nativeformats;
03644 o1nativeformats = c1->nativeformats;
03645 }
03646 res = ast_generic_bridge(c0, c1, config, fo, rc, nexteventts);
03647 if (res != AST_BRIDGE_RETRY)
03648 break;
03649 }
03650
03651 c0->_bridge = NULL;
03652 c1->_bridge = NULL;
03653
03654 manager_event(EVENT_FLAG_CALL, "Unlink",
03655 "Channel1: %s\r\n"
03656 "Channel2: %s\r\n"
03657 "Uniqueid1: %s\r\n"
03658 "Uniqueid2: %s\r\n"
03659 "CallerID1: %s\r\n"
03660 "CallerID2: %s\r\n",
03661 c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
03662 ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n", c0->name, c1->name);
03663
03664 return res;
03665 }
03666
03667
03668 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
03669 {
03670 int res;
03671
03672 if (chan->tech->setoption) {
03673 res = chan->tech->setoption(chan, option, data, datalen);
03674 if (res < 0)
03675 return res;
03676 } else {
03677 errno = ENOSYS;
03678 return -1;
03679 }
03680 if (block) {
03681
03682
03683 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
03684 return -1;
03685 }
03686 return 0;
03687 }
03688
03689 struct tonepair_def {
03690 int freq1;
03691 int freq2;
03692 int duration;
03693 int vol;
03694 };
03695
03696 struct tonepair_state {
03697 float freq1;
03698 float freq2;
03699 float vol;
03700 int duration;
03701 int pos;
03702 int origwfmt;
03703 struct ast_frame f;
03704 unsigned char offset[AST_FRIENDLY_OFFSET];
03705 short data[4000];
03706 };
03707
03708 static void tonepair_release(struct ast_channel *chan, void *params)
03709 {
03710 struct tonepair_state *ts = params;
03711
03712 if (chan) {
03713 ast_set_write_format(chan, ts->origwfmt);
03714 }
03715 free(ts);
03716 }
03717
03718 static void *tonepair_alloc(struct ast_channel *chan, void *params)
03719 {
03720 struct tonepair_state *ts;
03721 struct tonepair_def *td = params;
03722
03723 ts = malloc(sizeof(struct tonepair_state));
03724 if (!ts)
03725 return NULL;
03726 memset(ts, 0, sizeof(struct tonepair_state));
03727 ts->origwfmt = chan->writeformat;
03728 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
03729 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
03730 tonepair_release(NULL, ts);
03731 ts = NULL;
03732 } else {
03733 ts->freq1 = td->freq1;
03734 ts->freq2 = td->freq2;
03735 ts->duration = td->duration;
03736 ts->vol = td->vol;
03737 }
03738
03739 ast_set_flag(chan, AST_FLAG_WRITE_INT);
03740 return ts;
03741 }
03742
03743 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
03744 {
03745 struct tonepair_state *ts = data;
03746 int x;
03747
03748
03749
03750
03751 len = samples * 2;
03752
03753 if (len > sizeof(ts->data) / 2 - 1) {
03754 ast_log(LOG_WARNING, "Can't generate that much data!\n");
03755 return -1;
03756 }
03757 memset(&ts->f, 0, sizeof(ts->f));
03758 for (x = 0; x < (len / 2); x++) {
03759 ts->data[x] = ts->vol * (
03760 sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
03761 sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
03762 );
03763 }
03764 ts->f.frametype = AST_FRAME_VOICE;
03765 ts->f.subclass = AST_FORMAT_SLINEAR;
03766 ts->f.datalen = len;
03767 ts->f.samples = samples;
03768 ts->f.offset = AST_FRIENDLY_OFFSET;
03769 ts->f.data = ts->data;
03770 ast_write(chan, &ts->f);
03771 ts->pos += x;
03772 if (ts->duration > 0) {
03773 if (ts->pos >= ts->duration * 8)
03774 return -1;
03775 }
03776 return 0;
03777 }
03778
03779 static struct ast_generator tonepair = {
03780 alloc: tonepair_alloc,
03781 release: tonepair_release,
03782 generate: tonepair_generator,
03783 };
03784
03785 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
03786 {
03787 struct tonepair_def d = { 0, };
03788
03789 d.freq1 = freq1;
03790 d.freq2 = freq2;
03791 d.duration = duration;
03792 if (vol < 1)
03793 d.vol = 8192;
03794 else
03795 d.vol = vol;
03796 if (ast_activate_generator(chan, &tonepair, &d))
03797 return -1;
03798 return 0;
03799 }
03800
03801 void ast_tonepair_stop(struct ast_channel *chan)
03802 {
03803 ast_deactivate_generator(chan);
03804 }
03805
03806 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
03807 {
03808 struct ast_frame *f;
03809 int res;
03810
03811 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
03812 return res;
03813
03814
03815 while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {
03816 f = ast_read(chan);
03817 if (f)
03818 ast_frfree(f);
03819 else
03820 return -1;
03821 }
03822 return 0;
03823 }
03824
03825 ast_group_t ast_get_group(char *s)
03826 {
03827 char *copy;
03828 char *piece;
03829 char *c=NULL;
03830 int start=0, finish=0, x;
03831 ast_group_t group = 0;
03832
03833 copy = ast_strdupa(s);
03834 if (!copy) {
03835 ast_log(LOG_ERROR, "Out of memory\n");
03836 return 0;
03837 }
03838 c = copy;
03839
03840 while((piece = strsep(&c, ","))) {
03841 if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
03842
03843 } else if (sscanf(piece, "%d", &start)) {
03844
03845 finish = start;
03846 } else {
03847 ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
03848 continue;
03849 }
03850 for (x = start; x <= finish; x++) {
03851 if ((x > 63) || (x < 0)) {
03852 ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
03853 } else
03854 group |= ((ast_group_t) 1 << x);
03855 }
03856 }
03857 return group;
03858 }
03859
03860 static int (*ast_moh_start_ptr)(struct ast_channel *, char *) = NULL;
03861 static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
03862 static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
03863
03864
03865 void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, char *),
03866 void (*stop_ptr)(struct ast_channel *),
03867 void (*cleanup_ptr)(struct ast_channel *)
03868 )
03869 {
03870 ast_moh_start_ptr = start_ptr;
03871 ast_moh_stop_ptr = stop_ptr;
03872 ast_moh_cleanup_ptr = cleanup_ptr;
03873 }
03874
03875 void ast_uninstall_music_functions(void)
03876 {
03877 ast_moh_start_ptr = NULL;
03878 ast_moh_stop_ptr = NULL;
03879 ast_moh_cleanup_ptr = NULL;
03880 }
03881
03882
03883 int ast_moh_start(struct ast_channel *chan, char *mclass)
03884 {
03885 if (ast_moh_start_ptr)
03886 return ast_moh_start_ptr(chan, mclass);
03887
03888 if (option_verbose > 2)
03889 ast_verbose(VERBOSE_PREFIX_3 "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : "default");
03890
03891 return 0;
03892 }
03893
03894
03895 void ast_moh_stop(struct ast_channel *chan)
03896 {
03897 if(ast_moh_stop_ptr)
03898 ast_moh_stop_ptr(chan);
03899 }
03900
03901 void ast_moh_cleanup(struct ast_channel *chan)
03902 {
03903 if(ast_moh_cleanup_ptr)
03904 ast_moh_cleanup_ptr(chan);
03905 }
03906
03907 void ast_channels_init(void)
03908 {
03909 ast_cli_register(&cli_show_channeltypes);
03910 }
03911
03912
03913 char *ast_print_group(char *buf, int buflen, ast_group_t group)
03914 {
03915 unsigned int i;
03916 int first=1;
03917 char num[3];
03918
03919 buf[0] = '\0';
03920
03921 if (!group)
03922 return(buf);
03923
03924 for (i=0; i<=63; i++) {
03925 if (group & ((ast_group_t) 1 << i)) {
03926 if (!first) {
03927 strncat(buf, ", ", buflen);
03928 } else {
03929 first=0;
03930 }
03931 snprintf(num, sizeof(num), "%u", i);
03932 strncat(buf, num, buflen);
03933 }
03934 }
03935 return(buf);
03936 }
03937
03938 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
03939 {
03940 struct ast_variable *cur;
03941
03942 for (cur = vars; cur; cur = cur->next)
03943 pbx_builtin_setvar_helper(chan, cur->name, cur->value);
03944 }
03945
03946 static void copy_data_from_queue(struct ast_channel_spy_queue *queue, short *buf, unsigned int samples)
03947 {
03948 struct ast_frame *f;
03949 int tocopy;
03950 int bytestocopy;
03951
03952 while (samples) {
03953 f = queue->head;
03954
03955 if (!f) {
03956 ast_log(LOG_ERROR, "Ran out of frames before buffer filled!\n");
03957 break;
03958 }
03959
03960 tocopy = (f->samples > samples) ? samples : f->samples;
03961 bytestocopy = ast_codec_get_len(queue->format, tocopy);
03962 memcpy(buf, f->data, bytestocopy);
03963 samples -= tocopy;
03964 buf += tocopy;
03965 f->samples -= tocopy;
03966 f->data += bytestocopy;
03967 f->datalen -= bytestocopy;
03968 f->offset += bytestocopy;
03969 queue->samples -= tocopy;
03970 if (!f->samples) {
03971 queue->head = f->next;
03972 ast_frfree(f);
03973 }
03974 }
03975 }
03976
03977 struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsigned int samples)
03978 {
03979 struct ast_frame *result;
03980
03981 short read_buf[samples];
03982 short write_buf[samples];
03983 struct ast_frame *read_frame;
03984 struct ast_frame *write_frame;
03985 int need_dup;
03986 struct ast_frame stack_read_frame = { .frametype = AST_FRAME_VOICE,
03987 .subclass = spy->read_queue.format,
03988 .data = read_buf,
03989 .samples = samples,
03990 .datalen = ast_codec_get_len(spy->read_queue.format, samples),
03991 };
03992 struct ast_frame stack_write_frame = { .frametype = AST_FRAME_VOICE,
03993 .subclass = spy->write_queue.format,
03994 .data = write_buf,
03995 .samples = samples,
03996 .datalen = ast_codec_get_len(spy->write_queue.format, samples),
03997 };
03998
03999
04000 if (ast_test_flag(spy, CHANSPY_TRIGGER_FLUSH)) {
04001 if (spy->read_queue.samples > spy->write_queue.samples) {
04002 if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST)) {
04003 for (result = spy->read_queue.head; result; result = result->next)
04004 ast_frame_adjust_volume(result, spy->read_vol_adjustment);
04005 }
04006 result = spy->read_queue.head;
04007 spy->read_queue.head = NULL;
04008 spy->read_queue.samples = 0;
04009 ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
04010 return result;
04011 } else {
04012 if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST)) {
04013 for (result = spy->write_queue.head; result; result = result->next)
04014 ast_frame_adjust_volume(result, spy->write_vol_adjustment);
04015 }
04016 result = spy->write_queue.head;
04017 spy->write_queue.head = NULL;
04018 spy->write_queue.samples = 0;
04019 ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
04020 return result;
04021 }
04022 }
04023
04024 if ((spy->read_queue.samples < samples) || (spy->write_queue.samples < samples))
04025 return NULL;
04026
04027
04028 if ((spy->read_queue.head->samples == samples) &&
04029 (spy->write_queue.head->samples == samples)) {
04030 read_frame = spy->read_queue.head;
04031 spy->read_queue.head = read_frame->next;
04032 read_frame->next = NULL;
04033
04034 write_frame = spy->write_queue.head;
04035 spy->write_queue.head = write_frame->next;
04036 write_frame->next = NULL;
04037
04038 spy->read_queue.samples -= samples;
04039 spy->write_queue.samples -= samples;
04040
04041 need_dup = 0;
04042 } else {
04043 copy_data_from_queue(&spy->read_queue, read_buf, samples);
04044 copy_data_from_queue(&spy->write_queue, write_buf, samples);
04045
04046 read_frame = &stack_read_frame;
04047 write_frame = &stack_write_frame;
04048 need_dup = 1;
04049 }
04050
04051 if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST))
04052 ast_frame_adjust_volume(read_frame, spy->read_vol_adjustment);
04053
04054 if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST))
04055 ast_frame_adjust_volume(write_frame, spy->write_vol_adjustment);
04056
04057 if (ast_test_flag(spy, CHANSPY_MIXAUDIO)) {
04058 ast_frame_slinear_sum(read_frame, write_frame);
04059
04060 if (need_dup)
04061 result = ast_frdup(read_frame);
04062 else {
04063 result = read_frame;
04064 ast_frfree(write_frame);
04065 }
04066 } else {
04067 if (need_dup) {
04068 result = ast_frdup(read_frame);
04069 result->next = ast_frdup(write_frame);
04070 } else {
04071 result = read_frame;
04072 result->next = write_frame;
04073 }
04074 }
04075
04076 return result;
04077 }
04078
04079 static void *silence_generator_alloc(struct ast_channel *chan, void *data)
04080 {
04081
04082 return data;
04083 }
04084
04085 static void silence_generator_release(struct ast_channel *chan, void *data)
04086 {
04087
04088 }
04089
04090 static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
04091 {
04092 if (samples == 160) {
04093 short buf[160] = { 0, };
04094 struct ast_frame frame = {
04095 .frametype = AST_FRAME_VOICE,
04096 .subclass = AST_FORMAT_SLINEAR,
04097 .data = buf,
04098 .samples = 160,
04099 .datalen = sizeof(buf),
04100 };
04101
04102 if (ast_write(chan, &frame))
04103 return -1;
04104 } else {
04105 short buf[samples];
04106 int x;
04107 struct ast_frame frame = {
04108 .frametype = AST_FRAME_VOICE,
04109 .subclass = AST_FORMAT_SLINEAR,
04110 .data = buf,
04111 .samples = samples,
04112 .datalen = sizeof(buf),
04113 };
04114
04115 for (x = 0; x < samples; x++)
04116 buf[x] = 0;
04117
04118 if (ast_write(chan, &frame))
04119 return -1;
04120 }
04121
04122 return 0;
04123 }
04124
04125 static struct ast_generator silence_generator = {
04126 .alloc = silence_generator_alloc,
04127 .release = silence_generator_release,
04128 .generate = silence_generator_generate,
04129 };
04130
04131 struct ast_silence_generator {
04132 int old_write_format;
04133 };
04134
04135 struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan)
04136 {
04137 struct ast_silence_generator *state;
04138
04139 if (!(state = calloc(1, sizeof(*state)))) {
04140 ast_log(LOG_WARNING, "Could not allocate state structure\n");
04141 return NULL;
04142 }
04143
04144 state->old_write_format = chan->writeformat;
04145
04146 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
04147 ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
04148 free(state);
04149 return NULL;
04150 }
04151
04152 ast_activate_generator(chan, &silence_generator, state);
04153
04154 if (option_debug)
04155 ast_log(LOG_DEBUG, "Started silence generator on '%s'\n", chan->name);
04156
04157 return state;
04158 }
04159
04160 void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
04161 {
04162 if (!state)
04163 return;
04164
04165 ast_deactivate_generator(chan);
04166
04167 if (option_debug)
04168 ast_log(LOG_DEBUG, "Stopped silence generator on '%s'\n", chan->name);
04169
04170 if (ast_set_write_format(chan, state->old_write_format) < 0)
04171 ast_log(LOG_ERROR, "Could not return write format to its original state\n");
04172
04173 free(state);
04174 }