Fri Jun 19 12:10:33 2009

Asterisk developer's documentation


event.h File Reference

#include "asterisk/event_defs.h"

Go to the source code of this file.

Typedefs

typedef void(*) ast_event_cb_t (const struct ast_event *event, void *userdata)
 Subscriber event callback type.

Functions

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 event_type,...)
 Check if subscribers exist.
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 subscriber.
ast_eventast_event_get_cached (enum ast_event_type,...)
 Retrieve an event from the cache.
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.
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.
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.
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 ierator as an integer payload.
void 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.
ast_eventast_event_new (enum ast_event_type event_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.
void ast_event_report_subs (const struct ast_event_sub *sub)
 Report current subscriptions to a subscription subscriber.
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_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 uint)
 Append a uint parameter to a subscription.
void ast_event_sub_destroy (struct ast_event_sub *sub)
 Destroy an allocated subscription.
ast_event_subast_event_subscribe (enum ast_event_type event_type, ast_event_cb_t cb, void *userdata,...)
 Subscribe to events.
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.
ast_event_subast_event_unsubscribe (struct ast_event_sub *event_sub)
 Un-subscribe from events.


Detailed Description

Author:
Russell Bryant <russell@digium.com> Generic event system

Definition in file event.h.


Typedef Documentation

typedef void(*) ast_event_cb_t(const struct ast_event *event, void *userdata)

Subscriber event callback type.

Parameters:
event the event being passed to the subscriber
userdata the data provider in the call to ast_event_subscribe()
Returns:
The event callbacks do not return anything.

Definition at line 74 of file event.h.


Function Documentation

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 874 of file event.c.

References ast_realloc, ast_event_ie::ie_payload, and ast_event_ie::ie_payload_len.

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

00876 {
00877    struct ast_event_ie *ie;
00878    unsigned int extra_len;
00879    uint16_t event_len;
00880 
00881    event_len = ntohs((*event)->event_len);
00882    extra_len = sizeof(*ie) + data_len;
00883 
00884    if (!(*event = ast_realloc(*event, event_len + extra_len)))
00885       return -1;
00886 
00887    ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
00888    ie->ie_type = htons(ie_type);
00889    ie->ie_payload_len = htons(data_len);
00890    memcpy(ie->ie_payload, data, data_len);
00891 
00892    (*event)->event_len = htons(event_len + extra_len);
00893 
00894    return 0;
00895 }

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 852 of file event.c.

References ast_event_append_ie_raw(), ast_str_hash(), ast_event_ie_str_payload::hash, and ast_event_ie_str_payload::str.

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

00854 {
00855    struct ast_event_ie_str_payload *str_payload;
00856    size_t payload_len;
00857 
00858    payload_len = sizeof(*str_payload) + strlen(str);
00859    str_payload = alloca(payload_len);
00860 
00861    strcpy(str_payload->str, str);
00862    str_payload->hash = ast_str_hash(str);
00863 
00864    return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
00865 }

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 867 of file event.c.

References ast_event_append_ie_raw().

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

00869 {
00870    data = htonl(data);
00871    return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
00872 }

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 322 of file event.c.

References AST_DLLIST_EMPTY, AST_EVENT_ALL, AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_SUB_EXISTS, AST_EVENT_SUB_NONE, ast_event_subs, AST_LIST_HEAD_NOLOCK_STATIC, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), AST_RWDLLIST_RDLOCK, AST_RWDLLIST_TRAVERSE, AST_RWDLLIST_UNLOCK, ast_strdupa, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, LOG_ERROR, 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().

