00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 378781 $")
00033
00034 #include "asterisk/_private.h"
00035
00036 #include <sys/time.h>
00037 #include <signal.h>
00038 #include <math.h>
00039
00040 #include "asterisk/paths.h"
00041
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/frame.h"
00044 #include "asterisk/mod_format.h"
00045 #include "asterisk/sched.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/musiconhold.h"
00048 #include "asterisk/say.h"
00049 #include "asterisk/file.h"
00050 #include "asterisk/cli.h"
00051 #include "asterisk/translate.h"
00052 #include "asterisk/manager.h"
00053 #include "asterisk/cel.h"
00054 #include "asterisk/chanvars.h"
00055 #include "asterisk/linkedlists.h"
00056 #include "asterisk/indications.h"
00057 #include "asterisk/monitor.h"
00058 #include "asterisk/causes.h"
00059 #include "asterisk/callerid.h"
00060 #include "asterisk/utils.h"
00061 #include "asterisk/lock.h"
00062 #include "asterisk/app.h"
00063 #include "asterisk/transcap.h"
00064 #include "asterisk/devicestate.h"
00065 #include "asterisk/threadstorage.h"
00066 #include "asterisk/slinfactory.h"
00067 #include "asterisk/audiohook.h"
00068 #include "asterisk/framehook.h"
00069 #include "asterisk/timing.h"
00070 #include "asterisk/autochan.h"
00071 #include "asterisk/stringfields.h"
00072 #include "asterisk/global_datastores.h"
00073 #include "asterisk/data.h"
00074 #include "asterisk/features.h"
00075
00076 #ifdef HAVE_EPOLL
00077 #include <sys/epoll.h>
00078 #endif
00079
00080 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
00081 #if defined(HAVE_PRI)
00082 #include "libpri.h"
00083 #endif
00084 #endif
00085
00086 struct ast_epoll_data {
00087 struct ast_channel *chan;
00088 int which;
00089 };
00090
00091
00092 #if 0
00093 #define MONITOR_CONSTANT_DELAY
00094 #define MONITOR_DELAY 150 * 8
00095 #endif
00096
00097
00098 static int shutting_down;
00099
00100 static int uniqueint;
00101 static int chancount;
00102
00103 unsigned long global_fin, global_fout;
00104
00105 AST_THREADSTORAGE(state2str_threadbuf);
00106 #define STATE2STR_BUFSIZE 32
00107
00108
00109
00110 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
00111
00112
00113 #define AST_MIN_DTMF_DURATION 80
00114
00115
00116
00117 #define AST_MIN_DTMF_GAP 45
00118
00119
00120 struct chanlist {
00121 const struct ast_channel_tech *tech;
00122 AST_LIST_ENTRY(chanlist) list;
00123 };
00124
00125 #ifdef CHANNEL_TRACE
00126
00127 struct ast_chan_trace_data {
00128 int enabled;
00129 AST_LIST_HEAD_NOLOCK(, ast_chan_trace) trace;
00130 };
00131
00132
00133 struct ast_chan_trace {
00134 char context[AST_MAX_CONTEXT];
00135 char exten[AST_MAX_EXTENSION];
00136 int priority;
00137 AST_LIST_ENTRY(ast_chan_trace) entry;
00138 };
00139 #endif
00140
00141
00142 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
00143
00144 #ifdef LOW_MEMORY
00145 #define NUM_CHANNEL_BUCKETS 61
00146 #else
00147 #define NUM_CHANNEL_BUCKETS 1567
00148 #endif
00149
00150 #if 0
00151 #define DATA_EXPORT_CALLERID(MEMBER) \
00152 MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING) \
00153 MEMBER(ast_callerid, cid_num, AST_DATA_STRING) \
00154 MEMBER(ast_callerid, cid_name, AST_DATA_STRING) \
00155 MEMBER(ast_callerid, cid_ani, AST_DATA_STRING) \
00156 MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER) \
00157 MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER) \
00158 MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
00159
00160 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
00161 #endif
00162
00163 #define DATA_EXPORT_CHANNEL(MEMBER) \
00164 MEMBER(ast_channel, blockproc, AST_DATA_STRING) \
00165 MEMBER(ast_channel, appl, AST_DATA_STRING) \
00166 MEMBER(ast_channel, data, AST_DATA_STRING) \
00167 MEMBER(ast_channel, name, AST_DATA_STRING) \
00168 MEMBER(ast_channel, language, AST_DATA_STRING) \
00169 MEMBER(ast_channel, musicclass, AST_DATA_STRING) \
00170 MEMBER(ast_channel, accountcode, AST_DATA_STRING) \
00171 MEMBER(ast_channel, peeraccount, AST_DATA_STRING) \
00172 MEMBER(ast_channel, userfield, AST_DATA_STRING) \
00173 MEMBER(ast_channel, call_forward, AST_DATA_STRING) \
00174 MEMBER(ast_channel, uniqueid, AST_DATA_STRING) \
00175 MEMBER(ast_channel, linkedid, AST_DATA_STRING) \
00176 MEMBER(ast_channel, parkinglot, AST_DATA_STRING) \
00177 MEMBER(ast_channel, hangupsource, AST_DATA_STRING) \
00178 MEMBER(ast_channel, dialcontext, AST_DATA_STRING) \
00179 MEMBER(ast_channel, rings, AST_DATA_INTEGER) \
00180 MEMBER(ast_channel, priority, AST_DATA_INTEGER) \
00181 MEMBER(ast_channel, macropriority, AST_DATA_INTEGER) \
00182 MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER) \
00183 MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER) \
00184 MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER) \
00185 MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER) \
00186 MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER) \
00187 MEMBER(ast_channel, context, AST_DATA_STRING) \
00188 MEMBER(ast_channel, exten, AST_DATA_STRING) \
00189 MEMBER(ast_channel, macrocontext, AST_DATA_STRING) \
00190 MEMBER(ast_channel, macroexten, AST_DATA_STRING)
00191
00192 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
00193
00194
00195
00196 static struct ao2_container *channels;
00197
00198
00199
00200
00201
00202 static const struct {
00203 int cause;
00204 const char *name;
00205 const char *desc;
00206 } causes[] = {
00207 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
00208 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
00209 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
00210 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
00211 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
00212 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
00213 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
00214 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
00215 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
00216 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
00217 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
00218 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
00219 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
00220 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
00221 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
00222 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
00223 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
00224 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
00225 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
00226 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
00227 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
00228 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
00229 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
00230 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
00231 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
00232 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
00233 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
00234 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
00235 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
00236 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
00237 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
00238 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
00239 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
00240 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
00241 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
00242 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
00243 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
00244 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
00245 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
00246 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
00247 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
00248 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
00249 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
00250 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
00251 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
00252 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
00253 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
00254 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
00255 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
00256 };
00257
00258 struct ast_variable *ast_channeltype_list(void)
00259 {
00260 struct chanlist *cl;
00261 struct ast_variable *var = NULL, *prev = NULL;
00262
00263 AST_RWLIST_RDLOCK(&backends);
00264 AST_RWLIST_TRAVERSE(&backends, cl, list) {
00265 if (prev) {
00266 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
00267 prev = prev->next;
00268 } else {
00269 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
00270 prev = var;
00271 }
00272 }
00273 AST_RWLIST_UNLOCK(&backends);
00274
00275 return var;
00276 }
00277
00278 static void channel_data_add_flags(struct ast_data *tree,
00279 struct ast_channel *chan)
00280 {
00281 ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(chan, AST_FLAG_DEFER_DTMF));
00282 ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(chan, AST_FLAG_WRITE_INT));
00283 ast_data_add_bool(tree, "BLOCKING", ast_test_flag(chan, AST_FLAG_BLOCKING));
00284 ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(chan, AST_FLAG_ZOMBIE));
00285 ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(chan, AST_FLAG_EXCEPTION));
00286 ast_data_add_bool(tree, "MOH", ast_test_flag(chan, AST_FLAG_MOH));
00287 ast_data_add_bool(tree, "SPYING", ast_test_flag(chan, AST_FLAG_SPYING));
00288 ast_data_add_bool(tree, "NBRIDGE", ast_test_flag(chan, AST_FLAG_NBRIDGE));
00289 ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP));
00290 ast_data_add_bool(tree, "OUTGOING", ast_test_flag(chan, AST_FLAG_OUTGOING));
00291 ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(chan, AST_FLAG_IN_DTMF));
00292 ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(chan, AST_FLAG_EMULATE_DTMF));
00293 ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY));
00294 ast_data_add_bool(tree, "ANSWERED_ELSEWHERE", ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE));
00295 ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM));
00296 ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN));
00297 ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT));
00298 ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS));
00299 ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(chan, AST_FLAG_DISABLE_DEVSTATE_CACHE));
00300 }
00301
00302 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
00303 static const char *party_number_ton2str(int ton)
00304 {
00305 #if defined(HAVE_PRI)
00306 switch ((ton >> 4) & 0x07) {
00307 case PRI_TON_INTERNATIONAL:
00308 return "International";
00309 case PRI_TON_NATIONAL:
00310 return "National";
00311 case PRI_TON_NET_SPECIFIC:
00312 return "Network Specific";
00313 case PRI_TON_SUBSCRIBER:
00314 return "Subscriber";
00315 case PRI_TON_ABBREVIATED:
00316 return "Abbreviated";
00317 case PRI_TON_RESERVED:
00318 return "Reserved";
00319 case PRI_TON_UNKNOWN:
00320 default:
00321 break;
00322 }
00323 #endif
00324 return "Unknown";
00325 }
00326 #endif
00327
00328 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
00329 static const char *party_number_plan2str(int plan)
00330 {
00331 #if defined(HAVE_PRI)
00332 switch (plan & 0x0F) {
00333 default:
00334 case PRI_NPI_UNKNOWN:
00335 break;
00336 case PRI_NPI_E163_E164:
00337 return "Public (E.163/E.164)";
00338 case PRI_NPI_X121:
00339 return "Data (X.121)";
00340 case PRI_NPI_F69:
00341 return "Telex (F.69)";
00342 case PRI_NPI_NATIONAL:
00343 return "National Standard";
00344 case PRI_NPI_PRIVATE:
00345 return "Private";
00346 case PRI_NPI_RESERVED:
00347 return "Reserved";
00348 }
00349 #endif
00350 return "Unknown";
00351 }
00352 #endif
00353
00354 int ast_channel_data_add_structure(struct ast_data *tree,
00355 struct ast_channel *chan, int add_bridged)
00356 {
00357 struct ast_channel *bc;
00358 struct ast_data *data_bridged;
00359 struct ast_data *data_cdr;
00360 struct ast_data *data_flags;
00361 struct ast_data *data_zones;
00362 struct ast_data *enum_node;
00363 struct ast_data *data_softhangup;
00364 #if 0
00365 struct ast_data *data_callerid;
00366 char value_str[100];
00367 #endif
00368
00369 if (!tree) {
00370 return -1;
00371 }
00372
00373 ast_data_add_structure(ast_channel, tree, chan);
00374
00375 if (add_bridged) {
00376 bc = ast_bridged_channel(chan);
00377 if (bc) {
00378 data_bridged = ast_data_add_node(tree, "bridged");
00379 if (!data_bridged) {
00380 return -1;
00381 }
00382 ast_channel_data_add_structure(data_bridged, bc, 0);
00383 }
00384 }
00385
00386 ast_data_add_codecs(tree, "oldwriteformat", chan->oldwriteformat);
00387 ast_data_add_codecs(tree, "nativeformats", chan->nativeformats);
00388 ast_data_add_codecs(tree, "readformat", chan->readformat);
00389 ast_data_add_codecs(tree, "writeformat", chan->writeformat);
00390 ast_data_add_codecs(tree, "rawreadformat", chan->rawreadformat);
00391 ast_data_add_codecs(tree, "rawwriteformat", chan->rawwriteformat);
00392
00393
00394 enum_node = ast_data_add_node(tree, "state");
00395 if (!enum_node) {
00396 return -1;
00397 }
00398 ast_data_add_str(enum_node, "text", ast_state2str(chan->_state));
00399 ast_data_add_int(enum_node, "value", chan->_state);
00400
00401
00402 enum_node = ast_data_add_node(tree, "hangupcause");
00403 if (!enum_node) {
00404 return -1;
00405 }
00406 ast_data_add_str(enum_node, "text", ast_cause2str(chan->hangupcause));
00407 ast_data_add_int(enum_node, "value", chan->hangupcause);
00408
00409
00410 enum_node = ast_data_add_node(tree, "amaflags");
00411 if (!enum_node) {
00412 return -1;
00413 }
00414 ast_data_add_str(enum_node, "text", ast_cdr_flags2str(chan->amaflags));
00415 ast_data_add_int(enum_node, "value", chan->amaflags);
00416
00417
00418 enum_node = ast_data_add_node(tree, "transfercapability");
00419 if (!enum_node) {
00420 return -1;
00421 }
00422 ast_data_add_str(enum_node, "text", ast_transfercapability2str(chan->transfercapability));
00423 ast_data_add_int(enum_node, "value", chan->transfercapability);
00424
00425
00426 data_softhangup = ast_data_add_node(tree, "softhangup");
00427 if (!data_softhangup) {
00428 return -1;
00429 }
00430 ast_data_add_bool(data_softhangup, "dev", chan->_softhangup & AST_SOFTHANGUP_DEV);
00431 ast_data_add_bool(data_softhangup, "asyncgoto", chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO);
00432 ast_data_add_bool(data_softhangup, "shutdown", chan->_softhangup & AST_SOFTHANGUP_SHUTDOWN);
00433 ast_data_add_bool(data_softhangup, "timeout", chan->_softhangup & AST_SOFTHANGUP_TIMEOUT);
00434 ast_data_add_bool(data_softhangup, "appunload", chan->_softhangup & AST_SOFTHANGUP_APPUNLOAD);
00435 ast_data_add_bool(data_softhangup, "explicit", chan->_softhangup & AST_SOFTHANGUP_EXPLICIT);
00436 ast_data_add_bool(data_softhangup, "unbridge", chan->_softhangup & AST_SOFTHANGUP_UNBRIDGE);
00437
00438
00439 data_flags = ast_data_add_node(tree, "flags");
00440 if (!data_flags) {
00441 return -1;
00442 }
00443 channel_data_add_flags(data_flags, chan);
00444
00445 ast_data_add_uint(tree, "timetohangup", chan->whentohangup.tv_sec);
00446
00447 #if 0
00448
00449 data_callerid = ast_data_add_node(tree, "callerid");
00450 if (!data_callerid) {
00451 return -1;
00452 }
00453 ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
00454
00455 enum_node = ast_data_add_node(data_callerid, "cid_ton");
00456 if (!enum_node) {
00457 return -1;
00458 }
00459 ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
00460 snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
00461 party_number_ton2str(chan->cid.cid_ton),
00462 party_number_plan2str(chan->cid.cid_ton));
00463 ast_data_add_str(enum_node, "text", value_str);
00464 #endif
00465
00466
00467 if (chan->zone) {
00468 data_zones = ast_data_add_node(tree, "zone");
00469 if (!data_zones) {
00470 return -1;
00471 }
00472 ast_tone_zone_data_add_structure(data_zones, chan->zone);
00473 }
00474
00475
00476 data_cdr = ast_data_add_node(tree, "cdr");
00477 if (!data_cdr) {
00478 return -1;
00479 }
00480
00481 ast_cdr_data_add_structure(data_cdr, chan->cdr, 1);
00482
00483 return 0;
00484 }
00485
00486 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
00487 struct ast_channel *chan, const char *structure_name)
00488 {
00489 return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
00490 }
00491
00492
00493 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00494 {
00495 #define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n"
00496 struct chanlist *cl;
00497 int count_chan = 0;
00498
00499 switch (cmd) {
00500 case CLI_INIT:
00501 e->command = "core show channeltypes";
00502 e->usage =
00503 "Usage: core show channeltypes\n"
00504 " Lists available channel types registered in your\n"
00505 " Asterisk server.\n";
00506 return NULL;
00507 case CLI_GENERATE:
00508 return NULL;
00509 }
00510
00511 if (a->argc != 3)
00512 return CLI_SHOWUSAGE;
00513
00514 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
00515 ast_cli(a->fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
00516
00517 AST_RWLIST_RDLOCK(&backends);
00518 AST_RWLIST_TRAVERSE(&backends, cl, list) {
00519 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
00520 (cl->tech->devicestate) ? "yes" : "no",
00521 (cl->tech->indicate) ? "yes" : "no",
00522 (cl->tech->transfer) ? "yes" : "no");
00523 count_chan++;
00524 }
00525 AST_RWLIST_UNLOCK(&backends);
00526
00527 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
00528
00529 return CLI_SUCCESS;
00530
00531 #undef FORMAT
00532 }
00533
00534 static char *complete_channeltypes(struct ast_cli_args *a)
00535 {
00536 struct chanlist *cl;
00537 int which = 0;
00538 int wordlen;
00539 char *ret = NULL;
00540
00541 if (a->pos != 3)
00542 return NULL;
00543
00544 wordlen = strlen(a->word);
00545
00546 AST_RWLIST_RDLOCK(&backends);
00547 AST_RWLIST_TRAVERSE(&backends, cl, list) {
00548 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
00549 ret = ast_strdup(cl->tech->type);
00550 break;
00551 }
00552 }
00553 AST_RWLIST_UNLOCK(&backends);
00554
00555 return ret;
00556 }
00557
00558
00559 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00560 {
00561 struct chanlist *cl = NULL;
00562 char buf[512];
00563
00564 switch (cmd) {
00565 case CLI_INIT:
00566 e->command = "core show channeltype";
00567 e->usage =
00568 "Usage: core show channeltype <name>\n"
00569 " Show details about the specified channel type, <name>.\n";
00570 return NULL;
00571 case CLI_GENERATE:
00572 return complete_channeltypes(a);
00573 }
00574
00575 if (a->argc != 4)
00576 return CLI_SHOWUSAGE;
00577
00578 AST_RWLIST_RDLOCK(&backends);
00579
00580 AST_RWLIST_TRAVERSE(&backends, cl, list) {
00581 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
00582 break;
00583 }
00584
00585
00586 if (!cl) {
00587 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
00588 AST_RWLIST_UNLOCK(&backends);
00589 return CLI_FAILURE;
00590 }
00591
00592 ast_cli(a->fd,
00593 "-- Info about channel driver: %s --\n"
00594 " Device State: %s\n"
00595 " Indication: %s\n"
00596 " Transfer : %s\n"
00597 " Capabilities: %s\n"
00598 " Digit Begin: %s\n"
00599 " Digit End: %s\n"
00600 " Send HTML : %s\n"
00601 " Image Support: %s\n"
00602 " Text Support: %s\n",
00603 cl->tech->type,
00604 (cl->tech->devicestate) ? "yes" : "no",
00605 (cl->tech->indicate) ? "yes" : "no",
00606 (cl->tech->transfer) ? "yes" : "no",
00607 ast_getformatname_multiple(buf, sizeof(buf), (cl->tech->capabilities) ? cl->tech->capabilities : -1),
00608 (cl->tech->send_digit_begin) ? "yes" : "no",
00609 (cl->tech->send_digit_end) ? "yes" : "no",
00610 (cl->tech->send_html) ? "yes" : "no",
00611 (cl->tech->send_image) ? "yes" : "no",
00612 (cl->tech->send_text) ? "yes" : "no"
00613
00614 );
00615
00616 AST_RWLIST_UNLOCK(&backends);
00617
00618 return CLI_SUCCESS;
00619 }
00620
00621 static struct ast_cli_entry cli_channel[] = {
00622 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
00623 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
00624 };
00625
00626 static struct ast_frame *kill_read(struct ast_channel *chan)
00627 {
00628
00629 return NULL;
00630 }
00631
00632 static struct ast_frame *kill_exception(struct ast_channel *chan)
00633 {
00634
00635 return NULL;
00636 }
00637
00638 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
00639 {
00640
00641 return -1;
00642 }
00643
00644 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00645 {
00646
00647 return 0;
00648 }
00649
00650 static int kill_hangup(struct ast_channel *chan)
00651 {
00652 chan->tech_pvt = NULL;
00653 return 0;
00654 }
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 const struct ast_channel_tech ast_kill_tech = {
00666 .type = "Kill",
00667 .description = "Kill channel (should not see this)",
00668 .capabilities = -1,
00669 .read = kill_read,
00670 .exception = kill_exception,
00671 .write = kill_write,
00672 .fixup = kill_fixup,
00673 .hangup = kill_hangup,
00674 };
00675
00676 #ifdef CHANNEL_TRACE
00677
00678 static void ast_chan_trace_destroy_cb(void *data)
00679 {
00680 struct ast_chan_trace *trace;
00681 struct ast_chan_trace_data *traced = data;
00682 while ((trace = AST_LIST_REMOVE_HEAD(&traced->trace, entry))) {
00683 ast_free(trace);
00684 }
00685 ast_free(traced);
00686 }
00687
00688
00689 static const struct ast_datastore_info ast_chan_trace_datastore_info = {
00690 .type = "ChanTrace",
00691 .destroy = ast_chan_trace_destroy_cb
00692 };
00693
00694
00695 int ast_channel_trace_serialize(struct ast_channel *chan, struct ast_str **buf)
00696 {
00697 int total = 0;
00698 struct ast_chan_trace *trace;
00699 struct ast_chan_trace_data *traced;
00700 struct ast_datastore *store;
00701
00702 ast_channel_lock(chan);
00703 store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
00704 if (!store) {
00705 ast_channel_unlock(chan);
00706 return total;
00707 }
00708 traced = store->data;
00709 ast_str_reset(*buf);
00710 AST_LIST_TRAVERSE(&traced->trace, trace, entry) {
00711 if (ast_str_append(buf, 0, "[%d] => %s, %s, %d\n", total, trace->context, trace->exten, trace->priority) < 0) {
00712 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
00713 total = -1;
00714 break;
00715 }
00716 total++;
00717 }
00718 ast_channel_unlock(chan);
00719 return total;
00720 }
00721
00722
00723 int ast_channel_trace_is_enabled(struct ast_channel *chan)
00724 {
00725 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
00726 if (!store)
00727 return 0;
00728 return ((struct ast_chan_trace_data *)store->data)->enabled;
00729 }
00730
00731
00732 static int ast_channel_trace_data_update(struct ast_channel *chan, struct ast_chan_trace_data *traced)
00733 {
00734 struct ast_chan_trace *trace;
00735 if (!traced->enabled)
00736 return 0;
00737
00738
00739 if ((!AST_LIST_EMPTY(&traced->trace) && strcasecmp(AST_LIST_FIRST(&traced->trace)->context, chan->context)) ||
00740 (AST_LIST_EMPTY(&traced->trace))) {
00741
00742 if (AST_LIST_EMPTY(&traced->trace))
00743 ast_log(LOG_DEBUG, "Setting initial trace context to %s\n", chan->context);
00744 else
00745 ast_log(LOG_DEBUG, "Changing trace context from %s to %s\n", AST_LIST_FIRST(&traced->trace)->context, chan->context);
00746
00747 trace = ast_malloc(sizeof(*trace));
00748 if (!trace)
00749 return -1;
00750
00751 ast_copy_string(trace->context, chan->context, sizeof(trace->context));
00752 ast_copy_string(trace->exten, chan->exten, sizeof(trace->exten));
00753 trace->priority = chan->priority;
00754 AST_LIST_INSERT_HEAD(&traced->trace, trace, entry);
00755 }
00756 return 0;
00757 }
00758
00759
00760 int ast_channel_trace_update(struct ast_channel *chan)
00761 {
00762 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
00763 if (!store)
00764 return 0;
00765 return ast_channel_trace_data_update(chan, store->data);
00766 }
00767
00768
00769 int ast_channel_trace_enable(struct ast_channel *chan)
00770 {
00771 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
00772 struct ast_chan_trace_data *traced;
00773 if (!store) {
00774 store = ast_datastore_alloc(&ast_chan_trace_datastore_info, "ChanTrace");
00775 if (!store)
00776 return -1;
00777 traced = ast_calloc(1, sizeof(*traced));
00778 if (!traced) {
00779 ast_datastore_free(store);
00780 return -1;
00781 }
00782 store->data = traced;
00783 AST_LIST_HEAD_INIT_NOLOCK(&traced->trace);
00784 ast_channel_datastore_add(chan, store);
00785 }
00786 ((struct ast_chan_trace_data *)store->data)->enabled = 1;
00787 ast_channel_trace_data_update(chan, store->data);
00788 return 0;
00789 }
00790
00791
00792 int ast_channel_trace_disable(struct ast_channel *chan)
00793 {
00794 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
00795 if (!store)
00796 return 0;
00797 ((struct ast_chan_trace_data *)store->data)->enabled = 0;
00798 return 0;
00799 }
00800 #endif
00801
00802
00803 int ast_check_hangup(struct ast_channel *chan)
00804 {
00805 if (chan->_softhangup)
00806 return 1;
00807 if (ast_tvzero(chan->whentohangup))
00808 return 0;
00809 if (ast_tvdiff_ms(chan->whentohangup, ast_tvnow()) > 0)
00810 return 0;
00811 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(chan->whentohangup, ast_tvnow()));
00812 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
00813 return 1;
00814 }
00815
00816 int ast_check_hangup_locked(struct ast_channel *chan)
00817 {
00818 int res;
00819 ast_channel_lock(chan);
00820 res = ast_check_hangup(chan);
00821 ast_channel_unlock(chan);
00822 return res;
00823 }
00824
00825 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
00826 {
00827 struct ast_channel *chan = obj;
00828
00829 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
00830
00831 return 0;
00832 }
00833
00834 void ast_begin_shutdown(int hangup)
00835 {
00836 shutting_down = 1;
00837
00838 if (hangup) {
00839 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
00840 }
00841 }
00842
00843
00844 int ast_active_channels(void)
00845 {
00846 return channels ? ao2_container_count(channels) : 0;
00847 }
00848
00849 int ast_undestroyed_channels(void)
00850 {
00851 return ast_atomic_fetchadd_int(&chancount, 0);
00852 }
00853
00854
00855 void ast_cancel_shutdown(void)
00856 {
00857 shutting_down = 0;
00858 }
00859
00860
00861 int ast_shutting_down(void)
00862 {
00863 return shutting_down;
00864 }
00865
00866
00867 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
00868 {
00869 chan->whentohangup = ast_tvzero(offset) ? offset : ast_tvadd(offset, ast_tvnow());
00870 ast_queue_frame(chan, &ast_null_frame);
00871 return;
00872 }
00873
00874 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
00875 {
00876 struct timeval when = { offset, };
00877 ast_channel_setwhentohangup_tv(chan, when);
00878 }
00879
00880
00881 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
00882 {
00883 struct timeval whentohangup;
00884
00885 if (ast_tvzero(chan->whentohangup))
00886 return ast_tvzero(offset) ? 0 : -1;
00887
00888 if (ast_tvzero(offset))
00889 return 1;
00890
00891 whentohangup = ast_tvadd(offset, ast_tvnow());
00892
00893 return ast_tvdiff_ms(whentohangup, chan->whentohangup);
00894 }
00895
00896 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
00897 {
00898 struct timeval when = { offset, };
00899 return ast_channel_cmpwhentohangup_tv(chan, when);
00900 }
00901
00902
00903 int ast_channel_register(const struct ast_channel_tech *tech)
00904 {
00905 struct chanlist *chan;
00906
00907 AST_RWLIST_WRLOCK(&backends);
00908
00909 AST_RWLIST_TRAVERSE(&backends, chan, list) {
00910 if (!strcasecmp(tech->type, chan->tech->type)) {
00911 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
00912 AST_RWLIST_UNLOCK(&backends);
00913 return -1;
00914 }
00915 }
00916
00917 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
00918 AST_RWLIST_UNLOCK(&backends);
00919 return -1;
00920 }
00921 chan->tech = tech;
00922 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
00923
00924 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
00925
00926 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
00927
00928 AST_RWLIST_UNLOCK(&backends);
00929
00930 return 0;
00931 }
00932
00933
00934 void ast_channel_unregister(const struct ast_channel_tech *tech)
00935 {
00936 struct chanlist *chan;
00937
00938 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
00939
00940 AST_RWLIST_WRLOCK(&backends);
00941
00942 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
00943 if (chan->tech == tech) {
00944 AST_LIST_REMOVE_CURRENT(list);
00945 ast_free(chan);
00946 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
00947 break;
00948 }
00949 }
00950 AST_LIST_TRAVERSE_SAFE_END;
00951
00952 AST_RWLIST_UNLOCK(&backends);
00953 }
00954
00955
00956 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
00957 {
00958 struct chanlist *chanls;
00959 const struct ast_channel_tech *ret = NULL;
00960
00961 AST_RWLIST_RDLOCK(&backends);
00962
00963 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
00964 if (!strcasecmp(name, chanls->tech->type)) {
00965 ret = chanls->tech;
00966 break;
00967 }
00968 }
00969
00970 AST_RWLIST_UNLOCK(&backends);
00971
00972 return ret;
00973 }
00974
00975
00976 const char *ast_cause2str(int cause)
00977 {
00978 int x;
00979
00980 for (x = 0; x < ARRAY_LEN(causes); x++) {
00981 if (causes[x].cause == cause)
00982 return causes[x].desc;
00983 }
00984
00985 return "Unknown";
00986 }
00987
00988
00989 int ast_str2cause(const char *name)
00990 {
00991 int x;
00992
00993 for (x = 0; x < ARRAY_LEN(causes); x++)
00994 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
00995 return causes[x].cause;
00996
00997 return -1;
00998 }
00999
01000
01001
01002
01003 const char *ast_state2str(enum ast_channel_state state)
01004 {
01005 char *buf;
01006
01007 switch (state) {
01008 case AST_STATE_DOWN:
01009 return "Down";
01010 case AST_STATE_RESERVED:
01011 return "Rsrvd";
01012 case AST_STATE_OFFHOOK:
01013 return "OffHook";
01014 case AST_STATE_DIALING:
01015 return "Dialing";
01016 case AST_STATE_RING:
01017 return "Ring";
01018 case AST_STATE_RINGING:
01019 return "Ringing";
01020 case AST_STATE_UP:
01021 return "Up";
01022 case AST_STATE_BUSY:
01023 return "Busy";
01024 case AST_STATE_DIALING_OFFHOOK:
01025 return "Dialing Offhook";
01026 case AST_STATE_PRERING:
01027 return "Pre-ring";
01028 default:
01029 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
01030 return "Unknown";
01031 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)", state);
01032 return buf;
01033 }
01034 }
01035
01036
01037 char *ast_transfercapability2str(int transfercapability)
01038 {
01039 switch (transfercapability) {
01040 case AST_TRANS_CAP_SPEECH:
01041 return "SPEECH";
01042 case AST_TRANS_CAP_DIGITAL:
01043 return "DIGITAL";
01044 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
01045 return "RESTRICTED_DIGITAL";
01046 case AST_TRANS_CAP_3_1K_AUDIO:
01047 return "3K1AUDIO";
01048 case AST_TRANS_CAP_DIGITAL_W_TONES:
01049 return "DIGITAL_W_TONES";
01050 case AST_TRANS_CAP_VIDEO:
01051 return "VIDEO";
01052 default:
01053 return "UNKNOWN";
01054 }
01055 }
01056
01057
01058 format_t ast_best_codec(format_t fmts)
01059 {
01060
01061
01062 int x;
01063 static const format_t prefs[] =
01064 {
01065
01066 AST_FORMAT_ULAW,
01067
01068 AST_FORMAT_ALAW,
01069 AST_FORMAT_G719,
01070 AST_FORMAT_SIREN14,
01071 AST_FORMAT_SIREN7,
01072 AST_FORMAT_TESTLAW,
01073
01074 AST_FORMAT_G722,
01075
01076 AST_FORMAT_SLINEAR16,
01077 AST_FORMAT_SLINEAR,
01078
01079 AST_FORMAT_G726,
01080
01081 AST_FORMAT_G726_AAL2,
01082
01083 AST_FORMAT_ADPCM,
01084
01085
01086 AST_FORMAT_GSM,
01087
01088 AST_FORMAT_ILBC,
01089
01090 AST_FORMAT_SPEEX16,
01091 AST_FORMAT_SPEEX,
01092
01093
01094 AST_FORMAT_LPC10,
01095
01096 AST_FORMAT_G729A,
01097
01098 AST_FORMAT_G723_1,
01099 };
01100 char buf[512];
01101
01102
01103 fmts &= AST_FORMAT_AUDIO_MASK;
01104
01105
01106 for (x = 0; x < ARRAY_LEN(prefs); x++) {
01107 if (fmts & prefs[x])
01108 return prefs[x];
01109 }
01110
01111 ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), fmts));
01112
01113 return 0;
01114 }
01115
01116 static const struct ast_channel_tech null_tech = {
01117 .type = "NULL",
01118 .description = "Null channel (should not see this)",
01119 };
01120
01121 static void ast_channel_destructor(void *obj);
01122 static void ast_dummy_channel_destructor(void *obj);
01123
01124
01125 static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
01126 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
01127 const char *acctcode, const char *exten, const char *context,
01128 const char *linkedid, const int amaflag, const char *file, int line,
01129 const char *function, const char *name_fmt, va_list ap1, va_list ap2)
01130 {
01131 struct ast_channel *tmp;
01132 int x;
01133 int flags;
01134 struct varshead *headp;
01135 char *tech = "", *tech2 = NULL;
01136
01137
01138 if (shutting_down) {
01139 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
01140 return NULL;
01141 }
01142
01143 #if defined(REF_DEBUG)
01144 tmp = __ao2_alloc_debug(sizeof(*tmp), ast_channel_destructor, "", file, line,
01145 function, 1);
01146 #elif defined(__AST_DEBUG_MALLOC)
01147 tmp = __ao2_alloc_debug(sizeof(*tmp), ast_channel_destructor, "", file, line,
01148 function, 0);
01149 #else
01150 tmp = ao2_alloc(sizeof(*tmp), ast_channel_destructor);
01151 #endif
01152 if (!tmp) {
01153
01154 return NULL;
01155 }
01156
01157
01158
01159
01160
01161 tmp->timingfd = -1;
01162 for (x = 0; x < ARRAY_LEN(tmp->alertpipe); ++x) {
01163 tmp->alertpipe[x] = -1;
01164 }
01165 for (x = 0; x < ARRAY_LEN(tmp->fds); ++x) {
01166 tmp->fds[x] = -1;
01167 }
01168 #ifdef HAVE_EPOLL
01169 tmp->epfd = epoll_create(25);
01170 #endif
01171
01172 if (!(tmp->sched = sched_context_create())) {
01173 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
01174 return ast_channel_unref(tmp);
01175 }
01176
01177 ast_party_dialed_init(&tmp->dialed);
01178 ast_party_caller_init(&tmp->caller);
01179 ast_party_connected_line_init(&tmp->connected);
01180 ast_party_redirecting_init(&tmp->redirecting);
01181
01182 if (cid_name) {
01183 tmp->caller.id.name.valid = 1;
01184 tmp->caller.id.name.str = ast_strdup(cid_name);
01185 if (!tmp->caller.id.name.str) {
01186 return ast_channel_unref(tmp);
01187 }
01188 }
01189 if (cid_num) {
01190 tmp->caller.id.number.valid = 1;
01191 tmp->caller.id.number.str = ast_strdup(cid_num);
01192 if (!tmp->caller.id.number.str) {
01193 return ast_channel_unref(tmp);
01194 }
01195 }
01196
01197 if ((tmp->timer = ast_timer_open())) {
01198 if (strcmp(ast_timer_get_name(tmp->timer), "timerfd")) {
01199 needqueue = 0;
01200 }
01201 tmp->timingfd = ast_timer_fd(tmp->timer);
01202 }
01203
01204 if (needqueue) {
01205 if (pipe(tmp->alertpipe)) {
01206 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
01207 return ast_channel_unref(tmp);
01208 } else {
01209 flags = fcntl(tmp->alertpipe[0], F_GETFL);
01210 if (fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
01211 ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
01212 return ast_channel_unref(tmp);
01213 }
01214 flags = fcntl(tmp->alertpipe[1], F_GETFL);
01215 if (fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
01216 ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
01217 return ast_channel_unref(tmp);
01218 }
01219 }
01220 }
01221
01222
01223
01224
01225
01226
01227
01228
01229 if ((ast_string_field_init(tmp, 128))) {
01230 return ast_channel_unref(tmp);
01231 }
01232
01233
01234 ast_channel_set_fd(tmp, AST_ALERT_FD, tmp->alertpipe[0]);
01235
01236 ast_channel_set_fd(tmp, AST_TIMING_FD, tmp->timingfd);
01237
01238
01239 tmp->_state = state;
01240
01241 tmp->streamid = -1;
01242
01243 tmp->fin = global_fin;
01244 tmp->fout = global_fout;
01245
01246 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
01247 ast_string_field_build(tmp, uniqueid, "%li.%d", (long) time(NULL),
01248 ast_atomic_fetchadd_int(&uniqueint, 1));
01249 } else {
01250 ast_string_field_build(tmp, uniqueid, "%s-%li.%d", ast_config_AST_SYSTEM_NAME,
01251 (long) time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1));
01252 }
01253
01254 if (!ast_strlen_zero(linkedid)) {
01255 ast_string_field_set(tmp, linkedid, linkedid);
01256 } else {
01257 ast_string_field_set(tmp, linkedid, tmp->uniqueid);
01258 }
01259
01260 if (!ast_strlen_zero(name_fmt)) {
01261 char *slash, *slash2;
01262
01263
01264
01265
01266
01267
01268
01269 ast_string_field_build_va(tmp, name, name_fmt, ap1, ap2);
01270 tech = ast_strdupa(tmp->name);
01271 if ((slash = strchr(tech, '/'))) {
01272 if ((slash2 = strchr(slash + 1, '/'))) {
01273 tech2 = slash + 1;
01274 *slash2 = '\0';
01275 }
01276 *slash = '\0';
01277 }
01278 } else {
01279
01280
01281
01282
01283 ast_string_field_set(tmp, name, "-**Unknown**");
01284 }
01285
01286
01287
01288
01289 if (amaflag)
01290 tmp->amaflags = amaflag;
01291 else
01292 tmp->amaflags = ast_default_amaflags;
01293
01294 if (!ast_strlen_zero(acctcode))
01295 ast_string_field_set(tmp, accountcode, acctcode);
01296 else
01297 ast_string_field_set(tmp, accountcode, ast_default_accountcode);
01298
01299 if (!ast_strlen_zero(context))
01300 ast_copy_string(tmp->context, context, sizeof(tmp->context));
01301 else
01302 strcpy(tmp->context, "default");
01303
01304 if (!ast_strlen_zero(exten))
01305 ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
01306 else
01307 strcpy(tmp->exten, "s");
01308
01309 tmp->priority = 1;
01310
01311 tmp->cdr = ast_cdr_alloc();
01312 ast_cdr_init(tmp->cdr, tmp);
01313 ast_cdr_start(tmp->cdr);
01314
01315 ast_atomic_fetchadd_int(&chancount, +1);
01316 ast_cel_report_event(tmp, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
01317
01318 headp = &tmp->varshead;
01319 AST_LIST_HEAD_INIT_NOLOCK(headp);
01320
01321 AST_LIST_HEAD_INIT_NOLOCK(&tmp->datastores);
01322
01323 AST_LIST_HEAD_INIT_NOLOCK(&tmp->autochans);
01324
01325 ast_string_field_set(tmp, language, defaultlanguage);
01326
01327 tmp->tech = &null_tech;
01328
01329 ao2_link(channels, tmp);
01330
01331
01332
01333
01334
01335
01336
01337 if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
01338 ast_manager_event(tmp, EVENT_FLAG_CALL, "Newchannel",
01339 "Channel: %s\r\n"
01340 "ChannelState: %d\r\n"
01341 "ChannelStateDesc: %s\r\n"
01342 "CallerIDNum: %s\r\n"
01343 "CallerIDName: %s\r\n"
01344 "AccountCode: %s\r\n"
01345 "Exten: %s\r\n"
01346 "Context: %s\r\n"
01347 "Uniqueid: %s\r\n",
01348 tmp->name,
01349 state,
01350 ast_state2str(state),
01351 S_OR(cid_num, ""),
01352 S_OR(cid_name, ""),
01353 tmp->accountcode,
01354 S_OR(exten, ""),
01355 S_OR(context, ""),
01356 tmp->uniqueid);
01357 }
01358
01359 return tmp;
01360 }
01361
01362 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
01363 const char *cid_name, const char *acctcode,
01364 const char *exten, const char *context,
01365 const char *linkedid, const int amaflag,
01366 const char *file, int line, const char *function,
01367 const char *name_fmt, ...)
01368 {
01369 va_list ap1, ap2;
01370 struct ast_channel *result;
01371
01372 va_start(ap1, name_fmt);
01373 va_start(ap2, name_fmt);
01374 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
01375 linkedid, amaflag, file, line, function, name_fmt, ap1, ap2);
01376 va_end(ap1);
01377 va_end(ap2);
01378
01379 return result;
01380 }
01381
01382
01383
01384 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
01385 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
01386 #else
01387 struct ast_channel *ast_dummy_channel_alloc(void)
01388 #endif
01389 {
01390 struct ast_channel *tmp;
01391 struct varshead *headp;
01392 int x;
01393
01394 #if defined(REF_DEBUG)
01395 tmp = __ao2_alloc_debug(sizeof(*tmp), ast_dummy_channel_destructor, "dummy channel",
01396 file, line, function, 1);
01397 #elif defined(__AST_DEBUG_MALLOC)
01398 tmp = __ao2_alloc_debug(sizeof(*tmp), ast_dummy_channel_destructor, "dummy channel",
01399 file, line, function, 0);
01400 #else
01401 tmp = ao2_alloc(sizeof(*tmp), ast_dummy_channel_destructor);
01402 #endif
01403 if (!tmp) {
01404
01405 return NULL;
01406 }
01407
01408 if ((ast_string_field_init(tmp, 128))) {
01409 return ast_channel_unref(tmp);
01410 }
01411
01412
01413
01414
01415
01416
01417 tmp->timingfd = -1;
01418 for (x = 0; x < ARRAY_LEN(tmp->alertpipe); ++x) {
01419 tmp->alertpipe[x] = -1;
01420 }
01421 for (x = 0; x < ARRAY_LEN(tmp->fds); ++x) {
01422 tmp->fds[x] = -1;
01423 }
01424 #ifdef HAVE_EPOLL
01425 tmp->epfd = -1;
01426 #endif
01427
01428 headp = &tmp->varshead;
01429 AST_LIST_HEAD_INIT_NOLOCK(headp);
01430
01431 return tmp;
01432 }
01433
01434 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
01435 {
01436 struct ast_frame *f;
01437 struct ast_frame *cur;
01438 unsigned int new_frames = 0;
01439 unsigned int new_voice_frames = 0;
01440 unsigned int queued_frames = 0;
01441 unsigned int queued_voice_frames = 0;
01442 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
01443
01444 ast_channel_lock(chan);
01445
01446
01447
01448
01449
01450 cur = AST_LIST_LAST(&chan->readq);
01451 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
01452 switch (cur->subclass.integer) {
01453 case AST_CONTROL_END_OF_Q:
01454 if (fin->frametype == AST_FRAME_CONTROL
01455 && fin->subclass.integer == AST_CONTROL_HANGUP) {
01456
01457
01458
01459
01460 AST_LIST_REMOVE(&chan->readq, cur, frame_list);
01461 ast_frfree(cur);
01462
01463
01464
01465
01466
01467
01468 after = NULL;
01469 break;
01470 }
01471
01472 case AST_CONTROL_HANGUP:
01473
01474 ast_channel_unlock(chan);
01475 return 0;
01476 default:
01477 break;
01478 }
01479 }
01480
01481
01482 AST_LIST_HEAD_INIT_NOLOCK(&frames);
01483 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
01484 if (!(f = ast_frdup(cur))) {
01485 if (AST_LIST_FIRST(&frames)) {
01486 ast_frfree(AST_LIST_FIRST(&frames));
01487 }
01488 ast_channel_unlock(chan);
01489 return -1;
01490 }
01491
01492 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
01493 new_frames++;
01494 if (f->frametype == AST_FRAME_VOICE) {
01495 new_voice_frames++;
01496 }
01497 }
01498
01499
01500 AST_LIST_TRAVERSE(&chan->readq, cur, frame_list) {
01501 queued_frames++;
01502 if (cur->frametype == AST_FRAME_VOICE) {
01503 queued_voice_frames++;
01504 }
01505 }
01506
01507 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
01508 int count = 0;
01509 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", chan->name);
01510 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->readq, cur, frame_list) {
01511
01512 if (!AST_LIST_NEXT(cur, frame_list)) {
01513 break;
01514 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
01515 if (++count > 64) {
01516 break;
01517 }
01518 AST_LIST_REMOVE_CURRENT(frame_list);
01519 ast_frfree(cur);
01520 }
01521 }
01522 AST_LIST_TRAVERSE_SAFE_END;
01523 }
01524
01525 if (after) {
01526 AST_LIST_INSERT_LIST_AFTER(&chan->readq, &frames, after, frame_list);
01527 } else {
01528 if (head) {
01529 AST_LIST_APPEND_LIST(&frames, &chan->readq, frame_list);
01530 AST_LIST_HEAD_INIT_NOLOCK(&chan->readq);
01531 }
01532 AST_LIST_APPEND_LIST(&chan->readq, &frames, frame_list);
01533 }
01534
01535 if (chan->alertpipe[1] > -1) {
01536 int blah[new_frames];
01537
01538 memset(blah, 1, sizeof(blah));
01539 if (write(chan->alertpipe[1], &blah, sizeof(blah)) != (sizeof(blah))) {
01540 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %d): %s!\n",
01541 chan->name, queued_frames, strerror(errno));
01542 }
01543 } else if (chan->timingfd > -1) {
01544 ast_timer_enable_continuous(chan->timer);
01545 } else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
01546 pthread_kill(chan->blocker, SIGURG);
01547 }
01548
01549 ast_channel_unlock(chan);
01550
01551 return 0;
01552 }
01553
01554 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
01555 {
01556 return __ast_queue_frame(chan, fin, 0, NULL);
01557 }
01558
01559 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
01560 {
01561 return __ast_queue_frame(chan, fin, 1, NULL);
01562 }
01563
01564
01565 int ast_queue_hangup(struct ast_channel *chan)
01566 {
01567 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
01568
01569 if (!ast_channel_trylock(chan)) {
01570 chan->_softhangup |= AST_SOFTHANGUP_DEV;
01571 ast_channel_unlock(chan);
01572 }
01573 return ast_queue_frame(chan, &f);
01574 }
01575
01576
01577 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
01578 {
01579 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
01580
01581 if (cause >= 0)
01582 f.data.uint32 = cause;
01583
01584
01585 if (!ast_channel_trylock(chan)) {
01586 chan->_softhangup |= AST_SOFTHANGUP_DEV;
01587 if (cause < 0)
01588 f.data.uint32 = chan->hangupcause;
01589
01590 ast_channel_unlock(chan);
01591 }
01592
01593 return ast_queue_frame(chan, &f);
01594 }
01595
01596
01597 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
01598 {
01599 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
01600 return ast_queue_frame(chan, &f);
01601 }
01602
01603
01604 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
01605 const void *data, size_t datalen)
01606 {
01607 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
01608 return ast_queue_frame(chan, &f);
01609 }
01610
01611
01612 int ast_channel_defer_dtmf(struct ast_channel *chan)
01613 {
01614 int pre = 0;
01615
01616 if (chan) {
01617 pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF);
01618 ast_set_flag(chan, AST_FLAG_DEFER_DTMF);
01619 }
01620 return pre;
01621 }
01622
01623
01624 void ast_channel_undefer_dtmf(struct ast_channel *chan)
01625 {
01626 if (chan)
01627 ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
01628 }
01629
01630 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
01631 void *data, int ao2_flags)
01632 {
01633 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
01634 }
01635
01636 struct ast_channel_iterator {
01637
01638 struct ao2_iterator simple_iterator;
01639
01640
01641
01642 struct ao2_iterator *active_iterator;
01643 };
01644
01645 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
01646 {
01647 ao2_iterator_destroy(i->active_iterator);
01648 ast_free(i);
01649
01650 return NULL;
01651 }
01652
01653 static struct ast_channel_iterator *channel_iterator_search(const char *name,
01654 size_t name_len, const char *exten,
01655 const char *context)
01656 {
01657 struct ast_channel_iterator *i;
01658 struct ast_channel tmp_chan = {
01659 .name = name,
01660
01661
01662
01663 .rings = name_len,
01664 };
01665
01666 if (!(i = ast_calloc(1, sizeof(*i)))) {
01667 return NULL;
01668 }
01669
01670 if (exten) {
01671 ast_copy_string(tmp_chan.exten, exten, sizeof(tmp_chan.exten));
01672 }
01673
01674 if (context) {
01675 ast_copy_string(tmp_chan.context, context, sizeof(tmp_chan.context));
01676 }
01677
01678 if (!(i->active_iterator = ao2_find(channels, &tmp_chan,
01679 OBJ_MULTIPLE | ((!ast_strlen_zero(name) && (name_len == 0)) ? OBJ_POINTER : 0)))) {
01680 ast_free(i);
01681 return NULL;
01682 }
01683
01684 return i;
01685 }
01686
01687 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
01688 {
01689 return channel_iterator_search(NULL, 0, exten, context);
01690 }
01691
01692 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
01693 {
01694 return channel_iterator_search(name, name_len, NULL, NULL);
01695 }
01696
01697 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
01698 {
01699 struct ast_channel_iterator *i;
01700
01701 if (!(i = ast_calloc(1, sizeof(*i)))) {
01702 return NULL;
01703 }
01704
01705 i->simple_iterator = ao2_iterator_init(channels, 0);
01706 i->active_iterator = &i->simple_iterator;
01707
01708 return i;
01709 }
01710
01711 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
01712 {
01713 return ao2_iterator_next(i->active_iterator);
01714 }
01715
01716 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
01717 {
01718 struct ast_channel *chan = obj, *cmp_args = arg;
01719 size_t name_len;
01720 int ret = CMP_MATCH;
01721
01722
01723
01724
01725 name_len = cmp_args->rings;
01726
01727 ast_channel_lock(chan);
01728
01729 if (!ast_strlen_zero(cmp_args->name)) {
01730 if ((!name_len && strcasecmp(chan->name, cmp_args->name)) ||
01731 (name_len && strncasecmp(chan->name, cmp_args->name, name_len))) {
01732 ret = 0;
01733 }
01734 } else if (!ast_strlen_zero(cmp_args->exten)) {
01735 if (cmp_args->context && strcasecmp(chan->context, cmp_args->context) &&
01736 strcasecmp(chan->macrocontext, cmp_args->context)) {
01737 ret = 0;
01738 }
01739 if (ret && strcasecmp(chan->exten, cmp_args->exten) &&
01740 strcasecmp(chan->macroexten, cmp_args->exten)) {
01741 ret = 0;
01742 }
01743 } else if (!ast_strlen_zero(cmp_args->uniqueid)) {
01744 if ((!name_len && strcasecmp(chan->uniqueid, cmp_args->uniqueid)) ||
01745 (name_len && strncasecmp(chan->uniqueid, cmp_args->uniqueid, name_len))) {
01746 ret = 0;
01747 }
01748 } else {
01749 ret = 0;
01750 }
01751
01752 ast_channel_unlock(chan);
01753
01754 return ret;
01755 }
01756
01757 static struct ast_channel *ast_channel_get_full(const char *name, size_t name_len,
01758 const char *exten, const char *context)
01759 {
01760 struct ast_channel tmp_chan = {
01761 .name = name,
01762
01763
01764
01765 .rings = name_len,
01766 };
01767 struct ast_channel *chan;
01768
01769 if (exten) {
01770 ast_copy_string(tmp_chan.exten, exten, sizeof(tmp_chan.exten));
01771 }
01772
01773 if (context) {
01774 ast_copy_string(tmp_chan.context, context, sizeof(tmp_chan.context));
01775 }
01776
01777 if ((chan = ao2_find(channels, &tmp_chan,
01778 (!ast_strlen_zero(name) && (name_len == 0)) ? OBJ_POINTER : 0))) {
01779 return chan;
01780 }
01781
01782 if (!name) {
01783 return NULL;
01784 }
01785
01786
01787
01788
01789 {
01790 struct ast_channel tmp_chan2 = {
01791 .uniqueid = name,
01792 .rings = name_len,
01793 };
01794
01795 return ao2_find(channels, &tmp_chan2, 0);
01796 }
01797 }
01798
01799 struct ast_channel *ast_channel_get_by_name(const char *name)
01800 {
01801 return ast_channel_get_full(name, 0, NULL, NULL);
01802 }
01803
01804 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
01805 {
01806 return ast_channel_get_full(name, name_len, NULL, NULL);
01807 }
01808
01809 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
01810 {
01811 return ast_channel_get_full(NULL, 0, exten, context);
01812 }
01813
01814 int ast_is_deferrable_frame(const struct ast_frame *frame)
01815 {
01816
01817
01818
01819
01820 switch (frame->frametype) {
01821 case AST_FRAME_CONTROL:
01822 case AST_FRAME_TEXT:
01823 case AST_FRAME_IMAGE:
01824 case AST_FRAME_HTML:
01825 return 1;
01826
01827 case AST_FRAME_DTMF_END:
01828 case AST_FRAME_DTMF_BEGIN:
01829 case AST_FRAME_VOICE:
01830 case AST_FRAME_VIDEO:
01831 case AST_FRAME_NULL:
01832 case AST_FRAME_IAX:
01833 case AST_FRAME_CNG:
01834 case AST_FRAME_MODEM:
01835 return 0;
01836 }
01837 return 0;
01838 }
01839
01840
01841 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
01842 {
01843 struct ast_frame *f;
01844 struct ast_silence_generator *silgen = NULL;
01845 int res = 0;
01846 struct timeval start;
01847 int ms;
01848 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
01849
01850 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
01851
01852
01853 if (ast_opt_transmit_silence && !chan->generatordata) {
01854 silgen = ast_channel_start_silence_generator(chan);
01855 }
01856
01857 start = ast_tvnow();
01858 while ((ms = ast_remaining_ms(start, timeout_ms))) {
01859 struct ast_frame *dup_f = NULL;
01860
01861 if (cond && ((*cond)(data) == 0)) {
01862 break;
01863 }
01864 ms = ast_waitfor(chan, ms);
01865 if (ms < 0) {
01866 res = -1;
01867 break;
01868 }
01869 if (ms > 0) {
01870 f = ast_read(chan);
01871 if (!f) {
01872 res = -1;
01873 break;
01874 }
01875
01876 if (!ast_is_deferrable_frame(f)) {
01877 ast_frfree(f);
01878 continue;
01879 }
01880
01881 if ((dup_f = ast_frisolate(f))) {
01882 if (dup_f != f) {
01883 ast_frfree(f);
01884 }
01885 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
01886 }
01887 }
01888 }
01889
01890
01891 if (silgen) {
01892 ast_channel_stop_silence_generator(chan, silgen);
01893 }
01894
01895
01896
01897
01898
01899 ast_channel_lock(chan);
01900 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
01901 if (!res) {
01902 ast_queue_frame_head(chan, f);
01903 }
01904 ast_frfree(f);
01905 }
01906 ast_channel_unlock(chan);
01907
01908 return res;
01909 }
01910
01911
01912 int ast_safe_sleep(struct ast_channel *chan, int ms)
01913 {
01914 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
01915 }
01916
01917 struct ast_channel *ast_channel_release(struct ast_channel *chan)
01918 {
01919
01920 ao2_unlink(channels, chan);
01921 return ast_channel_unref(chan);
01922 }
01923
01924 void ast_party_name_init(struct ast_party_name *init)
01925 {
01926 init->str = NULL;
01927 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
01928 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
01929 init->valid = 0;
01930 }
01931
01932 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
01933 {
01934 if (dest == src) {
01935
01936 return;
01937 }
01938
01939 ast_free(dest->str);
01940 dest->str = ast_strdup(src->str);
01941 dest->char_set = src->char_set;
01942 dest->presentation = src->presentation;
01943 dest->valid = src->valid;
01944 }
01945
01946 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
01947 {
01948 init->str = NULL;
01949 init->char_set = guide->char_set;
01950 init->presentation = guide->presentation;
01951 init->valid = guide->valid;
01952 }
01953
01954 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
01955 {
01956 if (dest == src) {
01957
01958 return;
01959 }
01960
01961 if (src->str && src->str != dest->str) {
01962 ast_free(dest->str);
01963 dest->str = ast_strdup(src->str);
01964 }
01965
01966 dest->char_set = src->char_set;
01967 dest->presentation = src->presentation;
01968 dest->valid = src->valid;
01969 }
01970
01971 void ast_party_name_free(struct ast_party_name *doomed)
01972 {
01973 ast_free(doomed->str);
01974 doomed->str = NULL;
01975 }
01976
01977 void ast_party_number_init(struct ast_party_number *init)
01978 {
01979 init->str = NULL;
01980 init->plan = 0;
01981 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
01982 init->valid = 0;
01983 }
01984
01985 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
01986 {
01987 if (dest == src) {
01988
01989 return;
01990 }
01991
01992 ast_free(dest->str);
01993 dest->str = ast_strdup(src->str);
01994 dest->plan = src->plan;
01995 dest->presentation = src->presentation;
01996 dest->valid = src->valid;
01997 }
01998
01999 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
02000 {
02001 init->str = NULL;
02002 init->plan = guide->plan;
02003 init->presentation = guide->presentation;
02004 init->valid = guide->valid;
02005 }
02006
02007 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
02008 {
02009 if (dest == src) {
02010
02011 return;
02012 }
02013
02014 if (src->str && src->str != dest->str) {
02015 ast_free(dest->str);
02016 dest->str = ast_strdup(src->str);
02017 }
02018
02019 dest->plan = src->plan;
02020 dest->presentation = src->presentation;
02021 dest->valid = src->valid;
02022 }
02023
02024 void ast_party_number_free(struct ast_party_number *doomed)
02025 {
02026 ast_free(doomed->str);
02027 doomed->str = NULL;
02028 }
02029
02030 void ast_party_subaddress_init(struct ast_party_subaddress *init)
02031 {
02032 init->str = NULL;
02033 init->type = 0;
02034 init->odd_even_indicator = 0;
02035 init->valid = 0;
02036 }
02037
02038 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
02039 {
02040 if (dest == src) {
02041
02042 return;
02043 }
02044
02045 ast_free(dest->str);
02046 dest->str = ast_strdup(src->str);
02047 dest->type = src->type;
02048 dest->odd_even_indicator = src->odd_even_indicator;
02049 dest->valid = src->valid;
02050 }
02051
02052 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
02053 {
02054 init->str = NULL;
02055 init->type = guide->type;
02056 init->odd_even_indicator = guide->odd_even_indicator;
02057 init->valid = guide->valid;
02058 }
02059
02060 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
02061 {
02062 if (dest == src) {
02063
02064 return;
02065 }
02066
02067 if (src->str && src->str != dest->str) {
02068 ast_free(dest->str);
02069 dest->str = ast_strdup(src->str);
02070 }
02071
02072 dest->type = src->type;
02073 dest->odd_even_indicator = src->odd_even_indicator;
02074 dest->valid = src->valid;
02075 }
02076
02077 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
02078 {
02079 ast_free(doomed->str);
02080 doomed->str = NULL;
02081 }
02082
02083 void ast_party_id_init(struct ast_party_id *init)
02084 {
02085 ast_party_name_init(&init->name);
02086 ast_party_number_init(&init->number);
02087 ast_party_subaddress_init(&init->subaddress);
02088 init->tag = NULL;
02089 }
02090
02091 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
02092 {
02093 if (dest == src) {
02094
02095 return;
02096 }
02097
02098 ast_party_name_copy(&dest->name, &src->name);
02099 ast_party_number_copy(&dest->number, &src->number);
02100 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
02101
02102 ast_free(dest->tag);
02103 dest->tag = ast_strdup(src->tag);
02104 }
02105
02106 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
02107 {
02108 ast_party_name_set_init(&init->name, &guide->name);
02109 ast_party_number_set_init(&init->number, &guide->number);
02110 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
02111 init->tag = NULL;
02112 }
02113
02114 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
02115 {
02116 if (dest == src) {
02117
02118 return;
02119 }
02120
02121 if (!update || update->name) {
02122 ast_party_name_set(&dest->name, &src->name);
02123 }
02124 if (!update || update->number) {
02125 ast_party_number_set(&dest->number, &src->number);
02126 }
02127 if (!update || update->subaddress) {
02128 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
02129 }
02130
02131 if (src->tag && src->tag != dest->tag) {
02132 ast_free(dest->tag);
02133 dest->tag = ast_strdup(src->tag);
02134 }
02135 }
02136
02137 void ast_party_id_free(struct ast_party_id *doomed)
02138 {
02139 ast_party_name_free(&doomed->name);
02140 ast_party_number_free(&doomed->number);
02141 ast_party_subaddress_free(&doomed->subaddress);
02142
02143 ast_free(doomed->tag);
02144 doomed->tag = NULL;
02145 }
02146
02147 int ast_party_id_presentation(const struct ast_party_id *id)
02148 {
02149 int number_priority;
02150 int number_value;
02151 int number_screening;
02152 int name_priority;
02153 int name_value;
02154
02155
02156 if (!id->name.valid) {
02157 name_value = AST_PRES_UNAVAILABLE;
02158 name_priority = 3;
02159 } else {
02160 name_value = id->name.presentation & AST_PRES_RESTRICTION;
02161 switch (name_value) {
02162 case AST_PRES_RESTRICTED:
02163 name_priority = 0;
02164 break;
02165 case AST_PRES_ALLOWED:
02166 name_priority = 1;
02167 break;
02168 case AST_PRES_UNAVAILABLE:
02169 name_priority = 2;
02170 break;
02171 default:
02172 name_value = AST_PRES_UNAVAILABLE;
02173 name_priority = 3;
02174 break;
02175 }
02176 }
02177
02178
02179 if (!id->number.valid) {
02180 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
02181 number_value = AST_PRES_UNAVAILABLE;
02182 number_priority = 3;
02183 } else {
02184 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
02185 number_value = id->number.presentation & AST_PRES_RESTRICTION;
02186 switch (number_value) {
02187 case AST_PRES_RESTRICTED:
02188 number_priority = 0;
02189 break;
02190 case AST_PRES_ALLOWED:
02191 number_priority = 1;
02192 break;
02193 case AST_PRES_UNAVAILABLE:
02194 number_priority = 2;
02195 break;
02196 default:
02197 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
02198 number_value = AST_PRES_UNAVAILABLE;
02199 number_priority = 3;
02200 break;
02201 }
02202 }
02203
02204
02205 if (name_priority < number_priority) {
02206 number_value = name_value;
02207 }
02208 if (number_value == AST_PRES_UNAVAILABLE) {
02209 return AST_PRES_NUMBER_NOT_AVAILABLE;
02210 }
02211
02212 return number_value | number_screening;
02213 }
02214
02215 void ast_party_dialed_init(struct ast_party_dialed *init)
02216 {
02217 init->number.str = NULL;
02218 init->number.plan = 0;
02219 ast_party_subaddress_init(&init->subaddress);
02220 init->transit_network_select = 0;
02221 }
02222
02223 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
02224 {
02225 if (dest == src) {
02226
02227 return;
02228 }
02229
02230 ast_free(dest->number.str);
02231 dest->number.str = ast_strdup(src->number.str);
02232 dest->number.plan = src->number.plan;
02233 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
02234 dest->transit_network_select = src->transit_network_select;
02235 }
02236
02237 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
02238 {
02239 init->number.str = NULL;
02240 init->number.plan = guide->number.plan;
02241 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
02242 init->transit_network_select = guide->transit_network_select;
02243 }
02244
02245 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
02246 {
02247 if (src->number.str && src->number.str != dest->number.str) {
02248 ast_free(dest->number.str);
02249 dest->number.str = ast_strdup(src->number.str);
02250 }
02251 dest->number.plan = src->number.plan;
02252
02253 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
02254
02255 dest->transit_network_select = src->transit_network_select;
02256 }
02257
02258 void ast_party_dialed_free(struct ast_party_dialed *doomed)
02259 {
02260 ast_free(doomed->number.str);
02261 doomed->number.str = NULL;
02262 ast_party_subaddress_free(&doomed->subaddress);
02263 }
02264
02265 void ast_party_caller_init(struct ast_party_caller *init)
02266 {
02267 ast_party_id_init(&init->id);
02268 ast_party_id_init(&init->ani);
02269 init->ani2 = 0;
02270 }
02271
02272 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
02273 {
02274 if (dest == src) {
02275
02276 return;
02277 }
02278
02279 ast_party_id_copy(&dest->id, &src->id);
02280 ast_party_id_copy(&dest->ani, &src->ani);
02281 dest->ani2 = src->ani2;
02282 }
02283
02284 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
02285 {
02286 ast_party_id_set_init(&init->id, &guide->id);
02287 ast_party_id_set_init(&init->ani, &guide->ani);
02288 init->ani2 = guide->ani2;
02289 }
02290
02291 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
02292 {
02293 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
02294 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
02295 dest->ani2 = src->ani2;
02296 }
02297
02298 void ast_party_caller_free(struct ast_party_caller *doomed)
02299 {
02300 ast_party_id_free(&doomed->id);
02301 ast_party_id_free(&doomed->ani);
02302 }
02303
02304 void ast_party_connected_line_init(struct ast_party_connected_line *init)
02305 {
02306 ast_party_id_init(&init->id);
02307 ast_party_id_init(&init->ani);
02308 init->ani2 = 0;
02309 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
02310 }
02311
02312 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
02313 {
02314 if (dest == src) {
02315
02316 return;
02317 }
02318
02319 ast_party_id_copy(&dest->id, &src->id);
02320 ast_party_id_copy(&dest->ani, &src->ani);
02321 dest->ani2 = src->ani2;
02322 dest->source = src->source;
02323 }
02324
02325 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
02326 {
02327 ast_party_id_set_init(&init->id, &guide->id);
02328 ast_party_id_set_init(&init->ani, &guide->ani);
02329 init->ani2 = guide->ani2;
02330 init->source = guide->source;
02331 }
02332
02333 void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
02334 {
02335 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
02336 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
02337 dest->ani2 = src->ani2;
02338 dest->source = src->source;
02339 }
02340
02341 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
02342 {
02343 connected->id = caller->id;
02344 connected->ani = caller->ani;
02345 connected->ani2 = caller->ani2;
02346 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
02347 }
02348
02349 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
02350 {
02351 ast_party_id_free(&doomed->id);
02352 ast_party_id_free(&doomed->ani);
02353 }
02354
02355 void ast_party_redirecting_init(struct ast_party_redirecting *init)
02356 {
02357 ast_party_id_init(&init->from);
02358 ast_party_id_init(&init->to);
02359 init->count = 0;
02360 init->reason = AST_REDIRECTING_REASON_UNKNOWN;
02361 }
02362
02363 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
02364 {
02365 if (dest == src) {
02366
02367 return;
02368 }
02369
02370 ast_party_id_copy(&dest->from, &src->from);
02371 ast_party_id_copy(&dest->to, &src->to);
02372 dest->count = src->count;
02373 dest->reason = src->reason;
02374 }
02375
02376 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
02377 {
02378 ast_party_id_set_init(&init->from, &guide->from);
02379 ast_party_id_set_init(&init->to, &guide->to);
02380 init->count = guide->count;
02381 init->reason = guide->reason;
02382 }
02383
02384 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
02385 {
02386 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
02387 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
02388 dest->reason = src->reason;
02389 dest->count = src->count;
02390 }
02391
02392 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
02393 {
02394 ast_party_id_free(&doomed->from);
02395 ast_party_id_free(&doomed->to);
02396 }
02397
02398
02399 static void ast_channel_destructor(void *obj)
02400 {
02401 struct ast_channel *chan = obj;
02402 int fd;
02403 #ifdef HAVE_EPOLL
02404 int i;
02405 #endif
02406 struct ast_var_t *vardata;
02407 struct ast_frame *f;
02408 struct varshead *headp;
02409 struct ast_datastore *datastore;
02410 char device_name[AST_CHANNEL_NAME];
02411
02412 if (chan->name) {
02413
02414 ast_cel_report_event(chan, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
02415 ast_cel_check_retire_linkedid(chan);
02416 }
02417
02418
02419 ast_channel_lock(chan);
02420 while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry)))
02421
02422 ast_datastore_free(datastore);
02423 ast_channel_unlock(chan);
02424
02425
02426
02427 ast_channel_lock(chan);
02428 ast_channel_unlock(chan);
02429
02430 if (chan->tech_pvt) {
02431 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
02432 ast_free(chan->tech_pvt);
02433 }
02434
02435 if (chan->sched)
02436 sched_context_destroy(chan->sched);
02437
02438 if (chan->name) {
02439 char *dashptr;
02440
02441
02442 ast_copy_string(device_name, chan->name, sizeof(device_name));
02443 if ((dashptr = strrchr(device_name, '-'))) {
02444 *dashptr = '\0';
02445 }
02446 } else {
02447 device_name[0] = '\0';
02448 }
02449
02450
02451 if (chan->monitor)
02452 chan->monitor->stop( chan, 0 );
02453
02454
02455 if (chan->music_state)
02456 ast_moh_cleanup(chan);
02457
02458
02459 if (chan->readtrans)
02460 ast_translator_free_path(chan->readtrans);
02461 if (chan->writetrans)
02462 ast_translator_free_path(chan->writetrans);
02463 if (chan->pbx)
02464 ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
02465
02466 ast_party_dialed_free(&chan->dialed);
02467 ast_party_caller_free(&chan->caller);
02468 ast_party_connected_line_free(&chan->connected);
02469 ast_party_redirecting_free(&chan->redirecting);
02470
02471
02472 if ((fd = chan->alertpipe[0]) > -1)
02473 close(fd);
02474 if ((fd = chan->alertpipe[1]) > -1)
02475 close(fd);
02476 if (chan->timer) {
02477 ast_timer_close(chan->timer);
02478 chan->timer = NULL;
02479 }
02480 #ifdef HAVE_EPOLL
02481 for (i = 0; i < AST_MAX_FDS; i++) {
02482 if (chan->epfd_data[i])
02483 free(chan->epfd_data[i]);
02484 }
02485 close(chan->epfd);
02486 #endif
02487 while ((f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list)))
02488 ast_frfree(f);
02489
02490
02491
02492 headp = &chan->varshead;
02493 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
02494 ast_var_delete(vardata);
02495
02496 ast_app_group_discard(chan);
02497
02498
02499 ast_jb_destroy(chan);
02500
02501 if (chan->cdr) {
02502 ast_cdr_discard(chan->cdr);
02503 chan->cdr = NULL;
02504 }
02505
02506 if (chan->zone) {
02507 chan->zone = ast_tone_zone_unref(chan->zone);
02508 }
02509
02510 ast_string_field_free_memory(chan);
02511
02512 if (device_name[0]) {
02513
02514
02515
02516
02517
02518
02519
02520 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (chan->flags & AST_FLAG_DISABLE_DEVSTATE_CACHE ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
02521 }
02522 ast_atomic_fetchadd_int(&chancount, -1);
02523 }
02524
02525
02526 static void ast_dummy_channel_destructor(void *obj)
02527 {
02528 struct ast_channel *chan = obj;
02529 struct ast_var_t *vardata;
02530 struct varshead *headp;
02531 struct ast_datastore *datastore;
02532
02533
02534 while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry))) {
02535
02536 ast_datastore_free(datastore);
02537 }
02538
02539 headp = &chan->varshead;
02540
02541 ast_party_dialed_free(&chan->dialed);
02542 ast_party_caller_free(&chan->caller);
02543 ast_party_connected_line_free(&chan->connected);
02544 ast_party_redirecting_free(&chan->redirecting);
02545
02546
02547
02548 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
02549 ast_var_delete(vardata);
02550
02551 if (chan->cdr) {
02552 ast_cdr_discard(chan->cdr);
02553 chan->cdr = NULL;
02554 }
02555
02556 ast_string_field_free_memory(chan);
02557 }
02558
02559 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
02560 {
02561 return ast_datastore_alloc(info, uid);
02562 }
02563
02564 int ast_channel_datastore_free(struct ast_datastore *datastore)
02565 {
02566 return ast_datastore_free(datastore);
02567 }
02568
02569 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
02570 {
02571 struct ast_datastore *datastore = NULL, *datastore2;
02572
02573 AST_LIST_TRAVERSE(&from->datastores, datastore, entry) {
02574 if (datastore->inheritance > 0) {
02575 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
02576 if (datastore2) {
02577 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
02578 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
02579 AST_LIST_INSERT_TAIL(&to->datastores, datastore2, entry);
02580 }
02581 }
02582 }
02583 return 0;
02584 }
02585
02586 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
02587 {
02588 int res = 0;
02589
02590 AST_LIST_INSERT_HEAD(&chan->datastores, datastore, entry);
02591
02592 return res;
02593 }
02594
02595 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
02596 {
02597 return AST_LIST_REMOVE(&chan->datastores, datastore, entry) ? 0 : -1;
02598 }
02599
02600 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
02601 {
02602 struct ast_datastore *datastore = NULL;
02603
02604 if (info == NULL)
02605 return NULL;
02606
02607 AST_LIST_TRAVERSE(&chan->datastores, datastore, entry) {
02608 if (datastore->info != info) {
02609 continue;
02610 }
02611
02612 if (uid == NULL) {
02613
02614 break;
02615 }
02616
02617 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
02618
02619 break;
02620 }
02621 }
02622
02623 return datastore;
02624 }
02625
02626
02627 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
02628 {
02629 #ifdef HAVE_EPOLL
02630 struct epoll_event ev;
02631 struct ast_epoll_data *aed = NULL;
02632
02633 if (chan->fds[which] > -1) {
02634 epoll_ctl(chan->epfd, EPOLL_CTL_DEL, chan->fds[which], &ev);
02635 aed = chan->epfd_data[which];
02636 }
02637
02638
02639 if (fd > -1) {
02640 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
02641 return;
02642
02643 chan->epfd_data[which] = aed;
02644 aed->chan = chan;
02645 aed->which = which;
02646
02647 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
02648 ev.data.ptr = aed;
02649 epoll_ctl(chan->epfd, EPOLL_CTL_ADD, fd, &ev);
02650 } else if (aed) {
02651
02652 free(aed);
02653 chan->epfd_data[which] = NULL;
02654 }
02655 #endif
02656 chan->fds[which] = fd;
02657 return;
02658 }
02659
02660
02661 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
02662 {
02663 #ifdef HAVE_EPOLL
02664 struct epoll_event ev;
02665 int i = 0;
02666
02667 if (chan0->epfd == -1)
02668 return;
02669
02670
02671 for (i = 0; i < AST_MAX_FDS; i++) {
02672 if (chan1->fds[i] == -1)
02673 continue;
02674 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
02675 ev.data.ptr = chan1->epfd_data[i];
02676 epoll_ctl(chan0->epfd, EPOLL_CTL_ADD, chan1->fds[i], &ev);
02677 }
02678
02679 #endif
02680 return;
02681 }
02682
02683
02684 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
02685 {
02686 #ifdef HAVE_EPOLL
02687 struct epoll_event ev;
02688 int i = 0;
02689
02690 if (chan0->epfd == -1)
02691 return;
02692
02693 for (i = 0; i < AST_MAX_FDS; i++) {
02694 if (chan1->fds[i] == -1)
02695 continue;
02696 epoll_ctl(chan0->epfd, EPOLL_CTL_DEL, chan1->fds[i], &ev);
02697 }
02698
02699 #endif
02700 return;
02701 }
02702
02703 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
02704 {
02705 ast_channel_lock(chan);
02706
02707 chan->_softhangup &= ~flag;
02708
02709 if (!chan->_softhangup) {
02710 struct ast_frame *fr;
02711
02712
02713
02714
02715
02716
02717 fr = AST_LIST_LAST(&chan->readq);
02718 if (fr && fr->frametype == AST_FRAME_CONTROL &&
02719 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
02720 AST_LIST_REMOVE(&chan->readq, fr, frame_list);
02721 ast_frfree(fr);
02722 }
02723 }
02724
02725 ast_channel_unlock(chan);
02726 }
02727
02728
02729 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
02730 {
02731 ast_debug(1, "Soft-Hanging up channel '%s'\n", chan->name);
02732
02733 chan->_softhangup |= cause;
02734 ast_queue_frame(chan, &ast_null_frame);
02735
02736 if (ast_test_flag(chan, AST_FLAG_BLOCKING))
02737 pthread_kill(chan->blocker, SIGURG);
02738 return 0;
02739 }
02740
02741
02742 int ast_softhangup(struct ast_channel *chan, int cause)
02743 {
02744 int res;
02745
02746 ast_channel_lock(chan);
02747 res = ast_softhangup_nolock(chan, cause);
02748 ast_channel_unlock(chan);
02749
02750 return res;
02751 }
02752
02753 static void free_translation(struct ast_channel *clonechan)
02754 {
02755 if (clonechan->writetrans)
02756 ast_translator_free_path(clonechan->writetrans);
02757 if (clonechan->readtrans)
02758 ast_translator_free_path(clonechan->readtrans);
02759 clonechan->writetrans = NULL;
02760 clonechan->readtrans = NULL;
02761 clonechan->rawwriteformat = clonechan->nativeformats;
02762 clonechan->rawreadformat = clonechan->nativeformats;
02763 }
02764
02765 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
02766 {
02767 struct ast_channel *bridge;
02768
02769 ast_channel_lock(chan);
02770 if (force || ast_strlen_zero(chan->hangupsource)) {
02771 ast_string_field_set(chan, hangupsource, source);
02772 }
02773 bridge = ast_bridged_channel(chan);
02774 if (bridge) {
02775 ast_channel_ref(bridge);
02776 }
02777 ast_channel_unlock(chan);
02778
02779 if (bridge) {
02780 ast_channel_lock(bridge);
02781 if (force || ast_strlen_zero(bridge->hangupsource)) {
02782 ast_string_field_set(bridge, hangupsource, source);
02783 }
02784 ast_channel_unlock(bridge);
02785 ast_channel_unref(bridge);
02786 }
02787 }
02788
02789 static void destroy_hooks(struct ast_channel *chan)
02790 {
02791 if (chan->audiohooks) {
02792 ast_audiohook_detach_list(chan->audiohooks);
02793 chan->audiohooks = NULL;
02794 }
02795
02796 ast_framehook_list_destroy(chan);
02797 }
02798
02799
02800 int ast_hangup(struct ast_channel *chan)
02801 {
02802 char extra_str[64];
02803 int was_zombie;
02804
02805 ast_autoservice_stop(chan);
02806
02807 ast_channel_lock(chan);
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817 while (chan->masq) {
02818 ast_channel_unlock(chan);
02819 ast_do_masquerade(chan);
02820 ast_channel_lock(chan);
02821 }
02822
02823 if (chan->masqr) {
02824
02825
02826
02827
02828
02829 ast_set_flag(chan, AST_FLAG_ZOMBIE);
02830 destroy_hooks(chan);
02831 ast_channel_unlock(chan);
02832 return 0;
02833 }
02834
02835
02836 if (!(was_zombie = ast_test_flag(chan, AST_FLAG_ZOMBIE))) {
02837 ast_set_flag(chan, AST_FLAG_ZOMBIE);
02838 }
02839
02840 ast_channel_unlock(chan);
02841 ao2_unlink(channels, chan);
02842 ast_channel_lock(chan);
02843
02844 destroy_hooks(chan);
02845
02846 free_translation(chan);
02847
02848 if (chan->stream) {
02849 ast_closestream(chan->stream);
02850 chan->stream = NULL;
02851 }
02852
02853 if (chan->vstream) {
02854 ast_closestream(chan->vstream);
02855 chan->vstream = NULL;
02856 }
02857 if (chan->sched) {
02858 sched_context_destroy(chan->sched);
02859 chan->sched = NULL;
02860 }
02861
02862 if (chan->generatordata) {
02863 if (chan->generator && chan->generator->release) {
02864 chan->generator->release(chan, chan->generatordata);
02865 }
02866 }
02867 chan->generatordata = NULL;
02868 chan->generator = NULL;
02869
02870 snprintf(extra_str, sizeof(extra_str), "%d,%s,%s", chan->hangupcause, chan->hangupsource, S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
02871 ast_cel_report_event(chan, AST_CEL_HANGUP, NULL, extra_str, NULL);
02872
02873 if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
02874 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
02875 "is blocked by thread %ld in procedure %s! Expect a failure\n",
02876 (long) pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
02877 ast_assert(ast_test_flag(chan, AST_FLAG_BLOCKING) == 0);
02878 }
02879 if (!was_zombie) {
02880 ast_debug(1, "Hanging up channel '%s'\n", chan->name);
02881
02882 if (chan->tech->hangup) {
02883 chan->tech->hangup(chan);
02884 }
02885 } else {
02886 ast_debug(1, "Hanging up zombie '%s'\n", chan->name);
02887 }
02888
02889 ast_channel_unlock(chan);
02890
02891 ast_cc_offer(chan);
02892 ast_manager_event(chan, EVENT_FLAG_CALL, "Hangup",
02893 "Channel: %s\r\n"
02894 "Uniqueid: %s\r\n"
02895 "CallerIDNum: %s\r\n"
02896 "CallerIDName: %s\r\n"
02897 "ConnectedLineNum: %s\r\n"
02898 "ConnectedLineName: %s\r\n"
02899 "Cause: %d\r\n"
02900 "Cause-txt: %s\r\n",
02901 chan->name,
02902 chan->uniqueid,
02903 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, "<unknown>"),
02904 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, "<unknown>"),
02905 S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, "<unknown>"),
02906 S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, "<unknown>"),
02907 chan->hangupcause,
02908 ast_cause2str(chan->hangupcause)
02909 );
02910
02911 if (chan->cdr && !ast_test_flag(chan->cdr, AST_CDR_FLAG_BRIDGED) &&
02912 !ast_test_flag(chan->cdr, AST_CDR_FLAG_POST_DISABLED) &&
02913 (chan->cdr->disposition != AST_CDR_NULL || ast_test_flag(chan->cdr, AST_CDR_FLAG_DIALED))) {
02914 ast_channel_lock(chan);
02915 ast_cdr_end(chan->cdr);
02916 ast_cdr_detach(chan->cdr);
02917 chan->cdr = NULL;
02918 ast_channel_unlock(chan);
02919 }
02920
02921 ast_channel_unref(chan);
02922
02923 return 0;
02924 }
02925
02926 int ast_raw_answer(struct ast_channel *chan, int cdr_answer)
02927 {
02928 int res = 0;
02929
02930 ast_channel_lock(chan);
02931
02932
02933 if (ast_test_flag(chan, AST_FLAG_OUTGOING)) {
02934 ast_channel_unlock(chan);
02935 return 0;
02936 }
02937
02938
02939 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
02940 ast_channel_unlock(chan);
02941 return -1;
02942 }
02943
02944 ast_channel_unlock(chan);
02945
02946 switch (chan->_state) {
02947 case AST_STATE_RINGING:
02948 case AST_STATE_RING:
02949 ast_channel_lock(chan);
02950 if (chan->tech->answer) {
02951 res = chan->tech->answer(chan);
02952 }
02953 ast_setstate(chan, AST_STATE_UP);
02954 if (cdr_answer) {
02955 ast_cdr_answer(chan->cdr);
02956 }
02957 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
02958 ast_channel_unlock(chan);
02959 break;
02960 case AST_STATE_UP:
02961 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
02962
02963
02964
02965 if (cdr_answer) {
02966 ast_cdr_answer(chan->cdr);
02967 }
02968 break;
02969 default:
02970 break;
02971 }
02972
02973 ast_indicate(chan, -1);
02974
02975 return res;
02976 }
02977
02978 int __ast_answer(struct ast_channel *chan, unsigned int delay, int cdr_answer)
02979 {
02980 int res = 0;
02981 enum ast_channel_state old_state;
02982
02983 old_state = chan->_state;
02984 if ((res = ast_raw_answer(chan, cdr_answer))) {
02985 return res;
02986 }
02987
02988 switch (old_state) {
02989 case AST_STATE_RINGING:
02990 case AST_STATE_RING:
02991
02992
02993
02994 do {
02995 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
02996 struct ast_frame *cur, *new;
02997 int timeout_ms = MAX(delay, 500);
02998 unsigned int done = 0;
02999 struct timeval start;
03000
03001 AST_LIST_HEAD_INIT_NOLOCK(&frames);
03002
03003 start = ast_tvnow();
03004 for (;;) {
03005 int ms = ast_remaining_ms(start, timeout_ms);
03006 ms = ast_waitfor(chan, ms);
03007 if (ms < 0) {
03008 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", chan->name, strerror(errno));
03009 res = -1;
03010 break;
03011 }
03012 if (ms == 0) {
03013 ast_debug(2, "Didn't receive a media frame from %s within %d ms of answering. Continuing anyway\n", chan->name, MAX(delay, 500));
03014 break;
03015 }
03016 cur = ast_read(chan);
03017 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
03018 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
03019 if (cur) {
03020 ast_frfree(cur);
03021 }
03022 res = -1;
03023 ast_debug(2, "Hangup of channel %s detected in answer routine\n", chan->name);
03024 break;
03025 }
03026
03027 if ((new = ast_frisolate(cur)) != cur) {
03028 ast_frfree(cur);
03029 }
03030
03031 AST_LIST_INSERT_HEAD(&frames, new, frame_list);
03032
03033
03034
03035
03036
03037 if (delay) {
03038 continue;
03039 }
03040
03041 switch (new->frametype) {
03042
03043 case AST_FRAME_VOICE:
03044 case AST_FRAME_VIDEO:
03045 case AST_FRAME_TEXT:
03046 case AST_FRAME_DTMF_BEGIN:
03047 case AST_FRAME_DTMF_END:
03048 case AST_FRAME_IMAGE:
03049 case AST_FRAME_HTML:
03050 case AST_FRAME_MODEM:
03051 done = 1;
03052 break;
03053 case AST_FRAME_CONTROL:
03054 case AST_FRAME_IAX:
03055 case AST_FRAME_NULL:
03056 case AST_FRAME_CNG:
03057 break;
03058 }
03059
03060 if (done) {
03061 break;
03062 }
03063 }
03064
03065 if (res == 0) {
03066 ast_channel_lock(chan);
03067 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
03068 ast_queue_frame_head(chan, cur);
03069 ast_frfree(cur);
03070 }
03071 ast_channel_unlock(chan);
03072 }
03073 } while (0);
03074 break;
03075 default:
03076 break;
03077 }
03078
03079 return res;
03080 }
03081
03082 int ast_answer(struct ast_channel *chan)
03083 {
03084 return __ast_answer(chan, 0, 1);
03085 }
03086
03087 void ast_deactivate_generator(struct ast_channel *chan)
03088 {
03089 ast_channel_lock(chan);
03090 if (chan->generatordata) {
03091 if (chan->generator && chan->generator->release)
03092 chan->generator->release(chan, chan->generatordata);
03093 chan->generatordata = NULL;
03094 chan->generator = NULL;
03095 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
03096 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
03097 ast_settimeout(chan, 0, NULL, NULL);
03098 }
03099 ast_channel_unlock(chan);
03100 }
03101
03102 static int generator_force(const void *data)
03103 {
03104
03105 void *tmp;
03106 int res;
03107 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
03108 struct ast_channel *chan = (struct ast_channel *)data;
03109
03110 ast_channel_lock(chan);
03111 tmp = chan->generatordata;
03112 chan->generatordata = NULL;
03113 if (chan->generator)
03114 generate = chan->generator->generate;
03115 ast_channel_unlock(chan);
03116
03117 if (!tmp || !generate)
03118 return 0;
03119
03120 res = generate(chan, tmp, 0, ast_format_rate(chan->writeformat & AST_FORMAT_AUDIO_MASK) / 50);
03121
03122 ast_channel_lock(chan);
03123 if (chan->generator && generate == chan->generator->generate) {
03124 chan->generatordata = tmp;
03125 }
03126 ast_channel_unlock(chan);
03127
03128 if (res) {
03129 ast_debug(1, "Auto-deactivating generator\n");
03130 ast_deactivate_generator(chan);
03131 }
03132
03133 return 0;
03134 }
03135
03136 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
03137 {
03138 int res = 0;
03139
03140 ast_channel_lock(chan);
03141 if (chan->generatordata) {
03142 if (chan->generator && chan->generator->release)
03143 chan->generator->release(chan, chan->generatordata);
03144 chan->generatordata = NULL;
03145 }
03146 if (gen->alloc && !(chan->generatordata = gen->alloc(chan, params))) {
03147 res = -1;
03148 }
03149 if (!res) {
03150 ast_settimeout(chan, 50, generator_force, chan);
03151 chan->generator = gen;
03152 }
03153 ast_channel_unlock(chan);
03154
03155 ast_prod(chan);
03156
03157 return res;
03158 }
03159
03160
03161 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
03162 {
03163 int winner = -1;
03164 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
03165 return winner;
03166 }
03167
03168
03169 #ifdef HAVE_EPOLL
03170 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
03171 int *exception, int *outfd, int *ms)
03172 #else
03173 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
03174 int *exception, int *outfd, int *ms)
03175 #endif
03176 {
03177 struct timeval start = { 0 , 0 };
03178 struct pollfd *pfds = NULL;
03179 int res;
03180 long rms;
03181 int x, y, max;
03182 int sz;
03183 struct timeval now = { 0, 0 };
03184 struct timeval whentohangup = { 0, 0 }, diff;
03185 struct ast_channel *winner = NULL;
03186 struct fdmap {
03187 int chan;
03188 int fdno;
03189 } *fdmap = NULL;
03190
03191 if (outfd)
03192 *outfd = -99999;
03193 if (exception)
03194 *exception = 0;
03195
03196 if ((sz = n * AST_MAX_FDS + nfds)) {
03197 pfds = ast_alloca(sizeof(*pfds) * sz);
03198 fdmap = ast_alloca(sizeof(*fdmap) * sz);
03199 } else {
03200
03201 return NULL;
03202 }
03203
03204
03205 for (x = 0; x < n; x++) {
03206 while (c[x]->masq) {
03207 ast_do_masquerade(c[x]);
03208 }
03209
03210 ast_channel_lock(c[x]);
03211 if (!ast_tvzero(c[x]->whentohangup)) {
03212 if (ast_tvzero(whentohangup))
03213 now = ast_tvnow();
03214 diff = ast_tvsub(c[x]->whentohangup, now);
03215 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
03216
03217 c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03218 ast_channel_unlock(c[x]);
03219 return c[x];
03220 }
03221 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
03222 whentohangup = diff;
03223 }
03224 ast_channel_unlock(c[x]);
03225 }
03226
03227 rms = *ms;
03228
03229 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
03230 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000;
03231 if (*ms >= 0 && *ms < rms) {
03232 rms = *ms;
03233 }
03234 } else if (!ast_tvzero(whentohangup) && rms < 0) {
03235
03236 rms = INT_MAX;
03237 }
03238
03239
03240
03241
03242
03243 max = 0;
03244 for (x = 0; x < n; x++) {
03245 for (y = 0; y < AST_MAX_FDS; y++) {
03246 fdmap[max].fdno = y;
03247 fdmap[max].chan = x;
03248 max += ast_add_fd(&pfds[max], c[x]->fds[y]);
03249 }
03250 CHECK_BLOCKING(c[x]);
03251 }
03252
03253 for (x = 0; x < nfds; x++) {
03254 fdmap[max].chan = -1;
03255 max += ast_add_fd(&pfds[max], fds[x]);
03256 }
03257
03258 if (*ms > 0)
03259 start = ast_tvnow();
03260
03261 if (sizeof(int) == 4) {
03262 do {
03263 int kbrms = rms;
03264 if (kbrms > 600000)
03265 kbrms = 600000;
03266 res = ast_poll(pfds, max, kbrms);
03267 if (!res)
03268 rms -= kbrms;
03269 } while (!res && (rms > 0));
03270 } else {
03271 res = ast_poll(pfds, max, rms);
03272 }
03273 for (x = 0; x < n; x++)
03274 ast_clear_flag(c[x], AST_FLAG_BLOCKING);
03275 if (res < 0) {
03276 if (errno != EINTR)
03277 *ms = -1;
03278 return NULL;
03279 }
03280 if (!ast_tvzero(whentohangup)) {
03281 now = ast_tvnow();
03282 for (x = 0; x < n; x++) {
03283 if (!ast_tvzero(c[x]->whentohangup) && ast_tvcmp(c[x]->whentohangup, now) <= 0) {
03284 c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03285 if (winner == NULL)
03286 winner = c[x];
03287 }
03288 }
03289 }
03290 if (res == 0) {
03291 *ms = 0;
03292 return winner;
03293 }
03294
03295
03296
03297
03298
03299 for (x = 0; x < max; x++) {
03300 res = pfds[x].revents;
03301 if (res == 0)
03302 continue;
03303 if (fdmap[x].chan >= 0) {
03304 winner = c[fdmap[x].chan];
03305 if (res & POLLPRI)
03306 ast_set_flag(winner, AST_FLAG_EXCEPTION);
03307 else
03308 ast_clear_flag(winner, AST_FLAG_EXCEPTION);
03309 winner->fdno = fdmap[x].fdno;
03310 } else {
03311 if (outfd)
03312 *outfd = pfds[x].fd;
03313 if (exception)
03314 *exception = (res & POLLPRI) ? -1 : 0;
03315 winner = NULL;
03316 }
03317 }
03318 if (*ms > 0) {
03319 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
03320 if (*ms < 0)
03321 *ms = 0;
03322 }
03323 return winner;
03324 }
03325
03326 #ifdef HAVE_EPOLL
03327 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
03328 {
03329 struct timeval start = { 0 , 0 };
03330 int res = 0;
03331 struct epoll_event ev[1];
03332 long diff, rms = *ms;
03333 struct ast_channel *winner = NULL;
03334 struct ast_epoll_data *aed = NULL;
03335
03336
03337
03338 while (chan->masq) {
03339 ast_do_masquerade(chan);
03340 }
03341
03342 ast_channel_lock(chan);
03343
03344 if (!ast_tvzero(chan->whentohangup)) {
03345 if ((diff = ast_tvdiff_ms(chan->whentohangup, ast_tvnow())) < 0) {
03346
03347 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03348 ast_channel_unlock(chan);
03349 return NULL;
03350 }
03351
03352 if (rms > diff)
03353 rms = diff;
03354 }
03355
03356 ast_channel_unlock(chan);
03357
03358
03359 CHECK_BLOCKING(chan);
03360
03361 if (*ms > 0)
03362 start = ast_tvnow();
03363
03364
03365 res = epoll_wait(chan->epfd, ev, 1, rms);
03366
03367
03368 ast_clear_flag(chan, AST_FLAG_BLOCKING);
03369
03370
03371 if (res < 0) {
03372 if (errno != EINTR)
03373 *ms = -1;
03374 return NULL;
03375 }
03376
03377
03378 if (!ast_tvzero(chan->whentohangup)) {
03379 if (ast_tvdiff_ms(ast_tvnow(), chan->whentohangup) >= 0) {
03380 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03381 winner = chan;
03382 }
03383 }
03384
03385
03386 if (!res) {
03387 *ms = 0;
03388 return winner;
03389 }
03390
03391
03392 aed = ev[0].data.ptr;
03393 chan->fdno = aed->which;
03394 if (ev[0].events & EPOLLPRI)
03395 ast_set_flag(chan, AST_FLAG_EXCEPTION);
03396 else
03397 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
03398
03399 if (*ms > 0) {
03400 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
03401 if (*ms < 0)
03402 *ms = 0;
03403 }
03404
03405 return chan;
03406 }
03407
03408 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
03409 {
03410 struct timeval start = { 0 , 0 };
03411 int res = 0, i;
03412 struct epoll_event ev[25] = { { 0, } };
03413 struct timeval now = { 0, 0 };
03414 long whentohangup = 0, diff = 0, rms = *ms;
03415 struct ast_channel *winner = NULL;
03416
03417 for (i = 0; i < n; i++) {
03418 while (c[i]->masq) {
03419 ast_do_masquerade(c[i]);
03420 }
03421
03422 ast_channel_lock(c[i]);
03423 if (!ast_tvzero(c[i]->whentohangup)) {
03424 if (whentohangup == 0)
03425 now = ast_tvnow();
03426 if ((diff = ast_tvdiff_ms(c[i]->whentohangup, now)) < 0) {
03427 c[i]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03428 ast_channel_unlock(c[i]);
03429 return c[i];
03430 }
03431 if (!whentohangup || whentohangup > diff)
03432 whentohangup = diff;
03433 }
03434 ast_channel_unlock(c[i]);
03435 CHECK_BLOCKING(c[i]);
03436 }
03437
03438 rms = *ms;
03439 if (whentohangup) {
03440 rms = whentohangup;
03441 if (*ms >= 0 && *ms < rms)
03442 rms = *ms;
03443 }
03444
03445 if (*ms > 0)
03446 start = ast_tvnow();
03447
03448 res = epoll_wait(c[0]->epfd, ev, 25, rms);
03449
03450 for (i = 0; i < n; i++)
03451 ast_clear_flag(c[i], AST_FLAG_BLOCKING);
03452
03453 if (res < 0) {
03454 if (errno != EINTR)
03455 *ms = -1;
03456 return NULL;
03457 }
03458
03459 if (whentohangup) {
03460 now = ast_tvnow();
03461 for (i = 0; i < n; i++) {
03462 if (!ast_tvzero(c[i]->whentohangup) && ast_tvdiff_ms(now, c[i]->whentohangup) >= 0) {
03463 c[i]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
03464 if (!winner)
03465 winner = c[i];
03466 }
03467 }
03468 }
03469
03470 if (!res) {
03471 *ms = 0;
03472 return winner;
03473 }
03474
03475 for (i = 0; i < res; i++) {
03476 struct ast_epoll_data *aed = ev[i].data.ptr;
03477
03478 if (!ev[i].events || !aed)
03479 continue;
03480
03481 winner = aed->chan;
03482 if (ev[i].events & EPOLLPRI)
03483 ast_set_flag(winner, AST_FLAG_EXCEPTION);
03484 else
03485 ast_clear_flag(winner, AST_FLAG_EXCEPTION);
03486 winner->fdno = aed->which;
03487 }
03488
03489 if (*ms > 0) {
03490 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
03491 if (*ms < 0)
03492 *ms = 0;
03493 }
03494
03495 return winner;
03496 }
03497
03498 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
03499 int *exception, int *outfd, int *ms)
03500 {
03501
03502 if (outfd)
03503 *outfd = -99999;
03504 if (exception)
03505 *exception = 0;
03506
03507
03508 if (!n || nfds || c[0]->epfd == -1)
03509 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
03510 else if (!nfds && n == 1)
03511 return ast_waitfor_nandfds_simple(c[0], ms);
03512 else
03513 return ast_waitfor_nandfds_complex(c, n, ms);
03514 }
03515 #endif
03516
03517 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
03518 {
03519 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
03520 }
03521
03522 int ast_waitfor(struct ast_channel *c, int ms)
03523 {
03524 if (ms < 0) {
03525 do {
03526 ms = 100000;
03527 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
03528 } while (!ms);
03529 } else {
03530 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
03531 }
03532 return ms;
03533 }
03534
03535 int ast_waitfordigit(struct ast_channel *c, int ms)
03536 {
03537 return ast_waitfordigit_full(c, ms, -1, -1);
03538 }
03539
03540 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
03541 {
03542 int res;
03543 unsigned int real_rate = rate, max_rate;
03544
03545 ast_channel_lock(c);
03546
03547 if (c->timingfd == -1) {
03548 ast_channel_unlock(c);
03549 return -1;
03550 }
03551
03552 if (!func) {
03553 rate = 0;
03554 data = NULL;
03555 }
03556
03557 if (rate && rate > (max_rate = ast_timer_get_max_rate(c->timer))) {
03558 real_rate = max_rate;
03559 }
03560
03561 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
03562
03563 res = ast_timer_set_rate(c->timer, real_rate);
03564
03565 c->timingfunc = func;
03566 c->timingdata = data;
03567
03568 if (func == NULL && rate == 0 && c->fdno == AST_TIMING_FD) {
03569
03570
03571
03572
03573
03574
03575 c->fdno = -1;
03576 }
03577
03578 ast_channel_unlock(c);
03579
03580 return res;
03581 }
03582
03583 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
03584 {
03585 struct timeval start = ast_tvnow();
03586 int ms;
03587
03588
03589 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
03590 return -1;
03591
03592
03593 ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
03594
03595
03596
03597
03598 while ((ms = ast_remaining_ms(start, timeout_ms))) {
03599 struct ast_channel *rchan;
03600 int outfd = -1;
03601
03602 errno = 0;
03603
03604
03605
03606 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
03607
03608 if (!rchan && outfd < 0 && ms) {
03609 if (errno == 0 || errno == EINTR)
03610 continue;
03611 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
03612 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
03613 return -1;
03614 } else if (outfd > -1) {
03615
03616 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
03617 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
03618 return 1;
03619 } else if (rchan) {
03620 int res;
03621 struct ast_frame *f = ast_read(c);
03622 if (!f)
03623 return -1;
03624
03625 switch (f->frametype) {
03626 case AST_FRAME_DTMF_BEGIN:
03627 break;
03628 case AST_FRAME_DTMF_END:
03629 res = f->subclass.integer;
03630 ast_frfree(f);
03631 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
03632 return res;
03633 case AST_FRAME_CONTROL:
03634 switch (f->subclass.integer) {
03635 case AST_CONTROL_HANGUP:
03636 ast_frfree(f);
03637 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
03638 return -1;
03639 case AST_CONTROL_RINGING:
03640 case AST_CONTROL_ANSWER:
03641 case AST_CONTROL_SRCUPDATE:
03642 case AST_CONTROL_SRCCHANGE:
03643 case AST_CONTROL_CONNECTED_LINE:
03644 case AST_CONTROL_REDIRECTING:
03645 case AST_CONTROL_UPDATE_RTP_PEER:
03646 case AST_CONTROL_HOLD:
03647 case AST_CONTROL_UNHOLD:
03648 case -1:
03649
03650 break;
03651 default:
03652 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
03653 break;
03654 }
03655 break;
03656 case AST_FRAME_VOICE:
03657
03658 if (audiofd > -1) {
03659 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
03660 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
03661 }
03662 }
03663 default:
03664
03665 break;
03666 }
03667 ast_frfree(f);
03668 }
03669 }
03670
03671 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
03672
03673 return 0;
03674 }
03675
03676 static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
03677 {
03678 ast_manager_event(chan, EVENT_FLAG_DTMF,
03679 "DTMF",
03680 "Channel: %s\r\n"
03681 "Uniqueid: %s\r\n"
03682 "Digit: %c\r\n"
03683 "Direction: %s\r\n"
03684 "Begin: %s\r\n"
03685 "End: %s\r\n",
03686 chan->name, chan->uniqueid, digit, direction, begin, end);
03687 }
03688
03689 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
03690 {
03691 if (chan->generator && chan->generator->generate && chan->generatordata && !ast_internal_timing_enabled(chan)) {
03692 void *tmp = chan->generatordata;
03693 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = chan->generator->generate;
03694 int res;
03695 int samples;
03696
03697 if (chan->timingfunc) {
03698 ast_debug(1, "Generator got voice, switching to phase locked mode\n");
03699 ast_settimeout(chan, 0, NULL, NULL);
03700 }
03701
03702 chan->generatordata = NULL;
03703
03704 if (f->subclass.codec != chan->writeformat) {
03705 float factor;
03706 factor = ((float) ast_format_rate(chan->writeformat)) / ((float) ast_format_rate(f->subclass.codec));
03707 samples = (int) ( ((float) f->samples) * factor );
03708 } else {
03709 samples = f->samples;
03710 }
03711
03712
03713
03714
03715
03716
03717
03718
03719
03720 ast_channel_unlock(chan);
03721 res = generate(chan, tmp, f->datalen, samples);
03722 ast_channel_lock(chan);
03723 chan->generatordata = tmp;
03724 if (res) {
03725 ast_debug(1, "Auto-deactivating generator\n");
03726 ast_deactivate_generator(chan);
03727 }
03728
03729 } else if (f->frametype == AST_FRAME_CNG) {
03730 if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) {
03731 ast_debug(1, "Generator got CNG, switching to timed mode\n");
03732 ast_settimeout(chan, 50, generator_force, chan);
03733 }
03734 }
03735 }
03736
03737 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
03738 {
03739 struct ast_frame *fr = &chan->dtmff;
03740
03741 fr->frametype = AST_FRAME_DTMF_END;
03742 fr->subclass.integer = f->subclass.integer;
03743 fr->len = f->len;
03744
03745
03746
03747
03748
03749 ast_queue_frame(chan, fr);
03750 }
03751
03752
03753
03754
03755 static inline int should_skip_dtmf(struct ast_channel *chan)
03756 {
03757 if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
03758
03759
03760 return 1;
03761 }
03762
03763 if (!ast_tvzero(chan->dtmf_tv) &&
03764 ast_tvdiff_ms(ast_tvnow(), chan->dtmf_tv) < AST_MIN_DTMF_GAP) {
03765
03766
03767 return 1;
03768 }
03769
03770 return 0;
03771 }
03772
03773
03774
03775
03776
03777
03778
03779
03780
03781
03782 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
03783 {
03784 int diff = sample_rate - seek_rate;
03785
03786 if (diff > 0) {
03787 samples = samples / (float) (sample_rate / seek_rate);
03788 } else if (diff < 0) {
03789 samples = samples * (float) (seek_rate / sample_rate);
03790 }
03791
03792 return samples;
03793 }
03794
03795 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
03796 {
03797 struct ast_frame *f = NULL;
03798 int blah;
03799 int prestate;
03800 int cause = 0;
03801
03802
03803
03804
03805
03806 if (chan->masq) {
03807 ast_do_masquerade(chan);
03808 return &ast_null_frame;
03809 }
03810
03811
03812 ast_channel_lock(chan);
03813
03814
03815 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
03816 if (chan->generator)
03817 ast_deactivate_generator(chan);
03818
03819
03820
03821
03822
03823
03824
03825
03826
03827
03828 if (chan->_softhangup) {
03829 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
03830 } else {
03831 goto done;
03832 }
03833 } else {
03834 #ifdef AST_DEVMODE
03835
03836
03837
03838
03839
03840
03841
03842
03843
03844
03845
03846
03847 if (chan->fdno == -1) {
03848 ast_log(LOG_ERROR,
03849 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
03850 chan->name);
03851 }
03852 #endif
03853 }
03854
03855 prestate = chan->_state;
03856 if (chan->timingfd > -1 && chan->fdno == AST_TIMING_FD) {
03857 enum ast_timer_event res;
03858
03859 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
03860
03861 res = ast_timer_get_event(chan->timer);
03862
03863 switch (res) {
03864 case AST_TIMING_EVENT_EXPIRED:
03865 if (ast_timer_ack(chan->timer, 1) < 0) {
03866 ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
03867 goto done;
03868 }
03869
03870 if (chan->timingfunc) {
03871
03872 int (*func)(const void *) = chan->timingfunc;
03873 void *data = chan->timingdata;
03874 chan->fdno = -1;
03875 ast_channel_unlock(chan);
03876 func(data);
03877 } else {
03878 ast_timer_set_rate(chan->timer, 0);
03879 chan->fdno = -1;
03880 ast_channel_unlock(chan);
03881 }
03882
03883
03884 return &ast_null_frame;
03885
03886 case AST_TIMING_EVENT_CONTINUOUS:
03887 if (AST_LIST_EMPTY(&chan->readq) ||
03888 !AST_LIST_NEXT(AST_LIST_FIRST(&chan->readq), frame_list)) {
03889 ast_timer_disable_continuous(chan->timer);
03890 }
03891 break;
03892 }
03893
03894 } else if (chan->fds[AST_GENERATOR_FD] > -1 && chan->fdno == AST_GENERATOR_FD) {
03895
03896
03897
03898 void *tmp = chan->generatordata;
03899 chan->generatordata = NULL;
03900 chan->generator->generate(chan, tmp, -1, -1);
03901 chan->generatordata = tmp;
03902 f = &ast_null_frame;
03903 chan->fdno = -1;
03904 goto done;
03905 }
03906
03907
03908
03909 if (chan->alertpipe[0] > -1) {
03910 int flags = fcntl(chan->alertpipe[0], F_GETFL);
03911
03912
03913 if ((flags & O_NONBLOCK) == 0) {
03914 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", chan->name);
03915 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
03916 ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
03917 f = &ast_null_frame;
03918 goto done;
03919 }
03920 }
03921 if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
03922 if (errno != EINTR && errno != EAGAIN)
03923 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
03924 }
03925 }
03926
03927
03928
03929 if (!AST_LIST_EMPTY(&chan->readq)) {
03930 int skip_dtmf = should_skip_dtmf(chan);
03931
03932 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->readq, f, frame_list) {
03933
03934
03935
03936
03937 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
03938 continue;
03939 }
03940
03941 AST_LIST_REMOVE_CURRENT(frame_list);
03942 break;
03943 }
03944 AST_LIST_TRAVERSE_SAFE_END;
03945
03946 if (!f) {
03947
03948 f = &ast_null_frame;
03949 if (chan->alertpipe[0] > -1) {
03950 int poke = 0;
03951
03952
03953 if (write(chan->alertpipe[1], &poke, sizeof(poke)) != sizeof(poke)) {
03954 ast_log(LOG_ERROR, "Failed to write to alertpipe: %s\n", strerror(errno));
03955 }
03956 }
03957 }
03958
03959
03960
03961 if (f->frametype == AST_FRAME_CONTROL) {
03962 switch (f->subclass.integer) {
03963 case AST_CONTROL_HANGUP:
03964 chan->_softhangup |= AST_SOFTHANGUP_DEV;
03965 cause = f->data.uint32;
03966
03967 case AST_CONTROL_END_OF_Q:
03968 ast_frfree(f);
03969 f = NULL;
03970 break;
03971 default:
03972 break;
03973 }
03974 }
03975 } else {
03976 chan->blocker = pthread_self();
03977 if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
03978 if (chan->tech->exception)
03979 f = chan->tech->exception(chan);
03980 else {
03981 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
03982 f = &ast_null_frame;
03983 }
03984
03985 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
03986 } else if (chan->tech && chan->tech->read)
03987 f = chan->tech->read(chan);
03988 else
03989 ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
03990 }
03991
03992
03993
03994
03995
03996 chan->fdno = -1;
03997
03998
03999
04000 f = ast_framehook_list_read_event(chan->framehooks, f);
04001
04002 if (f) {
04003 struct ast_frame *readq_tail = AST_LIST_LAST(&chan->readq);
04004 struct ast_control_read_action_payload *read_action_payload;
04005 struct ast_party_connected_line connected;
04006
04007
04008
04009
04010 if (AST_LIST_NEXT(f, frame_list)) {
04011 ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
04012 ast_frfree(AST_LIST_NEXT(f, frame_list));
04013 AST_LIST_NEXT(f, frame_list) = NULL;
04014 }
04015
04016 switch (f->frametype) {
04017 case AST_FRAME_CONTROL:
04018 if (f->subclass.integer == AST_CONTROL_ANSWER) {
04019 if (!ast_test_flag(chan, AST_FLAG_OUTGOING)) {
04020 ast_debug(1, "Ignoring answer on an inbound call!\n");
04021 ast_frfree(f);
04022 f = &ast_null_frame;
04023 } else if (prestate == AST_STATE_UP && ast_bridged_channel(chan)) {
04024 ast_debug(1, "Dropping duplicate answer!\n");
04025 ast_frfree(f);
04026 f = &ast_null_frame;
04027 } else {
04028
04029 ast_setstate(chan, AST_STATE_UP);
04030
04031 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
04032 }
04033 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
04034 read_action_payload = f->data.ptr;
04035 switch (read_action_payload->action) {
04036 case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
04037 ast_party_connected_line_init(&connected);
04038 ast_party_connected_line_copy(&connected, &chan->connected);
04039 if (ast_connected_line_parse_data(read_action_payload->payload,
04040 read_action_payload->payload_size, &connected)) {
04041 ast_party_connected_line_free(&connected);
04042 break;
04043 }
04044 ast_channel_unlock(chan);
04045 if (ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
04046 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
04047 read_action_payload->payload,
04048 read_action_payload->payload_size);
04049 }
04050 ast_party_connected_line_free(&connected);
04051 ast_channel_lock(chan);
04052 break;
04053 }
04054 ast_frfree(f);
04055 f = &ast_null_frame;
04056 }
04057 break;
04058 case AST_FRAME_DTMF_END:
04059 send_dtmf_event(chan, "Received", f->subclass.integer, "No", "Yes");
04060 ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, chan->name, f->len);
04061
04062 if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF) || ast_test_flag(chan, AST_FLAG_EMULATE_DTMF)) {
04063 queue_dtmf_readq(chan, f);
04064 ast_frfree(f);
04065 f = &ast_null_frame;
04066 } else if (!ast_test_flag(chan, AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
04067 if (!ast_tvzero(chan->dtmf_tv) &&
04068 ast_tvdiff_ms(ast_tvnow(), chan->dtmf_tv) < AST_MIN_DTMF_GAP) {
04069
04070 queue_dtmf_readq(chan, f);
04071 ast_frfree(f);
04072 f = &ast_null_frame;
04073 } else {
04074
04075 f->frametype = AST_FRAME_DTMF_BEGIN;
04076 ast_set_flag(chan, AST_FLAG_EMULATE_DTMF);
04077 chan->emulate_dtmf_digit = f->subclass.integer;
04078 chan->dtmf_tv = ast_tvnow();
04079 if (f->len) {
04080 if (f->len > AST_MIN_DTMF_DURATION)
04081 chan->emulate_dtmf_duration = f->len;
04082 else
04083 chan->emulate_dtmf_duration = AST_MIN_DTMF_DURATION;
04084 } else
04085 chan->emulate_dtmf_duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
04086 ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, chan->emulate_dtmf_duration, chan->name);
04087 }
04088 if (chan->audiohooks) {
04089 struct ast_frame *old_frame = f;
04090
04091
04092
04093 f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
04094 if (old_frame != f)
04095 ast_frfree(old_frame);
04096 }
04097 } else {
04098 struct timeval now = ast_tvnow();
04099 if (ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
04100 ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, chan->name);
04101 ast_clear_flag(chan, AST_FLAG_IN_DTMF);
04102 if (!f->len)
04103 f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
04104
04105
04106
04107
04108
04109
04110
04111
04112
04113 if (ast_tvdiff_ms(now, chan->dtmf_tv) < AST_MIN_DTMF_DURATION) {
04114 f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
04115 ast_log(LOG_DTMF, "DTMF end '%c' detected to have actual duration %ld on the wire, emulation will be triggered on %s\n", f->subclass.integer, f->len, chan->name);
04116 }
04117 } else if (!f->len) {
04118 ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, chan->name);
04119 f->len = AST_MIN_DTMF_DURATION;
04120 }
04121 if (f->len < AST_MIN_DTMF_DURATION && !ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY)) {
04122 ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %d, emulating on %s\n", f->subclass.integer, f->len, AST_MIN_DTMF_DURATION, chan->name);
04123 ast_set_flag(chan, AST_FLAG_EMULATE_DTMF);
04124 chan->emulate_dtmf_digit = f->subclass.integer;
04125 chan->emulate_dtmf_duration = AST_MIN_DTMF_DURATION - f->len;
04126 ast_frfree(f);
04127 f = &ast_null_frame;
04128 } else {
04129 ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, chan->name);
04130 if (f->len < AST_MIN_DTMF_DURATION) {
04131 f->len = AST_MIN_DTMF_DURATION;
04132 }
04133 chan->dtmf_tv = now;
04134 }
04135 if (chan->audiohooks) {
04136 struct ast_frame *old_frame = f;
04137 f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
04138 if (old_frame != f)
04139 ast_frfree(old_frame);
04140 }
04141 }
04142 break;
04143 case AST_FRAME_DTMF_BEGIN:
04144 send_dtmf_event(chan, "Received", f->subclass.integer, "Yes", "No");
04145 ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, chan->name);
04146 if ( ast_test_flag(chan, AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY | AST_FLAG_EMULATE_DTMF) ||
04147 (!ast_tvzero(chan->dtmf_tv) &&
04148 ast_tvdiff_ms(ast_tvnow(), chan->dtmf_tv) < AST_MIN_DTMF_GAP) ) {
04149 ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, chan->name);
04150 ast_frfree(f);
04151 f = &ast_null_frame;
04152 } else {
04153 ast_set_flag(chan, AST_FLAG_IN_DTMF);
04154 chan->dtmf_tv = ast_tvnow();
04155 ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, chan->name);
04156 }
04157 break;
04158 case AST_FRAME_NULL:
04159
04160
04161
04162
04163 if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF)) {
04164 struct timeval now = ast_tvnow();
04165 if (!chan->emulate_dtmf_duration) {
04166 ast_clear_flag(chan, AST_FLAG_EMULATE_DTMF);
04167 chan->emulate_dtmf_digit = 0;
04168 } else if (ast_tvdiff_ms(now, chan->dtmf_tv) >= chan->emulate_dtmf_duration) {
04169 chan->emulate_dtmf_duration = 0;
04170 ast_frfree(f);
04171 f = &chan->dtmff;
04172 f->frametype = AST_FRAME_DTMF_END;
04173 f->subclass.integer = chan->emulate_dtmf_digit;
04174 f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
04175 chan->dtmf_tv = now;
04176 ast_clear_flag(chan, AST_FLAG_EMULATE_DTMF);
04177 chan->emulate_dtmf_digit = 0;
04178 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, chan->name);
04179 if (chan->audiohooks) {
04180 struct ast_frame *old_frame = f;
04181 f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
04182 if (old_frame != f) {
04183 ast_frfree(old_frame);
04184 }
04185 }
04186 }
04187 }
04188 break;
04189 case AST_FRAME_VOICE:
04190
04191
04192
04193
04194 if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF) && !chan->emulate_dtmf_duration) {
04195 ast_clear_flag(chan, AST_FLAG_EMULATE_DTMF);
04196 chan->emulate_dtmf_digit = 0;
04197 }
04198
04199 if (dropaudio || ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
04200 if (dropaudio)
04201 ast_read_generator_actions(chan, f);
04202 ast_frfree(f);
04203 f = &ast_null_frame;
04204 }
04205
04206 if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF) && !ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
04207 struct timeval now = ast_tvnow();
04208 if (ast_tvdiff_ms(now, chan->dtmf_tv) >= chan->emulate_dtmf_duration) {
04209 chan->emulate_dtmf_duration = 0;
04210 ast_frfree(f);
04211 f = &chan->dtmff;
04212 f->frametype = AST_FRAME_DTMF_END;
04213 f->subclass.integer = chan->emulate_dtmf_digit;
04214 f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
04215 chan->dtmf_tv = now;
04216 if (chan->audiohooks) {
04217 struct ast_frame *old_frame = f;
04218 f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
04219 if (old_frame != f)
04220 ast_frfree(old_frame);
04221 }
04222 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, chan->name);
04223 } else {
04224
04225 ast_frfree(f);
04226 f = &ast_null_frame;
04227 }
04228 } else if ((f->frametype == AST_FRAME_VOICE) && !(f->subclass.codec & chan->nativeformats)) {
04229
04230 char to[200];
04231 ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n",
04232 chan->name, ast_getformatname(f->subclass.codec), ast_getformatname_multiple(to, sizeof(to), chan->nativeformats));
04233 ast_frfree(f);
04234 f = &ast_null_frame;
04235 } else if ((f->frametype == AST_FRAME_VOICE)) {
04236
04237 if (chan->audiohooks) {
04238 struct ast_frame *old_frame = f;
04239 f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
04240 if (old_frame != f)
04241 ast_frfree(old_frame);
04242 }
04243 if (chan->monitor && chan->monitor->read_stream ) {
04244
04245 #ifndef MONITOR_CONSTANT_DELAY
04246 int jump = chan->outsmpl - chan->insmpl - 4 * f->samples;
04247 if (jump >= 0) {
04248 jump = calc_monitor_jump((chan->outsmpl - chan->insmpl), ast_format_rate(f->subclass.codec), ast_format_rate(chan->monitor->read_stream->fmt->format));
04249 if (ast_seekstream(chan->monitor->read_stream, jump, SEEK_FORCECUR) == -1)
04250 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
04251 chan->insmpl += (chan->outsmpl - chan->insmpl) + f->samples;
04252 } else
04253 chan->insmpl+= f->samples;
04254 #else
04255 int jump = calc_monitor_jump((chan->outsmpl - chan->insmpl), ast_format_rate(f->subclass.codec), ast_format_rate(chan->monitor->read_stream->fmt->format));
04256 if (jump - MONITOR_DELAY >= 0) {
04257 if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
04258 ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
04259 chan->insmpl += chan->outsmpl - chan->insmpl;
04260 } else
04261 chan->insmpl += f->samples;
04262 #endif
04263 if (chan->monitor->state == AST_MONITOR_RUNNING) {
04264 if (ast_writestream(chan->monitor->read_stream, f) < 0)
04265 ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
04266 }
04267 }
04268
04269 if (chan->readtrans && (f = ast_translate(chan->readtrans, f, 1)) == NULL) {
04270 f = &ast_null_frame;
04271 }
04272
04273
04274
04275
04276
04277
04278
04279
04280 if (AST_LIST_NEXT(f, frame_list)) {
04281 if (!readq_tail) {
04282 ast_queue_frame_head(chan, AST_LIST_NEXT(f, frame_list));
04283 } else {
04284 __ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list), 0, readq_tail);
04285 }
04286 ast_frfree(AST_LIST_NEXT(f, frame_list));
04287 AST_LIST_NEXT(f, frame_list) = NULL;
04288 }
04289
04290
04291
04292 ast_read_generator_actions(chan, f);
04293 }
04294 break;
04295 default:
04296
04297 break;
04298 }
04299 } else {
04300
04301 if (!chan->_softhangup) {
04302 chan->_softhangup |= AST_SOFTHANGUP_DEV;
04303 }
04304 if (cause)
04305 chan->hangupcause = cause;
04306 if (chan->generator)
04307 ast_deactivate_generator(chan);
04308
04309 }
04310
04311
04312 if (chan->fin & DEBUGCHAN_FLAG)
04313 ast_frame_dump(chan->name, f, "<<");
04314 chan->fin = FRAMECOUNT_INC(chan->fin);
04315
04316 done:
04317 if (chan->music_state && chan->generator && chan->generator->digit && f && f->frametype == AST_FRAME_DTMF_END)
04318 chan->generator->digit(chan, f->subclass.integer);
04319
04320 if (chan->audiohooks && ast_audiohook_write_list_empty(chan->audiohooks)) {
04321
04322 ast_audiohook_detach_list(chan->audiohooks);
04323 chan->audiohooks = NULL;
04324 }
04325 ast_channel_unlock(chan);
04326 return f;
04327 }
04328
04329 int ast_internal_timing_enabled(struct ast_channel *chan)
04330 {
04331 return (ast_opt_internal_timing && chan->timingfd > -1);
04332 }
04333
04334 struct ast_frame *ast_read(struct ast_channel *chan)
04335 {
04336 return __ast_read(chan, 0);
04337 }
04338
04339 struct ast_frame *ast_read_noaudio(struct ast_channel *chan)
04340 {
04341 return __ast_read(chan, 1);
04342 }
04343
04344 int ast_indicate(struct ast_channel *chan, int condition)
04345 {
04346 return ast_indicate_data(chan, condition, NULL, 0);
04347 }
04348
04349 static int attribute_const is_visible_indication(enum ast_control_frame_type condition)
04350 {
04351
04352
04353
04354 switch (condition) {
04355 case AST_CONTROL_PROGRESS:
04356 case AST_CONTROL_PROCEEDING:
04357 case AST_CONTROL_VIDUPDATE:
04358 case AST_CONTROL_SRCUPDATE:
04359 case AST_CONTROL_SRCCHANGE:
04360 case AST_CONTROL_RADIO_KEY:
04361 case AST_CONTROL_RADIO_UNKEY:
04362 case AST_CONTROL_OPTION:
04363 case AST_CONTROL_WINK:
04364 case AST_CONTROL_FLASH:
04365 case AST_CONTROL_OFFHOOK:
04366 case AST_CONTROL_TAKEOFFHOOK:
04367 case AST_CONTROL_ANSWER:
04368 case AST_CONTROL_HANGUP:
04369 case AST_CONTROL_CONNECTED_LINE:
04370 case AST_CONTROL_REDIRECTING:
04371 case AST_CONTROL_TRANSFER:
04372 case AST_CONTROL_T38_PARAMETERS:
04373 case _XXX_AST_CONTROL_T38:
04374 case AST_CONTROL_CC:
04375 case AST_CONTROL_READ_ACTION:
04376 case AST_CONTROL_AOC:
04377 case AST_CONTROL_END_OF_Q:
04378 case AST_CONTROL_UPDATE_RTP_PEER:
04379 break;
04380
04381 case AST_CONTROL_INCOMPLETE:
04382 case AST_CONTROL_CONGESTION:
04383 case AST_CONTROL_BUSY:
04384 case AST_CONTROL_RINGING:
04385 case AST_CONTROL_RING:
04386 case AST_CONTROL_HOLD:
04387
04388 return 1;
04389
04390 case AST_CONTROL_UNHOLD:
04391
04392 break;
04393 }
04394
04395 return 0;
04396 }
04397
04398 int ast_indicate_data(struct ast_channel *chan, int _condition,
04399 const void *data, size_t datalen)
04400 {
04401
04402
04403 enum ast_control_frame_type condition = _condition;
04404 struct ast_tone_zone_sound *ts = NULL;
04405 int res;
04406
04407 struct ast_frame *awesome_frame = NULL;
04408
04409 ast_channel_lock(chan);
04410
04411
04412 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
04413 res = -1;
04414 goto indicate_cleanup;
04415 }
04416
04417 if (!ast_framehook_list_is_empty(chan->framehooks)) {
04418
04419 struct ast_frame frame = {
04420 .frametype = AST_FRAME_CONTROL,
04421 .subclass.integer = condition,
04422 .data.ptr = (void *) data,
04423 .datalen = datalen
04424 };
04425
04426
04427 awesome_frame = ast_frdup(&frame);
04428
04429
04430 if (!(awesome_frame = ast_framehook_list_write_event(chan->framehooks, awesome_frame))
04431 || awesome_frame->frametype != AST_FRAME_CONTROL) {
04432
04433 res = 0;
04434 goto indicate_cleanup;
04435 }
04436
04437 condition = awesome_frame->subclass.integer;
04438 data = awesome_frame->data.ptr;
04439 datalen = awesome_frame->datalen;
04440 }
04441
04442 switch (condition) {
04443 case AST_CONTROL_CONNECTED_LINE:
04444 {
04445 struct ast_party_connected_line connected;
04446
04447 ast_party_connected_line_set_init(&connected, &chan->connected);
04448 res = ast_connected_line_parse_data(data, datalen, &connected);
04449 if (!res) {
04450 ast_channel_set_connected_line(chan, &connected, NULL);
04451 }
04452 ast_party_connected_line_free(&connected);
04453 }
04454 break;
04455
04456 case AST_CONTROL_REDIRECTING:
04457 {
04458 struct ast_party_redirecting redirecting;
04459
04460 ast_party_redirecting_set_init(&redirecting, &chan->redirecting);
04461 res = ast_redirecting_parse_data(data, datalen, &redirecting);
04462 if (!res) {
04463 ast_channel_set_redirecting(chan, &redirecting, NULL);
04464 }
04465 ast_party_redirecting_free(&redirecting);
04466 }
04467 break;
04468
04469 default:
04470 break;
04471 }
04472
04473 if (is_visible_indication(condition)) {
04474
04475 chan->visible_indication = condition;
04476 } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
04477
04478 chan->visible_indication = 0;
04479 }
04480
04481 if (chan->tech->indicate) {
04482
04483 res = chan->tech->indicate(chan, condition, data, datalen);
04484 } else {
04485 res = -1;
04486 }
04487
04488 if (!res) {
04489
04490 res = 0;
04491 goto indicate_cleanup;
04492 }
04493
04494
04495
04496
04497
04498
04499
04500 if (_condition < 0) {
04501
04502 ast_playtones_stop(chan);
04503 res = 0;
04504 goto indicate_cleanup;
04505 }
04506
04507
04508 switch (condition) {
04509 case _XXX_AST_CONTROL_T38:
04510
04511 res = -1;
04512 goto indicate_cleanup;
04513 case AST_CONTROL_T38_PARAMETERS:
04514
04515
04516
04517
04518
04519
04520
04521 goto indicate_cleanup;
04522 case AST_CONTROL_RINGING:
04523 ts = ast_get_indication_tone(chan->zone, "ring");
04524
04525
04526
04527
04528
04529
04530
04531 if (chan->_state == AST_STATE_UP) {
04532 res = 0;
04533 }
04534 break;
04535 case AST_CONTROL_BUSY:
04536 ts = ast_get_indication_tone(chan->zone, "busy");
04537 break;
04538 case AST_CONTROL_INCOMPLETE:
04539 case AST_CONTROL_CONGESTION:
04540 ts = ast_get_indication_tone(chan->zone, "congestion");
04541 break;
04542 case AST_CONTROL_PROGRESS:
04543 case AST_CONTROL_PROCEEDING:
04544 case AST_CONTROL_VIDUPDATE:
04545 case AST_CONTROL_SRCUPDATE:
04546 case AST_CONTROL_SRCCHANGE:
04547 case AST_CONTROL_RADIO_KEY:
04548 case AST_CONTROL_RADIO_UNKEY:
04549 case AST_CONTROL_OPTION:
04550 case AST_CONTROL_WINK:
04551 case AST_CONTROL_FLASH:
04552 case AST_CONTROL_OFFHOOK:
04553 case AST_CONTROL_TAKEOFFHOOK:
04554 case AST_CONTROL_ANSWER:
04555 case AST_CONTROL_HANGUP:
04556 case AST_CONTROL_RING:
04557 case AST_CONTROL_HOLD:
04558 case AST_CONTROL_UNHOLD:
04559 case AST_CONTROL_TRANSFER:
04560 case AST_CONTROL_CONNECTED_LINE:
04561 case AST_CONTROL_REDIRECTING:
04562 case AST_CONTROL_CC:
04563 case AST_CONTROL_READ_ACTION:
04564 case AST_CONTROL_AOC:
04565 case AST_CONTROL_END_OF_Q:
04566 case AST_CONTROL_UPDATE_RTP_PEER:
04567
04568 res = 0;
04569 break;
04570 }
04571
04572 if (ts) {
04573
04574 ast_debug(1, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
04575 res = ast_playtones_start(chan, 0, ts->data, 1);
04576 ts = ast_tone_zone_sound_unref(ts);
04577 }
04578
04579 if (res) {
04580
04581 ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
04582 }
04583
04584 indicate_cleanup:
04585 ast_channel_unlock(chan);
04586 if (awesome_frame) {
04587 ast_frfree(awesome_frame);
04588 }
04589
04590 return res;
04591 }
04592
04593 int ast_recvchar(struct ast_channel *chan, int timeout)
04594 {
04595 int c;
04596 char *buf = ast_recvtext(chan, timeout);
04597 if (buf == NULL)
04598 return -1;
04599 c = *(unsigned char *)buf;
04600 ast_free(buf);
04601 return c;
04602 }
04603
04604 char *ast_recvtext(struct ast_channel *chan, int timeout)
04605 {
04606 int res;
04607 char *buf = NULL;
04608 struct timeval start = ast_tvnow();
04609 int ms;
04610
04611 while ((ms = ast_remaining_ms(start, timeout))) {
04612 struct ast_frame *f;
04613
04614 if (ast_check_hangup(chan)) {
04615 break;
04616 }
04617 res = ast_waitfor(chan, ms);
04618 if (res <= 0) {
04619 break;
04620 }
04621 f = ast_read(chan);
04622 if (f == NULL) {
04623 break;
04624 }
04625 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HANGUP) {
04626 ast_frfree(f);
04627 break;
04628 } else if (f->frametype == AST_FRAME_TEXT) {
04629 buf = ast_strndup((char *) f->data.ptr, f->datalen);
04630 ast_frfree(f);
04631 break;
04632 }
04633 ast_frfree(f);
04634 }
04635 return buf;
04636 }
04637
04638 int ast_sendtext(struct ast_channel *chan, const char *text)
04639 {
04640 int res = 0;
04641
04642 ast_channel_lock(chan);
04643
04644 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
04645 ast_channel_unlock(chan);
04646 return -1;
04647 }
04648 CHECK_BLOCKING(chan);
04649 if (chan->tech->send_text)
04650 res = chan->tech->send_text(chan, text);
04651 ast_clear_flag(chan, AST_FLAG_BLOCKING);
04652 ast_channel_unlock(chan);
04653 return res;
04654 }
04655
04656 int ast_senddigit_begin(struct ast_channel *chan, char digit)
04657 {
04658
04659
04660 static const char * const dtmf_tones[] = {
04661 "941+1336",
04662 "697+1209",
04663 "697+1336",
04664 "697+1477",
04665 "770+1209",
04666 "770+1336",
04667 "770+1477",
04668 "852+1209",
04669 "852+1336",
04670 "852+1477",
04671 "697+1633",
04672 "770+1633",
04673 "852+1633",
04674 "941+1633",
04675 "941+1209",
04676 "941+1477"
04677 };
04678
04679 if (!chan->tech->send_digit_begin)
04680 return 0;
04681
04682 ast_channel_lock(chan);
04683 chan->sending_dtmf_digit = digit;
04684 chan->sending_dtmf_tv = ast_tvnow();
04685 ast_channel_unlock(chan);
04686
04687 if (!chan->tech->send_digit_begin(chan, digit))
04688 return 0;
04689
04690 if (digit >= '0' && digit <='9')
04691 ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
04692 else if (digit >= 'A' && digit <= 'D')
04693 ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
04694 else if (digit == '*')
04695 ast_playtones_start(chan, 0, dtmf_tones[14], 0);
04696 else if (digit == '#')
04697 ast_playtones_start(chan, 0, dtmf_tones[15], 0);
04698 else {
04699
04700 ast_debug(1, "Unable to generate DTMF tone '%c' for '%s'\n", digit, chan->name);
04701 }
04702
04703 return 0;
04704 }
04705
04706 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
04707 {
04708 int res = -1;
04709
04710 ast_channel_lock(chan);
04711 if (chan->sending_dtmf_digit == digit) {
04712 chan->sending_dtmf_digit = 0;
04713 }
04714 ast_channel_unlock(chan);
04715
04716 if (chan->tech->send_digit_end)
04717 res = chan->tech->send_digit_end(chan, digit, duration);
04718
04719 if (res && chan->generator)
04720 ast_playtones_stop(chan);
04721
04722 return 0;
04723 }
04724
04725 int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
04726 {
04727 if (chan->tech->send_digit_begin) {
04728 ast_senddigit_begin(chan, digit);
04729 ast_safe_sleep(chan, (duration >= AST_DEFAULT_EMULATE_DTMF_DURATION ? duration : AST_DEFAULT_EMULATE_DTMF_DURATION));
04730 }
04731
04732 return ast_senddigit_end(chan, digit, (duration >= AST_DEFAULT_EMULATE_DTMF_DURATION ? duration : AST_DEFAULT_EMULATE_DTMF_DURATION));
04733 }
04734
04735 int ast_prod(struct ast_channel *chan)
04736 {
04737 struct ast_frame a = { AST_FRAME_VOICE };
04738 char nothing[128];
04739
04740
04741 if (chan->_state != AST_STATE_UP) {
04742 ast_debug(1, "Prodding channel '%s'\n", chan->name);
04743 a.subclass.codec = chan->rawwriteformat;
04744 a.data.ptr = nothing + AST_FRIENDLY_OFFSET;
04745 a.src = "ast_prod";
04746 if (ast_write(chan, &a))
04747 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
04748 }
04749 return 0;
04750 }
04751
04752 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
04753 {
04754 int res;
04755 if (!chan->tech->write_video)
04756 return 0;
04757 res = ast_write(chan, fr);
04758 if (!res)
04759 res = 1;
04760 return res;
04761 }
04762
04763 struct plc_ds {
04764
04765
04766
04767
04768 int16_t *samples_buf;
04769
04770
04771
04772 size_t num_samples;
04773 plc_state_t plc_state;
04774 };
04775
04776 static void plc_ds_destroy(void *data)
04777 {
04778 struct plc_ds *plc = data;
04779 ast_free(plc->samples_buf);
04780 ast_free(plc);
04781 }
04782
04783 static const struct ast_datastore_info plc_ds_info = {
04784 .type = "plc",
04785 .destroy = plc_ds_destroy,
04786 };
04787
04788 static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
04789 {
04790 int num_new_samples = frame->samples;
04791 struct plc_ds *plc = datastore->data;
04792
04793
04794
04795
04796
04797
04798
04799
04800
04801
04802
04803
04804
04805
04806
04807
04808
04809
04810
04811
04812 if (!num_new_samples) {
04813 return;
04814 }
04815
04816
04817
04818
04819
04820 if (plc->num_samples < num_new_samples) {
04821 ast_free(plc->samples_buf);
04822 plc->samples_buf = ast_calloc(1, (num_new_samples * sizeof(*plc->samples_buf)) + (AST_FRIENDLY_OFFSET * 2));
04823 if (!plc->samples_buf) {
04824 ast_channel_datastore_remove(chan, datastore);
04825 ast_datastore_free(datastore);
04826 return;
04827 }
04828 plc->num_samples = num_new_samples;
04829 }
04830
04831 if (frame->datalen == 0) {
04832 plc_fillin(&plc->plc_state, plc->samples_buf + AST_FRIENDLY_OFFSET, frame->samples);
04833 frame->data.ptr = plc->samples_buf + AST_FRIENDLY_OFFSET;
04834 frame->datalen = num_new_samples * 2;
04835 frame->offset = AST_FRIENDLY_OFFSET * 2;
04836 } else {
04837 plc_rx(&plc->plc_state, frame->data.ptr, frame->samples);
04838 }
04839 }
04840
04841 static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
04842 {
04843 struct ast_datastore *datastore;
04844 struct plc_ds *plc;
04845
04846 datastore = ast_channel_datastore_find(chan, &plc_ds_info, NULL);
04847 if (datastore) {
04848 plc = datastore->data;
04849 adjust_frame_for_plc(chan, frame, datastore);
04850 return;
04851 }
04852
04853 datastore = ast_datastore_alloc(&plc_ds_info, NULL);
04854 if (!datastore) {
04855 return;
04856 }
04857 plc = ast_calloc(1, sizeof(*plc));
04858 if (!plc) {
04859 ast_datastore_free(datastore);
04860 return;
04861 }
04862 datastore->data = plc;
04863 ast_channel_datastore_add(chan, datastore);
04864 adjust_frame_for_plc(chan, frame, datastore);
04865 }
04866
04867 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
04868 {
04869 int res = -1;
04870 struct ast_frame *f = NULL;
04871 int count = 0;
04872
04873
04874 while(ast_channel_trylock(chan)) {
04875
04876 if(count++ > 10) {
04877 ast_debug(1, "Deadlock avoided for write to channel '%s'\n", chan->name);
04878 return 0;
04879 }
04880 usleep(1);
04881 }
04882
04883 if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
04884 goto done;
04885
04886
04887 while (chan->masq) {
04888 ast_channel_unlock(chan);
04889 ast_do_masquerade(chan);
04890 ast_channel_lock(chan);
04891 }
04892 if (chan->masqr) {
04893 res = 0;
04894 goto done;
04895 }
04896
04897
04898
04899 if (!(fr = ast_framehook_list_write_event(chan->framehooks, fr))) {
04900 res = 0;
04901 goto done;
04902 }
04903
04904 if (chan->generatordata && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
04905 if (ast_test_flag(chan, AST_FLAG_WRITE_INT)) {
04906 ast_deactivate_generator(chan);
04907 } else {
04908 if (fr->frametype == AST_FRAME_DTMF_END) {
04909
04910
04911
04912 ast_clear_flag(chan, AST_FLAG_BLOCKING);
04913 ast_channel_unlock(chan);
04914 res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
04915 ast_channel_lock(chan);
04916 CHECK_BLOCKING(chan);
04917 } else if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_UNHOLD) {
04918
04919 res = (chan->tech->indicate == NULL) ? 0 :
04920 chan->tech->indicate(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
04921 }
04922 res = 0;
04923 goto done;
04924 }
04925 }
04926
04927 if (chan->fout & DEBUGCHAN_FLAG)
04928 ast_frame_dump(chan->name, fr, ">>");
04929 CHECK_BLOCKING(chan);
04930 switch (fr->frametype) {
04931 case AST_FRAME_CONTROL:
04932 res = (chan->tech->indicate == NULL) ? 0 :
04933 chan->tech->indicate(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
04934 break;
04935 case AST_FRAME_DTMF_BEGIN:
04936 if (chan->audiohooks) {
04937 struct ast_frame *old_frame = fr;
04938 fr = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, fr);
04939 if (old_frame != fr)
04940 f = fr;
04941 }
04942 send_dtmf_event(chan, "Sent", fr->subclass.integer, "Yes", "No");
04943 ast_clear_flag(chan, AST_FLAG_BLOCKING);
04944 ast_channel_unlock(chan);
04945 res = ast_senddigit_begin(chan, fr->subclass.integer);
04946 ast_channel_lock(chan);
04947 CHECK_BLOCKING(chan);
04948 break;
04949 case AST_FRAME_DTMF_END:
04950 if (chan->audiohooks) {
04951 struct ast_frame *new_frame = fr;
04952
04953 new_frame = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, fr);
04954 if (new_frame != fr) {
04955 ast_frfree(new_frame);
04956 }
04957 }
04958 send_dtmf_event(chan, "Sent", fr->subclass.integer, "No", "Yes");
04959 ast_clear_flag(chan, AST_FLAG_BLOCKING);
04960 ast_channel_unlock(chan);
04961 res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
04962 ast_channel_lock(chan);
04963 CHECK_BLOCKING(chan);
04964 break;
04965 case AST_FRAME_TEXT:
04966 if (fr->subclass.integer == AST_FORMAT_T140) {
04967 res = (chan->tech->write_text == NULL) ? 0 :
04968 chan->tech->write_text(chan, fr);
04969 } else {
04970 res = (chan->tech->send_text == NULL) ? 0 :
04971 chan->tech->send_text(chan, (char *) fr->data.ptr);
04972 }
04973 break;
04974 case AST_FRAME_HTML:
04975 res = (chan->tech->send_html == NULL) ? 0 :
04976 chan->tech->send_html(chan, fr->subclass.integer, (char *) fr->data.ptr, fr->datalen);
04977 break;
04978 case AST_FRAME_VIDEO:
04979
04980 res = (chan->tech->write_video == NULL) ? 0 :
04981 chan->tech->write_video(chan, fr);
04982 break;
04983 case AST_FRAME_MODEM:
04984 res = (chan->tech->write == NULL) ? 0 :
04985 chan->tech->write(chan, fr);
04986 break;
04987 case AST_FRAME_VOICE:
04988 if (chan->tech->write == NULL)
04989 break;
04990
04991 if (ast_opt_generic_plc && fr->subclass.codec == AST_FORMAT_SLINEAR) {
04992 apply_plc(chan, fr);
04993 }
04994
04995
04996 if (fr->subclass.codec == chan->rawwriteformat) {
04997 f = fr;
04998 } else {
04999 if ((!(fr->subclass.codec & chan->nativeformats)) && (chan->writeformat != fr->subclass.codec)) {
05000 char nf[512];
05001
05002
05003
05004
05005
05006
05007
05008
05009
05010
05011 ast_log(LOG_WARNING, "Codec mismatch on channel %s setting write format to %s from %s native formats %s\n",
05012 chan->name, ast_getformatname(fr->subclass.codec), ast_getformatname(chan->writeformat),
05013 ast_getformatname_multiple(nf, sizeof(nf), chan->nativeformats & AST_FORMAT_AUDIO_MASK));
05014 ast_set_write_format(chan, fr->subclass.codec);
05015 }
05016
05017 f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
05018 }
05019
05020 if (!f) {
05021 res = 0;
05022 break;
05023 }
05024
05025 if (chan->audiohooks) {
05026 struct ast_frame *prev = NULL, *new_frame, *cur, *dup;
05027 int freeoldlist = 0;
05028
05029 if (f != fr) {
05030 freeoldlist = 1;
05031 }
05032
05033
05034
05035
05036 for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
05037 new_frame = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, cur);
05038
05039
05040
05041 if (new_frame != cur) {
05042
05043
05044
05045
05046 if ((dup = ast_frisolate(new_frame))) {
05047 AST_LIST_NEXT(dup, frame_list) = AST_LIST_NEXT(cur, frame_list);
05048 if (freeoldlist) {
05049 AST_LIST_NEXT(cur, frame_list) = NULL;
05050 ast_frfree(cur);
05051 }
05052 if (new_frame != dup) {
05053 ast_frfree(new_frame);
05054 }
05055 cur = dup;
05056 }
05057 }
05058
05059
05060
05061 if (prev) {
05062 AST_LIST_NEXT(prev, frame_list) = cur;
05063 } else {
05064 f = cur;
05065 }
05066 prev = cur;
05067 }
05068 }
05069
05070
05071
05072
05073
05074 if (chan->monitor && chan->monitor->write_stream) {
05075 struct ast_frame *cur;
05076
05077 for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
05078
05079 #ifndef MONITOR_CONSTANT_DELAY
05080 int jump = chan->insmpl - chan->outsmpl - 4 * cur->samples;
05081 if (jump >= 0) {
05082 jump = calc_monitor_jump((chan->insmpl - chan->outsmpl), ast_format_rate(f->subclass.codec), ast_format_rate(chan->monitor->read_stream->fmt->format));
05083 if (ast_seekstream(chan->monitor->write_stream, jump, SEEK_FORCECUR) == -1)
05084 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
05085 chan->outsmpl += (chan->insmpl - chan->outsmpl) + cur->samples;
05086 } else {
05087 chan->outsmpl += cur->samples;
05088 }
05089 #else
05090 int jump = calc_monitor_jump((chan->insmpl - chan->outsmpl), ast_format_rate(f->subclass.codec), ast_format_rate(chan->monitor->read_stream->fmt->format));
05091 if (jump - MONITOR_DELAY >= 0) {
05092 if (ast_seekstream(chan->monitor->write_stream, jump - cur->samples, SEEK_FORCECUR) == -1)
05093 ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
05094 chan->outsmpl += chan->insmpl - chan->outsmpl;
05095 } else {
05096 chan->outsmpl += cur->samples;
05097 }
05098 #endif
05099 if (chan->monitor->state == AST_MONITOR_RUNNING) {
05100 if (ast_writestream(chan->monitor->write_stream, cur) < 0)
05101 ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
05102 }
05103 }
05104 }
05105
05106
05107
05108
05109 if ((f != fr) && AST_LIST_NEXT(f, frame_list)) {
05110 struct ast_frame *cur, *next;
05111 unsigned int skip = 0;
05112
05113 for (cur = f, next = AST_LIST_NEXT(cur, frame_list);
05114 cur;
05115 cur = next, next = cur ? AST_LIST_NEXT(cur, frame_list) : NULL) {
05116 if (!skip) {
05117 if ((res = chan->tech->write(chan, cur)) < 0) {
05118 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05119 skip = 1;
05120 } else if (next) {
05121
05122
05123
05124 chan->fout = FRAMECOUNT_INC(chan->fout);
05125 }
05126 }
05127 ast_frfree(cur);
05128 }
05129
05130
05131 f = NULL;
05132 } else {
05133 res = chan->tech->write(chan, f);
05134 }
05135 break;
05136 case AST_FRAME_NULL:
05137 case AST_FRAME_IAX:
05138
05139 res = 0;
05140 break;
05141 default:
05142
05143
05144
05145 res = chan->tech->write(chan, fr);
05146 break;
05147 }
05148
05149 if (f && f != fr)
05150 ast_frfree(f);
05151 ast_clear_flag(chan, AST_FLAG_BLOCKING);
05152
05153
05154 if (res < 0) {
05155 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05156 } else {
05157 chan->fout = FRAMECOUNT_INC(chan->fout);
05158 }
05159 done:
05160 if (chan->audiohooks && ast_audiohook_write_list_empty(chan->audiohooks)) {
05161
05162 ast_audiohook_detach_list(chan->audiohooks);
05163 chan->audiohooks = NULL;
05164 }
05165 ast_channel_unlock(chan);
05166 return res;
05167 }
05168
05169 static int set_format(struct ast_channel *chan, format_t fmt, format_t *rawformat, format_t *format,
05170 struct ast_trans_pvt **trans, const int direction)
05171 {
05172 format_t native, native_fmt = ast_best_codec(fmt);
05173 int res;
05174 char from[200], to[200];
05175
05176
05177 fmt &= AST_FORMAT_AUDIO_MASK;
05178
05179 native = chan->nativeformats;
05180
05181 if (!fmt || !native)
05182 return 0;
05183
05184
05185 if (!ast_channel_setoption(chan, direction ? AST_OPTION_FORMAT_WRITE : AST_OPTION_FORMAT_READ, &native_fmt, sizeof(int*), 0)) {
05186 ast_debug(1, "Channel driver natively set channel %s to %s format %s\n", chan->name,
05187 direction ? "write" : "read", ast_getformatname(native_fmt));
05188 chan->nativeformats = *rawformat = *format = native_fmt;
05189 if (*trans) {
05190 ast_translator_free_path(*trans);
05191 }
05192 *trans = NULL;
05193 return 0;
05194 }
05195
05196
05197 if (!direction)
05198
05199 res = ast_translator_best_choice(&fmt, &native);
05200 else
05201
05202 res = ast_translator_best_choice(&native, &fmt);
05203
05204 if (res < 0) {
05205 ast_log(LOG_WARNING, "Unable to find a codec translation path from %s to %s\n",
05206 ast_getformatname_multiple(from, sizeof(from), native),
05207 ast_getformatname_multiple(to, sizeof(to), fmt));
05208 return -1;
05209 }
05210
05211
05212 ast_channel_lock(chan);
05213
05214 if ((*rawformat == native) && (*format == fmt) && ((*rawformat == *format) || (*trans))) {
05215
05216 ast_channel_unlock(chan);
05217 return 0;
05218 }
05219
05220 *rawformat = native;
05221
05222 *format = fmt;
05223
05224 if (*trans) {
05225 ast_translator_free_path(*trans);
05226 *trans = NULL;
05227 }
05228
05229 if (*format == *rawformat) {
05230
05231
05232
05233
05234
05235 res = 0;
05236 } else {
05237 if (!direction) {
05238
05239 *trans = ast_translator_build_path(*format, *rawformat);
05240 } else {
05241
05242 *trans = ast_translator_build_path(*rawformat, *format);
05243 }
05244 res = *trans ? 0 : -1;
05245 }
05246 ast_channel_unlock(chan);
05247 ast_debug(1, "Set channel %s to %s format %s\n", chan->name,
05248 direction ? "write" : "read", ast_getformatname(fmt));
05249 return res;
05250 }
05251
05252 int ast_set_read_format(struct ast_channel *chan, format_t fmt)
05253 {
05254 return set_format(chan, fmt, &chan->rawreadformat, &chan->readformat,
05255 &chan->readtrans, 0);
05256 }
05257
05258 int ast_set_write_format(struct ast_channel *chan, format_t fmt)
05259 {
05260 return set_format(chan, fmt, &chan->rawwriteformat, &chan->writeformat,
05261 &chan->writetrans, 1);
05262 }
05263
05264 const char *ast_channel_reason2str(int reason)
05265 {
05266 switch (reason)
05267 {
05268 case 0:
05269 return "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)";
05270 case AST_CONTROL_HANGUP:
05271 return "Hangup";
05272 case AST_CONTROL_RING:
05273 return "Local Ring";
05274 case AST_CONTROL_RINGING:
05275 return "Remote end Ringing";
05276 case AST_CONTROL_ANSWER:
05277 return "Remote end has Answered";
05278 case AST_CONTROL_BUSY:
05279 return "Remote end is Busy";
05280 case AST_CONTROL_CONGESTION:
05281 return "Congestion (circuits busy)";
05282 default:
05283 return "Unknown Reason!!";
05284 }
05285 }
05286
05287 static void handle_cause(int cause, int *outstate)
05288 {
05289 if (outstate) {
05290
05291 if (cause == AST_CAUSE_BUSY)
05292 *outstate = AST_CONTROL_BUSY;
05293 else if (cause == AST_CAUSE_CONGESTION)
05294 *outstate = AST_CONTROL_CONGESTION;
05295 else
05296 *outstate = 0;
05297 }
05298 }
05299
05300
05301
05302
05303
05304
05305
05306
05307
05308
05309
05310 static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
05311 {
05312 if (!ast_test_flag(parent, AST_FLAG_ZOMBIE) && !ast_check_hangup(parent)) {
05313 struct ast_party_redirecting redirecting;
05314
05315
05316
05317
05318
05319 ast_party_redirecting_init(&redirecting);
05320 ast_channel_lock(orig);
05321 ast_party_redirecting_copy(&redirecting, &orig->redirecting);
05322 ast_channel_unlock(orig);
05323 if (ast_channel_redirecting_macro(orig, parent, &redirecting, 1, 0)) {
05324 ast_channel_update_redirecting(parent, &redirecting, NULL);
05325 }
05326 ast_party_redirecting_free(&redirecting);
05327 }
05328
05329
05330 ast_channel_lock_both(parent, new_chan);
05331 ast_channel_inherit_variables(parent, new_chan);
05332 ast_channel_datastore_inherit(parent, new_chan);
05333 ast_channel_unlock(new_chan);
05334 ast_channel_unlock(parent);
05335 }
05336
05337 struct ast_channel *ast_call_forward(struct ast_channel *caller, struct ast_channel *orig, int *timeout, format_t format, struct outgoing_helper *oh, int *outstate)
05338 {
05339 char tmpchan[256];
05340 struct ast_channel *new_chan = NULL;
05341 char *data, *type;
05342 int cause = 0;
05343 int res;
05344
05345
05346 ast_copy_string(tmpchan, orig->call_forward, sizeof(tmpchan));
05347 if ((data = strchr(tmpchan, '/'))) {
05348 *data++ = '\0';
05349 type = tmpchan;
05350 } else {
05351 const char *forward_context;
05352 ast_channel_lock(orig);
05353 forward_context = pbx_builtin_getvar_helper(orig, "FORWARD_CONTEXT");
05354 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", orig->call_forward, S_OR(forward_context, orig->context));
05355 ast_channel_unlock(orig);
05356 data = tmpchan;
05357 type = "Local";
05358 }
05359 if (!(new_chan = ast_request(type, format, orig, data, &cause))) {
05360 ast_log(LOG_NOTICE, "Unable to create channel for call forward to '%s/%s' (cause = %d)\n", type, data, cause);
05361 handle_cause(cause, outstate);
05362 ast_hangup(orig);
05363 return NULL;
05364 }
05365
05366
05367 if (oh) {
05368 if (oh->vars) {
05369 ast_set_variables(new_chan, oh->vars);
05370 }
05371 if (oh->parent_channel) {
05372 call_forward_inherit(new_chan, oh->parent_channel, orig);
05373 }
05374 if (oh->account) {
05375 ast_channel_lock(new_chan);
05376 ast_cdr_setaccount(new_chan, oh->account);
05377 ast_channel_unlock(new_chan);
05378 }
05379 } else if (caller) {
05380 call_forward_inherit(new_chan, caller, orig);
05381 }
05382
05383 ast_channel_lock_both(orig, new_chan);
05384 ast_copy_flags(new_chan->cdr, orig->cdr, AST_CDR_FLAG_ORIGINATED);
05385 ast_string_field_set(new_chan, accountcode, orig->accountcode);
05386 ast_party_connected_line_copy(&new_chan->connected, &orig->connected);
05387 ast_party_redirecting_copy(&new_chan->redirecting, &orig->redirecting);
05388 ast_channel_unlock(new_chan);
05389 ast_channel_unlock(orig);
05390
05391
05392 res = ast_call(new_chan, data, 0);
05393 if (timeout) {
05394 *timeout = res;
05395 }
05396 if (res) {
05397 ast_log(LOG_NOTICE, "Unable to call forward to channel %s/%s\n", type, (char *)data);
05398 ast_hangup(orig);
05399 ast_hangup(new_chan);
05400 return NULL;
05401 }
05402 ast_hangup(orig);
05403
05404 return new_chan;
05405 }
05406
05407 struct ast_channel *__ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
05408 {
05409 int dummy_outstate;
05410 int cause = 0;
05411 struct ast_channel *chan;
05412 int res = 0;
05413 int last_subclass = 0;
05414 struct ast_party_connected_line connected;
05415
05416 if (outstate)
05417 *outstate = 0;
05418 else
05419 outstate = &dummy_outstate;
05420
05421 chan = ast_request(type, format, requestor, data, &cause);
05422 if (!chan) {
05423 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
05424 handle_cause(cause, outstate);
05425 return NULL;
05426 }
05427
05428 if (oh) {
05429 if (oh->vars) {
05430 ast_set_variables(chan, oh->vars);
05431 }
05432 if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) {
05433
05434
05435
05436
05437 cid_num = oh->cid_num;
05438 cid_name = oh->cid_name;
05439 }
05440 if (oh->parent_channel) {
05441
05442 ast_channel_lock_both(oh->parent_channel, chan);
05443 ast_channel_inherit_variables(oh->parent_channel, chan);
05444 ast_channel_datastore_inherit(oh->parent_channel, chan);
05445 ast_channel_unlock(oh->parent_channel);
05446 ast_channel_unlock(chan);
05447 }
05448 if (oh->account) {
05449 ast_channel_lock(chan);
05450 ast_cdr_setaccount(chan, oh->account);
05451 ast_channel_unlock(chan);
05452 }
05453 }
05454
05455
05456
05457
05458
05459
05460
05461
05462
05463 ast_set_callerid(chan, cid_num, cid_name, cid_num);
05464
05465 ast_set_flag(chan->cdr, AST_CDR_FLAG_ORIGINATED);
05466 ast_party_connected_line_set_init(&connected, &chan->connected);
05467 if (cid_num) {
05468 connected.id.number.valid = 1;
05469 connected.id.number.str = (char *) cid_num;
05470 connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
05471 }
05472 if (cid_name) {
05473 connected.id.name.valid = 1;
05474 connected.id.name.str = (char *) cid_name;
05475 connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
05476 }
05477 ast_channel_set_connected_line(chan, &connected, NULL);
05478
05479 if (ast_call(chan, data, 0)) {
05480 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
05481 } else {
05482 struct timeval start = ast_tvnow();
05483 res = 1;
05484 while (timeout && chan->_state != AST_STATE_UP) {
05485 struct ast_frame *f;
05486 int ms = ast_remaining_ms(start, timeout);
05487
05488 res = ast_waitfor(chan, ms);
05489 if (res == 0) {
05490 *outstate = AST_CONTROL_RINGING;
05491 break;
05492 }
05493 if (res < 0)
05494 break;
05495 if (!ast_strlen_zero(chan->call_forward)) {
05496 if (!(chan = ast_call_forward(NULL, chan, NULL, format, oh, outstate))) {
05497 return NULL;
05498 }
05499 continue;
05500 }
05501
05502 f = ast_read(chan);
05503 if (!f) {
05504 *outstate = AST_CONTROL_HANGUP;
05505 res = 0;
05506 break;
05507 }
05508 if (f->frametype == AST_FRAME_CONTROL) {
05509 switch (f->subclass.integer) {
05510 case AST_CONTROL_RINGING:
05511 *outstate = f->subclass.integer;
05512 break;
05513
05514 case AST_CONTROL_BUSY:
05515 ast_cdr_busy(chan->cdr);
05516 *outstate = f->subclass.integer;
05517 timeout = 0;
05518 break;
05519
05520 case AST_CONTROL_INCOMPLETE:
05521 ast_cdr_failed(chan->cdr);
05522 *outstate = AST_CONTROL_CONGESTION;
05523 timeout = 0;
05524 break;
05525
05526 case AST_CONTROL_CONGESTION:
05527 ast_cdr_failed(chan->cdr);
05528 *outstate = f->subclass.integer;
05529 timeout = 0;
05530 break;
05531
05532 case AST_CONTROL_ANSWER:
05533 ast_cdr_answer(chan->cdr);
05534 *outstate = f->subclass.integer;
05535 timeout = 0;
05536 break;
05537
05538
05539 case AST_CONTROL_PROGRESS:
05540 case AST_CONTROL_PROCEEDING:
05541 case AST_CONTROL_HOLD:
05542 case AST_CONTROL_UNHOLD:
05543 case AST_CONTROL_VIDUPDATE:
05544 case AST_CONTROL_SRCUPDATE:
05545 case AST_CONTROL_SRCCHANGE:
05546 case AST_CONTROL_CONNECTED_LINE:
05547 case AST_CONTROL_REDIRECTING:
05548 case AST_CONTROL_CC:
05549 case -1:
05550 break;
05551
05552 default:
05553 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass.integer);
05554 }
05555 last_subclass = f->subclass.integer;
05556 }
05557 ast_frfree(f);
05558 }
05559 }
05560
05561
05562 if (oh) {
05563 if (!ast_strlen_zero(oh->context))
05564 ast_copy_string(chan->context, oh->context, sizeof(chan->context));
05565 if (!ast_strlen_zero(oh->exten))
05566 ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten));
05567 if (oh->priority)
05568 chan->priority = oh->priority;
05569 }
05570 if (chan->_state == AST_STATE_UP)
05571 *outstate = AST_CONTROL_ANSWER;
05572
05573 if (res <= 0) {
05574 ast_channel_lock(chan);
05575 if (AST_CONTROL_RINGING == last_subclass) {
05576 chan->hangupcause = AST_CAUSE_NO_ANSWER;
05577 }
05578 if (!chan->cdr && (chan->cdr = ast_cdr_alloc())) {
05579 ast_cdr_init(chan->cdr, chan);
05580 }
05581 if (chan->cdr) {
05582 char tmp[256];
05583
05584 snprintf(tmp, sizeof(tmp), "%s/%s", type, (char *)data);
05585 ast_cdr_setapp(chan->cdr, "Dial", tmp);
05586 ast_cdr_update(chan);
05587 ast_cdr_start(chan->cdr);
05588 ast_cdr_end(chan->cdr);
05589
05590 if (ast_cdr_disposition(chan->cdr, chan->hangupcause)) {
05591 ast_cdr_failed(chan->cdr);
05592 }
05593 }
05594 ast_channel_unlock(chan);
05595 ast_hangup(chan);
05596 chan = NULL;
05597 }
05598 return chan;
05599 }
05600
05601 struct ast_channel *ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname)
05602 {
05603 return __ast_request_and_dial(type, format, requestor, data, timeout, outstate, cidnum, cidname, NULL);
05604 }
05605
05606 static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
05607 {
05608 int ops[2][2] = {
05609 {AST_OPTION_SECURE_SIGNALING, 0},
05610 {AST_OPTION_SECURE_MEDIA, 0},
05611 };
05612 int i;
05613 struct ast_channel *r = (struct ast_channel *) requestor;
05614 struct ast_datastore *ds;
05615
05616 if (!requestor || !out) {
05617 return 0;
05618 }
05619
05620 ast_channel_lock(r);
05621 if ((ds = ast_channel_datastore_find(r, &secure_call_info, NULL))) {
05622 struct ast_secure_call_store *encrypt = ds->data;
05623 ops[0][1] = encrypt->signaling;
05624 ops[1][1] = encrypt->media;
05625 } else {
05626 ast_channel_unlock(r);
05627 return 0;
05628 }
05629 ast_channel_unlock(r);
05630
05631 for (i = 0; i < 2; i++) {
05632 if (ops[i][1]) {
05633 if (ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0)) {
05634
05635 return -1;
05636 }
05637 } else {
05638
05639 ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0);
05640 }
05641 }
05642
05643 return 0;
05644 }
05645
05646 struct ast_channel *ast_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
05647 {
05648 struct chanlist *chan;
05649 struct ast_channel *c;
05650 format_t capabilities;
05651 format_t fmt;
05652 int res;
05653 int foo;
05654 format_t videoformat = format & AST_FORMAT_VIDEO_MASK;
05655 format_t textformat = format & AST_FORMAT_TEXT_MASK;
05656
05657 if (!cause)
05658 cause = &foo;
05659 *cause = AST_CAUSE_NOTDEFINED;
05660
05661 if (AST_RWLIST_RDLOCK(&backends)) {
05662 ast_log(LOG_WARNING, "Unable to lock technology backend list\n");
05663 return NULL;
05664 }
05665
05666 AST_RWLIST_TRAVERSE(&backends, chan, list) {
05667 if (strcasecmp(type, chan->tech->type))
05668 continue;
05669
05670 capabilities = chan->tech->capabilities;
05671 fmt = format & AST_FORMAT_AUDIO_MASK;
05672 if (fmt) {
05673
05674
05675
05676 res = ast_translator_best_choice(&fmt, &capabilities);
05677 if (res < 0) {
05678 char tmp1[256], tmp2[256];
05679 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %s) to %s\n", type,
05680 ast_getformatname_multiple(tmp1, sizeof(tmp1), chan->tech->capabilities),
05681 ast_getformatname_multiple(tmp2, sizeof(tmp2), format));
05682 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05683 AST_RWLIST_UNLOCK(&backends);
05684 return NULL;
05685 }
05686 }
05687 AST_RWLIST_UNLOCK(&backends);
05688 if (!chan->tech->requester)
05689 return NULL;
05690
05691 if (!(c = chan->tech->requester(type, capabilities | videoformat | textformat, requestor, data, cause)))
05692 return NULL;
05693
05694 if (set_security_requirements(requestor, c)) {
05695 ast_log(LOG_WARNING, "Setting security requirements failed\n");
05696 c = ast_channel_release(c);
05697 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05698 return NULL;
05699 }
05700
05701
05702 return c;
05703 }
05704
05705 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
05706 *cause = AST_CAUSE_NOSUCHDRIVER;
05707 AST_RWLIST_UNLOCK(&backends);
05708
05709 return NULL;
05710 }
05711
05712 int ast_call(struct ast_channel *chan, char *addr, int timeout)
05713 {
05714
05715
05716
05717 int res = -1;
05718
05719 ast_channel_lock(chan);
05720 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
05721 if (chan->cdr) {
05722 ast_set_flag(chan->cdr, AST_CDR_FLAG_DIALED);
05723 }
05724 if (chan->tech->call)
05725 res = chan->tech->call(chan, addr, timeout);
05726 ast_set_flag(chan, AST_FLAG_OUTGOING);
05727 }
05728 ast_channel_unlock(chan);
05729 return res;
05730 }
05731
05732
05733
05734
05735
05736
05737
05738
05739 int ast_transfer(struct ast_channel *chan, char *dest)
05740 {
05741 int res = -1;
05742
05743
05744 ast_channel_lock(chan);
05745 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
05746 if (chan->tech->transfer) {
05747 res = chan->tech->transfer(chan, dest);
05748 if (!res)
05749 res = 1;
05750 } else
05751 res = 0;
05752 }
05753 ast_channel_unlock(chan);
05754
05755 if (res <= 0) {
05756 return res;
05757 }
05758
05759 for (;;) {
05760 struct ast_frame *fr;
05761
05762 res = ast_waitfor(chan, -1);
05763
05764 if (res < 0 || !(fr = ast_read(chan))) {
05765 res = -1;
05766 break;
05767 }
05768
05769 if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_TRANSFER) {
05770 enum ast_control_transfer *message = fr->data.ptr;
05771
05772 if (*message == AST_TRANSFER_SUCCESS) {
05773 res = 1;
05774 } else {
05775 res = -1;
05776 }
05777
05778 ast_frfree(fr);
05779 break;
05780 }
05781
05782 ast_frfree(fr);
05783 }
05784
05785 return res;
05786 }
05787
05788 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
05789 {
05790 return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1);
05791 }
05792
05793 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
05794 {
05795 int pos = 0;
05796 int to = ftimeout;
05797
05798 struct ast_silence_generator *silgen = NULL;
05799
05800
05801 if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
05802 return -1;
05803 if (!len)
05804 return -1;
05805 for (;;) {
05806 int d;
05807 if (c->stream) {
05808 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
05809 ast_stopstream(c);
05810 if (!silgen && ast_opt_transmit_silence)
05811 silgen = ast_channel_start_silence_generator(c);
05812 usleep(1000);
05813 if (!d)
05814 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
05815 } else {
05816 if (!silgen && ast_opt_transmit_silence)
05817 silgen = ast_channel_start_silence_generator(c);
05818 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
05819 }
05820 if (d < 0) {
05821 ast_channel_stop_silence_generator(c, silgen);
05822 return AST_GETDATA_FAILED;
05823 }
05824 if (d == 0) {
05825 s[pos] = '\0';
05826 ast_channel_stop_silence_generator(c, silgen);
05827 return AST_GETDATA_TIMEOUT;
05828 }
05829 if (d == 1) {
05830 s[pos] = '\0';
05831 ast_channel_stop_silence_generator(c, silgen);
05832 return AST_GETDATA_INTERRUPTED;
05833 }
05834 if (strchr(enders, d) && (pos == 0)) {
05835 s[pos] = '\0';
05836 ast_channel_stop_silence_generator(c, silgen);
05837 return AST_GETDATA_EMPTY_END_TERMINATED;
05838 }
05839 if (!strchr(enders, d)) {
05840 s[pos++] = d;
05841 }
05842 if (strchr(enders, d) || (pos >= len)) {
05843 s[pos] = '\0';
05844 ast_channel_stop_silence_generator(c, silgen);
05845 return AST_GETDATA_COMPLETE;
05846 }
05847 to = timeout;
05848 }
05849
05850 return 0;
05851 }
05852
05853 int ast_channel_supports_html(struct ast_channel *chan)
05854 {
05855 return (chan->tech->send_html) ? 1 : 0;
05856 }
05857
05858 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
05859 {
05860 if (chan->tech->send_html)
05861 return chan->tech->send_html(chan, subclass, data, datalen);
05862 return -1;
05863 }
05864
05865 int ast_channel_sendurl(struct ast_channel *chan, const char *url)
05866 {
05867 return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1);
05868 }
05869
05870
05871 static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
05872 {
05873 format_t src, dst;
05874 int use_slin;
05875
05876
05877 if (from->tech->bridge && from->tech->bridge == to->tech->bridge &&
05878 !ast_channel_setoption(from, AST_OPTION_MAKE_COMPATIBLE, to, sizeof(struct ast_channel *), 0)) {
05879 return 0;
05880 }
05881
05882 if (from->readformat == to->writeformat && from->writeformat == to->readformat) {
05883
05884 return 0;
05885 }
05886
05887
05888 src = from->nativeformats;
05889 dst = to->nativeformats;
05890
05891
05892 if ((src & AST_FORMAT_AUDIO_MASK) == 0 || (dst & AST_FORMAT_AUDIO_MASK) == 0)
05893 return 0;
05894
05895 if (ast_translator_best_choice(&dst, &src) < 0) {
05896 ast_log(LOG_WARNING, "No path to translate from %s to %s\n", from->name, to->name);
05897 return -1;
05898 }
05899
05900
05901
05902
05903
05904
05905
05906 use_slin = (src == AST_FORMAT_SLINEAR || dst == AST_FORMAT_SLINEAR);
05907 if ((src != dst) && (ast_opt_generic_plc || ast_opt_transcode_via_slin) &&
05908 (ast_translate_path_steps(dst, src) != 1 || use_slin))
05909 dst = AST_FORMAT_SLINEAR;
05910 if (ast_set_read_format(from, dst) < 0) {
05911 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %s\n", from->name, ast_getformatname(dst));
05912 return -1;
05913 }
05914 if (ast_set_write_format(to, dst) < 0) {
05915 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %s\n", to->name, ast_getformatname(dst));
05916 return -1;
05917 }
05918 return 0;
05919 }
05920
05921 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
05922 {
05923
05924 int rc = 0;
05925
05926
05927 rc = ast_channel_make_compatible_helper(chan, peer);
05928
05929 if (rc < 0)
05930 return rc;
05931
05932
05933 rc = ast_channel_make_compatible_helper(peer, chan);
05934
05935 return rc;
05936 }
05937
05938 static int __ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clonechan, struct ast_datastore *xfer_ds)
05939 {
05940 int res = -1;
05941 struct ast_channel *final_orig, *final_clone, *base;
05942
05943 for (;;) {
05944 final_orig = original;
05945 final_clone = clonechan;
05946
05947 ast_channel_lock_both(original, clonechan);
05948
05949 if (ast_test_flag(original, AST_FLAG_ZOMBIE)
05950 || ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
05951
05952 ast_log(LOG_WARNING,
05953 "Can't setup masquerade. One or both channels is dead. (%s <-- %s)\n",
05954 original->name, clonechan->name);
05955 ast_channel_unlock(clonechan);
05956 ast_channel_unlock(original);
05957 return -1;
05958 }
05959
05960
05961
05962
05963
05964
05965 if (original->_bridge
05966 && (original->_bridge != ast_bridged_channel(original))
05967 && (original->_bridge->_bridge != original)) {
05968 final_orig = original->_bridge;
05969 }
05970 if (clonechan->_bridge
05971 && (clonechan->_bridge != ast_bridged_channel(clonechan))
05972 && (clonechan->_bridge->_bridge != clonechan)) {
05973 final_clone = clonechan->_bridge;
05974 }
05975 if (final_clone->tech->get_base_channel
05976 && (base = final_clone->tech->get_base_channel(final_clone))) {
05977 final_clone = base;
05978 }
05979
05980 if ((final_orig != original) || (final_clone != clonechan)) {
05981
05982
05983
05984
05985
05986 if (ast_channel_trylock(final_orig)) {
05987 ast_channel_unlock(clonechan);
05988 ast_channel_unlock(original);
05989
05990
05991 continue;
05992 }
05993 if (ast_channel_trylock(final_clone)) {
05994 ast_channel_unlock(final_orig);
05995 ast_channel_unlock(clonechan);
05996 ast_channel_unlock(original);
05997
05998
05999 continue;
06000 }
06001 ast_channel_unlock(clonechan);
06002 ast_channel_unlock(original);
06003 original = final_orig;
06004 clonechan = final_clone;
06005
06006 if (ast_test_flag(original, AST_FLAG_ZOMBIE)
06007 || ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
06008
06009 ast_log(LOG_WARNING,
06010 "Can't setup masquerade. One or both channels is dead. (%s <-- %s)\n",
06011 original->name, clonechan->name);
06012 ast_channel_unlock(clonechan);
06013 ast_channel_unlock(original);
06014 return -1;
06015 }
06016 }
06017 break;
06018 }
06019
06020 if (original == clonechan) {
06021 ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name);
06022 ast_channel_unlock(clonechan);
06023 ast_channel_unlock(original);
06024 return -1;
06025 }
06026
06027 ast_debug(1, "Planning to masquerade channel %s into the structure of %s\n",
06028 clonechan->name, original->name);
06029
06030 if (!original->masqr && !original->masq && !clonechan->masq && !clonechan->masqr) {
06031 original->masq = clonechan;
06032 clonechan->masqr = original;
06033 if (xfer_ds) {
06034 ast_channel_datastore_add(original, xfer_ds);
06035 }
06036 ast_queue_frame(original, &ast_null_frame);
06037 ast_queue_frame(clonechan, &ast_null_frame);
06038 ast_debug(1, "Done planning to masquerade channel %s into the structure of %s\n", clonechan->name, original->name);
06039 res = 0;
06040 } else if (original->masq) {
06041 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
06042 original->masq->name, original->name);
06043 } else if (original->masqr) {
06044
06045 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
06046 original->name, original->masqr->name);
06047 } else if (clonechan->masq) {
06048 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
06049 clonechan->masq->name, clonechan->name);
06050 } else {
06051 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
06052 clonechan->name, clonechan->masqr->name);
06053 }
06054
06055 ast_channel_unlock(clonechan);
06056 ast_channel_unlock(original);
06057
06058 return res;
06059 }
06060
06061 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
06062 {
06063 return __ast_channel_masquerade(original, clone, NULL);
06064 }
06065
06066
06067
06068
06069
06070
06071
06072
06073
06074
06075
06076 static void party_connected_line_copy_transfer(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
06077 {
06078 struct ast_party_connected_line connected;
06079
06080 connected = *((struct ast_party_connected_line *) src);
06081 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
06082
06083
06084 if (!connected.id.name.str) {
06085 connected.id.name.str = "";
06086 }
06087 if (!connected.id.number.str) {
06088 connected.id.number.str = "";
06089 }
06090 if (!connected.id.subaddress.str) {
06091 connected.id.subaddress.str = "";
06092 }
06093 if (!connected.id.tag) {
06094 connected.id.tag = "";
06095 }
06096
06097 ast_party_connected_line_copy(dest, &connected);
06098 }
06099
06100
06101 struct xfer_masquerade_ds {
06102
06103 struct ast_party_connected_line target_id;
06104
06105 struct ast_party_connected_line transferee_id;
06106
06107 int target_held;
06108
06109 int transferee_held;
06110 };
06111
06112
06113
06114
06115
06116
06117
06118
06119
06120
06121 static void xfer_ds_destroy(void *data)
06122 {
06123 struct xfer_masquerade_ds *ds = data;
06124
06125 ast_party_connected_line_free(&ds->target_id);
06126 ast_party_connected_line_free(&ds->transferee_id);
06127 ast_free(ds);
06128 }
06129
06130 static const struct ast_datastore_info xfer_ds_info = {
06131 .type = "xfer_colp",
06132 .destroy = xfer_ds_destroy,
06133 };
06134
06135 int ast_channel_transfer_masquerade(
06136 struct ast_channel *target_chan,
06137 const struct ast_party_connected_line *target_id,
06138 int target_held,
06139 struct ast_channel *transferee_chan,
06140 const struct ast_party_connected_line *transferee_id,
06141 int transferee_held)
06142 {
06143 struct ast_datastore *xfer_ds;
06144 struct xfer_masquerade_ds *xfer_colp;
06145 int res;
06146
06147 xfer_ds = ast_datastore_alloc(&xfer_ds_info, NULL);
06148 if (!xfer_ds) {
06149 return -1;
06150 }
06151
06152 xfer_colp = ast_calloc(1, sizeof(*xfer_colp));
06153 if (!xfer_colp) {
06154 ast_datastore_free(xfer_ds);
06155 return -1;
06156 }
06157 party_connected_line_copy_transfer(&xfer_colp->target_id, target_id);
06158 xfer_colp->target_held = target_held;
06159 party_connected_line_copy_transfer(&xfer_colp->transferee_id, transferee_id);
06160 xfer_colp->transferee_held = transferee_held;
06161 xfer_ds->data = xfer_colp;
06162
06163 res = __ast_channel_masquerade(target_chan, transferee_chan, xfer_ds);
06164 if (res) {
06165 ast_datastore_free(xfer_ds);
06166 }
06167 return res;
06168 }
06169
06170
06171
06172
06173
06174 static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
06175 {
06176 ast_manager_event(chan, EVENT_FLAG_CALL, "Rename", "Channel: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", chan->name, newname, chan->uniqueid);
06177 ast_string_field_set(chan, name, newname);
06178 }
06179
06180 void ast_change_name(struct ast_channel *chan, const char *newname)
06181 {
06182
06183 ao2_lock(channels);
06184 ast_channel_lock(chan);
06185 ao2_unlink(channels, chan);
06186 __ast_change_name_nolink(chan, newname);
06187 ao2_link(channels, chan);
06188 ast_channel_unlock(chan);
06189 ao2_unlock(channels);
06190 }
06191
06192 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
06193 {
06194 struct ast_var_t *current, *newvar;
06195 const char *varname;
06196
06197 AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
06198 int vartype = 0;
06199
06200 varname = ast_var_full_name(current);
06201 if (!varname)
06202 continue;
06203
06204 if (varname[0] == '_') {
06205 vartype = 1;
06206 if (varname[1] == '_')
06207 vartype = 2;
06208 }
06209
06210 switch (vartype) {
06211 case 1:
06212 newvar = ast_var_assign(&varname[1], ast_var_value(current));
06213 if (newvar) {
06214 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
06215 ast_debug(1, "Copying soft-transferable variable %s.\n", ast_var_name(newvar));
06216 }
06217 break;
06218 case 2:
06219 newvar = ast_var_assign(varname, ast_var_value(current));
06220 if (newvar) {
06221 AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
06222 ast_debug(1, "Copying hard-transferable variable %s.\n", ast_var_name(newvar));
06223 }
06224 break;
06225 default:
06226 ast_debug(1, "Not copying variable %s.\n", ast_var_name(current));
06227 break;
06228 }
06229 }
06230 }
06231
06232
06233
06234
06235
06236
06237
06238
06239
06240
06241 static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
06242 {
06243 struct ast_var_t *current, *newvar;
06244
06245
06246 AST_LIST_APPEND_LIST(&original->varshead, &clonechan->varshead, entries);
06247
06248
06249
06250 AST_LIST_TRAVERSE(&original->varshead, current, entries) {
06251 newvar = ast_var_assign(current->name, current->value);
06252 if (newvar)
06253 AST_LIST_INSERT_TAIL(&clonechan->varshead, newvar, entries);
06254 }
06255 }
06256
06257
06258
06259
06260
06261
06262
06263
06264
06265
06266
06267
06268
06269
06270 static const char *oldest_linkedid(const char *a, const char *b)
06271 {
06272 const char *satime, *saseq;
06273 const char *sbtime, *sbseq;
06274 const char *dash;
06275
06276 unsigned int atime, aseq, btime, bseq;
06277
06278 if (ast_strlen_zero(a))
06279 return b;
06280
06281 if (ast_strlen_zero(b))
06282 return a;
06283
06284 satime = a;
06285 sbtime = b;
06286
06287
06288 if ((dash = strrchr(satime, '-'))) {
06289 satime = dash+1;
06290 }
06291 if ((dash = strrchr(sbtime, '-'))) {
06292 sbtime = dash+1;
06293 }
06294
06295
06296 saseq = strchr(satime, '.');
06297 sbseq = strchr(sbtime, '.');
06298 if (!saseq || !sbseq)
06299 return NULL;
06300 saseq++;
06301 sbseq++;
06302
06303
06304 atime = atoi(satime);
06305 btime = atoi(sbtime);
06306 aseq = atoi(saseq);
06307 bseq = atoi(sbseq);
06308
06309
06310 if (atime == btime) {
06311 return (aseq < bseq) ? a : b;
06312 }
06313 else {
06314 return (atime < btime) ? a : b;
06315 }
06316 }
06317
06318
06319
06320 static void ast_channel_change_linkedid(struct ast_channel *chan, const char *linkedid)
06321 {
06322 ast_assert(linkedid != NULL);
06323
06324 if (!strcmp(chan->linkedid, linkedid)) {
06325 return;
06326 }
06327
06328 ast_cel_check_retire_linkedid(chan);
06329 ast_string_field_set(chan, linkedid, linkedid);
06330 ast_cel_linkedid_ref(linkedid);
06331 }
06332
06333
06334
06335
06336
06337 void ast_channel_set_linkgroup(struct ast_channel *chan, struct ast_channel *peer)
06338 {
06339 const char* linkedid=NULL;
06340 struct ast_channel *bridged;
06341
06342 linkedid = oldest_linkedid(chan->linkedid, peer->linkedid);
06343 linkedid = oldest_linkedid(linkedid, chan->uniqueid);
06344 linkedid = oldest_linkedid(linkedid, peer->uniqueid);
06345 if (chan->_bridge) {
06346 bridged = ast_bridged_channel(chan);
06347 if (bridged != peer) {
06348 linkedid = oldest_linkedid(linkedid, bridged->linkedid);
06349 linkedid = oldest_linkedid(linkedid, bridged->uniqueid);
06350 }
06351 }
06352 if (peer->_bridge) {
06353 bridged = ast_bridged_channel(peer);
06354 if (bridged != chan) {
06355 linkedid = oldest_linkedid(linkedid, bridged->linkedid);
06356 linkedid = oldest_linkedid(linkedid, bridged->uniqueid);
06357 }
06358 }
06359
06360
06361 linkedid = ast_strdupa(linkedid);
06362
06363 ast_channel_change_linkedid(chan, linkedid);
06364 ast_channel_change_linkedid(peer, linkedid);
06365 if (chan->_bridge) {
06366 bridged = ast_bridged_channel(chan);
06367 if (bridged != peer) {
06368 ast_channel_change_linkedid(bridged, linkedid);
06369 }
06370 }
06371 if (peer->_bridge) {
06372 bridged = ast_bridged_channel(peer);
06373 if (bridged != chan) {
06374 ast_channel_change_linkedid(bridged, linkedid);
06375 }
06376 }
06377 }
06378
06379
06380 static void ast_set_owners_and_peers(struct ast_channel *chan1,
06381 struct ast_channel *chan2)
06382 {
06383 if (!ast_strlen_zero(chan1->accountcode) && ast_strlen_zero(chan2->peeraccount)) {
06384 ast_log(LOG_DEBUG, "setting peeraccount to %s for %s from data on channel %s\n",
06385 chan1->accountcode, chan2->name, chan1->name);
06386 ast_string_field_set(chan2, peeraccount, chan1->accountcode);
06387 }
06388 if (!ast_strlen_zero(chan2->accountcode) && ast_strlen_zero(chan1->peeraccount)) {
06389 ast_log(LOG_DEBUG, "setting peeraccount to %s for %s from data on channel %s\n",
06390 chan2->accountcode, chan1->name, chan2->name);
06391 ast_string_field_set(chan1, peeraccount, chan2->accountcode);
06392 }
06393 if (!ast_strlen_zero(chan1->peeraccount) && ast_strlen_zero(chan2->accountcode)) {
06394 ast_log(LOG_DEBUG, "setting accountcode to %s for %s from data on channel %s\n",
06395 chan1->peeraccount, chan2->name, chan1->name);
06396 ast_string_field_set(chan2, accountcode, chan1->peeraccount);
06397 }
06398 if (!ast_strlen_zero(chan2->peeraccount) && ast_strlen_zero(chan1->accountcode)) {
06399 ast_log(LOG_DEBUG, "setting accountcode to %s for %s from data on channel %s\n",
06400 chan2->peeraccount, chan1->name, chan2->name);
06401 ast_string_field_set(chan1, accountcode, chan2->peeraccount);
06402 }
06403 if (0 != strcmp(chan1->accountcode, chan2->peeraccount)) {
06404 ast_log(LOG_DEBUG, "changing peeraccount from %s to %s on %s to match channel %s\n",
06405 chan2->peeraccount, chan1->peeraccount, chan2->name, chan1->name);
06406 ast_string_field_set(chan2, peeraccount, chan1->accountcode);
06407 }
06408 if (0 != strcmp(chan2->accountcode, chan1->peeraccount)) {
06409 ast_log(LOG_DEBUG, "changing peeraccount from %s to %s on %s to match channel %s\n",
06410 chan1->peeraccount, chan2->peeraccount, chan1->name, chan2->name);
06411 ast_string_field_set(chan1, peeraccount, chan2->accountcode);
06412 }
06413 }
06414
06415
06416
06417
06418 static void report_new_callerid(struct ast_channel *chan)
06419 {
06420 int pres;
06421
06422 pres = ast_party_id_presentation(&chan->caller.id);
06423 ast_manager_event(chan, EVENT_FLAG_CALL, "NewCallerid",
06424 "Channel: %s\r\n"
06425 "CallerIDNum: %s\r\n"
06426 "CallerIDName: %s\r\n"
06427 "Uniqueid: %s\r\n"
06428 "CID-CallingPres: %d (%s)\r\n",
06429 chan->name,
06430 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
06431 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
06432 chan->uniqueid,
06433 pres,
06434 ast_describe_caller_presentation(pres)
06435 );
06436 }
06437
06438
06439
06440
06441
06442
06443
06444
06445
06446
06447
06448 static void masquerade_colp_transfer(struct ast_channel *transferee, struct xfer_masquerade_ds *colp)
06449 {
06450 struct ast_control_read_action_payload *frame_payload;
06451 int payload_size;
06452 int frame_size;
06453 unsigned char connected_line_data[1024];
06454
06455
06456 if (colp->target_held) {
06457 ast_queue_control(transferee, AST_CONTROL_UNHOLD);
06458 }
06459
06460
06461
06462
06463
06464
06465
06466
06467
06468
06469 payload_size = ast_connected_line_build_data(connected_line_data,
06470 sizeof(connected_line_data), &colp->target_id, NULL);
06471 if (payload_size != -1) {
06472 frame_size = payload_size + sizeof(*frame_payload);
06473 frame_payload = ast_alloca(frame_size);
06474 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
06475 frame_payload->payload_size = payload_size;
06476 memcpy(frame_payload->payload, connected_line_data, payload_size);
06477 ast_queue_control_data(transferee, AST_CONTROL_READ_ACTION, frame_payload,
06478 frame_size);
06479 }
06480
06481
06482
06483
06484
06485
06486 ast_channel_queue_connected_line_update(transferee, &colp->transferee_id, NULL);
06487 }
06488
06489
06490
06491
06492
06493
06494
06495
06496 int ast_do_masquerade(struct ast_channel *original)
06497 {
06498 int x;
06499 int i;
06500 int origstate;
06501 int visible_indication;
06502 int clone_was_zombie = 0;
06503 struct ast_frame *current;
06504 const struct ast_channel_tech *t;
06505 void *t_pvt;
06506 union {
06507 struct ast_party_dialed dialed;
06508 struct ast_party_caller caller;
06509 struct ast_party_connected_line connected;
06510 struct ast_party_redirecting redirecting;
06511 } exchange;
06512 struct ast_channel *clonechan, *chans[2];
06513 struct ast_channel *bridged;
06514 struct ast_cdr *cdr;
06515 struct ast_datastore *xfer_ds;
06516 struct xfer_masquerade_ds *xfer_colp;
06517 format_t rformat;
06518 format_t wformat;
06519 format_t tmp_format;
06520 char newn[AST_CHANNEL_NAME];
06521 char orig[AST_CHANNEL_NAME];
06522 char masqn[AST_CHANNEL_NAME];
06523 char zombn[AST_CHANNEL_NAME];
06524 char clone_sending_dtmf_digit;
06525 struct timeval clone_sending_dtmf_tv;
06526
06527
06528
06529
06530
06531
06532
06533
06534
06535
06536
06537
06538
06539
06540
06541
06542
06543
06544
06545
06546
06547
06548
06549
06550
06551
06552
06553
06554
06555
06556
06557
06558
06559 ao2_lock(channels);
06560
06561
06562
06563
06564
06565 ast_channel_lock(original);
06566
06567 clonechan = original->masq;
06568 if (!clonechan) {
06569
06570
06571
06572
06573 ast_channel_unlock(original);
06574 ao2_unlock(channels);
06575 return 0;
06576 }
06577
06578
06579 ast_channel_ref(original);
06580 ast_channel_ref(clonechan);
06581
06582
06583 ao2_unlink(channels, original);
06584 ao2_unlink(channels, clonechan);
06585
06586
06587 xfer_ds = ast_channel_datastore_find(original, &xfer_ds_info, NULL);
06588 if (xfer_ds) {
06589 ast_channel_datastore_remove(original, xfer_ds);
06590 xfer_colp = xfer_ds->data;
06591 } else {
06592 xfer_colp = NULL;
06593 }
06594
06595
06596
06597
06598
06599 visible_indication = original->visible_indication;
06600 ast_channel_unlock(original);
06601 ast_indicate(original, -1);
06602
06603
06604
06605
06606
06607 if (xfer_colp && xfer_colp->transferee_held) {
06608 ast_indicate(clonechan, AST_CONTROL_UNHOLD);
06609 }
06610
06611
06612 ast_channel_lock_both(original, clonechan);
06613
06614 ast_debug(4, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
06615 clonechan->name, clonechan->_state, original->name, original->_state);
06616
06617 chans[0] = clonechan;
06618 chans[1] = original;
06619 ast_manager_event_multichan(EVENT_FLAG_CALL, "Masquerade", 2, chans,
06620 "Clone: %s\r\n"
06621 "CloneState: %s\r\n"
06622 "Original: %s\r\n"
06623 "OriginalState: %s\r\n",
06624 clonechan->name, ast_state2str(clonechan->_state), original->name, ast_state2str(original->_state));
06625
06626
06627
06628
06629
06630 rformat = original->readformat;
06631 wformat = original->writeformat;
06632 free_translation(clonechan);
06633 free_translation(original);
06634
06635
06636 clone_sending_dtmf_digit = clonechan->sending_dtmf_digit;
06637 clone_sending_dtmf_tv = clonechan->sending_dtmf_tv;
06638
06639
06640 ast_copy_string(orig, original->name, sizeof(orig));
06641
06642 ast_copy_string(newn, clonechan->name, sizeof(newn));
06643
06644 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
06645
06646
06647 __ast_change_name_nolink(clonechan, masqn);
06648
06649
06650 __ast_change_name_nolink(original, newn);
06651
06652
06653 ast_channel_set_linkgroup(original, clonechan);
06654
06655
06656 t = original->tech;
06657 original->tech = clonechan->tech;
06658 clonechan->tech = t;
06659
06660 t_pvt = original->tech_pvt;
06661 original->tech_pvt = clonechan->tech_pvt;
06662 clonechan->tech_pvt = t_pvt;
06663
06664
06665 cdr = original->cdr;
06666 original->cdr = clonechan->cdr;
06667 clonechan->cdr = cdr;
06668
06669
06670 for (i = 0; i < 2; i++) {
06671 x = original->alertpipe[i];
06672 original->alertpipe[i] = clonechan->alertpipe[i];
06673 clonechan->alertpipe[i] = x;
06674 }
06675
06676
06677
06678
06679
06680
06681
06682
06683
06684
06685
06686
06687 {
06688 AST_LIST_HEAD_NOLOCK(, ast_frame) tmp_readq;
06689
06690 AST_LIST_HEAD_INIT_NOLOCK(&tmp_readq);
06691 AST_LIST_APPEND_LIST(&tmp_readq, &original->readq, frame_list);
06692 AST_LIST_APPEND_LIST(&original->readq, &clonechan->readq, frame_list);
06693
06694 while ((current = AST_LIST_REMOVE_HEAD(&tmp_readq, frame_list))) {
06695 AST_LIST_INSERT_TAIL(&original->readq, current, frame_list);
06696 if (original->alertpipe[1] > -1) {
06697 int poke = 0;
06698
06699 if (write(original->alertpipe[1], &poke, sizeof(poke)) < 0) {
06700 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
06701 }
06702 }
06703 }
06704 }
06705
06706
06707 tmp_format = original->rawreadformat;
06708 original->rawreadformat = clonechan->rawreadformat;
06709 clonechan->rawreadformat = tmp_format;
06710
06711 tmp_format = original->rawwriteformat;
06712 original->rawwriteformat = clonechan->rawwriteformat;
06713 clonechan->rawwriteformat = tmp_format;
06714
06715 clonechan->_softhangup = AST_SOFTHANGUP_DEV;
06716
06717
06718
06719
06720
06721 origstate = original->_state;
06722 original->_state = clonechan->_state;
06723 clonechan->_state = origstate;
06724
06725
06726 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
06727 __ast_change_name_nolink(clonechan, zombn);
06728
06729
06730 t_pvt = original->monitor;
06731 original->monitor = clonechan->monitor;
06732 clonechan->monitor = t_pvt;
06733
06734
06735 ast_string_field_set(original, language, clonechan->language);
06736
06737
06738 ast_string_field_set(original, parkinglot, clonechan->parkinglot);
06739
06740
06741 for (x = 0; x < AST_MAX_FDS; x++) {
06742 if (x != AST_GENERATOR_FD)
06743 ast_channel_set_fd(original, x, clonechan->fds[x]);
06744 }
06745
06746 ast_app_group_update(clonechan, original);
06747
06748
06749 if (AST_LIST_FIRST(&clonechan->datastores)) {
06750 struct ast_datastore *ds;
06751
06752
06753
06754 AST_LIST_TRAVERSE_SAFE_BEGIN(&clonechan->datastores, ds, entry) {
06755 if (ds->info->chan_fixup)
06756 ds->info->chan_fixup(ds->data, clonechan, original);
06757 }
06758 AST_LIST_TRAVERSE_SAFE_END;
06759 AST_LIST_APPEND_LIST(&original->datastores, &clonechan->datastores, entry);
06760 }
06761
06762 ast_autochan_new_channel(clonechan, original);
06763
06764 clone_variables(original, clonechan);
06765
06766 original->adsicpe = clonechan->adsicpe;
06767
06768
06769
06770
06771
06772 ast_set_flag(original, ast_test_flag(clonechan, AST_FLAG_EXCEPTION | AST_FLAG_OUTGOING));
06773 original->fdno = clonechan->fdno;
06774
06775
06776
06777
06778
06779
06780
06781
06782 exchange.dialed = original->dialed;
06783 original->dialed = clonechan->dialed;
06784 clonechan->dialed = exchange.dialed;
06785
06786 exchange.caller = original->caller;
06787 original->caller = clonechan->caller;
06788 clonechan->caller = exchange.caller;
06789
06790 exchange.connected = original->connected;
06791 original->connected = clonechan->connected;
06792 clonechan->connected = exchange.connected;
06793
06794 exchange.redirecting = original->redirecting;
06795 original->redirecting = clonechan->redirecting;
06796 clonechan->redirecting = exchange.redirecting;
06797
06798 report_new_callerid(original);
06799
06800
06801 ast_channel_set_fd(original, AST_TIMING_FD, original->timingfd);
06802
06803
06804 original->nativeformats = clonechan->nativeformats;
06805
06806
06807
06808
06809
06810 ast_set_write_format(original, wformat);
06811
06812
06813 ast_set_read_format(original, rformat);
06814
06815
06816 ast_string_field_set(original, musicclass, clonechan->musicclass);
06817
06818
06819 ast_string_field_set(original, accountcode, S_OR(clonechan->accountcode, ""));
06820 if (original->_bridge) {
06821
06822 ast_string_field_set(original->_bridge, peeraccount, S_OR(clonechan->accountcode, ""));
06823 ast_cel_report_event(original, AST_CEL_BRIDGE_UPDATE, NULL, NULL, NULL);
06824 }
06825
06826 ast_debug(1, "Putting channel %s in %s/%s formats\n", original->name,
06827 ast_getformatname(wformat), ast_getformatname(rformat));
06828
06829
06830 if (original->tech->fixup && original->tech->fixup(clonechan, original)) {
06831 ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (clonechan)\n",
06832 original->tech->type, original->name);
06833 }
06834
06835
06836 if (clonechan->tech->fixup && clonechan->tech->fixup(original, clonechan)) {
06837 ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (original)\n",
06838 clonechan->tech->type, clonechan->name);
06839 }
06840
06841
06842
06843
06844
06845
06846
06847
06848
06849
06850 if (ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
06851 clone_was_zombie = 1;
06852 } else {
06853 ast_set_flag(clonechan, AST_FLAG_ZOMBIE);
06854 ast_queue_frame(clonechan, &ast_null_frame);
06855 }
06856
06857
06858 original->masq = NULL;
06859 clonechan->masqr = NULL;
06860
06861
06862
06863
06864
06865
06866
06867 ast_channel_unlock(original);
06868
06869
06870 if (clonechan->tech->hangup && clonechan->tech->hangup(clonechan)) {
06871 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
06872 } else {
06873
06874
06875
06876
06877
06878 clonechan->tech = &ast_kill_tech;
06879 }
06880
06881 ast_channel_unlock(clonechan);
06882
06883 if (clone_sending_dtmf_digit) {
06884
06885
06886
06887
06888 ast_bridge_end_dtmf(original, clone_sending_dtmf_digit, clone_sending_dtmf_tv,
06889 "masquerade");
06890 }
06891
06892
06893
06894
06895
06896
06897
06898
06899
06900
06901 if (visible_indication) {
06902 ast_indicate(original, visible_indication);
06903 }
06904
06905 ast_channel_lock(original);
06906
06907
06908 if (ast_test_flag(original, AST_FLAG_BLOCKING)) {
06909 pthread_kill(original->blocker, SIGURG);
06910 }
06911
06912 ast_debug(1, "Done Masquerading %s (%d)\n", original->name, original->_state);
06913
06914 if ((bridged = ast_bridged_channel(original))) {
06915 ast_channel_ref(bridged);
06916 ast_channel_unlock(original);
06917 ast_indicate(bridged, AST_CONTROL_SRCCHANGE);
06918 ast_channel_unref(bridged);
06919 } else {
06920 ast_channel_unlock(original);
06921 }
06922 ast_indicate(original, AST_CONTROL_SRCCHANGE);
06923
06924 if (xfer_colp) {
06925
06926
06927
06928
06929
06930 masquerade_colp_transfer(original, xfer_colp);
06931 }
06932
06933 if (xfer_ds) {
06934 ast_datastore_free(xfer_ds);
06935 }
06936
06937 if (clone_was_zombie) {
06938 ast_channel_lock(clonechan);
06939 ast_debug(1, "Destroying channel clone '%s'\n", clonechan->name);
06940 ast_manager_event(clonechan, EVENT_FLAG_CALL, "Hangup",
06941 "Channel: %s\r\n"
06942 "Uniqueid: %s\r\n"
06943 "Cause: %d\r\n"
06944 "Cause-txt: %s\r\n",
06945 clonechan->name,
06946 clonechan->uniqueid,
06947 clonechan->hangupcause,
06948 ast_cause2str(clonechan->hangupcause)
06949 );
06950 ast_channel_unlock(clonechan);
06951
06952
06953
06954
06955
06956 ast_channel_unref(clonechan);
06957 } else {
06958 ao2_link(channels, clonechan);
06959 }
06960
06961 ao2_link(channels, original);
06962 ao2_unlock(channels);
06963
06964
06965 ast_channel_unref(original);
06966 ast_channel_unref(clonechan);
06967
06968 return 0;
06969 }
06970
06971 void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
06972 {
06973 ast_channel_lock(chan);
06974
06975 if (cid_num) {
06976 chan->caller.id.number.valid = 1;
06977 ast_free(chan->caller.id.number.str);
06978 chan->caller.id.number.str = ast_strdup(cid_num);
06979 }
06980 if (cid_name) {
06981 chan->caller.id.name.valid = 1;
06982 ast_free(chan->caller.id.name.str);
06983 chan->caller.id.name.str = ast_strdup(cid_name);
06984 }
06985 if (cid_ani) {
06986 chan->caller.ani.number.valid = 1;
06987 ast_free(chan->caller.ani.number.str);
06988 chan->caller.ani.number.str = ast_strdup(cid_ani);
06989 }
06990 if (chan->cdr) {
06991 ast_cdr_setcid(chan->cdr, chan);
06992 }
06993
06994 report_new_callerid(chan);
06995
06996 ast_channel_unlock(chan);
06997 }
06998
06999 void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
07000 {
07001 if (&chan->caller == caller) {
07002
07003 return;
07004 }
07005
07006 ast_channel_lock(chan);
07007 ast_party_caller_set(&chan->caller, caller, update);
07008 ast_channel_unlock(chan);
07009 }
07010
07011 void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
07012 {
07013 const char *pre_set_number;
07014 const char *pre_set_name;
07015
07016 if (&chan->caller == caller) {
07017
07018 return;
07019 }
07020
07021 ast_channel_lock(chan);
07022 pre_set_number =
07023 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL);
07024 pre_set_name = S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL);
07025 ast_party_caller_set(&chan->caller, caller, update);
07026 if (S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL)
07027 != pre_set_number
07028 || S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL)
07029 != pre_set_name) {
07030
07031 report_new_callerid(chan);
07032 }
07033 if (chan->cdr) {
07034 ast_cdr_setcid(chan->cdr, chan);
07035 }
07036 ast_channel_unlock(chan);
07037 }
07038
07039 int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
07040 {
07041 int oldstate = chan->_state;
07042 char name[AST_CHANNEL_NAME], *dashptr;
07043
07044 if (oldstate == state)
07045 return 0;
07046
07047 ast_copy_string(name, chan->name, sizeof(name));
07048 if ((dashptr = strrchr(name, '-'))) {
07049 *dashptr = '\0';
07050 }
07051
07052 chan->_state = state;
07053
07054
07055
07056
07057 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (chan->flags & AST_FLAG_DISABLE_DEVSTATE_CACHE ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
07058
07059
07060 ast_manager_event(chan, EVENT_FLAG_CALL, "Newstate",
07061 "Channel: %s\r\n"
07062 "ChannelState: %d\r\n"
07063 "ChannelStateDesc: %s\r\n"
07064 "CallerIDNum: %s\r\n"
07065 "CallerIDName: %s\r\n"
07066 "ConnectedLineNum: %s\r\n"
07067 "ConnectedLineName: %s\r\n"
07068 "Uniqueid: %s\r\n",
07069 chan->name, chan->_state, ast_state2str(chan->_state),
07070 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
07071 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
07072 S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, ""),
07073 S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, ""),
07074 chan->uniqueid);
07075
07076 return 0;
07077 }
07078
07079
07080 struct ast_channel *ast_bridged_channel(struct ast_channel *chan)
07081 {
07082 struct ast_channel *bridged;
07083 bridged = chan->_bridge;
07084 if (bridged && bridged->tech->bridged_channel)
07085 bridged = bridged->tech->bridged_channel(chan, bridged);
07086 return bridged;
07087 }
07088
07089 static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, const char *sound, int remain)
07090 {
07091 int min = 0, sec = 0, check;
07092
07093 check = ast_autoservice_start(peer);
07094 if (check)
07095 return;
07096
07097 if (remain > 0) {
07098 if (remain / 60 > 1) {
07099 min = remain / 60;
07100 sec = remain % 60;
07101 } else {
07102 sec = remain;
07103 }
07104 }
07105
07106 if (!strcmp(sound,"timeleft")) {
07107 ast_stream_and_wait(chan, "vm-youhave", "");
07108 if (min) {
07109 ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, NULL);
07110 ast_stream_and_wait(chan, "queue-minutes", "");
07111 }
07112 if (sec) {
07113 ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, NULL);
07114 ast_stream_and_wait(chan, "queue-seconds", "");
07115 }
07116 } else {
07117 ast_stream_and_wait(chan, sound, "");
07118 }
07119
07120 ast_autoservice_stop(peer);
07121 }
07122
07123 static enum ast_bridge_result ast_generic_bridge(struct ast_channel *c0, struct ast_channel *c1,
07124 struct ast_bridge_config *config, struct ast_frame **fo,
07125 struct ast_channel **rc)
07126 {
07127
07128 struct ast_channel *cs[3];
07129 struct ast_frame *f;
07130 enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
07131 format_t o0nativeformats;
07132 format_t o1nativeformats;
07133 int watch_c0_dtmf;
07134 int watch_c1_dtmf;
07135 void *pvt0, *pvt1;
07136
07137 int frame_put_in_jb = 0;
07138 int jb_in_use;
07139 int to;
07140
07141 cs[0] = c0;
07142 cs[1] = c1;
07143 pvt0 = c0->tech_pvt;
07144 pvt1 = c1->tech_pvt;
07145 o0nativeformats = c0->nativeformats;
07146 o1nativeformats = c1->nativeformats;
07147 watch_c0_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_0;
07148 watch_c1_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_1;
07149
07150
07151 jb_in_use = ast_jb_do_usecheck(c0, c1);
07152 if (jb_in_use)
07153 ast_jb_empty_and_reset(c0, c1);
07154
07155 ast_poll_channel_add(c0, c1);
07156
07157 if (config->feature_timer > 0 && ast_tvzero(config->nexteventts)) {
07158
07159
07160
07161 config->nexteventts = ast_tvadd(ast_tvnow(), ast_samp2tv(config->feature_timer, 1000));
07162 }
07163
07164 for (;;) {
07165 struct ast_channel *who, *other;
07166
07167 if ((c0->tech_pvt != pvt0) || (c1->tech_pvt != pvt1) ||
07168 (o0nativeformats != c0->nativeformats) ||
07169 (o1nativeformats != c1->nativeformats)) {
07170
07171 res = AST_BRIDGE_RETRY;
07172 break;
07173 }
07174 if (config->nexteventts.tv_sec) {
07175 to = ast_tvdiff_ms(config->nexteventts, ast_tvnow());
07176 if (to <= 0) {
07177 if (config->timelimit && !config->feature_timer && !ast_test_flag(config, AST_FEATURE_WARNING_ACTIVE)) {
07178 res = AST_BRIDGE_RETRY;
07179
07180 ast_set_flag(config, AST_FEATURE_WARNING_ACTIVE);
07181 } else if (config->feature_timer) {
07182
07183 ast_clear_flag(config, AST_FEATURE_WARNING_ACTIVE);
07184 res = AST_BRIDGE_RETRY;
07185 } else {
07186 res = AST_BRIDGE_COMPLETE;
07187 }
07188 break;
07189 }
07190 } else {
07191
07192
07193
07194
07195 if (!ast_tvzero(config->nexteventts)) {
07196 int diff = ast_tvdiff_ms(config->nexteventts, ast_tvnow());
07197 if (diff <= 0) {
07198 res = AST_BRIDGE_RETRY;
07199 break;
07200 }
07201 }
07202 to = -1;
07203 }
07204
07205
07206 if (jb_in_use)
07207 to = ast_jb_get_when_to_wakeup(c0, c1, to);
07208 who = ast_waitfor_n(cs, 2, &to);
07209 if (!who) {
07210
07211 if (jb_in_use)
07212 ast_jb_get_and_deliver(c0, c1);
07213 if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {
07214 if (c0->_softhangup & AST_SOFTHANGUP_UNBRIDGE) {
07215 ast_channel_clear_softhangup(c0, AST_SOFTHANGUP_UNBRIDGE);
07216 }
07217 if (c1->_softhangup & AST_SOFTHANGUP_UNBRIDGE) {
07218 ast_channel_clear_softhangup(c1, AST_SOFTHANGUP_UNBRIDGE);
07219 }
07220 c0->_bridge = c1;
07221 c1->_bridge = c0;
07222 }
07223 continue;
07224 }
07225 f = ast_read(who);
07226 if (!f) {
07227 *fo = NULL;
07228 *rc = who;
07229 ast_debug(1, "Didn't get a frame from channel: %s\n",who->name);
07230 break;
07231 }
07232
07233 other = (who == c0) ? c1 : c0;
07234
07235 if (jb_in_use)
07236 frame_put_in_jb = !ast_jb_put(other, f);
07237
07238 if ((f->frametype == AST_FRAME_CONTROL) && !(config->flags & AST_BRIDGE_IGNORE_SIGS)) {
07239 int bridge_exit = 0;
07240
07241 switch (f->subclass.integer) {
07242 case AST_CONTROL_AOC:
07243 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
07244 break;
07245 case AST_CONTROL_REDIRECTING:
07246 if (ast_channel_redirecting_macro(who, other, f, other == c0, 1)) {
07247 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
07248 }
07249 break;
07250 case AST_CONTROL_CONNECTED_LINE:
07251 if (ast_channel_connected_line_macro(who, other, f, other == c0, 1)) {
07252 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
07253 }
07254 break;
07255 case AST_CONTROL_HOLD:
07256 case AST_CONTROL_UNHOLD:
07257 case AST_CONTROL_VIDUPDATE:
07258 case AST_CONTROL_SRCUPDATE:
07259 case AST_CONTROL_SRCCHANGE:
07260 case AST_CONTROL_T38_PARAMETERS:
07261 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
07262 if (jb_in_use) {
07263 ast_jb_empty_and_reset(c0, c1);
07264 }
07265 break;
07266 default:
07267 *fo = f;
07268 *rc = who;
07269 bridge_exit = 1;
07270 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass.integer, who->name);
07271 break;
07272 }
07273 if (bridge_exit)
07274 break;
07275 }
07276 if ((f->frametype == AST_FRAME_VOICE) ||
07277 (f->frametype == AST_FRAME_DTMF_BEGIN) ||
07278 (f->frametype == AST_FRAME_DTMF) ||
07279 (f->frametype == AST_FRAME_VIDEO) ||
07280 (f->frametype == AST_FRAME_IMAGE) ||
07281 (f->frametype == AST_FRAME_HTML) ||
07282 (f->frametype == AST_FRAME_MODEM) ||
07283 (f->frametype == AST_FRAME_TEXT)) {
07284
07285 int monitored_source = (who == c0) ? watch_c0_dtmf : watch_c1_dtmf;
07286
07287 if (monitored_source &&
07288 (f->frametype == AST_FRAME_DTMF_END ||
07289 f->frametype == AST_FRAME_DTMF_BEGIN)) {
07290 *fo = f;
07291 *rc = who;
07292 ast_debug(1, "Got DTMF %s on channel (%s)\n",
07293 f->frametype == AST_FRAME_DTMF_END ? "end" : "begin",
07294 who->name);
07295
07296 break;
07297 }
07298
07299 if (!frame_put_in_jb)
07300 ast_write(other, f);
07301
07302
07303 if (jb_in_use)
07304 ast_jb_get_and_deliver(c0, c1);
07305 }
07306
07307 ast_frfree(f);
07308
07309 #ifndef HAVE_EPOLL
07310
07311 cs[2] = cs[0];
07312 cs[0] = cs[1];
07313 cs[1] = cs[2];
07314 #endif
07315 }
07316
07317 ast_poll_channel_del(c0, c1);
07318
07319 return res;
07320 }
07321
07322
07323 int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
07324 {
07325
07326 if (!c0->tech->early_bridge || (c1 && (!c1->tech->early_bridge || c0->tech->early_bridge != c1->tech->early_bridge)))
07327 return -1;
07328
07329 return c0->tech->early_bridge(c0, c1);
07330 }
07331
07332
07333
07334
07335
07336
07337
07338 static void manager_bridge_event(int onoff, int type, struct ast_channel *c0, struct ast_channel *c1)
07339 {
07340 struct ast_channel *chans[2] = { c0, c1 };
07341 ast_manager_event_multichan(EVENT_FLAG_CALL, "Bridge", 2, chans,
07342 "Bridgestate: %s\r\n"
07343 "Bridgetype: %s\r\n"
07344 "Channel1: %s\r\n"
07345 "Channel2: %s\r\n"
07346 "Uniqueid1: %s\r\n"
07347 "Uniqueid2: %s\r\n"
07348 "CallerID1: %s\r\n"
07349 "CallerID2: %s\r\n",
07350 onoff ? "Link" : "Unlink",
07351 type == 1 ? "core" : "native",
07352 c0->name, c1->name,
07353 c0->uniqueid, c1->uniqueid,
07354 S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, ""),
07355 S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, ""));
07356 }
07357
07358 static void update_bridge_vars(struct ast_channel *c0, struct ast_channel *c1)
07359 {
07360 const char *c0_name;
07361 const char *c1_name;
07362 const char *c0_pvtid = NULL;
07363 const char *c1_pvtid = NULL;
07364
07365 ast_channel_lock(c1);
07366 c1_name = ast_strdupa(c1->name);
07367 if (c1->tech->get_pvt_uniqueid) {
07368 c1_pvtid = ast_strdupa(c1->tech->get_pvt_uniqueid(c1));
07369 }
07370 ast_channel_unlock(c1);
07371
07372 ast_channel_lock(c0);
07373 if (!ast_strlen_zero(pbx_builtin_getvar_helper(c0, "BRIDGEPEER"))) {
07374 pbx_builtin_setvar_helper(c0, "BRIDGEPEER", c1_name);
07375 }
07376 if (c1_pvtid) {
07377 pbx_builtin_setvar_helper(c0, "BRIDGEPVTCALLID", c1_pvtid);
07378 }
07379 c0_name = ast_strdupa(c0->name);
07380 if (c0->tech->get_pvt_uniqueid) {
07381 c0_pvtid = ast_strdupa(c0->tech->get_pvt_uniqueid(c0));
07382 }
07383 ast_channel_unlock(c0);
07384
07385 ast_channel_lock(c1);
07386 if (!ast_strlen_zero(pbx_builtin_getvar_helper(c1, "BRIDGEPEER"))) {
07387 pbx_builtin_setvar_helper(c1, "BRIDGEPEER", c0_name);
07388 }
07389 if (c0_pvtid) {
07390 pbx_builtin_setvar_helper(c1, "BRIDGEPVTCALLID", c0_pvtid);
07391 }
07392 ast_channel_unlock(c1);
07393 }
07394
07395 static void bridge_play_sounds(struct ast_channel *c0, struct ast_channel *c1)
07396 {
07397 const char *s, *sound;
07398
07399
07400
07401 ast_channel_lock(c0);
07402 if ((s = pbx_builtin_getvar_helper(c0, "BRIDGE_PLAY_SOUND"))) {
07403 sound = ast_strdupa(s);
07404 ast_channel_unlock(c0);
07405 bridge_playfile(c0, c1, sound, 0);
07406 pbx_builtin_setvar_helper(c0, "BRIDGE_PLAY_SOUND", NULL);
07407 } else {
07408 ast_channel_unlock(c0);
07409 }
07410
07411 ast_channel_lock(c1);
07412 if ((s = pbx_builtin_getvar_helper(c1, "BRIDGE_PLAY_SOUND"))) {
07413 sound = ast_strdupa(s);
07414 ast_channel_unlock(c1);
07415 bridge_playfile(c1, c0, sound, 0);
07416 pbx_builtin_setvar_helper(c1, "BRIDGE_PLAY_SOUND", NULL);
07417 } else {
07418 ast_channel_unlock(c1);
07419 }
07420 }
07421
07422
07423 enum ast_bridge_result ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1,
07424 struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
07425 {
07426 struct ast_channel *chans[2] = { c0, c1 };
07427 enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
07428 format_t o0nativeformats;
07429 format_t o1nativeformats;
07430 long time_left_ms=0;
07431 char caller_warning = 0;
07432 char callee_warning = 0;
07433
07434 *fo = NULL;
07435
07436 if (c0->_bridge) {
07437 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
07438 c0->name, c0->_bridge->name);
07439 return -1;
07440 }
07441 if (c1->_bridge) {
07442 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
07443 c1->name, c1->_bridge->name);
07444 return -1;
07445 }
07446
07447
07448 if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
07449 ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1))
07450 return -1;
07451
07452 caller_warning = ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING);
07453 callee_warning = ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING);
07454
07455 if (ast_tvzero(config->start_time)) {
07456 config->start_time = ast_tvnow();
07457 if (config->start_sound) {
07458 if (caller_warning) {
07459 bridge_playfile(c0, c1, config->start_sound, config->timelimit / 1000);
07460 }
07461 if (callee_warning) {
07462 bridge_playfile(c1, c0, config->start_sound, config->timelimit / 1000);
07463 }
07464 }
07465 }
07466
07467
07468 c0->_bridge = c1;
07469 c1->_bridge = c0;
07470
07471 ast_set_owners_and_peers(c0, c1);
07472
07473 o0nativeformats = c0->nativeformats;
07474 o1nativeformats = c1->nativeformats;
07475
07476 if (config->feature_timer && !ast_tvzero(config->nexteventts)) {
07477 config->nexteventts = ast_tvadd(config->feature_start_time, ast_samp2tv(config->feature_timer, 1000));
07478 } else if (config->timelimit) {
07479 time_left_ms = config->timelimit - ast_tvdiff_ms(ast_tvnow(), config->start_time);
07480 config->nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
07481 if ((caller_warning || callee_warning) && config->play_warning) {
07482 long next_warn = config->play_warning;
07483 if (time_left_ms < config->play_warning && config->warning_freq > 0) {
07484
07485 long warns_passed = (config->play_warning - time_left_ms) / config->warning_freq;
07486
07487
07488 next_warn = config->play_warning - warns_passed * config->warning_freq;
07489 }
07490 config->nexteventts = ast_tvsub(config->nexteventts, ast_samp2tv(next_warn, 1000));
07491 }
07492 } else {
07493 config->nexteventts.tv_sec = 0;
07494 config->nexteventts.tv_usec = 0;
07495 }
07496
07497 if (!c0->tech->send_digit_begin)
07498 ast_set_flag(c1, AST_FLAG_END_DTMF_ONLY);
07499 if (!c1->tech->send_digit_begin)
07500 ast_set_flag(c0, AST_FLAG_END_DTMF_ONLY);
07501 manager_bridge_event(1, 1, c0, c1);
07502
07503
07504 ast_indicate(c0, AST_CONTROL_SRCUPDATE);
07505 ast_indicate(c1, AST_CONTROL_SRCUPDATE);
07506
07507 for (;;) {
07508 struct timeval now = { 0, };
07509 int to;
07510
07511 to = -1;
07512
07513 if (!ast_tvzero(config->nexteventts)) {
07514 now = ast_tvnow();
07515 to = ast_tvdiff_ms(config->nexteventts, now);
07516 if (to <= 0) {
07517 if (!config->timelimit) {
07518 res = AST_BRIDGE_COMPLETE;
07519 break;
07520 }
07521 to = 0;
07522 }
07523 }
07524
07525 if (config->timelimit) {
07526 time_left_ms = config->timelimit - ast_tvdiff_ms(now, config->start_time);
07527 if (time_left_ms < to)
07528 to = time_left_ms;
07529
07530 if (time_left_ms <= 0) {
07531 if (caller_warning && config->end_sound)
07532 bridge_playfile(c0, c1, config->end_sound, 0);
07533 if (callee_warning && config->end_sound)
07534 bridge_playfile(c1, c0, config->end_sound, 0);
07535 *fo = NULL;
07536 res = 0;
07537 break;
07538 }
07539
07540 if (!to) {
07541 if (time_left_ms >= 5000 && config->warning_sound && config->play_warning && ast_test_flag(config, AST_FEATURE_WARNING_ACTIVE)) {
07542 int t = (time_left_ms + 500) / 1000;
07543 if (caller_warning)
07544 bridge_playfile(c0, c1, config->warning_sound, t);
07545 if (callee_warning)
07546 bridge_playfile(c1, c0, config->warning_sound, t);
07547 }
07548
07549 if (config->warning_freq && (time_left_ms > (config->warning_freq + 5000))) {
07550 config->nexteventts = ast_tvadd(config->nexteventts, ast_samp2tv(config->warning_freq, 1000));
07551 } else {
07552 config->nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
07553 }
07554 }
07555 ast_clear_flag(config, AST_FEATURE_WARNING_ACTIVE);
07556 }
07557
07558 if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {
07559 if (c0->_softhangup & AST_SOFTHANGUP_UNBRIDGE) {
07560 ast_channel_clear_softhangup(c0, AST_SOFTHANGUP_UNBRIDGE);
07561 }
07562 if (c1->_softhangup & AST_SOFTHANGUP_UNBRIDGE) {
07563 ast_channel_clear_softhangup(c1, AST_SOFTHANGUP_UNBRIDGE);
07564 }
07565 c0->_bridge = c1;
07566 c1->_bridge = c0;
07567 ast_debug(1, "Unbridge signal received. Ending native bridge.\n");
07568 continue;
07569 }
07570
07571
07572 if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
07573 ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1)) {
07574 *fo = NULL;
07575 res = 0;
07576 ast_debug(1, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",
07577 c0->name, c1->name,
07578 ast_test_flag(c0, AST_FLAG_ZOMBIE) ? "Yes" : "No",
07579 ast_check_hangup(c0) ? "Yes" : "No",
07580 ast_test_flag(c1, AST_FLAG_ZOMBIE) ? "Yes" : "No",
07581 ast_check_hangup(c1) ? "Yes" : "No");
07582 break;
07583 }
07584
07585 update_bridge_vars(c0, c1);
07586
07587 bridge_play_sounds(c0, c1);
07588
07589 if (c0->tech->bridge &&
07590
07591 (!config->timelimit || to > 1000 || to == 0) &&
07592 (c0->tech->bridge == c1->tech->bridge) &&
07593 !c0->monitor && !c1->monitor &&
07594 !c0->audiohooks && !c1->audiohooks &&
07595 ast_framehook_list_is_empty(c0->framehooks) && ast_framehook_list_is_empty(c1->framehooks) &&
07596 !c0->masq && !c0->masqr && !c1->masq && !c1->masqr) {
07597 int timeoutms = to - 1000 > 0 ? to - 1000 : to;
07598
07599 ast_set_flag(c0, AST_FLAG_NBRIDGE);
07600 ast_set_flag(c1, AST_FLAG_NBRIDGE);
07601 if ((res = c0->tech->bridge(c0, c1, config->flags, fo, rc, timeoutms)) == AST_BRIDGE_COMPLETE) {
07602 ast_manager_event_multichan(EVENT_FLAG_CALL, "Unlink", 2, chans,
07603 "Channel1: %s\r\n"
07604 "Channel2: %s\r\n"
07605 "Uniqueid1: %s\r\n"
07606 "Uniqueid2: %s\r\n"
07607 "CallerID1: %s\r\n"
07608 "CallerID2: %s\r\n",
07609 c0->name, c1->name,
07610 c0->uniqueid, c1->uniqueid,
07611 S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, "<unknown>"),
07612 S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, "<unknown>"));
07613
07614 ast_debug(1, "Returning from native bridge, channels: %s, %s\n", c0->name, c1->name);
07615
07616 ast_clear_flag(c0, AST_FLAG_NBRIDGE);
07617 ast_clear_flag(c1, AST_FLAG_NBRIDGE);
07618
07619 if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {
07620 continue;
07621 }
07622
07623 c0->_bridge = NULL;
07624 c1->_bridge = NULL;
07625 return res;
07626 } else {
07627 ast_clear_flag(c0, AST_FLAG_NBRIDGE);
07628 ast_clear_flag(c1, AST_FLAG_NBRIDGE);
07629 }
07630 switch (res) {
07631 case AST_BRIDGE_RETRY:
07632 if (config->play_warning) {
07633 ast_set_flag(config, AST_FEATURE_WARNING_ACTIVE);
07634 }
07635 continue;
07636 default:
07637 ast_verb(3, "Native bridging %s and %s ended\n", c0->name, c1->name);
07638
07639 case AST_BRIDGE_FAILED_NOWARN:
07640 break;
07641 }
07642 }
07643
07644 if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) ||
07645 (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
07646 !(c0->generator || c1->generator)) {
07647 if (ast_channel_make_compatible(c0, c1)) {
07648 ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
07649 manager_bridge_event(0, 1, c0, c1);
07650 return AST_BRIDGE_FAILED;
07651 }
07652 o0nativeformats = c0->nativeformats;
07653 o1nativeformats = c1->nativeformats;
07654 }
07655
07656 update_bridge_vars(c0, c1);
07657
07658 res = ast_generic_bridge(c0, c1, config, fo, rc);
07659 if (res != AST_BRIDGE_RETRY) {
07660 break;
07661 } else if (config->feature_timer) {
07662
07663 break;
07664 }
07665 }
07666
07667 ast_clear_flag(c0, AST_FLAG_END_DTMF_ONLY);
07668 ast_clear_flag(c1, AST_FLAG_END_DTMF_ONLY);
07669
07670
07671 ast_indicate(c0, AST_CONTROL_SRCUPDATE);
07672 ast_indicate(c1, AST_CONTROL_SRCUPDATE);
07673
07674 c0->_bridge = NULL;
07675 c1->_bridge = NULL;
07676
07677 ast_manager_event_multichan(EVENT_FLAG_CALL, "Unlink", 2, chans,
07678 "Channel1: %s\r\n"
07679 "Channel2: %s\r\n"
07680 "Uniqueid1: %s\r\n"
07681 "Uniqueid2: %s\r\n"
07682 "CallerID1: %s\r\n"
07683 "CallerID2: %s\r\n",
07684 c0->name, c1->name,
07685 c0->uniqueid, c1->uniqueid,
07686 S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, "<unknown>"),
07687 S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, "<unknown>"));
07688 ast_debug(1, "Bridge stops bridging channels %s and %s\n", c0->name, c1->name);
07689
07690 return res;
07691 }
07692
07693
07694 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
07695 {
07696 int res;
07697
07698 ast_channel_lock(chan);
07699 if (!chan->tech->setoption) {
07700 errno = ENOSYS;
07701 ast_channel_unlock(chan);
07702 return -1;
07703 }
07704
07705 if (block)
07706 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
07707
07708 res = chan->tech->setoption(chan, option, data, datalen);
07709 ast_channel_unlock(chan);
07710
07711 return res;
07712 }
07713
07714 int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
07715 {
07716 int res;
07717
07718 ast_channel_lock(chan);
07719 if (!chan->tech->queryoption) {
07720 errno = ENOSYS;
07721 ast_channel_unlock(chan);
07722 return -1;
07723 }
07724
07725 if (block)
07726 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
07727
07728 res = chan->tech->queryoption(chan, option, data, datalen);
07729 ast_channel_unlock(chan);
07730
07731 return res;
07732 }
07733
07734 struct tonepair_def {
07735 int freq1;
07736 int freq2;
07737 int duration;
07738 int vol;
07739 };
07740
07741 struct tonepair_state {
07742 int fac1;
07743 int fac2;
07744 int v1_1;
07745 int v2_1;
07746 int v3_1;
07747 int v1_2;
07748 int v2_2;
07749 int v3_2;
07750 format_t origwfmt;
07751 int pos;
07752 int duration;
07753 int modulate;
07754 struct ast_frame f;
07755 unsigned char offset[AST_FRIENDLY_OFFSET];
07756 short data[4000];
07757 };
07758
07759 static void tonepair_release(struct ast_channel *chan, void *params)
07760 {
07761 struct tonepair_state *ts = params;
07762
07763 if (chan)
07764 ast_set_write_format(chan, ts->origwfmt);
07765 ast_free(ts);
07766 }
07767
07768 static void *tonepair_alloc(struct ast_channel *chan, void *params)
07769 {
07770 struct tonepair_state *ts;
07771 struct tonepair_def *td = params;
07772
07773 if (!(ts = ast_calloc(1, sizeof(*ts))))
07774 return NULL;
07775 ts->origwfmt = chan->writeformat;
07776 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
07777 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
07778 tonepair_release(NULL, ts);
07779 ts = NULL;
07780 } else {
07781 ts->fac1 = 2.0 * cos(2.0 * M_PI * (td->freq1 / 8000.0)) * 32768.0;
07782 ts->v1_1 = 0;
07783 ts->v2_1 = sin(-4.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
07784 ts->v3_1 = sin(-2.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
07785 ts->v2_1 = 0;
07786 ts->fac2 = 2.0 * cos(2.0 * M_PI * (td->freq2 / 8000.0)) * 32768.0;
07787 ts->v2_2 = sin(-4.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
07788 ts->v3_2 = sin(-2.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
07789 ts->duration = td->duration;
07790 ts->modulate = 0;
07791 }
07792
07793 ast_set_flag(chan, AST_FLAG_WRITE_INT);
07794 return ts;
07795 }
07796
07797 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
07798 {
07799 struct tonepair_state *ts = data;
07800 int x;
07801
07802
07803
07804
07805 len = samples * 2;
07806
07807 if (len > sizeof(ts->data) / 2 - 1) {
07808 ast_log(LOG_WARNING, "Can't generate that much data!\n");
07809 return -1;
07810 }
07811 memset(&ts->f, 0, sizeof(ts->f));
07812 for (x=0;x<len/2;x++) {
07813 ts->v1_1 = ts->v2_1;
07814 ts->v2_1 = ts->v3_1;
07815 ts->v3_1 = (ts->fac1 * ts->v2_1 >> 15) - ts->v1_1;
07816
07817 ts->v1_2 = ts->v2_2;
07818 ts->v2_2 = ts->v3_2;
07819 ts->v3_2 = (ts->fac2 * ts->v2_2 >> 15) - ts->v1_2;
07820 if (ts->modulate) {
07821 int p;
07822 p = ts->v3_2 - 32768;
07823 if (p < 0) p = -p;
07824 p = ((p * 9) / 10) + 1;
07825 ts->data[x] = (ts->v3_1 * p) >> 15;
07826 } else
07827 ts->data[x] = ts->v3_1 + ts->v3_2;
07828 }
07829 ts->f.frametype = AST_FRAME_VOICE;
07830 ts->f.subclass.codec = AST_FORMAT_SLINEAR;
07831 ts->f.datalen = len;
07832 ts->f.samples = samples;
07833 ts->f.offset = AST_FRIENDLY_OFFSET;
07834 ts->f.data.ptr = ts->data;
07835 ast_write(chan, &ts->f);
07836 ts->pos += x;
07837 if (ts->duration > 0) {
07838 if (ts->pos >= ts->duration * 8)
07839 return -1;
07840 }
07841 return 0;
07842 }
07843
07844 static struct ast_generator tonepair = {
07845 .alloc = tonepair_alloc,
07846 .release = tonepair_release,
07847 .generate = tonepair_generator,
07848 };
07849
07850 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
07851 {
07852 struct tonepair_def d = { 0, };
07853
07854 d.freq1 = freq1;
07855 d.freq2 = freq2;
07856 d.duration = duration;
07857 d.vol = (vol < 1) ? 8192 : vol;
07858 if (ast_activate_generator(chan, &tonepair, &d))
07859 return -1;
07860 return 0;
07861 }
07862
07863 void ast_tonepair_stop(struct ast_channel *chan)
07864 {
07865 ast_deactivate_generator(chan);
07866 }
07867
07868 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
07869 {
07870 int res;
07871
07872 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
07873 return res;
07874
07875
07876 while (chan->generatordata && ast_waitfor(chan, 100) >= 0) {
07877 struct ast_frame *f = ast_read(chan);
07878 if (f)
07879 ast_frfree(f);
07880 else
07881 return -1;
07882 }
07883 return 0;
07884 }
07885
07886 ast_group_t ast_get_group(const char *s)
07887 {
07888 char *piece;
07889 char *c;
07890 int start=0, finish=0, x;
07891 ast_group_t group = 0;
07892
07893 if (ast_strlen_zero(s))
07894 return 0;
07895
07896 c = ast_strdupa(s);
07897
07898 while ((piece = strsep(&c, ","))) {
07899 if (sscanf(piece, "%30d-%30d", &start, &finish) == 2) {
07900
07901 } else if (sscanf(piece, "%30d", &start)) {
07902
07903 finish = start;
07904 } else {
07905 ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
07906 continue;
07907 }
07908 for (x = start; x <= finish; x++) {
07909 if ((x > 63) || (x < 0)) {
07910 ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
07911 } else
07912 group |= ((ast_group_t) 1 << x);
07913 }
07914 }
07915 return group;
07916 }
07917
07918 static int (*ast_moh_start_ptr)(struct ast_channel *, const char *, const char *) = NULL;
07919 static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
07920 static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
07921
07922 void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, const char *, const char *),
07923 void (*stop_ptr)(struct ast_channel *),
07924 void (*cleanup_ptr)(struct ast_channel *))
07925 {
07926 ast_moh_start_ptr = start_ptr;
07927 ast_moh_stop_ptr = stop_ptr;
07928 ast_moh_cleanup_ptr = cleanup_ptr;
07929 }
07930
07931 void ast_uninstall_music_functions(void)
07932 {
07933 ast_moh_start_ptr = NULL;
07934 ast_moh_stop_ptr = NULL;
07935 ast_moh_cleanup_ptr = NULL;
07936 }
07937
07938
07939 int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
07940 {
07941 if (ast_moh_start_ptr)
07942 return ast_moh_start_ptr(chan, mclass, interpclass);
07943
07944 ast_verb(3, "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : (interpclass ? interpclass : "default"));
07945
07946 return 0;
07947 }
07948
07949
07950 void ast_moh_stop(struct ast_channel *chan)
07951 {
07952 if (ast_moh_stop_ptr)
07953 ast_moh_stop_ptr(chan);
07954 }
07955
07956 void ast_moh_cleanup(struct ast_channel *chan)
07957 {
07958 if (ast_moh_cleanup_ptr)
07959 ast_moh_cleanup_ptr(chan);
07960 }
07961
07962 static int ast_channel_hash_cb(const void *obj, const int flags)
07963 {
07964 const struct ast_channel *chan = obj;
07965
07966
07967
07968 if (ast_strlen_zero(chan->name)) {
07969 return 0;
07970 }
07971
07972 return ast_str_case_hash(chan->name);
07973 }
07974
07975 int ast_plc_reload(void)
07976 {
07977 struct ast_variable *var;
07978 struct ast_flags config_flags = { 0 };
07979 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
07980 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
07981 return 0;
07982 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
07983 if (!strcasecmp(var->name, "genericplc")) {
07984 ast_set2_flag(&ast_options, ast_true(var->value), AST_OPT_FLAG_GENERIC_PLC);
07985 }
07986 }
07987 ast_config_destroy(cfg);
07988 return 0;
07989 }
07990
07991
07992
07993
07994
07995 static int data_channels_provider_handler(const struct ast_data_search *search,
07996 struct ast_data *root)
07997 {
07998 struct ast_channel *c;
07999 struct ast_channel_iterator *iter = NULL;
08000 struct ast_data *data_channel;
08001
08002 for (iter = ast_channel_iterator_all_new();
08003 iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
08004 ast_channel_lock(c);
08005
08006 data_channel = ast_data_add_node(root, "channel");
08007 if (!data_channel) {
08008 ast_channel_unlock(c);
08009 continue;
08010 }
08011
08012 if (ast_channel_data_add_structure(data_channel, c, 1) < 0) {
08013 ast_log(LOG_ERROR, "Unable to add channel structure for channel: %s\n", c->name);
08014 }
08015
08016 ast_channel_unlock(c);
08017
08018 if (!ast_data_search_match(search, data_channel)) {
08019 ast_data_remove_node(root, data_channel);
08020 }
08021 }
08022 if (iter) {
08023 ast_channel_iterator_destroy(iter);
08024 }
08025
08026 return 0;
08027 }
08028
08029
08030
08031
08032
08033 static int data_channeltypes_provider_handler(const struct ast_data_search *search,
08034 struct ast_data *data_root)
08035 {
08036 struct chanlist *cl;
08037 struct ast_data *data_type;
08038
08039 AST_RWLIST_RDLOCK(&backends);
08040 AST_RWLIST_TRAVERSE(&backends, cl, list) {
08041 data_type = ast_data_add_node(data_root, "type");
08042 if (!data_type) {
08043 continue;
08044 }
08045 ast_data_add_str(data_type, "name", cl->tech->type);
08046 ast_data_add_str(data_type, "description", cl->tech->description);
08047 ast_data_add_bool(data_type, "devicestate", cl->tech->devicestate ? 1 : 0);
08048 ast_data_add_bool(data_type, "indications", cl->tech->indicate ? 1 : 0);
08049 ast_data_add_bool(data_type, "transfer", cl->tech->transfer ? 1 : 0);
08050 ast_data_add_bool(data_type, "send_digit_begin", cl->tech->send_digit_begin ? 1 : 0);
08051 ast_data_add_bool(data_type, "send_digit_end", cl->tech->send_digit_end ? 1 : 0);
08052 ast_data_add_bool(data_type, "call", cl->tech->call ? 1 : 0);
08053 ast_data_add_bool(data_type, "hangup", cl->tech->hangup ? 1 : 0);
08054 ast_data_add_bool(data_type, "answer", cl->tech->answer ? 1 : 0);
08055 ast_data_add_bool(data_type, "read", cl->tech->read ? 1 : 0);
08056 ast_data_add_bool(data_type, "write", cl->tech->write ? 1 : 0);
08057 ast_data_add_bool(data_type, "send_text", cl->tech->send_text ? 1 : 0);
08058 ast_data_add_bool(data_type, "send_image", cl->tech->send_image ? 1 : 0);
08059 ast_data_add_bool(data_type, "send_html", cl->tech->send_html ? 1 : 0);
08060 ast_data_add_bool(data_type, "exception", cl->tech->exception ? 1 : 0);
08061 ast_data_add_bool(data_type, "bridge", cl->tech->bridge ? 1 : 0);
08062 ast_data_add_bool(data_type, "early_bridge", cl->tech->early_bridge ? 1 : 0);
08063 ast_data_add_bool(data_type, "fixup", cl->tech->fixup ? 1 : 0);
08064 ast_data_add_bool(data_type, "setoption", cl->tech->setoption ? 1 : 0);
08065 ast_data_add_bool(data_type, "queryoption", cl->tech->queryoption ? 1 : 0);
08066 ast_data_add_bool(data_type, "write_video", cl->tech->write_video ? 1 : 0);
08067 ast_data_add_bool(data_type, "write_text", cl->tech->write_text ? 1 : 0);
08068 ast_data_add_bool(data_type, "bridged_channel", cl->tech->bridged_channel ? 1 : 0);
08069 ast_data_add_bool(data_type, "func_channel_read", cl->tech->func_channel_read ? 1 : 0);
08070 ast_data_add_bool(data_type, "func_channel_write", cl->tech->func_channel_write ? 1 : 0);
08071 ast_data_add_bool(data_type, "get_base_channel", cl->tech->get_base_channel ? 1 : 0);
08072 ast_data_add_bool(data_type, "set_base_channel", cl->tech->set_base_channel ? 1 : 0);
08073 ast_data_add_bool(data_type, "get_pvt_uniqueid", cl->tech->get_pvt_uniqueid ? 1 : 0);
08074 ast_data_add_bool(data_type, "cc_callback", cl->tech->cc_callback ? 1 : 0);
08075
08076 ast_data_add_codecs(data_type, "capabilities", cl->tech->capabilities);
08077
08078 if (!ast_data_search_match(search, data_type)) {
08079 ast_data_remove_node(data_root, data_type);
08080 }
08081 }
08082 AST_RWLIST_UNLOCK(&backends);
08083
08084 return 0;
08085 }
08086
08087
08088
08089
08090
08091 static const struct ast_data_handler channels_provider = {
08092 .version = AST_DATA_HANDLER_VERSION,
08093 .get = data_channels_provider_handler
08094 };
08095
08096
08097
08098
08099
08100 static const struct ast_data_handler channeltypes_provider = {
08101 .version = AST_DATA_HANDLER_VERSION,
08102 .get = data_channeltypes_provider_handler
08103 };
08104
08105 static const struct ast_data_entry channel_providers[] = {
08106 AST_DATA_ENTRY("/asterisk/core/channels", &channels_provider),
08107 AST_DATA_ENTRY("/asterisk/core/channeltypes", &channeltypes_provider),
08108 };
08109
08110 static void channels_shutdown(void)
08111 {
08112 ast_data_unregister(NULL);
08113 if (channels) {
08114 ao2_ref(channels, -1);
08115 channels = NULL;
08116 }
08117 }
08118
08119 void ast_channels_init(void)
08120 {
08121 channels = ao2_container_alloc(NUM_CHANNEL_BUCKETS,
08122 ast_channel_hash_cb, ast_channel_cmp_cb);
08123
08124 ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
08125
08126 ast_data_register_multiple_core(channel_providers, ARRAY_LEN(channel_providers));
08127
08128 ast_plc_reload();
08129
08130 ast_register_atexit(channels_shutdown);
08131 }
08132
08133
08134 char *ast_print_group(char *buf, int buflen, ast_group_t group)
08135 {
08136 unsigned int i;
08137 int first = 1;
08138 char num[3];
08139
08140 buf[0] = '\0';
08141
08142 if (!group)
08143 return buf;
08144
08145 for (i = 0; i <= 63; i++) {
08146 if (group & ((ast_group_t) 1 << i)) {
08147 if (!first) {
08148 strncat(buf, ", ", buflen - strlen(buf) - 1);
08149 } else {
08150 first = 0;
08151 }
08152 snprintf(num, sizeof(num), "%u", i);
08153 strncat(buf, num, buflen - strlen(buf) - 1);
08154 }
08155 }
08156 return buf;
08157 }
08158
08159 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
08160 {
08161 struct ast_variable *cur;
08162
08163 for (cur = vars; cur; cur = cur->next)
08164 pbx_builtin_setvar_helper(chan, cur->name, cur->value);
08165 }
08166
08167 static void *silence_generator_alloc(struct ast_channel *chan, void *data)
08168 {
08169
08170 return data;
08171 }
08172
08173 static void silence_generator_release(struct ast_channel *chan, void *data)
08174 {
08175
08176 }
08177
08178 static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
08179 {
08180 short buf[samples];
08181 struct ast_frame frame = {
08182 .frametype = AST_FRAME_VOICE,
08183 .subclass.codec = AST_FORMAT_SLINEAR,
08184 .data.ptr = buf,
08185 .samples = samples,
08186 .datalen = sizeof(buf),
08187 };
08188
08189 memset(buf, 0, sizeof(buf));
08190
08191 if (ast_write(chan, &frame))
08192 return -1;
08193
08194 return 0;
08195 }
08196
08197 static struct ast_generator silence_generator = {
08198 .alloc = silence_generator_alloc,
08199 .release = silence_generator_release,
08200 .generate = silence_generator_generate,
08201 };
08202
08203 struct ast_silence_generator {
08204 int old_write_format;
08205 };
08206
08207 struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan)
08208 {
08209 struct ast_silence_generator *state;
08210
08211 if (!(state = ast_calloc(1, sizeof(*state)))) {
08212 return NULL;
08213 }
08214
08215 state->old_write_format = chan->writeformat;
08216
08217 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
08218 ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
08219 ast_free(state);
08220 return NULL;
08221 }
08222
08223 ast_activate_generator(chan, &silence_generator, state);
08224
08225 ast_debug(1, "Started silence generator on '%s'\n", chan->name);
08226
08227 return state;
08228 }
08229
08230 static int internal_deactivate_generator(struct ast_channel *chan, void* generator)
08231 {
08232 ast_channel_lock(chan);
08233
08234 if (!chan->generatordata) {
08235 ast_debug(1, "Trying to stop silence generator when there is no "
08236 "generator on '%s'\n", chan->name);
08237 ast_channel_unlock(chan);
08238 return 0;
08239 }
08240 if (chan->generator != generator) {
08241 ast_debug(1, "Trying to stop silence generator when it is not the current "
08242 "generator on '%s'\n", chan->name);
08243 ast_channel_unlock(chan);
08244 return 0;
08245 }
08246 if (chan->generator && chan->generator->release) {
08247 chan->generator->release(chan, chan->generatordata);
08248 }
08249 chan->generatordata = NULL;
08250 chan->generator = NULL;
08251 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
08252 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
08253 ast_settimeout(chan, 0, NULL, NULL);
08254 ast_channel_unlock(chan);
08255
08256 return 1;
08257 }
08258
08259 void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
08260 {
08261 if (!state)
08262 return;
08263
08264 if (internal_deactivate_generator(chan, &silence_generator)) {
08265 ast_debug(1, "Stopped silence generator on '%s'\n", chan->name);
08266
08267 if (ast_set_write_format(chan, state->old_write_format) < 0)
08268 ast_log(LOG_ERROR, "Could not return write format to its original state\n");
08269 }
08270 ast_free(state);
08271 }
08272
08273
08274
08275 const char *channelreloadreason2txt(enum channelreloadreason reason)
08276 {
08277 switch (reason) {
08278 case CHANNEL_MODULE_LOAD:
08279 return "LOAD (Channel module load)";
08280
08281 case CHANNEL_MODULE_RELOAD:
08282 return "RELOAD (Channel module reload)";
08283
08284 case CHANNEL_CLI_RELOAD:
08285 return "CLIRELOAD (Channel module reload by CLI command)";
08286
08287 default:
08288 return "MANAGERRELOAD (Channel module reload by manager)";
08289 }
08290 };
08291
08292
08293
08294
08295
08296
08297
08298
08299
08300 int ast_say_number(struct ast_channel *chan, int num,
08301 const char *ints, const char *language, const char *options)
08302 {
08303 return ast_say_number_full(chan, num, ints, language, options, -1, -1);
08304 }
08305
08306 int ast_say_enumeration(struct ast_channel *chan, int num,
08307 const char *ints, const char *language, const char *options)
08308 {
08309 return ast_say_enumeration_full(chan, num, ints, language, options, -1, -1);
08310 }
08311
08312 int ast_say_digits(struct ast_channel *chan, int num,
08313 const char *ints, const char *lang)
08314 {
08315 return ast_say_digits_full(chan, num, ints, lang, -1, -1);
08316 }
08317
08318 int ast_say_digit_str(struct ast_channel *chan, const char *str,
08319 const char *ints, const char *lang)
08320 {
08321 return ast_say_digit_str_full(chan, str, ints, lang, -1, -1);
08322 }
08323
08324 int ast_say_character_str(struct ast_channel *chan, const char *str,
08325 const char *ints, const char *lang)
08326 {
08327 return ast_say_character_str_full(chan, str, ints, lang, -1, -1);
08328 }
08329
08330 int ast_say_phonetic_str(struct ast_channel *chan, const char *str,
08331 const char *ints, const char *lang)
08332 {
08333 return ast_say_phonetic_str_full(chan, str, ints, lang, -1, -1);
08334 }
08335
08336 int ast_say_digits_full(struct ast_channel *chan, int num,
08337 const char *ints, const char *lang, int audiofd, int ctrlfd)
08338 {
08339 char buf[256];
08340
08341 snprintf(buf, sizeof(buf), "%d", num);
08342
08343 return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
08344 }
08345
08346 void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
08347 {
08348 ast_party_id_copy(&dest->id, &src->id);
08349 ast_party_id_copy(&dest->ani, &src->ani);
08350 dest->ani2 = src->ani2;
08351 }
08352
08353 void ast_connected_line_copy_to_caller(struct ast_party_caller *dest, const struct ast_party_connected_line *src)
08354 {
08355 ast_party_id_copy(&dest->id, &src->id);
08356 ast_party_id_copy(&dest->ani, &src->ani);
08357
08358 dest->ani2 = src->ani2;
08359 }
08360
08361 void ast_channel_set_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
08362 {
08363 if (&chan->connected == connected) {
08364
08365 return;
08366 }
08367
08368 ast_channel_lock(chan);
08369 ast_party_connected_line_set(&chan->connected, connected, update);
08370 ast_channel_unlock(chan);
08371 }
08372
08373
08374 struct ast_party_name_ies {
08375
08376 int str;
08377
08378 int char_set;
08379
08380 int presentation;
08381
08382 int valid;
08383 };
08384
08385
08386
08387
08388
08389
08390
08391
08392
08393
08394
08395
08396
08397
08398
08399 static int party_name_build_data(unsigned char *data, size_t datalen, const struct ast_party_name *name, const char *label, const struct ast_party_name_ies *ies)
08400 {
08401 size_t length;
08402 size_t pos = 0;
08403
08404
08405
08406
08407
08408 if (name->str) {
08409 length = strlen(name->str);
08410 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
08411 ast_log(LOG_WARNING, "No space left for %s name\n", label);
08412 return -1;
08413 }
08414 data[pos++] = ies->str;
08415 data[pos++] = length;
08416 memcpy(data + pos, name->str, length);
08417 pos += length;
08418 }
08419
08420 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08421 ast_log(LOG_WARNING, "No space left for %s name char set\n", label);
08422 return -1;
08423 }
08424 data[pos++] = ies->char_set;
08425 data[pos++] = 1;
08426 data[pos++] = name->char_set;
08427
08428 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08429 ast_log(LOG_WARNING, "No space left for %s name presentation\n", label);
08430 return -1;
08431 }
08432 data[pos++] = ies->presentation;
08433 data[pos++] = 1;
08434 data[pos++] = name->presentation;
08435
08436 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08437 ast_log(LOG_WARNING, "No space left for %s name valid\n", label);
08438 return -1;
08439 }
08440 data[pos++] = ies->valid;
08441 data[pos++] = 1;
08442 data[pos++] = name->valid;
08443
08444 return pos;
08445 }
08446
08447
08448 struct ast_party_number_ies {
08449
08450 int str;
08451
08452 int plan;
08453
08454 int presentation;
08455
08456 int valid;
08457 };
08458
08459
08460
08461
08462
08463
08464
08465
08466
08467
08468
08469
08470
08471
08472
08473 static int party_number_build_data(unsigned char *data, size_t datalen, const struct ast_party_number *number, const char *label, const struct ast_party_number_ies *ies)
08474 {
08475 size_t length;
08476 size_t pos = 0;
08477
08478
08479
08480
08481
08482 if (number->str) {
08483 length = strlen(number->str);
08484 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
08485 ast_log(LOG_WARNING, "No space left for %s number\n", label);
08486 return -1;
08487 }
08488 data[pos++] = ies->str;
08489 data[pos++] = length;
08490 memcpy(data + pos, number->str, length);
08491 pos += length;
08492 }
08493
08494 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08495 ast_log(LOG_WARNING, "No space left for %s numbering plan\n", label);
08496 return -1;
08497 }
08498 data[pos++] = ies->plan;
08499 data[pos++] = 1;
08500 data[pos++] = number->plan;
08501
08502 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08503 ast_log(LOG_WARNING, "No space left for %s number presentation\n", label);
08504 return -1;
08505 }
08506 data[pos++] = ies->presentation;
08507 data[pos++] = 1;
08508 data[pos++] = number->presentation;
08509
08510 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08511 ast_log(LOG_WARNING, "No space left for %s number valid\n", label);
08512 return -1;
08513 }
08514 data[pos++] = ies->valid;
08515 data[pos++] = 1;
08516 data[pos++] = number->valid;
08517
08518 return pos;
08519 }
08520
08521
08522 struct ast_party_subaddress_ies {
08523
08524 int str;
08525
08526 int type;
08527
08528 int odd_even_indicator;
08529
08530 int valid;
08531 };
08532
08533
08534
08535
08536
08537
08538
08539
08540
08541
08542
08543
08544
08545
08546
08547 static int party_subaddress_build_data(unsigned char *data, size_t datalen, const struct ast_party_subaddress *subaddress, const char *label, const struct ast_party_subaddress_ies *ies)
08548 {
08549 size_t length;
08550 size_t pos = 0;
08551
08552
08553
08554
08555
08556 if (subaddress->str) {
08557 length = strlen(subaddress->str);
08558 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
08559 ast_log(LOG_WARNING, "No space left for %s subaddress\n", label);
08560 return -1;
08561 }
08562 data[pos++] = ies->str;
08563 data[pos++] = length;
08564 memcpy(data + pos, subaddress->str, length);
08565 pos += length;
08566 }
08567
08568 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08569 ast_log(LOG_WARNING, "No space left for %s type of subaddress\n", label);
08570 return -1;
08571 }
08572 data[pos++] = ies->type;
08573 data[pos++] = 1;
08574 data[pos++] = subaddress->type;
08575
08576 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08577 ast_log(LOG_WARNING,
08578 "No space left for %s subaddress odd-even indicator\n", label);
08579 return -1;
08580 }
08581 data[pos++] = ies->odd_even_indicator;
08582 data[pos++] = 1;
08583 data[pos++] = subaddress->odd_even_indicator;
08584
08585 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08586 ast_log(LOG_WARNING, "No space left for %s subaddress valid\n", label);
08587 return -1;
08588 }
08589 data[pos++] = ies->valid;
08590 data[pos++] = 1;
08591 data[pos++] = subaddress->valid;
08592
08593 return pos;
08594 }
08595
08596
08597 struct ast_party_id_ies {
08598
08599 struct ast_party_name_ies name;
08600
08601 struct ast_party_number_ies number;
08602
08603 struct ast_party_subaddress_ies subaddress;
08604
08605 int tag;
08606
08607 int combined_presentation;
08608 };
08609
08610
08611
08612
08613
08614
08615
08616
08617
08618
08619
08620
08621
08622
08623
08624
08625 static int party_id_build_data(unsigned char *data, size_t datalen,
08626 const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies,
08627 const struct ast_set_party_id *update)
08628 {
08629 size_t length;
08630 size_t pos = 0;
08631 int res;
08632
08633
08634
08635
08636
08637
08638 if (!update || update->name) {
08639 res = party_name_build_data(data + pos, datalen - pos, &id->name, label,
08640 &ies->name);
08641 if (res < 0) {
08642 return -1;
08643 }
08644 pos += res;
08645 }
08646
08647 if (!update || update->number) {
08648 res = party_number_build_data(data + pos, datalen - pos, &id->number, label,
08649 &ies->number);
08650 if (res < 0) {
08651 return -1;
08652 }
08653 pos += res;
08654 }
08655
08656 if (!update || update->subaddress) {
08657 res = party_subaddress_build_data(data + pos, datalen - pos, &id->subaddress,
08658 label, &ies->subaddress);
08659 if (res < 0) {
08660 return -1;
08661 }
08662 pos += res;
08663 }
08664
08665
08666 if (id->tag) {
08667 length = strlen(id->tag);
08668 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
08669 ast_log(LOG_WARNING, "No space left for %s tag\n", label);
08670 return -1;
08671 }
08672 data[pos++] = ies->tag;
08673 data[pos++] = length;
08674 memcpy(data + pos, id->tag, length);
08675 pos += length;
08676 }
08677
08678
08679 if (!update || update->number) {
08680 int presentation;
08681
08682 if (!update || update->name) {
08683 presentation = ast_party_id_presentation(id);
08684 } else {
08685
08686
08687
08688
08689
08690 presentation = id->number.presentation;
08691 }
08692
08693 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08694 ast_log(LOG_WARNING, "No space left for %s combined presentation\n", label);
08695 return -1;
08696 }
08697 data[pos++] = ies->combined_presentation;
08698 data[pos++] = 1;
08699 data[pos++] = presentation;
08700 }
08701
08702 return pos;
08703 }
08704
08705
08706
08707
08708
08709 enum {
08710 AST_CONNECTED_LINE_NUMBER,
08711 AST_CONNECTED_LINE_NAME,
08712 AST_CONNECTED_LINE_NUMBER_PLAN,
08713 AST_CONNECTED_LINE_ID_PRESENTATION,
08714 AST_CONNECTED_LINE_SOURCE,
08715 AST_CONNECTED_LINE_SUBADDRESS,
08716 AST_CONNECTED_LINE_SUBADDRESS_TYPE,
08717 AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
08718 AST_CONNECTED_LINE_SUBADDRESS_VALID,
08719 AST_CONNECTED_LINE_TAG,
08720 AST_CONNECTED_LINE_VERSION,
08721 AST_CONNECTED_LINE_NAME_VALID,
08722 AST_CONNECTED_LINE_NAME_CHAR_SET,
08723 AST_CONNECTED_LINE_NAME_PRESENTATION,
08724 AST_CONNECTED_LINE_NUMBER_VALID,
08725 AST_CONNECTED_LINE_NUMBER_PRESENTATION,
08726 };
08727
08728 int ast_connected_line_build_data(unsigned char *data, size_t datalen, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
08729 {
08730 int32_t value;
08731 size_t pos = 0;
08732 int res;
08733
08734 static const struct ast_party_id_ies ies = {
08735 .name.str = AST_CONNECTED_LINE_NAME,
08736 .name.char_set = AST_CONNECTED_LINE_NAME_CHAR_SET,
08737 .name.presentation = AST_CONNECTED_LINE_NAME_PRESENTATION,
08738 .name.valid = AST_CONNECTED_LINE_NAME_VALID,
08739
08740 .number.str = AST_CONNECTED_LINE_NUMBER,
08741 .number.plan = AST_CONNECTED_LINE_NUMBER_PLAN,
08742 .number.presentation = AST_CONNECTED_LINE_NUMBER_PRESENTATION,
08743 .number.valid = AST_CONNECTED_LINE_NUMBER_VALID,
08744
08745 .subaddress.str = AST_CONNECTED_LINE_SUBADDRESS,
08746 .subaddress.type = AST_CONNECTED_LINE_SUBADDRESS_TYPE,
08747 .subaddress.odd_even_indicator = AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
08748 .subaddress.valid = AST_CONNECTED_LINE_SUBADDRESS_VALID,
08749
08750 .tag = AST_CONNECTED_LINE_TAG,
08751 .combined_presentation = AST_CONNECTED_LINE_ID_PRESENTATION,
08752 };
08753
08754
08755
08756
08757
08758
08759
08760 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
08761 ast_log(LOG_WARNING, "No space left for connected line frame version\n");
08762 return -1;
08763 }
08764 data[pos++] = AST_CONNECTED_LINE_VERSION;
08765 data[pos++] = 1;
08766 data[pos++] = 2;
08767
08768 res = party_id_build_data(data + pos, datalen - pos, &connected->id,
08769 "connected line", &ies, update ? &update->id : NULL);
08770 if (res < 0) {
08771 return -1;
08772 }
08773 pos += res;
08774
08775
08776 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
08777 ast_log(LOG_WARNING, "No space left for connected line source\n");
08778 return -1;
08779 }
08780 data[pos++] = AST_CONNECTED_LINE_SOURCE;
08781 data[pos++] = sizeof(value);
08782 value = htonl(connected->source);
08783 memcpy(data + pos, &value, sizeof(value));
08784 pos += sizeof(value);
08785
08786 return pos;
08787 }
08788
08789 int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
08790 {
08791 size_t pos;
08792 unsigned char ie_len;
08793 unsigned char ie_id;
08794 int32_t value;
08795 int frame_version = 1;
08796 int combined_presentation = 0;
08797 int got_combined_presentation = 0;
08798
08799 for (pos = 0; pos < datalen; pos += ie_len) {
08800 if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
08801 ast_log(LOG_WARNING, "Invalid connected line update\n");
08802 return -1;
08803 }
08804 ie_id = data[pos++];
08805 ie_len = data[pos++];
08806 if (datalen < pos + ie_len) {
08807 ast_log(LOG_WARNING, "Invalid connected line update\n");
08808 return -1;
08809 }
08810
08811 switch (ie_id) {
08812
08813 case AST_CONNECTED_LINE_VERSION:
08814 if (ie_len != 1) {
08815 ast_log(LOG_WARNING, "Invalid connected line frame version (%u)\n",
08816 (unsigned) ie_len);
08817 break;
08818 }
08819 frame_version = data[pos];
08820 break;
08821
08822 case AST_CONNECTED_LINE_NAME:
08823 ast_free(connected->id.name.str);
08824 connected->id.name.str = ast_malloc(ie_len + 1);
08825 if (connected->id.name.str) {
08826 memcpy(connected->id.name.str, data + pos, ie_len);
08827 connected->id.name.str[ie_len] = 0;
08828 }
08829 break;
08830 case AST_CONNECTED_LINE_NAME_CHAR_SET:
08831 if (ie_len != 1) {
08832 ast_log(LOG_WARNING, "Invalid connected line name char set (%u)\n",
08833 (unsigned) ie_len);
08834 break;
08835 }
08836 connected->id.name.char_set = data[pos];
08837 break;
08838 case AST_CONNECTED_LINE_NAME_PRESENTATION:
08839 if (ie_len != 1) {
08840 ast_log(LOG_WARNING, "Invalid connected line name presentation (%u)\n",
08841 (unsigned) ie_len);
08842 break;
08843 }
08844 connected->id.name.presentation = data[pos];
08845 break;
08846 case AST_CONNECTED_LINE_NAME_VALID:
08847 if (ie_len != 1) {
08848 ast_log(LOG_WARNING, "Invalid connected line name valid (%u)\n",
08849 (unsigned) ie_len);
08850 break;
08851 }
08852 connected->id.name.valid = data[pos];
08853 break;
08854
08855 case AST_CONNECTED_LINE_NUMBER:
08856 ast_free(connected->id.number.str);
08857 connected->id.number.str = ast_malloc(ie_len + 1);
08858 if (connected->id.number.str) {
08859 memcpy(connected->id.number.str, data + pos, ie_len);
08860 connected->id.number.str[ie_len] = 0;
08861 }
08862 break;
08863 case AST_CONNECTED_LINE_NUMBER_PLAN:
08864 if (ie_len != 1) {
08865 ast_log(LOG_WARNING, "Invalid connected line numbering plan (%u)\n",
08866 (unsigned) ie_len);
08867 break;
08868 }
08869 connected->id.number.plan = data[pos];
08870 break;
08871 case AST_CONNECTED_LINE_NUMBER_PRESENTATION:
08872 if (ie_len != 1) {
08873 ast_log(LOG_WARNING, "Invalid connected line number presentation (%u)\n",
08874 (unsigned) ie_len);
08875 break;
08876 }
08877 connected->id.number.presentation = data[pos];
08878 break;
08879 case AST_CONNECTED_LINE_NUMBER_VALID:
08880 if (ie_len != 1) {
08881 ast_log(LOG_WARNING, "Invalid connected line number valid (%u)\n",
08882 (unsigned) ie_len);
08883 break;
08884 }
08885 connected->id.number.valid = data[pos];
08886 break;
08887
08888 case AST_CONNECTED_LINE_ID_PRESENTATION:
08889 if (ie_len != 1) {
08890 ast_log(LOG_WARNING, "Invalid connected line combined presentation (%u)\n",
08891 (unsigned) ie_len);
08892 break;
08893 }
08894 combined_presentation = data[pos];
08895 got_combined_presentation = 1;
08896 break;
08897
08898 case AST_CONNECTED_LINE_SUBADDRESS:
08899 ast_free(connected->id.subaddress.str);
08900 connected->id.subaddress.str = ast_malloc(ie_len + 1);
08901 if (connected->id.subaddress.str) {
08902 memcpy(connected->id.subaddress.str, data + pos, ie_len);
08903 connected->id.subaddress.str[ie_len] = 0;
08904 }
08905 break;
08906 case AST_CONNECTED_LINE_SUBADDRESS_TYPE:
08907 if (ie_len != 1) {
08908 ast_log(LOG_WARNING, "Invalid connected line type of subaddress (%u)\n",
08909 (unsigned) ie_len);
08910 break;
08911 }
08912 connected->id.subaddress.type = data[pos];
08913 break;
08914 case AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN:
08915 if (ie_len != 1) {
08916 ast_log(LOG_WARNING,
08917 "Invalid connected line subaddress odd-even indicator (%u)\n",
08918 (unsigned) ie_len);
08919 break;
08920 }
08921 connected->id.subaddress.odd_even_indicator = data[pos];
08922 break;
08923 case AST_CONNECTED_LINE_SUBADDRESS_VALID:
08924 if (ie_len != 1) {
08925 ast_log(LOG_WARNING, "Invalid connected line subaddress valid (%u)\n",
08926 (unsigned) ie_len);
08927 break;
08928 }
08929 connected->id.subaddress.valid = data[pos];
08930 break;
08931
08932 case AST_CONNECTED_LINE_TAG:
08933 ast_free(connected->id.tag);
08934 connected->id.tag = ast_malloc(ie_len + 1);
08935 if (connected->id.tag) {
08936 memcpy(connected->id.tag, data + pos, ie_len);
08937 connected->id.tag[ie_len] = 0;
08938 }
08939 break;
08940
08941 case AST_CONNECTED_LINE_SOURCE:
08942 if (ie_len != sizeof(value)) {
08943 ast_log(LOG_WARNING, "Invalid connected line source (%u)\n",
08944 (unsigned) ie_len);
08945 break;
08946 }
08947 memcpy(&value, data + pos, sizeof(value));
08948 connected->source = ntohl(value);
08949 break;
08950
08951 default:
08952 ast_log(LOG_DEBUG, "Unknown connected line element: %u (%u)\n",
08953 (unsigned) ie_id, (unsigned) ie_len);
08954 break;
08955 }
08956 }
08957
08958 switch (frame_version) {
08959 case 1:
08960
08961
08962
08963
08964 connected->id.name.valid = 1;
08965 connected->id.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
08966 connected->id.number.valid = 1;
08967 if (got_combined_presentation) {
08968 connected->id.name.presentation = combined_presentation;
08969 connected->id.number.presentation = combined_presentation;
08970 }
08971 break;
08972 case 2:
08973
08974 break;
08975 default:
08976
08977
08978
08979
08980 ast_log(LOG_DEBUG, "Connected line frame has newer version: %u\n",
08981 (unsigned) frame_version);
08982 break;
08983 }
08984
08985 return 0;
08986 }
08987
08988 void ast_channel_update_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
08989 {
08990 unsigned char data[1024];
08991 size_t datalen;
08992
08993 datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
08994 if (datalen == (size_t) -1) {
08995 return;
08996 }
08997
08998 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
08999 }
09000
09001 void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
09002 {
09003 unsigned char data[1024];
09004 size_t datalen;
09005
09006 datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
09007 if (datalen == (size_t) -1) {
09008 return;
09009 }
09010
09011 ast_queue_control_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
09012 }
09013
09014 void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
09015 {
09016 if (&chan->redirecting == redirecting) {
09017
09018 return;
09019 }
09020
09021 ast_channel_lock(chan);
09022 ast_party_redirecting_set(&chan->redirecting, redirecting, update);
09023 ast_channel_unlock(chan);
09024 }
09025
09026
09027
09028
09029
09030 enum {
09031 AST_REDIRECTING_FROM_NUMBER,
09032 AST_REDIRECTING_FROM_NAME,
09033 AST_REDIRECTING_FROM_NUMBER_PLAN,
09034 AST_REDIRECTING_FROM_ID_PRESENTATION,
09035 AST_REDIRECTING_TO_NUMBER,
09036 AST_REDIRECTING_TO_NAME,
09037 AST_REDIRECTING_TO_NUMBER_PLAN,
09038 AST_REDIRECTING_TO_ID_PRESENTATION,
09039 AST_REDIRECTING_REASON,
09040 AST_REDIRECTING_COUNT,
09041 AST_REDIRECTING_FROM_SUBADDRESS,
09042 AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
09043 AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
09044 AST_REDIRECTING_FROM_SUBADDRESS_VALID,
09045 AST_REDIRECTING_TO_SUBADDRESS,
09046 AST_REDIRECTING_TO_SUBADDRESS_TYPE,
09047 AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
09048 AST_REDIRECTING_TO_SUBADDRESS_VALID,
09049 AST_REDIRECTING_FROM_TAG,
09050 AST_REDIRECTING_TO_TAG,
09051 AST_REDIRECTING_VERSION,
09052 AST_REDIRECTING_FROM_NAME_VALID,
09053 AST_REDIRECTING_FROM_NAME_CHAR_SET,
09054 AST_REDIRECTING_FROM_NAME_PRESENTATION,
09055 AST_REDIRECTING_FROM_NUMBER_VALID,
09056 AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
09057 AST_REDIRECTING_TO_NAME_VALID,
09058 AST_REDIRECTING_TO_NAME_CHAR_SET,
09059 AST_REDIRECTING_TO_NAME_PRESENTATION,
09060 AST_REDIRECTING_TO_NUMBER_VALID,
09061 AST_REDIRECTING_TO_NUMBER_PRESENTATION,
09062 };
09063
09064 int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
09065 {
09066 int32_t value;
09067 size_t pos = 0;
09068 int res;
09069
09070 static const struct ast_party_id_ies from_ies = {
09071 .name.str = AST_REDIRECTING_FROM_NAME,
09072 .name.char_set = AST_REDIRECTING_FROM_NAME_CHAR_SET,
09073 .name.presentation = AST_REDIRECTING_FROM_NAME_PRESENTATION,
09074 .name.valid = AST_REDIRECTING_FROM_NAME_VALID,
09075
09076 .number.str = AST_REDIRECTING_FROM_NUMBER,
09077 .number.plan = AST_REDIRECTING_FROM_NUMBER_PLAN,
09078 .number.presentation = AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
09079 .number.valid = AST_REDIRECTING_FROM_NUMBER_VALID,
09080
09081 .subaddress.str = AST_REDIRECTING_FROM_SUBADDRESS,
09082 .subaddress.type = AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
09083 .subaddress.odd_even_indicator = AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
09084 .subaddress.valid = AST_REDIRECTING_FROM_SUBADDRESS_VALID,
09085
09086 .tag = AST_REDIRECTING_FROM_TAG,
09087 .combined_presentation = AST_REDIRECTING_FROM_ID_PRESENTATION,
09088 };
09089 static const struct ast_party_id_ies to_ies = {
09090 .name.str = AST_REDIRECTING_TO_NAME,
09091 .name.char_set = AST_REDIRECTING_TO_NAME_CHAR_SET,
09092 .name.presentation = AST_REDIRECTING_TO_NAME_PRESENTATION,
09093 .name.valid = AST_REDIRECTING_TO_NAME_VALID,
09094
09095 .number.str = AST_REDIRECTING_TO_NUMBER,
09096 .number.plan = AST_REDIRECTING_TO_NUMBER_PLAN,
09097 .number.presentation = AST_REDIRECTING_TO_NUMBER_PRESENTATION,
09098 .number.valid = AST_REDIRECTING_TO_NUMBER_VALID,
09099
09100 .subaddress.str = AST_REDIRECTING_TO_SUBADDRESS,
09101 .subaddress.type = AST_REDIRECTING_TO_SUBADDRESS_TYPE,
09102 .subaddress.odd_even_indicator = AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
09103 .subaddress.valid = AST_REDIRECTING_TO_SUBADDRESS_VALID,
09104
09105 .tag = AST_REDIRECTING_TO_TAG,
09106 .combined_presentation = AST_REDIRECTING_TO_ID_PRESENTATION,
09107 };
09108
09109
09110 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
09111 ast_log(LOG_WARNING, "No space left for redirecting frame version\n");
09112 return -1;
09113 }
09114 data[pos++] = AST_REDIRECTING_VERSION;
09115 data[pos++] = 1;
09116 data[pos++] = 2;
09117
09118 res = party_id_build_data(data + pos, datalen - pos, &redirecting->from,
09119 "redirecting-from", &from_ies, update ? &update->from : NULL);
09120 if (res < 0) {
09121 return -1;
09122 }
09123 pos += res;
09124
09125 res = party_id_build_data(data + pos, datalen - pos, &redirecting->to,
09126 "redirecting-to", &to_ies, update ? &update->to : NULL);
09127 if (res < 0) {
09128 return -1;
09129 }
09130 pos += res;
09131
09132
09133 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
09134 ast_log(LOG_WARNING, "No space left for redirecting reason\n");
09135 return -1;
09136 }
09137 data[pos++] = AST_REDIRECTING_REASON;
09138 data[pos++] = sizeof(value);
09139 value = htonl(redirecting->reason);
09140 memcpy(data + pos, &value, sizeof(value));
09141 pos += sizeof(value);
09142
09143
09144 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
09145 ast_log(LOG_WARNING, "No space left for redirecting count\n");
09146 return -1;
09147 }
09148 data[pos++] = AST_REDIRECTING_COUNT;
09149 data[pos++] = sizeof(value);
09150 value = htonl(redirecting->count);
09151 memcpy(data + pos, &value, sizeof(value));
09152 pos += sizeof(value);
09153
09154 return pos;
09155 }
09156
09157 int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
09158 {
09159 size_t pos;
09160 unsigned char ie_len;
09161 unsigned char ie_id;
09162 int32_t value;
09163 int frame_version = 1;
09164 int from_combined_presentation = 0;
09165 int got_from_combined_presentation = 0;
09166 int to_combined_presentation = 0;
09167 int got_to_combined_presentation = 0;
09168
09169 for (pos = 0; pos < datalen; pos += ie_len) {
09170 if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
09171 ast_log(LOG_WARNING, "Invalid redirecting update\n");
09172 return -1;
09173 }
09174 ie_id = data[pos++];
09175 ie_len = data[pos++];
09176 if (datalen < pos + ie_len) {
09177 ast_log(LOG_WARNING, "Invalid redirecting update\n");
09178 return -1;
09179 }
09180
09181 switch (ie_id) {
09182
09183 case AST_REDIRECTING_VERSION:
09184 if (ie_len != 1) {
09185 ast_log(LOG_WARNING, "Invalid redirecting frame version (%u)\n",
09186 (unsigned) ie_len);
09187 break;
09188 }
09189 frame_version = data[pos];
09190 break;
09191
09192 case AST_REDIRECTING_FROM_NAME:
09193 ast_free(redirecting->from.name.str);
09194 redirecting->from.name.str = ast_malloc(ie_len + 1);
09195 if (redirecting->from.name.str) {
09196 memcpy(redirecting->from.name.str, data + pos, ie_len);
09197 redirecting->from.name.str[ie_len] = 0;
09198 }
09199 break;
09200 case AST_REDIRECTING_FROM_NAME_CHAR_SET:
09201 if (ie_len != 1) {
09202 ast_log(LOG_WARNING, "Invalid redirecting-from name char set (%u)\n",
09203 (unsigned) ie_len);
09204 break;
09205 }
09206 redirecting->from.name.char_set = data[pos];
09207 break;
09208 case AST_REDIRECTING_FROM_NAME_PRESENTATION:
09209 if (ie_len != 1) {
09210 ast_log(LOG_WARNING, "Invalid redirecting-from name presentation (%u)\n",
09211 (unsigned) ie_len);
09212 break;
09213 }
09214 redirecting->from.name.presentation = data[pos];
09215 break;
09216 case AST_REDIRECTING_FROM_NAME_VALID:
09217 if (ie_len != 1) {
09218 ast_log(LOG_WARNING, "Invalid redirecting-from name valid (%u)\n",
09219 (unsigned) ie_len);
09220 break;
09221 }
09222 redirecting->from.name.valid = data[pos];
09223 break;
09224
09225 case AST_REDIRECTING_FROM_NUMBER:
09226 ast_free(redirecting->from.number.str);
09227 redirecting->from.number.str = ast_malloc(ie_len + 1);
09228 if (redirecting->from.number.str) {
09229 memcpy(redirecting->from.number.str, data + pos, ie_len);
09230 redirecting->from.number.str[ie_len] = 0;
09231 }
09232 break;
09233 case AST_REDIRECTING_FROM_NUMBER_PLAN:
09234 if (ie_len != 1) {
09235 ast_log(LOG_WARNING, "Invalid redirecting-from numbering plan (%u)\n",
09236 (unsigned) ie_len);
09237 break;
09238 }
09239 redirecting->from.number.plan = data[pos];
09240 break;
09241 case AST_REDIRECTING_FROM_NUMBER_PRESENTATION:
09242 if (ie_len != 1) {
09243 ast_log(LOG_WARNING, "Invalid redirecting-from number presentation (%u)\n",
09244 (unsigned) ie_len);
09245 break;
09246 }
09247 redirecting->from.number.presentation = data[pos];
09248 break;
09249 case AST_REDIRECTING_FROM_NUMBER_VALID:
09250 if (ie_len != 1) {
09251 ast_log(LOG_WARNING, "Invalid redirecting-from number valid (%u)\n",
09252 (unsigned) ie_len);
09253 break;
09254 }
09255 redirecting->from.number.valid = data[pos];
09256 break;
09257
09258 case AST_REDIRECTING_FROM_ID_PRESENTATION:
09259 if (ie_len != 1) {
09260 ast_log(LOG_WARNING, "Invalid redirecting-from combined presentation (%u)\n",
09261 (unsigned) ie_len);
09262 break;
09263 }
09264 from_combined_presentation = data[pos];
09265 got_from_combined_presentation = 1;
09266 break;
09267
09268 case AST_REDIRECTING_FROM_SUBADDRESS:
09269 ast_free(redirecting->from.subaddress.str);
09270 redirecting->from.subaddress.str = ast_malloc(ie_len + 1);
09271 if (redirecting->from.subaddress.str) {
09272 memcpy(redirecting->from.subaddress.str, data + pos, ie_len);
09273 redirecting->from.subaddress.str[ie_len] = 0;
09274 }
09275 break;
09276 case AST_REDIRECTING_FROM_SUBADDRESS_TYPE:
09277 if (ie_len != 1) {
09278 ast_log(LOG_WARNING, "Invalid redirecting-from type of subaddress (%u)\n",
09279 (unsigned) ie_len);
09280 break;
09281 }
09282 redirecting->from.subaddress.type = data[pos];
09283 break;
09284 case AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN:
09285 if (ie_len != 1) {
09286 ast_log(LOG_WARNING,
09287 "Invalid redirecting-from subaddress odd-even indicator (%u)\n",
09288 (unsigned) ie_len);
09289 break;
09290 }
09291 redirecting->from.subaddress.odd_even_indicator = data[pos];
09292 break;
09293 case AST_REDIRECTING_FROM_SUBADDRESS_VALID:
09294 if (ie_len != 1) {
09295 ast_log(LOG_WARNING, "Invalid redirecting-from subaddress valid (%u)\n",
09296 (unsigned) ie_len);
09297 break;
09298 }
09299 redirecting->from.subaddress.valid = data[pos];
09300 break;
09301
09302 case AST_REDIRECTING_FROM_TAG:
09303 ast_free(redirecting->from.tag);
09304 redirecting->from.tag = ast_malloc(ie_len + 1);
09305 if (redirecting->from.tag) {
09306 memcpy(redirecting->from.tag, data + pos, ie_len);
09307 redirecting->from.tag[ie_len] = 0;
09308 }
09309 break;
09310
09311 case AST_REDIRECTING_TO_NAME:
09312 ast_free(redirecting->to.name.str);
09313 redirecting->to.name.str = ast_malloc(ie_len + 1);
09314 if (redirecting->to.name.str) {
09315 memcpy(redirecting->to.name.str, data + pos, ie_len);
09316 redirecting->to.name.str[ie_len] = 0;
09317 }
09318 break;
09319 case AST_REDIRECTING_TO_NAME_CHAR_SET:
09320 if (ie_len != 1) {
09321 ast_log(LOG_WARNING, "Invalid redirecting-to name char set (%u)\n",
09322 (unsigned) ie_len);
09323 break;
09324 }
09325 redirecting->to.name.char_set = data[pos];
09326 break;
09327 case AST_REDIRECTING_TO_NAME_PRESENTATION:
09328 if (ie_len != 1) {
09329 ast_log(LOG_WARNING, "Invalid redirecting-to name presentation (%u)\n",
09330 (unsigned) ie_len);
09331 break;
09332 }
09333 redirecting->to.name.presentation = data[pos];
09334 break;
09335 case AST_REDIRECTING_TO_NAME_VALID:
09336 if (ie_len != 1) {
09337 ast_log(LOG_WARNING, "Invalid redirecting-to name valid (%u)\n",
09338 (unsigned) ie_len);
09339 break;
09340 }
09341 redirecting->to.name.valid = data[pos];
09342 break;
09343
09344 case AST_REDIRECTING_TO_NUMBER:
09345 ast_free(redirecting->to.number.str);
09346 redirecting->to.number.str = ast_malloc(ie_len + 1);
09347 if (redirecting->to.number.str) {
09348 memcpy(redirecting->to.number.str, data + pos, ie_len);
09349 redirecting->to.number.str[ie_len] = 0;
09350 }
09351 break;
09352 case AST_REDIRECTING_TO_NUMBER_PLAN:
09353 if (ie_len != 1) {
09354 ast_log(LOG_WARNING, "Invalid redirecting-to numbering plan (%u)\n",
09355 (unsigned) ie_len);
09356 break;
09357 }
09358 redirecting->to.number.plan = data[pos];
09359 break;
09360 case AST_REDIRECTING_TO_NUMBER_PRESENTATION:
09361 if (ie_len != 1) {
09362 ast_log(LOG_WARNING, "Invalid redirecting-to number presentation (%u)\n",
09363 (unsigned) ie_len);
09364 break;
09365 }
09366 redirecting->to.number.presentation = data[pos];
09367 break;
09368 case AST_REDIRECTING_TO_NUMBER_VALID:
09369 if (ie_len != 1) {
09370 ast_log(LOG_WARNING, "Invalid redirecting-to number valid (%u)\n",
09371 (unsigned) ie_len);
09372 break;
09373 }
09374 redirecting->to.number.valid = data[pos];
09375 break;
09376
09377 case AST_REDIRECTING_TO_ID_PRESENTATION:
09378 if (ie_len != 1) {
09379 ast_log(LOG_WARNING, "Invalid redirecting-to combined presentation (%u)\n",
09380 (unsigned) ie_len);
09381 break;
09382 }
09383 to_combined_presentation = data[pos];
09384 got_to_combined_presentation = 1;
09385 break;
09386
09387 case AST_REDIRECTING_TO_SUBADDRESS:
09388 ast_free(redirecting->to.subaddress.str);
09389 redirecting->to.subaddress.str = ast_malloc(ie_len + 1);
09390 if (redirecting->to.subaddress.str) {
09391 memcpy(redirecting->to.subaddress.str, data + pos, ie_len);
09392 redirecting->to.subaddress.str[ie_len] = 0;
09393 }
09394 break;
09395 case AST_REDIRECTING_TO_SUBADDRESS_TYPE:
09396 if (ie_len != 1) {
09397 ast_log(LOG_WARNING, "Invalid redirecting-to type of subaddress (%u)\n",
09398 (unsigned) ie_len);
09399 break;
09400 }
09401 redirecting->to.subaddress.type = data[pos];
09402 break;
09403 case AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN:
09404 if (ie_len != 1) {
09405 ast_log(LOG_WARNING,
09406 "Invalid redirecting-to subaddress odd-even indicator (%u)\n",
09407 (unsigned) ie_len);
09408 break;
09409 }
09410 redirecting->to.subaddress.odd_even_indicator = data[pos];
09411 break;
09412 case AST_REDIRECTING_TO_SUBADDRESS_VALID:
09413 if (ie_len != 1) {
09414 ast_log(LOG_WARNING, "Invalid redirecting-to subaddress valid (%u)\n",
09415 (unsigned) ie_len);
09416 break;
09417 }
09418 redirecting->to.subaddress.valid = data[pos];
09419 break;
09420
09421 case AST_REDIRECTING_TO_TAG:
09422 ast_free(redirecting->to.tag);
09423 redirecting->to.tag = ast_malloc(ie_len + 1);
09424 if (redirecting->to.tag) {
09425 memcpy(redirecting->to.tag, data + pos, ie_len);
09426 redirecting->to.tag[ie_len] = 0;
09427 }
09428 break;
09429
09430 case AST_REDIRECTING_REASON:
09431 if (ie_len != sizeof(value)) {
09432 ast_log(LOG_WARNING, "Invalid redirecting reason (%u)\n",
09433 (unsigned) ie_len);
09434 break;
09435 }
09436 memcpy(&value, data + pos, sizeof(value));
09437 redirecting->reason = ntohl(value);
09438 break;
09439
09440 case AST_REDIRECTING_COUNT:
09441 if (ie_len != sizeof(value)) {
09442 ast_log(LOG_WARNING, "Invalid redirecting count (%u)\n",
09443 (unsigned) ie_len);
09444 break;
09445 }
09446 memcpy(&value, data + pos, sizeof(value));
09447 redirecting->count = ntohl(value);
09448 break;
09449
09450 default:
09451 ast_log(LOG_DEBUG, "Unknown redirecting element: %u (%u)\n",
09452 (unsigned) ie_id, (unsigned) ie_len);
09453 break;
09454 }
09455 }
09456
09457 switch (frame_version) {
09458 case 1:
09459
09460
09461
09462
09463 redirecting->from.name.valid = 1;
09464 redirecting->from.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
09465 redirecting->from.number.valid = 1;
09466 if (got_from_combined_presentation) {
09467 redirecting->from.name.presentation = from_combined_presentation;
09468 redirecting->from.number.presentation = from_combined_presentation;
09469 }
09470
09471 redirecting->to.name.valid = 1;
09472 redirecting->to.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
09473 redirecting->to.number.valid = 1;
09474 if (got_to_combined_presentation) {
09475 redirecting->to.name.presentation = to_combined_presentation;
09476 redirecting->to.number.presentation = to_combined_presentation;
09477 }
09478 break;
09479 case 2:
09480
09481 break;
09482 default:
09483
09484
09485
09486
09487 ast_log(LOG_DEBUG, "Redirecting frame has newer version: %u\n",
09488 (unsigned) frame_version);
09489 break;
09490 }
09491
09492 return 0;
09493 }
09494
09495 void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
09496 {
09497 unsigned char data[1024];
09498 size_t datalen;
09499
09500 datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
09501 if (datalen == (size_t) -1) {
09502 return;
09503 }
09504
09505 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
09506 }
09507
09508 void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
09509 {
09510 unsigned char data[1024];
09511 size_t datalen;
09512
09513 datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
09514 if (datalen == (size_t) -1) {
09515 return;
09516 }
09517
09518 ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
09519 }
09520
09521 int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int is_caller, int is_frame)
09522 {
09523 const char *macro;
09524 const char *macro_args;
09525 int retval;
09526
09527 ast_channel_lock(macro_chan);
09528 macro = pbx_builtin_getvar_helper(macro_chan, is_caller
09529 ? "CONNECTED_LINE_CALLER_SEND_MACRO" : "CONNECTED_LINE_CALLEE_SEND_MACRO");
09530 macro = ast_strdupa(S_OR(macro, ""));
09531 macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
09532 ? "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS" : "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS");
09533 macro_args = ast_strdupa(S_OR(macro_args, ""));
09534
09535 if (ast_strlen_zero(macro)) {
09536 ast_channel_unlock(macro_chan);
09537 return -1;
09538 }
09539
09540 if (is_frame) {
09541 const struct ast_frame *frame = connected_info;
09542
09543 ast_connected_line_parse_data(frame->data.ptr, frame->datalen, ¯o_chan->connected);
09544 } else {
09545 const struct ast_party_connected_line *connected = connected_info;
09546
09547 ast_party_connected_line_copy(¯o_chan->connected, connected);
09548 }
09549 ast_channel_unlock(macro_chan);
09550
09551 retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
09552 if (!retval) {
09553 struct ast_party_connected_line saved_connected;
09554
09555 ast_party_connected_line_init(&saved_connected);
09556 ast_channel_lock(macro_chan);
09557 ast_party_connected_line_copy(&saved_connected, ¯o_chan->connected);
09558 ast_channel_unlock(macro_chan);
09559 ast_channel_update_connected_line(macro_chan, &saved_connected, NULL);
09560 ast_party_connected_line_free(&saved_connected);
09561 }
09562
09563 return retval;
09564 }
09565
09566 int ast_channel_redirecting_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *redirecting_info, int is_caller, int is_frame)
09567 {
09568 const char *macro;
09569 const char *macro_args;
09570 int retval;
09571
09572 ast_channel_lock(macro_chan);
09573 macro = pbx_builtin_getvar_helper(macro_chan, is_caller
09574 ? "REDIRECTING_CALLER_SEND_MACRO" : "REDIRECTING_CALLEE_SEND_MACRO");
09575 macro = ast_strdupa(S_OR(macro, ""));
09576 macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
09577 ? "REDIRECTING_CALLER_SEND_MACRO_ARGS" : "REDIRECTING_CALLEE_SEND_MACRO_ARGS");
09578 macro_args = ast_strdupa(S_OR(macro_args, ""));
09579
09580 if (ast_strlen_zero(macro)) {
09581 ast_channel_unlock(macro_chan);
09582 return -1;
09583 }
09584
09585 if (is_frame) {
09586 const struct ast_frame *frame = redirecting_info;
09587
09588 ast_redirecting_parse_data(frame->data.ptr, frame->datalen, ¯o_chan->redirecting);
09589 } else {
09590 const struct ast_party_redirecting *redirecting = redirecting_info;
09591
09592 ast_party_redirecting_copy(¯o_chan->redirecting, redirecting);
09593 }
09594 ast_channel_unlock(macro_chan);
09595
09596 retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
09597 if (!retval) {
09598 struct ast_party_redirecting saved_redirecting;
09599
09600 ast_party_redirecting_init(&saved_redirecting);
09601 ast_channel_lock(macro_chan);
09602 ast_party_redirecting_copy(&saved_redirecting, ¯o_chan->redirecting);
09603 ast_channel_unlock(macro_chan);
09604 ast_channel_update_redirecting(macro_chan, &saved_redirecting, NULL);
09605 ast_party_redirecting_free(&saved_redirecting);
09606 }
09607
09608 return retval;
09609 }
09610
09611 static void *channel_cc_params_copy(void *data)
09612 {
09613 const struct ast_cc_config_params *src = data;
09614 struct ast_cc_config_params *dest = ast_cc_config_params_init();
09615 if (!dest) {
09616 return NULL;
09617 }
09618 ast_cc_copy_config_params(dest, src);
09619 return dest;
09620 }
09621
09622 static void channel_cc_params_destroy(void *data)
09623 {
09624 struct ast_cc_config_params *cc_params = data;
09625 ast_cc_config_params_destroy(cc_params);
09626 }
09627
09628 static const struct ast_datastore_info cc_channel_datastore_info = {
09629 .type = "Call Completion",
09630 .duplicate = channel_cc_params_copy,
09631 .destroy = channel_cc_params_destroy,
09632 };
09633
09634 int ast_channel_cc_params_init(struct ast_channel *chan,
09635 const struct ast_cc_config_params *base_params)
09636 {
09637 struct ast_cc_config_params *cc_params;
09638 struct ast_datastore *cc_datastore;
09639
09640 if (!(cc_params = ast_cc_config_params_init())) {
09641 return -1;
09642 }
09643
09644 if (!(cc_datastore = ast_datastore_alloc(&cc_channel_datastore_info, NULL))) {
09645 ast_cc_config_params_destroy(cc_params);
09646 return -1;
09647 }
09648
09649 if (base_params) {
09650 ast_cc_copy_config_params(cc_params, base_params);
09651 }
09652 cc_datastore->data = cc_params;
09653 ast_channel_datastore_add(chan, cc_datastore);
09654 return 0;
09655 }
09656
09657 struct ast_cc_config_params *ast_channel_get_cc_config_params(struct ast_channel *chan)
09658 {
09659 struct ast_datastore *cc_datastore;
09660
09661 if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
09662
09663
09664
09665
09666 if (ast_channel_cc_params_init(chan, NULL)) {
09667 return NULL;
09668 }
09669 if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
09670
09671 return NULL;
09672 }
09673 }
09674
09675 ast_assert(cc_datastore->data != NULL);
09676 return cc_datastore->data;
09677 }
09678
09679 int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
09680 {
09681 int len = name_buffer_length;
09682 char *dash;
09683 if (!ast_channel_queryoption(chan, AST_OPTION_DEVICE_NAME, device_name, &len, 0)) {
09684 return 0;
09685 }
09686
09687
09688 ast_copy_string(device_name, chan->name, name_buffer_length);
09689 if ((dash = strrchr(device_name, '-'))) {
09690 *dash = '\0';
09691 }
09692
09693 return 0;
09694 }
09695
09696 int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
09697 {
09698 int len = size;
09699 char *slash;
09700
09701 if (!ast_channel_queryoption(chan, AST_OPTION_CC_AGENT_TYPE, agent_type, &len, 0)) {
09702 return 0;
09703 }
09704
09705 ast_copy_string(agent_type, chan->name, size);
09706 if ((slash = strchr(agent_type, '/'))) {
09707 *slash = '\0';
09708 }
09709 return 0;
09710 }
09711
09712
09713
09714
09715
09716
09717
09718
09719
09720
09721 #undef ast_channel_alloc
09722 struct ast_channel __attribute__((format(printf, 10, 11)))
09723 *ast_channel_alloc(int needqueue, int state, const char *cid_num,
09724 const char *cid_name, const char *acctcode,
09725 const char *exten, const char *context,
09726 const char *linkedid, const int amaflag,
09727 const char *name_fmt, ...);
09728 struct ast_channel *ast_channel_alloc(int needqueue, int state, const char *cid_num,
09729 const char *cid_name, const char *acctcode,
09730 const char *exten, const char *context,
09731 const char *linkedid, const int amaflag,
09732 const char *name_fmt, ...)
09733 {
09734 va_list ap1, ap2;
09735 struct ast_channel *result;
09736
09737
09738 va_start(ap1, name_fmt);
09739 va_start(ap2, name_fmt);
09740 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
09741 linkedid, amaflag, __FILE__, __LINE__, __FUNCTION__, name_fmt, ap1, ap2);
09742 va_end(ap1);
09743 va_end(ap2);
09744
09745 return result;
09746 }