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