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