[Custom] separate custom impl
authorJaeyun Jung <jy1210.jung@samsung.com>
Thu, 22 Aug 2024 01:20:54 +0000 (10:20 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Thu, 22 Aug 2024 04:44:41 +0000 (13:44 +0900)
Separate custom connection implementation and update callbacks.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
include/nnstreamer-edge-custom.h
jni/nnstreamer-edge.mk
src/CMakeLists.txt
src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c [new file with mode: 0644]
src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h [new file with mode: 0644]
src/libnnstreamer-edge/nnstreamer-edge-internal.c
tests/nnstreamer-edge-custom-test.c

index eee65048562db35db8f28360cfcf343b390271d6..b069b6cda283cd61cd088db876e1a9ea29678cee 100644 (file)
@@ -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
 }
index de181eb78dd502d207fc183dfa7ba98cbb4b5941..6cd740ae2bdb2cdf94067c42b93f28bd13e5f5ec 100644 (file)
@@ -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 \
index acff3a85cbc9e49f05a4efd946e802e2aa65cc1f..0ae6eae9bd9791285fe51acf507475712625d117 100644 (file)
@@ -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 (file)
index 0000000..de4c370
--- /dev/null
@@ -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 <gichan2.jang@samsung.com>
+ * @bug    No known bugs except for NYI items
+ */
+
+#include <dlfcn.h>
+
+#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 (file)
index 0000000..250481b
--- /dev/null
@@ -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 <gichan2.jang@samsung.com>
+ * @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__ */
index 7e4c541fe3f00c360acb32b37264a2b0907a3e42..1d9c43ba1dd6ad215da6b428fde3688be1604d14 100644 (file)
@@ -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
  */
 #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;
     }
   }
 
index 48f599fbf1579cf0795b07650711decb45ec1e78..4dbd09260ce97b0b70c898fe0c9caa97ed7cbd26 100644 (file)
@@ -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;