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