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"
42 static struct ast_taskprocessor *event_dispatcher;
45 * \brief An event information element
47 * \note The format of this structure is important. Since these events may
48 * be sent directly over a network, changing this structure will break
49 * compatibility with older versions. However, at this point, this code
50 * has not made it into a release, so it is still fair game for change.
53 enum ast_event_ie_type ie_type:16;
54 /*! Total length of the IE payload */
55 uint16_t ie_payload_len;
56 unsigned char ie_payload[0];
57 } __attribute__((packed));
60 * \brief The payload for a string information element
62 struct ast_event_ie_str_payload {
63 /*! \brief A hash calculated with ast_str_hash(), to speed up comparisons */
65 /*! \brief The actual string, null terminated */
67 } __attribute__((packed));
72 * An ast_event consists of an event header (this structure), and zero or
73 * more information elements defined by ast_event_ie.
75 * \note The format of this structure is important. Since these events may
76 * be sent directly over a network, changing this structure will break
77 * compatibility with older versions. However, at this point, this code
78 * has not made it into a release, so it is still fair game for change.
82 enum ast_event_type type:16;
83 /*! Total length of the event */
84 uint16_t event_len:16;
85 /*! The data payload of the event, made up of information elements */
86 unsigned char payload[0];
87 } __attribute__((packed));
91 * \brief A holder for an event
93 * \details This struct used to have more of a purpose than it does now.
94 * It is used to hold events in the event cache. It can be completely removed
95 * if one of these two things is done:
96 * - ast_event gets changed such that it never has to be realloc()d
97 * - astobj2 is updated so that you can realloc() an astobj2 object
99 struct ast_event_ref {
100 struct ast_event *event;
103 struct ast_event_ie_val {
104 AST_LIST_ENTRY(ast_event_ie_val) entry;
105 enum ast_event_ie_type ie_type;
106 enum ast_event_ie_pltype ie_pltype;
118 /*! \brief Event subscription */
119 struct ast_event_sub {
120 enum ast_event_type type;
122 char description[64];
125 AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
126 AST_RWDLLIST_ENTRY(ast_event_sub) entry;
129 static uint32_t sub_uniqueid;
131 /*! \brief Event subscriptions
132 * The event subscribers are indexed by which event they are subscribed to */
133 static AST_RWDLLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
135 static int ast_event_cmp(void *obj, void *arg, int flags);
136 static int ast_event_hash_mwi(const void *obj, const int flags);
137 static int ast_event_hash_devstate(const void *obj, const int flags);
138 static int ast_event_hash_devstate_change(const void *obj, const int flags);
141 #define NUM_CACHE_BUCKETS 17
143 #define NUM_CACHE_BUCKETS 563
146 #define MAX_CACHE_ARGS 8
149 * \brief Event types that are kept in the cache.
153 * \brief Container of cached events
155 * \details This gets allocated in ast_event_init() when Asterisk starts
156 * for the event types declared as using the cache.
158 struct ao2_container *container;
159 /*! \brief Event type specific hash function */
160 ao2_hash_fn *hash_fn;
162 * \brief Information Elements used for caching
164 * \details This array is the set of information elements that will be unique
165 * among all events in the cache for this event type. When a new event gets
166 * cached, a previous event with the same values for these information elements
169 enum ast_event_ie_type cache_args[MAX_CACHE_ARGS];
170 } ast_event_cache[AST_EVENT_TOTAL] = {
172 .hash_fn = ast_event_hash_mwi,
173 .cache_args = { AST_EVENT_IE_MAILBOX, AST_EVENT_IE_CONTEXT },
175 [AST_EVENT_DEVICE_STATE] = {
176 .hash_fn = ast_event_hash_devstate,
177 .cache_args = { AST_EVENT_IE_DEVICE, },
179 [AST_EVENT_DEVICE_STATE_CHANGE] = {
180 .hash_fn = ast_event_hash_devstate_change,
181 .cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
188 static const char * const event_names[AST_EVENT_TOTAL] = {
189 [AST_EVENT_CUSTOM] = "Custom",
190 [AST_EVENT_MWI] = "MWI",
191 [AST_EVENT_SUB] = "Subscription",
192 [AST_EVENT_UNSUB] = "Unsubscription",
193 [AST_EVENT_DEVICE_STATE] = "DeviceState",
194 [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
195 [AST_EVENT_CEL] = "CEL",
196 [AST_EVENT_SECURITY] = "Security",
200 * \brief IE payload types and names
202 static const struct ie_map {
203 enum ast_event_ie_pltype ie_pltype;
205 } ie_maps[AST_EVENT_IE_TOTAL] = {
206 [AST_EVENT_IE_NEWMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
207 [AST_EVENT_IE_OLDMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
208 [AST_EVENT_IE_MAILBOX] = { AST_EVENT_IE_PLTYPE_STR, "Mailbox" },
209 [AST_EVENT_IE_UNIQUEID] = { AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
210 [AST_EVENT_IE_EVENTTYPE] = { AST_EVENT_IE_PLTYPE_UINT, "EventType" },
211 [AST_EVENT_IE_EXISTS] = { AST_EVENT_IE_PLTYPE_UINT, "Exists" },
212 [AST_EVENT_IE_DEVICE] = { AST_EVENT_IE_PLTYPE_STR, "Device" },
213 [AST_EVENT_IE_STATE] = { AST_EVENT_IE_PLTYPE_UINT, "State" },
214 [AST_EVENT_IE_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "Context" },
215 [AST_EVENT_IE_EID] = { AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
216 [AST_EVENT_IE_CEL_EVENT_TYPE] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventType" },
217 [AST_EVENT_IE_CEL_EVENT_TIME] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTime" },
218 [AST_EVENT_IE_CEL_EVENT_TIME_USEC] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTimeUSec" },
219 [AST_EVENT_IE_CEL_USEREVENT_NAME] = { AST_EVENT_IE_PLTYPE_UINT, "CELUserEventName" },
220 [AST_EVENT_IE_CEL_CIDNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDName" },
221 [AST_EVENT_IE_CEL_CIDNUM] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDNum" },
222 [AST_EVENT_IE_CEL_EXTEN] = { AST_EVENT_IE_PLTYPE_STR, "CELExten" },
223 [AST_EVENT_IE_CEL_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "CELContext" },
224 [AST_EVENT_IE_CEL_CHANNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELChanName" },
225 [AST_EVENT_IE_CEL_APPNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELAppName" },
226 [AST_EVENT_IE_CEL_APPDATA] = { AST_EVENT_IE_PLTYPE_STR, "CELAppData" },
227 [AST_EVENT_IE_CEL_AMAFLAGS] = { AST_EVENT_IE_PLTYPE_STR, "CELAMAFlags" },
228 [AST_EVENT_IE_CEL_ACCTCODE] = { AST_EVENT_IE_PLTYPE_UINT, "CELAcctCode" },
229 [AST_EVENT_IE_CEL_UNIQUEID] = { AST_EVENT_IE_PLTYPE_STR, "CELUniqueID" },
230 [AST_EVENT_IE_CEL_USERFIELD] = { AST_EVENT_IE_PLTYPE_STR, "CELUserField" },
231 [AST_EVENT_IE_CEL_CIDANI] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDani" },
232 [AST_EVENT_IE_CEL_CIDRDNIS] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDrdnis" },
233 [AST_EVENT_IE_CEL_CIDDNID] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDdnid" },
234 [AST_EVENT_IE_CEL_PEER] = { AST_EVENT_IE_PLTYPE_STR, "CELPeer" },
235 [AST_EVENT_IE_CEL_LINKEDID] = { AST_EVENT_IE_PLTYPE_STR, "CELLinkedID" },
236 [AST_EVENT_IE_CEL_PEERACCT] = { AST_EVENT_IE_PLTYPE_STR, "CELPeerAcct" },
237 [AST_EVENT_IE_CEL_EXTRA] = { AST_EVENT_IE_PLTYPE_STR, "CELExtra" },
238 [AST_EVENT_IE_SECURITY_EVENT] = { AST_EVENT_IE_PLTYPE_STR, "SecurityEvent" },
239 [AST_EVENT_IE_EVENT_VERSION] = { AST_EVENT_IE_PLTYPE_UINT, "EventVersion" },
240 [AST_EVENT_IE_SERVICE] = { AST_EVENT_IE_PLTYPE_STR, "Service" },
241 [AST_EVENT_IE_MODULE] = { AST_EVENT_IE_PLTYPE_STR, "Module" },
242 [AST_EVENT_IE_ACCOUNT_ID] = { AST_EVENT_IE_PLTYPE_STR, "AccountID" },
243 [AST_EVENT_IE_SESSION_ID] = { AST_EVENT_IE_PLTYPE_STR, "SessionID" },
244 [AST_EVENT_IE_SESSION_TV] = { AST_EVENT_IE_PLTYPE_STR, "SessionTV" },
245 [AST_EVENT_IE_ACL_NAME] = { AST_EVENT_IE_PLTYPE_STR, "ACLName" },
246 [AST_EVENT_IE_LOCAL_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "LocalAddress" },
247 [AST_EVENT_IE_REMOTE_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "RemoteAddress" },
248 [AST_EVENT_IE_EVENT_TV] = { AST_EVENT_IE_PLTYPE_STR, "EventTV" },
249 [AST_EVENT_IE_REQUEST_TYPE] = { AST_EVENT_IE_PLTYPE_STR, "RequestType" },
250 [AST_EVENT_IE_REQUEST_PARAMS] = { AST_EVENT_IE_PLTYPE_STR, "RequestParams" },
251 [AST_EVENT_IE_AUTH_METHOD] = { AST_EVENT_IE_PLTYPE_STR, "AuthMethod" },
252 [AST_EVENT_IE_SEVERITY] = { AST_EVENT_IE_PLTYPE_STR, "Severity" },
253 [AST_EVENT_IE_EXPECTED_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedAddress" },
254 [AST_EVENT_IE_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "Challenge" },
255 [AST_EVENT_IE_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "Response" },
256 [AST_EVENT_IE_EXPECTED_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedResponse" },
259 const char *ast_event_get_type_name(const struct ast_event *event)
261 enum ast_event_type type;
263 type = ast_event_get_type(event);
265 if (type < 0 || type >= ARRAY_LEN(event_names)) {
266 ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
270 return event_names[type];
273 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
277 for (i = 0; i < ARRAY_LEN(event_names); i++) {
278 if (strcasecmp(event_names[i], str)) {
289 const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
291 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
292 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
296 return ie_maps[ie_type].name;
299 enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
301 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
302 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
303 return AST_EVENT_IE_PLTYPE_UNKNOWN;
306 return ie_maps[ie_type].ie_pltype;
309 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
313 for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
314 if (strcasecmp(ie_maps[i].name, str)) {
325 size_t ast_event_get_size(const struct ast_event *event)
329 res = ntohs(event->event_len);
334 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
336 switch (ie_val->ie_pltype) {
337 case AST_EVENT_IE_PLTYPE_STR:
338 ast_free((char *) ie_val->payload.str);
340 case AST_EVENT_IE_PLTYPE_RAW:
341 ast_free(ie_val->payload.raw);
343 case AST_EVENT_IE_PLTYPE_UINT:
344 case AST_EVENT_IE_PLTYPE_BITFLAGS:
345 case AST_EVENT_IE_PLTYPE_EXISTS:
346 case AST_EVENT_IE_PLTYPE_UNKNOWN:
353 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
356 enum ast_event_ie_type ie_type;
357 enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
358 struct ast_event_ie_val *ie_val, *sub_ie_val;
359 struct ast_event_sub *sub;
360 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
362 if (type >= AST_EVENT_TOTAL) {
363 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
368 for (ie_type = va_arg(ap, enum ast_event_ie_type);
369 ie_type != AST_EVENT_IE_END;
370 ie_type = va_arg(ap, enum ast_event_ie_type))
372 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
374 memset(ie_value, 0, sizeof(*ie_value));
375 ie_value->ie_type = ie_type;
376 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
377 switch (ie_value->ie_pltype) {
378 case AST_EVENT_IE_PLTYPE_UINT:
379 ie_value->payload.uint = va_arg(ap, uint32_t);
381 case AST_EVENT_IE_PLTYPE_BITFLAGS:
382 ie_value->payload.uint = va_arg(ap, uint32_t);
384 case AST_EVENT_IE_PLTYPE_STR:
385 ie_value->payload.str = va_arg(ap, const char *);
387 case AST_EVENT_IE_PLTYPE_RAW:
389 void *data = va_arg(ap, void *);
390 size_t datalen = va_arg(ap, size_t);
391 ie_value->payload.raw = alloca(datalen);
392 memcpy(ie_value->payload.raw, data, datalen);
393 ie_value->raw_datalen = datalen;
396 case AST_EVENT_IE_PLTYPE_UNKNOWN:
398 case AST_EVENT_IE_PLTYPE_EXISTS:
403 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
408 AST_RWDLLIST_RDLOCK(&ast_event_subs[type]);
409 AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
410 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
413 AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
414 if (sub_ie_val->ie_type == ie_val->ie_type) {
420 /* This subscriber doesn't care about this IE, so consider
425 switch (ie_val->ie_pltype) {
426 case AST_EVENT_IE_PLTYPE_UINT:
427 break_out = (ie_val->payload.uint != sub_ie_val->payload.uint);
429 case AST_EVENT_IE_PLTYPE_BITFLAGS:
430 /* if the subscriber has requested *any* of the bitflags we are providing,
433 break_out = (ie_val->payload.uint & sub_ie_val->payload.uint);
435 case AST_EVENT_IE_PLTYPE_STR:
436 break_out = strcmp(ie_val->payload.str, sub_ie_val->payload.str);
438 case AST_EVENT_IE_PLTYPE_RAW:
439 break_out = memcmp(ie_val->payload.raw,
440 sub_ie_val->payload.raw, ie_val->raw_datalen);
442 case AST_EVENT_IE_PLTYPE_EXISTS:
443 /* The subscriber doesn't actually care what the value is */
446 case AST_EVENT_IE_PLTYPE_UNKNOWN:
456 /* Everything matched */
460 AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
463 /* All parameters were matched */
464 return AST_EVENT_SUB_EXISTS;
467 AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
468 if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL])) {
469 res = AST_EVENT_SUB_EXISTS;
471 AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
476 static int match_ie_val(const struct ast_event *event,
477 const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
479 switch (ie_val->ie_pltype) {
480 case AST_EVENT_IE_PLTYPE_UINT:
482 uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
484 return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
487 case AST_EVENT_IE_PLTYPE_BITFLAGS:
489 uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
491 /* if the subscriber has requested *any* of the bitflags that this event provides,
494 return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
497 case AST_EVENT_IE_PLTYPE_STR:
502 hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
503 if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
507 str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
508 if (str && !strcmp(str, ast_event_get_ie_str(event, ie_val->ie_type))) {
515 case AST_EVENT_IE_PLTYPE_RAW:
517 const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
519 return (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_val->raw_datalen)) ? 1 : 0;
522 case AST_EVENT_IE_PLTYPE_EXISTS:
524 return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
527 case AST_EVENT_IE_PLTYPE_UNKNOWN:
534 static int dump_cache_cb(void *obj, void *arg, int flags)
536 const struct ast_event_ref *event_ref = obj;
537 const struct ast_event *event = event_ref->event;
538 const struct ast_event_sub *event_sub = arg;
539 struct ast_event_ie_val *ie_val = NULL;
541 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
542 if (!match_ie_val(event, ie_val, NULL)) {
548 /* All parameters were matched on this cache entry, so dump it */
549 event_sub->cb(event, event_sub->userdata);
555 /*! \brief Dump the event cache for the subscribed event type */
556 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
558 ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
559 dump_cache_cb, (void *) event_sub);
562 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
564 struct ast_event_ie_val *ie_val;
565 struct ast_event *event;
567 event = ast_event_new(AST_EVENT_SUB,
568 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
569 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
570 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
576 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
577 switch (ie_val->ie_pltype) {
578 case AST_EVENT_IE_PLTYPE_UNKNOWN:
580 case AST_EVENT_IE_PLTYPE_EXISTS:
581 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
583 case AST_EVENT_IE_PLTYPE_UINT:
584 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
586 case AST_EVENT_IE_PLTYPE_BITFLAGS:
587 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
589 case AST_EVENT_IE_PLTYPE_STR:
590 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
592 case AST_EVENT_IE_PLTYPE_RAW:
593 ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
603 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
604 void ast_event_report_subs(const struct ast_event_sub *event_sub)
606 struct ast_event *event;
607 struct ast_event_sub *sub;
608 enum ast_event_type event_type = -1;
609 struct ast_event_ie_val *ie_val;
611 if (event_sub->type != AST_EVENT_SUB)
614 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
615 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
616 event_type = ie_val->payload.uint;
621 if (event_type == -1)
624 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
625 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
626 if (event_sub == sub) {
630 event = gen_sub_event(sub);
636 event_sub->cb(event, event_sub->userdata);
638 ast_event_destroy(event);
640 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
643 struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
644 ast_event_cb_t cb, void *userdata)
646 struct ast_event_sub *sub;
648 if (type < 0 || type >= AST_EVENT_TOTAL) {
649 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
653 if (!(sub = ast_calloc(1, sizeof(*sub)))) {
659 sub->userdata = userdata;
660 sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
665 int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
666 enum ast_event_ie_type ie_type, uint32_t unsigned_int)
668 struct ast_event_ie_val *ie_val;
670 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
674 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
678 ie_val->ie_type = ie_type;
679 ie_val->payload.uint = unsigned_int;
680 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
682 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
687 int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
688 enum ast_event_ie_type ie_type, uint32_t flags)
690 struct ast_event_ie_val *ie_val;
692 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
696 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
700 ie_val->ie_type = ie_type;
701 ie_val->payload.uint = flags;
702 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
704 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
709 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
710 enum ast_event_ie_type ie_type)
712 struct ast_event_ie_val *ie_val;
714 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
718 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
722 ie_val->ie_type = ie_type;
723 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
725 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
730 int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
731 enum ast_event_ie_type ie_type, const char *str)
733 struct ast_event_ie_val *ie_val;
735 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
739 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
743 ie_val->ie_type = ie_type;
744 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
746 if (!(ie_val->payload.str = ast_strdup(str))) {
751 ie_val->payload.hash = ast_str_hash(str);
753 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
758 int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
759 enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
761 struct ast_event_ie_val *ie_val;
763 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
767 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
771 ie_val->ie_type = ie_type;
772 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
773 ie_val->raw_datalen = raw_datalen;
775 if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
780 memcpy(ie_val->payload.raw, data, raw_datalen);
782 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
787 int ast_event_sub_activate(struct ast_event_sub *sub)
789 if (ast_event_check_subscriber(AST_EVENT_SUB,
790 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
791 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
792 struct ast_event *event;
794 event = gen_sub_event(sub);
797 ast_event_queue(event);
801 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
802 AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
803 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
808 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
809 char *description, void *userdata, ...)
812 enum ast_event_ie_type ie_type;
813 struct ast_event_sub *sub;
815 if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
819 ast_copy_string(sub->description, description, sizeof(sub->description));
821 va_start(ap, userdata);
822 for (ie_type = va_arg(ap, enum ast_event_ie_type);
823 ie_type != AST_EVENT_IE_END;
824 ie_type = va_arg(ap, enum ast_event_ie_type))
826 enum ast_event_ie_pltype ie_pltype;
828 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
831 case AST_EVENT_IE_PLTYPE_UNKNOWN:
833 case AST_EVENT_IE_PLTYPE_UINT:
835 uint32_t unsigned_int = va_arg(ap, uint32_t);
836 ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
839 case AST_EVENT_IE_PLTYPE_BITFLAGS:
841 uint32_t unsigned_int = va_arg(ap, uint32_t);
842 ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
845 case AST_EVENT_IE_PLTYPE_STR:
847 const char *str = va_arg(ap, const char *);
848 ast_event_sub_append_ie_str(sub, ie_type, str);
851 case AST_EVENT_IE_PLTYPE_RAW:
853 void *data = va_arg(ap, void *);
854 size_t data_len = va_arg(ap, size_t);
855 ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
858 case AST_EVENT_IE_PLTYPE_EXISTS:
859 ast_event_sub_append_ie_exists(sub, ie_type);
865 ast_event_sub_activate(sub);
870 void ast_event_sub_destroy(struct ast_event_sub *sub)
872 struct ast_event_ie_val *ie_val;
874 while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
875 ast_event_ie_val_destroy(ie_val);
881 const char *ast_event_subscriber_get_description(struct ast_event_sub *sub)
883 return sub ? sub->description : NULL;
886 struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
888 struct ast_event *event;
890 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
891 AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
892 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
894 if (ast_event_check_subscriber(AST_EVENT_UNSUB,
895 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
896 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
898 event = ast_event_new(AST_EVENT_UNSUB,
899 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
900 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
901 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
905 ast_event_queue(event);
909 ast_event_sub_destroy(sub);
914 void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
916 iterator->event_len = ntohs(event->event_len);
917 iterator->event = event;
918 iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
921 int ast_event_iterator_next(struct ast_event_iterator *iterator)
923 iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
924 return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
927 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
929 return ntohs(iterator->ie->ie_type);
932 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
934 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
937 uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
939 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
942 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
944 const struct ast_event_ie_str_payload *str_payload;
946 str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
948 return str_payload->str;
951 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
953 return iterator->ie->ie_payload;
956 enum ast_event_type ast_event_get_type(const struct ast_event *event)
958 return ntohs(event->type);
961 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
963 const uint32_t *ie_val;
965 ie_val = ast_event_get_ie_raw(event, ie_type);
967 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
970 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
972 const uint32_t *ie_val;
974 ie_val = ast_event_get_ie_raw(event, ie_type);
976 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
979 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
981 const struct ast_event_ie_str_payload *str_payload;
983 str_payload = ast_event_get_ie_raw(event, ie_type);
985 return str_payload->hash;
988 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
990 const struct ast_event_ie_str_payload *str_payload;
992 str_payload = ast_event_get_ie_raw(event, ie_type);
994 return str_payload->str;
997 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
999 struct ast_event_iterator iterator;
1002 for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1003 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1004 return ast_event_iterator_get_ie_raw(&iterator);
1011 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
1014 struct ast_event_ie_str_payload *str_payload;
1017 payload_len = sizeof(*str_payload) + strlen(str);
1018 str_payload = alloca(payload_len);
1020 strcpy(str_payload->str, str);
1021 str_payload->hash = ast_str_hash(str);
1023 return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
1026 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
1030 return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
1033 int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
1036 flags = htonl(flags);
1037 return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
1040 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
1041 const void *data, size_t data_len)
1043 struct ast_event_ie *ie;
1044 unsigned int extra_len;
1047 event_len = ntohs((*event)->event_len);
1048 extra_len = sizeof(*ie) + data_len;
1050 if (!(*event = ast_realloc(*event, event_len + extra_len))) {
1054 ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
1055 ie->ie_type = htons(ie_type);
1056 ie->ie_payload_len = htons(data_len);
1057 memcpy(ie->ie_payload, data, data_len);
1059 (*event)->event_len = htons(event_len + extra_len);
1064 struct ast_event *ast_event_new(enum ast_event_type type, ...)
1067 struct ast_event *event;
1068 enum ast_event_ie_type ie_type;
1069 struct ast_event_ie_val *ie_val;
1071 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
1074 if (type >= AST_EVENT_TOTAL) {
1075 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
1076 "type '%d'!\n", type);
1081 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1082 ie_type != AST_EVENT_IE_END;
1083 ie_type = va_arg(ap, enum ast_event_ie_type))
1085 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
1087 memset(ie_value, 0, sizeof(*ie_value));
1088 ie_value->ie_type = ie_type;
1089 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1090 switch (ie_value->ie_pltype) {
1091 case AST_EVENT_IE_PLTYPE_UINT:
1092 ie_value->payload.uint = va_arg(ap, uint32_t);
1094 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1095 ie_value->payload.uint = va_arg(ap, uint32_t);
1097 case AST_EVENT_IE_PLTYPE_STR:
1098 ie_value->payload.str = va_arg(ap, const char *);
1100 case AST_EVENT_IE_PLTYPE_RAW:
1102 void *data = va_arg(ap, void *);
1103 size_t datalen = va_arg(ap, size_t);
1104 ie_value->payload.raw = alloca(datalen);
1105 memcpy(ie_value->payload.raw, data, datalen);
1106 ie_value->raw_datalen = datalen;
1109 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1112 case AST_EVENT_IE_PLTYPE_EXISTS:
1117 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
1123 if (!(event = ast_calloc(1, sizeof(*event)))) {
1127 event->type = htons(type);
1128 event->event_len = htons(sizeof(*event));
1130 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
1131 switch (ie_val->ie_pltype) {
1132 case AST_EVENT_IE_PLTYPE_STR:
1133 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
1135 case AST_EVENT_IE_PLTYPE_UINT:
1136 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
1138 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1139 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
1141 case AST_EVENT_IE_PLTYPE_RAW:
1142 ast_event_append_ie_raw(&event, ie_val->ie_type,
1143 ie_val->payload.raw, ie_val->raw_datalen);
1145 case AST_EVENT_IE_PLTYPE_EXISTS:
1146 ast_log(LOG_WARNING, "PLTYPE_EXISTS unsupported in event_new\n");
1148 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1149 ast_log(LOG_WARNING, "PLTYPE_UNKNOWN passed as an IE type "
1150 "for a new event\n");
1159 if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
1160 /* If the event is originating on this server, add the server's
1161 * entity ID to the event. */
1162 ast_event_append_ie_raw(&event, AST_EVENT_IE_EID, &ast_eid_default, sizeof(ast_eid_default));
1168 void ast_event_destroy(struct ast_event *event)
1173 static void ast_event_ref_destroy(void *obj)
1175 struct ast_event_ref *event_ref = obj;
1177 ast_event_destroy(event_ref->event);
1180 static struct ast_event *ast_event_dup(const struct ast_event *event)
1182 struct ast_event *dup_event;
1185 event_len = ast_event_get_size(event);
1187 if (!(dup_event = ast_calloc(1, event_len))) {
1191 memcpy(dup_event, event, event_len);
1196 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
1199 enum ast_event_ie_type ie_type;
1200 struct ast_event *dup_event = NULL;
1201 struct ast_event_ref *cached_event_ref;
1202 struct ast_event *cache_arg_event;
1203 struct ast_event_ref tmp_event_ref = {
1206 struct ao2_container *container = NULL;
1208 if (type >= AST_EVENT_TOTAL) {
1209 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
1213 if (!(container = ast_event_cache[type].container)) {
1214 ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
1218 if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
1223 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1224 ie_type != AST_EVENT_IE_END;
1225 ie_type = va_arg(ap, enum ast_event_ie_type))
1227 enum ast_event_ie_pltype ie_pltype;
1229 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1231 switch (ie_pltype) {
1232 case AST_EVENT_IE_PLTYPE_UINT:
1233 ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1235 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1236 ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1238 case AST_EVENT_IE_PLTYPE_STR:
1239 ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
1241 case AST_EVENT_IE_PLTYPE_RAW:
1243 void *data = va_arg(ap, void *);
1244 size_t datalen = va_arg(ap, size_t);
1245 ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
1247 case AST_EVENT_IE_PLTYPE_EXISTS:
1248 ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
1250 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1256 tmp_event_ref.event = cache_arg_event;
1258 cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
1260 ast_event_destroy(cache_arg_event);
1261 cache_arg_event = NULL;
1263 if (cached_event_ref) {
1264 dup_event = ast_event_dup(cached_event_ref->event);
1265 ao2_ref(cached_event_ref, -1);
1266 cached_event_ref = NULL;
1272 static struct ast_event_ref *alloc_event_ref(void)
1274 return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
1277 /*! \brief Duplicate an event and add it to the cache
1278 * \note This assumes this index in to the cache is locked */
1279 static int ast_event_dup_and_cache(const struct ast_event *event)
1281 struct ast_event *dup_event;
1282 struct ast_event_ref *event_ref;
1284 if (!(dup_event = ast_event_dup(event))) {
1288 if (!(event_ref = alloc_event_ref())) {
1289 ast_event_destroy(dup_event);
1293 event_ref->event = dup_event;
1295 ao2_link(ast_event_cache[ast_event_get_type(event)].container, event_ref);
1297 ao2_ref(event_ref, -1);
1302 int ast_event_queue_and_cache(struct ast_event *event)
1304 struct ao2_container *container;
1305 struct ast_event_ref tmp_event_ref = {
1310 if (!(container = ast_event_cache[ast_event_get_type(event)].container)) {
1311 ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
1315 /* Remove matches from the cache */
1316 ao2_callback(container, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
1317 ast_event_cmp, &tmp_event_ref);
1319 res = ast_event_dup_and_cache(event);
1322 return ast_event_queue(event) ? -1 : res;
1325 static int handle_event(void *data)
1327 struct ast_event_ref *event_ref = data;
1328 struct ast_event_sub *sub;
1329 uint16_t host_event_type;
1331 host_event_type = ntohs(event_ref->event->type);
1333 /* Subscribers to this specific event first */
1334 AST_RWDLLIST_RDLOCK(&ast_event_subs[host_event_type]);
1335 AST_RWDLLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry) {
1336 struct ast_event_ie_val *ie_val;
1337 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
1338 if (!match_ie_val(event_ref->event, ie_val, NULL)) {
1345 sub->cb(event_ref->event, sub->userdata);
1347 AST_RWDLLIST_UNLOCK(&ast_event_subs[host_event_type]);
1349 /* Now to subscribers to all event types */
1350 AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
1351 AST_RWDLLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry) {
1352 sub->cb(event_ref->event, sub->userdata);
1354 AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
1356 ao2_ref(event_ref, -1);
1361 int ast_event_queue(struct ast_event *event)
1363 struct ast_event_ref *event_ref;
1364 uint16_t host_event_type;
1366 host_event_type = ntohs(event->type);
1369 if (host_event_type >= AST_EVENT_TOTAL) {
1370 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
1371 "type '%d'!\n", host_event_type);
1375 /* If nobody has subscribed to this event type, throw it away now */
1376 if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
1377 == AST_EVENT_SUB_NONE) {
1378 ast_event_destroy(event);
1379 ast_log(LOG_NOTICE, "Event destroyed, no subscriber\n");
1383 if (!(event_ref = alloc_event_ref())) {
1387 event_ref->event = event;
1389 return ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
1392 static int ast_event_hash_mwi(const void *obj, const int flags)
1394 const struct ast_event *event = obj;
1395 const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
1396 const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
1398 return ast_str_hash_add(context, ast_str_hash(mailbox));
1403 * \brief Hash function for AST_EVENT_DEVICE_STATE
1405 * \param[in] obj an ast_event
1406 * \param[in] flags unused
1408 * \return hash value
1410 static int ast_event_hash_devstate(const void *obj, const int flags)
1412 const struct ast_event *event = obj;
1414 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1419 * \brief Hash function for AST_EVENT_DEVICE_STATE_CHANGE
1421 * \param[in] obj an ast_event
1422 * \param[in] flags unused
1424 * \return hash value
1426 static int ast_event_hash_devstate_change(const void *obj, const int flags)
1428 const struct ast_event *event = obj;
1430 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1433 static int ast_event_hash(const void *obj, const int flags)
1435 const struct ast_event_ref *event_ref;
1436 const struct ast_event *event;
1437 ao2_hash_fn *hash_fn;
1440 event = event_ref->event;
1442 if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
1446 return hash_fn(event, flags);
1451 * \brief Compare two events
1453 * \param[in] obj the first event, as an ast_event_ref
1454 * \param[in] arg the second event, as an ast_event_ref
1455 * \param[in] flags unused
1457 * \pre Both events must be the same type.
1458 * \pre The event type must be declared as a cached event type in ast_event_cache
1460 * \details This function takes two events, and determines if they are considered
1461 * equivalent. The values of information elements specified in the cache arguments
1462 * for the event type are used to determine if the events are equivalent.
1464 * \retval 0 No match
1465 * \retval CMP_MATCH The events are considered equivalent based on the cache arguments
1467 static int ast_event_cmp(void *obj, void *arg, int flags)
1469 struct ast_event_ref *event_ref, *event_ref2;
1470 struct ast_event *event, *event2;
1471 int res = CMP_MATCH;
1473 enum ast_event_ie_type *cache_args;
1476 event = event_ref->event;
1479 event2 = event_ref2->event;
1481 cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
1483 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1484 struct ast_event_ie_val ie_val = {
1485 .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
1486 .ie_type = cache_args[i],
1489 if (!match_ie_val(event, &ie_val, event2)) {
1498 int ast_event_init(void)
1502 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1503 AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
1506 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1507 if (!ast_event_cache[i].hash_fn) {
1508 /* This event type is not cached. */
1512 if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
1513 ast_event_hash, ast_event_cmp))) {
1518 if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {