summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/asterisk/event.h57
-rw-r--r--include/asterisk/event_defs.h1
-rw-r--r--main/event.c55
3 files changed, 102 insertions, 11 deletions
diff --git a/include/asterisk/event.h b/include/asterisk/event.h
index 2febeb3f7..767ef1cee 100644
--- a/include/asterisk/event.h
+++ b/include/asterisk/event.h
@@ -413,4 +413,61 @@ const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_i
*/
enum ast_event_type ast_event_get_type(const struct ast_event *event);
+/*!
+ * \brief Initialize an event iterator instance
+ *
+ * \param iterator The iterator instance to initialize
+ * \param event The event that will be iterated through
+ *
+ * \return Nothing
+ */
+void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event);
+
+/*!
+ * \brief Move iterator instance to next IE
+ *
+ * \param iterator The iterator instance
+ *
+ * \retval 0 on success
+ * \retval -1 if end is reached
+ */
+int ast_event_iterator_next(struct ast_event_iterator *iterator);
+
+/*!
+ * \brief Get the type of the current IE in the iterator instance
+ *
+ * \param iterator The iterator instance
+ *
+ * \return the ie type as represented by one of the value sin the
+ * ast_event_ie_type enum
+ */
+enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator);
+
+/*!
+ * \brief Get the value of the current IE in the ierator as an integer payload
+ *
+ * \param iterator The iterator instance
+ *
+ * \return This returns the payload of the information element as a uint.
+ */
+uint32_t ast_event_iteragor_get_ie_uint(struct ast_event_iterator *iterator);
+
+/*!
+ * \brief Get the value of the current IE in the iterator as a string payload
+ *
+ * \param iterator The iterator instance
+ *
+ * \return This returns the payload of the information element as a string.
+ */
+const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator);
+
+/*!
+ * \brief Get the value of the current IE in the iterator instance that has a raw payload
+ *
+ * \param iterator The iterator instance
+ *
+ * \return This returns the payload of the information element as type raw.
+ */
+void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator);
+
#endif /* AST_EVENT_H */
diff --git a/include/asterisk/event_defs.h b/include/asterisk/event_defs.h
index 9aadaebf6..b664ac14b 100644
--- a/include/asterisk/event_defs.h
+++ b/include/asterisk/event_defs.h
@@ -138,5 +138,6 @@ enum ast_event_subscriber_res {
struct ast_event;
struct ast_event_ie;
struct ast_event_sub;
+struct ast_event_iterator;
#endif /* AST_EVENT_DEFS_H */
diff --git a/main/event.c b/main/event.c
index 58515e007..d7deede51 100644
--- a/main/event.c
+++ b/main/event.c
@@ -66,6 +66,12 @@ struct ast_event_ref {
AST_LIST_ENTRY(ast_event_ref) entry;
};
+struct ast_event_iterator {
+ uint16_t event_len;
+ const struct ast_event *event;
+ struct ast_event_ie *ie;
+};
+
/*! \brief data shared between event dispatching threads */
static struct {
ast_cond_t cond;
@@ -355,6 +361,40 @@ void ast_event_unsubscribe(struct ast_event_sub *sub)
ast_event_sub_destroy(sub);
}
+void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
+{
+ iterator->event_len = ntohs(event->event_len);
+ iterator->event = event;
+ iterator->ie = ((void *) event) + sizeof(*event);
+ return;
+}
+
+int ast_event_iterator_next(struct ast_event_iterator *iterator)
+{
+ iterator->ie = ((void *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len);
+ return ((iterator->event_len > (((void *) iterator->ie) - ((void *) iterator->event))) ? -1 : 0);
+}
+
+enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
+{
+ return iterator->ie->ie_type;
+}
+
+uint32_t ast_event_iteragor_get_ie_uint(struct ast_event_iterator *iterator)
+{
+ return ntohl(*iterator->ie->ie_payload);
+}
+
+const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
+{
+ return (const char*)iterator->ie->ie_payload;
+}
+
+void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
+{
+ return iterator->ie->ie_payload;
+}
+
enum ast_event_type ast_event_get_type(const struct ast_event *event)
{
return ntohs(event->type);
@@ -376,18 +416,11 @@ const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_i
const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
{
- struct ast_event_ie *ie;
- uint16_t event_len;
-
- ie_type = ntohs(ie_type);
- event_len = ntohs(event->event_len);
-
- ie = ((void *) event) + sizeof(*event);
+ struct ast_event_iterator iterator;
- while ((((void *) ie) - ((void *) event)) < event_len) {
- if (ie->ie_type == ie_type)
- return ie->ie_payload;
- ie = ((void *) ie) + sizeof(*ie) + ntohs(ie->ie_payload_len);
+ for (ast_event_iterator_init(&iterator, event); !ast_event_iterator_next(&iterator); ) {
+ if (ast_event_iterator_get_ie_type(&iterator) == ie_type)
+ return ast_event_iterator_get_ie_raw(&iterator);
}
return NULL;