Mon Aug 31 12:30:35 2015

Asterisk developer's documentation


event.c File Reference

Internal generic event system. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/event.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dlinkedlists.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/unaligned.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/astobj2.h"
#include "asterisk/cli.h"

Go to the source code of this file.

Data Structures

struct  ast_ev_check_list
 Subscription event check list. More...
struct  ast_event
 An event. More...
struct  ast_event_ie
 An event information element. More...
struct  ast_event_ie_str_payload
 The payload for a string information element. More...
struct  ast_event_ie_val
struct  ast_event_ref
 A holder for an event. More...
struct  ast_event_sub
 Event subscription. More...
struct  ast_event_sub_list
 Event subscriptions The event subscribers are indexed by which event they are subscribed to. More...
struct  ie_map
 IE payload types and names. More...

Defines

#define MAX_CACHE_ARGS   8
#define NUM_CACHE_BUCKETS   563

Functions

static struct ast_event_refalloc_event_ref (void)
int ast_event_append_eid (struct ast_event **event)
 Append the global EID IE.
int ast_event_append_ie_bitflags (struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t flags)
 Append an information element that has a bitflags payload.
int ast_event_append_ie_raw (struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
 Append an information element that has a raw payload.
int ast_event_append_ie_str (struct ast_event **event, enum ast_event_ie_type ie_type, const char *str)
 Append an information element that has a string payload.
int ast_event_append_ie_uint (struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t data)
 Append an information element that has an integer payload.
enum ast_event_subscriber_res ast_event_check_subscriber (enum ast_event_type type,...)
 Check if subscribers exist.
static int ast_event_cmp (void *obj, void *arg, int flags)
void ast_event_destroy (struct ast_event *event)
 Destroy an event.
void ast_event_dump_cache (const struct ast_event_sub *event_sub)
 Dump the event cache for the subscribed event type.
static struct ast_eventast_event_dup (const struct ast_event *event)
struct ast_eventast_event_get_cached (enum ast_event_type type,...)
 Retrieve an event from the cache.
uint32_t ast_event_get_ie_bitflags (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has a bitflags payload.
enum ast_event_ie_pltype ast_event_get_ie_pltype (enum ast_event_ie_type ie_type)
 Get the payload type for a given information element type.
const void * ast_event_get_ie_raw (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has a raw payload.
uint16_t ast_event_get_ie_raw_payload_len (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the length of the raw payload for a particular IE.
const char * ast_event_get_ie_str (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has a string payload.
uint32_t ast_event_get_ie_str_hash (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the hash for the string payload of an IE.
const char * ast_event_get_ie_type_name (enum ast_event_ie_type ie_type)
 Get the string representation of an information element type.
uint32_t ast_event_get_ie_uint (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has an integer payload.
size_t ast_event_get_size (const struct ast_event *event)
 Get the size of an event.
enum ast_event_type ast_event_get_type (const struct ast_event *event)
 Get the type for an event.
const char * ast_event_get_type_name (const struct ast_event *event)
 Get the string representation of the type of the given event.
static int ast_event_hash (const void *obj, const int flags)
static int ast_event_hash_devstate (const void *obj, const int flags)
static int ast_event_hash_devstate_change (const void *obj, const int flags)
static int ast_event_hash_mwi (const void *obj, const int flags)
static void ast_event_ie_val_destroy (struct ast_event_ie_val *ie_val)
int ast_event_init (void)
uint32_t ast_event_iterator_get_ie_bitflags (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator as a bitflags payload.
void * ast_event_iterator_get_ie_raw (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator instance that has a raw payload.
uint16_t ast_event_iterator_get_ie_raw_payload_len (struct ast_event_iterator *iterator)
 Get the length of the raw payload for the current IE for an iterator.
const char * ast_event_iterator_get_ie_str (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator as a string payload.
enum ast_event_ie_type ast_event_iterator_get_ie_type (struct ast_event_iterator *iterator)
 Get the type of the current IE in the iterator instance.
uint32_t ast_event_iterator_get_ie_uint (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator as an integer payload.
int ast_event_iterator_init (struct ast_event_iterator *iterator, const struct ast_event *event)
 Initialize an event iterator instance.
int ast_event_iterator_next (struct ast_event_iterator *iterator)
 Move iterator instance to next IE.
size_t ast_event_minimum_length (void)
 Get the minimum length of an ast_event.
struct ast_eventast_event_new (enum ast_event_type type,...)
 Create a new event.
int ast_event_queue (struct ast_event *event)
 Queue an event.
int ast_event_queue_and_cache (struct ast_event *event)
 Queue and cache an event.
static void ast_event_ref_destroy (void *obj)
void ast_event_report_subs (const struct ast_event_sub *event_sub)
 Send AST_EVENT_SUB events to this subscriber of ... subscriber events.
int ast_event_str_to_event_type (const char *str, enum ast_event_type *event_type)
 Convert a string into an event type.
int ast_event_str_to_ie_type (const char *str, enum ast_event_ie_type *ie_type)
 Convert a string to an IE type.
int ast_event_sub_activate (struct ast_event_sub *sub)
 Activate a dynamically built subscription.
int ast_event_sub_append_ie_bitflags (struct ast_event_sub *sub, enum ast_event_ie_type ie_type, uint32_t flags)
 Append a bitflags parameter to a subscription.
int ast_event_sub_append_ie_exists (struct ast_event_sub *sub, enum ast_event_ie_type ie_type)
 Append an 'exists' parameter to a subscription.
int ast_event_sub_append_ie_raw (struct ast_event_sub *sub, enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
 Append a raw parameter to a subscription.
int ast_event_sub_append_ie_str (struct ast_event_sub *sub, enum ast_event_ie_type ie_type, const char *str)
 Append a string parameter to a subscription.
int ast_event_sub_append_ie_uint (struct ast_event_sub *sub, enum ast_event_ie_type ie_type, uint32_t unsigned_int)
 Append a uint parameter to a subscription.
void ast_event_sub_destroy (struct ast_event_sub *sub)
 Destroy an allocated subscription.
struct ast_event_subast_event_subscribe (enum ast_event_type type, ast_event_cb_t cb, const char *description, void *userdata,...)
 Subscribe to events.
struct ast_event_subast_event_subscribe_new (enum ast_event_type type, ast_event_cb_t cb, void *userdata)
 Allocate a subscription, but do not activate it.
const char * ast_event_subscriber_get_description (struct ast_event_sub *sub)
 Get description for a subscription.
struct ast_event_subast_event_unsubscribe (struct ast_event_sub *sub)
 Un-subscribe from events.
static int dump_cache_cb (void *obj, void *arg, int flags)
static void dump_raw_ie (struct ast_event_iterator *i, struct ast_cli_args *a)
static char * event_dump_cache (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int event_dump_cli (void *obj, void *arg, int flags)
static void event_shutdown (void)
static void event_update_cache (struct ao2_container *cache, struct ast_event *event)
static struct ast_eventgen_sub_event (struct ast_event_sub *sub)
static int handle_event (void *data)
static int match_ie_val (const struct ast_event *event, const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
static int match_sub_ie_val_to_event (const struct ast_event_ie_val *sub_ie_val, const struct ast_ev_check_list *check_ie_vals)

Variables

struct {
   enum ast_event_ie_type   cache_args [MAX_CACHE_ARGS]
 Information Elements used for caching.
   struct ao2_container *   container
 Container of cached events.
   ao2_hash_fn *   hash_fn
 Event type specific hash function.
ast_event_cache [AST_EVENT_TOTAL]
 Event types that are kept in the cache.
static struct ast_event_sub_list ast_event_subs [AST_EVENT_TOTAL]
 Event subscriptions The event subscribers are indexed by which event they are subscribed to.
static const char *const cached_event_types [] = { "MWI", "DeviceState", "DeviceStateChange", NULL }
 Names of cached event types, for CLI tab completion.
static struct ast_cli_entry event_cli []
static struct ast_taskprocessorevent_dispatcher
static const char *const event_names [AST_EVENT_TOTAL]
 Event Names.
static struct ie_map ie_maps [AST_EVENT_IE_TOTAL]
 IE payload types and names.
static uint32_t sub_uniqueid

Detailed Description

Internal generic event system.

Author:
Russell Bryant <russell@digium.com>

Definition in file event.c.


Define Documentation

#define MAX_CACHE_ARGS   8

Definition at line 151 of file event.c.

#define NUM_CACHE_BUCKETS   563

Definition at line 148 of file event.c.

Referenced by ast_event_init().


Function Documentation

static struct ast_event_ref* alloc_event_ref ( void   )  [static, read]

Definition at line 1419 of file event.c.

References ao2_alloc, and ast_event_ref_destroy().

Referenced by ast_event_queue(), and event_update_cache().

01420 {
01421    return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
01422 }

int ast_event_append_eid ( struct ast_event **  event  ) 

Append the global EID IE.

Parameters:
event the event to append IE to
Note:
For ast_event_new() that includes IEs, this is done automatically for you.
Return values:
0 success
-1 failure

Definition at line 1308 of file event.c.

References ast_eid_default, ast_event_append_ie_raw(), and AST_EVENT_IE_EID.

Referenced by ast_event_new().

01309 {
01310    return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
01311          &ast_eid_default, sizeof(ast_eid_default));
01312 }

int ast_event_append_ie_bitflags ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
uint32_t  bitflags 
)

Append an information element that has a bitflags payload.

Parameters:
event the event that the IE will be appended to
ie_type the type of IE to append
bitflags the flags that are the payload of the IE
Return values:
0 success
-1 failure
Since:
1.8

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 1167 of file event.c.

References ast_event_append_ie_raw().

Referenced by ast_event_get_cached(), ast_event_new(), and gen_sub_event().

01169 {
01170    flags = htonl(flags);
01171    return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
01172 }

int ast_event_append_ie_raw ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
const void *  data,
size_t  data_len 
)

Append an information element that has a raw payload.

Parameters:
event the event that the IE will be appended to
ie_type the type of IE to append
data A pointer to the raw data for the payload of the IE
data_len The amount of data to copy into the payload
Return values:
0 success
-1 failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 1174 of file event.c.

References ast_free, ast_realloc, ast_event::event_len, ast_event_ie::ie_payload, and ast_event_ie::ie_payload_len.

Referenced by ast_event_append_eid(), ast_event_append_ie_bitflags(), ast_event_append_ie_str(), ast_event_append_ie_uint(), ast_event_get_cached(), ast_event_new(), and gen_sub_event().

01176 {
01177    struct ast_event_ie *ie;
01178    struct ast_event *old_event;
01179    unsigned int extra_len;
01180    uint16_t event_len;
01181 
01182    event_len = ntohs((*event)->event_len);
01183    extra_len = sizeof(*ie) + data_len;
01184 
01185    old_event = *event;
01186    *event = ast_realloc(*event, event_len + extra_len);
01187    if (!*event) {
01188       ast_free(old_event);
01189       return -1;
01190    }
01191 
01192    ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
01193    ie->ie_type = htons(ie_type);
01194    ie->ie_payload_len = htons(data_len);
01195    memcpy(ie->ie_payload, data, data_len);
01196 
01197    (*event)->event_len = htons(event_len + extra_len);
01198 
01199    return 0;
01200 }

int ast_event_append_ie_str ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
const char *  str 
)

Append an information element that has a string payload.

Parameters:
event the event that the IE will be appended to
ie_type the type of IE to append
str The string for the payload of the IE
Return values:
0 success
-1 failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 1139 of file event.c.

References ast_alloca, ast_event_append_ie_raw(), AST_EVENT_IE_DEVICE, ast_str_hash(), ast_strdupa, ast_tech_to_upper(), ast_event_ie_str_payload::hash, and ast_event_ie_str_payload::str.

Referenced by add_ie(), add_ipv4_ie(), add_timeval_ie(), ast_event_get_cached(), ast_event_new(), and gen_sub_event().

01141 {
01142    struct ast_event_ie_str_payload *str_payload;
01143    size_t payload_len;
01144 
01145    payload_len = sizeof(*str_payload) + strlen(str);
01146    str_payload = ast_alloca(payload_len);
01147 
01148    strcpy(str_payload->str, str);
01149    if (ie_type == AST_EVENT_IE_DEVICE) {
01150       char *uppertech = ast_strdupa(str);
01151       ast_tech_to_upper(uppertech);
01152       str_payload->hash = ast_str_hash(uppertech);
01153    } else {
01154       str_payload->hash = ast_str_hash(str);
01155    }
01156 
01157    return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
01158 }

int ast_event_append_ie_uint ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
uint32_t  data 
)

Append an information element that has an integer payload.

Parameters:
event the event that the IE will be appended to
ie_type the type of IE to append
data The integer for the payload of the IE
Return values:
0 success
-1 failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 1160 of file event.c.

References ast_event_append_ie_raw().

Referenced by add_ie(), ast_event_get_cached(), ast_event_new(), and gen_sub_event().

01162 {
01163    data = htonl(data);
01164    return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
01165 }

enum ast_event_subscriber_res ast_event_check_subscriber ( enum ast_event_type  event_type,
  ... 
)

Check if subscribers exist.

Parameters:
event_type This is the type of event that the caller would like to check for subscribers to.

The rest of the arguments to this function specify additional parameters for checking for subscriptions to subsets of an event type. The arguments must in sets of:

    <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]

and must end with AST_EVENT_IE_END.

If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed by a valid IE payload type. If the payload type specified is AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided. Otherwise, a payload must also be specified.

Returns:
This returns one of the values defined in the ast_event_subscriber_res enum which will indicate if subscribers exist that match the given criteria.

Example usage:

This example will check if there are any subscribers to MWI events for the mailbox defined in the "mailbox" variable.

Definition at line 448 of file event.c.

References ARRAY_LEN, ast_alloca, AST_EVENT_ALL, AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, AST_EVENT_SUB_EXISTS, AST_EVENT_SUB_NONE, ast_event_subs, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), AST_RWDLLIST_RDLOCK, AST_RWDLLIST_TRAVERSE, AST_RWDLLIST_UNLOCK, AST_RWLIST_FIRST, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_ev_check_list::ie_vals, LOG_ERROR, LOG_WARNING, match_sub_ie_val_to_event(), ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, and ast_event_ie_val::uint.

Referenced by ast_event_queue(), ast_event_sub_activate(), and ast_event_unsubscribe().

00449 {
00450    va_list ap;
00451    enum ast_event_ie_type ie_type;
00452    enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
00453    struct ast_event_ie_val *ie_val;
00454    struct ast_event_sub *sub;
00455    struct ast_ev_check_list check_ie_vals = {
00456       .ie_vals = AST_LIST_HEAD_NOLOCK_INIT_VALUE
00457    };
00458    const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
00459    int i;
00460    int want_specific_event;/* TRUE if looking for subscribers wanting specific parameters. */
00461 
00462    if (type >= AST_EVENT_TOTAL) {
00463       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00464       return res;
00465    }
00466 
00467    want_specific_event = 0;
00468    va_start(ap, type);
00469    for (ie_type = va_arg(ap, enum ast_event_ie_type);
00470       ie_type != AST_EVENT_IE_END;
00471       ie_type = va_arg(ap, enum ast_event_ie_type))
00472    {
00473       struct ast_event_ie_val *ie_value = ast_alloca(sizeof(*ie_value));
00474       int insert = 0;
00475 
00476       memset(ie_value, 0, sizeof(*ie_value));
00477       ie_value->ie_type = ie_type;
00478       ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00479       switch (ie_value->ie_pltype) {
00480       case AST_EVENT_IE_PLTYPE_UINT:
00481          ie_value->payload.uint = va_arg(ap, uint32_t);
00482          insert = 1;
00483          break;
00484       case AST_EVENT_IE_PLTYPE_BITFLAGS:
00485          ie_value->payload.uint = va_arg(ap, uint32_t);
00486          insert = 1;
00487          break;
00488       case AST_EVENT_IE_PLTYPE_STR:
00489          ie_value->payload.str = va_arg(ap, const char *);
00490          insert = 1;
00491          break;
00492       case AST_EVENT_IE_PLTYPE_RAW:
00493       {
00494          void *data = va_arg(ap, void *);
00495          size_t datalen = va_arg(ap, size_t);
00496 
00497          ie_value->payload.raw = ast_alloca(datalen);
00498          memcpy(ie_value->payload.raw, data, datalen);
00499          ie_value->raw_datalen = datalen;
00500          insert = 1;
00501          break;
00502       }
00503       case AST_EVENT_IE_PLTYPE_UNKNOWN:
00504       case AST_EVENT_IE_PLTYPE_EXISTS:
00505          /* Unsupported payload type. */
00506          break;
00507       }
00508 
00509       if (insert) {
00510          want_specific_event = 1;
00511          AST_LIST_INSERT_TAIL(&check_ie_vals.ie_vals, ie_value, entry);
00512       } else {
00513          ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
00514       }
00515    }
00516    va_end(ap);
00517 
00518    for (i = 0; i < ARRAY_LEN(event_types); i++) {
00519       AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
00520       if (want_specific_event) {
00521          AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
00522             AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
00523                if (!match_sub_ie_val_to_event(ie_val, &check_ie_vals)) {
00524                   /* The current subscription ie did not match an event ie. */
00525                   break;
00526                }
00527             }
00528             if (!ie_val) {
00529                /* Everything matched.  A subscriber is looking for this event. */
00530                break;
00531             }
00532          }
00533       } else {
00534          /* Just looking to see if there are ANY subscribers to the event type. */
00535          sub = AST_RWLIST_FIRST(&ast_event_subs[event_types[i]]);
00536       }
00537       AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
00538       if (sub) {
00539          break;
00540       }
00541    }
00542 
00543    return sub ? AST_EVENT_SUB_EXISTS : AST_EVENT_SUB_NONE;
00544 }

static int ast_event_cmp ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1628 of file event.c.

References ARRAY_LEN, ast_event_cache, ast_event_get_ie_pltype(), ast_event_get_type(), cache_args, CMP_MATCH, ast_event_ref::event, ast_event_ie_val::ie_pltype, and match_ie_val().

Referenced by ast_event_init(), and event_update_cache().

01629 {
01630    struct ast_event_ref *event_ref, *event_ref2;
01631    struct ast_event *event, *event2;
01632    int res = CMP_MATCH;
01633    int i;
01634    enum ast_event_ie_type *cache_args;
01635 
01636    event_ref = obj;
01637    event = event_ref->event;
01638 
01639    event_ref2 = arg;
01640    event2 = event_ref2->event;
01641 
01642    cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
01643 
01644    for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
01645       struct ast_event_ie_val ie_val = {
01646          .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
01647          .ie_type = cache_args[i],
01648       };
01649 
01650       if (!match_ie_val(event, &ie_val, event2)) {
01651          res = 0;
01652          break;
01653       }
01654    }
01655 
01656    return res;
01657 }

void ast_event_destroy ( struct ast_event event  ) 

Destroy an event.

Parameters:
event the event to destroy
Returns:
Nothing
Note:
Events that have been queued should *not* be destroyed by the code that created the event. It will be automatically destroyed after being dispatched to the appropriate subscribers.

Definition at line 1314 of file event.c.

References ast_free.

Referenced by ast_cel_report_event(), ast_event_get_cached(), ast_event_queue(), ast_event_queue_and_cache(), ast_event_ref_destroy(), ast_event_report_subs(), ast_event_sub_activate(), ast_event_unsubscribe(), devstate_cached(), event_update_cache(), get_cached_mwi(), handle_security_event(), has_voicemail(), process_collection(), stun_monitor_request(), unistim_send_mwi_to_peer(), and update_registry().

01315 {
01316    ast_free(event);
01317 }

void ast_event_dump_cache ( const struct ast_event_sub event_sub  ) 

Dump the event cache for the subscribed event type.

Dump the event cache for the subscriber.

Definition at line 654 of file event.c.

References ao2_callback, ast_event_cache, dump_cache_cb(), OBJ_NODATA, and ast_event_sub::type.

Referenced by add_publish_event(), aji_init_event_distribution(), ast_ais_evt_membership_changed(), and handle_devstate_change().

00655 {
00656    ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
00657          dump_cache_cb, (void *) event_sub);
00658 }

static struct ast_event* ast_event_dup ( const struct ast_event event  )  [static, read]

Definition at line 1326 of file event.c.

References ast_calloc, ast_event_get_size(), and ast_event::event_len.

Referenced by ast_event_get_cached(), and event_update_cache().

01327 {
01328    struct ast_event *dup_event;
01329    uint16_t event_len;
01330 
01331    event_len = ast_event_get_size(event);
01332 
01333    if (!(dup_event = ast_calloc(1, event_len))) {
01334       return NULL;
01335    }
01336 
01337    memcpy(dup_event, event, event_len);
01338 
01339    return dup_event;
01340 }

struct ast_event* ast_event_get_cached ( enum  ast_event_type,
  ... 
) [read]

Retrieve an event from the cache.

Parameters:
ast_event_type The type of event to retrieve from the cache

The rest of the arguments to this function specify information elements to match for retrieving events from the cache. They are specified in the form:

and must end with AST_EVENT_IE_END.

If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed by a valid IE payload type. If the payload type specified is AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided. Otherwise, a payload must also be specified.

Returns:
A reference to an event retrieved from the cache. If no event was found that matches the specified criteria, then NULL will be returned.
Note:
If more than one event in the cache matches the specified criteria, only one will be returned, and it is undefined which one it will be.
The caller of this function *must* call ast_event_destroy() on the returned event after it is done using it.

Example Usage:

This example will check for an MWI event in the cache that matches the specified mailbox. This would be the way to find out the last known state of a mailbox without having to poll the mailbox directly.

Definition at line 1342 of file event.c.

References ao2_find, ao2_ref, ast_event_append_ie_bitflags(), ast_event_append_ie_raw(), ast_event_append_ie_str(), ast_event_append_ie_uint(), ast_event_cache, ast_event_destroy(), ast_event_dup(), AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_event_new(), ast_log(), container, ast_event_ref::event, LOG_ERROR, LOG_WARNING, and OBJ_POINTER.

Referenced by devstate_cached(), get_cached_mwi(), has_voicemail(), process_collection(), unistim_send_mwi_to_peer(), and update_registry().

01343 {
01344    va_list ap;
01345    enum ast_event_ie_type ie_type;
01346    struct ast_event *dup_event = NULL;
01347    struct ast_event_ref *cached_event_ref;
01348    struct ast_event *cache_arg_event;
01349    struct ast_event_ref tmp_event_ref = {
01350       .event = NULL,
01351    };
01352    struct ao2_container *container = NULL;
01353 
01354    if (type >= AST_EVENT_TOTAL) {
01355       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
01356       return NULL;
01357    }
01358 
01359    if (!(container = ast_event_cache[type].container)) {
01360       ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
01361       return NULL;
01362    }
01363 
01364    if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
01365       return NULL;
01366    }
01367 
01368    va_start(ap, type);
01369    for (ie_type = va_arg(ap, enum ast_event_ie_type);
01370       ie_type != AST_EVENT_IE_END;
01371       ie_type = va_arg(ap, enum ast_event_ie_type))
01372    {
01373       enum ast_event_ie_pltype ie_pltype;
01374 
01375       ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
01376 
01377       switch (ie_pltype) {
01378       case AST_EVENT_IE_PLTYPE_UINT:
01379          ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
01380          break;
01381       case AST_EVENT_IE_PLTYPE_BITFLAGS:
01382          ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
01383          break;
01384       case AST_EVENT_IE_PLTYPE_STR:
01385          ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
01386          break;
01387       case AST_EVENT_IE_PLTYPE_RAW:
01388       {
01389          void *data = va_arg(ap, void *);
01390          size_t datalen = va_arg(ap, size_t);
01391          ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
01392          break;
01393       }
01394       case AST_EVENT_IE_PLTYPE_EXISTS:
01395          ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
01396          break;
01397       case AST_EVENT_IE_PLTYPE_UNKNOWN:
01398          break;
01399       }
01400    }
01401    va_end(ap);
01402 
01403    tmp_event_ref.event = cache_arg_event;
01404 
01405    cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
01406 
01407    ast_event_destroy(cache_arg_event);
01408    cache_arg_event = NULL;
01409 
01410    if (cached_event_ref) {
01411       dup_event = ast_event_dup(cached_event_ref->event);
01412       ao2_ref(cached_event_ref, -1);
01413       cached_event_ref = NULL;
01414    }
01415 
01416    return dup_event;
01417 }

uint32_t ast_event_get_ie_bitflags ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has a bitflags payload.

Parameters:
event The event to get the IE from
ie_type the type of information element to retrieve
Returns:
This returns the payload of the information element with the given type. However, an IE with a payload of 0, and the case where no IE is found yield the same return value.

Definition at line 1084 of file event.c.

References ast_event_get_ie_raw(), and get_unaligned_uint32().

Referenced by append_ie(), and match_ie_val().

01085 {
01086    const uint32_t *ie_val;
01087 
01088    ie_val = ast_event_get_ie_raw(event, ie_type);
01089 
01090    return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
01091 }

enum ast_event_ie_pltype ast_event_get_ie_pltype ( enum ast_event_ie_type  ie_type  ) 

Get the payload type for a given information element type.

Parameters:
ie_type the information element type to get the payload type of
Returns:
the payload type for the provided IE type
Since:
1.6.1

Definition at line 314 of file event.c.

References ARRAY_LEN, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_log(), ie_maps, ie_map::ie_pltype, and LOG_ERROR.

Referenced by append_ie(), ast_event_cmp(), and event_dump_cli().

00315 {
00316    if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
00317       ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00318       return AST_EVENT_IE_PLTYPE_UNKNOWN;
00319    }
00320 
00321    return ie_maps[ie_type].ie_pltype;
00322 }

const void* ast_event_get_ie_raw ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has a raw payload.

Parameters:
event The event to get the IE from
ie_type the type of information element to retrieve
Returns:
This returns the payload of the information element with the given type. If the information element isn't found, NULL will be returned.

Definition at line 1111 of file event.c.

References ast_event_iterator_get_ie_raw(), ast_event_iterator_get_ie_type(), ast_event_iterator_init(), and ast_event_iterator_next().

Referenced by aji_devstate_cb(), aji_mwi_cb(), ast_event_cb(), ast_event_get_ie_bitflags(), ast_event_get_ie_str(), ast_event_get_ie_str_hash(), ast_event_get_ie_uint(), ast_event_new(), devstate_cache_cb(), devstate_change_collector_cb(), evt_event_deliver_cb(), ie_is_present(), and match_ie_val().

01112 {
01113    struct ast_event_iterator iterator;
01114    int res;
01115 
01116    for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
01117       if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
01118          return ast_event_iterator_get_ie_raw(&iterator);
01119       }
01120    }
01121 
01122    return NULL;
01123 }

