Skip to content

Commit a062e5f

Browse files
committed
[Custom] Implement custom discovery function
Implement custom discovery function and add related unit tests. Signed-off-by: Gichan Jang <[email protected]>
1 parent 23979ac commit a062e5f

7 files changed

+122
-4
lines changed

include/nnstreamer-edge-custom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef struct
3434
int (*nns_edge_custom_connect) (void *priv);
3535
int (*nns_edge_custom_subscribe) (void *priv);
3636
int (*nns_edge_custom_is_connected) (void *priv);
37+
int (*nns_edge_custom_discover) (void *priv);
3738
int (*nns_edge_custom_set_event_cb) (void *priv, nns_edge_event_cb cb, void *user_data);
3839
int (*nns_edge_custom_send_data) (void *priv, nns_edge_data_h data_h);
3940
int (*nns_edge_custom_set_info) (void *priv, const char *key, const char *value);

include/nnstreamer-edge.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,14 @@ int nns_edge_release_handle (nns_edge_h edge_h);
214214
int nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb, void *user_data);
215215

216216
/**
217-
* @brief Discovery connectable devices within the network.
217+
* @brief Discover connectable devices within the network.
218218
* @param[in] edge_h The edge handle.
219-
* @param[in] user_data The user's custom data passed to discovery callback.
220219
* @return 0 on success. Otherwise a negative error value.
221220
* @retval #NNS_EDGE_ERROR_NONE Successful.
222221
* @retval #NNS_EDGE_ERROR_NOT_SUPPORTED Not supported.
223222
* @retval #NNS_EDGE_ERROR_INVALID_PARAMETER Given parameter is invalid.
224223
*/
225-
int nns_edge_discovery (nns_edge_h edge_h, void *user_data);
224+
int nns_edge_discover (nns_edge_h edge_h);
226225

227226
/**
228227
* @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().

src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle,
220220
return ret;
221221
}
222222

223+
/**
224+
* @brief Internal function to discover devices of custom connection.
225+
*/
226+
int
227+
nns_edge_custom_discover (nns_edge_custom_connection_h handle)
228+
{
229+
custom_connection_s *custom = (custom_connection_s *) handle;
230+
nns_edge_custom_s *custom_h;
231+
int ret;
232+
233+
if (!custom || !custom->instance)
234+
return NNS_EDGE_ERROR_INVALID_PARAMETER;
235+
236+
custom_h = custom->instance;
237+
238+
ret = custom_h->nns_edge_custom_discover (custom->priv);
239+
if (NNS_EDGE_ERROR_NONE != ret) {
240+
nns_edge_loge ("Failed to discover devices of custom connection.");
241+
}
242+
243+
return ret;
244+
}
245+
223246
/**
224247
* @brief Internal function to connect custom connection.
225248
*/

src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ int nns_edge_custom_start (nns_edge_custom_connection_h handle);
4242
*/
4343
int nns_edge_custom_stop (nns_edge_custom_connection_h handle);
4444

45+
/**
46+
* @brief Internal function to discover devices of custom connection.
47+
*/
48+
int nns_edge_custom_discover (nns_edge_custom_connection_h handle);
49+
4550
/**
4651
* @brief Internal function to set the event callback of custom connection.
4752
*/
@@ -76,6 +81,7 @@ int nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *k
7681
#define nns_edge_custom_release(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
7782
#define nns_edge_custom_start(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
7883
#define nns_edge_custom_stop(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
84+
#define nns_edge_custom_discover(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
7985
#define nns_edge_custom_set_event_callback(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
8086
#define nns_edge_custom_connect(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
8187
#define nns_edge_custom_is_connected(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)

src/libnnstreamer-edge/nnstreamer-edge-internal.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,3 +2143,39 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value)
21432143

21442144
return ret;
21452145
}
2146+
2147+
/**
2148+
* @brief Discover connectable devices within the network.
2149+
*/
2150+
int nns_edge_discover (nns_edge_h edge_h)
2151+
{
2152+
nns_edge_handle_s *eh;
2153+
int ret = NNS_EDGE_ERROR_NONE;
2154+
2155+
eh = (nns_edge_handle_s *) edge_h;
2156+
if (!eh) {
2157+
nns_edge_loge ("Invalid param, given edge handle is null.");
2158+
return NNS_EDGE_ERROR_INVALID_PARAMETER;
2159+
}
2160+
2161+
if (!nns_edge_handle_is_valid (eh)) {
2162+
nns_edge_loge ("Invalid param, given edge handle is invalid.");
2163+
return NNS_EDGE_ERROR_INVALID_PARAMETER;
2164+
}
2165+
2166+
nns_edge_lock (eh);
2167+
if (!eh->event_cb) {
2168+
nns_edge_loge ("NNStreamer-edge event callback is not registered.");
2169+
nns_edge_unlock (eh);
2170+
return NNS_EDGE_ERROR_CONNECTION_FAILURE;
2171+
}
2172+
2173+
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
2174+
ret = nns_edge_custom_discover (eh->custom_connection_h);
2175+
}
2176+
2177+
nns_edge_unlock (eh);
2178+
2179+
return ret;
2180+
}
2181+

