From: Jaeyun Jung Date: Thu, 22 Aug 2024 01:20:54 +0000 (+0900) Subject: [Custom] separate custom impl X-Git-Tag: accepted/tizen/unified/20240903.172453~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4a0868481bb7a1083ab2b296a1792eda6f65c25f;p=platform%2Fupstream%2Fnnstreamer-edge.git [Custom] separate custom impl Separate custom connection implementation and update callbacks. Signed-off-by: Jaeyun Jung --- diff --git a/include/nnstreamer-edge-custom.h b/include/nnstreamer-edge-custom.h index eee6504..b069b6c 100644 --- a/include/nnstreamer-edge-custom.h +++ b/include/nnstreamer-edge-custom.h @@ -24,7 +24,7 @@ extern "C" { * The user should implement the functions and provide them using nns_edge_custom_get_instance(). * Refer to the example in nnstreamer-edge-custom-test.c for more details. */ -typedef struct _NnsEdgeCustomDef +typedef struct { const char *(*nns_edge_custom_get_description) (); int (*nns_edge_custom_create) (void **priv); @@ -36,14 +36,14 @@ typedef struct _NnsEdgeCustomDef int (*nns_edge_custom_is_connected) (void *priv); int (*nns_edge_custom_set_event_cb) (void *priv, nns_edge_event_cb cb, void *user_data); int (*nns_edge_custom_send_data) (void *priv, nns_edge_data_h data_h); - int (*nns_edge_custom_set_option) (void *priv, const char *key, const char *value); - char *(*nns_edge_custom_get_option) (void *priv, const char *key); + int (*nns_edge_custom_set_info) (void *priv, const char *key, const char *value); + int (*nns_edge_custom_get_info) (void *priv, const char *key, char **value); } nns_edge_custom_s; /** * @brief Get nns edge custom connection instance. */ -void* nns_edge_custom_get_instance (); +const nns_edge_custom_s * nns_edge_custom_get_instance (void); #ifdef __cplusplus } diff --git a/jni/nnstreamer-edge.mk b/jni/nnstreamer-edge.mk index de181eb..6cd740a 100644 --- a/jni/nnstreamer-edge.mk +++ b/jni/nnstreamer-edge.mk @@ -10,6 +10,7 @@ NNSTREAMER_EDGE_INCLUDES := \ # nnstreamer-edge sources NNSTREAMER_EDGE_SRCS := \ + $(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c \ $(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-data.c \ $(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-event.c \ $(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-internal.c \ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index acff3a8..0ae6eae 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ # NNStreamer-Edge library SET(NNS_EDGE_SRCS + ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-custom-impl.c ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-metadata.c ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-data.c ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-event.c diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c new file mode 100644 index 0000000..de4c370 --- /dev/null +++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c @@ -0,0 +1,299 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/** + * Copyright (C) 2024 Samsung Electronics Co., Ltd. All Rights Reserved. + * + * @file nnstreamer-edge-custom-impl.c + * @date 14 Aug 2024 + * @brief Internal interface to support communication using custom library. + * @see https://github.com/nnstreamer/nnstreamer + * @author Gichan Jang + * @bug No known bugs except for NYI items + */ + +#include + +#include "nnstreamer-edge-custom-impl.h" +#include "nnstreamer-edge-log.h" + +typedef const nns_edge_custom_s *custom_get_instance (void); + +/** + * @brief Internal function to load custom library. + */ +static int +_load_custom_library (custom_connection_s * custom, const char *lib_path) +{ + void *handle; + nns_edge_custom_s *custom_h; + int ret = NNS_EDGE_ERROR_UNKNOWN; + + handle = dlopen (lib_path, RTLD_LAZY); + if (NULL == handle) { + nns_edge_loge ("Failed to open custom library: %s", dlerror ()); + goto error; + } + + custom_get_instance *get_instance = + (custom_get_instance *) dlsym (handle, "nns_edge_custom_get_instance"); + if (!get_instance) { + nns_edge_loge ("Failed to find nns_edge_custom_get_instance: %s", + dlerror ()); + goto error; + } + + custom_h = (nns_edge_custom_s *) get_instance (); + if (!custom_h) { + nns_edge_loge ("Failed to get custom instance from library."); + goto error; + } + + custom->dl_handle = handle; + custom->instance = custom_h; + ret = NNS_EDGE_ERROR_NONE; + +error: + if (NNS_EDGE_ERROR_NONE != ret) { + if (handle) + dlclose (handle); + } + + return ret; +} + +/** + * @brief Internal function to load custom connection from library. + */ +int +nns_edge_custom_load (custom_connection_s * custom, const char *lib_path) +{ + nns_edge_custom_s *custom_h; + int ret; + + ret = _load_custom_library (custom, lib_path); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge + ("Failed to load custom library. Please check the library path or permission."); + goto error; + } + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_create (&custom->priv); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to create custom connection handle."); + } + +error: + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_custom_release (custom); + } + + return ret; +} + +/** + * @brief Internal function to release custom connection. + */ +int +nns_edge_custom_release (custom_connection_s * custom) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_close (custom->priv); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to stop custom connection."); + } + + if (custom->dl_handle) { + dlclose (custom->dl_handle); + } + + custom->dl_handle = NULL; + custom->instance = NULL; + custom->priv = NULL; + + return ret; +} + +/** + * @brief Internal function to start custom connection. + */ +int +nns_edge_custom_start (custom_connection_s * custom) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_start (custom->priv); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to start custom connection."); + } + + return ret; +} + +/** + * @brief Internal function to stop custom connection. + */ +int +nns_edge_custom_stop (custom_connection_s * custom) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_stop (custom->priv); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to stop custom connection."); + } + + return ret; +} + +/** + * @brief Internal function to set the event callback of custom connection. + */ +int +nns_edge_custom_set_event_callback (custom_connection_s * custom, + nns_edge_event_cb cb, void *user_data) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_set_event_cb (custom->priv, cb, user_data); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to set event callback to custom connection."); + } + + return ret; +} + +/** + * @brief Internal function to connect custom connection. + */ +int +nns_edge_custom_connect (custom_connection_s * custom) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_connect (custom->priv); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to connect custom connection."); + } + + return ret; +} + +/** + * @brief Internal function to check custom connection. + */ +int +nns_edge_custom_is_connected (custom_connection_s * custom) +{ + nns_edge_custom_s *custom_h; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + return custom_h->nns_edge_custom_is_connected (custom->priv); +} + +/** + * @brief Internal function to send data to custom connection. + */ +int +nns_edge_custom_send_data (custom_connection_s * custom, nns_edge_data_h data_h) +{ + nns_edge_custom_s *custom_h; + int ret; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + ret = custom_h->nns_edge_custom_send_data (custom->priv, data_h); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to send data to custom connection."); + } + + return ret; +} + +/** + * @brief Internal function to set information to custom connection. + */ +int +nns_edge_custom_set_info (custom_connection_s * custom, const char *key, + const char *value) +{ + nns_edge_custom_s *custom_h; + int ret = NNS_EDGE_ERROR_NOT_SUPPORTED; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + if (custom_h->nns_edge_custom_set_info) { + ret = custom_h->nns_edge_custom_set_info (custom->priv, key, value); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to set information to custom connection."); + } + } + + return ret; +} + +/** + * @brief Internal function to get information from custom connection. + */ +int +nns_edge_custom_get_info (custom_connection_s * custom, const char *key, + char **value) +{ + nns_edge_custom_s *custom_h; + int ret = NNS_EDGE_ERROR_NOT_SUPPORTED; + + if (!custom || !custom->instance) + return NNS_EDGE_ERROR_INVALID_PARAMETER; + + custom_h = custom->instance; + + if (custom_h->nns_edge_custom_get_info) { + ret = custom_h->nns_edge_custom_get_info (custom->priv, key, value); + if (NNS_EDGE_ERROR_NONE != ret) { + nns_edge_loge ("Failed to get information from custom connection."); + } + } + + return ret; +} diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h new file mode 100644 index 0000000..250481b --- /dev/null +++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/** + * Copyright (C) 2024 Samsung Electronics Co., Ltd. All Rights Reserved. + * + * @file nnstreamer-edge-custom-impl.h + * @date 21 Aug 2024 + * @brief Internal interface to support communication using custom library. + * @see https://github.com/nnstreamer/nnstreamer + * @author Gichan Jang + * @bug No known bugs except for NYI items + */ + +#ifndef __NNSTREAMER_EDGE_CUSTOM_IMPL_H__ +#define __NNSTREAMER_EDGE_CUSTOM_IMPL_H__ + +#include "nnstreamer-edge-custom.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Data structure for edge custom connection. + */ +typedef struct +{ + void *dl_handle; + nns_edge_custom_s *instance; + void *priv; +} custom_connection_s; + +/** + * @brief Internal function to load custom connection from library. + */ +int nns_edge_custom_load (custom_connection_s *custom, const char *lib_path); + +/** + * @brief Internal function to release custom connection. + */ +int nns_edge_custom_release (custom_connection_s *custom); + +/** + * @brief Internal function to start custom connection. + */ +int nns_edge_custom_start (custom_connection_s *custom); + +/** + * @brief Internal function to stop custom connection. + */ +int nns_edge_custom_stop (custom_connection_s *custom); + +/** + * @brief Internal function to set the event callback of custom connection. + */ +int nns_edge_custom_set_event_callback (custom_connection_s *custom, nns_edge_event_cb cb, void *user_data); + +/** + * @brief Internal function to connect custom connection. + */ +int nns_edge_custom_connect (custom_connection_s *custom); + +/** + * @brief Internal function to check custom connection. + */ +int nns_edge_custom_is_connected (custom_connection_s *custom); + +/** + * @brief Internal function to send data to custom connection. + */ +int nns_edge_custom_send_data (custom_connection_s *custom, nns_edge_data_h data_h); + +/** + * @brief Internal function to set information to custom connection. + */ +int nns_edge_custom_set_info (custom_connection_s *custom, const char *key, const char *value); + +/** + * @brief Internal function to get information from custom connection. + */ +int nns_edge_custom_get_info (custom_connection_s *custom, const char *key, char **value); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* __NNSTREAMER_EDGE_CUSTOM_IMPL_H__ */ diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index 7e4c541..1d9c43b 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -22,7 +22,7 @@ #include "nnstreamer-edge-queue.h" #include "nnstreamer-edge-aitt.h" #include "nnstreamer-edge-mqtt.h" -#include "nnstreamer-edge-custom.h" +#include "nnstreamer-edge-custom-impl.h" #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 @@ -33,16 +33,6 @@ */ #define N_BACKLOG 10 -/** - * @brief Data structure for edge custom connection. - */ -typedef struct -{ - void *dl_handle; - void *instance; - void *priv; -} custom_connection_s; - /** * @brief Data structure for edge handle. */ @@ -935,16 +925,10 @@ _nns_edge_send_thread (void *thread_data) nns_edge_loge ("Failed to send data via MQTT connection."); break; case NNS_EDGE_CONNECT_TYPE_CUSTOM: - { - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) { - ret = custom_h->nns_edge_custom_send_data (eh->custom.priv, data_h); - if (NNS_EDGE_ERROR_NONE != ret) - nns_edge_loge ("Failed to send data via custom connection."); - } + ret = nns_edge_custom_send_data (&eh->custom, data_h); + if (NNS_EDGE_ERROR_NONE != ret) + nns_edge_loge ("Failed to send data via custom connection."); break; - } default: break; } @@ -1337,43 +1321,6 @@ error: return ret; } -/** - * @brief Load custom lib and get edge custom instance. - */ -static int -_nns_edge_load_custom_library (nns_edge_handle_s * eh, const char *lib_path) -{ - void *handle; - nns_edge_custom_s *custom_h; - - handle = dlopen (lib_path, RTLD_LAZY); - if (NULL == handle) { - nns_edge_loge ("Failed to open custom lib: %s", dlerror ()); - return NNS_EDGE_ERROR_UNKNOWN; - } - - void *(*custom_get_instance) () = - (void *(*)()) dlsym (handle, "nns_edge_custom_get_instance"); - if (!custom_get_instance) { - nns_edge_loge ("Failed to find nns_edge_custom_get_instance: %s", - dlerror ()); - dlclose (handle); - return NNS_EDGE_ERROR_UNKNOWN; - } - - custom_h = (nns_edge_custom_s *) custom_get_instance (); - if (!custom_h) { - nns_edge_loge ("Failed to get custom instance from library."); - dlclose (handle); - return NNS_EDGE_ERROR_UNKNOWN; - } - - eh->custom.dl_handle = handle; - eh->custom.instance = custom_h; - - return NNS_EDGE_ERROR_NONE; -} - /** * @brief Create edge custom handle. */ @@ -1383,7 +1330,6 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path, { int ret = NNS_EDGE_ERROR_NONE; nns_edge_handle_s *eh; - nns_edge_custom_s *custom_h; if (node_type < 0 || node_type >= NNS_EDGE_NODE_TYPE_UNKNOWN) { nns_edge_loge ("Invalid param, set exact node type."); @@ -1409,20 +1355,7 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path, eh = (nns_edge_handle_s *) (*edge_h); eh->connect_type = NNS_EDGE_CONNECT_TYPE_CUSTOM; - ret = _nns_edge_load_custom_library (eh, lib_path); - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge - ("Failed to load custom lib. Please check the custom lib path or permission."); - goto error; - } - - custom_h = (nns_edge_custom_s *) eh->custom.instance; - ret = custom_h->nns_edge_custom_create (&eh->custom.priv); - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to create custom connection handle."); - } - -error: + ret = nns_edge_custom_load (&eh->custom, lib_path); if (ret != NNS_EDGE_ERROR_NONE) nns_edge_release_handle (eh); @@ -1464,8 +1397,8 @@ nns_edge_create_handle (const char *id, nns_edge_connect_type_e connect_type, nns_edge_loge ("Failed to create edge handle."); return ret; } - eh = (nns_edge_handle_s *) * edge_h; + eh = (nns_edge_handle_s *) (*edge_h); eh->connect_type = connect_type; if (NNS_EDGE_CONNECT_TYPE_AITT == connect_type) { @@ -1483,35 +1416,6 @@ error: return ret; } -/** - * @brief Start the nnstreamer edge custom. - */ -static int -_nns_edge_custom_start (nns_edge_handle_s * eh) -{ - int ret = NNS_EDGE_ERROR_INVALID_PARAMETER; - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) - ret = custom_h->nns_edge_custom_start (eh->custom.priv); - - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to start edge custom connection."); - return ret; - } - - ret = custom_h->nns_edge_custom_set_event_cb (eh->custom.priv, eh->event_cb, - eh->user_data); - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to set event callback to custom connection."); - return ret; - } - - ret = _nns_edge_create_send_thread (eh); - - return ret; -} - /** * @brief Start the nnstreamer edge. */ @@ -1535,10 +1439,12 @@ nns_edge_start (nns_edge_h edge_h) nns_edge_lock (eh); if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - ret = _nns_edge_custom_start (edge_h); - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to start edge custom connection"); - } + ret = nns_edge_custom_start (&eh->custom); + if (NNS_EDGE_ERROR_NONE == ret) + ret = _nns_edge_create_send_thread (eh); + + if (NNS_EDGE_ERROR_NONE != ret) + nns_edge_loge ("Failed to start edge custom connection."); goto done; } @@ -1629,25 +1535,6 @@ done: return ret; } -/** - * @brief Stop the nnstreamer edge custom connection. - */ -static int -_nns_edge_custom_stop (nns_edge_handle_s * eh) -{ - int ret = NNS_EDGE_ERROR_INVALID_PARAMETER; - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) - ret = custom_h->nns_edge_custom_stop (eh->custom.priv); - - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to stop nns edge custom connection."); - } - - return ret; -} - /** * @brief Stop the nnstreamer edge. */ @@ -1675,37 +1562,17 @@ nns_edge_stop (nns_edge_h edge_h) } if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - ret = _nns_edge_custom_stop (eh); + ret = nns_edge_custom_stop (&eh->custom); } - eh->is_started = FALSE; + if (NNS_EDGE_ERROR_NONE == ret) + eh->is_started = FALSE; done: nns_edge_unlock (eh); return ret; } -static void -_nns_edge_custom_release (nns_edge_handle_s * eh) -{ - int ret = NNS_EDGE_ERROR_INVALID_PARAMETER; - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) - ret = custom_h->nns_edge_custom_close (eh->custom.priv); - - if (NNS_EDGE_ERROR_NONE != ret) { - nns_edge_loge ("Failed to stop nns edge custom connection."); - } - - if (eh->custom.dl_handle) - dlclose (eh->custom.dl_handle); - - eh->custom.dl_handle = NULL; - eh->custom.instance = NULL; - eh->custom.priv = NULL; -} - /** * @brief Release the given handle. */ @@ -1742,7 +1609,7 @@ nns_edge_release_handle (nns_edge_h edge_h) } break; case NNS_EDGE_CONNECT_TYPE_CUSTOM: - _nns_edge_custom_release (eh); + nns_edge_custom_release (&eh->custom); break; default: break; @@ -1819,15 +1686,22 @@ nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb, NNS_EDGE_EVENT_CALLBACK_RELEASED, NULL, 0, NULL); if (ret != NNS_EDGE_ERROR_NONE) { nns_edge_loge ("Failed to set new event callback."); - nns_edge_unlock (eh); - return ret; + goto error; + } + + if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { + ret = nns_edge_custom_set_event_callback (&eh->custom, cb, user_data); + if (NNS_EDGE_ERROR_NONE != ret) { + goto error; + } } eh->event_cb = cb; eh->user_data = user_data; +error: nns_edge_unlock (eh); - return NNS_EDGE_ERROR_NONE; + return ret; } /** @@ -1986,15 +1860,8 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port) goto done; } } else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) - ret = custom_h->nns_edge_custom_connect (eh->custom.priv); - else - ret = NNS_EDGE_ERROR_INVALID_PARAMETER; - + ret = nns_edge_custom_connect (&eh->custom); if (ret != NNS_EDGE_ERROR_NONE) { - nns_edge_loge ("Failed to connect to custom connection."); goto done; } } else { @@ -2064,12 +1931,7 @@ nns_edge_is_connected (nns_edge_h edge_h) return NNS_EDGE_ERROR_NONE; if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (custom_h) - return custom_h->nns_edge_custom_is_connected (eh->custom.priv); - else - return NNS_EDGE_ERROR_CONNECTION_FAILURE; + return nns_edge_custom_is_connected (&eh->custom); } conn_data = (nns_edge_conn_data_s *) eh->connections; @@ -2244,13 +2106,8 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value) if (ret == NNS_EDGE_ERROR_NONE && NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; - - if (!custom_h) { - ret = NNS_EDGE_ERROR_UNKNOWN; - } else if (custom_h->nns_edge_custom_set_option) { - ret = custom_h->nns_edge_custom_set_option (eh->custom.priv, key, value); - } + /* Pass value to custom library and ignore error. */ + nns_edge_custom_set_info (&eh->custom, key, value); } nns_edge_unlock (eh); @@ -2321,14 +2178,13 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value) if (ret == NNS_EDGE_ERROR_NONE && NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance; + char *val = NULL; - if (!custom_h) { - ret = NNS_EDGE_ERROR_UNKNOWN; - } else if (custom_h->nns_edge_custom_get_option) { - /* Release old value, then get from custom library. */ + if (nns_edge_custom_get_info (&eh->custom, key, &val) == + NNS_EDGE_ERROR_NONE) { + /* Replace value from custom library. */ SAFE_FREE (*value); - *value = custom_h->nns_edge_custom_get_option (eh->custom.priv, key); + *value = val; } } diff --git a/tests/nnstreamer-edge-custom-test.c b/tests/nnstreamer-edge-custom-test.c index 48f599f..4dbd092 100644 --- a/tests/nnstreamer-edge-custom-test.c +++ b/tests/nnstreamer-edge-custom-test.c @@ -27,7 +27,7 @@ static int nns_edge_custom_close (void *priv) { if (!priv) { - nns_edge_loge ("Invalid param, priv should not be null."); + nns_edge_loge ("Invalid param, handle should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; @@ -67,7 +67,7 @@ static int nns_edge_custom_start (void *priv) { if (!priv) { - nns_edge_loge ("Invalid param, priv should not be null."); + nns_edge_loge ("Invalid param, handle should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; @@ -80,7 +80,7 @@ static int nns_edge_custom_stop (void *priv) { if (!priv) { - nns_edge_loge ("Invalid param, priv should not be null."); + nns_edge_loge ("Invalid param, handle should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; @@ -93,7 +93,7 @@ static int nns_edge_custom_connect (void *priv) { if (!priv) { - nns_edge_loge ("Invalid param, priv is NULL"); + nns_edge_loge ("Invalid param, handle should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; @@ -126,8 +126,8 @@ nns_edge_custom_is_connected (void *priv) static int nns_edge_custom_set_event_cb (void *priv, nns_edge_event_cb cb, void *user_data) { - if (!priv || !cb) { - nns_edge_loge ("Invalid param, cb(%p)", cb); + if (!priv) { + nns_edge_loge ("Invalid param, handle should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } @@ -138,7 +138,7 @@ static int nns_edge_custom_send_data (void *priv, nns_edge_data_h data_h) { if (!priv || !data_h) { - nns_edge_loge ("Invalid param, data_h(%p)", data_h); + nns_edge_loge ("Invalid param, handle or data should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } @@ -146,10 +146,10 @@ nns_edge_custom_send_data (void *priv, nns_edge_data_h data_h) } static int -nns_edge_custom_set_option (void *priv, const char *key, const char *value) +nns_edge_custom_set_info (void *priv, const char *key, const char *value) { if (!priv || !key || !value) { - nns_edge_loge ("Invalid param, key(%s), value(%s)", key, value); + nns_edge_loge ("Invalid param, handle, key or value should not be null."); return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; @@ -157,26 +157,29 @@ nns_edge_custom_set_option (void *priv, const char *key, const char *value) if (strcasecmp (key, "PEER_ADDRESS") == 0) { SAFE_FREE (custom_h->peer_address); custom_h->peer_address = nns_edge_strdup (value); - } else { - nns_edge_loge ("key(%s) does not supported.", key); - return NNS_EDGE_ERROR_INVALID_PARAMETER; + return NNS_EDGE_ERROR_NONE; } - return NNS_EDGE_ERROR_NONE; + nns_edge_loge ("The key '%s' is not supported.", key); + return NNS_EDGE_ERROR_INVALID_PARAMETER; } -static char * -nns_edge_custom_get_option (void *priv, const char *key) +static int +nns_edge_custom_get_info (void *priv, const char *key, char **value) { - if (!priv || !key) { - nns_edge_loge ("Invalid param, key(%s)", key); - return NULL; + if (!priv || !key || !value) { + nns_edge_loge ("Invalid param, handle, key or value should not be null."); + return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv; - if (strcasecmp (key, "PEER_ADDRESS") == 0) - return nns_edge_strdup (custom_h->peer_address); - return NULL; + if (strcasecmp (key, "PEER_ADDRESS") == 0) { + *value = nns_edge_strdup (custom_h->peer_address); + return NNS_EDGE_ERROR_NONE; + } + + nns_edge_loge ("The key '%s' is not supported.", key); + return NNS_EDGE_ERROR_INVALID_PARAMETER; } nns_edge_custom_s edge_custom_h = { @@ -190,11 +193,11 @@ nns_edge_custom_s edge_custom_h = { .nns_edge_custom_is_connected = nns_edge_custom_is_connected, .nns_edge_custom_set_event_cb = nns_edge_custom_set_event_cb, .nns_edge_custom_send_data = nns_edge_custom_send_data, - .nns_edge_custom_set_option = nns_edge_custom_set_option, - .nns_edge_custom_get_option = nns_edge_custom_get_option + .nns_edge_custom_set_info = nns_edge_custom_set_info, + .nns_edge_custom_get_info = nns_edge_custom_get_info }; -void * +const nns_edge_custom_s * nns_edge_custom_get_instance () { return &edge_custom_h;