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