event.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * \author Russell Bryant <russell@digium.com>
  21. * \ref AstGenericEvents
  22. */
  23. /*!
  24. * \page AstGenericEvents Generic event system
  25. *
  26. * Prior to the creation of \ref stasis, the purpose of this API was to provide
  27. * a generic way to share events between Asterisk modules. Once there was a need
  28. * to disseminate data whose definition was provided by the producers/consumers,
  29. * it was no longer possible to use the binary representation in the generic
  30. * event system.
  31. *
  32. * That aside, the generic event system is still useful and used by several
  33. * modules in Asterisk.
  34. * - CEL uses the \ref ast_event representation to pass information to registered
  35. * backends.
  36. * - The \file res_corosync module publishes \ref ast_event representations of
  37. * information to other Asterisk instances in a cluster.
  38. * - Security event represent their event types and data using this system.
  39. * - Theoretically, any \ref stasis message can use this system to pass
  40. * information around in a binary format.
  41. *
  42. * Events have an associated event type, as well as information elements. The
  43. * information elements are the meta data that go along with each event. For
  44. * example, in the case of message waiting indication, the event type is MWI,
  45. * and each MWI event contains at least three information elements: the
  46. * mailbox, the number of new messages, and the number of old messages.
  47. */
  48. #ifndef AST_EVENT_H
  49. #define AST_EVENT_H
  50. #if defined(__cplusplus) || defined(c_plusplus)
  51. extern "C" {
  52. #endif
  53. #include "asterisk/event_defs.h"
  54. /*!
  55. * \brief Create a new event
  56. *
  57. * \param event_type The type of event to create
  58. *
  59. * The rest of the arguments to this function specify information elements to
  60. * add to the event. They are specified in the form:
  61. * \code
  62. * <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
  63. * \endcode
  64. * and must end with AST_EVENT_IE_END.
  65. *
  66. * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
  67. * by a valid IE payload type. A payload must also be specified
  68. * after the IE payload type.
  69. *
  70. * \note The EID IE will be appended automatically when this function is used
  71. * with at least one IE specified.
  72. *
  73. * \return This returns the event that has been created. If there is an error
  74. * creating the event, NULL will be returned.
  75. *
  76. * Example usage:
  77. *
  78. * \code
  79. * if (!(event = ast_event_new(AST_EVENT_MWI,
  80. * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
  81. * AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
  82. * AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
  83. * AST_EVENT_IE_END))) {
  84. * return;
  85. * }
  86. * \endcode
  87. *
  88. * This creates a MWI event with 3 information elements, a mailbox which is
  89. * a string, and the number of new and old messages, specified as integers.
  90. */
  91. struct ast_event *ast_event_new(enum ast_event_type event_type, ...);
  92. /*!
  93. * \brief Destroy an event
  94. *
  95. * \param event the event to destroy
  96. *
  97. * \return Nothing
  98. *
  99. */
  100. void ast_event_destroy(struct ast_event *event);
  101. /*!
  102. * \brief Append an information element that has a string payload
  103. *
  104. * \param event the event that the IE will be appended to
  105. * \param ie_type the type of IE to append
  106. * \param str The string for the payload of the IE
  107. *
  108. * \retval 0 success
  109. * \retval -1 failure
  110. *
  111. * The pointer to the event will get updated with the new location for the event
  112. * that now contains the appended information element. If the re-allocation of
  113. * the memory for this event fails, it will be set to NULL.
  114. */
  115. int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
  116. const char *str);
  117. /*!
  118. * \brief Append an information element that has an integer payload
  119. *
  120. * \param event the event that the IE will be appended to
  121. * \param ie_type the type of IE to append
  122. * \param data The integer for the payload of the IE
  123. *
  124. * \retval 0 success
  125. * \retval -1 failure
  126. *
  127. * The pointer to the event will get updated with the new location for the event
  128. * that now contains the appended information element. If the re-allocation of
  129. * the memory for this event fails, it will be set to NULL.
  130. */
  131. int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
  132. uint32_t data);
  133. /*!
  134. * \brief Append an information element that has a bitflags payload
  135. *
  136. * \param event the event that the IE will be appended to
  137. * \param ie_type the type of IE to append
  138. * \param bitflags the flags that are the payload of the IE
  139. *
  140. * \retval 0 success
  141. * \retval -1 failure
  142. * \since 1.8
  143. *
  144. * The pointer to the event will get updated with the new location for the event
  145. * that now contains the appended information element. If the re-allocation of
  146. * the memory for this event fails, it will be set to NULL.
  147. */
  148. int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
  149. uint32_t bitflags);
  150. /*!
  151. * \brief Append an information element that has a raw payload
  152. *
  153. * \param event the event that the IE will be appended to
  154. * \param ie_type the type of IE to append
  155. * \param data A pointer to the raw data for the payload of the IE
  156. * \param data_len The amount of data to copy into the payload
  157. *
  158. * \retval 0 success
  159. * \retval -1 failure
  160. *
  161. * The pointer to the event will get updated with the new location for the event
  162. * that now contains the appended information element. If the re-allocation of
  163. * the memory for this event fails, it will be set to NULL.
  164. */
  165. int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
  166. const void *data, size_t data_len);
  167. /*!
  168. * \brief Append the global EID IE
  169. *
  170. * \param event the event to append IE to
  171. *
  172. * \note For ast_event_new() that includes IEs, this is done automatically
  173. * for you.
  174. *
  175. * \retval 0 success
  176. * \retval -1 failure
  177. */
  178. int ast_event_append_eid(struct ast_event **event);
  179. /*!
  180. * \brief Get the value of an information element that has an integer payload
  181. *
  182. * \param event The event to get the IE from
  183. * \param ie_type the type of information element to retrieve
  184. *
  185. * \return This returns the payload of the information element with the given type.
  186. * However, an IE with a payload of 0, and the case where no IE is found
  187. * yield the same return value.
  188. */
  189. uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type);
  190. /*!
  191. * \brief Get the value of an information element that has a string payload
  192. *
  193. * \param event The event to get the IE from
  194. * \param ie_type the type of information element to retrieve
  195. *
  196. * \return This returns the payload of the information element with the given type.
  197. * If the information element isn't found, NULL will be returned.
  198. */
  199. const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type);
  200. /*!
  201. * \brief Get the value of an information element that has a raw payload
  202. *
  203. * \param event The event to get the IE from
  204. * \param ie_type the type of information element to retrieve
  205. *
  206. * \return This returns the payload of the information element with the given type.
  207. * If the information element isn't found, NULL will be returned.
  208. */
  209. const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type);
  210. /*!
  211. * \brief Get the length of the raw payload for a particular IE
  212. *
  213. * \param event The event to get the IE payload length from
  214. * \param ie_type the type of information element to get the length of
  215. *
  216. * \return If an IE of type ie_type is found, its payload length is returned.
  217. * Otherwise, 0 is returned.
  218. */
  219. uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type);
  220. /*!
  221. * \brief Get the string representation of the type of the given event
  222. *
  223. * \arg event the event to get the type of
  224. *
  225. * \return the string representation of the event type of the provided event
  226. * \since 1.6.1
  227. */
  228. const char *ast_event_get_type_name(const struct ast_event *event);
  229. /*!
  230. * \brief Get the string representation of an information element type
  231. *
  232. * \param ie_type the information element type to get the string representation of
  233. *
  234. * \return the string representation of the information element type
  235. * \since 1.6.1
  236. */
  237. const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type);
  238. /*!
  239. * \brief Get the payload type for a given information element type
  240. *
  241. * \param ie_type the information element type to get the payload type of
  242. *
  243. * \return the payload type for the provided IE type
  244. * \since 1.6.1
  245. */
  246. enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type);
  247. /*!
  248. * \brief Get the type for an event
  249. *
  250. * \param event the event to get the type for
  251. *
  252. * \return the event type as represented by one of the values in the
  253. * ast_event_type enum
  254. */
  255. enum ast_event_type ast_event_get_type(const struct ast_event *event);
  256. /*!
  257. * \brief Convert a string to an IE type
  258. *
  259. * \param str the string to convert
  260. * \param ie_type an output parameter for the IE type
  261. *
  262. * \retval 0 success
  263. * \retval non-zero failure
  264. * \since 1.6.1
  265. */
  266. int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type);
  267. /*!
  268. * \brief Get the size of an event
  269. *
  270. * \param event the event to get the size of
  271. *
  272. * \return the number of bytes contained in the event
  273. * \since 1.6.1
  274. */
  275. size_t ast_event_get_size(const struct ast_event *event);
  276. /*!
  277. * \brief Initialize an event iterator instance
  278. *
  279. * \param iterator The iterator instance to initialize
  280. * \param event The event that will be iterated through
  281. *
  282. * \retval 0 Success, there are IEs available to iterate
  283. * \retval -1 Failure, there are no IEs in the event to iterate
  284. */
  285. int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event);
  286. /*!
  287. * \brief Move iterator instance to next IE
  288. *
  289. * \param iterator The iterator instance
  290. *
  291. * \retval 0 on success
  292. * \retval -1 if end is reached
  293. */
  294. int ast_event_iterator_next(struct ast_event_iterator *iterator);
  295. /*!
  296. * \brief Get the type of the current IE in the iterator instance
  297. *
  298. * \param iterator The iterator instance
  299. *
  300. * \return the ie type as represented by one of the value sin the
  301. * ast_event_ie_type enum
  302. */
  303. enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator);
  304. /*!
  305. * \brief Get the value of the current IE in the iterator as an integer payload
  306. *
  307. * \param iterator The iterator instance
  308. *
  309. * \return This returns the payload of the information element as a uint.
  310. */
  311. uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator);
  312. /*!
  313. * \brief Get the value of the current IE in the iterator as a string payload
  314. *
  315. * \param iterator The iterator instance
  316. *
  317. * \return This returns the payload of the information element as a string.
  318. */
  319. const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator);
  320. /*!
  321. * \brief Get the minimum length of an ast_event.
  322. *
  323. * \return minimum amount of memory that will be consumed by any ast_event.
  324. */
  325. size_t ast_event_minimum_length(void);
  326. #if defined(__cplusplus) || defined(c_plusplus)
  327. }
  328. #endif
  329. #endif /* AST_EVENT_H */