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. See __manager_event for details */
  38. static int amihook_helper(int category, const char *event, char *content)
  39. {
  40. ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
  41. return 0;
  42. }
  43. static struct manager_custom_hook test_hook = {
  44. .file = __FILE__,
  45. .helper = &amihook_helper,
  46. };
  47. static int hook_send(void) {
  48. int res;
  49. /* Send a test action (core show version) to the AMI */
  50. res = ast_hook_send_action(&test_hook, "Action: Command\nCommand: core show version\nActionID: 987654321\n");
  51. return res;
  52. }
  53. static void register_hook(void) {
  54. /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
  55. ast_manager_unregister_hook(&test_hook);
  56. /* Register the hook for AMI events */
  57. ast_manager_register_hook(&test_hook);
  58. }
  59. static void unregister_hook(void) {
  60. /* Unregister the hook */
  61. ast_manager_unregister_hook(&test_hook);
  62. }
  63. static char *handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  64. {
  65. switch (cmd) {
  66. case CLI_INIT:
  67. e->command = "amihook send";
  68. e->usage = ""
  69. "Usage: amihook send"
  70. "";
  71. return NULL;
  72. case CLI_GENERATE:
  73. return NULL;
  74. case CLI_HANDLER:
  75. hook_send();
  76. return CLI_SUCCESS;
  77. }
  78. return CLI_FAILURE;
  79. }
  80. static char *handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  81. {
  82. switch (cmd) {
  83. case CLI_INIT:
  84. e->command = "amihook register";
  85. e->usage = ""
  86. "Usage: amihook register"
  87. "";
  88. return NULL;
  89. case CLI_GENERATE:
  90. return NULL;
  91. case CLI_HANDLER:
  92. register_hook();
  93. return CLI_SUCCESS;
  94. }
  95. return CLI_FAILURE;
  96. }
  97. static char *handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  98. {
  99. switch (cmd) {
  100. case CLI_INIT:
  101. e->command = "amihook unregister";
  102. e->usage = ""
  103. "Usage: amihook unregister"
  104. "";
  105. return NULL;
  106. case CLI_GENERATE:
  107. return NULL;
  108. case CLI_HANDLER:
  109. unregister_hook();
  110. return CLI_SUCCESS;
  111. }
  112. return CLI_FAILURE;
  113. }
  114. static struct ast_cli_entry cli_amihook_evt[] = {
  115. AST_CLI_DEFINE(handle_cli_amihook_send, "Send an AMI event"),
  116. AST_CLI_DEFINE(handle_cli_amihook_register_hook, "Register module for AMI hook"),
  117. AST_CLI_DEFINE(handle_cli_amihook_unregister_hook, "Unregister module for AMI hook"),
  118. };
  119. static int unload_module(void)
  120. {
  121. ast_manager_unregister_hook(&test_hook);
  122. return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
  123. }
  124. static int load_module(void)
  125. {
  126. int res;
  127. res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
  128. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  129. }
  130. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMI Hook Test Module");