ami_testhooks.c automatically registers hook
[asterisk/asterisk.git] / tests / test_amihooks.c
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
19 /*! \file
20  *
21  * \brief Test AMI hook
22  *
23  * \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com>
24  *
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.
27  */
28
29 /*** MODULEINFO
30         <defaultenabled>no</defaultenabled>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/module.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/manager.h"
41
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)
44 {
45         ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
46         return 0;
47 }
48
49 static struct manager_custom_hook test_hook = {
50         .file = __FILE__,
51         .helper = &amihook_helper,
52 };
53
54 static int hook_send(void) {
55         int res;
56
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");
59
60         return res;
61 }
62
63 static void register_hook(void) {
64
65         /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
66         ast_manager_unregister_hook(&test_hook);
67
68         /* Register the hook for AMI events */
69         ast_manager_register_hook(&test_hook);
70
71 }
72
73 static void unregister_hook(void) {
74
75         /* Unregister the hook */
76         ast_manager_unregister_hook(&test_hook);
77
78 }
79
80 static char *handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
81 {
82         switch (cmd) {
83         case CLI_INIT:
84                 e->command = "amihook send";
85                 e->usage = ""
86                         "Usage: amihook send"
87                         "";
88                 return NULL;
89         case CLI_GENERATE:
90                 return NULL;
91         case CLI_HANDLER:
92                 hook_send();
93                 return CLI_SUCCESS;
94         }
95
96         return CLI_FAILURE;
97 }
98
99 static char *handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
100 {
101         switch (cmd) {
102         case CLI_INIT:
103                 e->command = "amihook register";
104                 e->usage = ""
105                         "Usage: amihook register"
106                         "";
107                 return NULL;
108         case CLI_GENERATE:
109                 return NULL;
110         case CLI_HANDLER:
111                 register_hook();
112                 return CLI_SUCCESS;
113         }
114
115         return CLI_FAILURE;
116 }
117
118 static char *handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
119 {
120         switch (cmd) {
121         case CLI_INIT:
122                 e->command = "amihook unregister";
123                 e->usage = ""
124                         "Usage: amihook unregister"
125                         "";
126                 return NULL;
127         case CLI_GENERATE:
128                 return NULL;
129         case CLI_HANDLER:
130                 unregister_hook();
131                 return CLI_SUCCESS;
132         }
133
134         return CLI_FAILURE;
135 }
136
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"),
141 };
142
143 static int unload_module(void)
144 {
145         ast_manager_unregister_hook(&test_hook);
146         return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
147 }
148
149 static int load_module(void)
150 {
151         int res;
152
153         res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
154
155         return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
156 }
157
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMI Hook Test Module");