Skip to content

Commit

Permalink
[Custom] Implement custom discovery function
Browse files Browse the repository at this point in the history
Implement custom discovery function and add related unit tests.

Signed-off-by: Gichan Jang <[email protected]>
  • Loading branch information
gichan-jang committed Jan 21, 2025
1 parent 23979ac commit 0ff4ddc
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/nnstreamer-edge-custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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_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
5 changes: 2 additions & 3 deletions include/nnstreamer-edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,14 @@ 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 Discovery connectable devices within the network.
* @brief Discover connectable devices within the network.
* @param[in] edge_h The edge handle.
* @param[in] user_data The user's custom data passed to discovery callback.
* @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_discovery (nns_edge_h edge_h, void *user_data);
int nns_edge_discover (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
23 changes: 23 additions & 0 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle,
return ret;
}

/**
* @brief Internal function to discover devices of custom connection.
*/
int
nns_edge_custom_discover (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_discover (custom->priv);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to discover devices of custom connection.");
}

return ret;
}

/**
* @brief Internal function to connect custom connection.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ 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.
*/
int nns_edge_custom_discover (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to set the event callback of custom connection.
*/
Expand Down Expand Up @@ -76,6 +81,7 @@ 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_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: 36 additions & 0 deletions src/libnnstreamer-edge/nnstreamer-edge-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2143,3 +2143,39 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value)

return ret;
}

/**
* @brief Discover connectable devices within the network.
*/
int nns_edge_discover (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 (!eh->event_cb) {
nns_edge_loge ("NNStreamer-edge event callback is not registered.");
nns_edge_unlock (eh);
return NNS_EDGE_ERROR_CONNECTION_FAILURE;
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
ret = nns_edge_custom_discovery (eh->custom_connection_h);
}

nns_edge_unlock (eh);

return ret;
}

22 changes: 22 additions & 0 deletions tests/nnstreamer-edge-custom-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ nns_edge_custom_subscribe (void *priv)
return NNS_EDGE_ERROR_NOT_SUPPORTED;
}


static int
nns_edge_custom_discover (void *priv)
{
int ret = NNS_EDGE_ERROR_NONE;

if (!priv) {
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;
ret = nns_edge_event_invoke_callback (custom_h->event_cb, custom_h->user_data,
NNS_EDGE_EVENT_DEVICE_FOUND, NULL, 0, NULL);

return ret;
}

static int
nns_edge_custom_is_connected (void *priv)
{
Expand All @@ -130,6 +147,10 @@ nns_edge_custom_set_event_cb (void *priv, nns_edge_event_cb cb, void *user_data)
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;

custom_h->event_cb = cb;
custom_h->user_data = user_data;

return NNS_EDGE_ERROR_NONE;
}
Expand Down Expand Up @@ -187,6 +208,7 @@ 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_stop = nns_edge_custom_stop,
.nns_edge_custom_connect = nns_edge_custom_connect,
.nns_edge_custom_subscribe = nns_edge_custom_subscribe,
Expand Down
33 changes: 32 additions & 1 deletion tests/unittest_nnstreamer-edge-custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ TEST (edgeCustom, createHandleInvalidParam03_n)
static int
_test_edge_event_cb (nns_edge_event_h event_h, void *user_data)
{
nns_edge_event_e event = NNS_EDGE_EVENT_UNKNOWN;
int ret;
int *device_found = (int *) user_data;

ret = nns_edge_event_get_type (event_h, &event);
EXPECT_EQ (ret, NNS_EDGE_ERROR_NONE);

switch (event) {
case NNS_EDGE_EVENT_DEVICE_FOUND:
(*device_found)++;
break;
default:
break;
}

return NNS_EDGE_ERROR_NONE;
}

Expand All @@ -86,12 +101,13 @@ TEST (edgeCustom, expectedReturn)
nns_edge_h edge_h = NULL;
nns_edge_data_h data_h = NULL;
char *ret_str = NULL;
int device_found = 0;

ret = nns_edge_custom_create_handle ("temp_id", "libnnstreamer-edge-custom-test.so",
NNS_EDGE_NODE_TYPE_QUERY_SERVER, &edge_h);
ASSERT_EQ (NNS_EDGE_ERROR_NONE, ret);

ret = nns_edge_set_event_callback (edge_h, _test_edge_event_cb, NULL);
ret = nns_edge_set_event_callback (edge_h, _test_edge_event_cb, &device_found);
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
ret = nns_edge_set_info (edge_h, "PEER_ADDRESS", "TE:MP:AD:DR:ES:SS");
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
Expand All @@ -103,6 +119,10 @@ TEST (edgeCustom, expectedReturn)
ret = nns_edge_start (edge_h);
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);

ret = nns_edge_discover (edge_h);
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
EXPECT_EQ (1, device_found);

ret = nns_edge_is_connected (edge_h);
EXPECT_EQ (NNS_EDGE_ERROR_CONNECTION_FAILURE, ret);

Expand Down Expand Up @@ -195,6 +215,17 @@ TEST (edgeCustom, stopInvalidParam01_n)
EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
}

/**
* @brief Set event callback of edge custom - invalid param.
*/
TEST (edgeCustom, discoverInvalidParam01_n)
{
int ret;

ret = nns_edge_custom_discover (NULL);
EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
}

/**
* @brief Set event callback of edge custom - invalid param.
*/
Expand Down

0 comments on commit 0ff4ddc

Please sign in to comment.