00323 {
00324    va_list ap;
00325    enum ast_event_ie_type ie_type;
00326    enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
00327    struct ast_event_ie_val *ie_val, *sub_ie_val;
00328    struct ast_event_sub *sub;
00329    AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
00330 
00331    if (type >= AST_EVENT_TOTAL) {
00332       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00333       return res;
00334    }
00335 
00336    va_start(ap, type);
00337    for (ie_type = va_arg(ap, enum ast_event_type);
00338       ie_type != AST_EVENT_IE_END;
00339       ie_type = va_arg(ap, enum ast_event_type))
00340    {
00341       struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
00342       memset(ie_value, 0, sizeof(*ie_value));
00343       ie_value->ie_type = ie_type;
00344       ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00345       if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
00346          ie_value->payload.uint = va_arg(ap, uint32_t);
00347       else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
00348          ie_value->payload.str = ast_strdupa(va_arg(ap, const char *));
00349       else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
00350          void *data = va_arg(ap, void *);
00351          size_t datalen = va_arg(ap, size_t);
00352          ie_value->payload.raw = alloca(datalen);
00353          memcpy(ie_value->payload.raw, data, datalen);
00354          ie_value->raw_datalen = datalen;
00355       }
00356       AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
00357    }
00358    va_end(ap);
00359 
00360    AST_RWDLLIST_RDLOCK(&ast_event_subs[type]);
00361    AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
00362       AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
00363          AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
00364             if (sub_ie_val->ie_type == ie_val->ie_type)
00365                break;
00366          }
00367          if (!sub_ie_val) {
00368             if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
00369                break;
00370             continue;
00371          }
00372          /* The subscriber doesn't actually care what the value is */
00373          if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
00374             continue;
00375          if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
00376             ie_val->payload.uint != sub_ie_val->payload.uint)
00377             break;
00378          if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
00379             strcmp(ie_val->payload.str, sub_ie_val->payload.str))
00380             break;
00381          if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW &&
00382             memcmp(ie_val->payload.raw, sub_ie_val->payload.raw, ie_val->raw_datalen))
00383             break;
00384       }
00385       if (!ie_val)
00386          break;
00387    }
00388    AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
00389 
00390    if (sub) /* All parameters were matched */
00391       return AST_EVENT_SUB_EXISTS;
00392 
00393    AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
00394    if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
00395       res = AST_EVENT_SUB_EXISTS;
00396    AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
00397 
00398    return res;
00399 }

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 963 of file event.c.

References ast_free.

Referenced by ast_event_dup_and_cache(), ast_event_get_cached(), ast_event_queue(), ast_event_ref_destroy(), ast_event_report_subs(), devstate_cached(), get_cached_mwi(), has_voicemail(), process_collection(), unistim_send_mwi_to_peer(), and update_registry().

00964 {
00965    ast_free(event);
00966 }

void ast_event_dump_cache ( const struct ast_event_sub event_sub  ) 

Dump the event cache for the subscriber.

Since:
1.6.1

Definition at line 466 of file event.c.

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

Referenced by handle_devstate_change().

00467 {
00468    ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
00469          dump_cache_cb, (void *) event_sub);
00470 }

struct ast_event* ast_event_get_cached ( enum  ast_event_type,
  ... 
)

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:
    <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:
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 991 of file event.c.

References ao2_find, ao2_ref, 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_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().

00992 {
00993    va_list ap;
00994    enum ast_event_ie_type ie_type;
00995    struct ast_event *dup_event = NULL;
00996    struct ast_event_ref *cached_event_ref;
00997    struct ast_event *cache_arg_event;
00998    struct ast_event_ref tmp_event_ref = {
00999       .event = NULL,
01000    };
01001    struct ao2_container *container = NULL;
01002 
01003    if (type >= AST_EVENT_TOTAL) {
01004       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
01005       return NULL;
01006    }
01007 
01008    if (!(container = ast_event_cache[type].container)) {
01009       ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
01010       return NULL;
01011    }
01012 
01013    if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
01014       return NULL;
01015    }
01016 
01017    va_start(ap, type);
01018    for (ie_type = va_arg(ap, enum ast_event_type);
01019       ie_type != AST_EVENT_IE_END;
01020       ie_type = va_arg(ap, enum ast_event_type))
01021    {
01022       enum ast_event_ie_pltype ie_pltype;
01023 
01024       ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
01025 
01026       switch (ie_pltype) {
01027       case AST_EVENT_IE_PLTYPE_UINT:
01028          ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
01029          break;
01030       case AST_EVENT_IE_PLTYPE_STR:
01031          ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
01032          break;
01033       case AST_EVENT_IE_PLTYPE_RAW:
01034       {
01035          void *data = va_arg(ap, void *);
01036          size_t datalen = va_arg(ap, size_t);
01037          ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
01038       }
01039       case AST_EVENT_IE_PLTYPE_EXISTS:
01040          ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
01041          break;
01042       case AST_EVENT_IE_PLTYPE_UNKNOWN:
01043          break;
01044       }
01045    }
01046    va_end(ap);
01047 
01048    tmp_event_ref.event = cache_arg_event;
01049 
01050    cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
01051 
01052    ast_event_destroy(cache_arg_event);
01053    cache_arg_event = NULL;
01054 
01055    if (cached_event_ref) {
01056       dup_event = ast_event_dup(cached_event_ref->event);
01057       ao2_ref(cached_event_ref, -1);
01058       cached_event_ref = NULL;
01059    }
01060 
01061    return dup_event;
01062 }

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 265 of file event.c.

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

