func_holdintercept.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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. /*! \file
  19. *
  20. * \brief Function that intercepts HOLD frames from channels and raises events
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/stasis.h"
  37. #include "asterisk/stasis_channels.h"
  38. /*** DOCUMENTATION
  39. <function name="HOLD_INTERCEPT" language="en_US">
  40. <synopsis>
  41. Intercepts hold frames on a channel and raises an event instead of passing the frame on
  42. </synopsis>
  43. <syntax>
  44. <parameter name="action" required="true">
  45. <optionlist>
  46. <option name="remove">
  47. <para>W/O. Removes the hold interception function.</para>
  48. </option>
  49. <option name="set">
  50. <para>W/O. Enable hold interception on the channel. When
  51. enabled, the channel will intercept any hold action that
  52. is signalled from the device, and instead simply raise an
  53. event (AMI/ARI) indicating that the channel wanted to put other
  54. parties on hold.</para>
  55. </option>
  56. </optionlist>
  57. </parameter>
  58. </syntax>
  59. </function>
  60. ***/
  61. /*! \brief Private data structure used with the function's datastore */
  62. struct hold_intercept_data {
  63. int framehook_id;
  64. };
  65. /*! \brief The channel datastore the function uses to store state */
  66. static const struct ast_datastore_info hold_intercept_datastore = {
  67. .type = "hold_intercept",
  68. };
  69. /*! \internal \brief Disable hold interception on the channel */
  70. static int remove_hold_intercept(struct ast_channel *chan)
  71. {
  72. struct ast_datastore *datastore = NULL;
  73. struct hold_intercept_data *data;
  74. SCOPED_CHANNELLOCK(chan_lock, chan);
  75. datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL);
  76. if (!datastore) {
  77. ast_log(AST_LOG_WARNING, "Cannot remove HOLD_INTERCEPT from %s: HOLD_INTERCEPT not currently enabled\n",
  78. ast_channel_name(chan));
  79. return -1;
  80. }
  81. data = datastore->data;
  82. if (ast_framehook_detach(chan, data->framehook_id)) {
  83. ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT framehook from channel %s\n",
  84. ast_channel_name(chan));
  85. return -1;
  86. }
  87. if (ast_channel_datastore_remove(chan, datastore)) {
  88. ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT datastore from channel %s\n",
  89. ast_channel_name(chan));
  90. return -1;
  91. }
  92. ast_datastore_free(datastore);
  93. return 0;
  94. }
  95. /*! \brief Frame hook that is called to intercept hold/unhold */
  96. static struct ast_frame *hold_intercept_framehook(struct ast_channel *chan,
  97. struct ast_frame *f, enum ast_framehook_event event, void *data)
  98. {
  99. int frame_type;
  100. if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
  101. return f;
  102. }
  103. if (f->frametype != AST_FRAME_CONTROL) {
  104. return f;
  105. }
  106. frame_type = f->subclass.integer;
  107. if (frame_type != AST_CONTROL_HOLD && frame_type != AST_CONTROL_UNHOLD) {
  108. return f;
  109. }
  110. /* Munch munch */
  111. ast_frfree(f);
  112. f = &ast_null_frame;
  113. ast_channel_publish_cached_blob(chan,
  114. frame_type == AST_CONTROL_HOLD ? ast_channel_hold_type() : ast_channel_unhold_type(),
  115. NULL);
  116. return f;
  117. }
  118. /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
  119. static int hold_intercept_framehook_consume(void *data, enum ast_frame_type type)
  120. {
  121. return (type == AST_FRAME_CONTROL ? 1 : 0);
  122. }
  123. /*! \internal \brief Enable hold interception on the channel */
  124. static int set_hold_intercept(struct ast_channel *chan)
  125. {
  126. struct ast_datastore *datastore;
  127. struct hold_intercept_data *data;
  128. static struct ast_framehook_interface hold_framehook_interface = {
  129. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  130. .event_cb = hold_intercept_framehook,
  131. .consume_cb = hold_intercept_framehook_consume,
  132. .disable_inheritance = 1,
  133. };
  134. SCOPED_CHANNELLOCK(chan_lock, chan);
  135. datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL);
  136. if (datastore) {
  137. ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT already set on '%s'\n",
  138. ast_channel_name(chan));
  139. return 0;
  140. }
  141. datastore = ast_datastore_alloc(&hold_intercept_datastore, NULL);
  142. if (!datastore) {
  143. return -1;
  144. }
  145. data = ast_calloc(1, sizeof(*data));
  146. if (!data) {
  147. ast_datastore_free(datastore);
  148. return -1;
  149. }
  150. data->framehook_id = ast_framehook_attach(chan, &hold_framehook_interface);
  151. if (data->framehook_id < 0) {
  152. ast_log(AST_LOG_WARNING, "Failed to attach HOLD_INTERCEPT framehook to '%s'\n",
  153. ast_channel_name(chan));
  154. ast_datastore_free(datastore);
  155. ast_free(data);
  156. return -1;
  157. }
  158. datastore->data = data;
  159. ast_channel_datastore_add(chan, datastore);
  160. return 0;
  161. }
  162. /*! \internal \brief HOLD_INTERCEPT write function callback */
  163. static int hold_intercept_fn_write(struct ast_channel *chan, const char *function,
  164. char *data, const char *value)
  165. {
  166. int res;
  167. if (!chan) {
  168. return -1;
  169. }
  170. if (ast_strlen_zero(data)) {
  171. ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT requires an argument\n");
  172. return -1;
  173. }
  174. if (!strcasecmp(data, "set")) {
  175. res = set_hold_intercept(chan);
  176. } else if (!strcasecmp(data, "remove")) {
  177. res = remove_hold_intercept(chan);
  178. } else {
  179. ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT: unknown option %s\n", data);
  180. res = -1;
  181. }
  182. return res;
  183. }
  184. /*! \brief Definition of the HOLD_INTERCEPT function */
  185. static struct ast_custom_function hold_intercept_function = {
  186. .name = "HOLD_INTERCEPT",
  187. .write = hold_intercept_fn_write,
  188. };
  189. /*! \internal \brief Unload the module */
  190. static int unload_module(void)
  191. {
  192. return ast_custom_function_unregister(&hold_intercept_function);
  193. }
  194. /*! \internal \brief Load the module */
  195. static int load_module(void)
  196. {
  197. return ast_custom_function_register(&hold_intercept_function) ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  198. }
  199. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hold interception dialplan function");