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