00266 {
00267    if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
00268       ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00269       return AST_EVENT_IE_PLTYPE_UNKNOWN;
00270    }
00271 
00272    if (ie_maps[ie_type].ie_type != ie_type) {
00273       ast_log(LOG_ERROR, "The ie type passed in does not match the ie type defined in the ie table.\n");
00274       return AST_EVENT_IE_PLTYPE_UNKNOWN;
00275    }
00276 
00277    return ie_maps[ie_type].ie_pltype;
00278 }

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 839 of file event.c.

References ast_event_iterator_get_ie_raw(), ast_event_iterator_get_ie_type(), ast_event_iterator_init(), ast_event_iterator_next(), and ast_event_iterator::event.

Referenced by 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(), and match_ie_val().

00840 {
00841    struct ast_event_iterator iterator;
00842    int res = 0;
00843 
00844    for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
00845       if (ast_event_iterator_get_ie_type(&iterator) == ie_type)
00846          return ast_event_iterator_get_ie_raw(&iterator);
00847    }
00848 
00849    return NULL;
00850 }

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 830 of file event.c.

References ast_event_get_ie_raw(), and ast_event_ie_str_payload::str.

Referenced by ast_event_hash_devstate(), ast_event_hash_devstate_change(), ast_event_hash_mwi(), device_state_cb(), devstate_change_collector_cb(), match_ie_val(), and mwi_sub_event_cb().

00831 {
00832    const struct ast_event_ie_str_payload *str_payload;
00833 
00834    str_payload = ast_event_get_ie_raw(event, ie_type);
00835 
00836    return str_payload->str;
00837 }

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 821 of file event.c.

References ast_event_get_ie_raw(), and ast_event_ie_str_payload::hash.

Referenced by match_ie_val().

00822 {
00823    const struct ast_event_ie_str_payload *str_payload;
00824 
00825    str_payload = ast_event_get_ie_raw(event, ie_type);
00826 
00827    return str_payload->hash;
00828 }

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 250 of file event.c.

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

00251 {
00252    if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
00253       ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00254       return "";
00255    }
00256 
00257    if (ie_maps[ie_type].ie_type != ie_type) {
00258       ast_log(LOG_ERROR, "The ie type passed in does not match the ie type defined in the ie table.\n");
00259       return "";
00260    }
00261 
00262    return ie_maps[ie_type].name;
00263 }

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 812 of file event.c.

References ast_event_get_ie_raw(), and get_unaligned_uint32().

Referenced by device_state_cb(), devstate_cache_cb(), devstate_cached(), devstate_change_collector_cb(), get_cached_mwi(), has_voicemail(), match_ie_val(), mwi_sub_event_cb(), mwi_unsub_event_cb(), process_collection(), sip_send_mwi_to_peer(), unistim_send_mwi_to_peer(), and update_registry().

00813 {
00814    const uint32_t *ie_val;
00815 
00816    ie_val = ast_event_get_ie_raw(event, ie_type);
00817 
00818    return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
00819 }

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 295 of file event.c.

