From f3e5d3fb35acbacc3f36a4ca0f1e8a99d0f8f60f Mon Sep 17 00:00:00 2001 From: Wataru Ishida Date: Thu, 30 May 2019 12:08:08 -0700 Subject: [PATCH] mux: convert oid for notifications Signed-off-by: Wataru Ishida --- tai_mux/mux.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++-- tai_mux/mux.hpp | 15 ++++++++- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/tai_mux/mux.cpp b/tai_mux/mux.cpp index 6a0aad4..37487cb 100644 --- a/tai_mux/mux.cpp +++ b/tai_mux/mux.cpp @@ -234,6 +234,49 @@ tai_status_t Multiplexier::tai_log_set(_In_ tai_api_t api, _In_ tai_log_level_t return TAI_STATUS_SUCCESS; } +void notification_callback(void* context, tai_object_id_t oid, tai_attribute_t const * const attribute) { + if ( context == nullptr || attribute == nullptr ) { + return; + } + auto ctx = static_cast(context); + ctx->mux->notify(ctx, oid, attribute); +} + +void Multiplexier::notify(notification_context* ctx, tai_object_id_t real_oid, tai_attribute_t const * const src) { + auto oid = get_reverse_mapping(real_oid, ctx->adapter); + if ( oid == TAI_NULL_OBJECT_ID ) { + return; + } + auto key = std::pair(oid, ctx->notify_id); + tai_attribute_t dst; + tai_alloc_info_t info; + info.reference = src; + auto t = object_type_query(oid); + + auto meta = tai_metadata_get_attr_metadata(t, src->id); + if ( meta == nullptr ) { + return; + } + + dst.id = src->id; + + if ( tai_metadata_alloc_attr_value(meta, &dst, &info) != 0 ) { + return; + } + + if ( tai_metadata_deepcopy_attr_value(meta, src, &dst) != 0 ) { + goto err; + } + + if ( convert_oid(oid, &dst, &dst, true) != TAI_STATUS_SUCCESS ) { + goto err; + } + + ctx->handler.notify(ctx->handler.context, oid, &dst); +err: + tai_metadata_free_attr_value(meta, &dst, nullptr); +} + tai_status_t Multiplexier::set_attributes( std::function f, tai_object_id_t oid, uint32_t attr_count, const tai_attribute_t *attr_list) { tai_object_id_t id; ModuleAdapter *m_adapter; @@ -251,6 +294,10 @@ tai_status_t Multiplexier::set_attributes( std::functionget_mapping(oid, &adapter, nullptr) != 0 ) { + return TAI_STATUS_FAILURE; + } auto meta = tai_metadata_get_attr_metadata(t, src->id); const tai_object_map_list_t *oml; auto convert = [&](tai_object_id_t s) -> tai_object_id_t { @@ -313,6 +365,7 @@ tai_status_t Multiplexier::convert_oid(ModuleAdapter *adapter, tai_object_type_t } return oid; }; + auto key = std::pair(oid, src->id); switch (meta->attrvaluetype) { case TAI_ATTR_VALUE_TYPE_OID: @@ -344,6 +397,30 @@ tai_status_t Multiplexier::convert_oid(ModuleAdapter *adapter, tai_object_type_t } } break; + case TAI_ATTR_VALUE_TYPE_NOTIFICATION: + if ( reversed ) { + if ( m_notification_map.find(key) != m_notification_map.end() ) { + auto n = m_notification_map[key]; + dst->value.notification.context = n->handler.context; + dst->value.notification.notify = n->handler.notify; + } + } else { + if ( src->value.notification.notify == nullptr ) { + delete m_notification_map[key]; + m_notification_map.erase(key); + } else { + if ( m_notification_map.find(key) == m_notification_map.end() ) { + m_notification_map[key] = new notification_context(); + } + auto n = m_notification_map[key]; + n->mux = this; + n->adapter = adapter; + n->handler = src->value.notification; + n->notify_id = src->id; + dst->value.notification.context = n; + dst->value.notification.notify = notification_callback; + } + } } return TAI_STATUS_SUCCESS; } diff --git a/tai_mux/mux.hpp b/tai_mux/mux.hpp index e193fb4..11b1c72 100644 --- a/tai_mux/mux.hpp +++ b/tai_mux/mux.hpp @@ -29,6 +29,15 @@ class OIDAllocator { std::bitset<256> m_bitset; }; +class Multiplexier; + +struct notification_context { + Multiplexier* mux; + ModuleAdapter* adapter; + tai_notification_handler_t handler; + tai_attr_id_t notify_id; +}; + // singleton class Multiplexier { public: @@ -54,6 +63,8 @@ class Multiplexier { return m_pa->get_module_adapter(location); } + void notify(notification_context* ctx, tai_object_id_t real_oid, tai_attribute_t const * const src); + tai_status_t create_module( _Out_ tai_object_id_t *module_id, _In_ uint32_t attr_count, @@ -122,6 +133,8 @@ class Multiplexier { void operator = (const Multiplexier&){} PlatformAdapter *m_pa; std::map> m_map; + std::map, notification_context*> m_notification_map; + OIDAllocator m_oid_allocator; tai_status_t set_attributes( std::function f, tai_object_id_t oid, uint32_t attr_count, const tai_attribute_t *attr_list); @@ -166,7 +179,7 @@ class Multiplexier { return 0; } - tai_status_t convert_oid(ModuleAdapter *adapter, tai_object_type_t t, const tai_attribute_t *src, tai_attribute_t *dst, bool reversed); + tai_status_t convert_oid(tai_object_id_t oid, const tai_attribute_t *src, tai_attribute_t *dst, bool reversed); int free_attributes(tai_object_type_t t, std::vector& attributes); };