uint16_t ast_event_get_ie_raw_payload_len ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the length of the raw payload for a particular IE.

Parameters:
event The event to get the IE payload length from
ie_type the type of information element to get the length of
Returns:
If an IE of type ie_type is found, its payload length is returned. Otherwise, 0 is returned.

Definition at line 1125 of file event.c.

References ast_event_iterator_get_ie_raw_payload_len(), ast_event_iterator_get_ie_type(), ast_event_iterator_init(), and ast_event_iterator_next().

Referenced by match_ie_val().

01126 {
01127    struct ast_event_iterator iterator;
01128    int res;
01129 
01130    for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
01131       if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
01132          return ast_event_iterator_get_ie_raw_payload_len(&iterator);
01133       }
01134    }
01135 
01136    return 0;
01137 }

const char* ast_event_get_ie_str ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has a string payload.

Parameters:
event The event to get the IE from
ie_type the type of information element to retrieve
Returns:
This returns the payload of the information element with the given type. If the information element isn't found, NULL will be returned.

Definition at line 1102 of file event.c.

References ast_event_get_ie_raw(), and ast_event_ie_str_payload::str.

Referenced by aji_devstate_cb(), aji_mwi_cb(), append_ie(), ast_cel_fill_record(), ast_event_hash_devstate(), ast_event_hash_devstate_change(), ast_event_hash_mwi(), device_state_cb(), devstate_change_collector_cb(), generic_monitor_devstate_cb(), match_ie_val(), mwi_sub_event_cb(), and print_cel_sub().

