Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
func_hangupcause.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999-2012, Digium, Inc.
5 *
6 * Kinsey Moore <kmoore@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 Functions related to retreiving per-channel hangupcause information
22 *
23 * \author Kinsey Moore <kmoore@digium.com>
24 * \ingroup functions
25 *
26 * See Also:
27 * \arg \ref AstCREDITS
28 */
29
30/*** MODULEINFO
31 <support_level>core</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include "asterisk/module.h"
37#include "asterisk/channel.h"
38#include "asterisk/pbx.h"
39#include "asterisk/utils.h"
40#include "asterisk/app.h"
41
42/*** DOCUMENTATION
43 <function name="HANGUPCAUSE" language="en_US">
44 <since>
45 <version>11.0.0</version>
46 </since>
47 <synopsis>
48 Gets per-channel hangupcause information from the channel.
49 </synopsis>
50 <syntax>
51 <parameter name="channel" required="true">
52 <para>The name of the channel for which to retrieve cause information.</para>
53 </parameter>
54 <parameter name="type" required="true">
55 <para>Parameter describing which type of information is requested. Types are:</para>
56 <enumlist>
57 <enum name="tech"><para>Technology-specific cause information</para></enum>
58 <enum name="tech_extended"><para>Technology-specific extended list of cause information</para></enum>
59 <enum name="ast"><para>Translated Asterisk cause code</para></enum>
60 </enumlist>
61 </parameter>
62 </syntax>
63 <description>
64 <para>Gets technology-specific or translated Asterisk cause code information
65 from the channel for the specified channel that resulted from a dial.</para>
66 </description>
67 <see-also>
68 <ref type="function">HANGUPCAUSE_KEYS</ref>
69 <ref type="application">HangupCauseClear</ref>
70 </see-also>
71 </function>
72 <function name="HANGUPCAUSE_KEYS" language="en_US">
73 <since>
74 <version>11.0.0</version>
75 </since>
76 <synopsis>
77 Gets the list of channels for which hangup causes are available.
78 </synopsis>
79 <description>
80 <para>Returns a comma-separated list of channel names to be used with the HANGUPCAUSE function.</para>
81 </description>
82 <see-also>
83 <ref type="function">HANGUPCAUSE</ref>
84 <ref type="application">HangupCauseClear</ref>
85 </see-also>
86 </function>
87 <application name="HangupCauseClear" language="en_US">
88 <since>
89 <version>11.0.0</version>
90 </since>
91 <synopsis>
92 Clears hangup cause information from the channel that is available through HANGUPCAUSE.
93 </synopsis>
94 <description>
95 <para>Clears all channel-specific hangup cause information from the channel.
96 This is never done automatically (i.e. for new Dial()s).</para>
97 </description>
98 <see-also>
99 <ref type="function">HANGUPCAUSE</ref>
100 <ref type="function">HANGUPCAUSE_KEYS</ref>
101 </see-also>
102 </application>
103 ***/
104
105/*!
106 * \internal
107 * \brief Read values from the hangupcause ao2 container.
108 *
109 * \param chan Asterisk channel to read
110 * \param cmd Not used
111 * \param data HANGUPCAUSE function argument string
112 * \param buf Buffer to fill with read value.
113 * \param len Length of the buffer
114 *
115 * \retval 0 on success.
116 * \retval -1 on error.
117 */
118static int hangupcause_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
119{
120 char *parms;
121 struct ast_control_pvt_cause_code *cause_code;
122 int res = 0;
123 struct ast_str *causelist;
125 AST_APP_ARG(channel); /*!< Channel name */
126 AST_APP_ARG(type); /*!< Type of information requested (ast, tech or tech_extended) */
127 );
128
129 /* Ensure that the buffer is empty */
130 *buf = 0;
131
132 if (!chan) {
133 return -1;
134 }
135
136 parms = ast_strdupa(data);
138 if (args.argc != 2) {
139 /* Must have two arguments. */
140 ast_log(LOG_WARNING, "The HANGUPCAUSE function must have 2 parameters, not %u\n", args.argc);
141 return -1;
142 }
143
144 causelist = ast_str_create(128);
145 if (!causelist) {
146 ast_log(LOG_ERROR, "Unable to allocate buffer, cause information will be unavailable!\n");
147 return -1;
148 }
149
150 ast_channel_lock(chan);
151 if (!strcmp(args.type, "tech_extended")) {
152 struct ao2_iterator *cause_codes;
153 cause_codes = ast_channel_dialed_causes_find_multiple(chan, args.channel);
154 while ((cause_code = ao2_iterator_next(cause_codes))) {
155 if (!cause_code->cause_extended) {
156 ao2_ref(cause_code, -1);
157 continue;
158 }
159 ast_str_append(&causelist, 0, "%s%s", (ast_str_strlen(causelist) ? "," : ""), cause_code->code);
160 ao2_ref(cause_code, -1);
161 }
162 ao2_iterator_destroy(cause_codes);
163 } else {
164 cause_code = ast_channel_dialed_causes_find(chan, args.channel);
165 if (!cause_code) {
166 ast_log(LOG_WARNING, "Unable to find information for channel '%s'\n", args.channel);
167 ast_channel_unlock(chan);
168 return -1;
169 }
170 }
171 ast_channel_unlock(chan);
172
173 if (!strcmp(args.type, "ast")) {
175 } else if (!strcmp(args.type, "tech")) {
176 ast_copy_string(buf, cause_code->code, len);
177 } else if (!strcmp(args.type, "tech_extended")) {
179 } else {
180 ast_log(LOG_WARNING, "Information type not recognized (%s)\n", args.type);
181 res = -1;
182 }
183
184 if (cause_code) {
185 ao2_cleanup(cause_code);
186 }
187
188 return res;
189}
190
191/*!
192 * \internal
193 * \brief Read keys from the hangupcause ao2 container.
194 *
195 * \param chan Asterisk channel to read
196 * \param cmd Not used
197 * \param data HANGUPCAUSE_KEYS function argument string
198 * \param buf Buffer to fill with read value.
199 * \param len Length of the buffer
200 *
201 * \retval 0 on success.
202 * \retval -1 on error.
203 */
204static int hangupcause_keys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
205{
206 struct ast_str *chanlist;
207
208 /* Ensure that the buffer is empty */
209 *buf = 0;
210
211 if (!chan) {
212 return -1;
213 }
214
215 ast_channel_lock(chan);
217 ast_channel_unlock(chan);
218
221 }
222
224 return 0;
225}
226
227/*!
228 * \internal
229 * \brief Remove all keys from the hangupcause ao2 container.
230 *
231 * \param chan Asterisk channel to read
232 * \param data Not used
233 *
234 * \retval 0 on success.
235 * \retval -1 on error.
236 */
237static int hangupcause_clear_exec(struct ast_channel *chan, const char *data) {
238 ast_channel_lock(chan);
240 ast_channel_unlock(chan);
241 return 0;
242}
243
245 .name = "HANGUPCAUSE",
246 .read = hangupcause_read,
247};
248
250 .name = "HANGUPCAUSE_KEYS",
251 .read = hangupcause_keys_read,
252};
253
254static const char app[] = "HangupCauseClear";
255
256/*!
257 * \internal
258 * \brief Unload the function module
259 *
260 * \retval 0 on success.
261 * \retval -1 on error.
262 */
263static int unload_module(void)
264{
265 int res;
266
270 return res;
271}
272
273/*!
274 * \internal
275 * \brief Load and initialize the function module.
276 *
277 * \retval AST_MODULE_LOAD_SUCCESS on success.
278 * \retval AST_MODULE_LOAD_DECLINE on error.
279 */
289
290/* Do not wrap the following line. */
291AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "HANGUPCAUSE related functions and applications");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_log
Definition astobj2.c:42
#define ao2_iterator_next(iter)
Definition astobj2.h:1911
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static const char type[]
General Asterisk PBX channel definitions.
struct ast_control_pvt_cause_code * ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
Retrieve a ref-counted cause code information structure.
#define ast_channel_lock(chan)
Definition channel.h:2982
struct ast_str * ast_channel_dialed_causes_channels(const struct ast_channel *chan)
Retrieve a comma-separated list of channels for which dialed cause information is available.
const char * ast_cause2str(int cause) attribute_pure
Gives the string form of a given cause code.
Definition channel.c:612
struct ao2_iterator * ast_channel_dialed_causes_find_multiple(const struct ast_channel *chan, const char *chan_name)
Retrieve a ref-counted cause code information structure iterator.
void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
Clear all cause information from the channel.
#define ast_channel_unlock(chan)
Definition channel.h:2983
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static const char app[]
static int hangupcause_clear_exec(struct ast_channel *chan, const char *data)
static int hangupcause_keys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module(void)
static int unload_module(void)
static struct ast_custom_function hangupcause_function
static struct ast_custom_function hangupcause_keys_function
static int hangupcause_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition pbx_app.c:392
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition module.h:640
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct @519 args
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition strings.h:1139
size_t attribute_pure ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition strings.h:730
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition astobj2.h:1821
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition pbx.h:118
const char * name
Definition pbx.h:119
Support for dynamic strings.
Definition strings.h:623
List of channel drivers.
Definition app_dial.c:804
Utility functions.