References ast_event::event_len.

Referenced by ast_event_dup().

00296 {
00297    size_t res;
00298 
00299    res = ntohs(event->event_len);
00300 
00301    return res;
00302 }

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 807 of file event.c.

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

00808 {
00809    return ntohs(event->type);
00810 }

const char* ast_event_get_type_name ( const struct ast_event event  ) 

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

Returns:
the string representation of the event type of the provided event
Since:
1.6.1

Definition at line 221 of file event.c.

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

00222 {
00223    enum ast_event_type type;
00224 
00225    type = ast_event_get_type(event);
00226 
00227    if (type >= AST_EVENT_TOTAL || type < 0) {
00228       ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
00229       return "";
00230    }
00231 
00232    return event_names[type].name;
00233 }

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 802 of file event.c.

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

Referenced by ast_event_get_ie_raw().

00803 {
00804    return iterator->ie->ie_payload;
00805 }

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 793 of file event.c.

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

00794 {
00795    const struct ast_event_ie_str_payload *str_payload;
00796 
00797    str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
00798 
00799    return str_payload->str;
00800 }

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 783 of file event.c.

References ast_event_iterator::ie.

Referenced by ast_event_get_ie_raw().

00784 {
00785    return ntohs(iterator->ie->ie_type);
00786 }

uint32_t ast_event_iterator_get_ie_uint ( struct ast_event_iterator iterator  ) 

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

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

Definition at line 788 of file event.c.

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

00789 {
00790    return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
00791 }

void 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
Returns:
Nothing

Definition at line 769 of file event.c.

References ast_event_iterator::event, ast_event::event_len, ast_event_iterator::event_len, and ast_event_iterator::ie.

Referenced by ast_event_get_ie_raw().

00770 {
00771    iterator->event_len = ntohs(event->event_len);
00772    iterator->event = event;
00773    iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
00774    return;
00775 }

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

00778 {
00779    iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
00780    return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
00781 }

struct ast_event* ast_event_new ( enum ast_event_type  event_type,
  ... 
)

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

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 897 of file event.c.

References ast_calloc, ast_eid_default, 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_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_LIST_HEAD_NOLOCK_STATIC, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_strdupa, ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_sub::ie_vals, 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 ast_event_get_cached(), ast_event_unsubscribe(), devstate_event(), gen_sub_event(), notify_message(), process_collection(), and queue_mwi_event().

00898 {
00899    va_list ap;
00900    struct ast_event *event;
00901    enum ast_event_type ie_type;
00902    struct ast_event_ie_val *ie_val;
00903    AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
00904 
00905    /* Invalid type */
00906    if (type >= AST_EVENT_TOTAL) {
00907       ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
00908          "type '%d'!\n", type);
00909       return NULL;
00910    }
00911 
00912    va_start(ap, type);
00913    for (ie_type = va_arg(ap, enum ast_event_type);
00914       ie_type != AST_EVENT_IE_END;
00915       ie_type = va_arg(ap, enum ast_event_type))
00916    {
00917       struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
00918       memset(ie_value, 0, sizeof(*ie_value));
00919       ie_value->ie_type = ie_type;
00920       ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00921       if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
00922          ie_value->payload.uint = va_arg(ap, uint32_t);
00923       else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
00924          ie_value->payload.str = ast_strdupa(va_arg(ap, const char *));
00925       else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
00926          void *data = va_arg(ap, void *);
00927          size_t datalen = va_arg(ap, size_t);
00928          ie_value->payload.raw = alloca(datalen);
00929          memcpy(ie_value->payload.raw, data, datalen);
00930          ie_value->raw_datalen = datalen;
00931       }
00932       AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
00933    }
00934    va_end(ap);
00935 
00936    if (!(event = ast_calloc(1, sizeof(*event))))
00937       return NULL;
00938 
00939    event->type = htons(type);
00940    event->event_len = htons(sizeof(*event));
00941 
00942    AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
00943       if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
00944          ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
00945       else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
00946          ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
00947       else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW)
00948          ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
00949 
00950       if (!event)
00951          break;
00952    }
00953 
00954    if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
00955       /* If the event is originating on this server, add the server's
00956        * entity ID to the event. */
00957       ast_event_append_ie_raw(&event, AST_EVENT_IE_EID, &ast_eid_default, sizeof(ast_eid_default));
00958    }
00959 
00960    return event;
00961 }

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
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 a pool of event dispatching threads handle the event queue.

