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