Skip to content

Commit

Permalink
Add stop discovery function
Browse files Browse the repository at this point in the history
 - Add stop discovery function
 - Rename discover to start_discovery

Signed-off-by: Gichan Jang <[email protected]>
  • Loading branch information
gichan-jang committed Jan 23, 2025
1 parent 15033ae commit c3f160d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/nnstreamer-edge-custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 12 additions & 2 deletions include/nnstreamer-edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
31 changes: 27 additions & 4 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 33 additions & 3 deletions src/libnnstreamer-edge/nnstreamer-edge-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
16 changes: 14 additions & 2 deletions tests/nnstreamer-edge-custom-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 17 additions & 3 deletions tests/unittest_nnstreamer-edge-custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c3f160d

Please sign in to comment.