2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2008, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Internal generic event system
23 * \author Russell Bryant <russell@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
32 #include "asterisk/event.h"
33 #include "asterisk/linkedlists.h"
34 #include "asterisk/dlinkedlists.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/unaligned.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/taskprocessor.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/cli.h"
43 static struct ast_taskprocessor *event_dispatcher;
46 * \brief An event information element
48 * \note The format of this structure is important. Since these events may
49 * be sent directly over a network, changing this structure will break
50 * compatibility with older versions. However, at this point, this code
51 * has not made it into a release, so it is still fair game for change.
54 enum ast_event_ie_type ie_type:16;
55 /*! Total length of the IE payload */
56 uint16_t ie_payload_len;
57 unsigned char ie_payload[0];
58 } __attribute__((packed));
61 * \brief The payload for a string information element
63 struct ast_event_ie_str_payload {
64 /*! \brief A hash calculated with ast_str_hash(), to speed up comparisons */
66 /*! \brief The actual string, null terminated */
68 } __attribute__((packed));
73 * An ast_event consists of an event header (this structure), and zero or
74 * more information elements defined by ast_event_ie.
76 * \note The format of this structure is important. Since these events may
77 * be sent directly over a network, changing this structure will break
78 * compatibility with older versions. However, at this point, this code
79 * has not made it into a release, so it is still fair game for change.
83 enum ast_event_type type:16;
84 /*! Total length of the event */
85 uint16_t event_len:16;
86 /*! The data payload of the event, made up of information elements */
87 unsigned char payload[0];
88 } __attribute__((packed));
92 * \brief A holder for an event
94 * \details This struct used to have more of a purpose than it does now.
95 * It is used to hold events in the event cache. It can be completely removed
96 * if one of these two things is done:
97 * - ast_event gets changed such that it never has to be realloc()d
98 * - astobj2 is updated so that you can realloc() an astobj2 object
100 struct ast_event_ref {
101 struct ast_event *event;
104 struct ast_event_ie_val {
105 AST_LIST_ENTRY(ast_event_ie_val) entry;
106 enum ast_event_ie_type ie_type;
107 enum ast_event_ie_pltype ie_pltype;
119 /*! \brief Event subscription */
120 struct ast_event_sub {
121 enum ast_event_type type;
123 char description[64];
126 AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
127 AST_RWDLLIST_ENTRY(ast_event_sub) entry;
130 static uint32_t sub_uniqueid;
132 /*! \brief Event subscriptions
133 * The event subscribers are indexed by which event they are subscribed to */
134 static AST_RWDLLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
136 static int ast_event_cmp(void *obj, void *arg, int flags);
137 static int ast_event_hash_mwi(const void *obj, const int flags);
138 static int ast_event_hash_devstate(const void *obj, const int flags);
139 static int ast_event_hash_devstate_change(const void *obj, const int flags);
142 #define NUM_CACHE_BUCKETS 17
144 #define NUM_CACHE_BUCKETS 563
147 #define MAX_CACHE_ARGS 8
150 * \brief Event types that are kept in the cache.
154 * \brief Container of cached events
156 * \details This gets allocated in ast_event_init() when Asterisk starts
157 * for the event types declared as using the cache.
159 struct ao2_container *container;
160 /*! \brief Event type specific hash function */
161 ao2_hash_fn *hash_fn;
163 * \brief Information Elements used for caching
165 * \details This array is the set of information elements that will be unique
166 * among all events in the cache for this event type. When a new event gets
167 * cached, a previous event with the same values for these information elements
170 enum ast_event_ie_type cache_args[MAX_CACHE_ARGS];
171 } ast_event_cache[AST_EVENT_TOTAL] = {
173 .hash_fn = ast_event_hash_mwi,
174 .cache_args = { AST_EVENT_IE_MAILBOX, AST_EVENT_IE_CONTEXT },
176 [AST_EVENT_DEVICE_STATE] = {
177 .hash_fn = ast_event_hash_devstate,
178 .cache_args = { AST_EVENT_IE_DEVICE, },
180 [AST_EVENT_DEVICE_STATE_CHANGE] = {
181 .hash_fn = ast_event_hash_devstate_change,
182 .cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
187 * \brief Names of cached event types, for CLI tab completion
189 * \note These names must match what is in the event_names array.
191 static const char * const cached_event_types[] = { "MWI", "DeviceState", "DeviceStateChange", NULL };
196 static const char * const event_names[AST_EVENT_TOTAL] = {
197 [AST_EVENT_CUSTOM] = "Custom",
198 [AST_EVENT_MWI] = "MWI",
199 [AST_EVENT_SUB] = "Subscription",
200 [AST_EVENT_UNSUB] = "Unsubscription",
201 [AST_EVENT_DEVICE_STATE] = "DeviceState",
202 [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
203 [AST_EVENT_CEL] = "CEL",
204 [AST_EVENT_SECURITY] = "Security",
205 [AST_EVENT_NETWORK_CHANGE] = "NetworkChange",
209 * \brief IE payload types and names
211 static const struct ie_map {
212 enum ast_event_ie_pltype ie_pltype;
214 } ie_maps[AST_EVENT_IE_TOTAL] = {
215 [AST_EVENT_IE_NEWMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
216 [AST_EVENT_IE_OLDMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
217 [AST_EVENT_IE_MAILBOX] = { AST_EVENT_IE_PLTYPE_STR, "Mailbox" },
218 [AST_EVENT_IE_UNIQUEID] = { AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
219 [AST_EVENT_IE_EVENTTYPE] = { AST_EVENT_IE_PLTYPE_UINT, "EventType" },
220 [AST_EVENT_IE_EXISTS] = { AST_EVENT_IE_PLTYPE_UINT, "Exists" },
221 [AST_EVENT_IE_DEVICE] = { AST_EVENT_IE_PLTYPE_STR, "Device" },
222 [AST_EVENT_IE_STATE] = { AST_EVENT_IE_PLTYPE_UINT, "State" },
223 [AST_EVENT_IE_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "Context" },
224 [AST_EVENT_IE_EID] = { AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
225 [AST_EVENT_IE_CEL_EVENT_TYPE] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventType" },
226 [AST_EVENT_IE_CEL_EVENT_TIME] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTime" },
227 [AST_EVENT_IE_CEL_EVENT_TIME_USEC] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTimeUSec" },
228 [AST_EVENT_IE_CEL_USEREVENT_NAME] = { AST_EVENT_IE_PLTYPE_UINT, "CELUserEventName" },
229 [AST_EVENT_IE_CEL_CIDNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDName" },
230 [AST_EVENT_IE_CEL_CIDNUM] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDNum" },
231 [AST_EVENT_IE_CEL_EXTEN] = { AST_EVENT_IE_PLTYPE_STR, "CELExten" },
232 [AST_EVENT_IE_CEL_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "CELContext" },
233 [AST_EVENT_IE_CEL_CHANNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELChanName" },
234 [AST_EVENT_IE_CEL_APPNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELAppName" },
235 [AST_EVENT_IE_CEL_APPDATA] = { AST_EVENT_IE_PLTYPE_STR, "CELAppData" },
236 [AST_EVENT_IE_CEL_AMAFLAGS] = { AST_EVENT_IE_PLTYPE_STR, "CELAMAFlags" },
237 [AST_EVENT_IE_CEL_ACCTCODE] = { AST_EVENT_IE_PLTYPE_UINT, "CELAcctCode" },
238 [AST_EVENT_IE_CEL_UNIQUEID] = { AST_EVENT_IE_PLTYPE_STR, "CELUniqueID" },
239 [AST_EVENT_IE_CEL_USERFIELD] = { AST_EVENT_IE_PLTYPE_STR, "CELUserField" },
240 [AST_EVENT_IE_CEL_CIDANI] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDani" },
241 [AST_EVENT_IE_CEL_CIDRDNIS] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDrdnis" },
242 [AST_EVENT_IE_CEL_CIDDNID] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDdnid" },
243 [AST_EVENT_IE_CEL_PEER] = { AST_EVENT_IE_PLTYPE_STR, "CELPeer" },
244 [AST_EVENT_IE_CEL_LINKEDID] = { AST_EVENT_IE_PLTYPE_STR, "CELLinkedID" },
245 [AST_EVENT_IE_CEL_PEERACCT] = { AST_EVENT_IE_PLTYPE_STR, "CELPeerAcct" },
246 [AST_EVENT_IE_CEL_EXTRA] = { AST_EVENT_IE_PLTYPE_STR, "CELExtra" },
247 [AST_EVENT_IE_SECURITY_EVENT] = { AST_EVENT_IE_PLTYPE_STR, "SecurityEvent" },
248 [AST_EVENT_IE_EVENT_VERSION] = { AST_EVENT_IE_PLTYPE_UINT, "EventVersion" },
249 [AST_EVENT_IE_SERVICE] = { AST_EVENT_IE_PLTYPE_STR, "Service" },
250 [AST_EVENT_IE_MODULE] = { AST_EVENT_IE_PLTYPE_STR, "Module" },
251 [AST_EVENT_IE_ACCOUNT_ID] = { AST_EVENT_IE_PLTYPE_STR, "AccountID" },
252 [AST_EVENT_IE_SESSION_ID] = { AST_EVENT_IE_PLTYPE_STR, "SessionID" },
253 [AST_EVENT_IE_SESSION_TV] = { AST_EVENT_IE_PLTYPE_STR, "SessionTV" },
254 [AST_EVENT_IE_ACL_NAME] = { AST_EVENT_IE_PLTYPE_STR, "ACLName" },
255 [AST_EVENT_IE_LOCAL_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "LocalAddress" },
256 [AST_EVENT_IE_REMOTE_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "RemoteAddress" },
257 [AST_EVENT_IE_EVENT_TV] = { AST_EVENT_IE_PLTYPE_STR, "EventTV" },
258 [AST_EVENT_IE_REQUEST_TYPE] = { AST_EVENT_IE_PLTYPE_STR, "RequestType" },
259 [AST_EVENT_IE_REQUEST_PARAMS] = { AST_EVENT_IE_PLTYPE_STR, "RequestParams" },
260 [AST_EVENT_IE_AUTH_METHOD] = { AST_EVENT_IE_PLTYPE_STR, "AuthMethod" },
261 [AST_EVENT_IE_SEVERITY] = { AST_EVENT_IE_PLTYPE_STR, "Severity" },
262 [AST_EVENT_IE_EXPECTED_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedAddress" },
263 [AST_EVENT_IE_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "Challenge" },
264 [AST_EVENT_IE_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "Response" },
265 [AST_EVENT_IE_EXPECTED_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedResponse" },
268 const char *ast_event_get_type_name(const struct ast_event *event)
270 enum ast_event_type type;
272 type = ast_event_get_type(event);
274 if (type < 0 || type >= ARRAY_LEN(event_names)) {
275 ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
279 return event_names[type];
282 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
286 for (i = 0; i < ARRAY_LEN(event_names); i++) {
287 if (ast_strlen_zero(event_names[i]) || strcasecmp(event_names[i], str)) {
298 const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
300 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
301 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
305 return ie_maps[ie_type].name;
308 enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
310 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
311 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
312 return AST_EVENT_IE_PLTYPE_UNKNOWN;
315 return ie_maps[ie_type].ie_pltype;
318 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
322 for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
323 if (strcasecmp(ie_maps[i].name, str)) {
334 size_t ast_event_get_size(const struct ast_event *event)
338 res = ntohs(event->event_len);
343 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
345 switch (ie_val->ie_pltype) {
346 case AST_EVENT_IE_PLTYPE_STR:
347 ast_free((char *) ie_val->payload.str);
349 case AST_EVENT_IE_PLTYPE_RAW:
350 ast_free(ie_val->payload.raw);
352 case AST_EVENT_IE_PLTYPE_UINT:
353 case AST_EVENT_IE_PLTYPE_BITFLAGS:
354 case AST_EVENT_IE_PLTYPE_EXISTS:
355 case AST_EVENT_IE_PLTYPE_UNKNOWN:
364 * \brief Check if an ie_val matches a subscription
366 * \param sub subscription to check against
367 * \param ie_val IE value to check
369 * \retval 0 not matched
370 * \retval non-zero matched
372 static int match_ie_val_to_sub(const struct ast_event_sub *sub, const struct ast_event_ie_val *ie_val)
374 const struct ast_event_ie_val *sub_ie_val;
377 AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
378 if (sub_ie_val->ie_type == ie_val->ie_type) {
384 /* This subscriber doesn't care about this IE, so consider
389 switch (ie_val->ie_pltype) {
390 case AST_EVENT_IE_PLTYPE_UINT:
391 res = (ie_val->payload.uint != sub_ie_val->payload.uint);
393 case AST_EVENT_IE_PLTYPE_BITFLAGS:
395 * If the subscriber has requested *any* of the bitflags we are providing,
398 res = !(ie_val->payload.uint & sub_ie_val->payload.uint);
400 case AST_EVENT_IE_PLTYPE_STR:
401 res = strcmp(ie_val->payload.str, sub_ie_val->payload.str);
403 case AST_EVENT_IE_PLTYPE_RAW:
404 res = memcmp(ie_val->payload.raw,
405 sub_ie_val->payload.raw, ie_val->raw_datalen);
407 case AST_EVENT_IE_PLTYPE_EXISTS:
408 case AST_EVENT_IE_PLTYPE_UNKNOWN:
415 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
418 enum ast_event_ie_type ie_type;
419 enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
420 struct ast_event_ie_val *ie_val;
421 struct ast_event_sub *sub;
422 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
423 const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
426 if (type >= AST_EVENT_TOTAL) {
427 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
432 for (ie_type = va_arg(ap, enum ast_event_ie_type);
433 ie_type != AST_EVENT_IE_END;
434 ie_type = va_arg(ap, enum ast_event_ie_type))
436 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
438 memset(ie_value, 0, sizeof(*ie_value));
439 ie_value->ie_type = ie_type;
440 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
441 switch (ie_value->ie_pltype) {
442 case AST_EVENT_IE_PLTYPE_UINT:
443 ie_value->payload.uint = va_arg(ap, uint32_t);
445 case AST_EVENT_IE_PLTYPE_BITFLAGS:
446 ie_value->payload.uint = va_arg(ap, uint32_t);
448 case AST_EVENT_IE_PLTYPE_STR:
449 ie_value->payload.str = va_arg(ap, const char *);
451 case AST_EVENT_IE_PLTYPE_RAW:
453 void *data = va_arg(ap, void *);
454 size_t datalen = va_arg(ap, size_t);
455 ie_value->payload.raw = alloca(datalen);
456 memcpy(ie_value->payload.raw, data, datalen);
457 ie_value->raw_datalen = datalen;
460 case AST_EVENT_IE_PLTYPE_UNKNOWN:
462 case AST_EVENT_IE_PLTYPE_EXISTS:
467 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
472 for (i = 0; i < ARRAY_LEN(event_types); i++) {
473 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
474 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
475 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
476 if (match_ie_val_to_sub(sub, ie_val)) {
482 /* Everything matched. */
486 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
492 return sub ? AST_EVENT_SUB_EXISTS : AST_EVENT_SUB_NONE;
497 * \brief Check if an ie_val matches an event
499 * \param event event to check against
500 * \param ie_val IE value to check
501 * \param event2 optional event, if specified, the value to compare against will be pulled
502 * from this event instead of from the ie_val structure. In this case, only the IE
503 * type and payload type will be pulled from ie_val.
505 * \retval 0 not matched
506 * \retval non-zero matched
508 static int match_ie_val(const struct ast_event *event,
509 const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
511 switch (ie_val->ie_pltype) {
512 case AST_EVENT_IE_PLTYPE_UINT:
514 uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
516 return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
519 case AST_EVENT_IE_PLTYPE_BITFLAGS:
521 uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
524 * If the subscriber has requested *any* of the bitflags that this event provides,
527 return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
530 case AST_EVENT_IE_PLTYPE_STR:
535 hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
536 if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
540 str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
541 if (str && !strcmp(str, ast_event_get_ie_str(event, ie_val->ie_type))) {
548 case AST_EVENT_IE_PLTYPE_RAW:
550 const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
551 uint16_t ie_payload_len = event2 ? ast_event_get_ie_raw_payload_len(event2, ie_val->ie_type) : ie_val->raw_datalen;
553 return (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_payload_len)) ? 1 : 0;
556 case AST_EVENT_IE_PLTYPE_EXISTS:
558 return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
561 case AST_EVENT_IE_PLTYPE_UNKNOWN:
568 static int dump_cache_cb(void *obj, void *arg, int flags)
570 const struct ast_event_ref *event_ref = obj;
571 const struct ast_event *event = event_ref->event;
572 const struct ast_event_sub *event_sub = arg;
573 struct ast_event_ie_val *ie_val = NULL;
575 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
576 if (!match_ie_val(event, ie_val, NULL)) {
582 /* All parameters were matched on this cache entry, so dump it */
583 event_sub->cb(event, event_sub->userdata);
589 /*! \brief Dump the event cache for the subscribed event type */
590 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
592 ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
593 dump_cache_cb, (void *) event_sub);
596 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
598 struct ast_event_ie_val *ie_val;
599 struct ast_event *event;
601 event = ast_event_new(AST_EVENT_SUB,
602 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
603 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
604 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
610 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
611 switch (ie_val->ie_pltype) {
612 case AST_EVENT_IE_PLTYPE_UNKNOWN:
614 case AST_EVENT_IE_PLTYPE_EXISTS:
615 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
617 case AST_EVENT_IE_PLTYPE_UINT:
618 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
620 case AST_EVENT_IE_PLTYPE_BITFLAGS:
621 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
623 case AST_EVENT_IE_PLTYPE_STR:
624 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
626 case AST_EVENT_IE_PLTYPE_RAW:
627 ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
637 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
638 void ast_event_report_subs(const struct ast_event_sub *event_sub)
640 struct ast_event *event;
641 struct ast_event_sub *sub;
642 enum ast_event_type event_type = -1;
643 struct ast_event_ie_val *ie_val;
645 if (event_sub->type != AST_EVENT_SUB)
648 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
649 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
650 event_type = ie_val->payload.uint;
655 if (event_type == -1)
658 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
659 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
660 if (event_sub == sub) {
664 event = gen_sub_event(sub);
670 event_sub->cb(event, event_sub->userdata);
672 ast_event_destroy(event);
674 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
677 struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
678 ast_event_cb_t cb, void *userdata)
680 struct ast_event_sub *sub;
682 if (type < 0 || type >= AST_EVENT_TOTAL) {
683 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
687 if (!(sub = ast_calloc(1, sizeof(*sub)))) {
693 sub->userdata = userdata;
694 sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
699 int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
700 enum ast_event_ie_type ie_type, uint32_t unsigned_int)
702 struct ast_event_ie_val *ie_val;
704 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
708 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
712 ie_val->ie_type = ie_type;
713 ie_val->payload.uint = unsigned_int;
714 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
716 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
721 int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
722 enum ast_event_ie_type ie_type, uint32_t flags)
724 struct ast_event_ie_val *ie_val;
726 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
730 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
734 ie_val->ie_type = ie_type;
735 ie_val->payload.uint = flags;
736 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
738 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
743 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
744 enum ast_event_ie_type ie_type)
746 struct ast_event_ie_val *ie_val;
748 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
752 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
756 ie_val->ie_type = ie_type;
757 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
759 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
764 int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
765 enum ast_event_ie_type ie_type, const char *str)
767 struct ast_event_ie_val *ie_val;
769 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
773 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
777 ie_val->ie_type = ie_type;
778 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
780 if (!(ie_val->payload.str = ast_strdup(str))) {
785 ie_val->payload.hash = ast_str_hash(str);
787 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
792 int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
793 enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
795 struct ast_event_ie_val *ie_val;
797 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
801 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
805 ie_val->ie_type = ie_type;
806 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
807 ie_val->raw_datalen = raw_datalen;
809 if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
814 memcpy(ie_val->payload.raw, data, raw_datalen);
816 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
821 int ast_event_sub_activate(struct ast_event_sub *sub)
823 if (ast_event_check_subscriber(AST_EVENT_SUB,
824 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
825 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
826 struct ast_event *event;
828 event = gen_sub_event(sub);
831 ast_event_queue(event);
835 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
836 AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
837 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
842 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
843 const char *description, void *userdata, ...)
846 enum ast_event_ie_type ie_type;
847 struct ast_event_sub *sub;
849 if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
853 ast_copy_string(sub->description, description, sizeof(sub->description));
855 va_start(ap, userdata);
856 for (ie_type = va_arg(ap, enum ast_event_ie_type);
857 ie_type != AST_EVENT_IE_END;
858 ie_type = va_arg(ap, enum ast_event_ie_type))
860 enum ast_event_ie_pltype ie_pltype;
862 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
865 case AST_EVENT_IE_PLTYPE_UNKNOWN:
867 case AST_EVENT_IE_PLTYPE_UINT:
869 uint32_t unsigned_int = va_arg(ap, uint32_t);
870 ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
873 case AST_EVENT_IE_PLTYPE_BITFLAGS:
875 uint32_t unsigned_int = va_arg(ap, uint32_t);
876 ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
879 case AST_EVENT_IE_PLTYPE_STR:
881 const char *str = va_arg(ap, const char *);
882 ast_event_sub_append_ie_str(sub, ie_type, str);
885 case AST_EVENT_IE_PLTYPE_RAW:
887 void *data = va_arg(ap, void *);
888 size_t data_len = va_arg(ap, size_t);
889 ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
892 case AST_EVENT_IE_PLTYPE_EXISTS:
893 ast_event_sub_append_ie_exists(sub, ie_type);
899 ast_event_sub_activate(sub);
904 void ast_event_sub_destroy(struct ast_event_sub *sub)
906 struct ast_event_ie_val *ie_val;
908 while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
909 ast_event_ie_val_destroy(ie_val);
915 const char *ast_event_subscriber_get_description(struct ast_event_sub *sub)
917 return sub ? sub->description : NULL;
920 struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
922 struct ast_event *event;
924 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
925 AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
926 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
928 if (ast_event_check_subscriber(AST_EVENT_UNSUB,
929 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
930 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
932 event = ast_event_new(AST_EVENT_UNSUB,
933 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
934 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
935 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
939 ast_event_queue(event);
943 ast_event_sub_destroy(sub);
948 int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
952 iterator->event_len = ast_event_get_size(event);
953 iterator->event = event;
954 if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
955 iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
964 int ast_event_iterator_next(struct ast_event_iterator *iterator)
966 iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
967 return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
970 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
972 return ntohs(iterator->ie->ie_type);
975 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
977 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
980 uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
982 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
985 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
987 const struct ast_event_ie_str_payload *str_payload;
989 str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
991 return str_payload ? str_payload->str : NULL;
994 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
996 return iterator->ie->ie_payload;
999 uint16_t ast_event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
1001 return ntohs(iterator->ie->ie_payload_len);
1004 enum ast_event_type ast_event_get_type(const struct ast_event *event)
1006 return ntohs(event->type);
1009 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
1011 const uint32_t *ie_val;
1013 ie_val = ast_event_get_ie_raw(event, ie_type);
1015 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1018 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
1020 const uint32_t *ie_val;
1022 ie_val = ast_event_get_ie_raw(event, ie_type);
1024 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1027 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
1029 const struct ast_event_ie_str_payload *str_payload;
1031 str_payload = ast_event_get_ie_raw(event, ie_type);
1033 return str_payload ? str_payload->hash : 0;
1036 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
1038 const struct ast_event_ie_str_payload *str_payload;
1040 str_payload = ast_event_get_ie_raw(event, ie_type);
1042 return str_payload ? str_payload->str : NULL;
1045 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
1047 struct ast_event_iterator iterator;
1050 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1051 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1052 return ast_event_iterator_get_ie_raw(&iterator);
1059 uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type)
1061 struct ast_event_iterator iterator;
1064 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1065 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1066 return ast_event_iterator_get_ie_raw_payload_len(&iterator);
1073 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
1076 struct ast_event_ie_str_payload *str_payload;
1079 payload_len = sizeof(*str_payload) + strlen(str);
1080 str_payload = alloca(payload_len);
1082 strcpy(str_payload->str, str);
1083 str_payload->hash = ast_str_hash(str);
1085 return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
1088 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
1092 return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
1095 int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
1098 flags = htonl(flags);
1099 return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
1102 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
1103 const void *data, size_t data_len)
1105 struct ast_event_ie *ie;
1106 unsigned int extra_len;
1109 event_len = ntohs((*event)->event_len);
1110 extra_len = sizeof(*ie) + data_len;
1112 if (!(*event = ast_realloc(*event, event_len + extra_len))) {
1116 ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
1117 ie->ie_type = htons(ie_type);
1118 ie->ie_payload_len = htons(data_len);
1119 memcpy(ie->ie_payload, data, data_len);
1121 (*event)->event_len = htons(event_len + extra_len);
1126 struct ast_event *ast_event_new(enum ast_event_type type, ...)
1129 struct ast_event *event;
1130 enum ast_event_ie_type ie_type;
1131 struct ast_event_ie_val *ie_val;
1133 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
1136 if (type >= AST_EVENT_TOTAL) {
1137 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
1138 "type '%d'!\n", type);
1143 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1144 ie_type != AST_EVENT_IE_END;
1145 ie_type = va_arg(ap, enum ast_event_ie_type))
1147 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
1149 memset(ie_value, 0, sizeof(*ie_value));
1150 ie_value->ie_type = ie_type;
1151 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1152 switch (ie_value->ie_pltype) {
1153 case AST_EVENT_IE_PLTYPE_UINT:
1154 ie_value->payload.uint = va_arg(ap, uint32_t);
1156 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1157 ie_value->payload.uint = va_arg(ap, uint32_t);
1159 case AST_EVENT_IE_PLTYPE_STR:
1160 ie_value->payload.str = va_arg(ap, const char *);
1162 case AST_EVENT_IE_PLTYPE_RAW:
1164 void *data = va_arg(ap, void *);
1165 size_t datalen = va_arg(ap, size_t);
1166 ie_value->payload.raw = alloca(datalen);
1167 memcpy(ie_value->payload.raw, data, datalen);
1168 ie_value->raw_datalen = datalen;
1171 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1174 case AST_EVENT_IE_PLTYPE_EXISTS:
1179 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
1185 if (!(event = ast_calloc(1, sizeof(*event)))) {
1189 event->type = htons(type);
1190 event->event_len = htons(sizeof(*event));
1192 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
1193 switch (ie_val->ie_pltype) {
1194 case AST_EVENT_IE_PLTYPE_STR:
1195 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
1197 case AST_EVENT_IE_PLTYPE_UINT:
1198 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
1200 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1201 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
1203 case AST_EVENT_IE_PLTYPE_RAW:
1204 ast_event_append_ie_raw(&event, ie_val->ie_type,
1205 ie_val->payload.raw, ie_val->raw_datalen);
1207 case AST_EVENT_IE_PLTYPE_EXISTS:
1208 ast_log(LOG_WARNING, "PLTYPE_EXISTS unsupported in event_new\n");
1210 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1211 ast_log(LOG_WARNING, "PLTYPE_UNKNOWN passed as an IE type "
1212 "for a new event\n");
1221 if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
1222 /* If the event is originating on this server, add the server's
1223 * entity ID to the event. */
1224 ast_event_append_eid(&event);
1230 int ast_event_append_eid(struct ast_event **event)
1232 return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
1233 &ast_eid_default, sizeof(ast_eid_default));
1236 void ast_event_destroy(struct ast_event *event)
1241 static void ast_event_ref_destroy(void *obj)
1243 struct ast_event_ref *event_ref = obj;
1245 ast_event_destroy(event_ref->event);
1248 static struct ast_event *ast_event_dup(const struct ast_event *event)
1250 struct ast_event *dup_event;
1253 event_len = ast_event_get_size(event);
1255 if (!(dup_event = ast_calloc(1, event_len))) {
1259 memcpy(dup_event, event, event_len);
1264 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
1267 enum ast_event_ie_type ie_type;
1268 struct ast_event *dup_event = NULL;
1269 struct ast_event_ref *cached_event_ref;
1270 struct ast_event *cache_arg_event;
1271 struct ast_event_ref tmp_event_ref = {
1274 struct ao2_container *container = NULL;
1276 if (type >= AST_EVENT_TOTAL) {
1277 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
1281 if (!(container = ast_event_cache[type].container)) {
1282 ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
1286 if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
1291 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1292 ie_type != AST_EVENT_IE_END;
1293 ie_type = va_arg(ap, enum ast_event_ie_type))
1295 enum ast_event_ie_pltype ie_pltype;
1297 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1299 switch (ie_pltype) {
1300 case AST_EVENT_IE_PLTYPE_UINT:
1301 ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1303 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1304 ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1306 case AST_EVENT_IE_PLTYPE_STR:
1307 ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
1309 case AST_EVENT_IE_PLTYPE_RAW:
1311 void *data = va_arg(ap, void *);
1312 size_t datalen = va_arg(ap, size_t);
1313 ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
1315 case AST_EVENT_IE_PLTYPE_EXISTS:
1316 ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
1318 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1324 tmp_event_ref.event = cache_arg_event;
1326 cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
1328 ast_event_destroy(cache_arg_event);
1329 cache_arg_event = NULL;
1331 if (cached_event_ref) {
1332 dup_event = ast_event_dup(cached_event_ref->event);
1333 ao2_ref(cached_event_ref, -1);
1334 cached_event_ref = NULL;
1340 static struct ast_event_ref *alloc_event_ref(void)
1342 return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
1345 /*! \brief Duplicate an event and add it to the cache
1346 * \note This assumes this index in to the cache is locked */
1347 static int ast_event_dup_and_cache(const struct ast_event *event)
1349 struct ast_event *dup_event;
1350 struct ast_event_ref *event_ref;
1352 if (!(dup_event = ast_event_dup(event))) {
1356 if (!(event_ref = alloc_event_ref())) {
1357 ast_event_destroy(dup_event);
1361 event_ref->event = dup_event;
1363 ao2_link(ast_event_cache[ast_event_get_type(event)].container, event_ref);
1365 ao2_ref(event_ref, -1);
1370 int ast_event_queue_and_cache(struct ast_event *event)
1372 struct ao2_container *container;
1373 struct ast_event_ref tmp_event_ref = {
1378 if (!(container = ast_event_cache[ast_event_get_type(event)].container)) {
1379 ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
1383 /* Remove matches from the cache */
1384 ao2_callback(container, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
1385 ast_event_cmp, &tmp_event_ref);
1387 res = ast_event_dup_and_cache(event);
1390 return ast_event_queue(event) ? -1 : res;
1393 static int handle_event(void *data)
1395 struct ast_event_ref *event_ref = data;
1396 struct ast_event_sub *sub;
1397 const enum ast_event_type event_types[] = {
1398 ntohs(event_ref->event->type),
1403 for (i = 0; i < ARRAY_LEN(event_types); i++) {
1404 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
1405 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
1406 struct ast_event_ie_val *ie_val;
1407 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
1408 if (!match_ie_val(event_ref->event, ie_val, NULL)) {
1415 sub->cb(event_ref->event, sub->userdata);
1417 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
1420 ao2_ref(event_ref, -1);
1425 int ast_event_queue(struct ast_event *event)
1427 struct ast_event_ref *event_ref;
1428 uint16_t host_event_type;
1430 host_event_type = ntohs(event->type);
1433 if (host_event_type >= AST_EVENT_TOTAL) {
1434 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
1435 "type '%d'!\n", host_event_type);
1439 /* If nobody has subscribed to this event type, throw it away now */
1440 if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
1441 == AST_EVENT_SUB_NONE) {
1442 ast_event_destroy(event);
1446 if (!(event_ref = alloc_event_ref())) {
1450 event_ref->event = event;
1452 return ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
1455 static int ast_event_hash_mwi(const void *obj, const int flags)
1457 const struct ast_event *event = obj;
1458 const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
1459 const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
1461 return ast_str_hash_add(context, ast_str_hash(mailbox));
1466 * \brief Hash function for AST_EVENT_DEVICE_STATE
1468 * \param[in] obj an ast_event
1469 * \param[in] flags unused
1471 * \return hash value
1473 static int ast_event_hash_devstate(const void *obj, const int flags)
1475 const struct ast_event *event = obj;
1477 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1482 * \brief Hash function for AST_EVENT_DEVICE_STATE_CHANGE
1484 * \param[in] obj an ast_event
1485 * \param[in] flags unused
1487 * \return hash value
1489 static int ast_event_hash_devstate_change(const void *obj, const int flags)
1491 const struct ast_event *event = obj;
1493 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1496 static int ast_event_hash(const void *obj, const int flags)
1498 const struct ast_event_ref *event_ref;
1499 const struct ast_event *event;
1500 ao2_hash_fn *hash_fn;
1503 event = event_ref->event;
1505 if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
1509 return hash_fn(event, flags);
1514 * \brief Compare two events
1516 * \param[in] obj the first event, as an ast_event_ref
1517 * \param[in] arg the second event, as an ast_event_ref
1518 * \param[in] flags unused
1520 * \pre Both events must be the same type.
1521 * \pre The event type must be declared as a cached event type in ast_event_cache
1523 * \details This function takes two events, and determines if they are considered
1524 * equivalent. The values of information elements specified in the cache arguments
1525 * for the event type are used to determine if the events are equivalent.
1527 * \retval 0 No match
1528 * \retval CMP_MATCH The events are considered equivalent based on the cache arguments
1530 static int ast_event_cmp(void *obj, void *arg, int flags)
1532 struct ast_event_ref *event_ref, *event_ref2;
1533 struct ast_event *event, *event2;
1534 int res = CMP_MATCH;
1536 enum ast_event_ie_type *cache_args;
1539 event = event_ref->event;
1542 event2 = event_ref2->event;
1544 cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
1546 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1547 struct ast_event_ie_val ie_val = {
1548 .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
1549 .ie_type = cache_args[i],
1552 if (!match_ie_val(event, &ie_val, event2)) {
1561 static void dump_raw_ie(struct ast_event_iterator *i, struct ast_cli_args *a)
1564 enum ast_event_ie_type ie_type;
1565 const char *ie_type_name;
1567 ie_type = ast_event_iterator_get_ie_type(i);
1568 ie_type_name = ast_event_get_ie_type_name(ie_type);
1571 case AST_EVENT_IE_EID:
1572 ast_eid_to_str(eid_buf, sizeof(eid_buf), ast_event_iterator_get_ie_raw(i));
1573 ast_cli(a->fd, "%.30s: %s\n", ie_type_name, eid_buf);
1576 ast_cli(a->fd, "%s\n", ie_type_name);
1581 static int event_dump_cli(void *obj, void *arg, int flags)
1583 const struct ast_event_ref *event_ref = obj;
1584 const struct ast_event *event = event_ref->event;
1585 struct ast_cli_args *a = arg;
1586 struct ast_event_iterator i;
1588 if (ast_event_iterator_init(&i, event)) {
1589 ast_cli(a->fd, "Failed to initialize event iterator. :-(\n");
1593 ast_cli(a->fd, "Event: %s\n", ast_event_get_type_name(event));
1596 enum ast_event_ie_type ie_type;
1597 enum ast_event_ie_pltype ie_pltype;
1598 const char *ie_type_name;
1600 ie_type = ast_event_iterator_get_ie_type(&i);
1601 ie_type_name = ast_event_get_ie_type_name(ie_type);
1602 ie_pltype = ast_event_get_ie_pltype(ie_type);
1604 switch (ie_pltype) {
1605 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1606 case AST_EVENT_IE_PLTYPE_EXISTS:
1607 ast_cli(a->fd, "%s\n", ie_type_name);
1609 case AST_EVENT_IE_PLTYPE_STR:
1610 ast_cli(a->fd, "%.30s: %s\n", ie_type_name,
1611 ast_event_iterator_get_ie_str(&i));
1613 case AST_EVENT_IE_PLTYPE_UINT:
1614 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1615 ast_event_iterator_get_ie_uint(&i));
1617 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1618 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1619 ast_event_iterator_get_ie_bitflags(&i));
1621 case AST_EVENT_IE_PLTYPE_RAW:
1625 } while (!ast_event_iterator_next(&i));
1627 ast_cli(a->fd, "\n");
1632 static char *event_dump_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1634 enum ast_event_type event_type;
1635 enum ast_event_ie_type *cache_args;
1640 e->command = "event dump cache";
1642 "Usage: event dump cache <event type>\n"
1643 " Dump all of the cached events for the given event type.\n"
1644 " This is primarily intended for debugging.\n";
1648 return ast_cli_complete(a->word, cached_event_types, a->n);
1655 if (a->argc != e->args + 1) {
1656 return CLI_SHOWUSAGE;
1659 if (ast_event_str_to_event_type(a->argv[e->args], &event_type)) {
1660 ast_cli(a->fd, "Invalid cached event type: '%s'\n", a->argv[e->args]);
1661 return CLI_SHOWUSAGE;
1664 if (!ast_event_cache[event_type].container) {
1665 ast_cli(a->fd, "Event type '%s' has no cache.\n", a->argv[e->args]);
1669 ast_cli(a->fd, "Event Type: %s\n", a->argv[e->args]);
1670 ast_cli(a->fd, "Cache Unique Keys:\n");
1671 cache_args = ast_event_cache[event_type].cache_args;
1672 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1673 ast_cli(a->fd, "--> %s\n", ast_event_get_ie_type_name(cache_args[i]));
1676 ast_cli(a->fd, "\n--- Begin Cache Dump ---\n\n");
1677 ao2_callback(ast_event_cache[event_type].container, OBJ_NODATA, event_dump_cli, a);
1678 ast_cli(a->fd, "--- End Cache Dump ---\n\n");
1683 static struct ast_cli_entry event_cli[] = {
1684 AST_CLI_DEFINE(event_dump_cache, "Dump the internal event cache (for debugging)"),
1687 int ast_event_init(void)
1691 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1692 AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
1695 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1696 if (!ast_event_cache[i].hash_fn) {
1697 /* This event type is not cached. */
1701 if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
1702 ast_event_hash, ast_event_cmp))) {
1707 if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
1711 ast_cli_register_multiple(event_cli, ARRAY_LEN(event_cli));