tests/nnstreamer-edge-custom-test.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ nns_edge_custom_subscribe (void *priv)
108108
return NNS_EDGE_ERROR_NOT_SUPPORTED;
109109
}
110110

111+
112+
static int
113+
nns_edge_custom_discover (void *priv)
114+
{
115+
int ret = NNS_EDGE_ERROR_NONE;
116+
117+
if (!priv) {
118+
nns_edge_loge ("Invalid param, handle should not be null.");
119+
return NNS_EDGE_ERROR_INVALID_PARAMETER;
120+
}
121+
nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv;
122+
ret = nns_edge_event_invoke_callback (custom_h->event_cb, custom_h->user_data,
123+
NNS_EDGE_EVENT_DEVICE_FOUND, NULL, 0, NULL);
124+
125+
return ret;
126+
}
127+
111128
static int
112129
nns_edge_custom_is_connected (void *priv)
113130
{
@@ -130,6 +147,10 @@ nns_edge_custom_set_event_cb (void *priv, nns_edge_event_cb cb, void *user_data)
130147
nns_edge_loge ("Invalid param, handle should not be null.");
131148
return NNS_EDGE_ERROR_INVALID_PARAMETER;
132149
}
150+
nns_edge_custom_test_s *custom_h = (nns_edge_custom_test_s *) priv;
151+
152+
custom_h->event_cb = cb;
153+
custom_h->user_data = user_data;
133154

134155
return NNS_EDGE_ERROR_NONE;
135156
}
@@ -187,6 +208,7 @@ nns_edge_custom_s edge_custom_h = {
187208
.nns_edge_custom_create = nns_edge_custom_create,
188209
.nns_edge_custom_close = nns_edge_custom_close,
189210
.nns_edge_custom_start = nns_edge_custom_start,
211+
.nns_edge_custom_discover = nns_edge_custom_discover,
190212
.nns_edge_custom_stop = nns_edge_custom_stop,
191213
.nns_edge_custom_connect = nns_edge_custom_connect,
192214
.nns_edge_custom_subscribe = nns_edge_custom_subscribe,

tests/unittest_nnstreamer-edge-custom.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ TEST (edgeCustom, createHandleInvalidParam03_n)
7474
static int
7575
_test_edge_event_cb (nns_edge_event_h event_h, void *user_data)
7676
{
77+
nns_edge_event_e event = NNS_EDGE_EVENT_UNKNOWN;
78+
int ret;
79+
int *device_found = (int *) user_data;
80+
81+
ret = nns_edge_event_get_type (event_h, &event);
82+
EXPECT_EQ (ret, NNS_EDGE_ERROR_NONE);
83+
84+
switch (event) {
85+
case NNS_EDGE_EVENT_DEVICE_FOUND:
86+
(*device_found)++;
87+
break;
88+
default:
89+
break;
90+
}
91+
7792
return NNS_EDGE_ERROR_NONE;
7893
}
7994

@@ -86,12 +101,13 @@ TEST (edgeCustom, expectedReturn)
86101
nns_edge_h edge_h = NULL;
87102
nns_edge_data_h data_h = NULL;
88103
char *ret_str = NULL;
104+
int device_found = 0;
89105

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

94-
ret = nns_edge_set_event_callback (edge_h, _test_edge_event_cb, NULL);
110+
ret = nns_edge_set_event_callback (edge_h, _test_edge_event_cb, &device_found);
95111
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
96112
ret = nns_edge_set_info (edge_h, "PEER_ADDRESS", "TE:MP:AD:DR:ES:SS");
97113
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
@@ -103,6 +119,10 @@ TEST (edgeCustom, expectedReturn)
103119
ret = nns_edge_start (edge_h);
104120
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
105121

122+
ret = nns_edge_discover (edge_h);
123+
EXPECT_EQ (NNS_EDGE_ERROR_NONE, ret);
124+
EXPECT_EQ (1, device_found);
125+
106126
ret = nns_edge_is_connected (edge_h);
107127
EXPECT_EQ (NNS_EDGE_ERROR_CONNECTION_FAILURE, ret);
108128

@@ -195,6 +215,17 @@ TEST (edgeCustom, stopInvalidParam01_n)
195215
EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
196216
}
197217

218+
/**
219+
* @brief Set event callback of edge custom - invalid param.
220+
*/
221+
TEST (edgeCustom, discoverInvalidParam01_n)
222+
{
223+
int ret;
224+
225+
ret = nns_edge_custom_discover (NULL);
226+
EXPECT_NE (NNS_EDGE_ERROR_NONE, ret);
227+
}
228+
198229
/**
199230
* @brief Set event callback of edge custom - invalid param.
200231
*/

0 commit comments

Comments
 (0)