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" },
267 [AST_EVENT_IE_RECEIVED_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "ReceivedChallenge" },
268 [AST_EVENT_IE_RECEIVED_HASH] = { AST_EVENT_IE_PLTYPE_STR, "ReceivedHash" },
269 [AST_EVENT_IE_USING_PASSWORD] = { AST_EVENT_IE_PLTYPE_UINT, "UsingPassword" },
273 const char *ast_event_get_type_name(const struct ast_event *event)
275 enum ast_event_type type;
277 type = ast_event_get_type(event);
279 if (type < 0 || type >= ARRAY_LEN(event_names)) {
280 ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
284 return event_names[type];
287 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
291 for (i = 0; i < ARRAY_LEN(event_names); i++) {
292 if (ast_strlen_zero(event_names[i]) || strcasecmp(event_names[i], str)) {
303 const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
305 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
306 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
310 return ie_maps[ie_type].name;
313 enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
315 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
316 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
317 return AST_EVENT_IE_PLTYPE_UNKNOWN;
320 return ie_maps[ie_type].ie_pltype;
323 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
327 for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
328 if (strcasecmp(ie_maps[i].name, str)) {
339 size_t ast_event_get_size(const struct ast_event *event)
343 res = ntohs(event->event_len);
348 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
350 switch (ie_val->ie_pltype) {
351 case AST_EVENT_IE_PLTYPE_STR:
352 ast_free((char *) ie_val->payload.str);
354 case AST_EVENT_IE_PLTYPE_RAW:
355 ast_free(ie_val->payload.raw);
357 case AST_EVENT_IE_PLTYPE_UINT:
358 case AST_EVENT_IE_PLTYPE_BITFLAGS:
359 case AST_EVENT_IE_PLTYPE_EXISTS:
360 case AST_EVENT_IE_PLTYPE_UNKNOWN:
367 /*! \brief Subscription event check list. */
368 struct ast_ev_check_list {
369 AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
374 * \brief Check if a subscription ie_val matches an event.
376 * \param sub_ie_val Subscripton IE value to check
377 * \param check_ie_vals event list to check against
379 * \retval 0 not matched
380 * \retval non-zero matched
382 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)
384 const struct ast_event_ie_val *event_ie_val;
387 AST_LIST_TRAVERSE(&check_ie_vals->ie_vals, event_ie_val, entry) {
388 if (sub_ie_val->ie_type == event_ie_val->ie_type) {
393 /* We did not find the event ie the subscriber cares about. */
397 if (sub_ie_val->ie_pltype != event_ie_val->ie_pltype) {
398 if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
399 /* The subscription only cares that this ie exists. */
402 /* Payload types do not match. */
406 switch (sub_ie_val->ie_pltype) {
407 case AST_EVENT_IE_PLTYPE_UINT:
408 res = (sub_ie_val->payload.uint == event_ie_val->payload.uint);
410 case AST_EVENT_IE_PLTYPE_BITFLAGS:
412 * If the subscriber has requested *any* of the bitflags we are providing,
415 res = (sub_ie_val->payload.uint & event_ie_val->payload.uint);
417 case AST_EVENT_IE_PLTYPE_STR:
419 const char *substr = sub_ie_val->payload.str;
420 const char *estr = event_ie_val->payload.str;
421 if (sub_ie_val->ie_type == AST_EVENT_IE_DEVICE) {
422 substr = ast_tech_to_upper(ast_strdupa(substr));
423 estr = ast_tech_to_upper(ast_strdupa(estr));
425 res = !strcmp(substr, estr);
428 case AST_EVENT_IE_PLTYPE_RAW:
429 res = (sub_ie_val->raw_datalen == event_ie_val->raw_datalen
430 && !memcmp(sub_ie_val->payload.raw, event_ie_val->payload.raw,
431 sub_ie_val->raw_datalen));
433 case AST_EVENT_IE_PLTYPE_EXISTS:
434 /* Should never get here since check_ie_vals cannot have this type. */
436 case AST_EVENT_IE_PLTYPE_UNKNOWN:
438 * Should never be in a subscription event ie val list and
439 * check_ie_vals cannot have this type either.
447 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
450 enum ast_event_ie_type ie_type;
451 enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
452 struct ast_event_ie_val *ie_val;
453 struct ast_event_sub *sub;
454 struct ast_ev_check_list check_ie_vals = {
455 .ie_vals = AST_LIST_HEAD_NOLOCK_INIT_VALUE
457 const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
459 int want_specific_event;/* TRUE if looking for subscribers wanting specific parameters. */
461 if (type >= AST_EVENT_TOTAL) {
462 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
466 want_specific_event = 0;
468 for (ie_type = va_arg(ap, enum ast_event_ie_type);
469 ie_type != AST_EVENT_IE_END;
470 ie_type = va_arg(ap, enum ast_event_ie_type))
472 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
475 memset(ie_value, 0, sizeof(*ie_value));
476 ie_value->ie_type = ie_type;
477 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
478 switch (ie_value->ie_pltype) {
479 case AST_EVENT_IE_PLTYPE_UINT:
480 ie_value->payload.uint = va_arg(ap, uint32_t);
483 case AST_EVENT_IE_PLTYPE_BITFLAGS:
484 ie_value->payload.uint = va_arg(ap, uint32_t);
487 case AST_EVENT_IE_PLTYPE_STR:
488 ie_value->payload.str = va_arg(ap, const char *);
491 case AST_EVENT_IE_PLTYPE_RAW:
493 void *data = va_arg(ap, void *);
494 size_t datalen = va_arg(ap, size_t);
496 ie_value->payload.raw = alloca(datalen);
497 memcpy(ie_value->payload.raw, data, datalen);
498 ie_value->raw_datalen = datalen;
502 case AST_EVENT_IE_PLTYPE_UNKNOWN:
503 case AST_EVENT_IE_PLTYPE_EXISTS:
504 /* Unsupported payload type. */
509 want_specific_event = 1;
510 AST_LIST_INSERT_TAIL(&check_ie_vals.ie_vals, ie_value, entry);
512 ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
517 for (i = 0; i < ARRAY_LEN(event_types); i++) {
518 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
519 if (want_specific_event) {
520 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
521 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
522 if (!match_sub_ie_val_to_event(ie_val, &check_ie_vals)) {
523 /* The current subscription ie did not match an event ie. */
528 /* Everything matched. A subscriber is looking for this event. */
533 /* Just looking to see if there are ANY subscribers to the event type. */
534 sub = AST_RWLIST_FIRST(&ast_event_subs[event_types[i]]);
536 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
542 return sub ? AST_EVENT_SUB_EXISTS : AST_EVENT_SUB_NONE;
547 * \brief Check if an ie_val matches an event
549 * \param event event to check against
550 * \param ie_val IE value to check
551 * \param event2 optional event, if specified, the value to compare against will be pulled
552 * from this event instead of from the ie_val structure. In this case, only the IE
553 * type and payload type will be pulled from ie_val.
555 * \retval 0 not matched
556 * \retval non-zero matched
558 static int match_ie_val(const struct ast_event *event,
559 const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
561 switch (ie_val->ie_pltype) {
562 case AST_EVENT_IE_PLTYPE_UINT:
564 uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
566 return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
569 case AST_EVENT_IE_PLTYPE_BITFLAGS:
571 uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
574 * If the subscriber has requested *any* of the bitflags that this event provides,
577 return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
580 case AST_EVENT_IE_PLTYPE_STR:
585 hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
586 if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
590 str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
592 const char *e1str, *e2str;
593 e1str = ast_event_get_ie_str(event, ie_val->ie_type);
596 if (ie_val->ie_type == AST_EVENT_IE_DEVICE) {
597 e1str = ast_tech_to_upper(ast_strdupa(e1str));
598 e2str = ast_tech_to_upper(ast_strdupa(e2str));
601 if (!strcmp(e1str, e2str)) {
609 case AST_EVENT_IE_PLTYPE_RAW:
611 const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
612 uint16_t ie_payload_len = event2 ? ast_event_get_ie_raw_payload_len(event2, ie_val->ie_type) : ie_val->raw_datalen;
615 && ie_payload_len == ast_event_get_ie_raw_payload_len(event, ie_val->ie_type)
616 && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_payload_len)) ? 1 : 0;
619 case AST_EVENT_IE_PLTYPE_EXISTS:
621 return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
624 case AST_EVENT_IE_PLTYPE_UNKNOWN:
631 static int dump_cache_cb(void *obj, void *arg, int flags)
633 const struct ast_event_ref *event_ref = obj;
634 const struct ast_event *event = event_ref->event;
635 const struct ast_event_sub *event_sub = arg;
636 struct ast_event_ie_val *ie_val = NULL;
638 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
639 if (!match_ie_val(event, ie_val, NULL)) {
645 /* All parameters were matched on this cache entry, so dump it */
646 event_sub->cb(event, event_sub->userdata);
652 /*! \brief Dump the event cache for the subscribed event type */
653 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
655 ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
656 dump_cache_cb, (void *) event_sub);
659 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
661 struct ast_event_ie_val *ie_val;
662 struct ast_event *event;
664 event = ast_event_new(AST_EVENT_SUB,
665 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
666 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
667 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
672 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
673 switch (ie_val->ie_pltype) {
674 case AST_EVENT_IE_PLTYPE_UNKNOWN:
676 case AST_EVENT_IE_PLTYPE_EXISTS:
677 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
679 case AST_EVENT_IE_PLTYPE_UINT:
680 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
682 case AST_EVENT_IE_PLTYPE_BITFLAGS:
683 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
685 case AST_EVENT_IE_PLTYPE_STR:
686 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
688 case AST_EVENT_IE_PLTYPE_RAW:
689 ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
699 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
700 void ast_event_report_subs(const struct ast_event_sub *event_sub)
702 struct ast_event *event;
703 struct ast_event_sub *sub;
704 enum ast_event_type event_type = -1;
705 struct ast_event_ie_val *ie_val;
707 if (event_sub->type != AST_EVENT_SUB)
710 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
711 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
712 event_type = ie_val->payload.uint;
717 if (event_type == -1)
720 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
721 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
722 if (event_sub == sub) {
726 event = gen_sub_event(sub);
731 event_sub->cb(event, event_sub->userdata);
733 ast_event_destroy(event);
735 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
738 struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
739 ast_event_cb_t cb, void *userdata)
741 struct ast_event_sub *sub;
743 if (type < 0 || type >= AST_EVENT_TOTAL) {
744 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
748 if (!(sub = ast_calloc(1, sizeof(*sub)))) {
754 sub->userdata = userdata;
755 sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
760 int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
761 enum ast_event_ie_type ie_type, uint32_t unsigned_int)
763 struct ast_event_ie_val *ie_val;
765 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
769 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
773 ie_val->ie_type = ie_type;
774 ie_val->payload.uint = unsigned_int;
775 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
777 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
782 int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
783 enum ast_event_ie_type ie_type, uint32_t flags)
785 struct ast_event_ie_val *ie_val;
787 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
791 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
795 ie_val->ie_type = ie_type;
796 ie_val->payload.uint = flags;
797 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
799 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
804 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
805 enum ast_event_ie_type ie_type)
807 struct ast_event_ie_val *ie_val;
809 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
813 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
817 ie_val->ie_type = ie_type;
818 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
820 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
825 int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
826 enum ast_event_ie_type ie_type, const char *str)
828 struct ast_event_ie_val *ie_val;
830 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
834 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
838 ie_val->ie_type = ie_type;
839 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
841 if (!(ie_val->payload.str = ast_strdup(str))) {
846 if (ie_type == AST_EVENT_IE_DEVICE) {
847 char *uppertech = ast_strdupa(str);
848 ast_tech_to_upper(uppertech);
849 ie_val->payload.hash = ast_str_hash(uppertech);
851 ie_val->payload.hash = ast_str_hash(str);
854 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
859 int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
860 enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
862 struct ast_event_ie_val *ie_val;
864 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
868 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
872 ie_val->ie_type = ie_type;
873 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
874 ie_val->raw_datalen = raw_datalen;
876 if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
881 memcpy(ie_val->payload.raw, data, raw_datalen);
883 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
888 int ast_event_sub_activate(struct ast_event_sub *sub)
890 if (ast_event_check_subscriber(AST_EVENT_SUB,
891 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
892 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
893 struct ast_event *event;
895 event = gen_sub_event(sub);
896 if (event && ast_event_queue(event)) {
897 ast_event_destroy(event);
901 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
902 AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
903 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
908 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
909 const char *description, void *userdata, ...)
912 enum ast_event_ie_type ie_type;
913 struct ast_event_sub *sub;
915 if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
919 ast_copy_string(sub->description, description, sizeof(sub->description));
921 va_start(ap, userdata);
922 for (ie_type = va_arg(ap, enum ast_event_ie_type);
923 ie_type != AST_EVENT_IE_END;
924 ie_type = va_arg(ap, enum ast_event_ie_type))
926 enum ast_event_ie_pltype ie_pltype;
928 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
931 case AST_EVENT_IE_PLTYPE_UNKNOWN:
933 case AST_EVENT_IE_PLTYPE_UINT:
935 uint32_t unsigned_int = va_arg(ap, uint32_t);
936 ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
939 case AST_EVENT_IE_PLTYPE_BITFLAGS:
941 uint32_t unsigned_int = va_arg(ap, uint32_t);
942 ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
945 case AST_EVENT_IE_PLTYPE_STR:
947 const char *str = va_arg(ap, const char *);
948 ast_event_sub_append_ie_str(sub, ie_type, str);
951 case AST_EVENT_IE_PLTYPE_RAW:
953 void *data = va_arg(ap, void *);
954 size_t data_len = va_arg(ap, size_t);
955 ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
958 case AST_EVENT_IE_PLTYPE_EXISTS:
959 ast_event_sub_append_ie_exists(sub, ie_type);
965 ast_event_sub_activate(sub);
970 void ast_event_sub_destroy(struct ast_event_sub *sub)
972 struct ast_event_ie_val *ie_val;
974 while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
975 ast_event_ie_val_destroy(ie_val);
981 const char *ast_event_subscriber_get_description(struct ast_event_sub *sub)
983 return sub ? sub->description : NULL;
986 struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
988 struct ast_event *event;
990 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
991 AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
992 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
994 if (ast_event_check_subscriber(AST_EVENT_UNSUB,
995 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
996 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
998 event = ast_event_new(AST_EVENT_UNSUB,
999 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
1000 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
1001 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
1003 if (event && ast_event_queue(event)) {
1004 ast_event_destroy(event);
1008 ast_event_sub_destroy(sub);
1013 int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
1017 iterator->event_len = ast_event_get_size(event);
1018 iterator->event = event;
1019 if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
1020 iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
1022 iterator->ie = NULL;
1029 int ast_event_iterator_next(struct ast_event_iterator *iterator)
1031 iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
1032 return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
1035 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
1037 return ntohs(iterator->ie->ie_type);
1040 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
1042 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1045 uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
1047 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1050 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
1052 const struct ast_event_ie_str_payload *str_payload;
1054 str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
1056 return str_payload ? str_payload->str : NULL;
1059 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
1061 return iterator->ie->ie_payload;
1064 uint16_t ast_event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
1066 return ntohs(iterator->ie->ie_payload_len);
1069 enum ast_event_type ast_event_get_type(const struct ast_event *event)
1071 return ntohs(event->type);
1074 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
1076 const uint32_t *ie_val;
1078 ie_val = ast_event_get_ie_raw(event, ie_type);
1080 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1083 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
1085 const uint32_t *ie_val;
1087 ie_val = ast_event_get_ie_raw(event, ie_type);
1089 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1092 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
1094 const struct ast_event_ie_str_payload *str_payload;
1096 str_payload = ast_event_get_ie_raw(event, ie_type);
1098 return str_payload ? str_payload->hash : 0;
1101 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
1103 const struct ast_event_ie_str_payload *str_payload;
1105 str_payload = ast_event_get_ie_raw(event, ie_type);
1107 return str_payload ? str_payload->str : NULL;
1110 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
1112 struct ast_event_iterator iterator;
1115 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1116 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1117 return ast_event_iterator_get_ie_raw(&iterator);
1124 uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type)
1126 struct ast_event_iterator iterator;
1129 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1130 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1131 return ast_event_iterator_get_ie_raw_payload_len(&iterator);
1138 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
1141 struct ast_event_ie_str_payload *str_payload;
1144 payload_len = sizeof(*str_payload) + strlen(str);
1145 str_payload = alloca(payload_len);
1147 strcpy(str_payload->str, str);
1148 if (ie_type == AST_EVENT_IE_DEVICE) {
1149 char *uppertech = ast_strdupa(str);
1150 ast_tech_to_upper(uppertech);
1151 str_payload->hash = ast_str_hash(uppertech);
1153 str_payload->hash = ast_str_hash(str);
1156 return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
1159 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
1163 return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
1166 int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
1169 flags = htonl(flags);
1170 return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
1173 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
1174 const void *data, size_t data_len)
1176 struct ast_event_ie *ie;
1177 unsigned int extra_len;
1180 event_len = ntohs((*event)->event_len);
1181 extra_len = sizeof(*ie) + data_len;
1183 if (!(*event = ast_realloc(*event, event_len + extra_len))) {
1187 ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
1188 ie->ie_type = htons(ie_type);
1189 ie->ie_payload_len = htons(data_len);
1190 memcpy(ie->ie_payload, data, data_len);
1192 (*event)->event_len = htons(event_len + extra_len);
1197 struct ast_event *ast_event_new(enum ast_event_type type, ...)
1200 struct ast_event *event;
1201 enum ast_event_ie_type ie_type;
1202 struct ast_event_ie_val *ie_val;
1204 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
1207 if (type >= AST_EVENT_TOTAL) {
1208 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
1209 "type '%d'!\n", type);
1214 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1215 ie_type != AST_EVENT_IE_END;
1216 ie_type = va_arg(ap, enum ast_event_ie_type))
1218 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
1221 memset(ie_value, 0, sizeof(*ie_value));
1222 ie_value->ie_type = ie_type;
1223 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1224 switch (ie_value->ie_pltype) {
1225 case AST_EVENT_IE_PLTYPE_UINT:
1226 ie_value->payload.uint = va_arg(ap, uint32_t);
1229 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1230 ie_value->payload.uint = va_arg(ap, uint32_t);
1233 case AST_EVENT_IE_PLTYPE_STR:
1234 ie_value->payload.str = va_arg(ap, const char *);
1237 case AST_EVENT_IE_PLTYPE_RAW:
1239 void *data = va_arg(ap, void *);
1240 size_t datalen = va_arg(ap, size_t);
1241 ie_value->payload.raw = alloca(datalen);
1242 memcpy(ie_value->payload.raw, data, datalen);
1243 ie_value->raw_datalen = datalen;
1247 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1248 case AST_EVENT_IE_PLTYPE_EXISTS:
1253 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
1256 ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
1261 if (!(event = ast_calloc(1, sizeof(*event)))) {
1265 event->type = htons(type);
1266 event->event_len = htons(sizeof(*event));
1268 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
1269 switch (ie_val->ie_pltype) {
1270 case AST_EVENT_IE_PLTYPE_STR:
1271 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
1273 case AST_EVENT_IE_PLTYPE_UINT:
1274 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
1276 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1277 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
1279 case AST_EVENT_IE_PLTYPE_RAW:
1280 ast_event_append_ie_raw(&event, ie_val->ie_type,
1281 ie_val->payload.raw, ie_val->raw_datalen);
1283 case AST_EVENT_IE_PLTYPE_EXISTS:
1284 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1293 if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
1294 /* If the event is originating on this server, add the server's
1295 * entity ID to the event. */
1296 ast_event_append_eid(&event);
1302 int ast_event_append_eid(struct ast_event **event)
1304 return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
1305 &ast_eid_default, sizeof(ast_eid_default));
1308 void ast_event_destroy(struct ast_event *event)
1313 static void ast_event_ref_destroy(void *obj)
1315 struct ast_event_ref *event_ref = obj;
1317 ast_event_destroy(event_ref->event);
1320 static struct ast_event *ast_event_dup(const struct ast_event *event)
1322 struct ast_event *dup_event;
1325 event_len = ast_event_get_size(event);
1327 if (!(dup_event = ast_calloc(1, event_len))) {
1331 memcpy(dup_event, event, event_len);
1336 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
1339 enum ast_event_ie_type ie_type;
1340 struct ast_event *dup_event = NULL;
1341 struct ast_event_ref *cached_event_ref;
1342 struct ast_event *cache_arg_event;
1343 struct ast_event_ref tmp_event_ref = {
1346 struct ao2_container *container = NULL;
1348 if (type >= AST_EVENT_TOTAL) {
1349 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
1353 if (!(container = ast_event_cache[type].container)) {
1354 ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
1358 if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
1363 for (ie_type = va_arg(ap, enum ast_event_ie_type);
1364 ie_type != AST_EVENT_IE_END;
1365 ie_type = va_arg(ap, enum ast_event_ie_type))
1367 enum ast_event_ie_pltype ie_pltype;
1369 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1371 switch (ie_pltype) {
1372 case AST_EVENT_IE_PLTYPE_UINT:
1373 ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1375 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1376 ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1378 case AST_EVENT_IE_PLTYPE_STR:
1379 ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
1381 case AST_EVENT_IE_PLTYPE_RAW:
1383 void *data = va_arg(ap, void *);
1384 size_t datalen = va_arg(ap, size_t);
1385 ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
1388 case AST_EVENT_IE_PLTYPE_EXISTS:
1389 ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
1391 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1397 tmp_event_ref.event = cache_arg_event;
1399 cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
1401 ast_event_destroy(cache_arg_event);
1402 cache_arg_event = NULL;
1404 if (cached_event_ref) {
1405 dup_event = ast_event_dup(cached_event_ref->event);
1406 ao2_ref(cached_event_ref, -1);
1407 cached_event_ref = NULL;
1413 static struct ast_event_ref *alloc_event_ref(void)
1415 return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
1420 * \brief Update the given event cache with the new event.
1423 * \param cache Event cache container to update.
1424 * \param event New event to put in the cache.
1428 static void event_update_cache(struct ao2_container *cache, struct ast_event *event)
1430 struct ast_event_ref tmp_event_ref = {
1433 struct ast_event *dup_event;
1434 struct ast_event_ref *event_ref;
1436 /* Hold the cache container lock while it is updated. */
1439 /* Remove matches from the cache. */
1440 ao2_callback(cache, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
1441 ast_event_cmp, &tmp_event_ref);
1443 /* Save a copy of the event in the cache. */
1444 dup_event = ast_event_dup(event);
1446 event_ref = alloc_event_ref();
1448 event_ref->event = dup_event;
1449 ao2_link(cache, event_ref);
1450 ao2_ref(event_ref, -1);
1452 ast_event_destroy(dup_event);
1459 int ast_event_queue_and_cache(struct ast_event *event)
1461 struct ao2_container *container;
1463 container = ast_event_cache[ast_event_get_type(event)].container;
1465 ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
1467 event_update_cache(container, event);
1470 if (ast_event_queue(event)) {
1471 ast_event_destroy(event);
1476 static int handle_event(void *data)
1478 struct ast_event_ref *event_ref = data;
1479 struct ast_event_sub *sub;
1480 const enum ast_event_type event_types[] = {
1481 ntohs(event_ref->event->type),
1486 for (i = 0; i < ARRAY_LEN(event_types); i++) {
1487 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
1488 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
1489 struct ast_event_ie_val *ie_val;
1491 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
1492 if (!match_ie_val(event_ref->event, ie_val, NULL)) {
1493 /* The current subscription ie did not match an event ie. */
1498 /* The event did not match this subscription. */
1501 sub->cb(event_ref->event, sub->userdata);
1503 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
1506 ao2_ref(event_ref, -1);
1511 int ast_event_queue(struct ast_event *event)
1513 struct ast_event_ref *event_ref;
1514 uint16_t host_event_type;
1517 host_event_type = ntohs(event->type);
1520 if (host_event_type >= AST_EVENT_TOTAL) {
1521 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
1522 "type '%d'!\n", host_event_type);
1526 /* If nobody has subscribed to this event type, throw it away now */
1527 if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
1528 == AST_EVENT_SUB_NONE) {
1529 ast_event_destroy(event);
1533 if (!(event_ref = alloc_event_ref())) {
1537 event_ref->event = event;
1539 res = ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
1541 event_ref->event = NULL;
1542 ao2_ref(event_ref, -1);
1547 static int ast_event_hash_mwi(const void *obj, const int flags)
1549 const struct ast_event *event = obj;
1550 const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
1551 const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
1553 return ast_str_hash_add(context, ast_str_hash(mailbox));
1558 * \brief Hash function for AST_EVENT_DEVICE_STATE
1560 * \param[in] obj an ast_event
1561 * \param[in] flags unused
1563 * \return hash value
1565 static int ast_event_hash_devstate(const void *obj, const int flags)
1567 const struct ast_event *event = obj;
1569 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1574 * \brief Hash function for AST_EVENT_DEVICE_STATE_CHANGE
1576 * \param[in] obj an ast_event
1577 * \param[in] flags unused
1579 * \return hash value
1581 static int ast_event_hash_devstate_change(const void *obj, const int flags)
1583 const struct ast_event *event = obj;
1585 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
1588 static int ast_event_hash(const void *obj, const int flags)
1590 const struct ast_event_ref *event_ref;
1591 const struct ast_event *event;
1592 ao2_hash_fn *hash_fn;
1595 event = event_ref->event;
1597 if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
1601 return hash_fn(event, flags);
1606 * \brief Compare two events
1608 * \param[in] obj the first event, as an ast_event_ref
1609 * \param[in] arg the second event, as an ast_event_ref
1610 * \param[in] flags unused
1612 * \pre Both events must be the same type.
1613 * \pre The event type must be declared as a cached event type in ast_event_cache
1615 * \details This function takes two events, and determines if they are considered
1616 * equivalent. The values of information elements specified in the cache arguments
1617 * for the event type are used to determine if the events are equivalent.
1619 * \retval 0 No match
1620 * \retval CMP_MATCH The events are considered equivalent based on the cache arguments
1622 static int ast_event_cmp(void *obj, void *arg, int flags)
1624 struct ast_event_ref *event_ref, *event_ref2;
1625 struct ast_event *event, *event2;
1626 int res = CMP_MATCH;
1628 enum ast_event_ie_type *cache_args;
1631 event = event_ref->event;
1634 event2 = event_ref2->event;
1636 cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
1638 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1639 struct ast_event_ie_val ie_val = {
1640 .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
1641 .ie_type = cache_args[i],
1644 if (!match_ie_val(event, &ie_val, event2)) {
1653 static void dump_raw_ie(struct ast_event_iterator *i, struct ast_cli_args *a)
1656 enum ast_event_ie_type ie_type;
1657 const char *ie_type_name;
1659 ie_type = ast_event_iterator_get_ie_type(i);
1660 ie_type_name = ast_event_get_ie_type_name(ie_type);
1663 case AST_EVENT_IE_EID:
1664 ast_eid_to_str(eid_buf, sizeof(eid_buf), ast_event_iterator_get_ie_raw(i));
1665 ast_cli(a->fd, "%.30s: %s\n", ie_type_name, eid_buf);
1668 ast_cli(a->fd, "%s\n", ie_type_name);
1673 static int event_dump_cli(void *obj, void *arg, int flags)
1675 const struct ast_event_ref *event_ref = obj;
1676 const struct ast_event *event = event_ref->event;
1677 struct ast_cli_args *a = arg;
1678 struct ast_event_iterator i;
1680 if (ast_event_iterator_init(&i, event)) {
1681 ast_cli(a->fd, "Failed to initialize event iterator. :-(\n");
1685 ast_cli(a->fd, "Event: %s\n", ast_event_get_type_name(event));
1688 enum ast_event_ie_type ie_type;
1689 enum ast_event_ie_pltype ie_pltype;
1690 const char *ie_type_name;
1692 ie_type = ast_event_iterator_get_ie_type(&i);
1693 ie_type_name = ast_event_get_ie_type_name(ie_type);
1694 ie_pltype = ast_event_get_ie_pltype(ie_type);
1696 switch (ie_pltype) {
1697 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1698 case AST_EVENT_IE_PLTYPE_EXISTS:
1699 ast_cli(a->fd, "%s\n", ie_type_name);
1701 case AST_EVENT_IE_PLTYPE_STR:
1702 ast_cli(a->fd, "%.30s: %s\n", ie_type_name,
1703 ast_event_iterator_get_ie_str(&i));
1705 case AST_EVENT_IE_PLTYPE_UINT:
1706 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1707 ast_event_iterator_get_ie_uint(&i));
1709 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1710 ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1711 ast_event_iterator_get_ie_bitflags(&i));
1713 case AST_EVENT_IE_PLTYPE_RAW:
1717 } while (!ast_event_iterator_next(&i));
1719 ast_cli(a->fd, "\n");
1724 static char *event_dump_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1726 enum ast_event_type event_type;
1727 enum ast_event_ie_type *cache_args;
1732 e->command = "event dump cache";
1734 "Usage: event dump cache <event type>\n"
1735 " Dump all of the cached events for the given event type.\n"
1736 " This is primarily intended for debugging.\n";
1740 return ast_cli_complete(a->word, cached_event_types, a->n);
1747 if (a->argc != e->args + 1) {
1748 return CLI_SHOWUSAGE;
1751 if (ast_event_str_to_event_type(a->argv[e->args], &event_type)) {
1752 ast_cli(a->fd, "Invalid cached event type: '%s'\n", a->argv[e->args]);
1753 return CLI_SHOWUSAGE;
1756 if (!ast_event_cache[event_type].container) {
1757 ast_cli(a->fd, "Event type '%s' has no cache.\n", a->argv[e->args]);
1761 ast_cli(a->fd, "Event Type: %s\n", a->argv[e->args]);
1762 ast_cli(a->fd, "Cache Unique Keys:\n");
1763 cache_args = ast_event_cache[event_type].cache_args;
1764 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1765 ast_cli(a->fd, "--> %s\n", ast_event_get_ie_type_name(cache_args[i]));
1768 ast_cli(a->fd, "\n--- Begin Cache Dump ---\n\n");
1769 ao2_callback(ast_event_cache[event_type].container, OBJ_NODATA, event_dump_cli, a);
1770 ast_cli(a->fd, "--- End Cache Dump ---\n\n");
1775 static struct ast_cli_entry event_cli[] = {
1776 AST_CLI_DEFINE(event_dump_cache, "Dump the internal event cache (for debugging)"),
1779 int ast_event_init(void)
1783 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1784 AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
1787 for (i = 0; i < AST_EVENT_TOTAL; i++) {
1788 if (!ast_event_cache[i].hash_fn) {
1789 /* This event type is not cached. */
1793 if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
1794 ast_event_hash, ast_event_cmp))) {
1799 if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
1803 ast_cli_register_multiple(event_cli, ARRAY_LEN(event_cli));
1808 size_t ast_event_minimum_length(void)
1810 return sizeof(struct ast_event);