2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999-2012, Digium, Inc.
6 * Kinsey Moore <kmoore@digium.com>
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.
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.
21 * \brief Functions related to retreiving per-channel hangupcause information
23 * \author Kinsey Moore <kmoore@digium.com>
27 * \arg \ref AstCREDITS
31 <support_level>core</support_level>
36 ASTERISK_REGISTER_FILE()
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
45 <function name="HANGUPCAUSE" language="en_US">
47 Gets per-channel hangupcause information from the channel.
50 <parameter name="channel" required="true">
51 <para>The name of the channel for which to retrieve cause information.</para>
53 <parameter name="type" required="true">
54 <para>Parameter describing which type of information is requested. Types are:</para>
56 <enum name="tech"><para>Technology-specific cause information</para></enum>
57 <enum name="ast"><para>Translated Asterisk cause code</para></enum>
62 <para>Gets technology-specific or translated Asterisk cause code information
63 from the channel for the specified channel that resulted from a dial.</para>
66 <ref type="function">HANGUPCAUSE_KEYS</ref>
67 <ref type="application">HangupCauseClear</ref>
70 <function name="HANGUPCAUSE_KEYS" language="en_US">
72 Gets the list of channels for which hangup causes are available.
75 <para>Returns a comma-separated list of channel names to be used with the HANGUPCAUSE function.</para>
78 <ref type="function">HANGUPCAUSE</ref>
79 <ref type="application">HangupCauseClear</ref>
82 <application name="HangupCauseClear" language="en_US">
84 Clears hangup cause information from the channel that is available through HANGUPCAUSE.
87 <para>Clears all channel-specific hangup cause information from the channel.
88 This is never done automatically (i.e. for new Dial()s).</para>
91 <ref type="function">HANGUPCAUSE</ref>
92 <ref type="function">HANGUPCAUSE_KEYS</ref>
99 * \brief Read values from the hangupcause ao2 container.
101 * \param chan Asterisk channel to read
102 * \param cmd Not used
103 * \param data HANGUPCAUSE function argument string
104 * \param buf Buffer to fill with read value.
105 * \param len Length of the buffer
107 * \retval 0 on success.
108 * \retval -1 on error.
110 static int hangupcause_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
113 struct ast_control_pvt_cause_code *cause_code;
115 AST_DECLARE_APP_ARGS(args,
116 AST_APP_ARG(channel); /*!< Channel name */
117 AST_APP_ARG(type); /*!< Type of information requested (ast or tech) */
120 /* Ensure that the buffer is empty */
127 parms = ast_strdupa(data);
128 AST_STANDARD_APP_ARGS(args, parms);
129 if (args.argc != 2) {
130 /* Must have two arguments. */
131 ast_log(LOG_WARNING, "The HANGUPCAUSE function must have 2 parameters, not %u\n", args.argc);
135 ast_channel_lock(chan);
136 cause_code = ast_channel_dialed_causes_find(chan, args.channel);
137 ast_channel_unlock(chan);
140 ast_log(LOG_WARNING, "Unable to find information for channel %s\n", args.channel);
144 if (!strcmp(args.type, "ast")) {
145 ast_copy_string(buf, ast_cause2str(cause_code->ast_cause), len);
146 } else if (!strcmp(args.type, "tech")) {
147 ast_copy_string(buf, cause_code->code, len);
149 ast_log(LOG_WARNING, "Information type not recognized (%s)\n", args.type);
153 ao2_ref(cause_code, -1);
160 * \brief Read keys from the hangupcause ao2 container.
162 * \param chan Asterisk channel to read
163 * \param cmd Not used
164 * \param data HANGUPCAUSE_KEYS function argument string
165 * \param buf Buffer to fill with read value.
166 * \param len Length of the buffer
168 * \retval 0 on success.
169 * \retval -1 on error.
171 static int hangupcause_keys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
173 struct ast_str *chanlist;
175 /* Ensure that the buffer is empty */
182 ast_channel_lock(chan);
183 chanlist = ast_channel_dialed_causes_channels(chan);
184 ast_channel_unlock(chan);
186 if (chanlist && ast_str_strlen(chanlist)) {
187 ast_copy_string(buf, ast_str_buffer(chanlist), len);
196 * \brief Remove all keys from the hangupcause ao2 container.
198 * \param chan Asterisk channel to read
199 * \param data Not used
201 * \retval 0 on success.
202 * \retval -1 on error.
204 static int hangupcause_clear_exec(struct ast_channel *chan, const char *data) {
205 ast_channel_lock(chan);
206 ast_channel_dialed_causes_clear(chan);
207 ast_channel_unlock(chan);
211 static struct ast_custom_function hangupcause_function = {
212 .name = "HANGUPCAUSE",
213 .read = hangupcause_read,
216 static struct ast_custom_function hangupcause_keys_function = {
217 .name = "HANGUPCAUSE_KEYS",
218 .read = hangupcause_keys_read,
221 static const char app[] = "HangupCauseClear";
225 * \brief Unload the function module
227 * \retval 0 on success.
228 * \retval -1 on error.
230 static int unload_module(void)
234 res = ast_custom_function_unregister(&hangupcause_function);
235 res |= ast_custom_function_unregister(&hangupcause_keys_function);
236 res |= ast_unregister_application(app);
242 * \brief Load and initialize the function module.
244 * \retval AST_MODULE_LOAD_SUCCESS on success.
245 * \retval AST_MODULE_LOAD_DECLINE on error.
247 static int load_module(void)
251 res = ast_custom_function_register(&hangupcause_function);
252 res |= ast_custom_function_register(&hangupcause_keys_function);
253 res |= ast_register_application_xml(app, hangupcause_clear_exec);
254 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
257 /* Do not wrap the following line. */
258 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "HANGUPCAUSE related functions and applications");