01103 {
01104    const struct ast_event_ie_str_payload *str_payload;
01105 
01106    str_payload = ast_event_get_ie_raw(event, ie_type);
01107 
01108    return str_payload ? str_payload->str : NULL;
01109 }

uint32_t ast_event_get_ie_str_hash ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the hash for the string payload of an IE.

Parameters:
event The event to get the IE from
ie_type the type of information element to retrieve the hash for
Returns:
This function returns the hash value as calculated by ast_str_hash() for the string payload. This is stored in the event to avoid unnecessary string comparisons.

Definition at line 1093 of file event.c.

References ast_event_get_ie_raw(), and ast_event_ie_str_payload::hash.

Referenced by match_ie_val().

01094 {
01095    const struct ast_event_ie_str_payload *str_payload;
01096 
01097    str_payload = ast_event_get_ie_raw(event, ie_type);
01098 
01099    return str_payload ? str_payload->hash : 0;
01100 }

const char* ast_event_get_ie_type_name ( enum ast_event_ie_type  ie_type  ) 

Get the string representation of an information element type.

Parameters:
ie_type the information element type to get the string representation of
Returns:
the string representation of the information element type
Since:
1.6.1

Definition at line 304 of file event.c.

References ARRAY_LEN, ast_log(), ie_maps, LOG_ERROR, and ie_map::name.

Referenced by append_ie(), dump_raw_ie(), event_dump_cache(), event_dump_cli(), and security_event_cb().

00305 {
00306    if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
00307       ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00308       return "";
00309    }
00310 
00311    return ie_maps[ie_type].name;
00312 }

uint32_t ast_event_get_ie_uint ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has an integer payload.

Parameters:
event The event to get the IE from
ie_type the type of information element to retrieve
Returns:
This returns the payload of the information element with the given type. However, an IE with a payload of 0, and the case where no IE is found yield the same return value.

Definition at line 1075 of file event.c.

References ast_event_get_ie_raw(), and get_unaligned_uint32().

Referenced by aji_devstate_cb(), aji_mwi_cb(), append_ie(), ast_cel_fill_record(), device_state_cb(), devstate_cache_cb(), devstate_cached(), devstate_change_collector_cb(), generic_agent_devstate_cb(), generic_monitor_devstate_cb(), get_cached_mwi(), has_voicemail(), match_ie_val(), mwi_event_cb(), mwi_sub_event_cb(), mwi_unsub_event_cb(), process_collection(), security_event_cb(), unistim_send_mwi_to_peer(), and update_registry().

01076 {
01077    const uint32_t *ie_val;
01078 
01079    ie_val = ast_event_get_ie_raw(event, ie_type);
01080 
01081    return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
01082 }

size_t ast_event_get_size ( const struct ast_event event  ) 

Get the size of an event.

Parameters:
event the event to get the size of
Returns:
the number of bytes contained in the event
Since:
1.6.1

Definition at line 340 of file event.c.

References ast_event::event_len.

Referenced by ast_event_cb(), ast_event_dup(), and ast_event_iterator_init().

00341 {
00342    size_t res;
00343 
00344    res = ntohs(event->event_len);
00345 
00346    return res;
00347 }

enum ast_event_type ast_event_get_type ( const struct ast_event event  ) 

Get the type for an event.

Parameters:
event the event to get the type for
Returns:
the event type as represented by one of the values in the ast_event_type enum

Definition at line 1070 of file event.c.

Referenced by ast_event_cb(), ast_event_cmp(), ast_event_get_type_name(), ast_event_hash(), ast_event_queue_and_cache(), mwi_sub_event_cb(), and mwi_unsub_event_cb().

01071 {
01072    return ntohs(event->type);
01073 }

const char* ast_event_get_type_name ( const struct ast_event event  ) 

Get the string representation of the type of the given event.

  • event the event to get the type of
Returns:
the string representation of the event type of the provided event
Since:
1.6.1

Definition at line 274 of file event.c.

References ARRAY_LEN, ast_event_get_type(), ast_log(), event_names, LOG_ERROR, and ast_event_sub::type.

Referenced by event_dump_cli().

00275 {
00276    enum ast_event_type type;
00277 
00278    type = ast_event_get_type(event);
00279 
00280    if (type < 0 || type >= ARRAY_LEN(event_names)) {
00281       ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
00282       return "";
00283    }
00284 
00285    return event_names[type];
00286 }

