diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c index 08352d4..da91531 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c @@ -16,6 +16,16 @@ #include "nnstreamer-edge-log.h" #include "nnstreamer-edge-util.h" +/** + * @brief Internal data structure for edge custom connection. + */ +typedef struct +{ + void *dl_handle; + nns_edge_custom_s *instance; + void *priv; +} custom_connection_s; + typedef const nns_edge_custom_s *custom_get_instance (void); /** @@ -65,17 +75,25 @@ _load_custom_library (custom_connection_s * custom, const char *lib_path) * @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_load (const char *lib_path, + nns_edge_custom_connection_h * handle) { + custom_connection_s *custom; nns_edge_custom_s *custom_h; int ret; if (!STR_IS_VALID (lib_path)) return NNS_EDGE_ERROR_INVALID_PARAMETER; - if (!custom) + if (!handle) return NNS_EDGE_ERROR_INVALID_PARAMETER; + custom = (custom_connection_s *) calloc (1, sizeof (custom_connection_s)); + if (!custom) { + nns_edge_loge ("Failed to allocate memory for edge custom connection."); + return NNS_EDGE_ERROR_OUT_OF_MEMORY; + } + ret = _load_custom_library (custom, lib_path); if (NNS_EDGE_ERROR_NONE != ret) { nns_edge_loge @@ -91,7 +109,9 @@ nns_edge_custom_load (custom_connection_s * custom, const char *lib_path) } error: - if (NNS_EDGE_ERROR_NONE != ret) { + if (NNS_EDGE_ERROR_NONE == ret) { + *handle = custom; + } else { nns_edge_custom_release (custom); } @@ -102,8 +122,9 @@ 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) +nns_edge_custom_release (nns_edge_custom_connection_h handle) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -125,6 +146,7 @@ nns_edge_custom_release (custom_connection_s * custom) custom->instance = NULL; custom->priv = NULL; + free (custom); return ret; } @@ -132,8 +154,9 @@ nns_edge_custom_release (custom_connection_s * custom) * @brief Internal function to start custom connection. */ int -nns_edge_custom_start (custom_connection_s * custom) +nns_edge_custom_start (nns_edge_custom_connection_h handle) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -154,8 +177,9 @@ nns_edge_custom_start (custom_connection_s * custom) * @brief Internal function to stop custom connection. */ int -nns_edge_custom_stop (custom_connection_s * custom) +nns_edge_custom_stop (nns_edge_custom_connection_h handle) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -176,9 +200,10 @@ 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_custom_set_event_callback (nns_edge_custom_connection_h handle, nns_edge_event_cb cb, void *user_data) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -199,8 +224,9 @@ nns_edge_custom_set_event_callback (custom_connection_s * custom, * @brief Internal function to connect custom connection. */ int -nns_edge_custom_connect (custom_connection_s * custom) +nns_edge_custom_connect (nns_edge_custom_connection_h handle) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -221,8 +247,9 @@ 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) +nns_edge_custom_is_connected (nns_edge_custom_connection_h handle) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; if (!custom || !custom->instance) @@ -237,8 +264,10 @@ 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) +nns_edge_custom_send_data (nns_edge_custom_connection_h handle, + nns_edge_data_h data_h) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret; @@ -263,9 +292,10 @@ 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, +nns_edge_custom_set_info (nns_edge_custom_connection_h handle, const char *key, const char *value) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret = NNS_EDGE_ERROR_NOT_SUPPORTED; @@ -291,9 +321,10 @@ nns_edge_custom_set_info (custom_connection_s * custom, const char *key, * @brief Internal function to get information from custom connection. */ int -nns_edge_custom_get_info (custom_connection_s * custom, const char *key, +nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key, char **value) { + custom_connection_s *custom = (custom_connection_s *) handle; nns_edge_custom_s *custom_h; int ret = NNS_EDGE_ERROR_NOT_SUPPORTED; diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h index 250481b..cee219e 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h +++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h @@ -19,65 +19,57 @@ 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; +typedef void *nns_edge_custom_connection_h; /** * @brief Internal function to load custom connection from library. */ -int nns_edge_custom_load (custom_connection_s *custom, const char *lib_path); +int nns_edge_custom_load (const char *lib_path, nns_edge_custom_connection_h *handle); /** * @brief Internal function to release custom connection. */ -int nns_edge_custom_release (custom_connection_s *custom); +int nns_edge_custom_release (nns_edge_custom_connection_h handle); /** * @brief Internal function to start custom connection. */ -int nns_edge_custom_start (custom_connection_s *custom); +int nns_edge_custom_start (nns_edge_custom_connection_h handle); /** * @brief Internal function to stop custom connection. */ -int nns_edge_custom_stop (custom_connection_s *custom); +int nns_edge_custom_stop (nns_edge_custom_connection_h handle); /** * @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); +int nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle, nns_edge_event_cb cb, void *user_data); /** * @brief Internal function to connect custom connection. */ -int nns_edge_custom_connect (custom_connection_s *custom); +int nns_edge_custom_connect (nns_edge_custom_connection_h handle); /** * @brief Internal function to check custom connection. */ -int nns_edge_custom_is_connected (custom_connection_s *custom); +int nns_edge_custom_is_connected (nns_edge_custom_connection_h handle); /** * @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); +int nns_edge_custom_send_data (nns_edge_custom_connection_h handle, 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); +int nns_edge_custom_set_info (nns_edge_custom_connection_h handle, 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); +int nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key, char **value); #ifdef __cplusplus } diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index 7adf061..498fd75 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -75,7 +75,7 @@ typedef struct void *broker_h; /* Data for custom connection */ - custom_connection_s custom; + nns_edge_custom_connection_h custom_connection_h; } nns_edge_handle_s; /** @@ -919,7 +919,7 @@ _nns_edge_send_thread (void *thread_data) nns_edge_loge ("Failed to send data via MQTT connection."); break; case NNS_EDGE_CONNECT_TYPE_CUSTOM: - ret = nns_edge_custom_send_data (&eh->custom, data_h); + ret = nns_edge_custom_send_data (eh->custom_connection_h, data_h); if (NNS_EDGE_ERROR_NONE != ret) nns_edge_loge ("Failed to send data via custom connection."); break; @@ -1296,9 +1296,7 @@ _nns_edge_create_handle (const char *id, nns_edge_node_type_e node_type, eh->sending = false; eh->listener_fd = -1; eh->caps_str = nns_edge_strdup (""); - eh->custom.dl_handle = NULL; - eh->custom.instance = NULL; - eh->custom.priv = NULL; + eh->custom_connection_h = NULL; ret = nns_edge_metadata_create (&eh->metadata); if (ret != NNS_EDGE_ERROR_NONE) { @@ -1355,7 +1353,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_custom_load (&eh->custom, lib_path); + ret = nns_edge_custom_load (lib_path, &eh->custom_connection_h); if (ret != NNS_EDGE_ERROR_NONE) nns_edge_release_handle (eh); @@ -1427,7 +1425,7 @@ 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 (&eh->custom); + ret = nns_edge_custom_start (eh->custom_connection_h); if (NNS_EDGE_ERROR_NONE == ret) ret = _nns_edge_create_send_thread (eh); @@ -1536,7 +1534,7 @@ nns_edge_stop (nns_edge_h edge_h) } if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - ret = nns_edge_custom_stop (&eh->custom); + ret = nns_edge_custom_stop (eh->custom_connection_h); } if (NNS_EDGE_ERROR_NONE == ret) @@ -1578,7 +1576,7 @@ nns_edge_release_handle (nns_edge_h edge_h) } break; case NNS_EDGE_CONNECT_TYPE_CUSTOM: - if (nns_edge_custom_release (&eh->custom) != + if (nns_edge_custom_release (eh->custom_connection_h) != NNS_EDGE_ERROR_NONE) { nns_edge_logw ("Failed to close custom connection."); } @@ -1662,7 +1660,8 @@ nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb, } if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - ret = nns_edge_custom_set_event_callback (&eh->custom, cb, user_data); + ret = nns_edge_custom_set_event_callback (eh->custom_connection_h, + cb, user_data); if (NNS_EDGE_ERROR_NONE != ret) { goto error; } @@ -1811,7 +1810,7 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port) } } } else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - ret = nns_edge_custom_connect (&eh->custom); + ret = nns_edge_custom_connect (eh->custom_connection_h); if (ret != NNS_EDGE_ERROR_NONE) { goto done; } @@ -1878,7 +1877,7 @@ nns_edge_is_connected (nns_edge_h edge_h) return NNS_EDGE_ERROR_NONE; if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { - return nns_edge_custom_is_connected (&eh->custom); + return nns_edge_custom_is_connected (eh->custom_connection_h); } conn_data = (nns_edge_conn_data_s *) eh->connections; @@ -2043,7 +2042,7 @@ 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) { /* Pass value to custom library and ignore error. */ - if (nns_edge_custom_set_info (&eh->custom, key, value) != + if (nns_edge_custom_set_info (eh->custom_connection_h, key, value) != NNS_EDGE_ERROR_NONE) { nns_edge_logw ("Failed to set info '%s' in custom connection.", key); } @@ -2119,7 +2118,7 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value) NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) { char *val = NULL; - if (nns_edge_custom_get_info (&eh->custom, key, &val) == + if (nns_edge_custom_get_info (eh->custom_connection_h, key, &val) == NNS_EDGE_ERROR_NONE) { /* Replace value from custom library. */ SAFE_FREE (*value); diff --git a/tests/unittest_nnstreamer-edge-custom.cc b/tests/unittest_nnstreamer-edge-custom.cc index 07ffb6c..df04018 100644 --- a/tests/unittest_nnstreamer-edge-custom.cc +++ b/tests/unittest_nnstreamer-edge-custom.cc @@ -8,6 +8,7 @@ */ #include +#include "nnstreamer-edge-custom-impl.h" #include "nnstreamer-edge-custom.h" #include "nnstreamer-edge-data.h" #include "nnstreamer-edge-event.h" @@ -126,6 +127,245 @@ TEST (edgeCustom, expectedReturn) EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); } +/** + * @brief Load edge custom - invalid param. + */ +TEST (edgeCustom, loadInvalidParam01_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load (NULL, &handle); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Load edge custom - invalid param. + */ +TEST (edgeCustom, loadInvalidParam02_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("", &handle); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Load edge custom - invalid param. + */ +TEST (edgeCustom, loadInvalidParam03_n) +{ + int ret; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Release edge custom - invalid param. + */ +TEST (edgeCustom, releaseInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_release (NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Start edge custom - invalid param. + */ +TEST (edgeCustom, startInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_start (NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Stop edge custom - invalid param. + */ +TEST (edgeCustom, stopInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_stop (NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Set event callback of edge custom - invalid param. + */ +TEST (edgeCustom, setEventCbInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_set_event_callback (NULL, NULL, NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Connect edge custom - invalid param. + */ +TEST (edgeCustom, connectInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_connect (NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Check connection of edge custom - invalid param. + */ +TEST (edgeCustom, isConnectedInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_is_connected (NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Send data using edge custom - invalid param. + */ +TEST (edgeCustom, sendDataInvalidParam01_n) +{ + int ret; + nns_edge_data_h data_h; + + ret = nns_edge_data_create (&data_h); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_send_data (NULL, data_h); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_data_destroy (data_h); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Send data using edge custom - invalid param. + */ +TEST (edgeCustom, sendDataInvalidParam02_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", &handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_send_data (handle, NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_release (handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Set info using edge custom - invalid param. + */ +TEST (edgeCustom, setInfoInvalidParam01_n) +{ + int ret; + + ret = nns_edge_custom_set_info (NULL, "test-key", "test-value"); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Set info using edge custom - invalid param. + */ +TEST (edgeCustom, setInfoInvalidParam02_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", &handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_set_info (handle, NULL, "test-value"); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + ret = nns_edge_custom_set_info (handle, "", "test-value"); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_release (handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Set info using edge custom - invalid param. + */ +TEST (edgeCustom, setInfoInvalidParam03_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", &handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_set_info (handle, "test-key", NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + ret = nns_edge_custom_set_info (handle, "test-key", ""); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_release (handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Get info using edge custom - invalid param. + */ +TEST (edgeCustom, getInfoInvalidParam01_n) +{ + int ret; + char *value; + + ret = nns_edge_custom_get_info (NULL, "test-key", &value); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Get info using edge custom - invalid param. + */ +TEST (edgeCustom, getInfoInvalidParam02_n) +{ + int ret; + char *value; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", &handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_get_info (handle, NULL, &value); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + ret = nns_edge_custom_get_info (handle, "", &value); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_release (handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + +/** + * @brief Get info using edge custom - invalid param. + */ +TEST (edgeCustom, getInfoInvalidParam03_n) +{ + int ret; + nns_edge_custom_connection_h handle; + + ret = nns_edge_custom_load ("libnnstreamer-edge-custom-test.so", &handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_get_info (handle, "test-key", NULL); + EXPECT_NE (NNS_EDGE_ERROR_NONE, ret); + + ret = nns_edge_custom_release (handle); + EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret); +} + /** * @brief Main gtest */