Wed Jan 8 2020 09:50:18

Asterisk developer's documentation


res_ais.c File Reference

Usage of the SAForum AIS (Application Interface Specification) More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include "ais/ais.h"
#include "asterisk/module.h"
#include "asterisk/options.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"

Go to the source code of this file.

Data Structures

struct  ais_error
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
const char * ais_err2str (SaAisErrorT error)
 
int ast_ais_cmd (enum ast_ais_cmd cmd)
 
 ASTERISK_FILE_VERSION (__FILE__,"$Revision: 359053 $")
 
static void * dispatch_thread_handler (void *data)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SAForum AIS" , .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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ais_error ais_errors []
 
SaVersionT ais_version = { 'B', 1, 1 }
 
static struct ast_module_infoast_module_info = &__mod_info
 
struct {
   int   alert_pipe [2]
 
   pthread_t   id
 
   unsigned int   stop:1
 
dispatch_thread
 

Detailed Description

Usage of the SAForum AIS (Application Interface Specification)

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

This file contains the common code between the uses of the different AIS services.

Note
This module is still considered experimental, as it exposes the internal binary format of events between Asterisk servers over a network. However, this format is still subject to change between 1.6.X releases.

Definition in file res_ais.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 253 of file res_ais.c.

static void __unreg_module ( void  )
static

Definition at line 253 of file res_ais.c.

const char* ais_err2str ( SaAisErrorT  error)

Definition at line 105 of file res_ais.c.

References ais_errors, ARRAY_LEN, and ais_error::desc.

Referenced by add_subscribe_event(), ast_ais_clm_load_module(), ast_ais_clm_unload_module(), ast_ais_evt_load_module(), ast_ais_evt_unload_module(), ast_event_cb(), build_event_channel(), event_channel_destroy(), evt_event_deliver_cb(), and subscribe_event_destroy().

106 {
107  int x;
108 
109  for (x = 0; x < ARRAY_LEN(ais_errors); x++) {
110  if (ais_errors[x].error == error)
111  return ais_errors[x].desc;
112  }
113 
114  return "Unknown";
115 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
const char * desc
Definition: res_ais.c:74
static struct ais_error ais_errors[]
int ast_ais_cmd ( enum ast_ais_cmd  cmd)

Definition at line 182 of file res_ais.c.

References ast_debug, and dispatch_thread.

183 {
184  int res;
185 
186  res = write(dispatch_thread.alert_pipe[1], (char *) &cmd, sizeof(cmd));
187 
188  ast_debug(1, "AIS cmd: %d, res: %d\n", cmd, res);
189 
190  return res;
191 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static struct @322 dispatch_thread
ASTERISK_FILE_VERSION ( __FILE__  ,
"$Revision: 359053 $"   
)
static void* dispatch_thread_handler ( void *  data)
static

Definition at line 117 of file res_ais.c.

References ARRAY_LEN, AST_AIS_CMD_EXIT, AST_AIS_CMD_MEMBERSHIP_CHANGED, ast_ais_evt_membership_changed(), ast_debug, ast_log(), ast_poll, clm_handle, dispatch_thread, errno, evt_handle, and LOG_ERROR.

Referenced by load_module().

118 {
119  SaSelectionObjectT clm_fd, evt_fd;
120  int res;
121  struct pollfd pfd[3] = {
122  { .events = POLLIN, },
123  { .events = POLLIN, },
124  { .events = POLLIN, },
125  };
126  SaAisErrorT ais_res;
127 
128  ais_res = saClmSelectionObjectGet(clm_handle, &clm_fd);
129  if (ais_res != SA_AIS_OK) {
130  ast_log(LOG_ERROR, "Failed to retrieve select fd for CLM service. "
131  "This module will not operate.\n");
132  return NULL;
133  }
134 
135  ais_res = saEvtSelectionObjectGet(evt_handle, &evt_fd);
136  if (ais_res != SA_AIS_OK) {
137  ast_log(LOG_ERROR, "Failed to retrieve select fd for EVT service. "
138  "This module will not operate.\n");
139  return NULL;
140  }
141 
142  pfd[0].fd = clm_fd;
143  pfd[1].fd = evt_fd;
144  pfd[2].fd = dispatch_thread.alert_pipe[0];
145 
146  while (!dispatch_thread.stop) {
147  pfd[0].revents = 0;
148  pfd[1].revents = 0;
149  pfd[2].revents = 0;
150 
151  res = ast_poll(pfd, ARRAY_LEN(pfd), -1);
152  if (res == -1 && errno != EINTR && errno != EAGAIN) {
153  ast_log(LOG_ERROR, "Select error (%s) dispatch thread going away now, "
154  "and the module will no longer operate.\n", strerror(errno));
155  break;
156  }
157 
158  if (pfd[0].revents & POLLIN) {
159  saClmDispatch(clm_handle, SA_DISPATCH_ALL);
160  }
161  if (pfd[1].revents & POLLIN) {
162  saEvtDispatch(evt_handle, SA_DISPATCH_ALL);
163  }
164  if (pfd[2].revents & POLLIN) {
165  enum ast_ais_cmd cmd;
166  ast_debug(1, "Got a command in the poll() loop\n");
167  if (read(dispatch_thread.alert_pipe[0], &cmd, sizeof(cmd)) != -1) {
168  switch (cmd) {
171  break;
172  case AST_AIS_CMD_EXIT:
173  break;
174  }
175  }
176  }
177  }
178 
179  return NULL;
180 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
void ast_ais_evt_membership_changed(void)
Definition: evt.c:319
SaEvtHandleT evt_handle
Definition: evt.c:61
ast_ais_cmd
Definition: ais.h:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
#define LOG_ERROR
Definition: logger.h:155
SaClmHandleT clm_handle
Definition: clm.c:52
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static struct @322 dispatch_thread
static int load_module ( void  )
static

Definition at line 193 of file res_ais.c.

References ast_ais_clm_load_module(), ast_ais_clm_unload_module(), ast_ais_evt_load_module(), ast_ais_evt_unload_module(), ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_pthread_create_background, dispatch_thread, dispatch_thread_handler(), errno, and LOG_ERROR.

194 {
195  if (pipe(dispatch_thread.alert_pipe) == -1) {
196  ast_log(LOG_ERROR, "Failed to create alert pipe: %s (%d)\n",
197  strerror(errno), errno);
198  goto return_error;
199  }
200 
202  goto clm_failed;
203 
205  goto evt_failed;
206 
208  dispatch_thread_handler, NULL)) {
209  ast_log(LOG_ERROR, "Error starting AIS dispatch thread.\n");
210  goto dispatch_failed;
211  }
212 
214 
215 dispatch_failed:
217 evt_failed:
219 clm_failed:
220  close(dispatch_thread.alert_pipe[0]);
221  close(dispatch_thread.alert_pipe[1]);
222 return_error:
224 }
int ast_ais_evt_load_module(void)
Definition: evt.c:576
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
int ast_ais_clm_load_module(void)
Definition: clm.c:157
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_ais_evt_unload_module(void)
Definition: evt.c:592
int errno
static struct @322 dispatch_thread
static void * dispatch_thread_handler(void *data)
Definition: res_ais.c:117
int ast_ais_clm_unload_module(void)
Definition: clm.c:178
static int unload_module ( void  )
static

Definition at line 226 of file res_ais.c.

References ast_ais_clm_unload_module(), AST_AIS_CMD_EXIT, ast_ais_evt_unload_module(), ast_log(), AST_PTHREADT_NULL, dispatch_thread, errno, and LOG_ERROR.

227 {
230 
232  dispatch_thread.stop = 1;
233  if (ast_ais_cmd(AST_AIS_CMD_EXIT) == -1) {
234  ast_log(LOG_ERROR, "Failed to write to pipe: %s (%d)\n",
235  strerror(errno), errno);
236  }
237  pthread_join(dispatch_thread.id, NULL);
238  }
239 
240  if (dispatch_thread.alert_pipe[0] != -1) {
241  close(dispatch_thread.alert_pipe[0]);
242  dispatch_thread.alert_pipe[0] = -1;
243  }
244 
245  if (dispatch_thread.alert_pipe[1] != -1) {
246  close(dispatch_thread.alert_pipe[1]);
247  dispatch_thread.alert_pipe[1] = -1;
248  }
249 
250  return 0;
251 }
ast_ais_cmd
Definition: ais.h:50
#define AST_PTHREADT_NULL
Definition: lock.h:65
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_ais_evt_unload_module(void)
Definition: evt.c:592
int errno
static struct @322 dispatch_thread
int ast_ais_clm_unload_module(void)
Definition: clm.c:178

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SAForum AIS" , .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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 253 of file res_ais.c.

struct ais_error ais_errors[]
static

Referenced by ais_err2str().

SaVersionT ais_version = { 'B', 1, 1 }

Definition at line 70 of file res_ais.c.

Referenced by ast_ais_clm_load_module(), and ast_ais_evt_load_module().

int alert_pipe[2]

Definition at line 63 of file res_ais.c.

Definition at line 253 of file res_ais.c.

struct { ... } dispatch_thread
Initial value:
= {
.alert_pipe = { -1, -1 },
}
#define AST_PTHREADT_NULL
Definition: lock.h:65

Referenced by ast_ais_cmd(), dispatch_thread_handler(), load_module(), and unload_module().

pthread_t id

Definition at line 62 of file res_ais.c.

unsigned int stop

Definition at line 64 of file res_ais.c.