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