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