Definition at line 1150 of file event.c.

References alloc_event_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, event_dispatcher, handle_event(), and LOG_WARNING.

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

01151 {
01152    struct ast_event_ref *event_ref;
01153    uint16_t host_event_type;
01154 
01155    host_event_type = ntohs(event->type);
01156 
01157    /* Invalid type */
01158    if (host_event_type >= AST_EVENT_TOTAL) {
01159       ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
01160          "type '%d'!\n", host_event_type);
01161       return -1;
01162    }
01163 
01164    /* If nobody has subscribed to this event type, throw it away now */
01165    if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
01166          == AST_EVENT_SUB_NONE) {
01167       ast_event_destroy(event);
01168       return 0;
01169    }
01170 
01171    if (!(event_ref = alloc_event_ref())) {
01172       return -1;
01173    }
01174 
01175    event_ref->event = event;
01176 
01177    return ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
01178 }

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. If failure is returned, the event must be destroyed by the caller of this function.

Definition at line 1094 of file event.c.

References ao2_callback, ast_event_cache, ast_event_cmp(), ast_event_get_type(), ast_event_queue(), ast_log(), container, ast_event_ref::event, LOG_WARNING, OBJ_MULTIPLE, OBJ_NODATA, OBJ_POINTER, and OBJ_UNLINK.

Referenced by devstate_event(), notify_message(), process_collection(), and queue_mwi_event().

01095 {
01096    struct ao2_container *container;
01097    struct ast_event_ref tmp_event_ref = {
01098       .event = event,
01099    };
01100 
01101    if (!(container = ast_event_cache[ast_event_get_type(event)].container)) {
01102       ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
01103       goto queue_event;
01104    }
01105 
01106    /* Remove matches from the cache */
01107    ao2_callback(container, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
01108          ast_event_cmp, &tmp_event_ref);
01109 
01110 queue_event:
01111    return ast_event_queue(event);
01112 }

void ast_event_report_subs ( const struct ast_event_sub sub  ) 

Report current subscriptions to a subscription subscriber.

Returns:
nothing
This reports all of the current subscribers to a subscriber of subscribers to a specific event type. (Try saying that a few times fast).

The idea here is that it is sometimes very useful for a module to know when someone subscribes to events. However, when they first subscribe, this provides that module the ability to request the event core report to them all of the subscriptions to that event type that already exist.

Definition at line 510 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, event_sub, 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 start_poll_thread().

00511 {
00512    struct ast_event *event;
00513    struct ast_event_sub *sub;
00514    enum ast_event_type event_type = -1;
00515    struct ast_event_ie_val *ie_val;
00516 
00517    if (event_sub->type != AST_EVENT_SUB)
00518       return;
00519 
00520    AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
00521       if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
00522          event_type = ie_val->payload.uint;
00523          break;
00524       }
00525    }
00526 
00527    if (event_type == -1)
00528       return;
00529 
00530    AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
00531    AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
00532       if (event_sub == sub)
00533          continue;
00534 
00535       event = gen_sub_event(sub);
00536 
00537       if (!event)
00538          continue;
00539 
00540       event_sub->cb(event, event_sub->userdata);
00541 
00542       ast_event_destroy(event);
00543    }
00544    AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
00545 }

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 235 of file event.c.

References ARRAY_LEN, event_names, name, and event_name::type.

00236 {
00237    int i;
00238 
00239    for (i = 0; i < ARRAY_LEN(event_names); i++) {
00240       if (strcasecmp(event_names[i].name, str))
00241          continue;
00242 
00243       *event_type = event_names[i].type;
00244       return 0;
00245    }
00246 
00247    return -1;
00248 }

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 280 of file event.c.

