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