Opaquify char * and char[] in ast_channel
[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 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/channel.h"
35 #include "asterisk/stringfields.h"
36 #include "asterisk/data.h"
37 #include "asterisk/indications.h"
38
39 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
40
41 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
42 #define DATA_EXPORT_CALLERID(MEMBER)                            \
43         MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING)         \
44         MEMBER(ast_callerid, cid_num, AST_DATA_STRING)          \
45         MEMBER(ast_callerid, cid_name, AST_DATA_STRING)         \
46         MEMBER(ast_callerid, cid_ani, AST_DATA_STRING)          \
47         MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER)        \
48         MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER)        \
49         MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
50
51 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
52 #endif
53
54 #define DATA_EXPORT_CHANNEL(MEMBER)                                             \
55         MEMBER(ast_channel, __do_not_use_blockproc, AST_DATA_STRING)                            \
56         MEMBER(ast_channel, __do_not_use_appl, AST_DATA_STRING)                         \
57         MEMBER(ast_channel, __do_not_use_data, AST_DATA_STRING)                         \
58         MEMBER(ast_channel, __do_not_use_name, AST_DATA_STRING) \
59         MEMBER(ast_channel, __do_not_use_language, AST_DATA_STRING)                             \
60         MEMBER(ast_channel, __do_not_use_musicclass, AST_DATA_STRING)                   \
61         MEMBER(ast_channel, __do_not_use_accountcode, AST_DATA_STRING)                  \
62         MEMBER(ast_channel, __do_not_use_peeraccount, AST_DATA_STRING)                  \
63         MEMBER(ast_channel, __do_not_use_userfield, AST_DATA_STRING)                            \
64         MEMBER(ast_channel, __do_not_use_call_forward, AST_DATA_STRING)                 \
65         MEMBER(ast_channel, __do_not_use_uniqueid, AST_DATA_STRING)                             \
66         MEMBER(ast_channel, __do_not_use_linkedid, AST_DATA_STRING)                             \
67         MEMBER(ast_channel, __do_not_use_parkinglot, AST_DATA_STRING)                   \
68         MEMBER(ast_channel, __do_not_use_hangupsource, AST_DATA_STRING)                 \
69         MEMBER(ast_channel, __do_not_use_dialcontext, AST_DATA_STRING)                  \
70         MEMBER(ast_channel, rings, AST_DATA_INTEGER)                            \
71         MEMBER(ast_channel, priority, AST_DATA_INTEGER)                         \
72         MEMBER(ast_channel, macropriority, AST_DATA_INTEGER)                    \
73         MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER)                          \
74         MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER)                     \
75         MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER)                    \
76         MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER)   \
77         MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER)               \
78         MEMBER(ast_channel, __do_not_use_context, AST_DATA_STRING)                              \
79         MEMBER(ast_channel, __do_not_use_exten, AST_DATA_STRING)                                \
80         MEMBER(ast_channel, __do_not_use_macrocontext, AST_DATA_STRING)                 \
81         MEMBER(ast_channel, __do_not_use_macroexten, AST_DATA_STRING)
82
83 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
84
85 static void channel_data_add_flags(struct ast_data *tree,
86         struct ast_channel *chan)
87 {
88         ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(chan, AST_FLAG_DEFER_DTMF));
89         ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(chan, AST_FLAG_WRITE_INT));
90         ast_data_add_bool(tree, "BLOCKING", ast_test_flag(chan, AST_FLAG_BLOCKING));
91         ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(chan, AST_FLAG_ZOMBIE));
92         ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(chan, AST_FLAG_EXCEPTION));
93         ast_data_add_bool(tree, "MOH", ast_test_flag(chan, AST_FLAG_MOH));
94         ast_data_add_bool(tree, "SPYING", ast_test_flag(chan, AST_FLAG_SPYING));
95         ast_data_add_bool(tree, "NBRIDGE", ast_test_flag(chan, AST_FLAG_NBRIDGE));
96         ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP));
97         ast_data_add_bool(tree, "OUTGOING", ast_test_flag(chan, AST_FLAG_OUTGOING));
98         ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(chan, AST_FLAG_IN_DTMF));
99         ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(chan, AST_FLAG_EMULATE_DTMF));
100         ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY));
101         ast_data_add_bool(tree, "ANSWERED_ELSEWHERE", ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE));
102         ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM));
103         ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN));
104         ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT));
105         ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS));
106 }
107
108 int ast_channel_data_add_structure(struct ast_data *tree,
109         struct ast_channel *chan, int add_bridged)
110 {
111         struct ast_channel *bc;
112         struct ast_data *data_bridged;
113         struct ast_data *data_cdr;
114         struct ast_data *data_flags;
115         struct ast_data *data_zones;
116         struct ast_data *enum_node;
117         struct ast_data *data_softhangup;
118 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
119         struct ast_data *data_callerid;
120         char value_str[100];
121 #endif
122
123         if (!tree) {
124                 return -1;
125         }
126
127         ast_data_add_structure(ast_channel, tree, chan);
128
129         if (add_bridged) {
130                 bc = ast_bridged_channel(chan);
131                 if (bc) {
132                         data_bridged = ast_data_add_node(tree, "bridged");
133                         if (!data_bridged) {
134                                 return -1;
135                         }
136                         ast_channel_data_add_structure(data_bridged, bc, 0);
137                 }
138         }
139
140         ast_data_add_codec(tree, "oldwriteformat", &chan->oldwriteformat);
141         ast_data_add_codec(tree, "readformat", &chan->readformat);
142         ast_data_add_codec(tree, "writeformat", &chan->writeformat);
143         ast_data_add_codec(tree, "rawreadformat", &chan->rawreadformat);
144         ast_data_add_codec(tree, "rawwriteformat", &chan->rawwriteformat);
145         ast_data_add_codecs(tree, "nativeformats", chan->nativeformats);
146
147         /* state */
148         enum_node = ast_data_add_node(tree, "state");
149         if (!enum_node) {
150                 return -1;
151         }
152         ast_data_add_str(enum_node, "text", ast_state2str(chan->_state));
153         ast_data_add_int(enum_node, "value", chan->_state);
154
155         /* hangupcause */
156         enum_node = ast_data_add_node(tree, "hangupcause");
157         if (!enum_node) {
158                 return -1;
159         }
160         ast_data_add_str(enum_node, "text", ast_cause2str(chan->hangupcause));
161         ast_data_add_int(enum_node, "value", chan->hangupcause);
162
163         /* amaflags */
164         enum_node = ast_data_add_node(tree, "amaflags");
165         if (!enum_node) {
166                 return -1;
167         }
168         ast_data_add_str(enum_node, "text", ast_cdr_flags2str(chan->amaflags));
169         ast_data_add_int(enum_node, "value", chan->amaflags);
170
171         /* transfercapability */
172         enum_node = ast_data_add_node(tree, "transfercapability");
173         if (!enum_node) {
174                 return -1;
175         }
176         ast_data_add_str(enum_node, "text", ast_transfercapability2str(chan->transfercapability));
177         ast_data_add_int(enum_node, "value", chan->transfercapability);
178
179         /* _softphangup */
180         data_softhangup = ast_data_add_node(tree, "softhangup");
181         if (!data_softhangup) {
182                 return -1;
183         }
184         ast_data_add_bool(data_softhangup, "dev", chan->_softhangup & AST_SOFTHANGUP_DEV);
185         ast_data_add_bool(data_softhangup, "asyncgoto", chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO);
186         ast_data_add_bool(data_softhangup, "shutdown", chan->_softhangup & AST_SOFTHANGUP_SHUTDOWN);
187         ast_data_add_bool(data_softhangup, "timeout", chan->_softhangup & AST_SOFTHANGUP_TIMEOUT);
188         ast_data_add_bool(data_softhangup, "appunload", chan->_softhangup & AST_SOFTHANGUP_APPUNLOAD);
189         ast_data_add_bool(data_softhangup, "explicit", chan->_softhangup & AST_SOFTHANGUP_EXPLICIT);
190         ast_data_add_bool(data_softhangup, "unbridge", chan->_softhangup & AST_SOFTHANGUP_UNBRIDGE);
191
192         /* channel flags */
193         data_flags = ast_data_add_node(tree, "flags");
194         if (!data_flags) {
195                 return -1;
196         }
197         channel_data_add_flags(data_flags, chan);
198
199         ast_data_add_uint(tree, "timetohangup", chan->whentohangup.tv_sec);
200
201 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
202         /* callerid */
203         data_callerid = ast_data_add_node(tree, "callerid");
204         if (!data_callerid) {
205                 return -1;
206         }
207         ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
208         /* insert the callerid ton */
209         enum_node = ast_data_add_node(data_callerid, "cid_ton");
210         if (!enum_node) {
211                 return -1;
212         }
213         ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
214         snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
215                 party_number_ton2str(chan->cid.cid_ton),
216                 party_number_plan2str(chan->cid.cid_ton));
217         ast_data_add_str(enum_node, "text", value_str);
218 #endif
219
220         /* tone zone */
221         if (chan->zone) {
222                 data_zones = ast_data_add_node(tree, "zone");
223                 if (!data_zones) {
224                         return -1;
225                 }
226                 ast_tone_zone_data_add_structure(data_zones, chan->zone);
227         }
228
229         /* insert cdr */
230         data_cdr = ast_data_add_node(tree, "cdr");
231         if (!data_cdr) {
232                 return -1;
233         }
234
235         ast_cdr_data_add_structure(data_cdr, chan->cdr, 1);
236
237         return 0;
238 }
239
240 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
241         struct ast_channel *chan, const char *structure_name)
242 {
243         return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
244 }
245
246 /* ACCESSORS */
247
248 #define DEFINE_STRINGFIELD_SETTERS_FOR(field) \
249 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
250 { \
251         ast_string_field_set(chan, __do_not_use_##field, value); \
252 } \
253   \
254 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
255 { \
256         ast_string_field_build_va(chan, __do_not_use_##field, fmt, ap); \
257 } \
258 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
259 { \
260         va_list ap; \
261         va_start(ap, fmt); \
262         ast_channel_##field##_build_va(chan, fmt, ap); \
263         va_end(ap); \
264 }
265
266 DEFINE_STRINGFIELD_SETTERS_FOR(name)
267 DEFINE_STRINGFIELD_SETTERS_FOR(language)
268 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass)
269 DEFINE_STRINGFIELD_SETTERS_FOR(accountcode)
270 DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount)
271 DEFINE_STRINGFIELD_SETTERS_FOR(userfield)
272 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward)
273 DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid)
274 DEFINE_STRINGFIELD_SETTERS_FOR(linkedid)
275 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot)
276 DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource)
277 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext)
278
279 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
280 { \
281         return chan->__do_not_use_##field; \
282 }
283
284 DEFINE_STRINGFIELD_GETTER_FOR(name)
285 DEFINE_STRINGFIELD_GETTER_FOR(language)
286 DEFINE_STRINGFIELD_GETTER_FOR(musicclass)
287 DEFINE_STRINGFIELD_GETTER_FOR(accountcode)
288 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount)
289 DEFINE_STRINGFIELD_GETTER_FOR(userfield)
290 DEFINE_STRINGFIELD_GETTER_FOR(call_forward)
291 DEFINE_STRINGFIELD_GETTER_FOR(uniqueid)
292 DEFINE_STRINGFIELD_GETTER_FOR(linkedid)
293 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot)
294 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource)
295 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext)
296
297 const char *ast_channel_appl(const struct ast_channel *chan)
298 {
299         return chan->__do_not_use_appl;
300 }
301 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
302 {
303         chan->__do_not_use_appl = value;
304 }
305 const char *ast_channel_blockproc(const struct ast_channel *chan)
306 {
307         return chan->__do_not_use_blockproc;
308 }
309 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
310 {
311         chan->__do_not_use_blockproc = value;
312 }
313 const char *ast_channel_data(const struct ast_channel *chan)
314 {
315         return chan->__do_not_use_data;
316 }
317 void ast_channel_data_set(struct ast_channel *chan, const char *value)
318 {
319         chan->__do_not_use_data = value;
320 }
321
322
323 const char *ast_channel_context(const struct ast_channel *chan)
324 {
325         return chan->__do_not_use_context;
326 }
327 void ast_channel_context_set(struct ast_channel *chan, const char *value)
328 {
329         ast_copy_string(chan->__do_not_use_context, value, sizeof(chan->__do_not_use_context));
330 }
331 const char *ast_channel_exten(const struct ast_channel *chan)
332 {
333         return chan->__do_not_use_exten;
334 }
335 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
336 {
337         ast_copy_string(chan->__do_not_use_exten, value, sizeof(chan->__do_not_use_exten));
338 }
339 const char *ast_channel_macrocontext(const struct ast_channel *chan)
340 {
341         return chan->__do_not_use_macrocontext;
342 }
343 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
344 {
345         ast_copy_string(chan->__do_not_use_macrocontext, value, sizeof(chan->__do_not_use_macrocontext));
346 }
347 const char *ast_channel_macroexten(const struct ast_channel *chan)
348 {
349         return chan->__do_not_use_macroexten;
350 }
351 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
352 {
353         ast_copy_string(chan->__do_not_use_macroexten, value, sizeof(chan->__do_not_use_macroexten));
354 }