References ARRAY_LEN, ie_maps, ie_map::ie_type, and name.

00281 {
00282    int i;
00283 
00284    for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
00285       if (strcasecmp(ie_maps[i].name, str))
00286          continue;
00287 
00288       *ie_type = ie_maps[i].ie_type;
00289       return 0;
00290    }
00291 
00292    return -1;
00293 }

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 660 of file event.c.

References ast_event_check_subscriber(), 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().

00661 {
00662    if (ast_event_check_subscriber(AST_EVENT_SUB,
00663       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00664       AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00665       struct ast_event *event;
00666 
00667       event = gen_sub_event(sub);
00668 
00669       if (event)
00670          ast_event_queue(event);
00671    }
00672 
00673    AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00674    AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
00675    AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00676 
00677    return 0;
00678 }

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 588 of file event.c.

References ast_calloc, AST_EVENT_IE_MAX, AST_EVENT_IE_PLTYPE_EXISTS, AST_LIST_INSERT_TAIL, and ast_event_sub::ie_vals.

Referenced by ast_event_subscribe().

00590 {
00591    struct ast_event_ie_val *ie_val;
00592 
00593    if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
00594       return -1;
00595 
00596    if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
00597       return -1;
00598 
00599    ie_val->ie_type = ie_type;
00600    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
00601 
00602    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00603 
00604    return 0;
00605 }

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
raw the data 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 633 of file event.c.

References ast_calloc, AST_EVENT_IE_MAX, AST_EVENT_IE_PLTYPE_RAW, ast_free, AST_LIST_INSERT_TAIL, ast_malloc, and ast_event_sub::ie_vals.

Referenced by ast_event_subscribe().

00635 {
00636    struct ast_event_ie_val *ie_val;
00637 
00638    if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
00639       return -1;
00640 
00641    if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
00642       return -1;
00643 
00644    ie_val->ie_type = ie_type;
00645    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
00646    ie_val->raw_datalen = raw_datalen;
00647 
00648    if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
00649       ast_free(ie_val);
00650       return -1;
00651    }
00652 
00653    memcpy(ie_val->payload.raw, data, raw_datalen);
00654 
00655    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00656 
00657    return 0;
00658 }

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 607 of file event.c.

References ast_calloc, AST_EVENT_IE_MAX, AST_EVENT_IE_PLTYPE_STR, ast_free, AST_LIST_INSERT_TAIL, ast_str_hash(), ast_strdup, and ast_event_sub::ie_vals.

Referenced by ast_event_subscribe(), and handle_devstate_change().

00609 {
00610    struct ast_event_ie_val *ie_val;
00611 
00612    if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
00613       return -1;
00614 
00615    if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
00616       return -1;
00617 
00618    ie_val->ie_type = ie_type;
00619    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
00620 
00621    if (!(ie_val->payload.str = ast_strdup(str))) {
00622       ast_free(ie_val);
00623       return -1;
00624    }
00625 
00626    ie_val->payload.hash = ast_str_hash(str);
00627 
00628    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00629 
00630    return 0;
00631 }

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 568 of file event.c.

References ast_calloc, AST_EVENT_IE_MAX, AST_EVENT_IE_PLTYPE_UINT, AST_LIST_INSERT_TAIL, and ast_event_sub::ie_vals.

Referenced by ast_event_subscribe().

00570 {
00571    struct ast_event_ie_val *ie_val;
00572 
00573    if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
00574       return -1;
00575 
00576    if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
00577       return -1;
00578 
00579    ie_val->ie_type = ie_type;
00580    ie_val->payload.uint = unsigned_int;
00581    ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
00582 
00583    AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00584 
00585    return 0;
00586 }

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 733 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(), and handle_devstate_change().

00734 {
00735    struct ast_event_ie_val *ie_val;
00736 
00737    while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
00738       ast_event_ie_val_destroy(ie_val);
00739 
00740    ast_free(sub);
00741 }

struct ast_event_sub* ast_event_subscribe ( enum ast_event_type  event_type,
ast_event_cb_t  cb,
void *  userdata,
  ... 
)

