35bcb187dd4fa8069876552dca30166f02d9c4af
[asterisk/asterisk.git] / main / channel_internal_api.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Channel Accessor API
22  *
23  * This file is intended to be the only file that ever accesses the
24  * internals of an ast_channel. All other files should use the
25  * accessor functions defined here.
26  *
27  * \author Terry Wilson
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <unistd.h>
39 #include <fcntl.h>
40
41 #include "asterisk/paths.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/channel_internal.h"
44 #include "asterisk/data.h"
45 #include "asterisk/endpoints.h"
46 #include "asterisk/indications.h"
47 #include "asterisk/stasis_cache_pattern.h"
48 #include "asterisk/stasis_channels.h"
49 #include "asterisk/stasis_endpoints.h"
50 #include "asterisk/stringfields.h"
51 #include "asterisk/test.h"
52
53 /*!
54  * \brief Main Channel structure associated with a channel.
55  *
56  * \note When adding fields to this structure, it is important to add the field
57  *       'in position' with like-aligned fields, so as to keep the compiler from
58  *       having to add padding to align fields. The structure's fields are sorted
59  *       in this order: pointers, structures, long, int/enum, short, char. This
60  *       is especially important on 64-bit architectures, where mixing 4-byte
61  *       and 8-byte fields causes 4 bytes of padding to be added before many
62  *       8-byte fields.
63  */
64 struct ast_channel {
65         const struct ast_channel_tech *tech;            /*!< Technology (point to channel driver) */
66         void *tech_pvt;                                 /*!< Private data used by the technology driver */
67         void *music_state;                              /*!< Music State*/
68         void *generatordata;                            /*!< Current generator data if there is any */
69         struct ast_generator *generator;                /*!< Current active data generator */
70 /* BUGBUG bridged_channel must be eliminated from ast_channel */
71         struct ast_channel * bridged_channel;                   /*!< Who are we bridged to, if we're bridged.
72                                                          *   Who is proxying for us, if we are proxied (i.e. chan_agent).
73                                                          *   Do not access directly, use ast_bridged_channel(chan) */
74         struct ast_channel *masq;                       /*!< Channel that will masquerade as us */
75         struct ast_channel *masqr;                      /*!< Who we are masquerading as */
76         const char *blockproc;                          /*!< Procedure causing blocking */
77         const char *appl;                               /*!< Current application */
78         const char *data;                               /*!< Data passed to current application */
79         struct ast_sched_context *sched;                /*!< Schedule context */
80         struct ast_filestream *stream;                  /*!< Stream itself. */
81         struct ast_filestream *vstream;                 /*!< Video Stream itself. */
82         ast_timing_func_t timingfunc;
83         void *timingdata;
84         struct ast_pbx *pbx;                            /*!< PBX private structure for this channel */
85         struct ast_trans_pvt *writetrans;               /*!< Write translation path */
86         struct ast_trans_pvt *readtrans;                /*!< Read translation path */
87         struct ast_audiohook_list *audiohooks;
88         struct ast_framehook_list *framehooks;
89         struct ast_cdr *cdr;                            /*!< Call Detail Record */
90         struct ast_tone_zone *zone;                     /*!< Tone zone as set in indications.conf or
91                                                          *   in the CHANNEL dialplan function */
92         struct ast_channel_monitor *monitor;            /*!< Channel monitoring */
93         struct ast_callid *callid;                      /*!< Bound call identifier pointer */
94 #ifdef HAVE_EPOLL
95         struct ast_epoll_data *epfd_data[AST_MAX_FDS];
96 #endif
97         struct ao2_container *dialed_causes;            /*!< Contains tech-specific and Asterisk cause data from dialed channels */
98
99         AST_DECLARE_STRING_FIELDS(
100                 AST_STRING_FIELD(name);         /*!< ASCII unique channel name */
101                 AST_STRING_FIELD(language);     /*!< Language requested for voice prompts */
102                 AST_STRING_FIELD(musicclass);   /*!< Default music class */
103                 AST_STRING_FIELD(accountcode);  /*!< Account code for billing */
104                 AST_STRING_FIELD(peeraccount);  /*!< Peer account code for billing */
105                 AST_STRING_FIELD(userfield);    /*!< Userfield for CEL billing */
106                 AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
107                 AST_STRING_FIELD(uniqueid);     /*!< Unique Channel Identifier */
108                 AST_STRING_FIELD(linkedid);     /*!< Linked Channel Identifier -- gets propagated by linkage */
109                 AST_STRING_FIELD(parkinglot);   /*! Default parking lot, if empty, default parking lot  */
110                 AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
111                 AST_STRING_FIELD(dialcontext);  /*!< Dial: Extension context that we were called from */
112         );
113
114         struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
115         pthread_t blocker;           /*!< If anyone is blocking, this is them */
116
117         /*!
118          * \brief Dialed/Called information.
119          * \note Set on incoming channels to indicate the originally dialed party.
120          * \note Dialed Number Identifier (DNID)
121          */
122         struct ast_party_dialed dialed;
123
124         /*!
125          * \brief Channel Caller ID information.
126          * \note The caller id information is the caller id of this
127          * channel when it is used to initiate a call.
128          */
129         struct ast_party_caller caller;
130
131         /*!
132          * \brief Channel Connected Line ID information.
133          * \note The connected line information identifies the channel
134          * connected/bridged to this channel.
135          */
136         struct ast_party_connected_line connected;
137
138         /*!
139          * \brief Channel Connected Line ID information that was last indicated.
140          */
141         struct ast_party_connected_line connected_indicated;
142
143         /*! \brief Redirecting/Diversion information */
144         struct ast_party_redirecting redirecting;
145
146         struct ast_frame dtmff;                         /*!< DTMF frame */
147         struct varshead varshead;                       /*!< A linked list for channel variables. See \ref AstChanVar */
148         ast_group_t callgroup;                          /*!< Call group for call pickups */
149         ast_group_t pickupgroup;                        /*!< Pickup group - which calls groups can be picked up? */
150         struct ast_namedgroups *named_callgroups;       /*!< Named call group for call pickups */
151         struct ast_namedgroups *named_pickupgroups;     /*!< Named pickup group - which call groups can be picked up? */
152         struct timeval creationtime;                    /*!< The time of channel creation */
153         struct timeval answertime;                              /*!< The time the channel was answered */
154         struct ast_readq_list readq;
155         struct ast_jb jb;                               /*!< The jitterbuffer state */
156         struct timeval dtmf_tv;                         /*!< The time that an in process digit began, or the last digit ended */
157         struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
158         struct ast_datastore_list datastores; /*!< Data stores on the channel */
159         struct ast_autochan_list autochans; /*!< Autochans on the channel */
160         unsigned long insmpl;                           /*!< Track the read/written samples for monitor use */
161         unsigned long outsmpl;                          /*!< Track the read/written samples for monitor use */
162
163         int fds[AST_MAX_FDS];                           /*!< File descriptors for channel -- Drivers will poll on
164                                                          *   these file descriptors, so at least one must be non -1.
165                                                          *   See \arg \ref AstFileDesc */
166         int softhangup;                         /*!< Whether or not we have been hung up...  Do not set this value
167                                                          *   directly, use ast_softhangup() */
168         int fdno;                                       /*!< Which fd had an event detected on */
169         int streamid;                                   /*!< For streaming playback, the schedule ID */
170         int vstreamid;                                  /*!< For streaming video playback, the schedule ID */
171         struct ast_format oldwriteformat;  /*!< Original writer format */
172         int timingfd;                                   /*!< Timing fd */
173         enum ast_channel_state state;                   /*!< State of line -- Don't write directly, use ast_setstate() */
174         int rings;                                      /*!< Number of rings so far */
175         int priority;                                   /*!< Dialplan: Current extension priority */
176         int macropriority;                              /*!< Macro: Current non-macro priority. See app_macro.c */
177         int amaflags;                                   /*!< Set BEFORE PBX is started to determine AMA flags */
178         enum ast_channel_adsicpe adsicpe;               /*!< Whether or not ADSI is detected on CPE */
179         unsigned int fin;                               /*!< Frames in counters. The high bit is a debug mask, so
180                                                          *   the counter is only in the remaining bits */
181         unsigned int fout;                              /*!< Frames out counters. The high bit is a debug mask, so
182                                                          *   the counter is only in the remaining bits */
183         int hangupcause;                                /*!< Why is the channel hanged up. See causes.h */
184         unsigned int finalized:1;       /*!< Whether or not the channel has been successfully allocated */
185         struct ast_flags flags;                         /*!< channel flags of AST_FLAG_ type */
186         int alertpipe[2];
187         struct ast_format_cap *nativeformats;         /*!< Kinds of data this channel can natively handle */
188         struct ast_format readformat;            /*!< Requested read format (after translation) */
189         struct ast_format writeformat;           /*!< Requested write format (after translation) */
190         struct ast_format rawreadformat;         /*!< Raw read format (before translation) */
191         struct ast_format rawwriteformat;        /*!< Raw write format (before translation) */
192         unsigned int emulate_dtmf_duration;             /*!< Number of ms left to emulate DTMF for */
193 #ifdef HAVE_EPOLL
194         int epfd;
195 #endif
196         int visible_indication;                         /*!< Indication currently playing on the channel */
197
198         unsigned short transfercapability;              /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
199
200 /* BUGBUG the bridge pointer must change to an ast_channel_bridge pointer because it will never change while the channel is in the bridging system whereas the bridge could change. */
201         struct ast_bridge *bridge;                      /*!< Bridge this channel is participating in */
202         struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
203         struct ast_timer *timer;                        /*!< timer object that provided timingfd */
204
205         char context[AST_MAX_CONTEXT];                  /*!< Dialplan: Current extension context */
206         char exten[AST_MAX_EXTENSION];                  /*!< Dialplan: Current extension number */
207         char macrocontext[AST_MAX_CONTEXT];             /*!< Macro: Current non-macro context. See app_macro.c */
208         char macroexten[AST_MAX_EXTENSION];             /*!< Macro: Current non-macro extension. See app_macro.c */
209         char dtmf_digit_to_emulate;                     /*!< Digit being emulated */
210         char sending_dtmf_digit;                        /*!< Digit this channel is currently sending out. (zero if not sending) */
211         struct timeval sending_dtmf_tv;         /*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
212         struct stasis_cp_single *topics;                /*!< Topic for all channel's events */
213         struct stasis_subscription *forwarder;          /*!< Subscription for event forwarding to all topic */
214         struct stasis_subscription *endpoint_forward;   /*!< Subscription for event forwarding to endpoint's topic */
215 };
216
217 /*! \brief The monotonically increasing integer counter for channel uniqueids */
218 static int uniqueint;
219
220 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
221
222 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
223 #define DATA_EXPORT_CALLERID(MEMBER)                            \
224         MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING)         \
225         MEMBER(ast_callerid, cid_num, AST_DATA_STRING)          \
226         MEMBER(ast_callerid, cid_name, AST_DATA_STRING)         \
227         MEMBER(ast_callerid, cid_ani, AST_DATA_STRING)          \
228         MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER)        \
229         MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER)        \
230         MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
231
232 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
233 #endif
234
235 #define DATA_EXPORT_CHANNEL(MEMBER)                                             \
236         MEMBER(ast_channel, blockproc, AST_DATA_STRING)                         \
237         MEMBER(ast_channel, appl, AST_DATA_STRING)                              \
238         MEMBER(ast_channel, data, AST_DATA_STRING)                              \
239         MEMBER(ast_channel, name, AST_DATA_STRING) \
240         MEMBER(ast_channel, language, AST_DATA_STRING)                          \
241         MEMBER(ast_channel, musicclass, AST_DATA_STRING)                        \
242         MEMBER(ast_channel, accountcode, AST_DATA_STRING)                       \
243         MEMBER(ast_channel, peeraccount, AST_DATA_STRING)                       \
244         MEMBER(ast_channel, userfield, AST_DATA_STRING)                         \
245         MEMBER(ast_channel, call_forward, AST_DATA_STRING)                      \
246         MEMBER(ast_channel, uniqueid, AST_DATA_STRING)                          \
247         MEMBER(ast_channel, linkedid, AST_DATA_STRING)                          \
248         MEMBER(ast_channel, parkinglot, AST_DATA_STRING)                        \
249         MEMBER(ast_channel, hangupsource, AST_DATA_STRING)                      \
250         MEMBER(ast_channel, dialcontext, AST_DATA_STRING)                       \
251         MEMBER(ast_channel, rings, AST_DATA_INTEGER)                            \
252         MEMBER(ast_channel, priority, AST_DATA_INTEGER)                         \
253         MEMBER(ast_channel, macropriority, AST_DATA_INTEGER)                    \
254         MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER)                          \
255         MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER)                     \
256         MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER)                    \
257         MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER)   \
258         MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER)               \
259         MEMBER(ast_channel, context, AST_DATA_STRING)                           \
260         MEMBER(ast_channel, exten, AST_DATA_STRING)                             \
261         MEMBER(ast_channel, macrocontext, AST_DATA_STRING)                      \
262         MEMBER(ast_channel, macroexten, AST_DATA_STRING)
263
264 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
265
266 static void channel_data_add_flags(struct ast_data *tree,
267         struct ast_channel *chan)
268 {
269         ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF));
270         ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT));
271         ast_data_add_bool(tree, "BLOCKING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING));
272         ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE));
273         ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
274         ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
275         ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
276         ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
277         ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
278         ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
279         ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF));
280         ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
281         ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
282         ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
283         ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
284         ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
285         ast_data_add_bool(tree, "BRIDGE_DUAL_REDIRECT_WAIT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT));
286         ast_data_add_bool(tree, "ORIGINATED", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED));
287         ast_data_add_bool(tree, "DEAD", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEAD));
288 }
289
290 int ast_channel_data_add_structure(struct ast_data *tree,
291         struct ast_channel *chan, int add_bridged)
292 {
293         struct ast_channel *bc;
294         struct ast_data *data_bridged;
295         struct ast_data *data_cdr;
296         struct ast_data *data_flags;
297         struct ast_data *data_zones;
298         struct ast_data *enum_node;
299         struct ast_data *data_softhangup;
300 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
301         struct ast_data *data_callerid;
302         char value_str[100];
303 #endif
304
305         if (!tree) {
306                 return -1;
307         }
308
309         ast_data_add_structure(ast_channel, tree, chan);
310
311         if (add_bridged) {
312                 bc = ast_bridged_channel(chan);
313                 if (bc) {
314                         data_bridged = ast_data_add_node(tree, "bridged");
315                         if (!data_bridged) {
316                                 return -1;
317                         }
318                         ast_channel_data_add_structure(data_bridged, bc, 0);
319                 }
320         }
321
322         ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
323         ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
324         ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
325         ast_data_add_codec(tree, "rawreadformat", ast_channel_rawreadformat(chan));
326         ast_data_add_codec(tree, "rawwriteformat", ast_channel_rawwriteformat(chan));
327         ast_data_add_codecs(tree, "nativeformats", ast_channel_nativeformats(chan));
328
329         /* state */
330         enum_node = ast_data_add_node(tree, "state");
331         if (!enum_node) {
332                 return -1;
333         }
334         ast_data_add_str(enum_node, "text", ast_state2str(ast_channel_state(chan)));
335         ast_data_add_int(enum_node, "value", ast_channel_state(chan));
336
337         /* hangupcause */
338         enum_node = ast_data_add_node(tree, "hangupcause");
339         if (!enum_node) {
340                 return -1;
341         }
342         ast_data_add_str(enum_node, "text", ast_cause2str(ast_channel_hangupcause(chan)));
343         ast_data_add_int(enum_node, "value", ast_channel_hangupcause(chan));
344
345         /* amaflags */
346         enum_node = ast_data_add_node(tree, "amaflags");
347         if (!enum_node) {
348                 return -1;
349         }
350         ast_data_add_str(enum_node, "text", ast_channel_amaflags2string(ast_channel_amaflags(chan)));
351         ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
352
353         /* transfercapability */
354         enum_node = ast_data_add_node(tree, "transfercapability");
355         if (!enum_node) {
356                 return -1;
357         }
358         ast_data_add_str(enum_node, "text", ast_transfercapability2str(ast_channel_transfercapability(chan)));
359         ast_data_add_int(enum_node, "value", ast_channel_transfercapability(chan));
360
361         /* _softphangup */
362         data_softhangup = ast_data_add_node(tree, "softhangup");
363         if (!data_softhangup) {
364                 return -1;
365         }
366         ast_data_add_bool(data_softhangup, "dev", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_DEV);
367         ast_data_add_bool(data_softhangup, "asyncgoto", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO);
368         ast_data_add_bool(data_softhangup, "shutdown", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_SHUTDOWN);
369         ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
370         ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
371         ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
372         ast_data_add_bool(data_softhangup, "unbridge", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_UNBRIDGE);
373
374         /* channel flags */
375         data_flags = ast_data_add_node(tree, "flags");
376         if (!data_flags) {
377                 return -1;
378         }
379         channel_data_add_flags(data_flags, chan);
380
381         ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
382
383 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
384         /* callerid */
385         data_callerid = ast_data_add_node(tree, "callerid");
386         if (!data_callerid) {
387                 return -1;
388         }
389         ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
390         /* insert the callerid ton */
391         enum_node = ast_data_add_node(data_callerid, "cid_ton");
392         if (!enum_node) {
393                 return -1;
394         }
395         ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
396         snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
397                 party_number_ton2str(chan->cid.cid_ton),
398                 party_number_plan2str(chan->cid.cid_ton));
399         ast_data_add_str(enum_node, "text", value_str);
400 #endif
401
402         /* tone zone */
403         if (ast_channel_zone(chan)) {
404                 data_zones = ast_data_add_node(tree, "zone");
405                 if (!data_zones) {
406                         return -1;
407                 }
408                 ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
409         }
410
411         /* insert cdr */
412         data_cdr = ast_data_add_node(tree, "cdr");
413         if (!data_cdr) {
414                 return -1;
415         }
416
417         return 0;
418 }
419
420 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
421         struct ast_channel *chan, const char *structure_name)
422 {
423         return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
424 }
425
426 /* ACCESSORS */
427
428 #define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
429 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
430 { \
431         if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
432         if (!strcmp(value, chan->field)) return; \
433         ast_string_field_set(chan, field, value); \
434         if (publish) ast_channel_publish_snapshot(chan); \
435 } \
436   \
437 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
438 { \
439         ast_string_field_build_va(chan, field, fmt, ap); \
440         if (publish) ast_channel_publish_snapshot(chan); \
441 } \
442 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
443 { \
444         va_list ap; \
445         va_start(ap, fmt); \
446         ast_channel_##field##_build_va(chan, fmt, ap); \
447         va_end(ap); \
448 }
449
450 DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
451 DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
452 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
453 DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
454 DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
455 DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
456 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
457 DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid, 0, 1);
458 DEFINE_STRINGFIELD_SETTERS_FOR(linkedid, 1, 1);
459 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
460 DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
461 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);
462
463 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
464 { \
465         return chan->field; \
466 }
467
468 DEFINE_STRINGFIELD_GETTER_FOR(name);
469 DEFINE_STRINGFIELD_GETTER_FOR(language);
470 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
471 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
472 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
473 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
474 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
475 DEFINE_STRINGFIELD_GETTER_FOR(uniqueid);
476 DEFINE_STRINGFIELD_GETTER_FOR(linkedid);
477 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
478 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
479 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
480
481 const char *ast_channel_appl(const struct ast_channel *chan)
482 {
483         return chan->appl;
484 }
485 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
486 {
487         chan->appl = value;
488 }
489 const char *ast_channel_blockproc(const struct ast_channel *chan)
490 {
491         return chan->blockproc;
492 }
493 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
494 {
495         chan->blockproc = value;
496 }
497 const char *ast_channel_data(const struct ast_channel *chan)
498 {
499         return chan->data;
500 }
501 void ast_channel_data_set(struct ast_channel *chan, const char *value)
502 {
503         chan->data = value;
504 }
505
506 const char *ast_channel_context(const struct ast_channel *chan)
507 {
508         return chan->context;
509 }
510 void ast_channel_context_set(struct ast_channel *chan, const char *value)
511 {
512         ast_copy_string(chan->context, value, sizeof(chan->context));
513 }
514 const char *ast_channel_exten(const struct ast_channel *chan)
515 {
516         return chan->exten;
517 }
518 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
519 {
520         ast_copy_string(chan->exten, value, sizeof(chan->exten));
521 }
522 const char *ast_channel_macrocontext(const struct ast_channel *chan)
523 {
524         return chan->macrocontext;
525 }
526 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
527 {
528         ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
529 }
530 const char *ast_channel_macroexten(const struct ast_channel *chan)
531 {
532         return chan->macroexten;
533 }
534 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
535 {
536         ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
537 }
538
539 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
540 {
541         return chan->dtmf_digit_to_emulate;
542 }
543 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
544 {
545         chan->dtmf_digit_to_emulate = value;
546 }
547
548 char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
549 {
550         return chan->sending_dtmf_digit;
551 }
552 void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
553 {
554         chan->sending_dtmf_digit = value;
555 }
556
557 struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
558 {
559         return chan->sending_dtmf_tv;
560 }
561 void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
562 {
563         chan->sending_dtmf_tv = value;
564 }
565
566 enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
567 {
568         return chan->amaflags;
569 }
570
571 void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
572 {
573         if (chan->amaflags == value) {
574                 return;
575         }
576         chan->amaflags = value;
577         ast_channel_publish_snapshot(chan);
578 }
579
580 #ifdef HAVE_EPOLL
581 int ast_channel_epfd(const struct ast_channel *chan)
582 {
583         return chan->epfd;
584 }
585 void ast_channel_epfd_set(struct ast_channel *chan, int value)
586 {
587         chan->epfd = value;
588 }
589 #endif
590 int ast_channel_fdno(const struct ast_channel *chan)
591 {
592         return chan->fdno;
593 }
594 void ast_channel_fdno_set(struct ast_channel *chan, int value)
595 {
596         chan->fdno = value;
597 }
598 int ast_channel_hangupcause(const struct ast_channel *chan)
599 {
600         return chan->hangupcause;
601 }
602 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
603 {
604         chan->hangupcause = value;
605 }
606 int ast_channel_macropriority(const struct ast_channel *chan)
607 {
608         return chan->macropriority;
609 }
610 void ast_channel_macropriority_set(struct ast_channel *chan, int value)
611 {
612         chan->macropriority = value;
613 }
614 int ast_channel_priority(const struct ast_channel *chan)
615 {
616         return chan->priority;
617 }
618 void ast_channel_priority_set(struct ast_channel *chan, int value)
619 {
620         chan->priority = value;
621 }
622 int ast_channel_rings(const struct ast_channel *chan)
623 {
624         return chan->rings;
625 }
626 void ast_channel_rings_set(struct ast_channel *chan, int value)
627 {
628         chan->rings = value;
629 }
630 int ast_channel_streamid(const struct ast_channel *chan)
631 {
632         return chan->streamid;
633 }
634 void ast_channel_streamid_set(struct ast_channel *chan, int value)
635 {
636         chan->streamid = value;
637 }
638 int ast_channel_timingfd(const struct ast_channel *chan)
639 {
640         return chan->timingfd;
641 }
642 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
643 {
644         chan->timingfd = value;
645 }
646 int ast_channel_visible_indication(const struct ast_channel *chan)
647 {
648         return chan->visible_indication;
649 }
650 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
651 {
652         chan->visible_indication = value;
653 }
654 int ast_channel_vstreamid(const struct ast_channel *chan)
655 {
656         return chan->vstreamid;
657 }
658 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
659 {
660         chan->vstreamid = value;
661 }
662 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
663 {
664         return chan->transfercapability;
665 }
666 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
667 {
668         chan->transfercapability = value;
669 }
670 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
671 {
672         return chan->emulate_dtmf_duration;
673 }
674 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
675 {
676         chan->emulate_dtmf_duration = value;
677 }
678 unsigned int ast_channel_fin(const struct ast_channel *chan)
679 {
680         return chan->fin;
681 }
682 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
683 {
684         chan->fin = value;
685 }
686 unsigned int ast_channel_fout(const struct ast_channel *chan)
687 {
688         return chan->fout;
689 }
690 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
691 {
692         chan->fout = value;
693 }
694 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
695 {
696         return chan->insmpl;
697 }
698 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
699 {
700         chan->insmpl = value;
701 }
702 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
703 {
704         return chan->outsmpl;
705 }
706 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
707 {
708         chan->outsmpl = value;
709 }
710 void *ast_channel_generatordata(const struct ast_channel *chan)
711 {
712         return chan->generatordata;
713 }
714 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
715 {
716         chan->generatordata = value;
717 }
718 void *ast_channel_music_state(const struct ast_channel *chan)
719 {
720         return chan->music_state;
721 }
722 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
723 {
724         chan->music_state = value;
725 }
726 void *ast_channel_tech_pvt(const struct ast_channel *chan)
727 {
728         return chan->tech_pvt;
729 }
730 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
731 {
732         chan->tech_pvt = value;
733 }
734 void *ast_channel_timingdata(const struct ast_channel *chan)
735 {
736         return chan->timingdata;
737 }
738 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
739 {
740         chan->timingdata = value;
741 }
742 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
743 {
744         return chan->audiohooks;
745 }
746 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
747 {
748         chan->audiohooks = value;
749 }
750 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
751 {
752         return chan->cdr;
753 }
754 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
755 {
756         chan->cdr = value;
757 }
758 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
759 {
760         return chan->masq;
761 }
762 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
763 {
764         chan->masq = value;
765 }
766 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
767 {
768         return chan->masqr;
769 }
770 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
771 {
772         chan->masqr = value;
773 }
774 struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
775 {
776         return chan->monitor;
777 }
778 void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
779 {
780         chan->monitor = value;
781 }
782 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
783 {
784         return chan->stream;
785 }
786 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
787 {
788         chan->stream = value;
789 }
790 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
791 {
792         return chan->vstream;
793 }
794 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
795 {
796         chan->vstream = value;
797 }
798 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
799 {
800         return chan->nativeformats;
801 }
802 void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
803 {
804         chan->nativeformats = value;
805 }
806 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
807 {
808         return chan->framehooks;
809 }
810 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
811 {
812         chan->framehooks = value;
813 }
814 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
815 {
816         return chan->generator;
817 }
818 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
819 {
820         chan->generator = value;
821 }
822 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
823 {
824         return chan->pbx;
825 }
826 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
827 {
828         chan->pbx = value;
829 }
830 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
831 {
832         return chan->sched;
833 }
834 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
835 {
836         chan->sched = value;
837 }
838 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
839 {
840         return chan->timer;
841 }
842 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
843 {
844         chan->timer = value;
845 }
846 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
847 {
848         return chan->zone;
849 }
850 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
851 {
852         chan->zone = value;
853 }
854 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
855 {
856         return chan->readtrans;
857 }
858 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
859 {
860         chan->readtrans = value;
861 }
862 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
863 {
864         return chan->writetrans;
865 }
866 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
867 {
868         chan->writetrans = value;
869 }
870 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
871 {
872         return chan->tech;
873 }
874 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
875 {
876         chan->tech = value;
877         ast_channel_publish_snapshot(chan);
878 }
879 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
880 {
881         return chan->adsicpe;
882 }
883 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
884 {
885         chan->adsicpe = value;
886 }
887 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
888 {
889         return chan->state;
890 }
891 struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
892 {
893         if (chan->callid) {
894                 ast_callid_ref(chan->callid);
895                 return chan->callid;
896         }
897         return NULL;
898 }
899 void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
900 {
901         char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
902         char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
903         call_identifier_from[0] = '\0';
904         ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
905         if (chan->callid) {
906                 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
907                 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
908                 /* unbind if already set */
909                 ast_callid_unref(chan->callid);
910         }
911
912         chan->callid = ast_callid_ref(callid);
913
914         ast_test_suite_event_notify("CallIDChange",
915                 "State: CallIDChange\r\n"
916                 "Channel: %s\r\n"
917                 "CallID: %s\r\n"
918                 "PriorCallID: %s",
919                 ast_channel_name(chan),
920                 call_identifier_to,
921                 call_identifier_from);
922
923         ast_channel_publish_snapshot(chan);
924 }
925 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
926 {
927         chan->state = value;
928 }
929 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
930 {
931         return &chan->oldwriteformat;
932 }
933 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
934 {
935         return &chan->rawreadformat;
936 }
937 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
938 {
939         return &chan->rawwriteformat;
940 }
941 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
942 {
943         return &chan->readformat;
944 }
945 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
946 {
947         return &chan->writeformat;
948 }
949 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
950 {
951         return &chan->hangup_handlers;
952 }
953 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
954 {
955         return &chan->datastores;
956 }
957 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
958 {
959         return &chan->autochans;
960 }
961 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
962 {
963         return &chan->readq;
964 }
965 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
966 {
967         return &chan->dtmff;
968 }
969 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
970 {
971         return &chan->jb;
972 }
973 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
974 {
975         return &chan->caller;
976 }
977 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
978 {
979         return &chan->connected;
980 }
981 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
982 {
983         return &chan->connected_indicated;
984 }
985 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
986 {
987         return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
988 }
989 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
990 {
991         return &chan->dialed;
992 }
993 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
994 {
995         return &chan->redirecting;
996 }
997 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
998 {
999         return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
1000 }
1001 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
1002 {
1003         return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
1004 }
1005 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
1006 {
1007         return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
1008 }
1009 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
1010 {
1011         return &chan->dtmf_tv;
1012 }
1013 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
1014 {
1015         return &chan->whentohangup;
1016 }
1017 struct varshead *ast_channel_varshead(struct ast_channel *chan)
1018 {
1019         return &chan->varshead;
1020 }
1021 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
1022 {
1023         chan->dtmff = *value;
1024 }
1025 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
1026 {
1027         chan->jb = *value;
1028 }
1029 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
1030 {
1031         chan->caller = *value;
1032 }
1033 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
1034 {
1035         chan->connected = *value;
1036 }
1037 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
1038 {
1039         chan->dialed = *value;
1040 }
1041 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
1042 {
1043         chan->redirecting = *value;
1044 }
1045 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
1046 {
1047         chan->dtmf_tv = *value;
1048 }
1049 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
1050 {
1051         chan->whentohangup = *value;
1052         ast_channel_publish_snapshot(chan);
1053 }
1054 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1055 {
1056         chan->varshead = *value;
1057 }
1058 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1059 {
1060         return chan->creationtime;
1061 }
1062 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1063 {
1064         chan->creationtime = *value;
1065 }
1066
1067 struct timeval ast_channel_answertime(struct ast_channel *chan)
1068 {
1069         return chan->answertime;
1070 }
1071
1072 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
1073 {
1074         chan->answertime = *value;
1075 }
1076
1077 /* Evil softhangup accessors */
1078 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1079 {
1080         return chan->softhangup;
1081 }
1082 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1083 {
1084         chan->softhangup = value;
1085 }
1086 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1087 {
1088         chan->softhangup |= value;
1089 }
1090 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1091 {
1092         chan ->softhangup &= ~value;
1093 }
1094
1095 void ast_channel_callid_cleanup(struct ast_channel *chan)
1096 {
1097         if (chan->callid) {
1098                 chan->callid = ast_callid_unref(chan->callid);
1099         }
1100 }
1101
1102 /* Typedef accessors */
1103 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1104 {
1105         return chan->callgroup;
1106 }
1107 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1108 {
1109         chan->callgroup = value;
1110         ast_channel_publish_snapshot(chan);
1111 }
1112 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1113 {
1114         return chan->pickupgroup;
1115 }
1116 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1117 {
1118         chan->pickupgroup = value;
1119         ast_channel_publish_snapshot(chan);
1120 }
1121 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1122 {
1123         return chan->named_callgroups;
1124 }
1125 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1126 {
1127         ast_unref_namedgroups(chan->named_callgroups);
1128         chan->named_callgroups = ast_ref_namedgroups(value);
1129 }
1130 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1131 {
1132         return chan->named_pickupgroups;
1133 }
1134 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1135 {
1136         ast_unref_namedgroups(chan->named_pickupgroups);
1137         chan->named_pickupgroups = ast_ref_namedgroups(value);
1138 }
1139
1140 /* Alertpipe functions */
1141 int ast_channel_alert_write(struct ast_channel *chan)
1142 {
1143         char blah = 0x7F;
1144         return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1145 }
1146
1147 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1148 {
1149         int flags;
1150         char blah;
1151
1152         if (!ast_channel_internal_alert_readable(chan)) {
1153                 return AST_ALERT_NOT_READABLE;
1154         }
1155
1156         flags = fcntl(chan->alertpipe[0], F_GETFL);
1157         /* For some odd reason, the alertpipe occasionally loses nonblocking status,
1158          * which immediately causes a deadlock scenario.  Detect and prevent this. */
1159         if ((flags & O_NONBLOCK) == 0) {
1160                 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
1161                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1162                         ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1163                         return AST_ALERT_READ_FATAL;
1164                 }
1165         }
1166         if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
1167                 if (errno != EINTR && errno != EAGAIN) {
1168                         ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1169                         return AST_ALERT_READ_FAIL;
1170                 }
1171         }
1172
1173         return AST_ALERT_READ_SUCCESS;
1174 }
1175
1176 int ast_channel_alert_writable(struct ast_channel *chan)
1177 {
1178         return chan->alertpipe[1] > -1;
1179 }
1180
1181 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1182 {
1183         return chan->alertpipe[0] > -1;
1184 }
1185
1186 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1187 {
1188         chan->alertpipe[0] = chan->alertpipe[1] = -1;
1189 }
1190
1191 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1192 {
1193         if (ast_channel_internal_alert_readable(chan)) {
1194                 close(chan->alertpipe[0]);
1195         }
1196         if (ast_channel_alert_writable(chan)) {
1197                 close(chan->alertpipe[1]);
1198         }
1199 }
1200
1201 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1202 {
1203         if (pipe(chan->alertpipe)) {
1204                 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1205                 return -1;
1206         } else {
1207                 int flags = fcntl(chan->alertpipe[0], F_GETFL);
1208                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1209                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1210                         return -1;
1211                 }
1212                 flags = fcntl(chan->alertpipe[1], F_GETFL);
1213                 if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1214                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1215                         return -1;
1216                 }
1217         }
1218         return 0;
1219 }
1220
1221 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1222 {
1223         return chan->alertpipe[0];
1224 }
1225
1226 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1227 {
1228         int i;
1229         for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1230                 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1231         }
1232 }
1233
1234 /* file descriptor array accessors */
1235 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1236 {
1237         chan->fds[which] = value;
1238 }
1239 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1240 {
1241         ast_channel_internal_fd_set(chan, which, -1);
1242 }
1243 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1244 {
1245         int i;
1246         for (i = 0; i < AST_MAX_FDS; i++) {
1247                 ast_channel_internal_fd_clear(chan, i);
1248         }
1249 }
1250 int ast_channel_fd(const struct ast_channel *chan, int which)
1251 {
1252         return chan->fds[which];
1253 }
1254 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1255 {
1256         return ast_channel_fd(chan, which) > -1;
1257 }
1258
1259 #ifdef HAVE_EPOLL
1260 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1261 {
1262         return chan->epfd_data[which];
1263 }
1264 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1265 {
1266         chan->epfd_data[which] = value;
1267 }
1268 #endif
1269
1270 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1271 {
1272         return chan->blocker;
1273 }
1274 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1275 {
1276         chan->blocker = value;
1277 }
1278
1279 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1280 {
1281         return chan->timingfunc;
1282 }
1283 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1284 {
1285         chan->timingfunc = value;
1286 }
1287
1288 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1289 {
1290         return chan->bridge;
1291 }
1292 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1293 {
1294         chan->bridge = value;
1295         ast_channel_publish_snapshot(chan);
1296 }
1297
1298 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1299 {
1300         return chan->bridge_channel;
1301 }
1302 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1303 {
1304         chan->bridge_channel = value;
1305 }
1306
1307 struct ast_channel *ast_channel_internal_bridged_channel(const struct ast_channel *chan)
1308 {
1309         return chan->bridged_channel;
1310 }
1311 void ast_channel_internal_bridged_channel_set(struct ast_channel *chan, struct ast_channel *value)
1312 {
1313         chan->bridged_channel = value;
1314 }
1315
1316 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1317 {
1318         return &chan->flags;
1319 }
1320
1321 static int collect_names_cb(void *obj, void *arg, int flags) {
1322         struct ast_control_pvt_cause_code *cause_code = obj;
1323         struct ast_str **str = arg;
1324
1325         ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1326
1327         return 0;
1328 }
1329
1330 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1331 {
1332         struct ast_str *chanlist = ast_str_create(128);
1333
1334         if (!chanlist) {
1335                 return NULL;
1336         }
1337
1338         ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1339
1340         return chanlist;
1341 }
1342
1343 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1344 {
1345         return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1346 }
1347
1348 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1349 {
1350         struct ast_control_pvt_cause_code *ao2_cause_code;
1351         ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1352         ao2_cause_code = ao2_alloc(datalen, NULL);
1353
1354         if (ao2_cause_code) {
1355                 memcpy(ao2_cause_code, cause_code, datalen);
1356                 ao2_link(chan->dialed_causes, ao2_cause_code);
1357                 ao2_ref(ao2_cause_code, -1);
1358                 return 0;
1359         } else {
1360                 return -1;
1361         }
1362 }
1363
1364 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1365 {
1366         ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1367 }
1368
1369 /* \brief Hash function for pvt cause code frames */
1370 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1371 {
1372         const struct ast_control_pvt_cause_code *pc = vpc;
1373         return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1374 }
1375
1376 /* \brief Comparison function for pvt cause code frames */
1377 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1378 {
1379         struct ast_control_pvt_cause_code *pc = obj;
1380         char *str = ast_tech_to_upper(ast_strdupa(vstr));
1381         char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1382         return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1383 }
1384
1385 #define DIALED_CAUSES_BUCKETS 37
1386
1387 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const char *linkedid, const char *file, int line, const char *function)
1388 {
1389         struct ast_channel *tmp;
1390 #if defined(REF_DEBUG)
1391         tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1392                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 1);
1393 #elif defined(__AST_DEBUG_MALLOC)
1394         tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1395                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 0);
1396 #else
1397         tmp = ao2_alloc(sizeof(*tmp), destructor);
1398 #endif
1399
1400         if ((ast_string_field_init(tmp, 128))) {
1401                 return ast_channel_unref(tmp);
1402         }
1403
1404         if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1405                 return ast_channel_unref(tmp);
1406         }
1407
1408         if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1409                 ast_channel_uniqueid_build(tmp, "%li.%d", (long)time(NULL),
1410                         ast_atomic_fetchadd_int(&uniqueint, 1));
1411         } else {
1412                 ast_channel_uniqueid_build(tmp, "%s-%li.%d", ast_config_AST_SYSTEM_NAME,
1413                         (long)time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1));
1414         }
1415
1416         if (!ast_strlen_zero(linkedid)) {
1417                 ast_string_field_set(tmp, linkedid, linkedid);
1418         } else {
1419                 ast_string_field_set(tmp, linkedid, tmp->uniqueid);
1420         }
1421
1422         return tmp;
1423 }
1424
1425 void ast_channel_internal_cleanup(struct ast_channel *chan)
1426 {
1427         if (chan->dialed_causes) {
1428                 ao2_t_ref(chan->dialed_causes, -1,
1429                         "done with dialed causes since the channel is going away");
1430                 chan->dialed_causes = NULL;
1431         }
1432
1433         ast_string_field_free_memory(chan);
1434
1435         chan->forwarder = stasis_unsubscribe(chan->forwarder);
1436         chan->endpoint_forward = stasis_unsubscribe(chan->endpoint_forward);
1437
1438         stasis_cp_single_unsubscribe(chan->topics);
1439         chan->topics = NULL;
1440 }
1441
1442 void ast_channel_internal_finalize(struct ast_channel *chan)
1443 {
1444         chan->finalized = 1;
1445 }
1446
1447 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1448 {
1449         return chan->finalized;
1450 }
1451
1452 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1453 {
1454         if (!chan) {
1455                 return ast_channel_topic_all();
1456         }
1457
1458         return stasis_cp_single_topic(chan->topics);
1459 }
1460
1461 struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
1462 {
1463         if (!chan) {
1464                 return ast_channel_topic_all_cached();
1465         }
1466
1467         return stasis_cp_single_topic_cached(chan->topics);
1468 }
1469
1470 int ast_channel_forward_endpoint(struct ast_channel *chan,
1471         struct ast_endpoint *endpoint)
1472 {
1473         ast_assert(chan != NULL);
1474         ast_assert(endpoint != NULL);
1475
1476         chan->endpoint_forward =
1477                 stasis_forward_all(ast_channel_topic(chan),
1478                         ast_endpoint_topic(endpoint));
1479
1480         if (chan->endpoint_forward == NULL) {
1481                 return -1;
1482         }
1483
1484         return 0;
1485 }
1486
1487 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1488 {
1489         const char *topic_name = chan->uniqueid;
1490         ast_assert(chan->topics == NULL);
1491
1492         if (ast_strlen_zero(topic_name)) {
1493                 topic_name = "<dummy-channel>";
1494         }
1495
1496         chan->topics = stasis_cp_single_create(
1497                 ast_channel_cache_all(), topic_name);
1498
1499         if (!chan->topics) {
1500                 return -1;
1501         }
1502
1503         return 0;
1504 }