From 50d7f09bec491b7f95347c890a9f83ead50f6e68 Mon Sep 17 00:00:00 2001
From: Gichan Jang <gichan2.jang@samsung.com>
Date: Thu, 23 Jan 2025 10:03:07 +0900
Subject: [PATCH] Add stop discovery function

 - Add stop discovery function
 - Rename discover to start_discovery

Signed-off-by: Gichan Jang <gichan2.jang@samsung.com>
---
 include/nnstreamer-edge-custom.h              |  3 +-
 include/nnstreamer-edge.h                     | 14 ++++++--
 .../nnstreamer-edge-custom-impl.c             | 31 +++++++++++++---
 .../nnstreamer-edge-custom-impl.h             | 12 +++++--
 .../nnstreamer-edge-internal.c                | 36 +++++++++++++++++--
 tests/nnstreamer-edge-custom-test.c           | 16 +++++++--
 tests/unittest_nnstreamer-edge-custom.cc      | 20 +++++++++--
 7 files changed, 114 insertions(+), 18 deletions(-)

diff --git a/include/nnstreamer-edge-custom.h b/include/nnstreamer-edge-custom.h
index de33928..95b5594 100644
--- a/include/nnstreamer-edge-custom.h
+++ b/include/nnstreamer-edge-custom.h
@@ -34,7 +34,8 @@ typedef struct
   int (*nns_edge_custom_connect) (void *priv);
   int (*nns_edge_custom_subscribe) (void *priv);
   int (*nns_edge_custom_is_connected) (void *priv);
-  int (*nns_edge_custom_discover) (void *priv);
+  int (*nns_edge_custom_start_discovery) (void *priv);
+  int (*nns_edge_custom_stop_discovery) (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_info) (void *priv, const char *key, const char *value);
diff --git a/include/nnstreamer-edge.h b/include/nnstreamer-edge.h
index e47109e..a5d82d1 100644
--- a/include/nnstreamer-edge.h
+++ b/include/nnstreamer-edge.h
@@ -214,14 +214,24 @@ int nns_edge_release_handle (nns_edge_h edge_h);
 int nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb, void *user_data);
 
 /**
- * @brief Discover connectable devices within the network.
+ * @brief Start discovery connectable devices within the network.
  * @param[in] edge_h The edge handle.
  * @return 0 on success. Otherwise a negative error value.
  * @retval #NNS_EDGE_ERROR_NONE Successful.
  * @retval #NNS_EDGE_ERROR_NOT_SUPPORTED Not supported.
  * @retval #NNS_EDGE_ERROR_INVALID_PARAMETER Given parameter is invalid.
  */
-int nns_edge_discover (nns_edge_h edge_h);
+int nns_edge_start_discovery (nns_edge_h edge_h);
+
+/**
+ * @brief Stop discovery connectable devices within the network.
+ * @param[in] edge_h The edge handle.
+ * @return 0 on success. Otherwise a negative error value.
+ * @retval #NNS_EDGE_ERROR_NONE Successful.
+ * @retval #NNS_EDGE_ERROR_NOT_SUPPORTED Not supported.
+ * @retval #NNS_EDGE_ERROR_INVALID_PARAMETER Given parameter is invalid.
+ */
+int nns_edge_stop_discovery (nns_edge_h edge_h);
 
 /**
  * @brief Connect to the destination node. In the case of Hybrid and MQTT, the TOPIC, DEST_HOST and DEST_PORT must be set before connection using nns_edge_set_info().
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
index 93c5bc4..3d4dee1 100644
--- a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
+++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
@@ -221,10 +221,10 @@ nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle,
 }
 
 /**
- * @brief Internal function to discover devices of custom connection.
+ * @brief Internal function to start discovery devices of custom connection.
  */
 int
