From dc5690a10be65e1e6bb43ce8ff723bc4c492c8c3 Mon Sep 17 00:00:00 2001 From: Wataru Ishida Date: Thu, 22 Apr 2021 05:11:28 +0000 Subject: [PATCH] mux: support TAI meta APIs Signed-off-by: Wataru Ishida --- tai_mux/module_adapter.cpp | 6 +++ tai_mux/module_adapter.hpp | 49 +++++++++++++++++++++++++ tai_mux/mux.cpp | 65 +++++++++++++++++++++++++++++++++ tai_mux/mux.hpp | 7 ++++ tai_mux/oopt-tai | 2 +- tai_mux/platform_adapter.cpp | 22 ++++++++--- tai_mux/tests/Makefile | 32 +++++++++++++--- tai_mux/tests/custom_a/module.h | 27 ++++++++++++++ tai_mux/tests/custom_b/module.h | 27 ++++++++++++++ tai_mux/tests/static.json | 3 +- 10 files changed, 227 insertions(+), 13 deletions(-) create mode 100644 tai_mux/tests/custom_a/module.h create mode 100644 tai_mux/tests/custom_b/module.h diff --git a/tai_mux/module_adapter.cpp b/tai_mux/module_adapter.cpp index da22c58..72d702a 100644 --- a/tai_mux/module_adapter.cpp +++ b/tai_mux/module_adapter.cpp @@ -43,6 +43,12 @@ namespace tai::mux { if ( status != TAI_STATUS_SUCCESS ) { throw Exception(status); } + + status = tai_api_query(TAI_API_META, (void **)(&m_meta_api)); + if ( status != TAI_STATUS_SUCCESS ) { + TAI_WARN("no meta api: %s", name.c_str()); + } + } ModuleAdapter::~ModuleAdapter() { diff --git a/tai_mux/module_adapter.hpp b/tai_mux/module_adapter.hpp index 86203e1..7b80278 100644 --- a/tai_mux/module_adapter.hpp +++ b/tai_mux/module_adapter.hpp @@ -194,6 +194,54 @@ namespace tai::mux { return m_hostif_api->clear_host_interface_attributes(host_interface_id, attr_count, attr_list); } + tai_status_t list_metadata( + _In_ const tai_metadata_key_t * const key, + _Out_ uint32_t *count, + _Out_ const tai_attr_metadata_t * const **list) { + if ( m_meta_api == nullptr || m_meta_api->list_metadata == nullptr ) { + auto type = key->type; + if ( key->oid != TAI_NULL_OBJECT_ID ) { + type = tai_object_type_query(key->oid); + } + auto info = tai_metadata_all_object_type_infos[type]; + if ( info == nullptr ) { + *count = tai_metadata_attr_sorted_by_id_name_count; + *list = tai_metadata_attr_sorted_by_id_name; + return TAI_STATUS_SUCCESS; + } + *count = info->attrmetadatalength; + *list = info->attrmetadata; + return TAI_STATUS_SUCCESS; + } + // TODO list_metadata is not returning mux custom attributes + return m_meta_api->list_metadata(key, count, list); + } + + const tai_attr_metadata_t* get_attr_metadata( + _In_ const tai_metadata_key_t * const key, + _In_ tai_attr_id_t attr_id) { + if ( m_meta_api == nullptr || m_meta_api->get_attr_metadata == nullptr || ( TAI_MODULE_ATTR_CUSTOM_MUX_START <= attr_id && attr_id <= TAI_MODULE_ATTR_CUSTOM_MUX_END )) { + auto type = key->type; + if ( key->oid != TAI_NULL_OBJECT_ID ) { + type = tai_object_type_query(key->oid); + } + return tai_metadata_get_attr_metadata(type, attr_id); + } + return m_meta_api->get_attr_metadata(key, attr_id); + } + + const tai_object_type_info_t* get_object_info( + _In_ const tai_metadata_key_t *const key) { + if ( m_meta_api == nullptr || m_meta_api->get_object_info == nullptr ) { + auto type = key->type; + if ( key->oid != TAI_NULL_OBJECT_ID ) { + type = tai_object_type_query(key->oid); + } + return tai_metadata_get_object_type_info(type); + } + return m_meta_api->get_object_info(key); + } + private: void* m_dl; const std::string m_name; @@ -207,6 +255,7 @@ namespace tai::mux { tai_module_api_t* m_module_api; tai_host_interface_api_t* m_hostif_api; tai_network_interface_api_t* m_netif_api; + tai_meta_api_t* m_meta_api; }; using S_ModuleAdapter = std::shared_ptr; diff --git a/tai_mux/mux.cpp b/tai_mux/mux.cpp index c711bdc..46554d3 100644 --- a/tai_mux/mux.cpp +++ b/tai_mux/mux.cpp @@ -184,6 +184,71 @@ namespace tai::mux { return TAI_STATUS_SUCCESS; } + tai_status_t Platform::get_ma_and_meta_key(const tai_metadata_key_t *const key, tai_metadata_key_t& new_key, S_ModuleAdapter *ma) { + new_key = *key; + if ( key->location.count > 0 ) { + std::string loc(key->location.list, key->location.count); + *ma = m_pa->get_module_adapter(loc); + } else if ( key->oid != TAI_NULL_OBJECT_ID ) { + tai_object_id_t real_id; + if ( m_pa->get_mapping(key->oid, ma, &real_id) < 0 ) { + return TAI_STATUS_FAILURE; + } + new_key.oid = real_id; + } + return TAI_STATUS_SUCCESS; + } + + tai_status_t Platform::list_metadata(const tai_metadata_key_t *const key, uint32_t *count, const tai_attr_metadata_t *const **list) { + S_ModuleAdapter ma; + tai_metadata_key_t new_key; + auto ret = get_ma_and_meta_key(key, new_key, &ma); + if ( ret != TAI_STATUS_SUCCESS ) { + return ret; + } + + if ( ma ) { + return ma->list_metadata(&new_key, count, list); + } + + auto type = key->type; + auto info = tai_metadata_all_object_type_infos[type]; + if ( info == nullptr ) { + *count = tai_metadata_attr_sorted_by_id_name_count; + *list = tai_metadata_attr_sorted_by_id_name; + return TAI_STATUS_SUCCESS; + } + *count = info->attrmetadatalength; + *list = info->attrmetadata; + return TAI_STATUS_SUCCESS; + } + + const tai_attr_metadata_t* Platform::get_attr_metadata(const tai_metadata_key_t *const key, tai_attr_id_t attr_id) { + S_ModuleAdapter ma; + tai_metadata_key_t new_key; + auto ret = get_ma_and_meta_key(key, new_key, &ma); + if ( ret != TAI_STATUS_SUCCESS ) { + return nullptr; + } + if ( ma ) { + return ma->get_attr_metadata(&new_key, attr_id); + } + return tai_metadata_get_attr_metadata(new_key.type, attr_id); + } + + const tai_object_type_info_t* Platform::get_object_info(const tai_metadata_key_t *const key) { + S_ModuleAdapter ma; + tai_metadata_key_t new_key; + auto ret = get_ma_and_meta_key(key, new_key, &ma); + if ( ret != TAI_STATUS_SUCCESS ) { + return nullptr; + } + if ( ma ) { + return ma->get_object_info(&new_key); + } + return tai_metadata_get_object_type_info(key->type); + } + tai_status_t attribute_getter(tai_attribute_t* const attribute, void* user) { auto ctx = reinterpret_cast(user); auto pa = ctx->pa; diff --git a/tai_mux/mux.hpp b/tai_mux/mux.hpp index b81b602..90c5519 100644 --- a/tai_mux/mux.hpp +++ b/tai_mux/mux.hpp @@ -22,7 +22,14 @@ namespace tai::mux { tai_object_type_t get_object_type(tai_object_id_t id); tai_object_id_t get_module_id(tai_object_id_t id); tai_status_t set_log(tai_api_t tai_api_id, tai_log_level_t log_level, tai_log_fn log_fn); + + tai_status_t list_metadata(const tai_metadata_key_t *const key, uint32_t *count, const tai_attr_metadata_t *const **list); + const tai_attr_metadata_t* get_attr_metadata(const tai_metadata_key_t *const key, tai_attr_id_t attr_id); + const tai_object_type_info_t* get_object_info(const tai_metadata_key_t *const key); private: + + tai_status_t get_ma_and_meta_key(const tai_metadata_key_t *const key, tai_metadata_key_t& new_key, S_ModuleAdapter *ma); + S_PlatformAdapter m_pa; log_setting m_log_setting; }; diff --git a/tai_mux/oopt-tai b/tai_mux/oopt-tai index 0a483c5..7711e23 160000 --- a/tai_mux/oopt-tai +++ b/tai_mux/oopt-tai @@ -1 +1 @@ -Subproject commit 0a483c5c955af17b744238d7987dfee8ca6caf31 +Subproject commit 7711e2332db587b97d98fbcc4c3d0fde4afd3b16 diff --git a/tai_mux/platform_adapter.cpp b/tai_mux/platform_adapter.cpp index 0a1441a..6bf11b8 100644 --- a/tai_mux/platform_adapter.cpp +++ b/tai_mux/platform_adapter.cpp @@ -18,9 +18,15 @@ namespace tai::mux { void PlatformAdapter::notify(NotificationContext* ctx, tai_object_id_t real_oid, uint32_t attr_count, tai_attribute_t const * const attr_list) { auto oid = ctx->muxed_oid; std::vector attrs; + S_ModuleAdapter adapter; + auto ret = get_mapping(oid, &adapter, nullptr); + if ( ret < 0 ) { + return; + } for ( int i = 0; i < attr_count; i++ ) { auto src = attr_list[i]; - auto meta = tai_metadata_get_attr_metadata(ctx->object_type, src.id); + tai_metadata_key_t key{.oid=real_oid}; + auto meta = adapter->get_attr_metadata(&key, src.id); if ( meta == nullptr ) { continue; } @@ -43,12 +49,18 @@ namespace tai::mux { } tai_status_t PlatformAdapter::convert_oid(const tai_object_type_t& type, const tai_object_id_t& id, const tai_attribute_t * const src, tai_attribute_t * const dst, bool reversed) { - auto meta = tai_metadata_get_attr_metadata(type, src->id); const tai_object_map_list_t *oml; S_ModuleAdapter adapter; - if ( get_mapping(id, &adapter, nullptr) != 0 ) { + tai_object_id_t real_id; + if ( get_mapping(id, &adapter, &real_id) != 0 ) { return TAI_STATUS_FAILURE; } + tai_metadata_key_t key{.oid=real_id}; + auto meta = adapter->get_attr_metadata(&key, src->id); + if ( meta == nullptr ) { + return TAI_STATUS_FAILURE; + } + auto convert = [&](tai_object_id_t s) -> tai_object_id_t { if ( reversed ) { return get_reverse_mapping(s, adapter); @@ -156,7 +168,8 @@ namespace tai::mux { for ( auto i = 0; i < count; i++ ) { auto attribute = &attrs[i]; - auto meta = tai_metadata_get_attr_metadata(type, attribute->id); + tai_metadata_key_t key{.oid=real_id}; + auto meta = adapter->get_attr_metadata(&key, attribute->id); if ( meta == nullptr ) { return TAI_STATUS_FAILURE; } @@ -246,5 +259,4 @@ namespace tai::mux { return TAI_STATUS_NOT_SUPPORTED; } - } diff --git a/tai_mux/tests/Makefile b/tai_mux/tests/Makefile index 85a5505..5458afd 100644 --- a/tai_mux/tests/Makefile +++ b/tai_mux/tests/Makefile @@ -6,11 +6,31 @@ ifndef TAI_LIB_DIR TAI_LIB_DIR := $(TAI_DIR)/tools/framework endif -all: ../libtai.so static.json $(TAI_LIB_DIR)/examples/basic/libtai.so - TAI_MUX_STATIC_CONFIG_FILE=$(abspath static.json) TAI_TEST_TARGET=$(abspath ../libtai.so) $(MAKE) -C $(TAI_DIR)/tests +all: libtai.so static.json libtai-a.so libtai-b.so + TAI_MUX_STATIC_CONFIG_FILE=$(abspath static.json) TAI_TEST_TARGET=$(abspath libtai.so) $(MAKE) -C $(TAI_DIR)/tests -../libtai.so: - $(MAKE) -C $(@D) +libtai.so: + $(MAKE) -C .. + ln -sf ../$@ $@ -$(TAI_LIB_DIR)/examples/basic/libtai.so: - $(MAKE) -C $(@D) +libtai-a.so: + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_a/*.h))" $(MAKE) -C $(TAI_DIR)/meta clean + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_a/*.h))" $(MAKE) -C $(TAI_LIB_DIR)/examples/basic clean + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_a/*.h))" $(MAKE) -C $(TAI_LIB_DIR)/examples/basic + cp $(TAI_LIB_DIR)/examples/basic/libtai.so $@ + +libtai-b.so: + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_b/*.h))" $(MAKE) -C $(TAI_DIR)/meta clean + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_b/*.h))" $(MAKE) -C $(TAI_LIB_DIR)/examples/basic clean + TAI_META_CUSTOM_FILES="$(abspath $(wildcard custom_b/*.h))" $(MAKE) -C $(TAI_LIB_DIR)/examples/basic + cp $(TAI_LIB_DIR)/examples/basic/libtai.so $@ + +run: libtai-a.so libtai-b.so taish + TAI_MUX_STATIC_CONFIG_FILE=static.json LD_LIBRARY_PATH=..:$(abspath .) $(TAI_DIR)/tools/taish/taish_server -vn + +taish: + $(MAKE) -C $(TAI_DIR)/tools/taish + +clean: + $(RM) libtai-a.so libtai-b.so + $(MAKE) -C $(TAI_DIR)/tools/taish clean diff --git a/tai_mux/tests/custom_a/module.h b/tai_mux/tests/custom_a/module.h new file mode 100644 index 0000000..73b9acf --- /dev/null +++ b/tai_mux/tests/custom_a/module.h @@ -0,0 +1,27 @@ +#ifndef __TAI_BASIC_MODULE__ +#define __TAI_BASIC_MODULE__ + +#include + +typedef enum _basic_module_attr_t +{ + + /** + * @brief Custom attribute example + * + * @type bool + * @flags CREATE_AND_SET + */ + TAI_MODULE_ATTR_CUSTOM = TAI_MODULE_ATTR_CUSTOM_RANGE_START, + + /** + * @brief Custom attribute example + * + * @type bool + * @flags CREATE_AND_SET + */ + TAI_MODULE_ATTR_CUSTOM_A, + +} basic_module_attr_t; + +#endif diff --git a/tai_mux/tests/custom_b/module.h b/tai_mux/tests/custom_b/module.h new file mode 100644 index 0000000..64321b8 --- /dev/null +++ b/tai_mux/tests/custom_b/module.h @@ -0,0 +1,27 @@ +#ifndef __TAI_BASIC_MODULE__ +#define __TAI_BASIC_MODULE__ + +#include + +typedef enum _basic_module_attr_t +{ + + /** + * @brief Custom attribute example + * + * @type bool + * @flags CREATE_AND_SET + */ + TAI_MODULE_ATTR_CUSTOM = TAI_MODULE_ATTR_CUSTOM_RANGE_START, + + /** + * @brief Custom attribute example + * + * @type bool + * @flags CREATE_AND_SET + */ + TAI_MODULE_ATTR_CUSTOM_B, + +} basic_module_attr_t; + +#endif diff --git a/tai_mux/tests/static.json b/tai_mux/tests/static.json index 66b6598..dc6094d 100644 --- a/tai_mux/tests/static.json +++ b/tai_mux/tests/static.json @@ -1,3 +1,4 @@ { - "0": "../tools/framework/examples/basic/libtai.so" + "0": "libtai-a.so", + "1": "libtai-b.so" }