| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (C) 1999-2012, Digium, Inc. |
| 5 | * |
| 6 | * Kinsey Moore <[email protected]> |
| 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 <[email protected]> |
| 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="ast"><para>Translated Asterisk cause code</para></enum> |
| 59 | </enumlist> |
| 60 | </parameter> |
| 61 | </syntax> |
| 62 | <description> |
| 63 | <para>Gets technology-specific or translated Asterisk cause code information |
| 64 | from the channel for the specified channel that resulted from a dial.</para> |
| 65 | </description> |
| 66 | <see-also> |
| 67 | <ref type="function">HANGUPCAUSE_KEYS</ref> |
| 68 | <ref type="application">HangupCauseClear</ref> |
| 69 | </see-also> |
| 70 | </function> |
| 71 | <function name="HANGUPCAUSE_KEYS" language="en_US"> |
| 72 | <since> |
| 73 | <version>11.0.0</version> |
| 74 | </since> |
| 75 | <synopsis> |
| 76 | Gets the list of channels for which hangup causes are available. |
| 77 | </synopsis> |
| 78 | <description> |
| 79 | <para>Returns a comma-separated list of channel names to be used with the HANGUPCAUSE function.</para> |
| 80 | </description> |
| 81 | <see-also> |
| 82 | <ref type="function">HANGUPCAUSE</ref> |
| 83 | <ref type="application">HangupCauseClear</ref> |
| 84 | </see-also> |
| 85 | </function> |
| 86 | <application name="HangupCauseClear" language="en_US"> |
| 87 | <since> |
| 88 | <version>11.0.0</version> |
| 89 | </since> |
| 90 | <synopsis> |
| 91 | Clears hangup cause information from the channel that is available through HANGUPCAUSE. |
| 92 | </synopsis> |
| 93 | <description> |
| 94 | <para>Clears all channel-specific hangup cause information from the channel. |
| 95 | This is never done automatically (i.e. for new Dial()s).</para> |
| 96 | </description> |
| 97 | <see-also> |
| 98 | <ref type="function">HANGUPCAUSE</ref> |
| 99 | <ref type="function">HANGUPCAUSE_KEYS</ref> |
| 100 | </see-also> |
| 101 | </application> |
| 102 | ***/ |
| 103 | |
| 104 | /*! |
| 105 | * \internal |
| 106 | * \brief Read values from the hangupcause ao2 container. |
| 107 | * |
| 108 | * \param chan Asterisk channel to read |
| 109 | * \param cmd Not used |
| 110 | * \param data HANGUPCAUSE function argument string |
| 111 | * \param buf Buffer to fill with read value. |
| 112 | * \param len Length of the buffer |
| 113 | * |
| 114 | * \retval 0 on success. |
| 115 | * \retval -1 on error. |
| 116 | */ |
| 117 | static int hangupcause_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 118 | { |
| 119 | char *parms; |
| 120 | struct ast_control_pvt_cause_code *cause_code; |
| 121 | int res = 0; |
| 122 | AST_DECLARE_APP_ARGS(args, |
| 123 | AST_APP_ARG(channel); /*!< Channel name */ |
| 124 | AST_APP_ARG(type); /*!< Type of information requested (ast or tech) */ |
| 125 | ); |
| 126 | |
| 127 | /* Ensure that the buffer is empty */ |
| 128 | *buf = 0; |
| 129 | |
| 130 | if (!chan) { |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | parms = ast_strdupa(data); |
| 135 | AST_STANDARD_APP_ARGS(args, parms); |
| 136 | if (args.argc != 2) { |
| 137 | /* Must have two arguments. */ |
| 138 | ast_log(LOG_WARNING, "The HANGUPCAUSE function must have 2 parameters, not %u\n", args.argc); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | ast_channel_lock(chan); |
| 143 | cause_code = ast_channel_dialed_causes_find(chan, args.channel); |
| 144 | ast_channel_unlock(chan); |
| 145 | |
| 146 | if (!cause_code) { |
| 147 | ast_log(LOG_WARNING, "Unable to find information for channel %s\n", args.channel); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (!strcmp(args.type, "ast")) { |
| 152 | ast_copy_string(buf, ast_cause2str(cause_code->ast_cause), len); |
| 153 | } else if (!strcmp(args.type, "tech")) { |
| 154 | ast_copy_string(buf, cause_code->code, len); |
| 155 | } else { |
| 156 | ast_log(LOG_WARNING, "Information type not recognized (%s)\n", args.type); |
| 157 | res = -1; |
| 158 | } |
| 159 | |
| 160 | ao2_ref(cause_code, -1); |
| 161 | |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | * \internal |
| 167 | * \brief Read keys from the hangupcause ao2 container. |
| 168 | * |
| 169 | * \param chan Asterisk channel to read |
| 170 | * \param cmd Not used |
| 171 | * \param data HANGUPCAUSE_KEYS function argument string |
| 172 | * \param buf Buffer to fill with read value. |
| 173 | * \param len Length of the buffer |
| 174 | * |
| 175 | * \retval 0 on success. |
| 176 | * \retval -1 on error. |
| 177 | */ |
| 178 | static int hangupcause_keys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 179 | { |
| 180 | struct ast_str *chanlist; |
| 181 | |
| 182 | /* Ensure that the buffer is empty */ |
| 183 | *buf = 0; |
| 184 | |
| 185 | if (!chan) { |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | ast_channel_lock(chan); |
| 190 | chanlist = ast_channel_dialed_causes_channels(chan); |
| 191 | ast_channel_unlock(chan); |
| 192 | |
| 193 | if (chanlist && ast_str_strlen(chanlist)) { |
| 194 | ast_copy_string(buf, ast_str_buffer(chanlist), len); |
| 195 | } |
| 196 | |
| 197 | ast_free(chanlist); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | * \internal |
| 203 | * \brief Remove all keys from the hangupcause ao2 container. |
| 204 | * |
| 205 | * \param chan Asterisk channel to read |
| 206 | * \param data Not used |
| 207 | * |
| 208 | * \retval 0 on success. |
| 209 | * \retval -1 on error. |
| 210 | */ |
| 211 | static int hangupcause_clear_exec(struct ast_channel *chan, const char *data) { |
| 212 | ast_channel_lock(chan); |
| 213 | ast_channel_dialed_causes_clear(chan); |
| 214 | ast_channel_unlock(chan); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static struct ast_custom_function hangupcause_function = { |
| 219 | .name = "HANGUPCAUSE", |
| 220 | .read = hangupcause_read, |
| 221 | }; |
| 222 | |
| 223 | static struct ast_custom_function hangupcause_keys_function = { |
| 224 | .name = "HANGUPCAUSE_KEYS", |
| 225 | .read = hangupcause_keys_read, |
| 226 | }; |
| 227 | |
| 228 | static const char app[] = "HangupCauseClear"; |
| 229 | |
| 230 | /*! |
| 231 | * \internal |
| 232 | * \brief Unload the function module |
| 233 | * |
| 234 | * \retval 0 on success. |
| 235 | * \retval -1 on error. |
| 236 | */ |
| 237 | static int unload_module(void) |
| 238 | { |
| 239 | int res; |
| 240 | |
| 241 | res = ast_custom_function_unregister(&hangupcause_function); |
| 242 | res |= ast_custom_function_unregister(&hangupcause_keys_function); |
| 243 | res |= ast_unregister_application(app); |
| 244 | return res; |
| 245 | } |
| 246 | |
| 247 | /*! |
| 248 | * \internal |
| 249 | * \brief Load and initialize the function module. |
| 250 | * |
| 251 | * \retval AST_MODULE_LOAD_SUCCESS on success. |
| 252 | * \retval AST_MODULE_LOAD_DECLINE on error. |
| 253 | */ |
| 254 | static int load_module(void) |
| 255 | { |
| 256 | int res; |
| 257 | |
| 258 | res = ast_custom_function_register(&hangupcause_function); |
| 259 | res |= ast_custom_function_register(&hangupcause_keys_function); |
| 260 | res |= ast_register_application_xml(app, hangupcause_clear_exec); |
| 261 | return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 262 | } |
| 263 | |
| 264 | /* Do not wrap the following line. */ |
| 265 | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "HANGUPCAUSE related functions and applications"); |