Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


event.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2008, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief Internal generic event system
22  *
23  * \author Russell Bryant <russell@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
33 
34 #include "asterisk/_private.h"
35 
36 #include "asterisk/event.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/dlinkedlists.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/unaligned.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/taskprocessor.h"
44 #include "asterisk/astobj2.h"
45 #include "asterisk/cli.h"
46 
48 
49 /*!
50  * \brief An event information element
51  *
52  * \note The format of this structure is important. Since these events may
53  * be sent directly over a network, changing this structure will break
54  * compatibility with older versions. However, at this point, this code
55  * has not made it into a release, so it is still fair game for change.
56  */
57 struct ast_event_ie {
58  enum ast_event_ie_type ie_type:16;
59  /*! Total length of the IE payload */
60  uint16_t ie_payload_len;
61  unsigned char ie_payload[0];
62 } __attribute__((packed));
63 
64 /*!
65  * \brief The payload for a string information element
66  */
68  /*! \brief A hash calculated with ast_str_hash(), to speed up comparisons */
69  uint32_t hash;
70  /*! \brief The actual string, null terminated */
71  char str[1];
72 } __attribute__((packed));
73 
74 /*!
75  * \brief An event
76  *
77  * An ast_event consists of an event header (this structure), and zero or
78  * more information elements defined by ast_event_ie.
79  *
80  * \note The format of this structure is important. Since these events may
81  * be sent directly over a network, changing this structure will break
82  * compatibility with older versions. However, at this point, this code
83  * has not made it into a release, so it is still fair game for change.
84  */
85 struct ast_event {
86  /*! Event type */
88  /*! Total length of the event */
89  uint16_t event_len:16;
90  /*! The data payload of the event, made up of information elements */
91  unsigned char payload[0];
92 } __attribute__((packed));
93 
94 
95 /*!
96  * \brief A holder for an event
97  *
98  * \details This struct used to have more of a purpose than it does now.
99  * It is used to hold events in the event cache. It can be completely removed
100  * if one of these two things is done:
101  * - ast_event gets changed such that it never has to be realloc()d
102  * - astobj2 is updated so that you can realloc() an astobj2 object
103  */
105  struct ast_event *event;
106 };
107 
112  union {
113  uint32_t uint;
114  struct {
115  uint32_t hash;
116  const char *str;
117  };
118  void *raw;
119  } payload;
120  size_t raw_datalen;
121 };
122 
123 /*! \brief Event subscription */
127  char description[64];
128  void *userdata;
129  uint32_t uniqueid;
132 };
133 
134 static uint32_t sub_uniqueid;
135 
136 /*! \brief Event subscriptions
137  * The event subscribers are indexed by which event they are subscribed to */
139 
140 static int ast_event_cmp(void *obj, void *arg, int flags);
141 static int ast_event_hash_mwi(const void *obj, const int flags);
142 static int ast_event_hash_devstate(const void *obj, const int flags);
143 static int ast_event_hash_devstate_change(const void *obj, const int flags);
144 
145 #ifdef LOW_MEMORY
146 #define NUM_CACHE_BUCKETS 17
147 #else
148 #define NUM_CACHE_BUCKETS 563
149 #endif
150 
151 #define MAX_CACHE_ARGS 8
152 
153 /*!
154  * \brief Event types that are kept in the cache.
155  */
156 static struct {
157  /*!
158  * \brief Container of cached events
159  *
160  * \details This gets allocated in ast_event_init() when Asterisk starts
161  * for the event types declared as using the cache.
162  */
164  /*! \brief Event type specific hash function */
166  /*!
167  * \brief Information Elements used for caching
168  *
169  * \details This array is the set of information elements that will be unique
170  * among all events in the cache for this event type. When a new event gets
171  * cached, a previous event with the same values for these information elements
172  * will be replaced.
173  */
176  [AST_EVENT_MWI] = {
177  .hash_fn = ast_event_hash_mwi,
178  .cache_args = { AST_EVENT_IE_MAILBOX, AST_EVENT_IE_CONTEXT },
179  },
181  .hash_fn = ast_event_hash_devstate,
182  .cache_args = { AST_EVENT_IE_DEVICE, },
183  },
186  .cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
187  },
188 };
189 
190 /*!
191  * \brief Names of cached event types, for CLI tab completion
192  *
193  * \note These names must match what is in the event_names array.
194  */
195 static const char * const cached_event_types[] = { "MWI", "DeviceState", "DeviceStateChange", NULL };
196 
197 /*!
198  * \brief Event Names
199  */
200 static const char * const event_names[AST_EVENT_TOTAL] = {
201  [AST_EVENT_ALL] = "All",
202  [AST_EVENT_CUSTOM] = "Custom",
203  [AST_EVENT_MWI] = "MWI",
204  [AST_EVENT_SUB] = "Subscription",
205  [AST_EVENT_UNSUB] = "Unsubscription",
206  [AST_EVENT_DEVICE_STATE] = "DeviceState",
207  [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
208  [AST_EVENT_CEL] = "CEL",
209  [AST_EVENT_SECURITY] = "Security",
210  [AST_EVENT_NETWORK_CHANGE] = "NetworkChange",
211 };
212 
213 /*!
214  * \brief IE payload types and names
215  */
216 static const struct ie_map {
217  enum ast_event_ie_pltype ie_pltype;
218  const char *name;
220  [AST_EVENT_IE_NEWMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
221  [AST_EVENT_IE_OLDMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
229  [AST_EVENT_IE_EID] = { AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
233  [AST_EVENT_IE_CEL_USEREVENT_NAME] = { AST_EVENT_IE_PLTYPE_UINT, "CELUserEventName" },
238  [AST_EVENT_IE_CEL_CHANNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELChanName" },
241  [AST_EVENT_IE_CEL_AMAFLAGS] = { AST_EVENT_IE_PLTYPE_STR, "CELAMAFlags" },
243  [AST_EVENT_IE_CEL_UNIQUEID] = { AST_EVENT_IE_PLTYPE_STR, "CELUniqueID" },
244  [AST_EVENT_IE_CEL_USERFIELD] = { AST_EVENT_IE_PLTYPE_STR, "CELUserField" },
246  [AST_EVENT_IE_CEL_CIDRDNIS] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDrdnis" },
249  [AST_EVENT_IE_CEL_LINKEDID] = { AST_EVENT_IE_PLTYPE_STR, "CELLinkedID" },
250  [AST_EVENT_IE_CEL_PEERACCT] = { AST_EVENT_IE_PLTYPE_STR, "CELPeerAcct" },
252  [AST_EVENT_IE_SECURITY_EVENT] = { AST_EVENT_IE_PLTYPE_STR, "SecurityEvent" },
260  [AST_EVENT_IE_LOCAL_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "LocalAddress" },
261  [AST_EVENT_IE_REMOTE_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "RemoteAddress" },
263  [AST_EVENT_IE_REQUEST_TYPE] = { AST_EVENT_IE_PLTYPE_STR, "RequestType" },
264  [AST_EVENT_IE_REQUEST_PARAMS] = { AST_EVENT_IE_PLTYPE_STR, "RequestParams" },
267  [AST_EVENT_IE_EXPECTED_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedAddress" },
268  [AST_EVENT_IE_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "Challenge" },
270  [AST_EVENT_IE_EXPECTED_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedResponse" },
272 };
273 
274 const char *ast_event_get_type_name(const struct ast_event *event)
275 {
276  enum ast_event_type type;
277 
278  type = ast_event_get_type(event);
279 
280  if (type < 0 || type >= ARRAY_LEN(event_names)) {
281  ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
282  return "";
283  }
284 
285  return event_names[type];
286 }
287 
288 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
289 {
290  int i;
291 
292  for (i = 0; i < ARRAY_LEN(event_names); i++) {
293  if (ast_strlen_zero(event_names[i]) || strcasecmp(event_names[i], str)) {
294  continue;
295  }
296 
297  *event_type = i;
298  return 0;
299  }
300 
301  return -1;
302 }
303 
305 {
306  if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
307  ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
308  return "";
309  }
310 
311  return ie_maps[ie_type].name;
312 }
313 
315 {
316  if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
317  ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
319  }
320 
321  return ie_maps[ie_type].ie_pltype;
322 }
323 
324 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
325 {
326  int i;
327 
328  for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
329  if (strcasecmp(ie_maps[i].name, str)) {
330  continue;
331  }
332 
333  *ie_type = i;
334  return 0;
335  }
336 
337  return -1;
338 }
339 
340 size_t ast_event_get_size(const struct ast_event *event)
341 {
342  size_t res;
343 
344  res = ntohs(event->event_len);
345 
346  return res;
347 }
348 
349 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
350 {
351  switch (ie_val->ie_pltype) {
353  ast_free((char *) ie_val->payload.str);
354  break;
356  ast_free(ie_val->payload.raw);
357  break;
362  break;
363  }
364 
365  ast_free(ie_val);
366 }
367 
368 /*! \brief Subscription event check list. */
371 };
372 
373 /*!
374  * \internal
375  * \brief Check if a subscription ie_val matches an event.
376  *
377  * \param sub_ie_val Subscripton IE value to check
378  * \param check_ie_vals event list to check against
379  *
380  * \retval 0 not matched
381  * \retval non-zero matched
382  */
383 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 {
385  const struct ast_event_ie_val *event_ie_val;
386  int res = 0;
387 
388  AST_LIST_TRAVERSE(&check_ie_vals->ie_vals, event_ie_val, entry) {
389  if (sub_ie_val->ie_type == event_ie_val->ie_type) {
390  break;
391  }
392  }
393  if (!event_ie_val) {
394  /* We did not find the event ie the subscriber cares about. */
395  return 0;
396  }
397 
398  if (sub_ie_val->ie_pltype != event_ie_val->ie_pltype) {
399  if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
400  /* The subscription only cares that this ie exists. */
401  return 1;
402  }
403  /* Payload types do not match. */
404  return 0;
405  }
406 
407  switch (sub_ie_val->ie_pltype) {
409  res = (sub_ie_val->payload.uint == event_ie_val->payload.uint);
410  break;
412  /*
413  * If the subscriber has requested *any* of the bitflags we are providing,
414  * then it's a match.
415  */
416  res = (sub_ie_val->payload.uint & event_ie_val->payload.uint);
417  break;
419  {
420  const char *substr = sub_ie_val->payload.str;
421  const char *estr = event_ie_val->payload.str;
422  if (sub_ie_val->ie_type == AST_EVENT_IE_DEVICE) {
423  substr = ast_tech_to_upper(ast_strdupa(substr));
424  estr = ast_tech_to_upper(ast_strdupa(estr));
425  }
426  res = !strcmp(substr, estr);
427  break;
428  }
430  res = (sub_ie_val->raw_datalen == event_ie_val->raw_datalen
431  && !memcmp(sub_ie_val->payload.raw, event_ie_val->payload.raw,
432  sub_ie_val->raw_datalen));
433  break;
435  /* Should never get here since check_ie_vals cannot have this type. */
436  break;
438  /*
439  * Should never be in a subscription event ie val list and
440  * check_ie_vals cannot have this type either.
441  */
442  break;
443  }
444 
445  return res;
446 }
447 
449 {
450  va_list ap;
453  struct ast_event_ie_val *ie_val;
454  struct ast_event_sub *sub;
455  struct ast_ev_check_list check_ie_vals = {
457  };
458  const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
459  int i;
460  int want_specific_event;/* TRUE if looking for subscribers wanting specific parameters. */
461 
462  if (type >= AST_EVENT_TOTAL) {
463  ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
464  return res;
465  }
466 
467  want_specific_event = 0;
468  va_start(ap, type);
469  for (ie_type = va_arg(ap, enum ast_event_ie_type);
470  ie_type != AST_EVENT_IE_END;
471  ie_type = va_arg(ap, enum ast_event_ie_type))
472  {
473  struct ast_event_ie_val *ie_value = ast_alloca(sizeof(*ie_value));
474  int insert = 0;
475 
476  memset(ie_value, 0, sizeof(*ie_value));
477  ie_value->ie_type = ie_type;
478  ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
479  switch (ie_value->ie_pltype) {
481  ie_value->payload.uint = va_arg(ap, uint32_t);
482  insert = 1;
483  break;
485  ie_value->payload.uint = va_arg(ap, uint32_t);
486  insert = 1;
487  break;
489  ie_value->payload.str = va_arg(ap, const char *);
490  insert = 1;
491  break;
493  {
494  void *data = va_arg(ap, void *);
495  size_t datalen = va_arg(ap, size_t);
496 
497  ie_value->payload.raw = ast_alloca(datalen);
498  memcpy(ie_value->payload.raw, data, datalen);
499  ie_value->raw_datalen = datalen;
500  insert = 1;
501  break;
502  }
505  /* Unsupported payload type. */
506  break;
507  }
508 
509  if (insert) {
510  want_specific_event = 1;
511  AST_LIST_INSERT_TAIL(&check_ie_vals.ie_vals, ie_value, entry);
512  } else {
513  ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
514  }
515  }
516  va_end(ap);
517 
518  for (i = 0; i < ARRAY_LEN(event_types); i++) {
519  AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
520  if (want_specific_event) {
521  AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
522  AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
523  if (!match_sub_ie_val_to_event(ie_val, &check_ie_vals)) {
524  /* The current subscription ie did not match an event ie. */
525  break;
526  }
527  }
528  if (!ie_val) {
529  /* Everything matched. A subscriber is looking for this event. */
530  break;
531  }
532  }
533  } else {
534  /* Just looking to see if there are ANY subscribers to the event type. */
535  sub = AST_RWLIST_FIRST(&ast_event_subs[event_types[i]]);
536  }
537  AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
538  if (sub) {
539  break;
540  }
541  }
542 
544 }
545 
546 /*!
547  * \internal
548  * \brief Check if an ie_val matches an event
549  *
550  * \param event event to check against
551  * \param ie_val IE value to check
552  * \param event2 optional event, if specified, the value to compare against will be pulled
553  * from this event instead of from the ie_val structure. In this case, only the IE
554  * type and payload type will be pulled from ie_val.
555  *
556  * \retval 0 not matched
557  * \retval non-zero matched
558  */
559 static int match_ie_val(const struct ast_event *event,
560  const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
561 {
562  switch (ie_val->ie_pltype) {
564  {
565  uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
566 
567  return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
568  }
569 
571  {
572  uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
573 
574  /*
575  * If the subscriber has requested *any* of the bitflags that this event provides,
576  * then it's a match.
577  */
578  return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
579  }
580 
582  {
583  const char *str;
584  uint32_t hash;
585 
586  hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
587  if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
588  return 0;
589  }
590 
591  str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
592  if (str) {
593  const char *e1str, *e2str;
594  e1str = ast_event_get_ie_str(event, ie_val->ie_type);
595  e2str = str;
596 
597  if (ie_val->ie_type == AST_EVENT_IE_DEVICE) {
598  e1str = ast_tech_to_upper(ast_strdupa(e1str));
599  e2str = ast_tech_to_upper(ast_strdupa(e2str));
600  }
601 
602  if (!strcmp(e1str, e2str)) {
603  return 1;
604  }
605  }
606 
607  return 0;
608  }
609 
611  {
612  const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
613  uint16_t ie_payload_len = event2 ? ast_event_get_ie_raw_payload_len(event2, ie_val->ie_type) : ie_val->raw_datalen;
614 
615  return (buf
616  && ie_payload_len == ast_event_get_ie_raw_payload_len(event, ie_val->ie_type)
617  && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_payload_len)) ? 1 : 0;
618  }
619 
621  {
622  return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
623  }
624 
626  return 0;
627  }
628 
629  return 0;
630 }
631 
632 static int dump_cache_cb(void *obj, void *arg, int flags)
633 {
634  const struct ast_event_ref *event_ref = obj;
635  const struct ast_event *event = event_ref->event;
636  const struct ast_event_sub *event_sub = arg;
637  struct ast_event_ie_val *ie_val = NULL;
638 
639  AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
640  if (!match_ie_val(event, ie_val, NULL)) {
641  break;
642  }
643  }
644 
645  if (!ie_val) {
646  /* All parameters were matched on this cache entry, so dump it */
647  event_sub->cb(event, event_sub->userdata);
648  }
649 
650  return 0;
651 }
652 
653 /*! \brief Dump the event cache for the subscribed event type */
655 {
656  ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
657  dump_cache_cb, (void *) event_sub);
658 }
659 
660 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
661 {
662  struct ast_event_ie_val *ie_val;
663  struct ast_event *event;
664 
670  if (!event)
671  return NULL;
672 
673  AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
674  switch (ie_val->ie_pltype) {
676  break;
679  break;
681  ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
682  break;
684  ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
685  break;
687  ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
688  break;
690  ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
691  break;
692  }
693  if (!event)
694  break;
695  }
696 
697  return event;
698 }
699 
700 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
702 {
703  struct ast_event *event;
704  struct ast_event_sub *sub;
705  enum ast_event_type event_type = -1;
706  struct ast_event_ie_val *ie_val;
707 
708  if (event_sub->type != AST_EVENT_SUB)
709  return;
710 
711  AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
712  if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
713  event_type = ie_val->payload.uint;
714  break;
715  }
716  }
717 
718  if (event_type == -1)
719  return;
720 
721  AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
722  AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
723  if (event_sub == sub) {
724  continue;
725  }
726 
727  event = gen_sub_event(sub);
728  if (!event) {
729  continue;
730  }
731 
732  event_sub->cb(event, event_sub->userdata);
733 
734  ast_event_destroy(event);
735  }
736  AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
737 }
738 
740  ast_event_cb_t cb, void *userdata)
741 {
742  struct ast_event_sub *sub;
743 
744  if (type < 0 || type >= AST_EVENT_TOTAL) {
745  ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
746  return NULL;
747  }
748 
749  if (!(sub = ast_calloc(1, sizeof(*sub)))) {
750  return NULL;
751  }
752 
753  sub->type = type;
754  sub->cb = cb;
755  sub->userdata = userdata;
756  sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
757 
758  return sub;
759 }
760 
762  enum ast_event_ie_type ie_type, uint32_t unsigned_int)
763 {
764  struct ast_event_ie_val *ie_val;
765 
766  if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
767  return -1;
768  }
769 
770  if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
771  return -1;
772  }
773 
774  ie_val->ie_type = ie_type;
775  ie_val->payload.uint = unsigned_int;
777 
778  AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
779 
780  return 0;
781 }
782 
784  enum ast_event_ie_type ie_type, uint32_t flags)
785 {
786  struct ast_event_ie_val *ie_val;
787 
788  if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
789  return -1;
790  }
791 
792  if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
793  return -1;
794  }
795 
796  ie_val->ie_type = ie_type;
797  ie_val->payload.uint = flags;
799 
800  AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
801 
802  return 0;
803 }
804 
807 {
808  struct ast_event_ie_val *ie_val;
809 
810  if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
811  return -1;
812  }
813 
814  if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
815  return -1;
816  }
817 
818  ie_val->ie_type = ie_type;
820 
821  AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
822 
823  return 0;
824 }
825 
827  enum ast_event_ie_type ie_type, const char *str)
828 {
829  struct ast_event_ie_val *ie_val;
830 
831  if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
832  return -1;
833  }
834 
835  if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
836  return -1;
837  }
838 
839  ie_val->ie_type = ie_type;
841 
842  if (!(ie_val->payload.str = ast_strdup(str))) {
843  ast_free(ie_val);
844  return -1;
845  }
846 
847  if (ie_type == AST_EVENT_IE_DEVICE) {
848  char *uppertech = ast_strdupa(str);
849  ast_tech_to_upper(uppertech);
850  ie_val->payload.hash = ast_str_hash(uppertech);
851  } else {
852  ie_val->payload.hash = ast_str_hash(str);
853  }
854 
855  AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
856 
857  return 0;
858 }
859 
861  enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
862 {
863  struct ast_event_ie_val *ie_val;
864 
865  if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
866  return -1;
867  }
868 
869  if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
870  return -1;
871  }
872 
873  ie_val->ie_type = ie_type;
875  ie_val->raw_datalen = raw_datalen;
876 
877  if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
878  ast_free(ie_val);
879  return -1;
880  }
881 
882  memcpy(ie_val->payload.raw, data, raw_datalen);
883 
884  AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
885 
886  return 0;
887 }
888 
890 {
894  struct ast_event *event;
895 
896  event = gen_sub_event(sub);
897  if (event && ast_event_queue(event)) {
898  ast_event_destroy(event);
899  }
900  }
901 
903  AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
905 
906  return 0;
907 }
908 
910  const char *description, void *userdata, ...)
911 {
912  va_list ap;
913  enum ast_event_ie_type ie_type;
914  struct ast_event_sub *sub;
915 
916  if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
917  return NULL;
918  }
919 
920  ast_copy_string(sub->description, description, sizeof(sub->description));
921 
922  va_start(ap, userdata);
923  for (ie_type = va_arg(ap, enum ast_event_ie_type);
924  ie_type != AST_EVENT_IE_END;
925  ie_type = va_arg(ap, enum ast_event_ie_type))
926  {
927  enum ast_event_ie_pltype ie_pltype;
928 
929  ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
930 
931  switch (ie_pltype) {
933  break;
935  {
936  uint32_t unsigned_int = va_arg(ap, uint32_t);
937  ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
938  break;
939  }
941  {
942  uint32_t unsigned_int = va_arg(ap, uint32_t);
943  ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
944  break;
945  }
947  {
948  const char *str = va_arg(ap, const char *);
949  ast_event_sub_append_ie_str(sub, ie_type, str);
950  break;
951  }
953  {
954  void *data = va_arg(ap, void *);
955  size_t data_len = va_arg(ap, size_t);
956  ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
957  break;
958  }
960  ast_event_sub_append_ie_exists(sub, ie_type);
961  break;
962  }
963  }
964  va_end(ap);
965 
967 
968  return sub;
969 }
970 
972 {
973  struct ast_event_ie_val *ie_val;
974 
975  while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
976  ast_event_ie_val_destroy(ie_val);
977  }
978 
979  ast_free(sub);
980 }
981 
983 {
984  return sub ? sub->description : NULL;
985 }
986 
988 {
989  struct ast_event *event;
990 
992  AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
994 
998 
1004  if (event && ast_event_queue(event)) {
1005  ast_event_destroy(event);
1006  }
1007  }
1008 
1009  ast_event_sub_destroy(sub);
1010 
1011  return NULL;
1012 }
1013 
1014 int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
1015 {
1016  int res = 0;
1017 
1018  iterator->event_len = ast_event_get_size(event);
1019  iterator->event = event;
1020  if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
1021  iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
1022  } else {
1023  iterator->ie = NULL;
1024  res = -1;
1025  }
1026 
1027  return res;
1028 }
1029 
1031 {
1032  iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
1033  return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
1034 }
1035 
1037 {
1038  return ntohs(iterator->ie->ie_type);
1039 }
1040 
1042 {
1043  return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1044 }
1045 
1047 {
1048  return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
1049 }
1050 
1052 {
1053  const struct ast_event_ie_str_payload *str_payload;
1054 
1055  str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
1056 
1057  return str_payload ? str_payload->str : NULL;
1058 }
1059 
1061 {
1062  return iterator->ie->ie_payload;
1063 }
1064 
1066 {
1067  return ntohs(iterator->ie->ie_payload_len);
1068 }
1069 
1071 {
1072  return ntohs(event->type);
1073 }
1074 
1075 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
1076 {
1077  const uint32_t *ie_val;
1078 
1079  ie_val = ast_event_get_ie_raw(event, ie_type);
1080 
1081  return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1082 }
1083 
1084 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
1085 {
1086  const uint32_t *ie_val;
1087 
1088  ie_val = ast_event_get_ie_raw(event, ie_type);
1089 
1090  return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
1091 }
1092 
1093 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
1094 {
1095  const struct ast_event_ie_str_payload *str_payload;
1096 
1097  str_payload = ast_event_get_ie_raw(event, ie_type);
1098 
1099  return str_payload ? str_payload->hash : 0;
1100 }
1101 
1102 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
1103 {
1104  const struct ast_event_ie_str_payload *str_payload;
1105 
1106  str_payload = ast_event_get_ie_raw(event, ie_type);
1107 
1108  return str_payload ? str_payload->str : NULL;
1109 }
1110 
1111 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
1112 {
1113  struct ast_event_iterator iterator;
1114  int res;
1115 
1116  for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1117  if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1118  return ast_event_iterator_get_ie_raw(&iterator);
1119  }
1120  }
1121 
1122  return NULL;
1123 }
1124 
1126 {
1127  struct ast_event_iterator iterator;
1128  int res;
1129 
1130  for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
1131  if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
1133  }
1134  }
1135 
1136  return 0;
1137 }
1138 
1140  const char *str)
1141 {
1142  struct ast_event_ie_str_payload *str_payload;
1143  size_t payload_len;
1144 
1145  payload_len = sizeof(*str_payload) + strlen(str);
1146  str_payload = ast_alloca(payload_len);
1147 
1148  strcpy(str_payload->str, str);
1149  if (ie_type == AST_EVENT_IE_DEVICE) {
1150  char *uppertech = ast_strdupa(str);
1151  ast_tech_to_upper(uppertech);
1152  str_payload->hash = ast_str_hash(uppertech);
1153  } else {
1154  str_payload->hash = ast_str_hash(str);
1155  }
1156 
1157  return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
1158 }
1159 
1160 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
1161  uint32_t data)
1162 {
1163  data = htonl(data);
1164  return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
1165 }
1166 
1168  uint32_t flags)
1169 {
1170  flags = htonl(flags);
1171  return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
1172 }
1173 
1174 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
1175  const void *data, size_t data_len)
1176 {
1177  struct ast_event_ie *ie;
1178  struct ast_event *old_event;
1179  unsigned int extra_len;
1180  uint16_t event_len;
1181 
1182  event_len = ntohs((*event)->event_len);
1183  extra_len = sizeof(*ie) + data_len;
1184 
1185  old_event = *event;
1186  *event = ast_realloc(*event, event_len + extra_len);
1187  if (!*event) {
1188  ast_free(old_event);
1189  return -1;
1190  }
1191 
1192  ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
1193  ie->ie_type = htons(ie_type);
1194  ie->ie_payload_len = htons(data_len);
1195  memcpy(ie->ie_payload, data, data_len);
1196 
1197  (*event)->event_len = htons(event_len + extra_len);
1198 
1199  return 0;
1200 }
1201 
1203 {
1204  va_list ap;
1205  struct ast_event *event;
1206  enum ast_event_ie_type ie_type;
1207  struct ast_event_ie_val *ie_val;
1208  int has_ie = 0;
1210 
1211  /* Invalid type */
1212  if (type >= AST_EVENT_TOTAL) {
1213  ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
1214  "type '%u'!\n", type);
1215  return NULL;
1216  }
1217 
1218  va_start(ap, type);
1219  for (ie_type = va_arg(ap, enum ast_event_ie_type);
1220  ie_type != AST_EVENT_IE_END;
1221  ie_type = va_arg(ap, enum ast_event_ie_type))
1222  {
1223  struct ast_event_ie_val *ie_value = ast_alloca(sizeof(*ie_value));
1224  int insert = 0;
1225 
1226  memset(ie_value, 0, sizeof(*ie_value));
1227  ie_value->ie_type = ie_type;
1228  ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1229  switch (ie_value->ie_pltype) {
1231  ie_value->payload.uint = va_arg(ap, uint32_t);
1232  insert = 1;
1233  break;
1235  ie_value->payload.uint = va_arg(ap, uint32_t);
1236  insert = 1;
1237  break;
1239  ie_value->payload.str = va_arg(ap, const char *);
1240  insert = 1;
1241  break;
1243  {
1244  void *data = va_arg(ap, void *);
1245  size_t datalen = va_arg(ap, size_t);
1246  ie_value->payload.raw = ast_alloca(datalen);
1247  memcpy(ie_value->payload.raw, data, datalen);
1248  ie_value->raw_datalen = datalen;
1249  insert = 1;
1250  break;
1251  }
1254  break;
1255  }
1256 
1257  if (insert) {
1258  AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
1259  has_ie = 1;
1260  } else {
1261  ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
1262  }
1263  }
1264  va_end(ap);
1265 
1266  if (!(event = ast_calloc(1, sizeof(*event)))) {
1267  return NULL;
1268  }
1269 
1270  event->type = htons(type);
1271  event->event_len = htons(sizeof(*event));
1272 
1273  AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
1274  switch (ie_val->ie_pltype) {
1276  ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
1277  break;
1279  ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
1280  break;
1282  ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
1283  break;
1285  ast_event_append_ie_raw(&event, ie_val->ie_type,
1286  ie_val->payload.raw, ie_val->raw_datalen);
1287  break;
1290  break;
1291  }
1292 
1293  /* realloc inside one of the append functions failed */
1294  if (!event) {
1295  return NULL;
1296  }
1297  }
1298 
1299  if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
1300  /* If the event is originating on this server, add the server's
1301  * entity ID to the event. */
1302  ast_event_append_eid(&event);
1303  }
1304 
1305  return event;
1306 }
1307 
1308 int ast_event_append_eid(struct ast_event **event)
1309 {
1311  &ast_eid_default, sizeof(ast_eid_default));
1312 }
1313 
1314 void ast_event_destroy(struct ast_event *event)
1315 {
1316  ast_free(event);
1317 }
1318 
1319 static void ast_event_ref_destroy(void *obj)
1320 {
1321  struct ast_event_ref *event_ref = obj;
1322 
1323  ast_event_destroy(event_ref->event);
1324 }
1325 
1326 static struct ast_event *ast_event_dup(const struct ast_event *event)
1327 {
1328  struct ast_event *dup_event;
1329  uint16_t event_len;
1330 
1331  event_len = ast_event_get_size(event);
1332 
1333  if (!(dup_event = ast_calloc(1, event_len))) {
1334  return NULL;
1335  }
1336 
1337  memcpy(dup_event, event, event_len);
1338 
1339  return dup_event;
1340 }
1341 
1343 {
1344  va_list ap;
1345  enum ast_event_ie_type ie_type;
1346  struct ast_event *dup_event = NULL;
1347  struct ast_event_ref *cached_event_ref;
1348  struct ast_event *cache_arg_event;
1349  struct ast_event_ref tmp_event_ref = {
1350  .event = NULL,
1351  };
1352  struct ao2_container *container = NULL;
1353 
1354  if (type >= AST_EVENT_TOTAL) {
1355  ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
1356  return NULL;
1357  }
1358 
1359  if (!(container = ast_event_cache[type].container)) {
1360  ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
1361  return NULL;
1362  }
1363 
1364  if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
1365  return NULL;
1366  }
1367 
1368  va_start(ap, type);
1369  for (ie_type = va_arg(ap, enum ast_event_ie_type);
1370  ie_type != AST_EVENT_IE_END;
1371  ie_type = va_arg(ap, enum ast_event_ie_type))
1372  {
1373  enum ast_event_ie_pltype ie_pltype;
1374 
1375  ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
1376 
1377  switch (ie_pltype) {
1379  ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1380  break;
1382  ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
1383  break;
1385  ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
1386  break;
1388  {
1389  void *data = va_arg(ap, void *);
1390  size_t datalen = va_arg(ap, size_t);
1391  ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
1392  break;
1393  }
1395  ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
1396  break;
1398  break;
1399  }
1400  }
1401  va_end(ap);
1402 
1403  tmp_event_ref.event = cache_arg_event;
1404 
1405  cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
1406 
1407  ast_event_destroy(cache_arg_event);
1408  cache_arg_event = NULL;
1409 
1410  if (cached_event_ref) {
1411  dup_event = ast_event_dup(cached_event_ref->event);
1412  ao2_ref(cached_event_ref, -1);
1413  cached_event_ref = NULL;
1414  }
1415 
1416  return dup_event;
1417 }
1418 
1419 static struct ast_event_ref *alloc_event_ref(void)
1420 {
1421  return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
1422 }
1423 
1424 /*!
1425  * \internal
1426  * \brief Update the given event cache with the new event.
1427  * \since 1.8
1428  *
1429  * \param cache Event cache container to update.
1430  * \param event New event to put in the cache.
1431  *
1432  * \return Nothing
1433  */
1435 {
1436  struct ast_event_ref tmp_event_ref = {
1437  .event = event,
1438  };
1439  struct ast_event *dup_event;
1440  struct ast_event_ref *event_ref;
1441 
1442  /* Hold the cache container lock while it is updated. */
1443  ao2_lock(cache);
1444 
1445  /* Remove matches from the cache. */
1447  ast_event_cmp, &tmp_event_ref);
1448 
1449  /* Save a copy of the event in the cache. */
1450  dup_event = ast_event_dup(event);
1451  if (dup_event) {
1452  event_ref = alloc_event_ref();
1453  if (event_ref) {
1454  event_ref->event = dup_event;
1455  ao2_link(cache, event_ref);
1456  ao2_ref(event_ref, -1);
1457  } else {
1458  ast_event_destroy(dup_event);
1459  }
1460  }
1461 
1462  ao2_unlock(cache);
1463 }
1464 
1466 {
1467  struct ao2_container *container;
1468 
1469  container = ast_event_cache[ast_event_get_type(event)].container;
1470  if (!container) {
1471  ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
1472  } else {
1473  event_update_cache(container, event);
1474  }
1475 
1476  if (ast_event_queue(event)) {
1477  ast_event_destroy(event);
1478  }
1479  return 0;
1480 }
1481 
1482 static int handle_event(void *data)
1483 {
1484  struct ast_event_ref *event_ref = data;
1485  struct ast_event_sub *sub;
1486  const enum ast_event_type event_types[] = {
1487  ntohs(event_ref->event->type),
1489  };
1490  int i;
1491 
1492  for (i = 0; i < ARRAY_LEN(event_types); i++) {
1493  AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
1494  AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
1495  struct ast_event_ie_val *ie_val;
1496 
1497  AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
1498  if (!match_ie_val(event_ref->event, ie_val, NULL)) {
1499  /* The current subscription ie did not match an event ie. */
1500  break;
1501  }
1502  }
1503  if (ie_val) {
1504  /* The event did not match this subscription. */
1505  continue;
1506  }
1507  sub->cb(event_ref->event, sub->userdata);
1508  }
1509  AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
1510  }
1511 
1512  ao2_ref(event_ref, -1);
1513 
1514  return 0;
1515 }
1516 
1517 int ast_event_queue(struct ast_event *event)
1518 {
1519  struct ast_event_ref *event_ref;
1520  uint16_t host_event_type;
1521  int res;
1522 
1523  host_event_type = ntohs(event->type);
1524 
1525  /* Invalid type */
1526  if (host_event_type >= AST_EVENT_TOTAL) {
1527  ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
1528  "type '%d'!\n", host_event_type);
1529  return -1;
1530  }
1531 
1532  /* If nobody has subscribed to this event type, throw it away now */
1533  if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
1534  == AST_EVENT_SUB_NONE) {
1535  ast_event_destroy(event);
1536  return 0;
1537  }
1538 
1539  if (!(event_ref = alloc_event_ref())) {
1540  return -1;
1541  }
1542 
1543  event_ref->event = event;
1544 
1545  res = ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
1546  if (res) {
1547  event_ref->event = NULL;
1548  ao2_ref(event_ref, -1);
1549  }
1550  return res;
1551 }
1552 
1553 static int ast_event_hash_mwi(const void *obj, const int flags)
1554 {
1555  const struct ast_event *event = obj;
1556  const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
1557  const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
1558 
1559  return ast_str_hash_add(context, ast_str_hash(mailbox));
1560 }
1561 
1562 /*!
1563  * \internal
1564  * \brief Hash function for AST_EVENT_DEVICE_STATE
1565  *
1566  * \param[in] obj an ast_event
1567  * \param[in] flags unused
1568  *
1569  * \return hash value
1570  */
1571 static int ast_event_hash_devstate(const void *obj, const int flags)
1572 {
1573  const struct ast_event *event = obj;
1574 
1576 }
1577 
1578 /*!
1579  * \internal
1580  * \brief Hash function for AST_EVENT_DEVICE_STATE_CHANGE
1581  *
1582  * \param[in] obj an ast_event
1583  * \param[in] flags unused
1584  *
1585  * \return hash value
1586  */
1587 static int ast_event_hash_devstate_change(const void *obj, const int flags)
1588 {
1589  const struct ast_event *event = obj;
1590 
1592 }
1593 
1594 static int ast_event_hash(const void *obj, const int flags)
1595 {
1596  const struct ast_event_ref *event_ref;
1597  const struct ast_event *event;
1599 
1600  event_ref = obj;
1601  event = event_ref->event;
1602 
1603  if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
1604  return 0;
1605  }
1606 
1607  return hash_fn(event, flags);
1608 }
1609 
1610 /*!
1611  * \internal
1612  * \brief Compare two events
1613  *
1614  * \param[in] obj the first event, as an ast_event_ref
1615  * \param[in] arg the second event, as an ast_event_ref
1616  * \param[in] flags unused
1617  *
1618  * \pre Both events must be the same type.
1619  * \pre The event type must be declared as a cached event type in ast_event_cache
1620  *
1621  * \details This function takes two events, and determines if they are considered
1622  * equivalent. The values of information elements specified in the cache arguments
1623  * for the event type are used to determine if the events are equivalent.
1624  *
1625  * \retval 0 No match
1626  * \retval CMP_MATCH The events are considered equivalent based on the cache arguments
1627  */
1628 static int ast_event_cmp(void *obj, void *arg, int flags)
1629 {
1630  struct ast_event_ref *event_ref, *event_ref2;
1631  struct ast_event *event, *event2;
1632  int res = CMP_MATCH;
1633  int i;
1635 
1636  event_ref = obj;
1637  event = event_ref->event;
1638 
1639  event_ref2 = arg;
1640  event2 = event_ref2->event;
1641 
1642  cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
1643 
1644  for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1645  struct ast_event_ie_val ie_val = {
1646  .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
1647  .ie_type = cache_args[i],
1648  };
1649 
1650  if (!match_ie_val(event, &ie_val, event2)) {
1651  res = 0;
1652  break;
1653  }
1654  }
1655 
1656  return res;
1657 }
1658 
1659 static void dump_raw_ie(struct ast_event_iterator *i, struct ast_cli_args *a)
1660 {
1661  char eid_buf[32];
1663  const char *ie_type_name;
1664 
1665  ie_type = ast_event_iterator_get_ie_type(i);
1666  ie_type_name = ast_event_get_ie_type_name(ie_type);
1667 
1668  switch (ie_type) {
1669  case AST_EVENT_IE_EID:
1670  ast_eid_to_str(eid_buf, sizeof(eid_buf), ast_event_iterator_get_ie_raw(i));
1671  ast_cli(a->fd, "%.30s: %s\n", ie_type_name, eid_buf);
1672  break;
1673  default:
1674  ast_cli(a->fd, "%s\n", ie_type_name);
1675  break;
1676  }
1677 }
1678 
1679 static int event_dump_cli(void *obj, void *arg, int flags)
1680 {
1681  const struct ast_event_ref *event_ref = obj;
1682  const struct ast_event *event = event_ref->event;
1683  struct ast_cli_args *a = arg;
1684  struct ast_event_iterator i;
1685 
1686  if (ast_event_iterator_init(&i, event)) {
1687  ast_cli(a->fd, "Failed to initialize event iterator. :-(\n");
1688  return 0;
1689  }
1690 
1691  ast_cli(a->fd, "Event: %s\n", ast_event_get_type_name(event));
1692 
1693  do {
1694  enum ast_event_ie_type ie_type;
1695  enum ast_event_ie_pltype ie_pltype;
1696  const char *ie_type_name;
1697 
1698  ie_type = ast_event_iterator_get_ie_type(&i);
1699  ie_type_name = ast_event_get_ie_type_name(ie_type);
1700  ie_pltype = ast_event_get_ie_pltype(ie_type);
1701 
1702  switch (ie_pltype) {
1705  ast_cli(a->fd, "%s\n", ie_type_name);
1706  break;
1708  ast_cli(a->fd, "%.30s: %s\n", ie_type_name,
1710  break;
1712  ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1714  break;
1716  ast_cli(a->fd, "%.30s: %u\n", ie_type_name,
1718  break;
1720  dump_raw_ie(&i, a);
1721  break;
1722  }
1723  } while (!ast_event_iterator_next(&i));
1724 
1725  ast_cli(a->fd, "\n");
1726 
1727  return 0;
1728 }
1729 
1730 static char *event_dump_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1731 {
1732  enum ast_event_type event_type;
1734  int i;
1735 
1736  switch (cmd) {
1737  case CLI_INIT:
1738  e->command = "event dump cache";
1739  e->usage =
1740  "Usage: event dump cache <event type>\n"
1741  " Dump all of the cached events for the given event type.\n"
1742  " This is primarily intended for debugging.\n";
1743  return NULL;
1744  case CLI_GENERATE:
1745  if (a->pos == 3) {
1746  return ast_cli_complete(a->word, cached_event_types, a->n);
1747  }
1748  return NULL;
1749  case CLI_HANDLER:
1750  break;
1751  }
1752 
1753  if (a->argc != e->args + 1) {
1754  return CLI_SHOWUSAGE;
1755  }
1756 
1757  if (ast_event_str_to_event_type(a->argv[e->args], &event_type)) {
1758  ast_cli(a->fd, "Invalid cached event type: '%s'\n", a->argv[e->args]);
1759  return CLI_SHOWUSAGE;
1760  }
1761 
1762  if (!ast_event_cache[event_type].container) {
1763  ast_cli(a->fd, "Event type '%s' has no cache.\n", a->argv[e->args]);
1764  return CLI_SUCCESS;
1765  }
1766 
1767  ast_cli(a->fd, "Event Type: %s\n", a->argv[e->args]);
1768  ast_cli(a->fd, "Cache Unique Keys:\n");
1769  cache_args = ast_event_cache[event_type].cache_args;
1770  for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
1771  ast_cli(a->fd, "--> %s\n", ast_event_get_ie_type_name(cache_args[i]));
1772  }
1773 
1774  ast_cli(a->fd, "\n--- Begin Cache Dump ---\n\n");
1775  ao2_callback(ast_event_cache[event_type].container, OBJ_NODATA, event_dump_cli, a);
1776  ast_cli(a->fd, "--- End Cache Dump ---\n\n");
1777 
1778  return CLI_SUCCESS;
1779 }
1780 
1781 static struct ast_cli_entry event_cli[] = {
1782  AST_CLI_DEFINE(event_dump_cache, "Dump the internal event cache (for debugging)"),
1783 };
1784 
1785 /*! \internal \brief Clean up resources on Asterisk shutdown */
1786 static void event_shutdown(void)
1787 {
1788  struct ast_event_sub *sub;
1789  int i;
1790 
1792 
1793  if (event_dispatcher) {
1794  event_dispatcher = ast_taskprocessor_unreference(event_dispatcher);
1795  }
1796 
1797  /* Remove any remaining subscriptions. Note that we can't just call
1798  * unsubscribe, as it will attempt to lock the subscription list
1799  * as well */
1800  for (i = 0; i < AST_EVENT_TOTAL; i++) {
1802  while ((sub = AST_RWDLLIST_REMOVE_HEAD(&ast_event_subs[i], entry))) {
1803  ast_event_sub_destroy(sub);
1804  }
1807  }
1808 
1809  for (i = 0; i < AST_EVENT_TOTAL; i++) {
1810  if (!ast_event_cache[i].hash_fn) {
1811  continue;
1812  }
1813  if (ast_event_cache[i].container) {
1814  ao2_ref(ast_event_cache[i].container, -1);
1815  }
1816  }
1817 }
1818 
1820 {
1821  int i;
1822 
1823  for (i = 0; i < AST_EVENT_TOTAL; i++) {
1825  }
1826 
1827  for (i = 0; i < AST_EVENT_TOTAL; i++) {
1828  if (!ast_event_cache[i].hash_fn) {
1829  /* This event type is not cached. */
1830  continue;
1831  }
1832 
1835  goto event_init_cleanup;
1836  }
1837  }
1838 
1839  if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
1840  goto event_init_cleanup;
1841  }
1842 
1844 
1846 
1847  return 0;
1848 
1849 event_init_cleanup:
1850  event_shutdown();
1851  return -1;
1852 }
1853 
1855 {
1856  return sizeof(struct ast_event);
1857 }
#define NUM_CACHE_BUCKETS
Definition: event.c:148
int ast_event_sub_append_ie_str(struct ast_event_sub *sub, enum ast_event_ie_type ie_type, const char *str)
Append a string parameter to a subscription.
Definition: event.c:826
#define AST_RWDLLIST_HEAD(name, type)
Defines a structure to be used to hold a read/write list of specified type.
Definition: dlinkedlists.h:184
Channel Event CID name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:151
A holder for an event.
Definition: event.c:104
Channel Event app name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:181
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
An event.
Definition: event.c:85
int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
Convert a string to an IE type.
Definition: event.c:324
Asterisk locking-related definitions:
struct ast_event_ie * ie
Definition: event_defs.h:338
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub, enum ast_event_ie_type ie_type, uint32_t flags)
Append a bitflags parameter to a subscription.
Definition: event.c:783
uint32_t hash
A hash calculated with ast_str_hash(), to speed up comparisons.
Definition: event.c:69
#define AST_RWDLLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: dlinkedlists.h:797
static int event_dump_cli(void *obj, void *arg, int flags)
Definition: event.c:1679
int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
Convert a string into an event type.
Definition: event.c:288
uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a bitflags payload.
Definition: event.c:1084
enum ast_event_type ast_event_get_type(const struct ast_event *event)
Get the type for an event.
Definition: event.c:1070
#define ast_strdup(a)
Definition: astmm.h:109
Definition: ast_expr2.c:325
Channel Event extra data Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:253
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Definition: netsock.c:222
ast_event_ie_pltype
Payload types for event information elements.
Definition: event_defs.h:299
enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type event_type,...)
Check if subscribers exist.
Definition: event.c:448
enum ast_event_ie_pltype ie_pltype
Definition: event.c:111
int ast_taskprocessor_push(struct ast_taskprocessor *tps, int(*task_exe)(void *datap), void *datap)
Push a task into the specified taskprocessor queue and signal the taskprocessor thread.
uint16_t ie_payload_len
Definition: event.c:58
struct ast_event_ie_val::@256 entry
int ast_event_queue_and_cache(struct ast_event *event)
Queue and cache an event.
Definition: event.c:1465
void ast_event_report_subs(const struct ast_event_sub *sub)
Report current subscriptions to a subscription subscriber.
Definition: event.c:701
descriptor for a cli entry.
Definition: cli.h:165
const int argc
Definition: cli.h:154
#define LOG_WARNING
Definition: logger.h:144
struct ast_event_sub * event_sub
Definition: devicestate.c:201
union ast_event_ie_val::@257 payload
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
struct ast_taskprocessor * ast_taskprocessor_get(const char *name, enum ast_tps_options create)
Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary...
Must be the last IE value +1.
Definition: event_defs.h:293
Channel Event channel name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:175
Hint that someone cares that an IE exists Used by: AST_EVENT_SUB Payload type: UINT (ast_event_ie_typ...
Definition: event_defs.h:101
struct ast_event * event
Definition: event.c:105
Subscription event check list.
Definition: event.c:369
Channel Event UniqueID Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:205
Channel Event context name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:169
ast_event_subscriber_res
Results for checking for subscribers.
Definition: event_defs.h:318
Channel Event app args/data Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:187
Channel Event peeraccount Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:247
Channel Event Time (micro-seconds) Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:139
Channel Event CID dnid Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:229
Definition: cli.h:146
const char * ast_event_subscriber_get_description(struct ast_event_sub *sub)
Get description for a subscription.
Definition: event.c:982
Description Used by: AST_EVENT_SUB, AST_EVENT_UNSUB Payload type: STR.
Definition: event_defs.h:259
struct ast_event * ast_event_get_cached(enum ast_event_type,...)
Retrieve an event from the cache.
Definition: event.c:1342
size_t raw_datalen
Definition: event.c:120
#define AST_RWDLLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Definition: dlinkedlists.h:827
int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t bitflags)
Append an information element that has a bitflags payload.
Definition: event.c:1167
Number of new messages Used by: AST_EVENT_MWI Payload type: UINT.
Definition: event_defs.h:71
Number of Used by: AST_EVENT_MWI Payload type: UINT.
Definition: event_defs.h:77
uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
Get the value of the current IE in the iterator as a bitflags payload.
Definition: event.c:1046
enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
Get the payload type for a given information element type.
Definition: event.c:314
#define ao2_unlock(a)
Definition: astobj2.h:497
#define AST_RWDLLIST_ENTRY
Definition: dlinkedlists.h:412
const char * str
Definition: app_jack.c:144
char str[1]
The actual string, null terminated.
Definition: event.c:71
enum ast_event_ie_type cache_args[MAX_CACHE_ARGS]
Information Elements used for caching.
Definition: event.c:174
supposed to be an opaque type
Definition: event_defs.h:335
void ast_event_sub_destroy(struct ast_event_sub *sub)
Destroy an allocated subscription.
Definition: event.c:971
static int dump_cache_cb(void *obj, void *arg, int flags)
Definition: event.c:632
uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the hash for the string payload of an IE.
Definition: event.c:1093
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
static const char *const event_names[AST_EVENT_TOTAL]
Event Names.
Definition: event.c:200
struct ao2_container * container
The asterisk data main content structure.
Definition: data.c:177
struct ast_ev_check_list::@261 ie_vals
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
Definition: lock.h:603
Channel Event Type Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:127
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
Definition: event_defs.h:266
uint32_t uniqueid
Definition: event.c:129
Utility functions.
const char * str
Definition: event.c:116
int args
This gets set in ast_cli_register()
Definition: cli.h:179
static struct ast_event_sub_list ast_event_subs[AST_EVENT_TOTAL]
Channel Event Time (seconds) Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:133
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: cli.c:1535
Channel Event CID num Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:157
static force_inline int ast_str_hash_add(const char *str, int hash)
Compute a hash value on a string.
Definition: strings.h:974
Channel Event extension name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:163
int ast_event_queue(struct ast_event *event)
Queue an event.
Definition: event.c:1517
ast_event_ie_type
Event Information Element types.
Definition: event_defs.h:62
Handle unaligned data access.
ast_event_type
Event types.
Definition: event_defs.h:30
Context IE Used by AST_EVENT_MWI Payload type: str.
Definition: event_defs.h:121
#define MAX_CACHE_ARGS
Definition: event.c:151
Event subscriptions The event subscribers are indexed by which event they are subscribed to...
Definition: event.c:138
A set of macros to manage doubly-linked lists.
void(* ast_event_cb_t)(const struct ast_event *event, void *userdata)
Subscriber event callback type.
Definition: event.h:74
static struct ast_event * ast_event_dup(const struct ast_event *event)
Definition: event.c:1326
#define AST_RWDLLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: dlinkedlists.h:134
const int fd
Definition: cli.h:153
An event information element.
Definition: event.c:57
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const int n
Definition: cli.h:159
void * userdata
Definition: event.c:128
static int ast_event_cmp(void *obj, void *arg, int flags)
Definition: event.c:1628
const char * name
Definition: event.c:218
#define ao2_ref(o, delta)
Definition: astobj2.h:472
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
#define ao2_lock(a)
Definition: astobj2.h:488
static struct ie_map ie_maps[AST_EVENT_IE_TOTAL]
Channel Event Userfield Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:211
A set of macros to manage forward-linked lists.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
static int ast_event_hash_devstate(const void *obj, const int flags)
Definition: event.c:1571
Channel Event CID RDNIS field Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:223
static unsigned int get_unaligned_uint32(const void *p)
Definition: unaligned.h:38
static char * event_dump_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: event.c:1730
Event non-cachability flag Used by: All events Payload type: UINT.
Definition: event_defs.h:291
int ast_event_sub_append_ie_raw(struct ast_event_sub *sub, enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
Append a raw parameter to a subscription.
Definition: event.c:860
const void * ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a raw payload.
Definition: event.c:1111
static int match_ie_val(const struct ast_event *event, const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
Definition: event.c:559
Event type Used by: AST_EVENT_SUB, AST_EVENT_UNSUB Payload type: UINT.
Definition: event_defs.h:95
const char *const * argv
Definition: cli.h:155
void ast_event_dump_cache(const struct ast_event_sub *event_sub)
Dump the event cache for the subscriber.
Definition: event.c:654
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:224
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
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)
Definition: event.c:383
#define AST_LIST_HEAD_NOLOCK_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:345
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
char * ast_tech_to_upper(char *dev_str)
Convert the tech portion of a device string to upper case.
Definition: strings.h:939
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:1174
ast_event_cb_t cb
Definition: event.c:126
enum ast_event_ie_type ie_type
Definition: event.c:110
Channel Event User Event Name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:145
#define CLI_SHOWUSAGE
Definition: cli.h:44
static uint32_t sub_uniqueid
Definition: event.c:134
#define AST_RWDLLIST_REMOVE_HEAD
Definition: dlinkedlists.h:988
static struct ast_taskprocessor * event_dispatcher
Definition: event.c:47
Event subscription.
Definition: event.c:124
int ast_event_sub_activate(struct ast_event_sub *sub)
Activate a dynamically built subscription.
Definition: event.c:889
int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type, const char *str)
Append an information element that has a string payload.
Definition: event.c:1139
#define AST_RWDLLIST_INSERT_TAIL
Definition: dlinkedlists.h:937
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int ast_event_hash_mwi(const void *obj, const int flags)
Definition: event.c:1553
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
static struct @255 ast_event_cache[AST_EVENT_TOTAL]
Event types that are kept in the cache.
static void ast_event_ref_destroy(void *obj)
Definition: event.c:1319
static struct ast_event_ref * alloc_event_ref(void)
Definition: event.c:1419
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
int ast_event_init(void)
Definition: event.c:1819
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
void * raw
Definition: event.c:118
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
char * command
Definition: cli.h:180
size_t ast_event_minimum_length(void)
Get the minimum length of an ast_event.
Definition: event.c:1854
struct ast_event_sub::@260 ie_vals
Channel Event Peer – for Things involving multiple channels, like BRIDGE Used by: AST_EVENT_CEL Paylo...
Definition: event_defs.h:235
Channel Event CID ANI field Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:217
const char * word
Definition: cli.h:157
int ast_event_sub_append_ie_uint(struct ast_event_sub *sub, enum ast_event_ie_type ie_type, uint32_t uint)
Append a uint parameter to a subscription.
Definition: event.c:761
static void event_update_cache(struct ao2_container *cache, struct ast_event *event)
Definition: event.c:1434
static int handle_event(void *data)
Definition: event.c:1482
An API for managing task processing threads that can be shared across modules.
#define AST_RWDLLIST_WRLOCK(head)
Write locks a list.
Definition: dlinkedlists.h:58
char description[64]
Definition: event.c:127
Channel Event AMA flags Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:193
static const char *const cached_event_types[]
Names of cached event types, for CLI tab completion.
Definition: event.c:195
static const char type[]
Definition: chan_nbs.c:57
static int ast_event_hash(const void *obj, const int flags)
Definition: event.c:1594
#define AST_LIST_HEAD_NOLOCK_INIT_VALUE
Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK.
Definition: linkedlists.h:251
uint16_t event_len
Definition: event_defs.h:336
static int ast_event_hash_devstate_change(const void *obj, const int flags)
Definition: event.c:1587
struct ao2_container * cache
Definition: pbx_realtime.c:78
const char * usage
Definition: cli.h:171
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has an integer payload.
Definition: event.c:1075
uint16_t event_len
Definition: event.c:87
struct ast_eid ast_eid_default
Global EID.
Definition: asterisk.c:192
#define CLI_SUCCESS
Definition: cli.h:43
int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
Initialize an event iterator instance.
Definition: event.c:1014
Channel Event LinkedID Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:241
struct ast_event * event
Definition: event_defs.h:337
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:67
void ast_event_destroy(struct ast_event *event)
Destroy an event.
Definition: event.c:1314
int ast_event_iterator_next(struct ast_event_iterator *iterator)
Move iterator instance to next IE.
Definition: event.c:1030
enum ast_event_type type
Definition: event.c:125
static struct ast_cli_entry event_cli[]
Definition: event.c:1781
Standard Command Line Interface.
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
Definition: event.c:1202
#define ast_calloc(a, b)
Definition: astmm.h:82
struct ast_event_sub * ast_event_subscribe(enum ast_event_type event_type, ast_event_cb_t cb, const char *description, void *userdata,...)
Subscribe to events.
Definition: event.c:909
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
Get the value of the current IE in the iterator as an integer payload.
Definition: event.c:1041
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
#define AST_RWDLLIST_TRAVERSE
Definition: dlinkedlists.h:506
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
const int pos
Definition: cli.h:158
int ast_event_append_eid(struct ast_event **event)
Append the global EID IE.
Definition: event.c:1308
#define ast_realloc(a, b)
Definition: astmm.h:103
uint16_t ast_event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
Get the length of the raw payload for the current IE for an iterator.
Definition: event.c:1065
static struct ast_event * gen_sub_event(struct ast_event_sub *sub)
Definition: event.c:660
const char * ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
Get the string representation of an information element type.
Definition: event.c:304
int( ao2_hash_fn)(const void *obj, const int flags)
Definition: astobj2.h:695
static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
Definition: event.c:349
#define AST_DLLIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: dlinkedlists.h:999
Unique ID Used by: AST_EVENT_SUB, AST_EVENT_UNSUB Payload type: UINT.
Definition: event_defs.h:89
const char * ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
Get the value of the current IE in the iterator as a string payload.
Definition: event.c:1051
size_t ast_event_get_size(const struct ast_event *event)
Get the size of an event.
Definition: event.c:340
uint32_t hash
Definition: event.c:115
const char * ast_event_get_type_name(const struct ast_event *event)
Get the string representation of the type of the given event.
Definition: event.c:274
static void dump_raw_ie(struct ast_event_iterator *i, struct ast_cli_args *a)
Definition: event.c:1659
enum ast_event_ie_pltype ie_pltype
Definition: event.c:217
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
#define AST_RWLIST_FIRST
Definition: linkedlists.h:422
unsigned char payload[0]
Definition: event.c:91
Generic State IE Used by AST_EVENT_DEVICE_STATE_CHANGE Payload type: UINT The actual state values dep...
Definition: event_defs.h:115
const char * ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a string payload.
Definition: event.c:1102
#define AST_RWDLLIST_RDLOCK(head)
Read locks a list.
Definition: dlinkedlists.h:71
void * ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
Get the value of the current IE in the iterator instance that has a raw payload.
Definition: event.c:1060
#define ast_malloc(a)
Definition: astmm.h:91
Device Name Used by AST_EVENT_DEVICE_STATE_CHANGE Payload type: STR.
Definition: event_defs.h:107
enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
Get the type of the current IE in the iterator instance.
Definition: event.c:1036
static void event_shutdown(void)
Definition: event.c:1786
struct ast_event_sub * ast_event_unsubscribe(struct ast_event_sub *event_sub)
Un-subscribe from events.
Definition: event.c:987
struct ast_event_sub * ast_event_subscribe_new(enum ast_event_type type, ast_event_cb_t cb, void *userdata)
Allocate a subscription, but do not activate it.
Definition: event.c:739
ao2_hash_fn * hash_fn
Event type specific hash function.
Definition: event.c:165
IE payload types and names.
Definition: event.c:216
uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the length of the raw payload for a particular IE.
Definition: event.c:1125
static char mailbox[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:197
uint32_t uint
Definition: event.c:113
int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t data)
Append an information element that has an integer payload.
Definition: event.c:1160
Channel Event AccountCode Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:199
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
Mailbox name.
Definition: event_defs.h:83
unsigned char ie_payload[0]
Definition: event.c:61
The payload for a string information element.
Definition: event.c:67
int ast_event_sub_append_ie_exists(struct ast_event_sub *sub, enum ast_event_ie_type ie_type)
Append an &#39;exists&#39; parameter to a subscription.
Definition: event.c:805
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:949