static int ast_event_hash ( const void *  obj,
const int  flags 
) [static]

Definition at line 1594 of file event.c.

References ast_event_cache, ast_event_get_type(), ast_event_ref::event, and hash_fn.

Referenced by ast_event_init().

01595 {
01596    const struct ast_event_ref *event_ref;
01597    const struct ast_event *event;
01598    ao2_hash_fn *hash_fn;
01599 
01600    event_ref = obj;
01601    event = event_ref->event;
01602 
01603    if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
01604       return 0;
01605    }
01606 
01607    return hash_fn(event, flags);
01608 }

static int ast_event_hash_devstate ( const void *  obj,
const int  flags 
) [static]

Definition at line 1571 of file event.c.

References ast_event_get_ie_str(), AST_EVENT_IE_DEVICE, and ast_str_hash().

01572 {
01573    const struct ast_event *event = obj;
01574 
01575    return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
01576 }

static int ast_event_hash_devstate_change ( const void *  obj,
const int  flags 
) [static]

Definition at line 1587 of file event.c.

References ast_event_get_ie_str(), AST_EVENT_IE_DEVICE, and ast_str_hash().

01588 {
01589    const struct ast_event *event = obj;
01590 
01591    return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
01592 }

static int ast_event_hash_mwi ( const void *  obj,
const int  flags 
) [static]

Definition at line 1553 of file event.c.

References ast_event_get_ie_str(), AST_EVENT_IE_CONTEXT, AST_EVENT_IE_MAILBOX, ast_str_hash(), ast_str_hash_add(), context, and mailbox.

01554 {
01555    const struct ast_event *event = obj;
01556    const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
01557    const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
01558 
01559    return ast_str_hash_add(context, ast_str_hash(mailbox));
01560 }

static void ast_event_ie_val_destroy ( struct ast_event_ie_val ie_val  )  [static]
int ast_event_init ( void   ) 

Provided by event.c

Definition at line 1819 of file event.c.

References ao2_container_alloc, ARRAY_LEN, ast_cli_register_multiple(), ast_event_cache, ast_event_cmp(), ast_event_hash(), ast_event_subs, ast_register_atexit(), AST_RWDLLIST_HEAD_INIT, ast_taskprocessor_get(), container, event_cli, event_shutdown(), hash_fn, and NUM_CACHE_BUCKETS.

Referenced by main().

01820 {
01821    int i;
01822 
01823    for (i = 0; i < AST_EVENT_TOTAL; i++) {
01824       AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
01825    }
01826 
01827    for (i = 0; i < AST_EVENT_TOTAL; i++) {
01828       if (!ast_event_cache[i].hash_fn) {
01829          /* This event type is not cached. */
01830          continue;
01831       }
01832 
01833       if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
01834             ast_event_hash, ast_event_cmp))) {
01835          goto event_init_cleanup;
01836       }
01837    }
01838 
01839    if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
01840       goto event_init_cleanup;
01841    }
01842 
01843    ast_cli_register_multiple(event_cli, ARRAY_LEN(event_cli));
01844 
01845    ast_register_atexit(event_shutdown);
01846 
01847    return 0;
01848 
01849 event_init_cleanup:
01850    event_shutdown();
01851    return -1;
01852 }

uint32_t ast_event_iterator_get_ie_bitflags ( struct ast_event_iterator iterator  ) 

Get the value of the current IE in the iterator as a bitflags payload.

Parameters:
iterator The iterator instance
Returns:
This returns the payload of the information element as bitflags.

Definition at line 1046 of file event.c.

References get_unaligned_uint32(), ast_event_iterator::ie, and ast_event_ie::ie_payload.

Referenced by event_dump_cli().

01047 {
01048    return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
01049 }

void* ast_event_iterator_get_ie_raw ( struct ast_event_iterator iterator  ) 

Get the value of the current IE in the iterator instance that has a raw payload.

Parameters:
iterator The iterator instance
Returns:
This returns the payload of the information element as type raw.

Definition at line 1060 of file event.c.

References ast_event_iterator::ie, and ast_event_ie::ie_payload.

Referenced by ast_event_get_ie_raw(), and dump_raw_ie().

01061 {
01062    return iterator->ie->ie_payload;
01063 }

uint16_t ast_event_iterator_get_ie_raw_payload_len ( struct ast_event_iterator iterator  ) 

Get the length of the raw payload for the current IE for an iterator.

Parameters:
iterator The IE iterator
Returns:
The payload length of the current IE

Definition at line 1065 of file event.c.

References ast_event_iterator::ie, and ast_event_ie::ie_payload_len.

Referenced by ast_event_get_ie_raw_payload_len().

01066 {
01067    return ntohs(iterator->ie->ie_payload_len);
01068 }

const char* ast_event_iterator_get_ie_str ( struct ast_event_iterator iterator  ) 

Get the value of the current IE in the iterator as a string payload.

Parameters:
iterator The iterator instance
Returns:
This returns the payload of the information element as a string.

Definition at line 1051 of file event.c.

References ast_event_iterator::ie, ast_event_ie::ie_payload, and ast_event_ie_str_payload::str.

Referenced by event_dump_cli().

01052 {
01053    const struct ast_event_ie_str_payload *str_payload;
01054 
01055    str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
01056 
01057    return str_payload ? str_payload->str : NULL;
01058 }

enum ast_event_ie_type ast_event_iterator_get_ie_type ( struct ast_event_iterator iterator  ) 

Get the type of the current IE in the iterator instance.

Parameters:
iterator The iterator instance
Returns:
the ie type as represented by one of the value sin the ast_event_ie_type enum

Definition at line 1036 of file event.c.

References ast_event_iterator::ie.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), dump_raw_ie(), and event_dump_cli().

01037 {
01038    return ntohs(iterator->ie->ie_type);
01039 }

uint32_t ast_event_iterator_get_ie_uint ( struct ast_event_iterator iterator  ) 

Get the value of the current IE in the iterator as an integer payload.

Parameters:
iterator The iterator instance
Returns:
This returns the payload of the information element as a uint.

Definition at line 1041 of file event.c.

References get_unaligned_uint32(), ast_event_iterator::ie, and ast_event_ie::ie_payload.

Referenced by event_dump_cli().

01042 {
01043    return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
01044 }

int ast_event_iterator_init ( struct ast_event_iterator iterator,
const struct ast_event event 
)

Initialize an event iterator instance.

Parameters:
iterator The iterator instance to initialize
event The event that will be iterated through
Return values:
0 Success, there are IEs available to iterate
-1 Failure, there are no IEs in the event to iterate

Definition at line 1014 of file event.c.

References ast_event_get_size(), ast_event_iterator::event, ast_event_iterator::event_len, and ast_event_iterator::ie.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), and event_dump_cli().

01015 {
01016    int res = 0;
01017 
01018    iterator->event_len = ast_event_get_size(event);
01019    iterator->event = event;
01020    if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
01021       iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
01022    } else {
01023       iterator->ie = NULL;
01024       res = -1;
01025    }
01026 
01027    return res;
01028 }

int ast_event_iterator_next ( struct ast_event_iterator iterator  ) 

Move iterator instance to next IE.

Parameters:
iterator The iterator instance
Return values:
0 on success
-1 if end is reached

Definition at line 1030 of file event.c.

References ast_event_iterator::event, ast_event_iterator::event_len, ast_event_iterator::ie, and ast_event_ie::ie_payload_len.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), and event_dump_cli().

01031 {
01032    iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
01033    return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
01034 }

size_t ast_event_minimum_length ( void   ) 

Get the minimum length of an ast_event.

Returns:
minimum amount of memory that will be consumed by any ast_event.

Definition at line 1854 of file event.c.

Referenced by evt_event_deliver_cb().

01855 {
01856    return sizeof(struct ast_event);
01857 }

struct ast_event* ast_event_new ( enum ast_event_type  event_type,
  ... 
) [read]

Create a new event.

Parameters:
event_type The type of event to create

The rest of the arguments to this function specify information elements to add to the event. They are specified in the form:

and must end with AST_EVENT_IE_END.

If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed by a valid IE payload type. The payload type, EXISTS, should not be used here because it makes no sense to do so. So, a payload must also be specified after the IE payload type.

Note:
The EID IE will be appended automatically when this function is used with at least one IE specified.
Returns:
This returns the event that has been created. If there is an error creating the event, NULL will be returned.

Example usage:

This creates a MWI event with 3 information elements, a mailbox which is a string, and the number of new and old messages, specified as integers.

Definition at line 1202 of file event.c.

References ast_alloca, ast_calloc, ast_event_append_eid(), ast_event_append_ie_bitflags(), ast_event_append_ie_raw(), ast_event_append_ie_str(), ast_event_append_ie_uint(), ast_event_get_ie_raw(), AST_EVENT_IE_EID, AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, AST_LIST_HEAD_NOLOCK_STATIC, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, LOG_WARNING, ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, and ast_event_ie_val::uint.

Referenced by aji_handle_pubsub_event(), alloc_event(), ast_cel_report_event(), ast_event_get_cached(), ast_event_unsubscribe(), devstate_event(), gen_sub_event(), handle_request_notify(), notify_message(), process_collection(), queue_mwi_event(), and stun_monitor_request().

