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