Asterisk - The Open Source Telephony Project  GIT-master-1f78ee9
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"

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 = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 
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.

Referenced by AST_TEST_DEFINE().

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.

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

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 }
static ast_mutex_t hook_lock
Definition: test_amihooks.c:46
Definition: astman.c:222
ast_cond_t hook_cond
Definition: test_amihooks.c:47
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_cond_signal(cond)
Definition: lock.h:201
int done
Definition: test_amihooks.c:48
#define ast_log
Definition: astobj2.c:42
#define LOG_NOTICE
Definition: logger.h:263
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ 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.

References AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, done, sip_to_pjsip::info(), TEST_EXECUTE, TEST_INIT, and wait_for_hook().

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 }
static int wait_for_hook(struct ast_test *test)
Definition: test_amihooks.c:50
#define CATEGORY
Definition: test_amihooks.c:44
int done
Definition: test_amihooks.c:48
ast_cli_command
calling arguments for new-style handlers.
Definition: cli.h:151
def info(msg)

◆ 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.

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

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:
168  register_hook();
169  return CLI_SUCCESS;
170  }
171 
172  return CLI_FAILURE;
173 }
Definition: cli.h:152
#define NULL
Definition: resample.c:96
#define CLI_FAILURE
Definition: cli.h:46
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
static void register_hook(void)

◆ 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.

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

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)
Definition: cli.h:152
#define NULL
Definition: resample.c:96
#define CLI_FAILURE
Definition: cli.h:46
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ 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.

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

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:
187  unregister_hook();
188  return CLI_SUCCESS;
189  }
190 
191  return CLI_FAILURE;
192 }
Definition: cli.h:152
static void unregister_hook(void)
#define NULL
Definition: resample.c:96
#define CLI_FAILURE
Definition: cli.h:46
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ hook_send()

static int hook_send ( void  )
static

Definition at line 111 of file test_amihooks.c.

References ast_hook_send_action().

Referenced by handle_cli_amihook_send().

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:2937
static struct manager_custom_hook test_hook

◆ load_module()

static int load_module ( void  )
static

Definition at line 207 of file test_amihooks.c.

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

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

◆ register_hook()

static void register_hook ( void  )
static

Definition at line 120 of file test_amihooks.c.

References ast_manager_register_hook(), and ast_manager_unregister_hook().

Referenced by handle_cli_amihook_register_hook().

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:1942
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:1934
static struct manager_custom_hook test_hook

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 200 of file test_amihooks.c.

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

201 {
202  AST_TEST_UNREGISTER(amihook_cli_send);
205 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
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:1942
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
static struct manager_custom_hook test_hook
static struct ast_cli_entry cli_amihook_evt[]

◆ unregister_hook()

static void unregister_hook ( void  )
static

Definition at line 130 of file test_amihooks.c.

References ast_manager_unregister_hook().

Referenced by handle_cli_amihook_unregister_hook().

130  {
131 
132  /* Unregister the hook */
134 
135 }
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:1942
static struct manager_custom_hook test_hook

◆ wait_for_hook()

static int wait_for_hook ( struct ast_test *  test)
static

Definition at line 50 of file test_amihooks.c.

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().

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 }
static ast_mutex_t hook_lock
Definition: test_amihooks.c:46
static int timeout
Definition: cdr_mysql.c:86
ast_cond_t hook_cond
Definition: test_amihooks.c:47
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ast_mutex_lock(a)
Definition: lock.h:187
int done
Definition: test_amihooks.c:48
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:204
#define ast_mutex_unlock(a)
Definition: lock.h:188

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "AMI Hook Test Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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_unregister_hook(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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)

Definition at line 194 of file test_amihooks.c.

◆ 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 = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
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: test_amihooks.c:95

Definition at line 106 of file test_amihooks.c.