![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
channel.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * 00021 * \brief Channel Management 00022 * 00023 * \author Mark Spencer <markster@digium.com> 00024 */ 00025 00026 #include "asterisk.h" 00027 00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 51859 $") 00029 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 #include <stdarg.h> 00033 #include <string.h> 00034 #include <sys/time.h> 00035 #include <signal.h> 00036 #include <errno.h> 00037 #include <unistd.h> 00038 #include <math.h> 00039 00040 #include "asterisk/zapata.h" 00041 00042 #include "asterisk/pbx.h" 00043 #include "asterisk/frame.h" 00044 #include "asterisk/sched.h" 00045 #include "asterisk/options.h" 00046 #include "asterisk/channel.h" 00047 #include "asterisk/chanspy.h" 00048 #include "asterisk/musiconhold.h" 00049 #include "asterisk/logger.h" 00050 #include "asterisk/say.h" 00051 #include "asterisk/file.h" 00052 #include "asterisk/cli.h" 00053 #include "asterisk/translate.h" 00054 #include "asterisk/manager.h" 00055 #include "asterisk/chanvars.h" 00056 #include "asterisk/linkedlists.h" 00057 #include "asterisk/indications.h" 00058 #include "asterisk/monitor.h" 00059 #include "asterisk/causes.h" 00060 #include "asterisk/callerid.h" 00061 #include "asterisk/utils.h" 00062 #include "asterisk/lock.h" 00063 #include "asterisk/app.h" 00064 #include "asterisk/transcap.h" 00065 #include "asterisk/devicestate.h" 00066 #include "asterisk/sha1.h" 00067 #include "asterisk/threadstorage.h" 00068 #include "asterisk/slinfactory.h" 00069 00070 struct channel_spy_trans { 00071 int last_format; 00072 struct ast_trans_pvt *path; 00073 }; 00074 00075 struct ast_channel_spy_list { 00076 struct channel_spy_trans read_translator; 00077 struct channel_spy_trans write_translator; 00078 AST_LIST_HEAD_NOLOCK(, ast_channel_spy) list; 00079 }; 00080 00081 struct ast_channel_whisper_buffer { 00082 ast_mutex_t lock; 00083 struct ast_slinfactory sf; 00084 unsigned int original_format; 00085 struct ast_trans_pvt *path; 00086 }; 00087 00088 /* uncomment if you have problems with 'monitoring' synchronized files */ 00089 #if 0 00090 #define MONITOR_CONSTANT_DELAY 00091 #define MONITOR_DELAY 150 * 8 /* 150 ms of MONITORING DELAY */ 00092 #endif 00093 00094 /*! Prevent new channel allocation if shutting down. */ 00095 static int shutting_down; 00096 00097 static int uniqueint; 00098 00099 unsigned long global_fin, global_fout; 00100 00101 AST_THREADSTORAGE(state2str_threadbuf); 00102 #define STATE2STR_BUFSIZE 32 00103 00104 /*! 100ms */ 00105 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100 00106 00107 struct chanlist { 00108 const struct ast_channel_tech *tech; 00109 AST_LIST_ENTRY(chanlist) list; 00110 }; 00111 00112 /*! the list of registered channel types */ 00113 static AST_LIST_HEAD_NOLOCK_STATIC(backends, chanlist); 00114 00115 /*! the list of channels we have. Note that the lock for this list is used for 00116 both the channels list and the backends list. */ 00117 static AST_LIST_HEAD_STATIC(channels, ast_channel); 00118 00119 /*! map AST_CAUSE's to readable string representations */ 00120 const struct ast_cause { 00121 int cause; 00122 const char *name; 00123 const char *desc; 00124 } causes[] = { 00125 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" }, 00126 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" }, 00127 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" }, 00128 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" }, 00129 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" }, 00130 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" }, 00131 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" }, 00132 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" }, 00133 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" }, 00134 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" }, 00135 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" }, 00136 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" }, 00137 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" }, 00138 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" }, 00139 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" }, 00140 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" }, 00141 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" }, 00142 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" }, 00143 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" }, 00144 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" }, 00145 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" }, 00146 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" }, 00147 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" }, 00148 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" }, 00149 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" }, 00150 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" }, 00151 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" }, 00152 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" }, 00153 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" }, 00154 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" }, 00155 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" }, 00156 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" }, 00157 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" }, 00158 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" }, 00159 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" }, 00160 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." }, 00161 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" }, 00162 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" }, 00163 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" }, 00164 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" }, 00165 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" }, 00166 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" }, 00167 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" }, 00168 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" }, 00169 }; 00170 00171 struct ast_variable *ast_channeltype_list(void) 00172 { 00173 struct chanlist *cl; 00174 struct ast_variable *var=NULL, *prev = NULL; 00175 AST_LIST_TRAVERSE(&backends, cl, list) { 00176 if (prev) { 00177 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description))) 00178 prev = prev->next; 00179 } else { 00180 var = ast_variable_new(cl->tech->type, cl->tech->description); 00181 prev = var; 00182 } 00183 } 00184 return var; 00185 } 00186 00187 static int show_channeltypes(int fd, int argc, char *argv[]) 00188 { 00189 #define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n" 00190 struct chanlist *cl; 00191 int count_chan = 0; 00192 00193 ast_cli(fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer"); 00194 ast_cli(fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------"); 00195 if (AST_LIST_LOCK(&channels)) { 00196 ast_log(LOG_WARNING, "Unable to lock channel list\n"); 00197 return -1; 00198 } 00199 AST_LIST_TRAVERSE(&backends, cl, list) { 00200 ast_cli(fd, FORMAT, cl->tech->type, cl->tech->description, 00201 (cl->tech->devicestate) ? "yes" : "no", 00202 (cl->tech->indicate) ? "yes" : "no", 00203 (cl->tech->transfer) ? "yes" : "no"); 00204 count_chan++; 00205 } 00206 AST_LIST_UNLOCK(&channels); 00207 ast_cli(fd, "----------\n%d channel drivers registered.\n", count_chan); 00208 return RESULT_SUCCESS; 00209 00210 #undef FORMAT 00211 00212 } 00213 00214 static int show_channeltype(int fd, int argc, char *argv[]) 00215 { 00216 struct chanlist *cl = NULL; 00217 00218 if (argc != 4) 00219 return RESULT_SHOWUSAGE; 00220 00221 if (AST_LIST_LOCK(&channels)) { 00222 ast_log(LOG_WARNING, "Unable to lock channel list\n"); 00223 return RESULT_FAILURE; 00224 } 00225 00226 AST_LIST_TRAVERSE(&backends, cl, list) { 00227 if (!strncasecmp(cl->tech->type, argv[3], strlen(cl->tech->type))) { 00228 break; 00229 } 00230 } 00231 00232 00233 if (!cl) { 00234 ast_cli(fd, "\n%s is not a registered channel driver.\n", argv[3]); 00235 AST_LIST_UNLOCK(&channels); 00236 return RESULT_FAILURE; 00237 } 00238 00239 ast_cli(fd, 00240 "-- Info about channel driver: %s --\n" 00241 " Device State: %s\n" 00242 " Indication: %s\n" 00243 " Transfer : %s\n" 00244 " Capabilities: %d\n" 00245 " Digit Begin: %s\n" 00246 " Digit End: %s\n" 00247 " Send HTML : %s\n" 00248 " Image Support: %s\n" 00249 " Text Support: %s\n", 00250 cl->tech->type, 00251 (cl->tech->devicestate) ? "yes" : "no", 00252 (cl->tech->indicate) ? "yes" : "no", 00253 (cl->tech->transfer) ? "yes" : "no", 00254 (cl->tech->capabilities) ? cl->tech->capabilities : -1, 00255 (cl->tech->send_digit_begin) ? "yes" : "no", 00256 (cl->tech->send_digit_end) ? "yes" : "no", 00257 (cl->tech->send_html) ? "yes" : "no", 00258 (cl->tech->send_image) ? "yes" : "no", 00259 (cl->tech->send_text) ? "yes" : "no" 00260 00261 ); 00262 00263 AST_LIST_UNLOCK(&channels); 00264 return RESULT_SUCCESS; 00265 } 00266 00267 static char *complete_channeltypes(const char *line, const char *word, int pos, int state) 00268 { 00269 struct chanlist *cl; 00270 int which = 0; 00271 int wordlen; 00272 char *ret = NULL; 00273 00274 if (pos != 3) 00275 return NULL; 00276 00277 wordlen = strlen(word); 00278 00279 AST_LIST_TRAVERSE(&backends, cl, list) { 00280 if (!strncasecmp(word, cl->tech->type, wordlen) && ++which > state) { 00281 ret = strdup(cl->tech->type); 00282 break; 00283 } 00284 } 00285 00286 return ret; 00287 } 00288 00289 static const char show_channeltypes_usage[] = 00290 "Usage: core show channeltypes\n" 00291 " Lists available channel types registered in your Asterisk server.\n"; 00292 00293 static const char show_channeltype_usage[] = 00294 "Usage: core show channeltype <name>\n" 00295 " Show details about the specified channel type, <name>.\n"; 00296 00297 static struct ast_cli_entry cli_channel[] = { 00298 { { "core", "show", "channeltypes", NULL }, 00299 show_channeltypes, "List available channel types", 00300 show_channeltypes_usage }, 00301 00302 { { "core", "show", "channeltype", NULL }, 00303 show_channeltype, "Give more details on that channel type", 00304 show_channeltype_usage, complete_channeltypes }, 00305 }; 00306 00307 /*! \brief Checks to see if a channel is needing hang up */ 00308 int ast_check_hangup(struct ast_channel *chan) 00309 { 00310 if (chan->_softhangup) /* yes if soft hangup flag set */ 00311 return 1; 00312 if (!chan->tech_pvt) /* yes if no technology private data */ 00313 return 1; 00314 if (!chan->whentohangup) /* no if no hangup scheduled */ 00315 return 0; 00316 if (chan->whentohangup > time(NULL)) /* no if hangup time has not come yet. */ 00317 return 0; 00318 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT; /* record event */ 00319 return 1; 00320 } 00321 00322 static int ast_check_hangup_locked(struct ast_channel *chan) 00323 { 00324 int res; 00325 ast_channel_lock(chan); 00326 res = ast_check_hangup(chan); 00327 ast_channel_unlock(chan); 00328 return res; 00329 } 00330 00331 /*! \brief Initiate system shutdown */ 00332 void ast_begin_shutdown(int hangup) 00333 { 00334 struct ast_channel *c; 00335 shutting_down = 1; 00336 if (hangup) { 00337 AST_LIST_LOCK(&channels); 00338 AST_LIST_TRAVERSE(&channels, c, chan_list) 00339 ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN); 00340 AST_LIST_UNLOCK(&channels); 00341 } 00342 } 00343 00344 /*! \brief returns number of active/allocated channels */ 00345 int ast_active_channels(void) 00346 { 00347 struct ast_channel *c; 00348 int cnt = 0; 00349 AST_LIST_LOCK(&channels); 00350 AST_LIST_TRAVERSE(&channels, c, chan_list) 00351 cnt++; 00352 AST_LIST_UNLOCK(&channels); 00353 return cnt; 00354 } 00355 00356 /*! \brief Cancel a shutdown in progress */ 00357 void ast_cancel_shutdown(void) 00358 { 00359 shutting_down = 0; 00360 } 00361 00362 /*! \brief Returns non-zero if Asterisk is being shut down */ 00363 int ast_shutting_down(void) 00364 { 00365 return shutting_down; 00366 } 00367 00368 /*! \brief Set when to hangup channel */ 00369 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset) 00370 { 00371 chan->whentohangup = offset ? time(NULL) + offset : 0; 00372 ast_queue_frame(chan, &ast_null_frame); 00373 return; 00374 } 00375 00376 /*! \brief Compare a offset with when to hangup channel */ 00377 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset) 00378 { 00379 time_t whentohangup; 00380 00381 if (chan->whentohangup == 0) { 00382 return (offset == 0) ? 0 : -1; 00383 } else { 00384 if (offset == 0) /* XXX why is this special ? */ 00385 return (1); 00386 else { 00387 whentohangup = offset + time (NULL); 00388 if (chan->whentohangup < whentohangup) 00389 return (1); 00390 else if (chan->whentohangup == whentohangup) 00391 return (0); 00392 else 00393 return (-1); 00394 } 00395 } 00396 } 00397 00398 /*! \brief Register a new telephony channel in Asterisk */ 00399 int ast_channel_register(const struct ast_channel_tech *tech) 00400 { 00401 struct chanlist *chan; 00402 00403 AST_LIST_LOCK(&channels); 00404 00405 AST_LIST_TRAVERSE(&backends, chan, list) { 00406 if (!strcasecmp(tech->type, chan->tech->type)) { 00407 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type); 00408 AST_LIST_UNLOCK(&channels); 00409 return -1; 00410 } 00411 } 00412 00413 if (!(chan = ast_calloc(1, sizeof(*chan)))) { 00414 AST_LIST_UNLOCK(&channels); 00415 return -1; 00416 } 00417 chan->tech = tech; 00418 AST_LIST_INSERT_HEAD(&backends, chan, list); 00419 00420 if (option_debug) 00421 ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description); 00422 00423 if (option_verbose > 1) 00424 ast_verbose(VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->tech->type, 00425 chan->tech->description); 00426 00427 AST_LIST_UNLOCK(&channels); 00428 return 0; 00429 } 00430 00431 void ast_channel_unregister(const struct ast_channel_tech *tech) 00432 { 00433 struct chanlist *chan; 00434 00435 if (option_debug) 00436 ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", tech->type); 00437 00438 AST_LIST_LOCK(&channels); 00439 00440 AST_LIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) { 00441 if (chan->tech == tech) { 00442 AST_LIST_REMOVE_CURRENT(&backends, list); 00443 free(chan); 00444 if (option_verbose > 1) 00445 ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", tech->type); 00446 break; 00447 } 00448 } 00449 AST_LIST_TRAVERSE_SAFE_END 00450 00451 AST_LIST_UNLOCK(&channels); 00452 } 00453 00454 const struct ast_channel_tech *ast_get_channel_tech(const char *name) 00455 { 00456 struct chanlist *chanls; 00457 const struct ast_channel_tech *ret = NULL; 00458 00459 if (AST_LIST_LOCK(&channels)) { 00460 ast_log(LOG_WARNING, "Unable to lock channel tech list\n"); 00461 return NULL; 00462 } 00463 00464 AST_LIST_TRAVERSE(&backends, chanls, list) { 00465 if (!strcasecmp(name, chanls->tech->type)) { 00466 ret = chanls->tech; 00467 break; 00468 } 00469 } 00470 00471 AST_LIST_UNLOCK(&channels); 00472 00473 return ret; 00474 } 00475 00476 /*! \brief Gives the string form of a given hangup cause */ 00477 const char *ast_cause2str(int cause) 00478 { 00479 int x; 00480 00481 for (x=0; x < sizeof(causes) / sizeof(causes[0]); x++) { 00482 if (causes[x].cause == cause) 00483 return causes[x].desc; 00484 } 00485 00486 return "Unknown"; 00487 } 00488 00489 /*! \brief Convert a symbolic hangup cause to number */ 00490 int ast_str2cause(const char *name) 00491 { 00492 int x; 00493 00494 for (x = 0; x < sizeof(causes) / sizeof(causes[0]); x++) 00495 if (strncasecmp(causes[x].name, name, strlen(causes[x].name)) == 0) 00496 return causes[x].cause; 00497 00498 return -1; 00499 } 00500 00501 /*! \brief Gives the string form of a given channel state. 00502 \note This function is not reentrant. 00503 */ 00504 const char *ast_state2str(enum ast_channel_state state) 00505 { 00506 char *buf; 00507 00508 switch (state) { 00509 case AST_STATE_DOWN: 00510 return "Down"; 00511 case AST_STATE_RESERVED: 00512 return "Rsrvd"; 00513 case AST_STATE_OFFHOOK: 00514 return "OffHook"; 00515 case AST_STATE_DIALING: 00516 return "Dialing"; 00517 case AST_STATE_RING: 00518 return "Ring"; 00519 case AST_STATE_RINGING: 00520 return "Ringing"; 00521 case AST_STATE_UP: 00522 return "Up"; 00523 case AST_STATE_BUSY: 00524 return "Busy"; 00525 case AST_STATE_DIALING_OFFHOOK: 00526 return "Dialing Offhook"; 00527 case AST_STATE_PRERING: 00528 return "Pre-ring"; 00529 default: 00530 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE))) 00531 return "Unknown"; 00532 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)", state); 00533 return buf; 00534 } 00535 } 00536 00537 /*! \brief Gives the string form of a given transfer capability */ 00538 char *ast_transfercapability2str(int transfercapability) 00539 { 00540 switch (transfercapability) { 00541 case AST_TRANS_CAP_SPEECH: 00542 return "SPEECH"; 00543 case AST_TRANS_CAP_DIGITAL: 00544 return "DIGITAL"; 00545 case AST_TRANS_CAP_RESTRICTED_DIGITAL: 00546 return "RESTRICTED_DIGITAL"; 00547 case AST_TRANS_CAP_3_1K_AUDIO: 00548 return "3K1AUDIO"; 00549 case AST_TRANS_CAP_DIGITAL_W_TONES: 00550 return "DIGITAL_W_TONES"; 00551 case AST_TRANS_CAP_VIDEO: 00552 return "VIDEO"; 00553 default: 00554 return "UNKNOWN"; 00555 } 00556 } 00557 00558 /*! \brief Pick the best audio codec */ 00559 int ast_best_codec(int fmts) 00560 { 00561 /* This just our opinion, expressed in code. We are asked to choose 00562 the best codec to use, given no information */ 00563 int x; 00564 static int prefs[] = 00565 { 00566 /*! Okay, ulaw is used by all telephony equipment, so start with it */ 00567 AST_FORMAT_ULAW, 00568 /*! Unless of course, you're a silly European, so then prefer ALAW */ 00569 AST_FORMAT_ALAW, 00570 /*! Okay, well, signed linear is easy to translate into other stuff */ 00571 AST_FORMAT_SLINEAR, 00572 /*! G.726 is standard ADPCM, in RFC3551 packing order */ 00573 AST_FORMAT_G726, 00574 /*! G.726 is standard ADPCM, in AAL2 packing order */ 00575 AST_FORMAT_G726_AAL2, 00576 /*! ADPCM has great sound quality and is still pretty easy to translate */ 00577 AST_FORMAT_ADPCM, 00578 /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to 00579 translate and sounds pretty good */ 00580 AST_FORMAT_GSM, 00581 /*! iLBC is not too bad */ 00582 AST_FORMAT_ILBC, 00583 /*! Speex is free, but computationally more expensive than GSM */ 00584 AST_FORMAT_SPEEX, 00585 /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough 00586 to use it */ 00587 AST_FORMAT_LPC10, 00588 /*! G.729a is faster than 723 and slightly less expensive */ 00589 AST_FORMAT_G729A, 00590 /*! Down to G.723.1 which is proprietary but at least designed for voice */ 00591 AST_FORMAT_G723_1, 00592 }; 00593 00594 /* Strip out video */ 00595 fmts &= AST_FORMAT_AUDIO_MASK; 00596 00597 /* Find the first preferred codec in the format given */ 00598 for (x=0; x < (sizeof(prefs) / sizeof(prefs[0]) ); x++) 00599 if (fmts & prefs[x]) 00600 return prefs[x]; 00601 ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts); 00602 return 0; 00603 } 00604 00605 static const struct ast_channel_tech null_tech = { 00606 .type = "NULL", 00607 .description = "Null channel (should not see this)", 00608 }; 00609 00610 /*! \brief Create a new channel structure */ 00611 struct ast_channel *ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *name_fmt, ...) 00612 { 00613 struct ast_channel *tmp; 00614 int x; 00615 int flags; 00616 struct varshead *headp; 00617 va_list ap1, ap2; 00618 00619 /* If shutting down, don't allocate any new channels */ 00620 if (shutting_down) { 00621 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n"); 00622 return NULL; 00623 } 00624 00625 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) 00626 return NULL; 00627 00628 if (!(tmp->sched = sched_context_create())) { 00629 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n"); 00630 free(tmp); 00631 return NULL; 00632 } 00633 00634 ast_string_field_init(tmp, 128); 00635 00636 /* Don't bother initializing the last two FD here, because they 00637 will *always* be set just a few lines down (AST_TIMING_FD, 00638 AST_ALERT_FD). */ 00639 for (x = 0; x < AST_MAX_FDS - 2; x++) 00640 tmp->fds[x] = -1; 00641 00642 #ifdef HAVE_ZAPTEL 00643 tmp->timingfd = open("/dev/zap/timer", O_RDWR); 00644 if (tmp->timingfd > -1) { 00645 /* Check if timing interface supports new 00646 ping/pong scheme */ 00647 flags = 1; 00648 if (!ioctl(tmp->timingfd, ZT_TIMERPONG, &flags)) 00649 needqueue = 0; 00650 } 00651 #else 00652 tmp->timingfd = -1; 00653 #endif 00654 00655 if (needqueue) { 00656 if (pipe(tmp->alertpipe)) { 00657 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe!\n"); 00658 ast_string_field_free_pools(tmp); 00659 free(tmp); 00660 return NULL; 00661 } else { 00662 flags = fcntl(tmp->alertpipe[0], F_GETFL); 00663 fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK); 00664 flags = fcntl(tmp->alertpipe[1], F_GETFL); 00665 fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK); 00666 } 00667 } else /* Make sure we've got it done right if they don't */ 00668 tmp->alertpipe[0] = tmp->alertpipe[1] = -1; 00669 00670 /* Always watch the alertpipe */ 00671 tmp->fds[AST_ALERT_FD] = tmp->alertpipe[0]; 00672 /* And timing pipe */ 00673 tmp->fds[AST_TIMING_FD] = tmp->timingfd; 00674 ast_string_field_set(tmp, name, "**Unknown**"); 00675 00676 /* Initial state */ 00677 tmp->_state = state; 00678 00679 tmp->streamid = -1; 00680 00681 tmp->fin = global_fin; 00682 tmp->fout = global_fout; 00683 00684 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) { 00685 ast_string_field_build(tmp, uniqueid, "%li.%d", (long) time(NULL), 00686 ast_atomic_fetchadd_int(&uniqueint, 1)); 00687 } else { 00688 ast_string_field_build(tmp, uniqueid, "%s-%li.%d", ast_config_AST_SYSTEM_NAME, 00689 (long) time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1)); 00690 } 00691 00692 if (!ast_strlen_zero(name_fmt)) { 00693 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call. 00694 * And they all use slightly different formats for their name string. 00695 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here. 00696 * This means, that the stringfields must have a routine that takes the va_lists directly, and 00697 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list. 00698 * This new function was written so this can be accomplished. 00699 */ 00700 va_start(ap1, name_fmt); 00701 va_start(ap2, name_fmt); 00702 ast_string_field_build_va(tmp, name, name_fmt, ap1, ap2); 00703 va_end(ap1); 00704 va_end(ap2); 00705 00706 /* and now, since the channel structure is built, and has its name, let's call the 00707 * manager event generator with this Newchannel event. This is the proper and correct 00708 * place to make this call, but you sure do have to pass a lot of data into this func 00709 * to do it here! 00710 */ 00711 manager_event(EVENT_FLAG_CALL, "Newchannel", 00712 "Channel: %s\r\n" 00713 "State: %s\r\n" 00714 "CallerIDNum: %s\r\n" 00715 "CallerIDName: %s\r\n" 00716 "Uniqueid: %s\r\n", 00717 tmp->name, ast_state2str(state), 00718 S_OR(cid_num, "<unknown>"), 00719 S_OR(cid_name, "<unknown>"), 00720 tmp->uniqueid); 00721 } 00722 00723 headp = &tmp->varshead; 00724 AST_LIST_HEAD_INIT_NOLOCK(headp); 00725 00726 ast_mutex_init(&tmp->lock); 00727 00728 AST_LIST_HEAD_INIT_NOLOCK(&tmp->datastores); 00729 00730 strcpy(tmp->context, "default"); 00731 strcpy(tmp->exten, "s"); 00732 tmp->priority = 1; 00733 00734 ast_string_field_set(tmp, language, defaultlanguage); 00735 tmp->amaflags = ast_default_amaflags; 00736 ast_string_field_set(tmp, accountcode, ast_default_accountcode); 00737 00738 tmp->tech = &null_tech; 00739 00740 AST_LIST_LOCK(&channels); 00741 AST_LIST_INSERT_HEAD(&channels, tmp, chan_list); 00742 AST_LIST_UNLOCK(&channels); 00743 00744 return tmp; 00745 } 00746 00747 /*! \brief Queue an outgoing media frame */ 00748 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin) 00749 { 00750 struct ast_frame *f; 00751 struct ast_frame *cur; 00752 int blah = 1; 00753 int qlen = 0; 00754 00755 /* Build us a copy and free the original one */ 00756 if (!(f = ast_frdup(fin))) { 00757 ast_log(LOG_WARNING, "Unable to duplicate frame\n"); 00758 return -1; 00759 } 00760 ast_channel_lock(chan); 00761 00762 /* See if the last frame on the queue is a hangup, if so don't queue anything */ 00763 if ((cur = AST_LIST_LAST(&chan->readq)) && (cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) { 00764 ast_frfree(f); 00765 ast_channel_unlock(chan); 00766 return 0; 00767 } 00768 00769 /* Count how many frames exist on the queue */ 00770 AST_LIST_TRAVERSE(&chan->readq, cur, frame_list) { 00771 qlen++; 00772 } 00773 00774 /* Allow up to 96 voice frames outstanding, and up to 128 total frames */ 00775 if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) { 00776 if (fin->frametype != AST_FRAME_VOICE) { 00777 ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name); 00778 CRASH; 00779 } else { 00780 if (option_debug) 00781 ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name); 00782 ast_frfree(f); 00783 ast_channel_unlock(chan); 00784 return 0; 00785 } 00786 } 00787 AST_LIST_INSERT_TAIL(&chan->readq, f, frame_list); 00788 if (chan->alertpipe[1] > -1) { 00789 if (write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah)) 00790 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n", 00791 chan->name, f->frametype, f->subclass, qlen, strerror(errno)); 00792 #ifdef HAVE_ZAPTEL 00793 } else if (chan->timingfd > -1) { 00794 ioctl(chan->timingfd, ZT_TIMERPING, &blah); 00795 #endif 00796 } else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) { 00797 pthread_kill(chan->blocker, SIGURG); 00798 } 00799 ast_channel_unlock(chan); 00800 return 0; 00801 } 00802 00803 /*! \brief Queue a hangup frame for channel */ 00804 int ast_queue_hangup(struct ast_channel *chan) 00805 { 00806 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP }; 00807 /* Yeah, let's not change a lock-critical value without locking */ 00808 if (!ast_channel_trylock(chan)) { 00809 chan->_softhangup |= AST_SOFTHANGUP_DEV; 00810 ast_channel_unlock(chan); 00811 } 00812 return ast_queue_frame(chan, &f); 00813 } 00814 00815 /*! \brief Queue a control frame */ 00816 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control) 00817 { 00818 struct ast_frame f = { AST_FRAME_CONTROL, }; 00819 00820 f.subclass = control; 00821 00822 return ast_queue_frame(chan, &f); 00823 } 00824 00825 /*! \brief Queue a control frame with payload */ 00826 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, 00827 const void *data, size_t datalen) 00828 { 00829 struct ast_frame f = { AST_FRAME_CONTROL, }; 00830 00831 f.subclass = control; 00832 f.data = (void *) data; 00833 f.datalen = datalen; 00834 00835 return ast_queue_frame(chan, &f); 00836 } 00837 00838 /*! \brief Set defer DTMF flag on channel */ 00839 int ast_channel_defer_dtmf(struct ast_channel *chan) 00840 { 00841 int pre = 0; 00842 00843 if (chan) { 00844 pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF); 00845 ast_set_flag(chan, AST_FLAG_DEFER_DTMF); 00846 } 00847 return pre; 00848 } 00849 00850 /*! \brief Unset defer DTMF flag on channel */ 00851 void ast_channel_undefer_dtmf(struct ast_channel *chan) 00852 { 00853 if (chan) 00854 ast_clear_flag(chan, AST_FLAG_DEFER_DTMF); 00855 } 00856 00857 /*! 00858 * \brief Helper function to find channels. 00859 * 00860 * It supports these modes: 00861 * 00862 * prev != NULL : get channel next in list after prev 00863 * name != NULL : get channel with matching name 00864 * name != NULL && namelen != 0 : get channel whose name starts with prefix 00865 * exten != NULL : get channel whose exten or macroexten matches 00866 * context != NULL && exten != NULL : get channel whose context or macrocontext 00867 * 00868 * It returns with the channel's lock held. If getting the individual lock fails, 00869 * unlock and retry quickly up to 10 times, then give up. 00870 * 00871 * \note XXX Note that this code has cost O(N) because of the need to verify 00872 * that the object is still on the global list. 00873 * 00874 * \note XXX also note that accessing fields (e.g. c->name in ast_log()) 00875 * can only be done with the lock held or someone could delete the 00876 * object while we work on it. This causes some ugliness in the code. 00877 * Note that removing the first ast_log() may be harmful, as it would 00878 * shorten the retry period and possibly cause failures. 00879 * We should definitely go for a better scheme that is deadlock-free. 00880 */ 00881 static struct ast_channel *channel_find_locked(const struct ast_channel *prev, 00882 const char *name, const int namelen, 00883 const char *context, const char *exten) 00884 { 00885 const char *msg = prev ? "deadlock" : "initial deadlock"; 00886 int retries; 00887 struct ast_channel *c; 00888 00889 for (retries = 0; retries < 10; retries++) { 00890 int done; 00891 AST_LIST_LOCK(&channels); 00892 AST_LIST_TRAVERSE(&channels, c, chan_list) { 00893 if (prev) { /* look for next item */ 00894 if (c != prev) /* not this one */ 00895 continue; 00896 /* found, prepare to return c->next */ 00897 c = AST_LIST_NEXT(c, chan_list); 00898 } 00899 if (name) { /* want match by name */ 00900 if ((!namelen && strcasecmp(c->name, name)) || 00901 (namelen && strncasecmp(c->name, name, namelen))) 00902 continue; /* name match failed */ 00903 } else if (exten) { 00904 if (context && strcasecmp(c->context, context) && 00905 strcasecmp(c->macrocontext, context)) 00906 continue; /* context match failed */ 00907 if (strcasecmp(c->exten, exten) && 00908 strcasecmp(c->macroexten, exten)) 00909 continue; /* exten match failed */ 00910 } 00911 /* if we get here, c points to the desired record */ 00912 break; 00913 } 00914 /* exit if chan not found or mutex acquired successfully */ 00915 /* this is slightly unsafe, as we _should_ hold the lock to access c->name */ 00916 done = c == NULL || ast_channel_trylock(c) == 0; 00917 if (!done) { 00918 if (option_debug) 00919 ast_log(LOG_DEBUG, "Avoiding %s for channel '%p'\n", msg, c); 00920 } 00921 AST_LIST_UNLOCK(&channels); 00922 if (done) 00923 return c; 00924 usleep(1); /* give other threads a chance before retrying */ 00925 } 00926 /* 00927 * c is surely not null, but we don't have the lock so cannot 00928 * access c->name 00929 */ 00930 if (option_debug) 00931 ast_log(LOG_DEBUG, "Failure, could not lock '%p' after %d retries!\n", 00932 c, retries); 00933 00934 return NULL; 00935 } 00936 00937 /*! \brief Browse channels in use */ 00938 struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev) 00939 { 00940 return channel_find_locked(prev, NULL, 0, NULL, NULL); 00941 } 00942 00943 /*! \brief Get channel by name and lock it */ 00944 struct ast_channel *ast_get_channel_by_name_locked(const char *name) 00945 { 00946 return channel_find_locked(NULL, name, 0, NULL, NULL); 00947 } 00948 00949 /*! \brief Get channel by name prefix and lock it */ 00950 struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen) 00951 { 00952 return channel_find_locked(NULL, name, namelen, NULL, NULL); 00953 } 00954 00955 /*! \brief Get next channel by name prefix and lock it */ 00956 struct ast_channel *ast_walk_channel_by_name_prefix_locked(const struct ast_channel *chan, const char *name, 00957 const int namelen) 00958 { 00959 return channel_find_locked(chan, name, namelen, NULL, NULL); 00960 } 00961 00962 /*! \brief Get channel by exten (and optionally context) and lock it */ 00963 struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context) 00964 { 00965 return channel_find_locked(NULL, NULL, 0, context, exten); 00966 } 00967 00968 /*! \brief Get next channel by exten (and optionally context) and lock it */ 00969 struct ast_channel *ast_walk_channel_by_exten_locked(const struct ast_channel *chan, const char *exten, 00970 const char *context) 00971 { 00972 return channel_find_locked(chan, NULL, 0, context, exten); 00973 } 00974 00975 /*! \brief Wait, look for hangups and condition arg */ 00976 int ast_safe_sleep_conditional(struct ast_channel *chan, int ms, int (*cond)(void*), void *data) 00977 { 00978 struct ast_frame *f; 00979 00980 while (ms > 0) { 00981 if (cond && ((*cond)(data) == 0)) 00982 return 0; 00983 ms = ast_waitfor(chan, ms); 00984 if (ms < 0) 00985 return -1; 00986 if (ms > 0) { 00987 f = ast_read(chan); 00988 if (!f) 00989 return -1; 00990 ast_frfree(f); 00991 } 00992 } 00993 return 0; 00994 } 00995 00996 /*! \brief Wait, look for hangups */ 00997 int ast_safe_sleep(struct ast_channel *chan, int ms) 00998 { 00999 return ast_safe_sleep_conditional(chan, ms, NULL, NULL); 01000 } 01001 01002 static void free_cid(struct ast_callerid *cid) 01003 { 01004 if (cid->cid_dnid) 01005 free(cid->cid_dnid); 01006 if (cid->cid_num) 01007 free(cid->cid_num); 01008 if (cid->cid_name) 01009 free(cid->cid_name); 01010 if (cid->cid_ani) 01011 free(cid->cid_ani); 01012 if (cid->cid_rdnis) 01013 free(cid->cid_rdnis); 01014 cid->cid_dnid = cid->cid_num = cid->cid_name = cid->cid_ani = cid->cid_rdnis = NULL; 01015 } 01016 01017 /*! \brief Free a channel structure */ 01018 void ast_channel_free(struct ast_channel *chan) 01019 { 01020 int fd; 01021 struct ast_var_t *vardata; 01022 struct ast_frame *f; 01023 struct varshead *headp; 01024 struct ast_datastore *datastore = NULL; 01025 char name[AST_CHANNEL_NAME]; 01026 01027 headp=&chan->varshead; 01028 01029 AST_LIST_LOCK(&channels); 01030 AST_LIST_REMOVE(&channels, chan, chan_list); 01031 /* Lock and unlock the channel just to be sure nobody 01032 has it locked still */ 01033 ast_channel_lock(chan); 01034 ast_channel_unlock(chan); 01035 if (chan->tech_pvt) { 01036 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name); 01037 free(chan->tech_pvt); 01038 } 01039 01040 if (chan->sched) 01041 sched_context_destroy(chan->sched); 01042 01043 ast_copy_string(name, chan->name, sizeof(name)); 01044 01045 /* Stop monitoring */ 01046 if (chan->monitor) 01047 chan->monitor->stop( chan, 0 ); 01048 01049 /* If there is native format music-on-hold state, free it */ 01050 if (chan->music_state) 01051 ast_moh_cleanup(chan); 01052 01053 /* if someone is whispering on the channel, stop them */ 01054 if (chan->whisper) 01055 ast_channel_whisper_stop(chan); 01056 01057 /* Free translators */ 01058 if (chan->readtrans) 01059 ast_translator_free_path(chan->readtrans); 01060 if (chan->writetrans) 01061 ast_translator_free_path(chan->writetrans); 01062 if (chan->pbx) 01063 ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name); 01064 free_cid(&chan->cid); 01065 ast_mutex_destroy(&chan->lock); 01066 /* Close pipes if appropriate */ 01067 if ((fd = chan->alertpipe[0]) > -1) 01068 close(fd); 01069 if ((fd = chan->alertpipe[1]) > -1) 01070 close(fd); 01071 if ((fd = chan->timingfd) > -1) 01072 close(fd); 01073 while ((f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list))) 01074 ast_frfree(f); 01075 01076 /* Get rid of each of the data stores on the channel */ 01077 while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry))) 01078 /* Free the data store */ 01079 ast_channel_datastore_free(datastore); 01080 AST_LIST_HEAD_INIT_NOLOCK(&chan->datastores); 01081 01082 /* loop over the variables list, freeing all data and deleting list items */ 01083 /* no need to lock the list, as the channel is already locked */ 01084 01085 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) 01086 ast_var_delete(vardata); 01087 01088 /* Destroy the jitterbuffer */ 01089 ast_jb_destroy(chan); 01090 01091 ast_string_field_free_pools(chan); 01092 free(chan); 01093 AST_LIST_UNLOCK(&channels); 01094 01095 ast_device_state_changed_literal(name); 01096 } 01097 01098 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, char *uid) 01099 { 01100 struct ast_datastore *datastore = NULL; 01101 01102 /* Make sure we at least have type so we can identify this */ 01103 if (info == NULL) { 01104 return NULL; 01105 } 01106 01107 /* Allocate memory for datastore and clear it */ 01108 datastore = ast_calloc(1, sizeof(*datastore)); 01109 if (datastore == NULL) { 01110 return NULL; 01111 } 01112 01113 datastore->info = info; 01114 01115 datastore->uid = ast_strdup(uid); 01116 01117 return datastore; 01118 } 01119 01120 int ast_channel_datastore_free(struct ast_datastore *datastore) 01121 { 01122 int res = 0; 01123 01124 /* Using the destroy function (if present) destroy the data */ 01125 if (datastore->info->destroy != NULL && datastore->data != NULL) { 01126 datastore->info->destroy(datastore->data); 01127 datastore->data = NULL; 01128 } 01129 01130 /* Free allocated UID memory */ 01131 if (datastore->uid != NULL) { 01132 free(datastore->uid); 01133 datastore->uid = NULL; 01134 } 01135 01136 /* Finally free memory used by ourselves */ 01137 free(datastore); 01138 01139 return res; 01140 } 01141 01142 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore) 01143 { 01144 int res = 0; 01145 01146 AST_LIST_INSERT_HEAD(&chan->datastores, datastore, entry); 01147 01148 return res; 01149 } 01150 01151 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore) 01152 { 01153 struct ast_datastore *datastore2 = NULL; 01154 int res = -1; 01155 01156 /* Find our position and remove ourselves */ 01157 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->datastores, datastore2, entry) { 01158 if (datastore2 == datastore) { 01159 AST_LIST_REMOVE_CURRENT(&chan->datastores, entry); 01160 res = 0; 01161 break; 01162 } 01163 } 01164 AST_LIST_TRAVERSE_SAFE_END 01165 01166 return res; 01167 } 01168 01169 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, char *uid) 01170 { 01171 struct ast_datastore *datastore = NULL; 01172 01173 if (info == NULL) 01174 return NULL; 01175 01176 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->datastores, datastore, entry) { 01177 if (datastore->info == info) { 01178 if (uid != NULL && datastore->uid != NULL) { 01179 if (!strcasecmp(uid, datastore->uid)) { 01180 /* Matched by type AND uid */ 01181 break; 01182 } 01183 } else { 01184 /* Matched by type at least */ 01185 break; 01186 } 01187 } 01188 } 01189 AST_LIST_TRAVERSE_SAFE_END 01190 01191 return datastore; 01192 } 01193 01194 int ast_channel_spy_add(struct ast_channel *chan, struct ast_channel_spy *spy) 01195 { 01196 /* Link the owner channel to the spy */ 01197 spy->chan = chan; 01198 01199 if (!ast_test_flag(spy, CHANSPY_FORMAT_AUDIO)) { 01200 ast_log(LOG_WARNING, "Could not add channel spy '%s' to channel '%s', only audio format spies are supported.\n", 01201 spy->type, chan->name); 01202 return -1; 01203 } 01204 01205 if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST) && (spy->read_queue.format != AST_FORMAT_SLINEAR)) { 01206 ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n", 01207 ast_getformatname(spy->read_queue.format)); 01208 return -1; 01209 } 01210 01211 if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST) && (spy->write_queue.format != AST_FORMAT_SLINEAR)) { 01212 ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n", 01213 ast_getformatname(spy->write_queue.format)); 01214 return -1; 01215 } 01216 01217 if (ast_test_flag(spy, CHANSPY_MIXAUDIO) && 01218 ((spy->read_queue.format != AST_FORMAT_SLINEAR) || 01219 (spy->write_queue.format != AST_FORMAT_SLINEAR))) { 01220 ast_log(LOG_WARNING, "Cannot provide audio mixing on '%s'-'%s' format spies\n", 01221 ast_getformatname(spy->read_queue.format), ast_getformatname(spy->write_queue.format)); 01222 return -1; 01223 } 01224 01225 if (!chan->spies) { 01226 if (!(chan->spies = ast_calloc(1, sizeof(*chan->spies)))) { 01227 return -1; 01228 } 01229 01230 AST_LIST_HEAD_INIT_NOLOCK(&chan->spies->list); 01231 AST_LIST_INSERT_HEAD(&chan->spies->list, spy, list); 01232 } else { 01233 AST_LIST_INSERT_TAIL(&chan->spies->list, spy, list); 01234 } 01235 01236 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) { 01237 ast_cond_init(&spy->trigger, NULL); 01238 ast_set_flag(spy, CHANSPY_TRIGGER_READ); 01239 ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE); 01240 } 01241 01242 if (option_debug) 01243 ast_log(LOG_DEBUG, "Spy %s added to channel %s\n", 01244 spy->type, chan->name); 01245 01246 return 0; 01247 } 01248 01249 /* Clean up a channel's spy information */ 01250 static void spy_cleanup(struct ast_channel *chan) 01251 { 01252 if (!AST_LIST_EMPTY(&chan->spies->list)) 01253 return; 01254 if (chan->spies->read_translator.path) 01255 ast_translator_free_path(chan->spies->read_translator.path); 01256 if (chan->spies->write_translator.path) 01257 ast_translator_free_path(chan->spies->write_translator.path); 01258 free(chan->spies); 01259 chan->spies = NULL; 01260 return; 01261 } 01262 01263 /* Detach a spy from it's channel */ 01264 static void spy_detach(struct ast_channel_spy *spy, struct ast_channel *chan) 01265 { 01266 ast_mutex_lock(&spy->lock); 01267 01268 /* We only need to poke them if they aren't already done */ 01269 if (spy->status != CHANSPY_DONE) { 01270 /* Indicate to the spy to stop */ 01271 spy->status = CHANSPY_STOP; 01272 spy->chan = NULL; 01273 /* Poke the spy if needed */ 01274 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) 01275 ast_cond_signal(&spy->trigger); 01276 } 01277 01278 /* Print it out while we still have a lock so the structure can't go away (if signalled above) */ 01279 if (option_debug) 01280 ast_log(LOG_DEBUG, "Spy %s removed from channel %s\n", spy->type, chan->name); 01281 01282 ast_mutex_unlock(&spy->lock); 01283 01284 return; 01285 } 01286 01287 void ast_channel_spy_stop_by_type(struct ast_channel *chan, const char *type) 01288 { 01289 struct ast_channel_spy *spy = NULL; 01290 01291 if (!chan->spies) 01292 return; 01293 01294 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) { 01295 ast_mutex_lock(&spy->lock); 01296 if ((spy->type == type) && (spy->status == CHANSPY_RUNNING)) { 01297 ast_mutex_unlock(&spy->lock); 01298 AST_LIST_REMOVE_CURRENT(&chan->spies->list, list); 01299 spy_detach(spy, chan); 01300 } else 01301 ast_mutex_unlock(&spy->lock); 01302 } 01303 AST_LIST_TRAVERSE_SAFE_END 01304 spy_cleanup(chan); 01305 } 01306 01307 void ast_channel_spy_trigger_wait(struct ast_channel_spy *spy) 01308 { 01309 struct timeval tv; 01310 struct timespec ts; 01311 01312 tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000)); 01313 ts.tv_sec = tv.tv_sec; 01314 ts.tv_nsec = tv.tv_usec * 1000; 01315 01316 ast_cond_timedwait(&spy->trigger, &spy->lock, &ts); 01317 } 01318 01319 void ast_channel_spy_remove(struct ast_channel *chan, struct ast_channel_spy *spy) 01320 { 01321 if (!chan->spies) 01322 return; 01323 01324 AST_LIST_REMOVE(&chan->spies->list, spy, list); 01325 spy_detach(spy, chan); 01326 spy_cleanup(chan); 01327 } 01328 01329 void ast_channel_spy_free(struct ast_channel_spy *spy) 01330 { 01331 struct ast_frame *f = NULL; 01332 01333 if (spy->status == CHANSPY_DONE) 01334 return; 01335 01336 /* Switch status to done in case we get called twice */ 01337 spy->status = CHANSPY_DONE; 01338 01339 /* Drop any frames in the queue */ 01340 while ((f = AST_LIST_REMOVE_HEAD(&spy->write_queue.list, frame_list))) 01341 ast_frfree(f); 01342 while ((f = AST_LIST_REMOVE_HEAD(&spy->read_queue.list, frame_list))) 01343 ast_frfree(f); 01344 01345 /* Destroy the condition if in use */ 01346 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) 01347 ast_cond_destroy(&spy->trigger); 01348 01349 /* Destroy our mutex since it is no longer in use */ 01350 ast_mutex_destroy(&spy->lock); 01351 01352 return; 01353 } 01354 01355 static void detach_spies(struct ast_channel *chan) 01356 { 01357 struct ast_channel_spy *spy = NULL; 01358 01359 if (!chan->spies) 01360 return; 01361 01362 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) { 01363 AST_LIST_REMOVE_CURRENT(&chan->spies->list, list); 01364 spy_detach(spy, chan); 01365 } 01366 AST_LIST_TRAVERSE_SAFE_END 01367 01368 spy_cleanup(chan); 01369 } 01370 01371 /*! \brief Softly hangup a channel, don't lock */ 01372 int ast_softhangup_nolock(struct ast_channel *chan, int cause) 01373 { 01374 if (option_debug) 01375 ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name); 01376 /* Inform channel driver that we need to be hung up, if it cares */ 01377 chan->_softhangup |= cause; 01378 ast_queue_frame(chan, &ast_null_frame); 01379 /* Interrupt any poll call or such */ 01380 if (ast_test_flag(chan, AST_FLAG_BLOCKING)) 01381 pthread_kill(chan->blocker, SIGURG); 01382 return 0; 01383 } 01384 01385 /*! \brief Softly hangup a channel, lock */ 01386 int ast_softhangup(struct ast_channel *chan, int cause) 01387 { 01388 int res; 01389 ast_channel_lock(chan); 01390 res = ast_softhangup_nolock(chan, cause); 01391 ast_channel_unlock(chan); 01392 return res; 01393 } 01394 01395 enum spy_direction { 01396 SPY_READ, 01397 SPY_WRITE, 01398 }; 01399 01400 #define SPY_QUEUE_SAMPLE_LIMIT 4000 /* half of one second */ 01401 01402 static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f, enum spy_direction dir) 01403 { 01404 struct ast_frame *translated_frame = NULL; 01405 struct ast_channel_spy *spy; 01406 struct channel_spy_trans *trans; 01407 01408 trans = (dir == SPY_READ) ? &chan->spies->read_translator : &chan->spies->write_translator; 01409 01410 AST_LIST_TRAVERSE(&chan->spies->list, spy, list) { 01411 struct ast_channel_spy_queue *queue; 01412 struct ast_frame *duped_fr; 01413 01414 ast_mutex_lock(&spy->lock); 01415 01416 queue = (dir == SPY_READ) ? &spy->read_queue : &spy->write_queue; 01417 01418 if ((queue->format == AST_FORMAT_SLINEAR) && (f->subclass != AST_FORMAT_SLINEAR)) { 01419 if (!translated_frame) { 01420 if (trans->path && (trans->last_format != f->subclass)) { 01421 ast_translator_free_path(trans->path); 01422 trans->path = NULL; 01423 } 01424 if (!trans->path) { 01425 if (option_debug) 01426 ast_log(LOG_DEBUG, "Building translator from %s to SLINEAR for spies on channel %s\n", 01427 ast_getformatname(f->subclass), chan->name); 01428 if ((trans->path = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) { 01429 ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n", 01430 ast_getformatname(f->subclass), ast_getformatname(AST_FORMAT_SLINEAR)); 01431 ast_mutex_unlock(&spy->lock); 01432 continue; 01433 } else { 01434 trans->last_format = f->subclass; 01435 } 01436 } 01437 if (!(translated_frame = ast_translate(trans->path, f, 0))) { 01438 ast_log(LOG_ERROR, "Translation to %s failed, dropping frame for spies\n", 01439 ast_getformatname(AST_FORMAT_SLINEAR)); 01440 ast_mutex_unlock(&spy->lock); 01441 break; 01442 } 01443 } 01444 duped_fr = ast_frdup(translated_frame); 01445 } else if (f->subclass != queue->format) { 01446 ast_log(LOG_WARNING, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n", 01447 spy->type, chan->name, 01448 ast_getformatname(queue->format), ast_getformatname(f->subclass)); 01449 ast_mutex_unlock(&spy->lock); 01450 continue; 01451 } else 01452 duped_fr = ast_frdup(f); 01453 01454 AST_LIST_INSERT_TAIL(&queue->list, duped_fr, frame_list); 01455 01456 queue->samples += f->samples; 01457 01458 if (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) { 01459 if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) { 01460 switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) { 01461 case CHANSPY_TRIGGER_READ: 01462 if (dir == SPY_WRITE) { 01463 ast_set_flag(spy, CHANSPY_TRIGGER_WRITE); 01464 ast_clear_flag(spy, CHANSPY_TRIGGER_READ); 01465 if (option_debug) 01466 ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to write-trigger mode\n", 01467 spy->type, chan->name); 01468 } 01469 break; 01470 case CHANSPY_TRIGGER_WRITE: 01471 if (dir == SPY_READ) { 01472 ast_set_flag(spy, CHANSPY_TRIGGER_READ); 01473 ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE); 01474 if (option_debug) 01475 ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to read-trigger mode\n", 01476 spy->type, chan->name); 01477 } 01478 break; 01479 } 01480 if (option_debug) 01481 ast_log(LOG_DEBUG, "Triggering queue flush for spy '%s' on '%s'\n", 01482 spy->type, chan->name); 01483 ast_set_flag(spy, CHANSPY_TRIGGER_FLUSH); 01484 ast_cond_signal(&spy->trigger); 01485 } else { 01486 if (option_debug) 01487 ast_log(LOG_DEBUG, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n", 01488 spy->type, chan->name, (dir == SPY_READ) ? "read" : "write"); 01489 while (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) { 01490 struct ast_frame *drop = AST_LIST_REMOVE_HEAD(&queue->list, frame_list); 01491 queue->samples -= drop->samples; 01492 ast_frfree(drop); 01493 } 01494 } 01495 } else { 01496 switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) { 01497 case CHANSPY_TRIGGER_READ: 01498 if (dir == SPY_READ) 01499 ast_cond_signal(&spy->trigger); 01500 break; 01501 case CHANSPY_TRIGGER_WRITE: 01502 if (dir == SPY_WRITE) 01503 ast_cond_signal(&spy->trigger); 01504 break; 01505 } 01506 } 01507 01508 ast_mutex_unlock(&spy->lock); 01509 } 01510 01511 if (translated_frame) 01512 ast_frfree(translated_frame); 01513 } 01514 01515 static void free_translation(struct ast_channel *clone) 01516 { 01517 if (clone->writetrans) 01518 ast_translator_free_path(clone->writetrans); 01519 if (clone->readtrans) 01520 ast_translator_free_path(clone->readtrans); 01521 clone->writetrans = NULL; 01522 clone->readtrans = NULL; 01523 clone->rawwriteformat = clone->nativeformats; 01524 clone->rawreadformat = clone->nativeformats; 01525 } 01526 01527 /*! \brief Hangup a channel */ 01528 int ast_hangup(struct ast_channel *chan) 01529 { 01530 int res = 0; 01531 01532 /* Don't actually hang up a channel that will masquerade as someone else, or 01533 if someone is going to masquerade as us */ 01534 ast_channel_lock(chan); 01535 01536 detach_spies(chan); /* get rid of spies */ 01537 01538 if (chan->masq) { 01539 if (ast_do_masquerade(chan)) 01540 ast_log(LOG_WARNING, "Failed to perform masquerade\n"); 01541 } 01542 01543 if (chan->masq) { 01544 ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name); 01545 ast_channel_unlock(chan); 01546 return 0; 01547 } 01548 /* If this channel is one which will be masqueraded into something, 01549 mark it as a zombie already, so we know to free it later */ 01550 if (chan->masqr) { 01551 ast_set_flag(chan, AST_FLAG_ZOMBIE); 01552 ast_channel_unlock(chan); 01553 return 0; 01554 } 01555 free_translation(chan); 01556 /* Close audio stream */ 01557 if (chan->stream) { 01558 ast_closestream(chan->stream); 01559 chan->stream = NULL; 01560 } 01561 /* Close video stream */ 01562 if (chan->vstream) { 01563 ast_closestream(chan->vstream); 01564 chan->vstream = NULL; 01565 } 01566 if (chan->sched) { 01567 sched_context_destroy(chan->sched); 01568 chan->sched = NULL; 01569 } 01570 01571 if (chan->generatordata) /* Clear any tone stuff remaining */ 01572 if (chan->generator && chan->generator->release) 01573 chan->generator->release(chan, chan->generatordata); 01574 chan->generatordata = NULL; 01575 chan->generator = NULL; 01576 if (chan->cdr) { /* End the CDR if it hasn't already */ 01577 ast_cdr_end(chan->cdr); 01578 ast_cdr_detach(chan->cdr); /* Post and Free the CDR */ 01579 chan->cdr = NULL; 01580 } 01581 if (ast_test_flag(chan, AST_FLAG_BLOCKING)) { 01582 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd " 01583 "is blocked by thread %ld in procedure %s! Expect a failure\n", 01584 (long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc); 01585 CRASH; 01586 } 01587 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE)) { 01588 if (option_debug) 01589 ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name); 01590 if (chan->tech->hangup) 01591 res = chan->tech->hangup(chan); 01592 } else { 01593 if (option_debug) 01594 ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name); 01595 } 01596 01597 ast_channel_unlock(chan); 01598 manager_event(EVENT_FLAG_CALL, "Hangup", 01599 "Channel: %s\r\n" 01600 "Uniqueid: %s\r\n" 01601 "Cause: %d\r\n" 01602 "Cause-txt: %s\r\n", 01603 chan->name, 01604 chan->uniqueid, 01605 chan->hangupcause, 01606 ast_cause2str(chan->hangupcause) 01607 ); 01608 ast_channel_free(chan); 01609 return res; 01610 } 01611 01612 int __ast_answer(struct ast_channel *chan, unsigned int delay) 01613 { 01614 int res = 0; 01615 01616 ast_channel_lock(chan); 01617 01618 /* You can't answer an outbound call */ 01619 if (ast_test_flag(chan, AST_FLAG_OUTGOING)) { 01620 ast_channel_unlock(chan); 01621 return 0; 01622 } 01623 01624 /* Stop if we're a zombie or need a soft hangup */ 01625 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) { 01626 ast_channel_unlock(chan); 01627 return -1; 01628 } 01629 01630 switch (chan->_state) { 01631 case AST_STATE_RINGING: 01632 case AST_STATE_RING: 01633 if (chan->tech->answer) 01634 res = chan->tech->answer(chan); 01635 ast_setstate(chan, AST_STATE_UP); 01636 ast_cdr_answer(chan->cdr); 01637 ast_channel_unlock(chan); 01638 if (delay) 01639 ast_safe_sleep(chan, delay); 01640 return res; 01641 break; 01642 case AST_STATE_UP: 01643 ast_cdr_answer(chan->cdr); 01644 break; 01645 default: 01646 break; 01647 } 01648 01649 ast_channel_unlock(chan); 01650 01651 return res; 01652 } 01653 01654 int ast_answer(struct ast_channel *chan) 01655 { 01656 return __ast_answer(chan, 500); 01657 } 01658 01659 void ast_deactivate_generator(struct ast_channel *chan) 01660 { 01661 ast_channel_lock(chan); 01662 if (chan->generatordata) { 01663 if (chan->generator && chan->generator->release) 01664 chan->generator->release(chan, chan->generatordata); 01665 chan->generatordata = NULL; 01666 chan->generator = NULL; 01667 chan->fds[AST_GENERATOR_FD] = -1; 01668 ast_clear_flag(chan, AST_FLAG_WRITE_INT); 01669 ast_settimeout(chan, 0, NULL, NULL); 01670 } 01671 ast_channel_unlock(chan); 01672 } 01673 01674 static int generator_force(void *data) 01675 { 01676 /* Called if generator doesn't have data */ 01677 void *tmp; 01678 int res; 01679 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples); 01680 struct ast_channel *chan = data; 01681 tmp = chan->generatordata; 01682 chan->generatordata = NULL; 01683 generate = chan->generator->generate; 01684 res = generate(chan, tmp, 0, 160); 01685 chan->generatordata = tmp; 01686 if (res) { 01687 if (option_debug) 01688 ast_log(LOG_DEBUG, "Auto-deactivating generator\n"); 01689 ast_deactivate_generator(chan); 01690 } 01691 return 0; 01692 } 01693 01694 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params) 01695 { 01696 int res = 0; 01697 01698 ast_channel_lock(chan); 01699 01700 if (chan->generatordata) { 01701 if (chan->generator && chan->generator->release) 01702 chan->generator->release(chan, chan->generatordata); 01703 chan->generatordata = NULL; 01704 } 01705 01706 ast_prod(chan); 01707 if (gen->alloc && !(chan->generatordata = gen->alloc(chan, params))) { 01708 res = -1; 01709 } 01710 01711 if (!res) { 01712 ast_settimeout(chan, 160, generator_force, chan); 01713 chan->generator = gen; 01714 } 01715 01716 ast_channel_unlock(chan); 01717 01718 return res; 01719 } 01720 01721 /*! \brief Wait for x amount of time on a file descriptor to have input. */ 01722 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception) 01723 { 01724 int winner = -1; 01725 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms); 01726 return winner; 01727 } 01728 01729 /*! \brief Wait for x amount of time on a file descriptor to have input. */ 01730 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds, 01731 int *exception, int *outfd, int *ms) 01732 { 01733 struct timeval start = { 0 , 0 }; 01734 struct pollfd *pfds; 01735 int res; 01736 long rms; 01737 int x, y, max; 01738 int sz; 01739 time_t now = 0; 01740 long whentohangup = 0, diff; 01741 struct ast_channel *winner = NULL; 01742 struct fdmap { 01743 int chan; 01744 int fdno; 01745 } *fdmap; 01746 01747 sz = n * AST_MAX_FDS + nfds; 01748 pfds = alloca(sizeof(*pfds) * sz); 01749 fdmap = alloca(sizeof(*fdmap) * sz); 01750 01751 if (outfd) 01752 *outfd = -99999; 01753 if (exception) 01754 *exception = 0; 01755 01756 /* Perform any pending masquerades */ 01757 for (x=0; x < n; x++) { 01758 ast_channel_lock(c[x]); 01759 if (c[x]->masq) { 01760 if (ast_do_masquerade(c[x])) { 01761 ast_log(LOG_WARNING, "Masquerade failed\n"); 01762 *ms = -1; 01763 ast_channel_unlock(c[x]); 01764 return NULL; 01765 } 01766 } 01767 if (c[x]->whentohangup) { 01768 if (!whentohangup) 01769 time(&now); 01770 diff = c[x]->whentohangup - now; 01771 if (diff < 1) { 01772 /* Should already be hungup */ 01773 c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT; 01774 ast_channel_unlock(c[x]); 01775 return c[x]; 01776 } 01777 if (!whentohangup || (diff < whentohangup)) 01778 whentohangup = diff; 01779 } 01780 ast_channel_unlock(c[x]); 01781 } 01782 /* Wait full interval */ 01783 rms = *ms; 01784 if (whentohangup) { 01785 rms = whentohangup * 1000; /* timeout in milliseconds */ 01786 if (*ms >= 0 && *ms < rms) /* original *ms still smaller */ 01787 rms = *ms; 01788 } 01789 /* 01790 * Build the pollfd array, putting the channels' fds first, 01791 * followed by individual fds. Order is important because 01792 * individual fd's must have priority over channel fds. 01793 */ 01794 max = 0; 01795 for (x=0; x<n; x++) { 01796 for (y=0; y<AST_MAX_FDS; y++) { 01797 fdmap[max].fdno = y; /* fd y is linked to this pfds */ 01798 fdmap[max].chan = x; /* channel x is linked to this pfds */ 01799 max += ast_add_fd(&pfds[max], c[x]->fds[y]); 01800 } 01801 CHECK_BLOCKING(c[x]); 01802 } 01803 /* Add the individual fds */ 01804 for (x=0; x<nfds; x++) { 01805 fdmap[max].chan = -1; 01806 max += ast_add_fd(&pfds[max], fds[x]); 01807 } 01808 01809 if (*ms > 0) 01810 start = ast_tvnow(); 01811 01812 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */ 01813 do { 01814 int kbrms = rms; 01815 if (kbrms > 600000) 01816 kbrms = 600000; 01817 res = poll(pfds, max, kbrms); 01818 if (!res) 01819 rms -= kbrms; 01820 } while (!res && (rms > 0)); 01821 } else { 01822 res = poll(pfds, max, rms); 01823 } 01824 for (x=0; x<n; x++) 01825 ast_clear_flag(c[x], AST_FLAG_BLOCKING); 01826 if (res < 0) { /* Simulate a timeout if we were interrupted */ 01827 if (errno != EINTR) 01828 *ms = -1; 01829 return NULL; 01830 } 01831 if (whentohangup) { /* if we have a timeout, check who expired */ 01832 time(&now); 01833 for (x=0; x<n; x++) { 01834 if (c[x]->whentohangup && now >= c[x]->whentohangup) { 01835 c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT; 01836 if (winner == NULL) 01837 winner = c[x]; 01838 } 01839 } 01840 } 01841 if (res == 0) { /* no fd ready, reset timeout and done */ 01842 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */ 01843 return winner; 01844 } 01845 /* 01846 * Then check if any channel or fd has a pending event. 01847 * Remember to check channels first and fds last, as they 01848 * must have priority on setting 'winner' 01849 */ 01850 for (x = 0; x < max; x++) { 01851 res = pfds[x].revents; 01852 if (res == 0) 01853 continue; 01854 if (fdmap[x].chan >= 0) { /* this is a channel */ 01855 winner = c[fdmap[x].chan]; /* override previous winners */ 01856 if (res & POLLPRI) 01857 ast_set_flag(winner, AST_FLAG_EXCEPTION); 01858 else 01859 ast_clear_flag(winner, AST_FLAG_EXCEPTION); 01860 winner->fdno = fdmap[x].fdno; 01861 } else { /* this is an fd */ 01862 if (outfd) 01863 *outfd = pfds[x].fd; 01864 if (exception) 01865 *exception = (res & POLLPRI) ? -1 : 0; 01866 winner = NULL; 01867 } 01868 } 01869 if (*ms > 0) { 01870 *ms -= ast_tvdiff_ms(ast_tvnow(), start); 01871 if (*ms < 0) 01872 *ms = 0; 01873 } 01874 return winner; 01875 } 01876 01877 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms) 01878 { 01879 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms); 01880 } 01881 01882 int ast_waitfor(struct ast_channel *c, int ms) 01883 { 01884 int oldms = ms; /* -1 if no timeout */ 01885 01886 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms); 01887 if ((ms < 0) && (oldms < 0)) 01888 ms = 0; 01889 return ms; 01890 } 01891 01892 /* XXX never to be called with ms = -1 */ 01893 int ast_waitfordigit(struct ast_channel *c, int ms) 01894 { 01895 return ast_waitfordigit_full(c, ms, -1, -1); 01896 } 01897 01898 int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data) 01899 { 01900 int res = -1; 01901 #ifdef HAVE_ZAPTEL 01902 if (c->timingfd > -1) { 01903 if (!func) { 01904 samples = 0; 01905 data = 0; 01906 } 01907 if (option_debug) 01908 ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples); 01909 res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples); 01910 c->timingfunc = func; 01911 c->timingdata = data; 01912 } 01913 #endif 01914 return res; 01915 } 01916 01917 int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd) 01918 { 01919 01920 /* Stop if we're a zombie or need a soft hangup */ 01921 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c)) 01922 return -1; 01923 /* Wait for a digit, no more than ms milliseconds total. */ 01924 while (ms) { 01925 struct ast_channel *rchan; 01926 int outfd; 01927 01928 errno = 0; 01929 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms); 01930 if (!rchan && outfd < 0 && ms) { 01931 if (errno == 0 || errno == EINTR) 01932 continue; 01933 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno)); 01934 return -1; 01935 } else if (outfd > -1) { 01936 /* The FD we were watching has something waiting */ 01937 return 1; 01938 } else if (rchan) { 01939 int res; 01940 struct ast_frame *f = ast_read(c); 01941 if (!f) 01942 return -1; 01943 01944 switch (f->frametype) { 01945 case AST_FRAME_DTMF: 01946 res = f->subclass; 01947 ast_frfree(f); 01948 return res; 01949 case AST_FRAME_CONTROL: 01950 switch (f->subclass) { 01951 case AST_CONTROL_HANGUP: 01952 ast_frfree(f); 01953 return -1; 01954 case AST_CONTROL_RINGING: 01955 case AST_CONTROL_ANSWER: 01956 /* Unimportant */ 01957 break; 01958 default: 01959 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass); 01960 break; 01961 } 01962 break; 01963 case AST_FRAME_VOICE: 01964 /* Write audio if appropriate */ 01965 if (audiofd > -1) 01966 write(audiofd, f->data, f->datalen); 01967 default: 01968 /* Ignore */ 01969 break; 01970 } 01971 ast_frfree(f); 01972 } 01973 } 01974 return 0; /* Time is up */ 01975 } 01976 01977 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio) 01978 { 01979 struct ast_frame *f = NULL; /* the return value */ 01980 int blah; 01981 int prestate; 01982 01983 /* this function is very long so make sure there is only one return 01984 * point at the end (there is only one exception to this). 01985 */ 01986 ast_channel_lock(chan); 01987 if (chan->masq) { 01988 if (ast_do_masquerade(chan)) 01989 ast_log(LOG_WARNING, "Failed to perform masquerade\n"); 01990 else 01991 f = &ast_null_frame; 01992 goto done; 01993 } 01994 01995 /* Stop if we're a zombie or need a soft hangup */ 01996 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) { 01997 if (chan->generator) 01998 ast_deactivate_generator(chan); 01999 goto done; 02000 } 02001 prestate = chan->_state; 02002 02003 if (!ast_test_flag(chan, AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF | AST_FLAG_IN_DTMF) && 02004 !ast_strlen_zero(chan->dtmfq)) { 02005 /* We have DTMF that has been deferred. Return it now */ 02006 chan->dtmff.subclass = chan->dtmfq[0]; 02007 /* Drop first digit from the buffer */ 02008 memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1); 02009 f = &chan->dtmff; 02010 if (ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY)) 02011 chan->dtmff.frametype = AST_FRAME_DTMF_END; 02012 else { 02013 chan->dtmff.frametype = AST_FRAME_DTMF_BEGIN; 02014 ast_set_flag(chan, AST_FLAG_EMULATE_DTMF); 02015 chan->emulate_dtmf_digit = f->subclass; 02016 chan->emulate_dtmf_duration = AST_DEFAULT_EMULATE_DTMF_DURATION; 02017 chan->dtmf_begin_tv = ast_tvnow(); 02018 } 02019 goto done; 02020 } 02021 02022 /* Read and ignore anything on the alertpipe, but read only 02023 one sizeof(blah) per frame that we send from it */ 02024 if (chan->alertpipe[0] > -1) 02025 read(chan->alertpipe[0], &blah, sizeof(blah)); 02026 02027 #ifdef HAVE_ZAPTEL 02028 if (chan->timingfd > -1 && chan->fdno == AST_TIMING_FD && ast_test_flag(chan, AST_FLAG_EXCEPTION)) { 02029 int res; 02030 02031 ast_clear_flag(chan, AST_FLAG_EXCEPTION); 02032 blah = -1; 02033 /* IF we can't get event, assume it's an expired as-per the old interface */ 02034 res = ioctl(chan->timingfd, ZT_GETEVENT, &blah); 02035 if (res) 02036 blah = ZT_EVENT_TIMER_EXPIRED; 02037 02038 if (blah == ZT_EVENT_TIMER_PING) { 02039 if (AST_LIST_EMPTY(&chan->readq) || !AST_LIST_NEXT(AST_LIST_FIRST(&chan->readq), frame_list)) { 02040 /* Acknowledge PONG unless we need it again */ 02041 if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) { 02042 ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno)); 02043 } 02044 } 02045 } else if (blah == ZT_EVENT_TIMER_EXPIRED) { 02046 ioctl(chan->timingfd, ZT_TIMERACK, &blah); 02047 if (chan->timingfunc) { 02048 /* save a copy of func/data before unlocking the channel */ 02049 int (*func)(void *) = chan->timingfunc; 02050 void *data = chan->timingdata; 02051 ast_channel_unlock(chan); 02052 func(data); 02053 } else { 02054 blah = 0; 02055 ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah); 02056 chan->timingdata = NULL; 02057 ast_channel_unlock(chan); 02058 } 02059 /* cannot 'goto done' because the channel is already unlocked */ 02060 return &ast_null_frame; 02061 } else 02062 ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name); 02063 } else 02064 #endif 02065 if (chan->fds[AST_GENERATOR_FD] > -1 && chan->fdno == AST_GENERATOR_FD) { 02066 /* if the AST_GENERATOR_FD is set, call the generator with args 02067 * set to -1 so it can do whatever it needs to. 02068 */ 02069 void *tmp = chan->generatordata; 02070 chan->generatordata = NULL; /* reset to let ast_write get through */ 02071 chan->generator->generate(chan, tmp, -1, -1); 02072 chan->generatordata = tmp; 02073 f = &ast_null_frame; 02074 goto done; 02075 } 02076 02077 /* Check for pending read queue */ 02078 if (!AST_LIST_EMPTY(&chan->readq)) { 02079 f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list); 02080 /* Interpret hangup and return NULL */ 02081 /* XXX why not the same for frames from the channel ? */ 02082 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP) { 02083 ast_frfree(f); 02084 f = NULL; 02085 } 02086 } else { 02087 chan->blocker = pthread_self(); 02088 if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) { 02089 if (chan->tech->exception) 02090 f = chan->tech->exception(chan); 02091 else { 02092 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name); 02093 f = &ast_null_frame; 02094 } 02095 /* Clear the exception flag */ 02096 ast_clear_flag(chan, AST_FLAG_EXCEPTION); 02097 } else if (chan->tech->read) 02098 f = chan->tech->read(chan); 02099 else 02100 ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name); 02101 } 02102 02103 if (f) { 02104 /* if the channel driver returned more than one frame, stuff the excess 02105 into the readq for the next ast_read call (note that we can safely assume 02106 that the readq is empty, because otherwise we would not have called into 02107 the channel driver and f would be only a single frame) 02108 */ 02109 if (AST_LIST_NEXT(f, frame_list)) { 02110 AST_LIST_HEAD_SET_NOLOCK(&chan->readq, AST_LIST_NEXT(f, frame_list)); 02111 AST_LIST_NEXT(f, frame_list) = NULL; 02112 } 02113 02114 switch (f->frametype) { 02115 case AST_FRAME_CONTROL: 02116 if (f->subclass == AST_CONTROL_ANSWER) { 02117 if (!ast_test_flag(chan, AST_FLAG_OUTGOING)) { 02118 if (option_debug) 02119 ast_log(LOG_DEBUG, "Ignoring answer on an inbound call!\n"); 02120 ast_frfree(f); 02121 f = &ast_null_frame; 02122 } else if (prestate == AST_STATE_UP) { 02123 if (option_debug) 02124 ast_log(LOG_DEBUG, "Dropping duplicate answer!\n"); 02125 ast_frfree(f); 02126 f = &ast_null_frame; 02127 } else { 02128 /* Answer the CDR */ 02129 ast_setstate(chan, AST_STATE_UP); 02130 ast_cdr_answer(chan->cdr); 02131 } 02132 } 02133 break; 02134 case AST_FRAME_DTMF_END: 02135 ast_log(LOG_DTMF, "DTMF end '%c' received on %s\n", f->subclass, chan->name); 02136 /* Queue it up if DTMF is deffered, or if DTMF emulation is forced. 02137 * However, only let emulation be forced if the other end cares about BEGIN frames */ 02138 if ( ast_test_flag(chan, AST_FLAG_DEFER_DTMF) || 02139 (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF) && !ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY)) ) { 02140 if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2) 02141 chan->dtmfq[strlen(chan->dtmfq)] = f->subclass; 02142 else 02143 ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name); 02144 ast_frfree(f); 02145 f = &ast_null_frame; 02146 } else if (!ast_test_flag(chan, AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) { 02147 f->frametype = AST_FRAME_DTMF_BEGIN; 02148 ast_set_flag(chan, AST_FLAG_EMULATE_DTMF); 02149 chan->emulate_dtmf_digit = f->subclass; 02150 chan->dtmf_begin_tv = ast_tvnow(); 02151 if (f->len) 02152 chan->emulate_dtmf_duration = f->len; 02153 else 02154 chan->emulate_dtmf_duration = AST_DEFAULT_EMULATE_DTMF_DURATION; 02155 } else { 02156 ast_clear_flag(chan, AST_FLAG_IN_DTMF); 02157 if (!f->len) 02158 f->len = ast_tvdiff_ms(chan->dtmf_begin_tv, ast_tvnow()); 02159 } 02160 break; 02161 case AST_FRAME_DTMF_BEGIN: 02162 ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass, chan->name); 02163 if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY)) { 02164 ast_frfree(f); 02165 f = &ast_null_frame; 02166 } else { 02167 ast_set_flag(chan, AST_FLAG_IN_DTMF); 02168 chan->dtmf_begin_tv = ast_tvnow(); 02169 } 02170 break; 02171 case AST_FRAME_VOICE: 02172 /* The EMULATE_DTMF flag must be cleared here as opposed to when the samples 02173 * first get to zero, because we want to make sure we pass at least one 02174 * voice frame through before starting the next digit, to ensure a gap 02175 * between DTMF digits. */ 02176 if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF) && !chan->emulate_dtmf_duration) { 02177 ast_clear_flag(chan, AST_FLAG_EMULATE_DTMF); 02178 chan->emulate_dtmf_digit = 0; 02179 } 02180 02181 if (dropaudio || ast_test_flag(chan, AST_FLAG_IN_DTMF)) { 02182 ast_frfree(f); 02183 f = &ast_null_frame; 02184 } else if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF)) { 02185 if ((f->samples / 8) >= chan->emulate_dtmf_duration) { /* XXX 8kHz */ 02186 chan->emulate_dtmf_duration = 0; 02187 f->frametype = AST_FRAME_DTMF_END; 02188 f->subclass = chan->emulate_dtmf_digit; 02189 } else { 02190 chan->emulate_dtmf_duration -= f->samples / 8; /* XXX 8kHz */ 02191 ast_frfree(f); 02192 f = &ast_null_frame; 02193 } 02194 } else if (!(f->subclass & chan->nativeformats)) { 02195 /* This frame can't be from the current native formats -- drop it on the 02196 floor */ 02197 ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n", 02198 chan->name, ast_getformatname(f->subclass), ast_getformatname(chan->nativeformats)); 02199 ast_frfree(f); 02200 f = &ast_null_frame; 02201 } else { 02202 if (chan->spies) 02203 queue_frame_to_spies(chan, f, SPY_READ); 02204 02205 if (chan->monitor && chan->monitor->read_stream ) { 02206 /* XXX what does this do ? */ 02207 #ifndef MONITOR_CONSTANT_DELAY 02208 int jump = chan->outsmpl - chan->insmpl - 4 * f->samples; 02209 if (jump >= 0) { 02210 jump = chan->outsmpl - chan->insmpl; 02211 if (ast_seekstream(chan->monitor->read_stream, jump, SEEK_FORCECUR) == -1) 02212 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n"); 02213 chan->insmpl += jump + f->samples; 02214 } else 02215 chan->insmpl+= f->samples; 02216 #else 02217 int jump = chan->outsmpl - chan->insmpl; 02218 if (jump - MONITOR_DELAY >= 0) { 02219 if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1) 02220 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n"); 02221 chan->insmpl += jump; 02222 } else 02223 chan->insmpl += f->samples; 02224 #endif 02225 if (chan->monitor->state == AST_MONITOR_RUNNING) { 02226 if (ast_writestream(chan->monitor->read_stream, f) < 0) 02227 ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n"); 02228 } 02229 } 02230 02231 if (chan->readtrans && (f = ast_translate(chan->readtrans, f, 1)) == NULL) 02232 f = &ast_null_frame; 02233 02234 /* Run generator sitting on the line if timing device not available 02235 * and synchronous generation of outgoing frames is necessary */ 02236 if (chan->generatordata && !ast_internal_timing_enabled(chan)) { 02237 void *tmp = chan->generatordata; 02238 int res; 02239 02240 if (chan->timingfunc) { 02241 if (option_debug > 1) 02242 ast_log(LOG_DEBUG, "Generator got voice, switching to phase locked mode\n"); 02243 ast_settimeout(chan, 0, NULL, NULL); 02244 } 02245 02246 chan->generatordata = NULL; /* reset, to let writes go through */ 02247 res = chan->generator->generate(chan, tmp, f->datalen, f->samples); 02248 chan->generatordata = tmp; 02249 if (res) { 02250 if (option_debug > 1) 02251 ast_log(LOG_DEBUG, "Auto-deactivating generator\n"); 02252 ast_deactivate_generator(chan); 02253 } 02254 02255 } else if (f->frametype == AST_FRAME_CNG) { 02256 if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) { 02257 if (option_debug > 1) 02258 ast_log(LOG_DEBUG, "Generator got CNG, switching to timed mode\n"); 02259 ast_settimeout(chan, 160, generator_force, chan); 02260 } 02261 } 02262 } 02263 default: 02264 /* Just pass it on! */ 02265 break; 02266 } 02267 } else { 02268 /* Make sure we always return NULL in the future */ 02269 chan->_softhangup |= AST_SOFTHANGUP_DEV; 02270 if (chan->generator) 02271 ast_deactivate_generator(chan); 02272 /* End the CDR if appropriate */ 02273 if (chan->cdr) 02274 ast_cdr_end(chan->cdr); 02275 } 02276 02277 /* High bit prints debugging */ 02278 if (chan->fin & DEBUGCHAN_FLAG) 02279 ast_frame_dump(chan->name, f, "<<"); 02280 chan->fin = FRAMECOUNT_INC(chan->fin); 02281 02282 done: 02283 ast_channel_unlock(chan); 02284 return f; 02285 } 02286 02287 int ast_internal_timing_enabled(struct ast_channel *chan) 02288 { 02289 int ret = ast_opt_internal_timing && chan->timingfd > -1; 02290 if (option_debug > 4) 02291 ast_log(LOG_DEBUG, "Internal timing is %s (option_internal_timing=%d chan->timingfd=%d)\n", ret? "enabled": "disabled", ast_opt_internal_timing, chan->timingfd); 02292 return ret; 02293 } 02294 02295 struct ast_frame *ast_read(struct ast_channel *chan) 02296 { 02297 return __ast_read(chan, 0); 02298 } 02299 02300 struct ast_frame *ast_read_noaudio(struct ast_channel *chan) 02301 { 02302 return __ast_read(chan, 1); 02303 } 02304 02305 int ast_indicate(struct ast_channel *chan, int condition) 02306 { 02307 return ast_indicate_data(chan, condition, NULL, 0); 02308 } 02309 02310 int ast_indicate_data(struct ast_channel *chan, int condition, const void *data, size_t datalen) 02311 { 02312 int res = -1; 02313 02314 ast_channel_lock(chan); 02315 /* Stop if we're a zombie or need a soft hangup */ 02316 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) { 02317 ast_channel_unlock(chan); 02318 return -1; 02319 } 02320 if (chan->tech->indicate) 02321 res = chan->tech->indicate(chan, condition, data, datalen); 02322 ast_channel_unlock(chan); 02323 if (!chan->tech->indicate || res) { 02324 /* 02325 * Device does not support (that) indication, lets fake 02326 * it by doing our own tone generation. (PM2002) 02327 */ 02328 if (condition < 0) 02329 ast_playtones_stop(chan); 02330 else { 02331 const struct ind_tone_zone_sound *ts = NULL; 02332 switch (condition) { 02333 case AST_CONTROL_RINGING: 02334 ts = ast_get_indication_tone(chan->zone, "ring"); 02335 break; 02336 case AST_CONTROL_BUSY: 02337 ts = ast_get_indication_tone(chan->zone, "busy"); 02338 break; 02339 case AST_CONTROL_CONGESTION: 02340 ts = ast_get_indication_tone(chan->zone, "congestion"); 02341 break; 02342 } 02343 if (ts && ts->data[0]) { 02344 if (option_debug) 02345 ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition); 02346 ast_playtones_start(chan,0,ts->data, 1); 02347 res = 0; 02348 } else if (condition == AST_CONTROL_PROGRESS) { 02349 /* ast_playtones_stop(chan); */ 02350 } else if (condition == AST_CONTROL_PROCEEDING) { 02351 /* Do nothing, really */ 02352 } else if (condition == AST_CONTROL_HOLD) { 02353 /* Do nothing.... */ 02354 } else if (condition == AST_CONTROL_UNHOLD) { 02355 /* Do nothing.... */ 02356 } else if (condition == AST_CONTROL_VIDUPDATE) { 02357 /* Do nothing.... */ 02358 } else { 02359 /* not handled */ 02360 ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name); 02361 res = -1; 02362 } 02363 } 02364 } 02365 return res; 02366 } 02367 02368 int ast_recvchar(struct ast_channel *chan, int timeout) 02369 { 02370 int c; 02371 char *buf = ast_recvtext(chan, timeout); 02372 if (buf == NULL) 02373 return -1; /* error or timeout */ 02374 c = *(unsigned char *)buf; 02375 free(buf); 02376 return c; 02377 } 02378 02379 char *ast_recvtext(struct ast_channel *chan, int timeout) 02380 { 02381 int res, done = 0; 02382 char *buf = NULL; 02383 02384 while (!done) { 02385 struct ast_frame *f; 02386 if (ast_check_hangup(chan)) 02387 break; 02388 res = ast_waitfor(chan, timeout); 02389 if (res <= 0) /* timeout or error */ 02390 break; 02391 timeout = res; /* update timeout */ 02392 f = ast_read(chan); 02393 if (f == NULL) 02394 break; /* no frame */ 02395 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP) 02396 done = 1; /* force a break */ 02397 else if (f->frametype == AST_FRAME_TEXT) { /* what we want */ 02398 buf = ast_strndup((char *) f->data, f->datalen); /* dup and break */ 02399 done = 1; 02400 } 02401 ast_frfree(f); 02402 } 02403 return buf; 02404 } 02405 02406 int ast_sendtext(struct ast_channel *chan, const char *text) 02407 { 02408 int res = 0; 02409 /* Stop if we're a zombie or need a soft hangup */ 02410 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) 02411 return -1; 02412 CHECK_BLOCKING(chan); 02413 if (chan->tech->send_text) 02414 res = chan->tech->send_text(chan, text); 02415 ast_clear_flag(chan, AST_FLAG_BLOCKING); 02416 return res; 02417 } 02418 02419 int ast_senddigit_begin(struct ast_channel *chan, char digit) 02420 { 02421 int res = -1; 02422 02423 if (chan->tech->send_digit_begin) 02424 res = chan->tech->send_digit_begin(chan, digit); 02425 02426 if (res) { 02427 /* 02428 * Device does not support DTMF tones, lets fake 02429 * it by doing our own generation. (PM2002) 02430 */ 02431 static const char* dtmf_tones[] = { 02432 "!941+1336/100,!0/100", /* 0 */ 02433 "!697+1209/100,!0/100", /* 1 */ 02434 "!697+1336/100,!0/100", /* 2 */ 02435 "!697+1477/100,!0/100", /* 3 */ 02436 "!770+1209/100,!0/100", /* 4 */ 02437 "!770+1336/100,!0/100", /* 5 */ 02438 "!770+1477/100,!0/100", /* 6 */ 02439 "!852+1209/100,!0/100", /* 7 */ 02440 "!852+1336/100,!0/100", /* 8 */ 02441 "!852+1477/100,!0/100", /* 9 */ 02442 "!697+1633/100,!0/100", /* A */ 02443 "!770+1633/100,!0/100", /* B */ 02444 "!852+1633/100,!0/100", /* C */ 02445 "!941+1633/100,!0/100", /* D */ 02446 "!941+1209/100,!0/100", /* * */ 02447 "!941+1477/100,!0/100" }; /* # */ 02448 if (digit >= '0' && digit <='9') 02449 ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0); 02450 else if (digit >= 'A' && digit <= 'D') 02451 ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0); 02452 else if (digit == '*') 02453 ast_playtones_start(chan, 0, dtmf_tones[14], 0); 02454 else if (digit == '#') 02455 ast_playtones_start(chan, 0, dtmf_tones[15], 0); 02456 else { 02457 /* not handled */ 02458 if (option_debug) 02459 ast_log(LOG_DEBUG, "Unable to generate DTMF tone '%c' for '%s'\n", digit, chan->name); 02460 } 02461 } 02462 02463 return 0; 02464 } 02465 02466 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration) 02467 { 02468 int res = -1; 02469 02470 if (chan->tech->send_digit_end) 02471 res = chan->tech->send_digit_end(chan, digit, duration); 02472 02473 if (res && chan->generator) 02474 ast_playtones_stop(chan); 02475 02476 return 0; 02477 } 02478 02479 int ast_senddigit(struct ast_channel *chan, char digit) 02480 { 02481 if (!ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY)) { 02482 ast_senddigit_begin(chan, digit); 02483 ast_safe_sleep(chan, 100); /* XXX 100ms ... probably should be configurable */ 02484 } 02485 02486 return ast_senddigit_end(chan, digit, 100); 02487 } 02488 02489 int ast_prod(struct ast_channel *chan) 02490 { 02491 struct ast_frame a = { AST_FRAME_VOICE }; 02492 char nothing[128]; 02493 02494 /* Send an empty audio frame to get things moving */ 02495 if (chan->_state != AST_STATE_UP) { 02496 if (option_debug) 02497 ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name); 02498 a.subclass = chan->rawwriteformat; 02499 a.data = nothing + AST_FRIENDLY_OFFSET; 02500 a.src = "ast_prod"; 02501 if (ast_write(chan, &a)) 02502 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name); 02503 } 02504 return 0; 02505 } 02506 02507 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr) 02508 { 02509 int res; 02510 if (!chan->tech->write_video) 02511 return 0; 02512 res = ast_write(chan, fr); 02513 if (!res) 02514 res = 1; 02515 return res; 02516 } 02517 02518 int ast_write(struct ast_channel *chan, struct ast_frame *fr) 02519 { 02520 int res = -1; 02521 struct ast_frame *f = NULL; 02522 02523 /* Stop if we're a zombie or need a soft hangup */ 02524 ast_channel_lock(chan); 02525 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) 02526 goto done; 02527 02528 /* Handle any pending masquerades */ 02529 if (chan->masq && ast_do_masquerade(chan)) { 02530 ast_log(LOG_WARNING, "Failed to perform masquerade\n"); 02531 goto done; 02532 } 02533 if (chan->masqr) { 02534 res = 0; /* XXX explain, why 0 ? */ 02535 goto done; 02536 } 02537 if (chan->generatordata) { 02538 if (ast_test_flag(chan, AST_FLAG_WRITE_INT)) 02539 ast_deactivate_generator(chan); 02540 else { 02541 res = 0; /* XXX explain, why 0 ? */ 02542 goto done; 02543 } 02544 } 02545 /* High bit prints debugging */ 02546 if (chan->fout & DEBUGCHAN_FLAG) 02547 ast_frame_dump(chan->name, fr, ">>"); 02548 CHECK_BLOCKING(chan); 02549 switch (fr->frametype) { 02550 case AST_FRAME_CONTROL: 02551 res = (chan->tech->indicate == NULL) ? 0 : 02552 chan->tech->indicate(chan, fr->subclass, fr->data, fr->datalen); 02553 break; 02554 case AST_FRAME_DTMF_BEGIN: 02555 ast_clear_flag(chan, AST_FLAG_BLOCKING); 02556 ast_channel_unlock(chan); 02557 res = ast_senddigit_begin(chan, fr->subclass); 02558 ast_channel_lock(chan); 02559 CHECK_BLOCKING(chan); 02560 break; 02561 case AST_FRAME_DTMF_END: 02562 ast_clear_flag(chan, AST_FLAG_BLOCKING); 02563 ast_channel_unlock(chan); 02564 res = ast_senddigit_end(chan, fr->subclass, fr->len); 02565 ast_channel_lock(chan); 02566 CHECK_BLOCKING(chan); 02567 break; 02568 case AST_FRAME_TEXT: 02569 res = (chan->tech->send_text == NULL) ? 0 : 02570 chan->tech->send_text(chan, (char *) fr->data); 02571 break; 02572 case AST_FRAME_HTML: 02573 res = (chan->tech->send_html == NULL) ? 0 : 02574 chan->tech->send_html(chan, fr->subclass, (char *) fr->data, fr->datalen); 02575 break; 02576 case AST_FRAME_VIDEO: 02577 /* XXX Handle translation of video codecs one day XXX */ 02578 res = (chan->tech->write_video == NULL) ? 0 : 02579 chan->tech->write_video(chan, fr); 02580 break; 02581 case AST_FRAME_MODEM: 02582 res = (chan->tech->write == NULL) ? 0 : 02583 chan->tech->write(chan, fr); 02584 break; 02585 case AST_FRAME_VOICE: 02586 if (chan->tech->write == NULL) 02587 break; /*! \todo XXX should return 0 maybe ? */ 02588 02589 /* If someone is whispering on this channel then we must ensure that we are always getting signed linear frames */ 02590 if (ast_test_flag(chan, AST_FLAG_WHISPER)) { 02591 if (fr->subclass == AST_FORMAT_SLINEAR) 02592 f = fr; 02593 else { 02594 ast_mutex_lock(&chan->whisper->lock); 02595 if (chan->writeformat != AST_FORMAT_SLINEAR) { 02596 /* Rebuild the translation path and set our write format back to signed linear */ 02597 chan->whisper->original_format = chan->writeformat; 02598 ast_set_write_format(chan, AST_FORMAT_SLINEAR); 02599 if (chan->whisper->path) 02600 ast_translator_free_path(chan->whisper->path); 02601 chan->whisper->path = ast_translator_build_path(AST_FORMAT_SLINEAR, chan->whisper->original_format); 02602 } 02603 /* Translate frame using the above translation path */ 02604 f = (chan->whisper->path) ? ast_translate(chan->whisper->path, fr, 0) : fr; 02605 ast_mutex_unlock(&chan->whisper->lock); 02606 } 02607 } else { 02608 /* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */ 02609 if (fr->subclass == chan->rawwriteformat) 02610 f = fr; 02611 else 02612 f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr; 02613 } 02614 02615 /* If we have no frame of audio, then we have to bail out */ 02616 if (f == NULL) { 02617 res = 0; 02618 break; 02619 } 02620 02621 /* If spies are on the channel then queue the frame out to them */ 02622 if (chan->spies) 02623 queue_frame_to_spies(chan, f, SPY_WRITE); 02624 02625 /* If Monitor is running on this channel, then we have to write frames out there too */ 02626 if (chan->monitor && chan->monitor->write_stream) { 02627 /* XXX must explain this code */ 02628 #ifndef MONITOR_CONSTANT_DELAY 02629 int jump = chan->insmpl - chan->outsmpl - 4 * f->samples; 02630 if (jump >= 0) { 02631 jump = chan->insmpl - chan->outsmpl; 02632 if (ast_seekstream(chan->monitor->write_stream, jump, SEEK_FORCECUR) == -1) 02633 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n"); 02634 chan->outsmpl += jump + f->samples; 02635 } else 02636 chan->outsmpl += f->samples; 02637 #else 02638 int jump = chan->insmpl - chan->outsmpl; 02639 if (jump - MONITOR_DELAY >= 0) { 02640 if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1) 02641 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n"); 02642 chan->outsmpl += jump; 02643 } else 02644 chan->outsmpl += f->samples; 02645 #endif 02646 if (chan->monitor->state == AST_MONITOR_RUNNING) { 02647 if (ast_writestream(chan->monitor->write_stream, f) < 0) 02648 ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n"); 02649 } 02650 } 02651 02652 /* Finally the good part! Write this out to the channel */ 02653 if (ast_test_flag(chan, AST_FLAG_WHISPER)) { 02654 /* frame is assumed to be in SLINEAR, since that is 02655 required for whisper mode */ 02656 ast_frame_adjust_volume(f, -2); 02657 if (ast_slinfactory_available(&chan->whisper->sf) >= f->samples) { 02658 short buf[f->samples]; 02659 struct ast_frame whisper = { 02660 .frametype = AST_FRAME_VOICE, 02661 .subclass = AST_FORMAT_SLINEAR, 02662 .data = buf, 02663 .datalen = sizeof(buf), 02664 .samples = f->samples, 02665 }; 02666 02667 ast_mutex_lock(&chan->whisper->lock); 02668 if (ast_slinfactory_read(&chan->whisper->sf, buf, f->samples)) 02669 ast_frame_slinear_sum(f, &whisper); 02670 ast_mutex_unlock(&chan->whisper->lock); 02671 } 02672 /* and now put it through the regular translator */ 02673 f = (chan->writetrans) ? ast_translate(chan->writetrans, f, 0) : f; 02674 } 02675 if (f) 02676 res = chan->tech->write(chan, f); 02677 else 02678 res = 0; 02679 break; 02680 case AST_FRAME_NULL: 02681 case AST_FRAME_IAX: 02682 /* Ignore these */ 02683 res = 0; 02684 break; 02685 default: 02686 /* At this point, fr is the incoming frame and f is NULL. Channels do 02687 * not expect to get NULL as a frame pointer and will segfault. Hence, 02688 * we output the original frame passed in. */ 02689 res = chan->tech->write(chan, fr); 02690 break; 02691 } 02692 02693 if (f && f != fr) 02694 ast_frfree(f); 02695 ast_clear_flag(chan, AST_FLAG_BLOCKING); 02696 /* Consider a write failure to force a soft hangup */ 02697 if (res < 0) 02698 chan->_softhangup |= AST_SOFTHANGUP_DEV; 02699 else { 02700 chan->fout = FRAMECOUNT_INC(chan->fout); 02701 } 02702 done: 02703 ast_channel_unlock(chan); 02704 return res; 02705 } 02706 02707 static int set_format(struct ast_channel *chan, int fmt, int *rawformat, int *format, 02708 struct ast_trans_pvt **trans, const int direction) 02709 { 02710 int native; 02711 int res; 02712 02713 /* Make sure we only consider audio */ 02714 fmt &= AST_FORMAT_AUDIO_MASK; 02715 02716 native = chan->nativeformats; 02717 /* Find a translation path from the native format to one of the desired formats */ 02718 if (!direction) 02719 /* reading */ 02720 res = ast_translator_best_choice(&fmt, &native); 02721 else 02722 /* writing */ 02723 res = ast_translator_best_choice(&native, &fmt); 02724 02725 if (res < 0) { 02726 ast_log(LOG_WARNING, "Unable to find a codec translation path from %s to %s\n", 02727 ast_getformatname(native), ast_getformatname(fmt)); 02728 return -1; 02729 } 02730 02731 /* Now we have a good choice for both. */ 02732 ast_channel_lock(chan); 02733 02734 if ((*rawformat == native) && (*format == fmt) && ((*rawformat == *format) || (*trans))) { 02735 /* the channel is already in these formats, so nothing to do */ 02736 ast_channel_unlock(chan); 02737 return 0; 02738 } 02739 02740 *rawformat = native; 02741 /* User perspective is fmt */ 02742 *format = fmt; 02743 /* Free any read translation we have right now */ 02744 if (*trans) 02745 ast_translator_free_path(*trans); 02746 /* Build a translation path from the raw format to the desired format */ 02747 if (!direction) 02748 /* reading */ 02749 *trans = ast_translator_build_path(*format, *rawformat); 02750 else 02751 /* writing */ 02752 *trans = ast_translator_build_path(*rawformat, *format); 02753 ast_channel_unlock(chan); 02754 if (option_debug) 02755 ast_log(LOG_DEBUG, "Set channel %s to %s format %s\n", chan->name, 02756 direction ? "write" : "read", ast_getformatname(fmt)); 02757 return 0; 02758 } 02759 02760 int ast_set_read_format(struct ast_channel *chan, int fmt) 02761 { 02762 return set_format(chan, fmt, &chan->rawreadformat, &chan->readformat, 02763 &chan->readtrans, 0); 02764 } 02765 02766 int ast_set_write_format(struct ast_channel *chan, int fmt) 02767 { 02768 return set_format(chan, fmt, &chan->rawwriteformat, &chan->writeformat, 02769 &chan->writetrans, 1); 02770 } 02771 02772 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) 02773 { 02774 int dummy_outstate; 02775 int cause = 0; 02776 struct ast_channel *chan; 02777 int res = 0; 02778 02779 if (outstate) 02780 *outstate = 0; 02781 else 02782 outstate = &dummy_outstate; /* make outstate always a valid pointer */ 02783 02784 chan = ast_request(type, format, data, &cause); 02785 if (!chan) { 02786 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data); 02787 /* compute error and return */ 02788 if (cause == AST_CAUSE_BUSY) 02789 *outstate = AST_CONTROL_BUSY; 02790 else if (cause == AST_CAUSE_CONGESTION) 02791 *outstate = AST_CONTROL_CONGESTION; 02792 return NULL; 02793 } 02794 02795 if (oh) { 02796 if (oh->vars) 02797 ast_set_variables(chan, oh->vars); 02798 /* XXX why is this necessary, for the parent_channel perhaps ? */ 02799 if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) 02800 ast_set_callerid(chan, oh->cid_num, oh->cid_name, oh->cid_num); 02801 if (oh->parent_channel) 02802 ast_channel_inherit_variables(oh->parent_channel, chan); 02803 if (oh->account) 02804 ast_cdr_setaccount(chan, oh->account); 02805 } 02806 ast_set_callerid(chan, cid_num, cid_name, cid_num); 02807 02808 if (ast_call(chan, data, 0)) { /* ast_call failed... */ 02809 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data); 02810 } else { 02811 res = 1; /* mark success in case chan->_state is already AST_STATE_UP */ 02812 while (timeout && chan->_state != AST_STATE_UP) { 02813 struct ast_frame *f; 02814 res = ast_waitfor(chan, timeout); 02815 if (res <= 0) /* error, timeout, or done */ 02816 break; 02817 if (timeout > -1) 02818 timeout = res; 02819 f = ast_read(chan); 02820 if (!f) { 02821 *outstate = AST_CONTROL_HANGUP; 02822 res = 0; 02823 break; 02824 } 02825 if (f->frametype == AST_FRAME_CONTROL) { 02826 switch (f->subclass) { 02827 case AST_CONTROL_RINGING: /* record but keep going */ 02828 *outstate = f->subclass; 02829 break; 02830 02831 case AST_CONTROL_BUSY: 02832 case AST_CONTROL_CONGESTION: 02833 case AST_CONTROL_ANSWER: 02834 *outstate = f->subclass; 02835 timeout = 0; /* trick to force exit from the while() */ 02836 break; 02837 02838 /* Ignore these */ 02839 case AST_CONTROL_PROGRESS: 02840 case AST_CONTROL_PROCEEDING: 02841 case AST_CONTROL_HOLD: 02842 case AST_CONTROL_UNHOLD: 02843 case AST_CONTROL_VIDUPDATE: 02844 case -1: /* Ignore -- just stopping indications */ 02845 break; 02846 02847 default: 02848 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass); 02849 } 02850 } 02851 ast_frfree(f); 02852 } 02853 } 02854 02855 /* Final fixups */ 02856 if (oh) { 02857 if (!ast_strlen_zero(oh->context)) 02858 ast_copy_string(chan->context, oh->context, sizeof(chan->context)); 02859 if (!ast_strlen_zero(oh->exten)) 02860 ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten)); 02861 if (oh->priority) 02862 chan->priority = oh->priority; 02863 } 02864 if (chan->_state == AST_STATE_UP) 02865 *outstate = AST_CONTROL_ANSWER; 02866 02867 if (res <= 0) { 02868 if (!chan->cdr && (chan->cdr = ast_cdr_alloc())) 02869 ast_cdr_init(chan->cdr, chan); 02870 if (chan->cdr) { 02871 char tmp[256]; 02872 snprintf(tmp, sizeof(tmp), "%s/%s", type, (char *)data); 02873 ast_cdr_setapp(chan->cdr,"Dial",tmp); 02874 ast_cdr_update(chan); 02875 ast_cdr_start(chan->cdr); 02876 ast_cdr_end(chan->cdr); 02877 /* If the cause wasn't handled properly */ 02878 if (ast_cdr_disposition(chan->cdr,chan->hangupcause)) 02879 ast_cdr_failed(chan->cdr); 02880 } 02881 ast_hangup(chan); 02882 chan = NULL; 02883 } 02884 return chan; 02885 } 02886 02887 struct ast_channel *ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname) 02888 { 02889 return __ast_request_and_dial(type, format, data, timeout, outstate, cidnum, cidname, NULL); 02890 } 02891 02892 struct ast_channel *ast_request(const char *type, int format, void *data, int *cause) 02893 { 02894 struct chanlist *chan; 02895 struct ast_channel *c; 02896 int capabilities; 02897 int fmt; 02898 int res; 02899 int foo; 02900 int videoformat = format & AST_FORMAT_VIDEO_MASK; 02901 02902 if (!cause) 02903 cause = &foo; 02904 *cause = AST_CAUSE_NOTDEFINED; 02905 02906 if (AST_LIST_LOCK(&channels)) { 02907 ast_log(LOG_WARNING, "Unable to lock channel list\n"); 02908 return NULL; 02909 } 02910 02911 AST_LIST_TRAVERSE(&backends, chan, list) { 02912 if (strcasecmp(type, chan->tech->type)) 02913 continue; 02914 02915 capabilities = chan->tech->capabilities; 02916 fmt = format & AST_FORMAT_AUDIO_MASK; 02917 res = ast_translator_best_choice(&fmt, &capabilities); 02918 if (res < 0) { 02919 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->tech->capabilities, format); 02920 AST_LIST_UNLOCK(&channels); 02921 return NULL; 02922 } 02923 AST_LIST_UNLOCK(&channels); 02924 if (!chan->tech->requester) 02925 return NULL; 02926 02927 if (!(c = chan->tech->requester(type, capabilities | videoformat, data, cause))) 02928 return NULL; 02929 02930 /* no need to generate a Newchannel event here; it is done in the channel_alloc call */ 02931 return c; 02932 } 02933 02934 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type); 02935 *cause = AST_CAUSE_NOSUCHDRIVER; 02936 AST_LIST_UNLOCK(&channels); 02937 02938 return NULL; 02939 } 02940 02941 int ast_call(struct ast_channel *chan, char *addr, int timeout) 02942 { 02943 /* Place an outgoing call, but don't wait any longer than timeout ms before returning. 02944 If the remote end does not answer within the timeout, then do NOT hang up, but 02945 return anyway. */ 02946 int res = -1; 02947 /* Stop if we're a zombie or need a soft hangup */ 02948 ast_channel_lock(chan); 02949 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) { 02950 if (chan->tech->call) 02951 res = chan->tech->call(chan, addr, timeout); 02952 ast_set_flag(chan, AST_FLAG_OUTGOING); 02953 } 02954 ast_channel_unlock(chan); 02955 return res; 02956 } 02957 02958 /*! 02959 \brief Transfer a call to dest, if the channel supports transfer 02960 02961 Called by: 02962 \arg app_transfer 02963 \arg the manager interface 02964 */ 02965 int ast_transfer(struct ast_channel *chan, char *dest) 02966 { 02967 int res = -1; 02968 02969 /* Stop if we're a zombie or need a soft hangup */ 02970 ast_channel_lock(chan); 02971 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) { 02972 if (chan->tech->transfer) { 02973 res = chan->tech->transfer(chan, dest); 02974 if (!res) 02975 res = 1; 02976 } else 02977 res = 0; 02978 } 02979 ast_channel_unlock(chan); 02980 return res; 02981 } 02982 02983 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders) 02984 { 02985 return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1); 02986 } 02987 02988 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd) 02989 { 02990 int pos = 0; /* index in the buffer where we accumulate digits */ 02991 int to = ftimeout; 02992 02993 /* Stop if we're a zombie or need a soft hangup */ 02994 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c)) 02995 return -1; 02996 if (!len) 02997 return -1; 02998 for (;;) { 02999 int d; 03000 if (c->stream) { 03001 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd); 03002 ast_stopstream(c); 03003 usleep(1000); 03004 if (!d) 03005 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd); 03006 } else { 03007 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd); 03008 } 03009 if (d < 0) 03010 return -1; 03011 if (d == 0) { 03012 s[pos]='\0'; 03013 return 1; 03014 } 03015 if (d == 1) { 03016 s[pos]='\0'; 03017 return 2; 03018 } 03019 if (!strchr(enders, d)) 03020 s[pos++] = d; 03021 if (strchr(enders, d) || (pos >= len)) { 03022 s[pos]='\0'; 03023 return 0; 03024 } 03025 to = timeout; 03026 } 03027 /* Never reached */ 03028 return 0; 03029 } 03030 03031 int ast_channel_supports_html(struct ast_channel *chan) 03032 { 03033 return (chan->tech->send_html) ? 1 : 0; 03034 } 03035 03036 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen) 03037 { 03038 if (chan->tech->send_html) 03039 return chan->tech->send_html(chan, subclass, data, datalen); 03040 return -1; 03041 } 03042 03043 int ast_channel_sendurl(struct ast_channel *chan, const char *url) 03044 { 03045 return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1); 03046 } 03047 03048 /*! \brief Set up translation from one channel to another */ 03049 static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to) 03050 { 03051 int src; 03052 int dst; 03053 03054 /* Set up translation from the 'from' channel to the 'to' channel */ 03055 src = from->nativeformats; 03056 dst = to->nativeformats; 03057 if (ast_translator_best_choice(&dst, &src) < 0) { 03058 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", from->name, src, to->name, dst); 03059 return -1; 03060 } 03061 03062 /* if the best path is not 'pass through', then 03063 transcoding is needed; if desired, force transcode path 03064 to use SLINEAR between channels, but only if there is 03065 no direct conversion available */ 03066 if ((src != dst) && ast_opt_transcode_via_slin && 03067 (ast_translate_path_steps(dst, src) != 1)) 03068 dst = AST_FORMAT_SLINEAR; 03069 if (ast_set_read_format(from, dst) < 0) { 03070 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", from->name, dst); 03071 return -1; 03072 } 03073 if (ast_set_write_format(to, dst) < 0) { 03074 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", to->name, dst); 03075 return -1; 03076 } 03077 return 0; 03078 } 03079 03080 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer) 03081 { 03082 /* Some callers do not check return code, and we must try to set all call legs correctly */ 03083 int rc = 0; 03084 03085 /* Set up translation from the chan to the peer */ 03086 rc = ast_channel_make_compatible_helper(chan, peer); 03087 03088 if (rc < 0) 03089 return rc; 03090 03091 /* Set up translation from the peer to the chan */ 03092 rc = ast_channel_make_compatible_helper(peer, chan); 03093 03094 return rc; 03095 } 03096 03097 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone) 03098 { 03099 int res = -1; 03100 struct ast_channel *final_orig = original, *final_clone = clone; 03101 03102 ast_channel_lock(original); 03103 while (ast_channel_trylock(clone)) { 03104 ast_channel_unlock(original); 03105 usleep(1); 03106 ast_channel_lock(original); 03107 } 03108 03109 /* each of these channels may be sitting behind a channel proxy (i.e. chan_agent) 03110 and if so, we don't really want to masquerade it, but its proxy */ 03111 if (original->_bridge && (original->_bridge != ast_bridged_channel(original))) 03112 final_orig = original->_bridge; 03113 03114 if (clone->_bridge && (clone->_bridge != ast_bridged_channel(clone))) 03115 final_clone = clone->_bridge; 03116 03117 if ((final_orig != original) || (final_clone != clone)) { 03118 ast_channel_lock(final_orig); 03119 while (ast_channel_trylock(final_clone)) { 03120 ast_channel_unlock(final_orig); 03121 usleep(1); 03122 ast_channel_lock(final_orig); 03123 } 03124 ast_channel_unlock(clone); 03125 ast_channel_unlock(original); 03126 original = final_orig; 03127 clone = final_clone; 03128 } 03129 03130 if (original == clone) { 03131 ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name); 03132 ast_channel_unlock(clone); 03133 ast_channel_unlock(original); 03134 return -1; 03135 } 03136 03137 if (option_debug) 03138 ast_log(LOG_DEBUG, "Planning to masquerade channel %s into the structure of %s\n", 03139 clone->name, original->name); 03140 if (original->masq) { 03141 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n", 03142 original->masq->name, original->name); 03143 } else if (clone->masqr) { 03144 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n", 03145 clone->name, clone->masqr->name); 03146 } else { 03147 original->masq = clone; 03148 clone->masqr = original; 03149 ast_queue_frame(original, &ast_null_frame); 03150 ast_queue_frame(clone, &ast_null_frame); 03151 if (option_debug) 03152 ast_log(LOG_DEBUG, "Done planning to masquerade channel %s into the structure of %s\n", clone->name, original->name); 03153 res = 0; 03154 } 03155 03156 ast_channel_unlock(clone); 03157 ast_channel_unlock(original); 03158 03159 return res; 03160 } 03161 03162 void ast_change_name(struct ast_channel *chan, char *newname) 03163 { 03164 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", chan->name, newname, chan->uniqueid); 03165 ast_string_field_set(chan, name, newname); 03166 } 03167 03168 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child) 03169 { 03170 struct ast_var_t *current, *newvar; 03171 const char *varname; 03172 03173 AST_LIST_TRAVERSE(&parent->varshead, current, entries) { 03174 int vartype = 0; 03175 03176 varname = ast_var_full_name(current); 03177 if (!varname) 03178 continue; 03179 03180 if (varname[0] == '_') { 03181 vartype = 1; 03182 if (varname[1] == '_') 03183 vartype = 2; 03184 } 03185 03186 switch (vartype) { 03187 case 1: 03188 newvar = ast_var_assign(&varname[1], ast_var_value(current)); 03189 if (newvar) { 03190 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries); 03191 if (option_debug) 03192 ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", ast_var_name(newvar)); 03193 } 03194 break; 03195 case 2: 03196 newvar = ast_var_assign(ast_var_full_name(current), ast_var_value(current)); 03197 if (newvar) { 03198 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries); 03199 if (option_debug) 03200 ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", ast_var_name(newvar)); 03201 } 03202 break; 03203 default: 03204 if (option_debug) 03205 ast_log(LOG_DEBUG, "Not copying variable %s.\n", ast_var_name(current)); 03206 break; 03207 } 03208 } 03209 } 03210 03211 /*! 03212 \brief Clone channel variables from 'clone' channel into 'original' channel 03213 03214 All variables except those related to app_groupcount are cloned. 03215 Variables are actually _removed_ from 'clone' channel, presumably 03216 because it will subsequently be destroyed. 03217 03218 \note Assumes locks will be in place on both channels when called. 03219 */ 03220 static void clone_variables(struct ast_channel *original, struct ast_channel *clone) 03221 { 03222 struct ast_var_t *varptr; 03223 03224 /* we need to remove all app_groupcount related variables from the original 03225 channel before merging in the clone's variables; any groups assigned to the 03226 original channel should be released, only those assigned to the clone 03227 should remain 03228 */ 03229 03230 AST_LIST_TRAVERSE_SAFE_BEGIN(&original->varshead, varptr, entries) { 03231 if (!strncmp(ast_var_name(varptr), GROUP_CATEGORY_PREFIX, strlen(GROUP_CATEGORY_PREFIX))) { 03232 AST_LIST_REMOVE_CURRENT(&original->varshead, entries); 03233 ast_var_delete(varptr); 03234 } 03235 } 03236 AST_LIST_TRAVERSE_SAFE_END; 03237 03238 /* Append variables from clone channel into original channel */ 03239 /* XXX Is this always correct? We have to in order to keep MACROS working XXX */ 03240 if (AST_LIST_FIRST(&clone->varshead)) 03241 AST_LIST_APPEND_LIST(&original->varshead, &clone->varshead, entries); 03242 } 03243 03244 /*! 03245 \brief Masquerade a channel 03246 03247 \note Assumes channel will be locked when called 03248 */ 03249 int ast_do_masquerade(struct ast_channel *original) 03250 { 03251 int x,i; 03252 int res=0; 03253 int origstate; 03254 struct ast_frame *cur; 03255 const struct ast_channel_tech *t; 03256 void *t_pvt; 03257 struct ast_callerid tmpcid; 03258 struct ast_channel *clone = original->masq; 03259 struct ast_channel_spy_list *spy_list = NULL; 03260 struct ast_channel_spy *spy = NULL; 03261 int rformat = original->readformat; 03262 int wformat = original->writeformat; 03263 char newn[100]; 03264 char orig[100]; 03265 char masqn[100]; 03266 char zombn[100]; 03267 03268 if (option_debug > 3) 03269 ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n", 03270 clone->name, clone->_state, original->name, original->_state); 03271 03272 manager_event(EVENT_FLAG_CALL, "Masquerade", "Clone: %s\r\nCloneState: %s\r\nOriginal: %s\r\nOriginalState: %s\r\n", 03273 clone->name, ast_state2str(clone->_state), original->name, ast_state2str(original->_state)); 03274 03275 /* XXX This is a seriously wacked out operation. We're essentially putting the guts of 03276 the clone channel into the original channel. Start by killing off the original 03277 channel's backend. I'm not sure we're going to keep this function, because 03278 while the features are nice, the cost is very high in terms of pure nastiness. XXX */ 03279 03280 /* We need the clone's lock, too */ 03281 ast_channel_lock(clone); 03282 03283 if (option_debug > 1) 03284 ast_log(LOG_DEBUG, "Got clone lock for masquerade on '%s' at %p\n", clone->name, &clone->lock); 03285 03286 /* Having remembered the original read/write formats, we turn off any translation on either 03287 one */ 03288 free_translation(clone); 03289 free_translation(original); 03290 03291 03292 /* Unlink the masquerade */ 03293 original->masq = NULL; 03294 clone->masqr = NULL; 03295 03296 /* Save the original name */ 03297 ast_copy_string(orig, original->name, sizeof(orig)); 03298 /* Save the new name */ 03299 ast_copy_string(newn, clone->name, sizeof(newn)); 03300 /* Create the masq name */ 03301 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn); 03302 03303 /* Copy the name from the clone channel */ 03304 ast_string_field_set(original, name, newn); 03305 03306 /* Mangle the name of the clone channel */ 03307 ast_string_field_set(clone, name, masqn); 03308 03309 /* Notify any managers of the change, first the masq then the other */ 03310 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid); 03311 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig, newn, original->uniqueid); 03312 03313 /* Swap the technologies */ 03314 t = original->tech; 03315 original->tech = clone->tech; 03316 clone->tech = t; 03317 03318 t_pvt = original->tech_pvt; 03319 original->tech_pvt = clone->tech_pvt; 03320 clone->tech_pvt = t_pvt; 03321 03322 /* Swap the readq's */ 03323 cur = AST_LIST_FIRST(&original->readq); 03324 AST_LIST_HEAD_SET_NOLOCK(&original->readq, AST_LIST_FIRST(&clone->readq)); 03325 AST_LIST_HEAD_SET_NOLOCK(&clone->readq, cur); 03326 03327 /* Swap the alertpipes */ 03328 for (i = 0; i < 2; i++) { 03329 x = original->alertpipe[i]; 03330 original->alertpipe[i] = clone->alertpipe[i]; 03331 clone->alertpipe[i] = x; 03332 } 03333 03334 /* Swap the raw formats */ 03335 x = original->rawreadformat; 03336 original->rawreadformat = clone->rawreadformat; 03337 clone->rawreadformat = x; 03338 x = original->rawwriteformat; 03339 original->rawwriteformat = clone->rawwriteformat; 03340 clone->rawwriteformat = x; 03341 03342 /* Swap the spies */ 03343 spy_list = original->spies; 03344 original->spies = clone->spies; 03345 clone->spies = spy_list; 03346 03347 /* Update channel on respective spy lists if present */ 03348 if (original->spies) { 03349 AST_LIST_TRAVERSE(&original->spies->list, spy, list) { 03350 ast_mutex_lock(&spy->lock); 03351 spy->chan = original; 03352 ast_mutex_unlock(&spy->lock); 03353 } 03354 } 03355 if (clone->spies) { 03356 AST_LIST_TRAVERSE(&clone->spies->list, spy, list) { 03357 ast_mutex_lock(&spy->lock); 03358 spy->chan = clone; 03359 ast_mutex_unlock(&spy->lock); 03360 } 03361 } 03362 03363 /* Save any pending frames on both sides. Start by counting 03364 * how many we're going to need... */ 03365 x = 0; 03366 if (original->alertpipe[1] > -1) { 03367 AST_LIST_TRAVERSE(&clone->readq, cur, frame_list) 03368 x++; 03369 } 03370 03371 /* If we had any, prepend them to the ones already in the queue, and 03372 * load up the alertpipe */ 03373 if (AST_LIST_FIRST(&clone->readq)) { 03374 AST_LIST_INSERT_TAIL(&clone->readq, AST_LIST_FIRST(&original->readq), frame_list); 03375 AST_LIST_HEAD_SET_NOLOCK(&original->readq, AST_LIST_FIRST(&clone->readq)); 03376 AST_LIST_HEAD_SET_NOLOCK(&clone->readq, NULL); 03377 for (i = 0; i < x; i++) 03378 write(original->alertpipe[1], &x, sizeof(x)); 03379 } 03380 03381 clone->_softhangup = AST_SOFTHANGUP_DEV; 03382 03383 03384 /* And of course, so does our current state. Note we need not 03385 call ast_setstate since the event manager doesn't really consider 03386 these separate. We do this early so that the clone has the proper 03387 state of the original channel. */ 03388 origstate = original->_state; 03389 original->_state = clone->_state; 03390 clone->_state = origstate; 03391 03392 if (clone->tech->fixup){ 03393 res = clone->tech->fixup(original, clone); 03394 if (res) 03395 ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name); 03396 } 03397 03398 /* Start by disconnecting the original's physical side */ 03399 if (clone->tech->hangup) 03400 res = clone->tech->hangup(clone); 03401 if (res) { 03402 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n"); 03403 ast_channel_unlock(clone); 03404 return -1; 03405 } 03406 03407 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig); 03408 /* Mangle the name of the clone channel */ 03409 ast_string_field_set(clone, name, zombn); 03410 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid); 03411 03412 /* Update the type. */ 03413 t_pvt = original->monitor; 03414 original->monitor = clone->monitor; 03415 clone->monitor = t_pvt; 03416 03417 /* Keep the same language. */ 03418 ast_string_field_set(original, language, clone->language); 03419 /* Copy the FD's other than the generator fd */ 03420 for (x = 0; x < AST_MAX_FDS; x++) { 03421 if (x != AST_GENERATOR_FD) 03422 original->fds[x] = clone->fds[x]; 03423 } 03424 03425 /* move any whisperer over */ 03426 ast_channel_whisper_stop(original); 03427 if (ast_test_flag(clone, AST_FLAG_WHISPER)) { 03428 original->whisper = clone->whisper; 03429 ast_set_flag(original, AST_FLAG_WHISPER); 03430 clone->whisper = NULL; 03431 ast_clear_flag(clone, AST_FLAG_WHISPER); 03432 } 03433 03434 /* Move data stores over */ 03435 if (AST_LIST_FIRST(&clone->datastores)) 03436 AST_LIST_INSERT_TAIL(&original->datastores, AST_LIST_FIRST(&clone->datastores), entry); 03437 AST_LIST_HEAD_INIT_NOLOCK(&clone->datastores); 03438 03439 clone_variables(original, clone); 03440 AST_LIST_HEAD_INIT_NOLOCK(&clone->varshead); 03441 /* Presense of ADSI capable CPE follows clone */ 03442 original->adsicpe = clone->adsicpe; 03443 /* Bridge remains the same */ 03444 /* CDR fields remain the same */ 03445 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */ 03446 /* Application and data remain the same */ 03447 /* Clone exception becomes real one, as with fdno */ 03448 ast_copy_flags(original, clone, AST_FLAG_EXCEPTION); 03449 original->fdno = clone->fdno; 03450 /* Schedule context remains the same */ 03451 /* Stream stuff stays the same */ 03452 /* Keep the original state. The fixup code will need to work with it most likely */ 03453 03454 /* Just swap the whole structures, nevermind the allocations, they'll work themselves 03455 out. */ 03456 tmpcid = original->cid; 03457 original->cid = clone->cid; 03458 clone->cid = tmpcid; 03459 03460 /* Restore original timing file descriptor */ 03461 original->fds[AST_TIMING_FD] = original->timingfd; 03462 03463 /* Our native formats are different now */ 03464 original->nativeformats = clone->nativeformats; 03465 03466 /* Context, extension, priority, app data, jump table, remain the same */ 03467 /* pvt switches. pbx stays the same, as does next */ 03468 03469 /* Set the write format */ 03470 ast_set_write_format(original, wformat); 03471 03472 /* Set the read format */ 03473 ast_set_read_format(original, rformat); 03474 03475 /* Copy the music class */ 03476 ast_string_field_set(original, musicclass, clone->musicclass); 03477 03478 if (option_debug) 03479 ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat); 03480 03481 /* Okay. Last thing is to let the channel driver know about all this mess, so he 03482 can fix up everything as best as possible */ 03483 if (original->tech->fixup) { 03484 res = original->tech->fixup(clone, original); 03485 if (res) { 03486 ast_log(LOG_WARNING, "Channel for type '%s' could not fixup channel %s\n", 03487 original->tech->type, original->name); 03488 ast_channel_unlock(clone); 03489 return -1; 03490 } 03491 } else 03492 ast_log(LOG_WARNING, "Channel type '%s' does not have a fixup routine (for %s)! Bad things may happen.\n", 03493 original->tech->type, original->name); 03494 03495 /* Now, at this point, the "clone" channel is totally F'd up. We mark it as 03496 a zombie so nothing tries to touch it. If it's already been marked as a 03497 zombie, then free it now (since it already is considered invalid). */ 03498 if (ast_test_flag(clone, AST_FLAG_ZOMBIE)) { 03499 if (option_debug) 03500 ast_log(LOG_DEBUG, "Destroying channel clone '%s'\n", clone->name); 03501 ast_channel_unlock(clone); 03502 manager_event(EVENT_FLAG_CALL, "Hangup", 03503 "Channel: %s\r\n" 03504 "Uniqueid: %s\r\n" 03505 "Cause: %d\r\n" 03506 "Cause-txt: %s\r\n", 03507 clone->name, 03508 clone->uniqueid, 03509 clone->hangupcause, 03510 ast_cause2str(clone->hangupcause) 03511 ); 03512 ast_channel_free(clone); 03513 } else { 03514 if (option_debug) 03515 ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name); 03516 ast_set_flag(clone, AST_FLAG_ZOMBIE); 03517 ast_queue_frame(clone, &ast_null_frame); 03518 ast_channel_unlock(clone); 03519 } 03520 03521 /* Signal any blocker */ 03522 if (ast_test_flag(original, AST_FLAG_BLOCKING)) 03523 pthread_kill(original->blocker, SIGURG); 03524 if (option_debug) 03525 ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n", original->name, original->_state); 03526 return 0; 03527 } 03528 03529 void ast_set_callerid(struct ast_channel *chan, const char *callerid, const char *calleridname, const char *ani) 03530 { 03531 if (callerid) { 03532 if (chan->cid.cid_num) 03533 free(chan->cid.cid_num); 03534 chan->cid.cid_num = ast_strdup(callerid); 03535 } 03536 if (calleridname) { 03537 if (chan->cid.cid_name) 03538 free(chan->cid.cid_name); 03539 chan->cid.cid_name = ast_strdup(calleridname); 03540 } 03541 if (ani) { 03542 if (chan->cid.cid_ani) 03543 free(chan->cid.cid_ani); 03544 chan->cid.cid_ani = ast_strdup(ani); 03545 } 03546 if (chan->cdr) 03547 ast_cdr_setcid(chan->cdr, chan); 03548 manager_event(EVENT_FLAG_CALL, "Newcallerid", 03549 "Channel: %s\r\n" 03550 "CallerIDNum: %s\r\n" 03551 "CallerIDName: %s\r\n" 03552 "Uniqueid: %s\r\n" 03553 "CID-CallingPres: %d (%s)\r\n", 03554 chan->name, 03555 S_OR(chan->cid.cid_num, "<Unknown>"), 03556 S_OR(chan->cid.cid_name, "<Unknown>"), 03557 chan->uniqueid, 03558 chan->cid.cid_pres, 03559 ast_describe_caller_presentation(chan->cid.cid_pres) 03560 ); 03561 } 03562 03563 int ast_setstate(struct ast_channel *chan, enum ast_channel_state state) 03564 { 03565 int oldstate = chan->_state; 03566 03567 if (oldstate == state) 03568 return 0; 03569 03570 chan->_state = state; 03571 ast_device_state_changed_literal(chan->name); 03572 /* setstate used to conditionally report Newchannel; this is no more */ 03573 manager_event(EVENT_FLAG_CALL, 03574 "Newstate", 03575 "Channel: %s\r\n" 03576 "State: %s\r\n" 03577 "CallerIDNum: %s\r\n" 03578 "CallerIDName: %s\r\n" 03579 "Uniqueid: %s\r\n", 03580 chan->name, ast_state2str(chan->_state), 03581 S_OR(chan->cid.cid_num, "<unknown>"), 03582 S_OR(chan->cid.cid_name, "<unknown>"), 03583 chan->uniqueid); 03584 03585 return 0; 03586 } 03587 03588 /*! \brief Find bridged channel */ 03589 struct ast_channel *ast_bridged_channel(struct ast_channel *chan) 03590 { 03591 struct ast_channel *bridged; 03592 bridged = chan->_bridge; 03593 if (bridged && bridged->tech->bridged_channel) 03594 bridged = bridged->tech->bridged_channel(chan, bridged); 03595 return bridged; 03596 } 03597 03598 static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, const char *sound, int remain) 03599 { 03600 int min = 0, sec = 0, check; 03601 03602 check = ast_autoservice_start(peer); 03603 if (check) 03604 return; 03605 03606 if (remain > 0) { 03607 if (remain / 60 > 1) { 03608 min = remain / 60; 03609 sec = remain % 60; 03610 } else { 03611 sec = remain; 03612 } 03613 } 03614 03615 if (!strcmp(sound,"timeleft")) { /* Queue support */ 03616 ast_stream_and_wait(chan, "vm-youhave", ""); 03617 if (min) { 03618 ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, NULL); 03619 ast_stream_and_wait(chan, "queue-minutes", ""); 03620 } 03621 if (sec) { 03622 ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, NULL); 03623 ast_stream_and_wait(chan, "queue-seconds", ""); 03624 } 03625 } else { 03626 ast_stream_and_wait(chan, sound, ""); 03627 } 03628 03629 ast_autoservice_stop(peer); 03630 } 03631 03632 static enum ast_bridge_result ast_generic_bridge(struct ast_channel *c0, struct ast_channel *c1, 03633 struct ast_bridge_config *config, struct ast_frame **fo, 03634 struct ast_channel **rc, struct timeval bridge_end) 03635 { 03636 /* Copy voice back and forth between the two channels. */ 03637 struct ast_channel *cs[3]; 03638 struct ast_frame *f; 03639 enum ast_bridge_result res = AST_BRIDGE_COMPLETE; 03640 int o0nativeformats; 03641 int o1nativeformats; 03642 int watch_c0_dtmf; 03643 int watch_c1_dtmf; 03644 void *pvt0, *pvt1; 03645 /* Indicates whether a frame was queued into a jitterbuffer */ 03646 int frame_put_in_jb = 0; 03647 int jb_in_use; 03648 int to; 03649 03650 cs[0] = c0; 03651 cs[1] = c1; 03652 pvt0 = c0->tech_pvt; 03653 pvt1 = c1->tech_pvt; 03654 o0nativeformats = c0->nativeformats; 03655 o1nativeformats = c1->nativeformats; 03656 watch_c0_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_0; 03657 watch_c1_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_1; 03658 03659 /* Check the need of a jitterbuffer for each channel */ 03660 jb_in_use = ast_jb_do_usecheck(c0, c1); 03661 03662 for (;;) { 03663 struct ast_channel *who, *other; 03664 03665 if ((c0->tech_pvt != pvt0) || (c1->tech_pvt != pvt1) || 03666 (o0nativeformats != c0->nativeformats) || 03667 (o1nativeformats != c1->nativeformats)) { 03668 /* Check for Masquerade, codec changes, etc */ 03669 res = AST_BRIDGE_RETRY; 03670 break; 03671 } 03672 if (bridge_end.tv_sec) { 03673 to = ast_tvdiff_ms(bridge_end, ast_tvnow()); 03674 if (to <= 0) { 03675 if (config->timelimit) 03676 res = AST_BRIDGE_RETRY; 03677 else 03678 res = AST_BRIDGE_COMPLETE; 03679 break; 03680 } 03681 } else 03682 to = -1; 03683 /* Calculate the appropriate max sleep interval - in general, this is the time, 03684 left to the closest jb delivery moment */ 03685 if (jb_in_use) 03686 to = ast_jb_get_when_to_wakeup(c0, c1, to); 03687 who = ast_waitfor_n(cs, 2, &to); 03688 if (!who) { 03689 /* No frame received within the specified timeout - check if we have to deliver now */ 03690 if (jb_in_use) 03691 ast_jb_get_and_deliver(c0, c1); 03692 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) { 03693 if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE) 03694 c0->_softhangup = 0; 03695 if (c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) 03696 c1->_softhangup = 0; 03697 c0->_bridge = c1; 03698 c1->_bridge = c0; 03699 } 03700 continue; 03701 } 03702 f = ast_read(who); 03703 if (!f) { 03704 *fo = NULL; 03705 *rc = who; 03706 if (option_debug) 03707 ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name); 03708 break; 03709 } 03710 03711 other = (who == c0) ? c1 : c0; /* the 'other' channel */ 03712 /* Try add the frame info the who's bridged channel jitterbuff */ 03713 if (jb_in_use) 03714