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