01203 {
01204    va_list ap;
01205    struct ast_event *event;
01206    enum ast_event_ie_type ie_type;
01207    struct ast_event_ie_val *ie_val;
01208    int has_ie = 0;
01209    AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
01210 
01211    /* Invalid type */
01212    if (type >= AST_EVENT_TOTAL) {
01213       ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
01214          "type '%u'!\n", type);
01215       return NULL;
01216    }
01217 
01218    va_start(ap, type);
01219    for (ie_type = va_arg(ap, enum ast_event_ie_type);
01220       ie_type != AST_EVENT_IE_END;
01221       ie_type = va_arg(ap, enum ast_event_ie_type))
01222    {
01223       struct ast_event_ie_val *ie_value = ast_alloca(sizeof(*ie_value));
01224       int insert = 0;
01225 
01226       memset(ie_value, 0, sizeof(*ie_value));
01227       ie_value->ie_type = ie_type;
01228       ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
01229       switch (ie_value->ie_pltype) {
01230       case AST_EVENT_IE_PLTYPE_UINT:
01231          ie_value->payload.uint = va_arg(ap, uint32_t);
01232          insert = 1;
01233          break;
01234       case AST_EVENT_IE_PLTYPE_BITFLAGS:
01235          ie_value->payload.uint = va_arg(ap, uint32_t);
01236          insert = 1;
01237          break;
01238       case AST_EVENT_IE_PLTYPE_STR:
01239          ie_value->payload.str = va_arg(ap, const char *);
01240          insert = 1;
01241          break;
01242       case AST_EVENT_IE_PLTYPE_RAW:
01243       {
01244          void *data = va_arg(ap, void *);
01245          size_t datalen = va_arg(ap, size_t);
01246          ie_value->payload.raw = ast_alloca(datalen);
01247          memcpy(ie_value->payload.raw, data, datalen);
01248          ie_value->raw_datalen = datalen;
01249          insert = 1;
01250          break;
01251       }
01252       case AST_EVENT_IE_PLTYPE_UNKNOWN:
01253       case AST_EVENT_IE_PLTYPE_EXISTS:
01254          break;
01255       }
01256 
01257       if (insert) {
01258          AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
01259          has_ie = 1;
01260       } else {
01261          ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
01262       }
01263    }
01264    va_end(ap);
01265 
01266    if (!(event = ast_calloc(1, sizeof(*event)))) {
01267       return NULL;
01268    }
01269 
01270    event->type = htons(type);
01271    event->event_len = htons(sizeof(*event));
01272 
01273    AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
01274       switch (ie_val->ie_pltype) {
01275       case AST_EVENT_IE_PLTYPE_STR:
01276          ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
01277          break;
01278       case AST_EVENT_IE_PLTYPE_UINT:
01279          ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
01280          break;
01281       case AST_EVENT_IE_PLTYPE_BITFLAGS:
01282          ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
01283          break;
01284       case AST_EVENT_IE_PLTYPE_RAW:
01285          ast_event_append_ie_raw(&event, ie_val->ie_type,
01286                ie_val->payload.raw, ie_val->raw_datalen);
01287          break;
01288       case AST_EVENT_IE_PLTYPE_EXISTS:
01289       case AST_EVENT_IE_PLTYPE_UNKNOWN:
01290          break;
01291       }
01292 
01293       /* realloc inside one of the append functions failed */
01294       if (!event) {
01295          return NULL;
01296       }
01297    }
01298 
01299    if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
01300       /* If the event is originating on this server, add the server's
01301        * entity ID to the event. */
01302       ast_event_append_eid(&event);
01303    }
01304 
01305    return event;
01306 }

int ast_event_queue ( struct ast_event event  ) 

Queue an event.

Parameters:
event the event to be queued
Return values:
zero success
non-zero failure. Note that the caller of this function is responsible for destroying the event in the case of a failure.

This function queues an event to be dispatched to all of the appropriate subscribers. This function will not block while the event is being dispatched because the event is queued up for a dispatching thread to handle.

Definition at line 1517 of file event.c.

References alloc_event_ref(), ao2_ref, ast_event_check_subscriber(), ast_event_destroy(), AST_EVENT_IE_END, AST_EVENT_SUB_NONE, ast_log(), ast_taskprocessor_push(), ast_event_ref::event, handle_event(), and LOG_WARNING.

Referenced by aji_handle_pubsub_event(), ast_cel_report_event(), ast_event_queue_and_cache(), ast_event_sub_activate(), ast_event_unsubscribe(), devstate_event(), handle_security_event(), process_collection(), and stun_monitor_request().

01518 {
01519    struct ast_event_ref *event_ref;
01520    uint16_t host_event_type;
01521    int res;
01522 
01523    host_event_type = ntohs(event->type);
01524 
01525    /* Invalid type */
01526    if (host_event_type >= AST_EVENT_TOTAL) {
01527       ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
01528          "type '%d'!\n", host_event_type);
01529       return -1;
01530    }
01531 
01532    /* If nobody has subscribed to this event type, throw it away now */
01533    if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
01534          == AST_EVENT_SUB_NONE) {
01535       ast_event_destroy(event);
01536       return 0;
01537    }
01538 
01539    if (!(event_ref = alloc_event_ref())) {
01540       return -1;
01541    }
01542 
01543    event_ref->event = event;
01544 
01545    res = ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
01546    if (res) {
01547       event_ref->event = NULL;
01548       ao2_ref(event_ref, -1);
01549    }
01550    return res;
01551 }

int ast_event_queue_and_cache ( struct ast_event event  ) 

Queue and cache an event.

Parameters:
event the event to be queued and cached

The purpose of caching events is so that the core can retain the last known information for events that represent some sort of state. That way, when code needs to find out the current state, it can query the cache.

The event API already knows which events can be cached and how to cache them.

Return values:
0 success
non-zero failure.

Definition at line 1465 of file event.c.

References ast_event_cache, ast_event_destroy(), ast_event_get_type(), ast_event_queue(), ast_log(), container, event_update_cache(), and LOG_WARNING.

Referenced by aji_handle_pubsub_event(), devstate_event(), handle_request_notify(), notify_message(), process_collection(), queue_event(), and queue_mwi_event().

01466 {
01467    struct ao2_container *container;
01468 
01469    container = ast_event_cache[ast_event_get_type(event)].container;
01470    if (!container) {
01471       ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
01472    } else {
01473       event_update_cache(container, event);
01474    }
01475 
01476    if (ast_event_queue(event)) {
01477       ast_event_destroy(event);
01478    }
01479    return 0;
01480 }

static void ast_event_ref_destroy ( void *  obj  )  [static]

Definition at line 1319 of file event.c.

References ast_event_destroy(), and ast_event_ref::event.

Referenced by alloc_event_ref().

01320 {
01321    struct ast_event_ref *event_ref = obj;
01322 
01323    ast_event_destroy(event_ref->event);
01324 }

void ast_event_report_subs ( const struct ast_event_sub event_sub  ) 

Send AST_EVENT_SUB events to this subscriber of ... subscriber events.

Report current subscriptions to a subscription subscriber.

Definition at line 701 of file event.c.

References ast_event_destroy(), AST_EVENT_IE_EVENTTYPE, AST_EVENT_SUB, ast_event_subs, AST_LIST_TRAVERSE, AST_RWDLLIST_RDLOCK, AST_RWDLLIST_TRAVERSE, AST_RWDLLIST_UNLOCK, ast_event_sub::cb, gen_sub_event(), ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, ast_event_sub::type, ast_event_ie_val::uint, and ast_event_sub::userdata.

Referenced by handle_cli_status(), and start_poll_thread().

00702 {
00703    struct ast_event *event;
00704    struct ast_event_sub *sub;
00705    enum ast_event_type event_type = -1;
00706    struct ast_event_ie_val *ie_val;
00707 
00708    if (event_sub->type != AST_EVENT_SUB)
00709       return;
00710 
00711    AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
00712       if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
00713          event_type = ie_val->payload.uint;
00714          break;
00715       }
00716    }
00717 
00718    if (event_type == -1)
00719       return;
00720 
00721    AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
00722    AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
00723       if (event_sub == sub) {
00724          continue;
00725       }
00726 
00727       event = gen_sub_event(sub);
00728       if (!event) {
00729          continue;
00730       }
00731 
00732       event_sub->cb(event, event_sub->userdata);
00733 
00734       ast_event_destroy(event);
00735    }
00736    AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
00737 }

int ast_event_str_to_event_type ( const char *  str,
enum ast_event_type event_type 
)

Convert a string into an event type.

Parameters:
str the string to convert
event_type an output parameter for the event type
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 288 of file event.c.

References ARRAY_LEN, ast_strlen_zero(), and event_names.

Referenced by event_dump_cache().

00289 {
00290    int i;
00291 
00292    for (i = 0; i < ARRAY_LEN(event_names); i++) {
00293       if (ast_strlen_zero(event_names[i]) || strcasecmp(event_names[i], str)) {
00294          continue;
00295       }
00296 
00297       *event_type = i;
00298       return 0;
00299    }
00300 
00301    return -1;
00302 }

int ast_event_str_to_ie_type ( const char *  str,
enum ast_event_ie_type ie_type 
)

Convert a string to an IE type.

Parameters:
str the string to convert
ie_type an output parameter for the IE type
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 324 of file event.c.

References ARRAY_LEN, ie_maps, and name.

00325 {
00326    int i;
00327 
00328    for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
00329       if (strcasecmp(ie_maps[i].name, str)) {
00330          continue;
00331       }
00332 
00333       *ie_type = i;
00334       return 0;
00335    }
00336 
00337    return -1;
00338 }

int ast_event_sub_activate ( struct ast_event_sub sub  ) 

Activate a dynamically built subscription.

Parameters:
sub the subscription to activate that was allocated using ast_event_subscribe_new()

Once a dynamically built subscription has had all of the parameters added to it, it should be activated using this function.

Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 889 of file event.c.

References ast_event_check_subscriber(), ast_event_destroy(), AST_EVENT_IE_END, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, ast_event_queue(), AST_EVENT_SUB, AST_EVENT_SUB_NONE, ast_event_subs, AST_RWDLLIST_INSERT_TAIL, AST_RWDLLIST_UNLOCK, AST_RWDLLIST_WRLOCK, gen_sub_event(), and ast_event_sub::type.

Referenced by ast_event_subscribe().

00890 {
00891    if (ast_event_check_subscriber(AST_EVENT_SUB,
00892       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00893       AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00894       struct ast_event *event;
00895 
00896       event = gen_sub_event(sub);
00897       if (event && ast_event_queue(event)) {
00898          ast_event_destroy(event);
00899       }
00900    }
00901 
00902    AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00903    AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
00904    AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00905 
00906    return 0;
00907 }

int ast_event_sub_append_ie_bitflags ( struct ast_event_sub sub,
enum ast_event_ie_type  ie_type,
uint32_t  flags 
)

Append a bitflags parameter to a subscription.

Parameters:
sub the dynamic subscription allocated with ast_event_subscribe_new()
ie_type the information element type for the parameter
flags the flags that must be present in the event to match this subscription
Return values:
0 success
non-zero failure
Since:
1.8

Definition at line 783 of file event.c.

References ast_calloc, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_TOTAL, AST_LIST_INSERT_TAIL, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, and ast_event_ie_val::uint.

Referenced by ast_event_subscribe().

00785 {
00786    struct ast_event_ie_val *ie_val;
00787 
00788    if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00789       return -1;
00790    }
00791 
00792    if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00793       return -1;
00794    }
00795 
00796    ie_val->ie_type = ie_type;
00797    ie_val->payload.uint = flags;
00798    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
00799 
00800    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00801 
00802    return 0;
00803 }

int ast_event_sub_append_ie_exists ( struct ast_event_sub sub,
enum ast_event_ie_type  ie_type 
)

Append an 'exists' parameter to a subscription.

Parameters:
sub the dynamic subscription allocated with ast_event_subscribe_new()
ie_type the information element type that must be present in the event for it to match this subscription.
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 805 of file event.c.

References ast_calloc, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_TOTAL, AST_LIST_INSERT_TAIL, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, and ast_event_sub::ie_vals.

Referenced by ast_event_subscribe().

00807 {
00808    struct ast_event_ie_val *ie_val;
00809 
00810    if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00811       return -1;
00812    }
00813 
00814    if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00815       return -1;
00816    }
00817 
00818    ie_val->ie_type = ie_type;
00819    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
00820 
00821    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00822 
00823    return 0;
00824 }

int ast_event_sub_append_ie_raw ( struct ast_event_sub sub,
enum ast_event_ie_type  ie_type,
void *  data,
size_t  raw_datalen 
)

Append a raw parameter to a subscription.

Parameters:
sub the dynamic subscription allocated with ast_event_subscribe_new()
ie_type the information element type for the parameter
data the data that must be present in the event to match this subscription
raw_datalen length of data
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 860 of file event.c.

References ast_calloc, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_TOTAL, ast_free, AST_LIST_INSERT_TAIL, ast_malloc, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, ast_event_ie_val::raw, and ast_event_ie_val::raw_datalen.

Referenced by ast_event_subscribe().

00862 {
00863    struct ast_event_ie_val *ie_val;
00864 
00865    if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00866       return -1;
00867    }
00868 
00869    if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00870       return -1;
00871    }
00872 
00873    ie_val->ie_type = ie_type;
00874    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
00875    ie_val->raw_datalen = raw_datalen;
00876 
00877    if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
00878       ast_free(ie_val);
00879       return -1;
00880    }
00881 
00882    memcpy(ie_val->payload.raw, data, raw_datalen);
00883 
00884    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00885 
00886    return 0;
00887 }

int ast_event_sub_append_ie_str ( struct ast_event_sub sub,
enum ast_event_ie_type  ie_type,
const char *  str 
)

Append a string parameter to a subscription.

Parameters:
sub the dynamic subscription allocated with ast_event_subscribe_new()
ie_type the information element type for the parameter
str the string that must be present in the event to match this subscription
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 826 of file event.c.

References ast_calloc, AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_TOTAL, ast_free, AST_LIST_INSERT_TAIL, ast_str_hash(), ast_strdup, ast_strdupa, ast_tech_to_upper(), ast_event_ie_val::hash, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, and ast_event_ie_val::str.

Referenced by ast_event_subscribe(), and handle_devstate_change().

00828 {
00829    struct ast_event_ie_val *ie_val;
00830 
00831    if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00832       return -1;
00833    }
00834 
00835    if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00836       return -1;
00837    }
00838 
00839    ie_val->ie_type = ie_type;
00840    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
00841 
00842    if (!(ie_val->payload.str = ast_strdup(str))) {
00843       ast_free(ie_val);
00844       return -1;
00845    }
00846 
00847    if (ie_type == AST_EVENT_IE_DEVICE) {
00848       char *uppertech = ast_strdupa(str);
00849       ast_tech_to_upper(uppertech);
00850       ie_val->payload.hash = ast_str_hash(uppertech);
00851    } else {
00852       ie_val->payload.hash = ast_str_hash(str);
00853    }
00854 
00855    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00856 
00857    return 0;
00858 }

int ast_event_sub_append_ie_uint ( struct ast_event_sub sub,
enum ast_event_ie_type  ie_type,
uint32_t  uint 
)

Append a uint parameter to a subscription.

Parameters:
sub the dynamic subscription allocated with ast_event_subscribe_new()
ie_type the information element type for the parameter
uint the value that must be present in the event to match this subscription
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 761 of file event.c.

References ast_calloc, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_TOTAL, AST_LIST_INSERT_TAIL, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, and ast_event_ie_val::uint.

Referenced by ast_event_subscribe(), and handle_cli_status().

00763 {
00764    struct ast_event_ie_val *ie_val;
00765 
00766    if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00767       return -1;
00768    }
00769 
00770    if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00771       return -1;
00772    }
00773 
00774    ie_val->ie_type = ie_type;
00775    ie_val->payload.uint = unsigned_int;
00776    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
00777 
00778    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00779 
00780    return 0;
00781 }

void ast_event_sub_destroy ( struct ast_event_sub sub  ) 

Destroy an allocated subscription.

Parameters:
sub the subscription to destroy

This function should be used when a subscription is allocated with ast_event_subscribe_new(), but for some reason, you want to destroy it instead of activating it. This could be because of an error when reading in the configuration for the dynamically built subscription.

Since:
1.6.1

Definition at line 971 of file event.c.

References ast_event_ie_val_destroy(), ast_free, AST_LIST_REMOVE_HEAD, and ast_event_sub::ie_vals.

Referenced by ast_event_unsubscribe(), event_shutdown(), handle_cli_status(), and handle_devstate_change().

00972 {
00973    struct ast_event_ie_val *ie_val;
00974 
00975    while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
00976       ast_event_ie_val_destroy(ie_val);
00977    }
00978 
00979    ast_free(sub);
00980 }

struct ast_event_sub* ast_event_subscribe ( enum ast_event_type  event_type,
ast_event_cb_t  cb,
const char *  description,
void *  userdata,
  ... 
) [read]

Subscribe to events.

Parameters:
event_type The type of events to subscribe to
cb The function to be called with events
description Description of the subscription.
userdata data to be passed to the event callback

The rest of the arguments to this function specify additional parameters for the subscription to filter which events are passed to this subscriber. The arguments must be in sets of:

and must end with AST_EVENT_IE_END.

If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed by a valid IE payload type. If the payload type specified is AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided. Otherwise, a payload must also be specified.

Returns:
This returns a reference to the subscription for use with un-subscribing later. If there is a failure in creating the subscription, NULL will be returned.

Example usage:

This creates a subscription to AST_EVENT_MWI events that contain an information element, AST_EVENT_IE_MAILBOX, with the same string value contained in peer->mailbox. Also, the event callback will be passed a pointer to the peer.

Note:
A NULL description will cause this function to crash, so watch out!

Definition at line 909 of file event.c.

References ast_copy_string(), AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_event_sub_activate(), ast_event_sub_append_ie_bitflags(), ast_event_sub_append_ie_exists(), ast_event_sub_append_ie_raw(), ast_event_sub_append_ie_str(), ast_event_sub_append_ie_uint(), ast_event_subscribe_new(), ast_event_sub::description, and str.

Referenced by add_peer_mwi_subs(), add_publish_event(), aji_init_event_distribution(), ast_enable_distributed_devstate(), build_gateway(), build_peer(), cc_generic_agent_start_monitoring(), config_line(), create_new_generic_list(), load_module(), load_pbx(), mkintf(), network_change_event_subscribe(), and start_poll_thread().

00911 {
00912    va_list ap;
00913    enum ast_event_ie_type ie_type;
00914    struct ast_event_sub *sub;
00915 
00916    if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
00917       return NULL;
00918    }
00919 
00920    ast_copy_string(sub->description, description, sizeof(sub->description));
00921 
00922    va_start(ap, userdata);
00923    for (ie_type = va_arg(ap, enum ast_event_ie_type);
00924       ie_type != AST_EVENT_IE_END;
00925       ie_type = va_arg(ap, enum ast_event_ie_type))
00926    {
00927       enum ast_event_ie_pltype ie_pltype;
00928 
00929       ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00930 
00931       switch (ie_pltype) {
00932       case AST_EVENT_IE_PLTYPE_UNKNOWN:
00933          break;
00934       case AST_EVENT_IE_PLTYPE_UINT:
00935       {
00936          uint32_t unsigned_int = va_arg(ap, uint32_t);
00937          ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
00938          break;
00939       }
00940       case AST_EVENT_IE_PLTYPE_BITFLAGS:
00941       {
00942          uint32_t unsigned_int = va_arg(ap, uint32_t);
00943          ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
00944          break;
00945       }
00946       case AST_EVENT_IE_PLTYPE_STR:
00947       {
00948          const char *str = va_arg(ap, const char *);
00949          ast_event_sub_append_ie_str(sub, ie_type, str);
00950          break;
00951       }
00952       case AST_EVENT_IE_PLTYPE_RAW:
00953       {
00954          void *data = va_arg(ap, void *);
00955          size_t data_len = va_arg(ap, size_t);
00956          ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
00957          break;
00958       }
00959       case AST_EVENT_IE_PLTYPE_EXISTS:
00960          ast_event_sub_append_ie_exists(sub, ie_type);
00961          break;
00962       }
00963    }
00964    va_end(ap);
00965 
00966    ast_event_sub_activate(sub);
00967 
00968    return sub;
00969 }

struct ast_event_sub* ast_event_subscribe_new ( enum ast_event_type  type,
ast_event_cb_t  cb,
void *  userdata 
) [read]

Allocate a subscription, but do not activate it.

Parameters:
type the event type to subscribe to
cb the function to call when an event matches this subscription
userdata data to pass to the provided callback

This function should be used when you want to dynamically build a subscription.

Returns:
the allocated subscription, or NULL on failure
Since:
1.6.1

Definition at line 739 of file event.c.

References ast_atomic_fetchadd_int(), ast_calloc, ast_log(), ast_event_sub::cb, LOG_ERROR, ast_event_sub::type, ast_event_sub::uniqueid, and ast_event_sub::userdata.

Referenced by ast_event_subscribe(), handle_cli_status(), and handle_devstate_change().

00741 {
00742    struct ast_event_sub *sub;
00743 
00744    if (type < 0 || type >= AST_EVENT_TOTAL) {
00745       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00746       return NULL;
00747    }
00748 
00749    if (!(sub = ast_calloc(1, sizeof(*sub)))) {
00750       return NULL;
00751    }
00752 
00753    sub->type = type;
00754    sub->cb = cb;
00755    sub->userdata = userdata;
00756    sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
00757 
00758    return sub;
00759 }

const char* ast_event_subscriber_get_description ( struct ast_event_sub sub  ) 

Get description for a subscription.

Parameters:
sub subscription
Returns:
string description of the subscription

Definition at line 982 of file event.c.

References ast_event_sub::description.

00983 {
00984    return sub ? sub->description : NULL;
00985 }

struct ast_event_sub* ast_event_unsubscribe ( struct ast_event_sub event_sub  )  [read]

Un-subscribe from events.

Parameters:
event_sub This is the reference to the subscription returned by ast_event_subscribe.

This function will remove a subscription and free the associated data structures.

Returns:
NULL for convenience.
Version:
1.6.1 return changed to NULL

Definition at line 987 of file event.c.

References AST_DLLIST_REMOVE, ast_event_check_subscriber(), ast_event_destroy(), AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_END, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_UNIQUEID, ast_event_new(), ast_event_queue(), ast_event_sub_destroy(), AST_EVENT_SUB_NONE, ast_event_subs, AST_EVENT_UNSUB, AST_RWDLLIST_UNLOCK, AST_RWDLLIST_WRLOCK, ast_event_sub::description, ast_event_sub::type, and ast_event_sub::uniqueid.

Referenced by add_peer_mwi_subs(), cc_generic_agent_destructor(), destroy_dahdi_pvt(), destroy_endpoint(), destroy_mailbox(), generic_agent_devstate_unsubscribe(), generic_monitor_instance_list_destructor(), network_change_event_unsubscribe(), peer_destructor(), publish_event_destroy(), stop_poll_thread(), unload_module(), and unload_pbx().

