2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Digium, Inc.
6 * David Brooks <dbrooks@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 Test AMI hook
23 * \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com>
25 * This is simply an example or test module illustrating the ability for a custom module
26 * to hook into AMI. Registration for AMI events and sending of AMI actions is shown.
30 <defaultenabled>no</defaultenabled>
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/module.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/manager.h"
43 /* The helper function is required by struct manager_custom_hook. See __manager_event for details */
44 static int amihook_helper(int category, const char *event, char *content)
46 ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
50 static struct manager_custom_hook test_hook = {
52 .helper = &amihook_helper,
55 static int hook_send(void) {
58 /* Send a test action (core show version) to the AMI */
59 res = ast_hook_send_action(&test_hook, "Action: Command\nCommand: core show version\nActionID: 987654321\n");
64 static void register_hook(void) {
66 /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
67 ast_manager_unregister_hook(&test_hook);
69 /* Register the hook for AMI events */
70 ast_manager_register_hook(&test_hook);
74 static void unregister_hook(void) {
76 /* Unregister the hook */
77 ast_manager_unregister_hook(&test_hook);
81 static char *handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
85 e->command = "amihook send";
100 static char *handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
104 e->command = "amihook register";
106 "Usage: amihook register"
119 static char *handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
123 e->command = "amihook unregister";
125 "Usage: amihook unregister"
138 static struct ast_cli_entry cli_amihook_evt[] = {
139 AST_CLI_DEFINE(handle_cli_amihook_send, "Send an AMI event"),
140 AST_CLI_DEFINE(handle_cli_amihook_register_hook, "Register module for AMI hook"),
141 AST_CLI_DEFINE(handle_cli_amihook_unregister_hook, "Unregister module for AMI hook"),
144 static int unload_module(void)
146 ast_manager_unregister_hook(&test_hook);
147 return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
150 static int load_module(void)
154 res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
156 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMI Hook Test Module");