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_ALL] = "All",
198 [AST_EVENT_CUSTOM] = "Custom",
199 [AST_EVENT_MWI] = "MWI",
200 [AST_EVENT_SUB] = "Subscription",
201 [AST_EVENT_UNSUB] = "Unsubscription",
202 [AST_EVENT_DEVICE_STATE] = "DeviceState",
203 [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
204 [AST_EVENT_CEL] = "CEL",
205 [AST_EVENT_SECURITY] = "Security",
206 [AST_EVENT_NETWORK_CHANGE] = "NetworkChange",
210 * \brief IE payload types and names
212 static const struct ie_map {
213 enum ast_event_ie_pltype ie_pltype;
215 } ie_maps[AST_EVENT_IE_TOTAL] = {
216 [AST_EVENT_IE_NEWMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
217 [AST_EVENT_IE_OLDMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
218 [AST_EVENT_IE_MAILBOX] = { AST_EVENT_IE_PLTYPE_STR, "Mailbox" },
219 [AST_EVENT_IE_UNIQUEID] = { AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
220 [AST_EVENT_IE_EVENTTYPE] = { AST_EVENT_IE_PLTYPE_UINT, "EventType" },
221 [AST_EVENT_IE_EXISTS] = { AST_EVENT_IE_PLTYPE_UINT, "Exists" },
222 [AST_EVENT_IE_DEVICE] = { AST_EVENT_IE_PLTYPE_STR, "Device" },
223 [AST_EVENT_IE_STATE] = { AST_EVENT_IE_PLTYPE_UINT, "State" },
224 [AST_EVENT_IE_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "Context" },
225 [AST_EVENT_IE_EID] = { AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
226 [AST_EVENT_IE_CEL_EVENT_TYPE] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventType" },
227 [AST_EVENT_IE_CEL_EVENT_TIME] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTime" },
228 [AST_EVENT_IE_CEL_EVENT_TIME_USEC] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTimeUSec" },
229 [AST_EVENT_IE_CEL_USEREVENT_NAME] = { AST_EVENT_IE_PLTYPE_UINT, "CELUserEventName" },
230 [AST_EVENT_IE_CEL_CIDNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDName" },
231 [AST_EVENT_IE_CEL_CIDNUM] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDNum" },
232 [AST_EVENT_IE_CEL_EXTEN] = { AST_EVENT_IE_PLTYPE_STR, "CELExten" },
233 [AST_EVENT_IE_CEL_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "CELContext" },
234 [AST_EVENT_IE_CEL_CHANNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELChanName" },
235 [AST_EVENT_IE_CEL_APPNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELAppName" },
236 [AST_EVENT_IE_CEL_APPDATA] = { AST_EVENT_IE_PLTYPE_STR, "CELAppData" },
237 [AST_EVENT_IE_CEL_AMAFLAGS] = { AST_EVENT_IE_PLTYPE_STR, "CELAMAFlags" },
238 [AST_EVENT_IE_CEL_ACCTCODE] = { AST_EVENT_IE_PLTYPE_UINT, "CELAcctCode" },
239 [AST_EVENT_IE_CEL_UNIQUEID] = { AST_EVENT_IE_PLTYPE_STR, "CELUniqueID" },
240 [AST_EVENT_IE_CEL_USERFIELD] = { AST_EVENT_IE_PLTYPE_STR, "CELUserField" },
241 [AST_EVENT_IE_CEL_CIDANI] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDani" },
242 [AST_EVENT_IE_CEL_CIDRDNIS] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDrdnis" },
243 [AST_EVENT_IE_CEL_CIDDNID] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDdnid" },
244 [AST_EVENT_IE_CEL_PEER] = { AST_EVENT_IE_PLTYPE_STR, "CELPeer" },
245 [AST_EVENT_IE_CEL_LINKEDID] = { AST_EVENT_IE_PLTYPE_STR, "CELLinkedID" },
246 [AST_EVENT_IE_CEL_PEERACCT] = { AST_EVENT_IE_PLTYPE_STR, "CELPeerAcct" },
247 [AST_EVENT_IE_CEL_EXTRA] = { AST_EVENT_IE_PLTYPE_STR, "CELExtra" },
248 [AST_EVENT_IE_SECURITY_EVENT] = { AST_EVENT_IE_PLTYPE_STR, "SecurityEvent" },
249 [AST_EVENT_IE_EVENT_VERSION] = { AST_EVENT_IE_PLTYPE_UINT, "EventVersion" },
250 [AST_EVENT_IE_SERVICE] = { AST_EVENT_IE_PLTYPE_STR, "Service" },
251 [AST_EVENT_IE_MODULE] = { AST_EVENT_IE_PLTYPE_STR, "Module" },
252 [AST_EVENT_IE_ACCOUNT_ID] = { AST_EVENT_IE_PLTYPE_STR, "AccountID" },
253 [AST_EVENT_IE_SESSION_ID] = { AST_EVENT_IE_PLTYPE_STR, "SessionID" },
254 [AST_EVENT_IE_SESSION_TV] = { AST_EVENT_IE_PLTYPE_STR, "SessionTV" },
255 [AST_EVENT_IE_ACL_NAME] = { AST_EVENT_IE_PLTYPE_STR, "ACLName" },
256 [AST_EVENT_IE_LOCAL_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "LocalAddress" },
257 [AST_EVENT_IE_REMOTE_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "RemoteAddress" },
258 [AST_EVENT_IE_EVENT_TV] = { AST_EVENT_IE_PLTYPE_STR, "EventTV" },
259 [AST_EVENT_IE_REQUEST_TYPE] = { AST_EVENT_IE_PLTYPE_STR, "RequestType" },
260 [AST_EVENT_IE_REQUEST_PARAMS] = { AST_EVENT_IE_PLTYPE_STR, "RequestParams" },
261 [AST_EVENT_IE_AUTH_METHOD] = { AST_EVENT_IE_PLTYPE_STR, "AuthMethod" },
262 [AST_EVENT_IE_SEVERITY] = { AST_EVENT_IE_PLTYPE_STR, "Severity" },
263 [AST_EVENT_IE_EXPECTED_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedAddress" },
264 [AST_EVENT_IE_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "Challenge" },
265 [AST_EVENT_IE_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "Response" },
266 [AST_EVENT_IE_EXPECTED_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedResponse" },
269 const char *ast_event_get_type_name(const struct ast_event *event)
271 enum ast_event_type type;
273 type = ast_event_get_type(event);
275 if (type < 0 || type >= ARRAY_LEN(event_names)) {
276 ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
280 return event_names[type];
283 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
287 for (i = 0; i < ARRAY_LEN(event_names); i++) {
288 if (ast_strlen_zero(event_names[i]) || strcasecmp(event_names[i], str)) {
299 const char *ast_event_get_ie_type_name(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);
306 return ie_maps[ie_type].name;
309 enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
311 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
312 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
313 return AST_EVENT_IE_PLTYPE_UNKNOWN;
316 return ie_maps[ie_type].ie_pltype;
319 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
323 for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
324 if (strcasecmp(ie_maps[i].name, str)) {
335 size_t ast_event_get_size(const struct ast_event *event)
339 res = ntohs(event->event_len);
344 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
346 switch (ie_val->ie_pltype) {
347 case AST_EVENT_IE_PLTYPE_STR:
348 ast_free((char *) ie_val->payload.str);
350 case AST_EVENT_IE_PLTYPE_RAW:
351 ast_free(ie_val->payload.raw);
353 case AST_EVENT_IE_PLTYPE_UINT:
354 case AST_EVENT_IE_PLTYPE_BITFLAGS:
355 case AST_EVENT_IE_PLTYPE_EXISTS:
356 case AST_EVENT_IE_PLTYPE_UNKNOWN:
363 /*! \brief Subscription event check list. */
364 struct ast_ev_check_list {
365 AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
370 * \brief Check if a subscription ie_val matches an event.
372 * \param sub_ie_val Subscripton IE value to check
373 * \param check_ie_vals event list to check against
375 * \retval 0 not matched
376 * \retval non-zero matched
378 static int match_sub_ie_val_to_event(const struct ast_event_ie_val *sub_ie_val, const struct ast_ev_check_list *check_ie_vals)
380 const struct ast_event_ie_val *event_ie_val;
383 AST_LIST_TRAVERSE(&check_ie_vals->ie_vals, event_ie_val, entry) {
384 if (event_ie_val->ie_type == sub_ie_val->ie_type) {
389 /* The did not find the event ie the subscriber cares about. */
393 if (sub_ie_val->ie_pltype != event_ie_val->ie_pltype) {
394 if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
395 /* The subscription only cares that this ie exists. */
398 /* Payload types do not match. */
402 switch (sub_ie_val->ie_pltype) {
403 case AST_EVENT_IE_PLTYPE_UINT:
404 res = (sub_ie_val->payload.uint == event_ie_val->payload.uint);
406 case AST_EVENT_IE_PLTYPE_BITFLAGS:
408 * If the subscriber has requested *any* of the bitflags we are providing,
411 res = (sub_ie_val->payload.uint & event_ie_val->payload.uint);
413 case AST_EVENT_IE_PLTYPE_STR:
414 res = !strcmp(sub_ie_val->payload.str, event_ie_val->payload.str);
416 case AST_EVENT_IE_PLTYPE_RAW:
417 res = (sub_ie_val->raw_datalen == event_ie_val->raw_datalen
418 && !memcmp(sub_ie_val->payload.raw, event_ie_val->payload.raw,
419 sub_ie_val->raw_datalen));
421 case AST_EVENT_IE_PLTYPE_EXISTS:
422 /* Should never get here since check_ie_vals cannot have this type. */
424 case AST_EVENT_IE_PLTYPE_UNKNOWN:
426 * Should never be in a subscription event ie val list and
427 * check_ie_vals cannot have this type either.
435 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
438 enum ast_event_ie_type ie_type;
439 enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
440 struct ast_event_ie_val *ie_val;
441 struct ast_event_sub *sub;
442 struct ast_ev_check_list check_ie_vals = {
443 .ie_vals = AST_LIST_HEAD_NOLOCK_INIT_VALUE
445 const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
448 if (type >= AST_EVENT_TOTAL) {
449 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
454 for (ie_type = va_arg(ap, enum ast_event_ie_type);
455 ie_type != AST_EVENT_IE_END;
456 ie_type = va_arg(ap, enum ast_event_ie_type))
458 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
461 memset(ie_value, 0, sizeof(*ie_value));
462 ie_value->ie_type = ie_type;
463 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
464 switch (ie_value->ie_pltype) {
465 case AST_EVENT_IE_PLTYPE_UINT:
466 ie_value->payload.uint = va_arg(ap, uint32_t);
469 case AST_EVENT_IE_PLTYPE_BITFLAGS:
470 ie_value->payload.uint = va_arg(ap, uint32_t);
473 case AST_EVENT_IE_PLTYPE_STR:
474 ie_value->payload.str = va_arg(ap, const char *);
477 case AST_EVENT_IE_PLTYPE_RAW:
479 void *data = va_arg(ap, void *);
480 size_t datalen = va_arg(ap, size_t);
482 ie_value->payload.raw = alloca(datalen);
483 memcpy(ie_value->payload.raw, data, datalen);
484 ie_value->raw_datalen = datalen;
488 case AST_EVENT_IE_PLTYPE_UNKNOWN:
489 case AST_EVENT_IE_PLTYPE_EXISTS:
490 /* Unsupported payload type. */
495 AST_LIST_INSERT_TAIL(&check_ie_vals.ie_vals, ie_value, entry);
497 ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
502 for (i = 0; i < ARRAY_LEN(event_types); i++) {
503 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
504 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
505 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
506 if (!match_sub_ie_val_to_event(ie_val, &check_ie_vals)) {
507 /* The current subscription ie did not match an event ie. */
512 /* Everything matched. A subscriber is looking for this event. */
516 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
522 return sub ? AST_EVENT_SUB_EXISTS : AST_EVENT_SUB_NONE;
527 * \brief Check if an ie_val matches an event
529 * \param event event to check against
530 * \param ie_val IE value to check
531 * \param event2 optional event, if specified, the value to compare against will be pulled
532 * from this event instead of from the ie_val structure. In this case, only the IE
533 * type and payload type will be pulled from ie_val.
535 * \retval 0 not matched
536 * \retval non-zero matched
538 static int match_ie_val(const struct ast_event *event,
539 const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
541 switch (ie_val->ie_pltype) {
542 case AST_EVENT_IE_PLTYPE_UINT:
544 uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
546 return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
549 case AST_EVENT_IE_PLTYPE_BITFLAGS:
551 uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
554 * If the subscriber has requested *any* of the bitflags that this event provides,
557 return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
560 case AST_EVENT_IE_PLTYPE_STR:
565 hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
566 if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
570 str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
571 if (str && !strcmp(str, ast_event_get_ie_str(event, ie_val->ie_type))) {
578 case AST_EVENT_IE_PLTYPE_RAW:
580 const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
581 uint16_t ie_payload_len = event2 ? ast_event_get_ie_raw_payload_len(event2, ie_val->ie_type) : ie_val->raw_datalen;
584 && ie_payload_len == ast_event_get_ie_raw_payload_len(event, ie_val->ie_type)
585 && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_payload_len)) ? 1 : 0;
588 case AST_EVENT_IE_PLTYPE_EXISTS:
590 return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
593 case AST_EVENT_IE_PLTYPE_UNKNOWN:
600 static int dump_cache_cb(void *obj, void *arg, int flags)
602 const struct ast_event_ref *event_ref = obj;
603 const struct ast_event *event = event_ref->event;
604 const struct ast_event_sub *event_sub = arg;
605 struct ast_event_ie_val *ie_val = NULL;
607 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
608 if (!match_ie_val(event, ie_val, NULL)) {
614 /* All parameters were matched on this cache entry, so dump it */
615 event_sub->cb(event, event_sub->userdata);
621 /*! \brief Dump the event cache for the subscribed event type */
622 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
624 ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
625 dump_cache_cb, (void *) event_sub);
628 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
630 struct ast_event_ie_val *ie_val;
631 struct ast_event *event;
633 event = ast_event_new(AST_EVENT_SUB,
634 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
635 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
636 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
641 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
642 switch (ie_val->ie_pltype) {
643 case AST_EVENT_IE_PLTYPE_UNKNOWN:
645 case AST_EVENT_IE_PLTYPE_EXISTS:
646 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
648 case AST_EVENT_IE_PLTYPE_UINT:
649 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
651 case AST_EVENT_IE_PLTYPE_BITFLAGS:
652 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
654 case AST_EVENT_IE_PLTYPE_STR:
655 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
657 case AST_EVENT_IE_PLTYPE_RAW:
658 ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
668 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
669 void ast_event_report_subs(const struct ast_event_sub *event_sub)
671 struct ast_event *event;
672 struct ast_event_sub *sub;
673 enum ast_event_type event_type = -1;
674 struct ast_event_ie_val *ie_val;
676 if (event_sub->type != AST_EVENT_SUB)
679 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
680 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
681 event_type = ie_val->payload.uint;
686 if (event_type == -1)
689 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
690 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
691 if (event_sub == sub) {
695 event = gen_sub_event(sub);
700 event_sub->cb(event, event_sub->userdata);
702 ast_event_destroy(event);
704 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
707 struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
708 ast_event_cb_t cb, void *userdata)
710 struct ast_event_sub *sub;
712 if (type < 0 || type >= AST_EVENT_TOTAL) {
713 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
717 if (!(sub = ast_calloc(1, sizeof(*sub)))) {
723 sub->userdata = userdata;
724 sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
729 int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
730 enum ast_event_ie_type ie_type, uint32_t unsigned_int)
732 struct ast_event_ie_val *ie_val;
734 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
738 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
742 ie_val->ie_type = ie_type;
743 ie_val->payload.uint = unsigned_int;
744 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
746 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
751 int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
752 enum ast_event_ie_type ie_type, uint32_t flags)
754 struct ast_event_ie_val *ie_val;
756 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
760 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
764 ie_val->ie_type = ie_type;
765 ie_val->payload.uint = flags;
766 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
768 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
773 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
774 enum ast_event_ie_type ie_type)
776 struct ast_event_ie_val *ie_val;
778 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
782 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
786 ie_val->ie_type = ie_type;
787 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
789 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
794 int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
795 enum ast_event_ie_type ie_type, const char *str)
797 struct ast_event_ie_val *ie_val;
799 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
803 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
807 ie_val->ie_type = ie_type;
808 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
810 if (!(ie_val->payload.str = ast_strdup(str))) {
815 ie_val->payload.hash = ast_str_hash(str);
817 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
822 int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
823 enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
825 struct ast_event_ie_val *ie_val;
827 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
831 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
835 ie_val->ie_type = ie_type;
836 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
837 ie_val->raw_datalen = raw_datalen;
839 if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
844 memcpy(ie_val->payload.raw, data, raw_datalen);
846 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
851 int ast_event_sub_activate(struct ast_event_sub *sub)
853 if (ast_event_check_subscriber(AST_EVENT_SUB,
854 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
855 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
856 struct ast_event *event;
858 event = gen_sub_event(sub);
859 if (event && ast_event_queue(event)) {
860 ast_event_destroy(event);
864 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
865 AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
866 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
871 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
872 const char *description, void *userdata, ...)
875 enum ast_event_ie_type ie_type;
876 struct ast_event_sub *sub;
878 if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
882 ast_copy_string(sub->description, description, sizeof(sub->description));
884 va_start(ap, userdata);
885 for (ie_type = va_arg(ap, enum ast_event_ie_type);
886 ie_type != AST_EVENT_IE_END;
887 ie_type = va_arg(ap, enum ast_event_ie_type))
889 enum ast_event_ie_pltype ie_pltype;
891 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
894 case AST_EVENT_IE_PLTYPE_UNKNOWN:
896 case AST_EVENT_IE_PLTYPE_UINT:
898 uint32_t unsigned_int = va_arg(ap, uint32_t);
899 ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
902 case AST_EVENT_IE_PLTYPE_BITFLAGS:
904 uint32_t unsigned_int = va_arg(ap, uint32_t);
905 ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
908 case AST_EVENT_IE_PLTYPE_STR:
910 const char *str = va_arg(ap, const char *);
911 ast_event_sub_append_ie_str(sub, ie_type, str);
914 case AST_EVENT_IE_PLTYPE_RAW:
916 void *data = va_arg(ap, void *);
917 size_t data_len = va_arg(ap, size_t);
918 ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
921 case AST_EVENT_IE_PLTYPE_EXISTS:
922 ast_event_sub_append_ie_exists(sub, ie_type);
928 ast_event_sub_activate(sub);
933 void ast_event_sub_destroy(struct ast_event_sub *sub)
935 struct ast_event_ie_val *ie_val;
937 while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
938 ast_event_ie_val_destroy(ie_val);
944 const char *ast_event_subscriber_get_description(struct ast_event_sub *sub)
946 return sub ? sub->description : NULL;
949 struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
951 struct ast_event *event;
953 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
954 AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
955 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
957 if (ast_event_check_subscriber(AST_EVENT_UNSUB,
958 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
959 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
961 event = ast_event_new(AST_EVENT_UNSUB,
962 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
963 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
964 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
966 if (event && ast_event_queue(event)) {
967 ast_event_destroy(event);
971 ast_event_sub_destroy(sub);
976 int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
980 iterator->event_len = ast_event_get_size(event);
981 iterator->event = event;
982 if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
983 iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
992 int ast_event_iterator_next(struct ast_event_iterator *iterator)
994 iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
995 return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
998 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
1000 return ntohs(iterator->ie->ie_type);
1003 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
1005 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1008 uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
1010 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1013 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
1015 const struct ast_event_ie_str_payload *str_payload;
1017 str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
1019 return str_payload ? str_payload->str : NULL;
1022 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
1024 return iterator->ie->ie_payload;
1027 uint16_t ast_event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
1029 return ntohs(iterator->ie->ie_payload_len);
1032 enum ast_event_type ast_event_get_type(const struct ast_event *event)
1034 return ntohs(event->type);
1037 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
1039 const uint32_t *ie_val;
1041 ie_val = ast_event_get_ie_raw(event, ie_type);
1043 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1046 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
1048 const uint32_t *ie_val;
1050 ie_val = ast_event_get_ie_raw(event, ie_type);
1052 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1055 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
1057 const struct ast_event_ie_str_payload *str_payload;
1059 str_payload = ast_event_get_ie_raw(event, ie_type);
1061 return str_payload ? str_payload->hash : 0;
1064 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
1066 const struct ast_event_ie_str_payload *str_payload;
1068 str_payload = ast_event_get_ie_raw(event, ie_type);
1070 return str_payload ? str_payload->str : NULL;
1073 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
1075 struct ast_event_iterator iterator;
1078 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1079 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1080 return ast_event_iterator_get_ie_raw(&iterator);
1087 uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type)
1089 struct ast_event_iterator iterator;
1092 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1093 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1094 return ast_event_iterator_get_ie_raw_payload_len(&iterator);
1101 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
1104 struct ast_event_ie_str_payload *str_payload;
1107 payload_len = sizeof(*str_payload) + strlen(str);
1108 str_payload = alloca(payload_len);
1110 strcpy(str_payload->str, str);
1111 str_payload->hash = ast_str_hash(str);
1113 return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
1116 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
1120 return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
1123 int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
1126 flags = htonl(flags);
1127 return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
1130 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
1131 const void *data, size_t data_len)
1133 struct ast_event_ie *ie;
1134 unsigned int extra_len;
1137 event_len = ntohs((*event)->event_len);
1138 extra_len = sizeof(*ie) + data_len;
1140 if (!(*event = ast_realloc(*event, event_len + extra_len))) {
1144 ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
1145 ie->ie_type = htons(ie_type);
1146 ie->ie_payload_len = htons(data_len);
1147 memcpy(ie->ie_payload, data, data_len);
1149 (*event)->event_len = htons(event_len + extra_len);
1154 struct ast_event *ast_event_new(enum ast_event_type type, ...)
1157 struct ast_event *event;
1158 enum ast_event_ie_type ie_type;
1159 struct ast_event_ie_val *ie_val;
1161 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
1164 if (type >= AST_EVENT_TOTAL) {
1165 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
1166 "type '%d'!\n", type);
1171 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1172 ie_type != AST_EVENT_IE_END;
1173 ie_type = va_arg(ap, enum ast_event_ie_type))
1175 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
1178 memset(ie_value, 0, sizeof(*ie_value));
1179 ie_value->ie_type = ie_type;
1180 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1181 switch (ie_value->ie_pltype) {
1182 case AST_EVENT_IE_PLTYPE_UINT:
1183 ie_value->payload.uint = va_arg(ap, uint32_t);
1186 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1187 ie_value->payload.uint = va_arg(ap, uint32_t);
1190 case AST_EVENT_IE_PLTYPE_STR:
1191 ie_value->payload.str = va_arg(ap, const char *);
1194 case AST_EVENT_IE_PLTYPE_RAW:
1196 void *data = va_arg(ap, void *);
1197 size_t datalen = va_arg(ap, size_t);
1198 ie_value->payload.raw = alloca(datalen);
1199 memcpy(ie_value->payload.raw, data, datalen);
1200 ie_value->raw_datalen = datalen;
1204 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1205 case AST_EVENT_IE_PLTYPE_EXISTS:
1210 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
1213 ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
1218 if (!(event = ast_calloc(1, sizeof(*event)))) {
1222 event->type = htons(type);
1223 event->event_len = htons(sizeof(*event));
1225 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
1226 switch (ie_val->ie_pltype) {
1227 case AST_EVENT_IE_PLTYPE_STR:
1228 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
1230 case AST_EVENT_IE_PLTYPE_UINT:
1231 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
1233 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1234 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
1236 case AST_EVENT_IE_PLTYPE_RAW:
1237 ast_event_append_ie_raw(&event, ie_val->ie_type,
1238 ie_val->payload.raw, ie_val->raw_datalen);
1240 case AST_EVENT_IE_PLTYPE_EXISTS:
1241 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1250 if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
1251 /* If the event is originating on this server, add the server's
1252 * entity ID to the event. */
1253 ast_event_append_eid(&event);
1259 int ast_event_append_eid(struct ast_event **event)
1261 return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
1262 &ast_eid_default, sizeof(ast_eid_default));
1265 void ast_event_destroy(struct ast_event *event)
1270 static void ast_event_ref_destroy(void *obj)
1272 struct ast_event_ref *event_ref = obj;
1274 ast_event_destroy(event_ref->event);
1277 static struct ast_event *ast_event_dup(const struct ast_event *event)
1279 struct ast_event *dup_event;
1282 event_len = ast_event_get_size(event);
1284 if (!(dup_event = ast_calloc(1, event_len))) {
1288 memcpy(dup_event, event, event_len);
1293 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
1296 enum ast_event_ie_type ie_type;
1297 struct ast_event *dup_event = NULL;
1298 struct ast_event_ref *cached_event_ref;
1299 struct ast_event *cache_arg_event;
1300 struct ast_event_ref tmp_event_ref = {
1303 struct ao2_container *container = NULL;
1305 if (type >= AST_EVENT_TOTAL) {
1306 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
1310 if (!(container = ast_event_cache[type].container)) {
1311 ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
1315 if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
1320 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1321 ie_type != AST_EVENT_IE_END;
1322 ie_type = va_arg(ap, enum ast_event_ie_type))
1324 enum ast_event_ie_pltype ie_pltype;
1326 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1328 switch (ie_pltype) {
1329 case AST_EVENT_IE_PLTYPE_UINT:
1330 ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1332 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1333 ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1335 case AST_EVENT_IE_PLTYPE_STR:
1336 ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
1338 case AST_EVENT_IE_PLTYPE_RAW:
1340 void *data = va_arg(ap, void *);
1341 size_t datalen = va_arg(ap, size_t);
1342 ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
1344 case AST_EVENT_IE_PLTYPE_EXISTS:
1345 ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
1347 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1353 tmp_event_ref.event = cache_arg_event;
1355 cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
1357 ast_event_destroy(cache_arg_event);
1358 cache_arg_event = NULL;
1360 if (cached_event_ref) {
1361 dup_event = ast_event_dup(cached_event_ref->event);
1362 ao2_ref(cached_event_ref, -1);
1363 cached_event_ref = NULL;
1369 static struct ast_event_ref *alloc_event_ref(void)
1371 return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
1376 * \brief Update the given event cache with the new event.
1379 * \param cache Event cache container to update.
1380 * \param event New event to put in the cache.
1384 static void event_update_cache(struct ao2_container *cache, struct ast_event *event)
1386 struct ast_event_ref tmp_event_ref = {
1389 struct ast_event *dup_event;
1390 struct ast_event_ref *event_ref;
1392 /* Hold the cache container lock while it is updated. */
1395 /* Remove matches from the cache. */
1396 ao2_callback(cache, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
1397 ast_event_cmp, &tmp_event_ref);
1399 /* Save a copy of the event in the cache. */
1400 dup_event = ast_event_dup(event);
1402 event_ref = alloc_event_ref();
1404 event_ref->event = dup_event;
1405 ao2_link(cache, event_ref);
1406 ao2_ref(event_ref, -1);
1408 ast_event_destroy(dup_event);
1415 int ast_event_queue_and_cache(struct ast_event *event)
1417 struct ao2_container *container;
1419 container = ast_event_cache[ast_event_get_type(event)].container;
1421 ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
1423 event_update_cache(container, event);
1426 if (ast_event_queue(event)) {
1427 ast_event_destroy(event);
1432 static int handle_event(void *data)
1434 struct ast_event_ref *event_ref = data;
1435 struct ast_event_sub *sub;
1436 const enum ast_event_type event_types[] = {
1437 ntohs(event_ref->event->type),
1442 for (i = 0; i < ARRAY_LEN(event_types); i++) {
1443 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
1444 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
1445 struct ast_event_ie_val *ie_val;
1447 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
1448 if (!match_ie_val(event_ref->event, ie_val, NULL)) {
1449 /* The current subscription ie did not match an event ie. */
1454 /* The event did not match this subscription. */
1457 sub->cb(event_ref->event, sub->userdata);
1459 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
1462 ao2_ref(event_ref, -1);
1467 int ast_event_queue(struct ast_event *event)
1469 struct ast_event_ref *event_ref;
1470 uint16_t host_event_type;
1473 host_event_type = ntohs(event->type);
1476 if (host_event_type >= AST_EVENT_TOTAL) {
1477 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
1478 "type '%d'!\n", host_event_type);
1482 /* If nobody has subscribed to this event type, throw it away now */
1483 if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
1484 == AST_EVENT_SUB_NONE) {
1485 ast_event_destroy(event);
1489 if (!(event_ref = alloc_event_ref())) {
1493 event_ref->event = event;
1495 res = ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
1497 event_ref->event = NULL;
1498 ao2_ref(event_ref, -1);
1503 static int ast_event_hash_mwi(const void *obj, const int flags)
1505 const struct ast_event *event = obj;
1506 const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
1507 const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
1509 return ast_str_hash_add(context, ast_str_hash(mailbox));
1514 * \brief Hash function for AST_EVENT_DEVICE_STATE
1516 * \param[in] obj an ast_event
1517 * \param[in] flags unused
1519 * \return hash value
1521 static int ast_event_hash_devstate(const void *obj, const int flags)
1523 const struct ast_event *event = obj;
1525 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1530 * \brief Hash function for AST_EVENT_DEVICE_STATE_CHANGE
1532 * \param[in] obj an ast_event
1533 * \param[in] flags unused
1535 * \return hash value
1537 static int ast_event_hash_devstate_change(const void *obj, const int flags)
1539 const struct ast_event *event = obj;
1541 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1544 static int ast_event_hash(const void *obj, const int flags)
1546 const struct ast_event_ref *event_ref;
1547 const struct ast_event *event;
1548 ao2_hash_fn *hash_fn;
1551 event = event_ref->event;
1553 if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
1557 return hash_fn(event, flags);
1562 * \brief Compare two events
1564 * \param[in] obj the first event, as an ast_event_ref
1565 * \param[in] arg the second event, as an ast_event_ref
1566 * \param[in] flags unused
1568 * \pre Both events must be the same type.
1569 * \pre The event type must be declared as a cached event type in ast_event_cache
1571 * \details This function takes two events, and determines if they are considered
1572 * equivalent. The values of information elements specified in the cache arguments
1573 * for the event type are used to determine if the events are equivalent.
1575 * \retval 0 No match
1576 * \retval CMP_MATCH The events are considered equivalent based on the cache arguments
1578 static int ast_event_cmp(void *obj, void *arg, int flags)
1580 struct ast_event_ref *event_ref, *event_ref2;
1581 struct ast_event *event, *event2;
1582 int res = CMP_MATCH;
1584 enum ast_event_ie_type *cache_args;
1587 event = event_ref->event;
1590 event2 = event_ref2->event;
1592 cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
1594 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1595 struct ast_event_ie_val ie_val = {
1596 .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
1597 .ie_type = cache_args[i],
1600 if (!match_ie_val(event, &ie_val, event2)) {
1609 static void dump_raw_ie(struct ast_event_iterator *i, struct ast_cli_args *a)
1612 enum ast_event_ie_type ie_type;
1613 const char *ie_type_name;
1615 ie_type = ast_event_iterator_get_ie_type(i);
1616 ie_type_name = ast_event_get_ie_type_name(ie_type);
1619 case AST_EVENT_IE_EID:
1620 ast_eid_to_str(eid_buf, sizeof(eid_buf), ast_event_iterator_get_ie_raw(i));
1621 ast_cli(a->fd, "%.30s: %s\n", ie_type_name, eid_buf);
1624 ast_cli(a->fd, "%s\n", ie_type_name);
1629 static int event_dump_cli(void *obj, void *arg, int flags)
1631 const struct ast_event_ref *event_ref = obj;
1632 const struct ast_event *event = event_ref->event;
1633 struct ast_cli_args *a = arg;
1634 struct ast_event_iterator i;
1636 if (ast_event_iterator_init(&i, event)) {
1637 ast_cli(a->fd, "Failed to initialize event iterator. :-(\n");
1641 ast_cli(a->fd, "Event: %s\n", ast_event_get_type_name(event));
1644 enum ast_event_ie_type ie_type;
1645 enum ast_event_ie_pltype ie_pltype;
1646 const char *ie_type_name;
1648 ie_type = ast_event_iterator_get_ie_type(&i);
1649 ie_type_name = ast_event_get_ie_type_name(ie_type);
1650 ie_pltype = ast_event_get_ie_pltype(ie_type);
1652 switch (ie_pltype) {
1653 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1654 case AST_EVENT_IE_PLTYPE_EXISTS:
1655 ast_cli(a->fd, "%s\n", ie_type_name);
1657 case AST_EVENT_IE_PLTYPE_STR:
1658 ast_cli(a->fd, "%.30s: %s\n", ie_type_name,
1659 ast_event_iterator_get_ie_str(&i));
1661 case AST_EVENT_IE_PLTYPE_UINT:
1662 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1663 ast_event_iterator_get_ie_uint(&i));
1665 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1666 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1667 ast_event_iterator_get_ie_bitflags(&i));
1669 case AST_EVENT_IE_PLTYPE_RAW:
1673 } while (!ast_event_iterator_next(&i));
1675 ast_cli(a->fd, "\n");
1680 static char *event_dump_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1682 enum ast_event_type event_type;
1683 enum ast_event_ie_type *cache_args;
1688 e->command = "event dump cache";
1690 "Usage: event dump cache <event type>\n"
1691 " Dump all of the cached events for the given event type.\n"
1692 " This is primarily intended for debugging.\n";
1696 return ast_cli_complete(a->word, cached_event_types, a->n);
1703 if (a->argc != e->args + 1) {
1704 return CLI_SHOWUSAGE;
1707 if (ast_event_str_to_event_type(a->argv[e->args], &event_type)) {
1708 ast_cli(a->fd, "Invalid cached event type: '%s'\n", a->argv[e->args]);
1709 return CLI_SHOWUSAGE;
1712 if (!ast_event_cache[event_type].container) {
1713 ast_cli(a->fd, "Event type '%s' has no cache.\n", a->argv[e->args]);
1717 ast_cli(a->fd, "Event Type: %s\n", a->argv[e->args]);
1718 ast_cli(a->fd, "Cache Unique Keys:\n");
1719 cache_args = ast_event_cache[event_type].cache_args;
1720 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1721 ast_cli(a->fd, "--> %s\n", ast_event_get_ie_type_name(cache_args[i]));
1724 ast_cli(a->fd, "\n--- Begin Cache Dump ---\n\n");
1725 ao2_callback(ast_event_cache[event_type].container, OBJ_NODATA, event_dump_cli, a);
1726 ast_cli(a->fd, "--- End Cache Dump ---\n\n");
1731 static struct ast_cli_entry event_cli[] = {
1732 AST_CLI_DEFINE(event_dump_cache, "Dump the internal event cache (for debugging)"),
1735 int ast_event_init(void)
1739 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1740 AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
1743 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1744 if (!ast_event_cache[i].hash_fn) {
1745 /* This event type is not cached. */
1749 if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
1750 ast_event_hash, ast_event_cmp))) {
1755 if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
1759 ast_cli_register_multiple(event_cli, ARRAY_LEN(event_cli));