00988 {
00989    struct ast_event *event;
00990 
00991    AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00992    AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
00993    AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00994 
00995    if (ast_event_check_subscriber(AST_EVENT_UNSUB,
00996       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00997       AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00998 
00999       event = ast_event_new(AST_EVENT_UNSUB,
01000          AST_EVENT_IE_UNIQUEID,    AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
01001          AST_EVENT_IE_EVENTTYPE,   AST_EVENT_IE_PLTYPE_UINT, sub->type,
01002          AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
01003          AST_EVENT_IE_END);
01004       if (event && ast_event_queue(event)) {
01005          ast_event_destroy(event);
01006       }
01007    }
01008 
01009    ast_event_sub_destroy(sub);
01010 
01011    return NULL;
01012 }

static int dump_cache_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 632 of file event.c.

References AST_LIST_TRAVERSE, ast_event_sub::cb, ast_event_ref::event, event_sub, ast_event_sub::ie_vals, match_ie_val(), and ast_event_sub::userdata.

Referenced by ast_event_dump_cache().

00633 {
00634    const struct ast_event_ref *event_ref = obj;
00635    const struct ast_event *event = event_ref->event;
00636    const struct ast_event_sub *event_sub = arg;
00637    struct ast_event_ie_val *ie_val = NULL;
00638 
00639    AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
00640       if (!match_ie_val(event, ie_val, NULL)) {
00641          break;
00642       }
00643    }
00644 
00645    if (!ie_val) {
00646       /* All parameters were matched on this cache entry, so dump it */
00647       event_sub->cb(event, event_sub->userdata);
00648    }
00649 
00650    return 0;
00651 }

static void dump_raw_ie ( struct ast_event_iterator i,
struct ast_cli_args a 
) [static]

Definition at line 1659 of file event.c.

References ast_cli(), ast_eid_to_str(), ast_event_get_ie_type_name(), AST_EVENT_IE_EID, ast_event_iterator_get_ie_raw(), ast_event_iterator_get_ie_type(), ast_cli_args::fd, and ast_event_ie_val::ie_type.

Referenced by event_dump_cli().

01660 {
01661    char eid_buf[32];
01662    enum ast_event_ie_type ie_type;
01663    const char *ie_type_name;
01664 
01665    ie_type = ast_event_iterator_get_ie_type(i);
01666    ie_type_name = ast_event_get_ie_type_name(ie_type);
01667 
01668    switch (ie_type) {
01669    case AST_EVENT_IE_EID:
01670       ast_eid_to_str(eid_buf, sizeof(eid_buf), ast_event_iterator_get_ie_raw(i));
01671       ast_cli(a->fd, "%.30s: %s\n", ie_type_name, eid_buf);
01672       break;
01673    default:
01674       ast_cli(a->fd, "%s\n", ie_type_name);
01675       break;
01676    }
01677 }

static char* event_dump_cache ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 1730 of file event.c.

References ao2_callback, ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ARRAY_LEN, ast_cli(), ast_cli_complete(), ast_event_cache, ast_event_get_ie_type_name(), ast_event_str_to_event_type(), cache_args, cached_event_types, CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, container, event_dump_cli(), ast_cli_args::fd, ast_cli_args::n, OBJ_NODATA, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

01731 {
01732    enum ast_event_type event_type;
01733    enum ast_event_ie_type *cache_args;
01734    int i;
01735 
01736    switch (cmd) {
01737    case CLI_INIT:
01738       e->command = "event dump cache";
01739       e->usage =
01740          "Usage: event dump cache <event type>\n"
01741          "       Dump all of the cached events for the given event type.\n"
01742          "       This is primarily intended for debugging.\n";
01743       return NULL;
01744    case CLI_GENERATE:
01745       if (a->pos == 3) {
01746          return ast_cli_complete(a->word, cached_event_types, a->n);
01747       }
01748       return NULL;
01749    case CLI_HANDLER:
01750       break;
01751    }
01752 
01753    if (a->argc != e->args + 1) {
01754       return CLI_SHOWUSAGE;
01755    }
01756 
01757    if (ast_event_str_to_event_type(a->argv[e->args], &event_type)) {
01758       ast_cli(a->fd, "Invalid cached event type: '%s'\n", a->argv[e->args]);
01759       return CLI_SHOWUSAGE;
01760    }
01761 
01762    if (!ast_event_cache[event_type].container) {
01763       ast_cli(a->fd, "Event type '%s' has no cache.\n", a->argv[e->args]);
01764       return CLI_SUCCESS;
01765    }
01766 
01767    ast_cli(a->fd, "Event Type: %s\n", a->argv[e->args]);
01768    ast_cli(a->fd, "Cache Unique Keys:\n");
01769    cache_args = ast_event_cache[event_type].cache_args;
01770    for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
01771       ast_cli(a->fd, "--> %s\n", ast_event_get_ie_type_name(cache_args[i]));
01772    }
01773 
01774    ast_cli(a->fd, "\n--- Begin Cache Dump ---\n\n");
01775    ao2_callback(ast_event_cache[event_type].container, OBJ_NODATA, event_dump_cli, a);
01776    ast_cli(a->fd, "--- End Cache Dump ---\n\n");
01777 
01778    return CLI_SUCCESS;
01779 }

static int event_dump_cli ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1679 of file event.c.

References ast_cli(), ast_event_get_ie_pltype(), ast_event_get_ie_type_name(), ast_event_get_type_name(), AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_event_iterator_get_ie_bitflags(), ast_event_iterator_get_ie_str(), ast_event_iterator_get_ie_type(), ast_event_iterator_get_ie_uint(), ast_event_iterator_init(), ast_event_iterator_next(), dump_raw_ie(), ast_event_iterator::event, ast_event_ref::event, and ast_cli_args::fd.

Referenced by event_dump_cache().

01680 {
01681    const struct ast_event_ref *event_ref = obj;
01682    const struct ast_event *event = event_ref->event;
01683    struct ast_cli_args *a = arg;
01684    struct ast_event_iterator i;
01685 
01686    if (ast_event_iterator_init(&i, event)) {
01687       ast_cli(a->fd, "Failed to initialize event iterator.  :-(\n");
01688       return 0;
01689    }
01690 
01691    ast_cli(a->fd, "Event: %s\n", ast_event_get_type_name(event));
01692 
01693    do {
01694       enum ast_event_ie_type ie_type;
01695       enum ast_event_ie_pltype ie_pltype;
01696       const char *ie_type_name;
01697 
01698       ie_type = ast_event_iterator_get_ie_type(&i);
01699       ie_type_name = ast_event_get_ie_type_name(ie_type);
01700       ie_pltype = ast_event_get_ie_pltype(ie_type);
01701 
01702       switch (ie_pltype) {
01703       case AST_EVENT_IE_PLTYPE_UNKNOWN:
01704       case AST_EVENT_IE_PLTYPE_EXISTS:
01705          ast_cli(a->fd, "%s\n", ie_type_name);
01706          break;
01707       case AST_EVENT_IE_PLTYPE_STR:
01708          ast_cli(a->fd, "%.30s: %s\n", ie_type_name,
01709                ast_event_iterator_get_ie_str(&i));
01710          break;
01711       case AST_EVENT_IE_PLTYPE_UINT:
01712          ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
01713                ast_event_iterator_get_ie_uint(&i));
01714          break;
01715       case AST_EVENT_IE_PLTYPE_BITFLAGS:
01716          ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
01717                ast_event_iterator_get_ie_bitflags(&i));
01718          break;
01719       case AST_EVENT_IE_PLTYPE_RAW:
01720          dump_raw_ie(&i, a);
01721          break;
01722       }
01723    } while (!ast_event_iterator_next(&i));
01724 
01725    ast_cli(a->fd, "\n");
01726 
01727    return 0;
01728 }

static void event_shutdown ( void   )  [static]

Definition at line 1786 of file event.c.

References ao2_ref, ARRAY_LEN, ast_cli_unregister_multiple(), ast_event_cache, ast_event_sub_destroy(), ast_event_subs, AST_RWDLLIST_HEAD_DESTROY, AST_RWDLLIST_REMOVE_HEAD, AST_RWDLLIST_UNLOCK, AST_RWDLLIST_WRLOCK, ast_taskprocessor_unreference(), container, event_cli, and hash_fn.

Referenced by ast_event_init().

01787 {
01788    struct ast_event_sub *sub;
01789    int i;
01790 
01791    ast_cli_unregister_multiple(event_cli, ARRAY_LEN(event_cli));
01792 
01793    if (event_dispatcher) {
01794       event_dispatcher = ast_taskprocessor_unreference(event_dispatcher);
01795    }
01796 
01797    /* Remove any remaining subscriptions.  Note that we can't just call
01798     * unsubscribe, as it will attempt to lock the subscription list
01799     * as well */
01800    for (i = 0; i < AST_EVENT_TOTAL; i++) {
01801       AST_RWDLLIST_WRLOCK(&ast_event_subs[i]);
01802       while ((sub = AST_RWDLLIST_REMOVE_HEAD(&ast_event_subs[i], entry))) {
01803          ast_event_sub_destroy(sub);
01804       }
01805       AST_RWDLLIST_UNLOCK(&ast_event_subs[i]);
01806       AST_RWDLLIST_HEAD_DESTROY(&ast_event_subs[i]);
01807    }
01808 
01809    for (i = 0; i < AST_EVENT_TOTAL; i++) {
01810       if (!ast_event_cache[i].hash_fn) {
01811          continue;
01812       }
01813       if (ast_event_cache[i].container) {
01814          ao2_ref(ast_event_cache[i].container, -1);
01815       }
01816    }
01817 }

static void event_update_cache ( struct ao2_container cache,
struct ast_event event 
) [static]

Definition at line 1434 of file event.c.

References alloc_event_ref(), ao2_callback, ao2_link, ao2_lock, ao2_ref, ao2_unlock, ast_event_cmp(), ast_event_destroy(), ast_event_dup(), ast_event_ref::event, OBJ_MULTIPLE, OBJ_NODATA, OBJ_POINTER, and OBJ_UNLINK.

Referenced by ast_event_queue_and_cache().

01435 {
01436    struct ast_event_ref tmp_event_ref = {
01437       .event = event,
01438    };
01439    struct ast_event *dup_event;
01440    struct ast_event_ref *event_ref;
01441 
01442    /* Hold the cache container lock while it is updated. */
01443    ao2_lock(cache);
01444 
01445    /* Remove matches from the cache. */
01446    ao2_callback(cache, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
01447       ast_event_cmp, &tmp_event_ref);
01448 
01449    /* Save a copy of the event in the cache. */
01450    dup_event = ast_event_dup(event);
01451    if (dup_event) {
01452       event_ref = alloc_event_ref();
01453       if (event_ref) {
01454          event_ref->event = dup_event;
01455          ao2_link(cache, event_ref);
01456          ao2_ref(event_ref, -1);
01457       } else {
01458          ast_event_destroy(dup_event);
01459       }
01460    }
01461 
01462    ao2_unlock(cache);
01463 }

static struct ast_event* gen_sub_event ( struct ast_event_sub sub  )  [static, read]

Definition at line 660 of file event.c.

References ast_event_append_ie_bitflags(), ast_event_append_ie_raw(), ast_event_append_ie_str(), ast_event_append_ie_uint(), AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_END, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_EXISTS, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, AST_EVENT_IE_UNIQUEID, ast_event_new(), AST_EVENT_SUB, AST_LIST_TRAVERSE, ast_event_sub::description, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, ast_event_sub::type, ast_event_ie_val::uint, and ast_event_sub::uniqueid.

Referenced by ast_event_report_subs(), and ast_event_sub_activate().

00661 {
00662    struct ast_event_ie_val *ie_val;
00663    struct ast_event *event;
00664 
00665    event = ast_event_new(AST_EVENT_SUB,
00666       AST_EVENT_IE_UNIQUEID,    AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
00667       AST_EVENT_IE_EVENTTYPE,   AST_EVENT_IE_PLTYPE_UINT, sub->type,
00668       AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
00669       AST_EVENT_IE_END);
00670    if (!event)
00671       return NULL;
00672 
00673    AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
00674       switch (ie_val->ie_pltype) {
00675       case AST_EVENT_IE_PLTYPE_UNKNOWN:
00676          break;
00677       case AST_EVENT_IE_PLTYPE_EXISTS:
00678          ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
00679          break;
00680       case AST_EVENT_IE_PLTYPE_UINT:
00681          ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
00682          break;
00683       case AST_EVENT_IE_PLTYPE_BITFLAGS:
00684          ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
00685          break;
00686       case AST_EVENT_IE_PLTYPE_STR:
00687          ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
00688          break;
00689       case AST_EVENT_IE_PLTYPE_RAW:
00690          ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
00691          break;
00692       }
00693       if (!event)
00694          break;
00695    }
00696 
00697    return event;
00698 }

static int handle_event ( void *  data  )  [static]

Definition at line 1482 of file event.c.

References ao2_ref, ARRAY_LEN, AST_EVENT_ALL, ast_event_subs, AST_LIST_TRAVERSE, AST_RWDLLIST_RDLOCK, AST_RWDLLIST_TRAVERSE, AST_RWDLLIST_UNLOCK, ast_event_sub::cb, ast_event_ref::event, ast_event_sub::ie_vals, match_ie_val(), and ast_event_sub::userdata.

Referenced by ast_event_queue().

01483 {
01484    struct ast_event_ref *event_ref = data;
01485    struct ast_event_sub *sub;
01486    const enum ast_event_type event_types[] = {
01487       ntohs(event_ref->event->type),
01488       AST_EVENT_ALL
01489    };
01490    int i;
01491 
01492    for (i = 0; i < ARRAY_LEN(event_types); i++) {
01493       AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
01494       AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
01495          struct ast_event_ie_val *ie_val;
01496 
01497          AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
01498             if (!match_ie_val(event_ref->event, ie_val, NULL)) {
01499                /* The current subscription ie did not match an event ie. */
01500                break;
01501             }
01502          }
01503          if (ie_val) {
01504             /* The event did not match this subscription. */
01505             continue;
01506          }
01507          sub->cb(event_ref->event, sub->userdata);
01508       }
01509       AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
01510    }
01511 
01512    ao2_ref(event_ref, -1);
01513 
01514    return 0;
01515 }

static int match_ie_val ( const struct ast_event event,
const struct ast_event_ie_val ie_val,
const struct ast_event event2 
) [static]

Definition at line 559 of file event.c.

References ast_event_get_ie_bitflags(), ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), ast_event_get_ie_str(), ast_event_get_ie_str_hash(), ast_event_get_ie_uint(), AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_strdupa, ast_tech_to_upper(), ast_event_ie_val::hash, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, str, and ast_event_ie_val::uint.

Referenced by ast_event_cmp(), dump_cache_cb(), and handle_event().

00561 {
00562    switch (ie_val->ie_pltype) {
00563    case AST_EVENT_IE_PLTYPE_UINT:
00564    {
00565       uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
00566 
00567       return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
00568    }
00569 
00570    case AST_EVENT_IE_PLTYPE_BITFLAGS:
00571    {
00572       uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
00573 
00574       /*
00575        * If the subscriber has requested *any* of the bitflags that this event provides,
00576        * then it's a match.
00577        */
00578       return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
00579    }
00580 
00581    case AST_EVENT_IE_PLTYPE_STR:
00582    {
00583       const char *str;
00584       uint32_t hash;
00585 
00586       hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
00587       if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
00588          return 0;
00589       }
00590 
00591       str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
00592       if (str) {
00593          const char *e1str, *e2str;
00594          e1str = ast_event_get_ie_str(event, ie_val->ie_type);
00595          e2str = str;
00596 
00597          if (ie_val->ie_type == AST_EVENT_IE_DEVICE) {
00598             e1str = ast_tech_to_upper(ast_strdupa(e1str));
00599             e2str = ast_tech_to_upper(ast_strdupa(e2str));
00600          }
00601 
00602          if (!strcmp(e1str, e2str)) {
00603             return 1;
00604          }
00605       }
00606 
00607       return 0;
00608    }
00609 
00610    case AST_EVENT_IE_PLTYPE_RAW:
00611    {
00612       const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
00613       uint16_t ie_payload_len = event2 ? ast_event_get_ie_raw_payload_len(event2, ie_val->ie_type) : ie_val->raw_datalen;
00614 
00615       return (buf
00616          && ie_payload_len == ast_event_get_ie_raw_payload_len(event, ie_val->ie_type)
00617          && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_payload_len)) ? 1 : 0;
00618    }
00619 
00620    case AST_EVENT_IE_PLTYPE_EXISTS:
00621    {
00622       return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
00623    }
00624 
00625    case AST_EVENT_IE_PLTYPE_UNKNOWN:
00626       return 0;
00627    }
00628 
00629    return 0;
00630 }

static int match_sub_ie_val_to_event ( const struct ast_event_ie_val sub_ie_val,
const struct ast_ev_check_list check_ie_vals 
) [static]

Definition at line 383 of file event.c.

References AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, AST_LIST_TRAVERSE, ast_strdupa, ast_tech_to_upper(), ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, and ast_event_ie_val::uint.

Referenced by ast_event_check_subscriber().

00384 {
00385    const struct ast_event_ie_val *event_ie_val;
00386    int res = 0;
00387 
00388    AST_LIST_TRAVERSE(&check_ie_vals->ie_vals, event_ie_val, entry) {
00389       if (sub_ie_val->ie_type == event_ie_val->ie_type) {
00390          break;
00391       }
00392    }
00393    if (!event_ie_val) {
00394       /* We did not find the event ie the subscriber cares about. */
00395       return 0;
00396    }
00397 
00398    if (sub_ie_val->ie_pltype != event_ie_val->ie_pltype) {
00399       if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
00400          /* The subscription only cares that this ie exists. */
00401          return 1;
00402       }
00403       /* Payload types do not match. */
00404       return 0;
00405    }
00406 
00407    switch (sub_ie_val->ie_pltype) {
00408    case AST_EVENT_IE_PLTYPE_UINT:
00409       res = (sub_ie_val->payload.uint == event_ie_val->payload.uint);
00410       break;
00411    case AST_EVENT_IE_PLTYPE_BITFLAGS:
00412       /*
00413        * If the subscriber has requested *any* of the bitflags we are providing,
00414        * then it's a match.
00415        */
00416       res = (sub_ie_val->payload.uint & event_ie_val->payload.uint);
00417       break;
00418    case AST_EVENT_IE_PLTYPE_STR:
00419    {
00420       const char *substr = sub_ie_val->payload.str;
00421       const char *estr = event_ie_val->payload.str;
00422       if (sub_ie_val->ie_type == AST_EVENT_IE_DEVICE) {
00423          substr = ast_tech_to_upper(ast_strdupa(substr));
00424          estr = ast_tech_to_upper(ast_strdupa(estr));
00425       }
00426       res = !strcmp(substr, estr);
00427       break;
00428    }
00429    case AST_EVENT_IE_PLTYPE_RAW:
00430       res = (sub_ie_val->raw_datalen == event_ie_val->raw_datalen
00431          && !memcmp(sub_ie_val->payload.raw, event_ie_val->payload.raw,
00432             sub_ie_val->raw_datalen));
00433       break;
00434    case AST_EVENT_IE_PLTYPE_EXISTS:
00435       /* Should never get here since check_ie_vals cannot have this type. */
00436       break;
00437    case AST_EVENT_IE_PLTYPE_UNKNOWN:
00438       /*
00439        * Should never be in a subscription event ie val list and
00440        * check_ie_vals cannot have this type either.
00441        */
00442       break;
00443    }
00444 
00445    return res;
00446 }


Variable Documentation

struct { ... } ast_event_cache[AST_EVENT_TOTAL] [static]
struct ast_event_sub_list ast_event_subs[AST_EVENT_TOTAL] [static]

Event subscriptions The event subscribers are indexed by which event they are subscribed to.

Referenced by ast_event_check_subscriber(), ast_event_init(), ast_event_report_subs(), ast_event_sub_activate(), ast_event_unsubscribe(), event_shutdown(), and handle_event().

enum ast_event_ie_type cache_args[MAX_CACHE_ARGS]

Information Elements used for caching.

This array is the set of information elements that will be unique among all events in the cache for this event type. When a new event gets cached, a previous event with the same values for these information elements will be replaced.

Definition at line 174 of file event.c.

Referenced by ast_event_cmp(), and event_dump_cache().

const char* const cached_event_types[] = { "MWI", "DeviceState", "DeviceStateChange", NULL } [static]

Names of cached event types, for CLI tab completion.

Note:
These names must match what is in the event_names array.

Definition at line 195 of file event.c.

Referenced by event_dump_cache().

Container of cached events.

This gets allocated in ast_event_init() when Asterisk starts for the event types declared as using the cache.

Definition at line 163 of file event.c.

struct ast_cli_entry event_cli[] [static]
Initial value:
 {
   AST_CLI_DEFINE(event_dump_cache, "Dump the internal event cache (for debugging)"),
}

Definition at line 1781 of file event.c.

Referenced by ast_event_init(), and event_shutdown().

Definition at line 47 of file event.c.

const char* const event_names[AST_EVENT_TOTAL] [static]

Event Names.

Definition at line 200 of file event.c.

Referenced by ast_event_get_type_name(), and ast_event_str_to_event_type().

Event type specific hash function.

Definition at line 165 of file event.c.

Referenced by ast_event_hash(), ast_event_init(), and event_shutdown().

struct ie_map ie_maps[AST_EVENT_IE_TOTAL] [static]

IE payload types and names.

Referenced by ast_event_get_ie_pltype(), ast_event_get_ie_type_name(), and ast_event_str_to_ie_type().

uint32_t sub_uniqueid [static]

Definition at line 134 of file event.c.


Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1