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