-nns_edge_custom_discover (nns_edge_custom_connection_h handle)
+nns_edge_custom_start_discovery (nns_edge_custom_connection_h handle)
 {
   custom_connection_s *custom = (custom_connection_s *) handle;
   nns_edge_custom_s *custom_h;
@@ -235,9 +235,32 @@ nns_edge_custom_discover (nns_edge_custom_connection_h handle)
 
   custom_h = custom->instance;
 
-  ret = custom_h->nns_edge_custom_discover (custom->priv);
+  ret = custom_h->nns_edge_custom_start_discovery (custom->priv);
   if (NNS_EDGE_ERROR_NONE != ret) {
-    nns_edge_loge ("Failed to discover devices of custom connection.");
+    nns_edge_loge ("Failed to start discovery devices of custom connection.");
+  }
+
+  return ret;
+}
+
+/**
+ * @brief Internal function to stop discovery devices of custom connection.
+ */
+int
+nns_edge_custom_stop_discovery (nns_edge_custom_connection_h handle)
+{
+  custom_connection_s *custom = (custom_connection_s *) handle;
+  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_discovery (custom->priv);
+  if (NNS_EDGE_ERROR_NONE != ret) {
+    nns_edge_loge ("Failed to stop discovery devices of custom connection.");
   }
 
   return ret;
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
index 2fbe1f0..2a98992 100644
--- a/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
+++ b/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
@@ -43,9 +43,14 @@ int nns_edge_custom_start (nns_edge_custom_connection_h handle);
 int nns_edge_custom_stop (nns_edge_custom_connection_h handle);
 
 /**
- * @brief Internal function to discover devices of custom connection.
+ * @brief Internal function to start discovery devices of custom connection.
  */
-int nns_edge_custom_discover (nns_edge_custom_connection_h handle);
+int nns_edge_custom_start_discovery (nns_edge_custom_connection_h handle);
+
+/**
+ * @brief Internal function to stop discovery devices of custom connection.
+ */
+int nns_edge_custom_stop_discovery (nns_edge_custom_connection_h handle);
 
 /**
  * @brief Internal function to set the event callback of custom connection.
@@ -81,7 +86,8 @@ int nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *k
 #define nns_edge_custom_release(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_custom_start(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_custom_stop(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
-#define nns_edge_custom_discover(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
+#define nns_edge_custom_start_discovery(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
+#define nns_edge_custom_stop_discovery(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_custom_set_event_callback(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_custom_connect(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_custom_is_connected(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c
index a6a74bf..762815d 100644
--- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c
+++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c
@@ -2145,9 +2145,9 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value)
 }
 
 /**
- * @brief Discover connectable devices within the network.
+ * @brief Start discovery connectable devices within the network.
  */
-int nns_edge_discover (nns_edge_h edge_h)
+int nns_edge_start_discovery (nns_edge_h edge_h)
 {
   nns_edge_handle_s *eh;
   int ret = NNS_EDGE_ERROR_NONE;
@@ -2171,7 +2171,37 @@ int nns_edge_discover (nns_edge_h edge_h)
   }
 
   if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
-    ret = nns_edge_custom_discover (eh->custom_connection_h);
+    ret = nns_edge_custom_start_discovery (eh->custom_connection_h);
+  }
+
+  nns_edge_unlock (eh);
+
+  return ret;
+}
+
+/**
+ * @brief Stop discovery connectable devices within the network.
+ */
+int nns_edge_stop_discovery (nns_edge_h edge_h)
+{
+  nns_edge_handle_s *eh;
+  int ret = NNS_EDGE_ERROR_NONE;
+
+  eh = (nns_edge_handle_s *) edge_h;
+  if (!eh) {
+    nns_edge_loge ("Invalid param, given edge handle is null.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
+  if (!nns_edge_handle_is_valid (eh)) {
+    nns_edge_loge ("Invalid param, given edge handle is invalid.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
+  nns_edge_lock (eh);
+
+  if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
+    ret = nns_edge_custom_stop_discovery (eh->custom_connection_h);
   }
 
   nns_edge_unlock (eh);
diff --git a/tests/nnstreamer-edge-custom-test.c b/tests/nnstreamer-edge-custom-test.c
index 7c00b03..b627d9a 100644
--- a/tests/nnstreamer-edge-custom-test.c
+++ b/tests/nnstreamer-edge-custom-test.c
@@ -110,7 +110,7 @@ nns_edge_custom_subscribe (void *priv)
 
 
 static int
-nns_edge_custom_discover (void *priv)
+nns_edge_custom_start_discovery (void *priv)
 {
   int ret = NNS_EDGE_ERROR_NONE;
 
@@ -125,6 +125,17 @@ nns_edge_custom_discover (void *priv)
   return ret;
 }
 
+static int
+nns_edge_custom_stop_discovery (void *priv)
+{
+  if (!priv) {
+    nns_edge_loge ("Invalid param, handle should not be null.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
+  return NNS_EDGE_ERROR_NONE;
+}
+
 static int
 nns_edge_custom_is_connected (void *priv)
 {
@@ -208,7 +219,8 @@ nns_edge_custom_s edge_custom_h = {
   .nns_edge_custom_create = nns_edge_custom_create,
   .nns_edge_custom_close = nns_edge_custom_close,
   .nns_edge_custom_start = nns_edge_custom_start,
-  .nns_edge_custom_discover = nns_edge_custom_discover,
+  .nns_edge_custom_start_discovery = nns_edge_custom_start_discovery,
+  .nns_edge_custom_stop_discovery = nns_edge_custom_stop_discovery,
   .nns_edge_custom_stop = nns_edge_custom_stop,
   .nns_edge_custom_connect = nns_edge_custom_connect,
   .nns_edge_custom_subscribe = nns_edge_custom_subscribe,
diff --git a/tests/unittest_nnstreamer-edge-custom.cc b/tests/unittest_nnstreamer-edge-custom.cc
index 0b565b5..0f0c55a 100644
--- a/tests/unittest_nnstreamer-edge-custom.cc
+++ b/tests/unittest_nnstreamer-edge-custom.cc
@@ -119,10 +119,13 @@ TEST (edgeCustom, expectedReturn)
   ret = nns_edge_start (edge_h);
   EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
 
-  ret = nns_edge_discover (edge_h);
+  ret = nns_edge_start_discovery (edge_h);
   EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
   EXPECT_EQ (1, device_found);
 
+  ret = nns_edge_stop_discovery (edge_h);
+  EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
+
   ret = nns_edge_is_connected (edge_h);
   EXPECT_EQ (NNS_EDGE_ERROR_CONNECTION_FAILURE, ret);
 
@@ -218,11 +221,22 @@ TEST (edgeCustom, stopInvalidParam01_n)
 /**
  * @brief Set event callback of edge custom - invalid param.
  */
-TEST (edgeCustom, discoverInvalidParam01_n)
+TEST (edgeCustom, startDiscoveryInvalidParam01_n)
+{
+  int ret;
+
+  ret = nns_edge_custom_start_discovery (NULL);
+  EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
+}
+
+/**
+ * @brief Set event callback of edge custom - invalid param.
+ */
+TEST (edgeCustom, stopDiscoveryInvalidParam01_n)
 {
   int ret;
 
-  ret = nns_edge_custom_discover (NULL);
+  ret = nns_edge_custom_stop_discovery (NULL);
   EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
 }