Subscribe to events.

Parameters:
event_type The type of events to subscribe to
cb The function to be called with events
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.

Definition at line 680 of file event.c.

References AST_EVENT_IE_END, 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_exists(), ast_event_sub_append_ie_raw(), ast_event_sub_append_ie_str(), ast_event_sub_append_ie_uint(), ast_event_subscribe_new(), and str.

Referenced by add_peer_mwi_subs(), ast_enable_distributed_devstate(), build_device(), build_gateway(), build_peer(), load_module(), load_pbx(), and start_poll_thread().

00682 {
00683    va_list ap;
00684    enum ast_event_ie_type ie_type;
00685    struct ast_event_sub *sub;
00686 
00687    if (!(sub = ast_event_subscribe_new(type, cb, userdata)))
00688       return NULL;
00689 
00690    va_start(ap, userdata);
00691    for (ie_type = va_arg(ap, enum ast_event_type);
00692       ie_type != AST_EVENT_IE_END;
00693       ie_type = va_arg(ap, enum ast_event_type))
00694    {
00695       enum ast_event_ie_pltype ie_pltype;
00696 
00697       ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00698 
00699       switch (ie_pltype) {
00700       case AST_EVENT_IE_PLTYPE_UNKNOWN:
00701          break;
00702       case AST_EVENT_IE_PLTYPE_UINT:
00703       {
00704          uint32_t unsigned_int = va_arg(ap, uint32_t);
00705          ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
00706          break;
00707       }
00708       case AST_EVENT_IE_PLTYPE_STR:
00709       {
00710          const char *str = va_arg(ap, const char *);
00711          ast_event_sub_append_ie_str(sub, ie_type, str);
00712          break;
00713       }
00714       case AST_EVENT_IE_PLTYPE_RAW:
00715       {
00716          void *data = va_arg(ap, void *);
00717          size_t data_len = va_arg(ap, size_t);
00718          ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
00719          break;
00720       }
00721       case AST_EVENT_IE_PLTYPE_EXISTS:
00722          ast_event_sub_append_ie_exists(sub, ie_type);
00723          break;
00724       }
00725    }
00726    va_end(ap);
00727 
00728    ast_event_sub_activate(sub);
00729 
00730    return sub;
00731 }

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

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 547 of file event.c.

References ast_atomic_fetchadd_int(), ast_calloc, ast_log(), and LOG_ERROR.

Referenced by ast_event_subscribe(), and handle_devstate_change().

00549 {
00550    struct ast_event_sub *sub;
00551 
00552    if (type < 0 || type >= AST_EVENT_TOTAL) {
00553       ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00554       return NULL;
00555    }
00556 
00557    if (!(sub = ast_calloc(1, sizeof(*sub))))
00558       return NULL;
00559 
00560    sub->type = type;
00561    sub->cb = cb;
00562    sub->userdata = userdata;
00563    sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
00564 
00565    return sub;
00566 }

struct ast_event_sub* ast_event_unsubscribe ( struct ast_event_sub event_sub  ) 

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 743 of file event.c.

References AST_DLLIST_REMOVE, ast_event_check_subscriber(), AST_EVENT_IE_END, AST_EVENT_IE_EVENTTYPE, 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::type, and ast_event_sub::uniqueid.

Referenced by destroy_dahdi_pvt(), destroy_mailbox(), peer_destructor(), stop_poll_thread(), and unload_module().

00744 {
00745    struct ast_event *event;
00746 
00747    AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00748    AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
00749    AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00750 
00751    if (ast_event_check_subscriber(AST_EVENT_UNSUB,
00752       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00753       AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00754       
00755       event = ast_event_new(AST_EVENT_UNSUB,
00756          AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
00757          AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00758          AST_EVENT_IE_END);
00759 
00760       if (event)
00761          ast_event_queue(event);
00762    }
00763 
00764    ast_event_sub_destroy(sub);
00765 
00766    return NULL;
00767 }


Generated on Fri Jun 19 12:10:34 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7