Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
Macros | Functions | Variables
test_amihooks.c File Reference

Test AMI hook. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
#include "asterisk/test.h"
Include dependency graph for test_amihooks.c:

Go to the source code of this file.

Macros

#define CATEGORY   "/main/amihooks/"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int amihook_helper (int category, const char *event, char *content)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (amihook_cli_send)
 
static char * handle_cli_amihook_register_hook (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_amihook_send (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_amihook_unregister_hook (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int hook_send (void)
 
static int load_module (void)
 
static void register_hook (void)
 
static int unload_module (void)
 
static void unregister_hook (void)
 
static int wait_for_hook (struct ast_test *test)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "AMI Hook Test Module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_amihook_evt []
 
int done
 
ast_cond_t hook_cond
 
static ast_mutex_t hook_lock = AST_MUTEX_INIT_VALUE
 
static struct manager_custom_hook test_hook
 

Detailed Description

Test AMI hook.

Author
David Brooks dbroo.nosp@m.ks@d.nosp@m.igium.nosp@m..com based off of code written by Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

This started, and continues to serves, as an example illustrating the ability for a custom module to hook into AMI. Registration for AMI events and sending of AMI actions is shown. A test has also been created that utilizes the original example in order to make sure the ami event hook gets raised.

Definition in file test_amihooks.c.

Macro Definition Documentation

◆ CATEGORY

#define CATEGORY   "/main/amihooks/"

Definition at line 44 of file test_amihooks.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 218 of file test_amihooks.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 218 of file test_amihooks.c.

◆ amihook_helper()

static int amihook_helper ( int  category,
const char *  event,
char *  content 
)
static

Definition at line 95 of file test_amihooks.c.

96{
97 ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
98
100 done = 1;
103 return 0;
104}
#define ast_log
Definition astobj2.c:42
#define LOG_NOTICE
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_mutex_lock(a)
Definition lock.h:196
#define ast_cond_signal(cond)
Definition lock.h:210
ast_cond_t hook_cond
int done
static ast_mutex_t hook_lock

References ast_cond_signal, ast_log, ast_mutex_lock, ast_mutex_unlock, done, hook_cond, hook_lock, and LOG_NOTICE.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 218 of file test_amihooks.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( amihook_cli_send  )

Definition at line 72 of file test_amihooks.c.

73{
74 switch (cmd) {
75 case TEST_INIT:
76 info->name = __func__;
77 info->category = CATEGORY;
78 info->summary = "Execute an action using an AMI hook";
79 info->description = info->summary;
80 return AST_TEST_NOT_RUN;
81 case TEST_EXECUTE:
82 break;
83 }
84
85 done = 0;
86 if (ast_cli_command(-1, "amihook send")) {
87 return AST_TEST_FAIL;
88 }
89
91}
#define ast_cli_command(fd, s)
Definition cli.h:232
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_FAIL
Definition test.h:196
@ AST_TEST_NOT_RUN
Definition test.h:194
static int wait_for_hook(struct ast_test *test)
#define CATEGORY

References ast_cli_command, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, done, TEST_EXECUTE, TEST_INIT, and wait_for_hook().

◆ handle_cli_amihook_register_hook()

static char * handle_cli_amihook_register_hook ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 156 of file test_amihooks.c.

157{
158 switch (cmd) {
159 case CLI_INIT:
160 e->command = "amihook register";
161 e->usage = ""
162 "Usage: amihook register"
163 "";
164 return NULL;
165 case CLI_GENERATE:
166 return NULL;
167 case CLI_HANDLER:
169 return CLI_SUCCESS;
170 }
171
172 return CLI_FAILURE;
173}
#define CLI_SUCCESS
Definition cli.h:44
@ CLI_HANDLER
Definition cli.h:154
@ CLI_INIT
Definition cli.h:152
@ CLI_GENERATE
Definition cli.h:153
#define CLI_FAILURE
Definition cli.h:46
#define NULL
Definition resample.c:96
char * command
Definition cli.h:186
const char * usage
Definition cli.h:177
static void register_hook(void)

References CLI_FAILURE, CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, NULL, register_hook(), and ast_cli_entry::usage.

◆ handle_cli_amihook_send()

static char * handle_cli_amihook_send ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 137 of file test_amihooks.c.

138{
139 switch (cmd) {
140 case CLI_INIT:
141 e->command = "amihook send";
142 e->usage = ""
143 "Usage: amihook send"
144 "";
145 return NULL;
146 case CLI_GENERATE:
147 return NULL;
148 case CLI_HANDLER:
149 hook_send();
150 return CLI_SUCCESS;
151 }
152
153 return CLI_FAILURE;
154}
static int hook_send(void)

References CLI_FAILURE, CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, hook_send(), NULL, and ast_cli_entry::usage.

◆ handle_cli_amihook_unregister_hook()

static char * handle_cli_amihook_unregister_hook ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 175 of file test_amihooks.c.

176{
177 switch (cmd) {
178 case CLI_INIT:
179 e->command = "amihook unregister";
180 e->usage = ""
181 "Usage: amihook unregister"
182 "";
183 return NULL;
184 case CLI_GENERATE:
185 return NULL;
186 case CLI_HANDLER:
188 return CLI_SUCCESS;
189 }
190
191 return CLI_FAILURE;
192}
static void unregister_hook(void)

References CLI_FAILURE, CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, NULL, unregister_hook(), and ast_cli_entry::usage.

◆ hook_send()

static int hook_send ( void  )
static

Definition at line 111 of file test_amihooks.c.

111 {
112 int res;
113
114 /* Send a test action (core show version) to the AMI */
115 res = ast_hook_send_action(&test_hook, "Action: Command\nCommand: core show version\nActionID: 987654321\n");
116
117 return res;
118}
int ast_hook_send_action(struct manager_custom_hook *hook, const char *msg)
Registered hooks can call this function to invoke actions and they will receive responses through reg...
Definition manager.c:1760
static struct manager_custom_hook test_hook

References ast_hook_send_action(), and test_hook.

Referenced by handle_cli_amihook_send().

◆ load_module()

static int load_module ( void  )
static

Definition at line 207 of file test_amihooks.c.

208{
209 int res;
210
212
213 AST_TEST_REGISTER(amihook_cli_send);
214
216}
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition cli.h:265
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
#define AST_TEST_REGISTER(cb)
Definition test.h:127
static struct ast_cli_entry cli_amihook_evt[]
#define ARRAY_LEN(a)
Definition utils.h:703

References ARRAY_LEN, ast_cli_register_multiple, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, and cli_amihook_evt.

◆ register_hook()

static void register_hook ( void  )
static

Definition at line 120 of file test_amihooks.c.

120 {
121
122 /* Unregister the hook, we don't want a double-registration (Bad Things(tm) happen) */
124
125 /* Register the hook for AMI events */
127
128}
void ast_manager_unregister_hook(struct manager_custom_hook *hook)
Delete a custom hook to be called when an event is fired.
Definition manager.c:673
void ast_manager_register_hook(struct manager_custom_hook *hook)
Add a custom hook to be called when an event is fired.
Definition manager.c:665

References ast_manager_register_hook(), ast_manager_unregister_hook(), and test_hook.

Referenced by handle_cli_amihook_register_hook().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 200 of file test_amihooks.c.

201{
202 AST_TEST_UNREGISTER(amihook_cli_send);
205}
void ast_cli_unregister_multiple(void)
Definition ael_main.c:408
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_manager_unregister_hook(), AST_TEST_UNREGISTER, cli_amihook_evt, and test_hook.

◆ unregister_hook()

static void unregister_hook ( void  )
static

Definition at line 130 of file test_amihooks.c.

130 {
131
132 /* Unregister the hook */
134
135}

References ast_manager_unregister_hook(), and test_hook.

Referenced by handle_cli_amihook_unregister_hook().

◆ wait_for_hook()

static int wait_for_hook ( struct ast_test *  test)
static

Definition at line 50 of file test_amihooks.c.

51{
52 struct timeval start = ast_tvnow();
53 struct timespec timeout = {
54 .tv_sec = start.tv_sec + 2,
55 .tv_nsec = start.tv_usec * 1000
56 };
57 int res = 0;
58
60 while (!done) {
61 if (ast_cond_timedwait(&hook_cond, &hook_lock, &timeout) == ETIMEDOUT) {
62 ast_test_status_update(test, "Test timed out while waiting for hook event\n");
63 res = -1;
64 break;
65 }
66 }
68
69 return res;
70}
#define ast_cond_timedwait(cond, mutex, time)
Definition lock.h:213
#define ast_test_status_update(a, b, c...)
Definition test.h:129
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition time.h:159

References ast_cond_timedwait, ast_mutex_lock, ast_mutex_unlock, ast_test_status_update, ast_tvnow(), done, hook_cond, and hook_lock.

Referenced by AST_TEST_DEFINE().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "AMI Hook Test Module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 218 of file test_amihooks.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 218 of file test_amihooks.c.

◆ cli_amihook_evt

struct ast_cli_entry cli_amihook_evt[]
static
Initial value:
= {
{ .handler = handle_cli_amihook_send , .summary = "Send an AMI event" ,},
{ .handler = handle_cli_amihook_register_hook , .summary = "Register module for AMI hook" ,},
{ .handler = handle_cli_amihook_unregister_hook , .summary = "Unregister module for AMI hook" ,},
}
static char * handle_cli_amihook_register_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_amihook_send(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_amihook_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Definition at line 194 of file test_amihooks.c.

194 {
195 AST_CLI_DEFINE(handle_cli_amihook_send, "Send an AMI event"),
196 AST_CLI_DEFINE(handle_cli_amihook_register_hook, "Register module for AMI hook"),
197 AST_CLI_DEFINE(handle_cli_amihook_unregister_hook, "Unregister module for AMI hook"),
198};
#define AST_CLI_DEFINE(fn, txt,...)
Definition cli.h:197

Referenced by load_module(), and unload_module().

◆ done

int done

◆ hook_cond

ast_cond_t hook_cond

Definition at line 47 of file test_amihooks.c.

Referenced by amihook_helper(), and wait_for_hook().

◆ hook_lock

ast_mutex_t hook_lock = AST_MUTEX_INIT_VALUE
static

Definition at line 46 of file test_amihooks.c.

Referenced by amihook_helper(), and wait_for_hook().

◆ test_hook

struct manager_custom_hook test_hook
static
Initial value:
= {
.file = __FILE__,
.helper = &amihook_helper,
}
static int amihook_helper(int category, const char *event, char *content)

Definition at line 106 of file test_amihooks.c.

106 {
107 .file = __FILE__,
108 .helper = &amihook_helper,
109};

Referenced by hook_send(), register_hook(), unload_module(), and unregister_hook().