test_amihooks.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * David Brooks <dbrooks@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 Test AMI hook
  21. *
  22. * \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com>
  23. *
  24. * This is simply an example or test module illustrating the ability for a custom module
  25. * to hook into AMI. Registration for AMI events and sending of AMI actions is shown.
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/module.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/manager.h"
  37. /* The helper function is required by struct manager_custom_hook.
  38. * See __ast_manager_event_multichan for details */
  39. static int amihook_helper(int category, const char *event, char *content)
  40. {
  41. ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
  42. return 0;
  43. }
  44. static struct manager_custom_hook test_hook = {
  45. .file = __FILE__,
  46. .helper = &amihook_helper,
  47. };
  48. static int hook_send(void) {
  49. int res;
  50. /* Send a test action (core show version) to the AMI */
  51. res = ast_hook_send_action(&test_hook, "Action: Command\nCommand: core show version\nActionID: 987654321\n");
  52. return res;
  53. }
  54. static void register_hook(void) {
  55. /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
  56. ast_manager_unregister_hook(&test_hook);
  57. /* Register the hook for AMI events */
  58. ast_manager_register_hook(&test_hook);
  59. }
  60. static void unregister_hook(void) {
  61. /* Unregister the hook */
  62. ast_manager_unregister_hook(&test_hook);
  63. }
  64. static char *handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  65. {
  66. switch (cmd) {
  67. case CLI_INIT:
  68. e->command = "amihook send";
  69. e->usage = ""
  70. "Usage: amihook send"
  71. "";
  72. return NULL;
  73. case CLI_GENERATE:
  74. return NULL;
  75. case CLI_HANDLER:
  76. hook_send();
  77. return CLI_SUCCESS;
  78. }
  79. return CLI_FAILURE;
  80. }
  81. static char *handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  82. {
  83. switch (cmd) {
  84. case CLI_INIT:
  85. e->command = "amihook register";
  86. e->usage = ""
  87. "Usage: amihook register"
  88. "";
  89. return NULL;
  90. case CLI_GENERATE:
  91. return NULL;
  92. case CLI_HANDLER:
  93. register_hook();
  94. return CLI_SUCCESS;
  95. }
  96. return CLI_FAILURE;
  97. }
  98. static char *handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  99. {
  100. switch (cmd) {
  101. case CLI_INIT:
  102. e->command = "amihook unregister";
  103. e->usage = ""
  104. "Usage: amihook unregister"
  105. "";
  106. return NULL;
  107. case CLI_GENERATE:
  108. return NULL;
  109. case CLI_HANDLER:
  110. unregister_hook();
  111. return CLI_SUCCESS;
  112. }
  113. return CLI_FAILURE;
  114. }
  115. static struct ast_cli_entry cli_amihook_evt[] = {
  116. AST_CLI_DEFINE(handle_cli_amihook_send, "Send an AMI event"),
  117. AST_CLI_DEFINE(handle_cli_amihook_register_hook, "Register module for AMI hook"),
  118. AST_CLI_DEFINE(handle_cli_amihook_unregister_hook, "Unregister module for AMI hook"),
  119. };
  120. static int unload_module(void)
  121. {
  122. ast_manager_unregister_hook(&test_hook);
  123. return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
  124. }
  125. static int load_module(void)
  126. {
  127. int res;
  128. res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
  129. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  130. }
  131. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMI Hook Test Module");