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>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/manager.h"
42 /* The helper function is required by struct manager_custom_hook. See __manager_event for details */
43 static int amihook_helper(int category, const char *event, char *content)
45 ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
49 static struct manager_custom_hook test_hook = {
51 .helper = &amihook_helper,
54 static int hook_send(void) {
57 /* Send a test action (core show version) to the AMI */
58 res = ast_hook_send_action(&test_hook, "Action: Command\nCommand: core show version\nActionID: 987654321\n");
63 static void register_hook(void) {
65 /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
66 ast_manager_unregister_hook(&test_hook);
68 /* Register the hook for AMI events */
69 ast_manager_register_hook(&test_hook);
73 static void unregister_hook(void) {
75 /* Unregister the hook */
76 ast_manager_unregister_hook(&test_hook);
80 static char *handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
84 e->command = "amihook send";
99 static char *handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
103 e->command = "amihook register";
105 "Usage: amihook register"
118 static char *handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
122 e->command = "amihook unregister";
124 "Usage: amihook unregister"
137 static struct ast_cli_entry cli_amihook_evt[] = {
138 AST_CLI_DEFINE(handle_cli_amihook_send, "Send an AMI event"),
139 AST_CLI_DEFINE(handle_cli_amihook_register_hook, "Register module for AMI hook"),
140 AST_CLI_DEFINE(handle_cli_amihook_unregister_hook, "Unregister module for AMI hook"),
143 static int unload_module(void)
145 ast_manager_unregister_hook(&test_hook);
146 return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
149 static int load_module(void)
153 res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
155 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMI Hook Test Module");