Skip to content

Commit e30d740

Browse files
committed
[Custom] handle for custom connection
Define handle for custom connection to hide internal structure. Signed-off-by: Jaeyun Jung <[email protected]>
1 parent e0d3b31 commit e30d740

File tree

4 files changed

+308
-45
lines changed

4 files changed

+308
-45
lines changed

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
#include "nnstreamer-edge-log.h"
1717
#include "nnstreamer-edge-util.h"
1818

19+
/**
20+
* @brief Internal data structure for edge custom connection.
21+
*/
22+
typedef struct
23+
{
24+
void *dl_handle;
25+
nns_edge_custom_s *instance;
26+
void *priv;
27+
} custom_connection_s;
28+
1929
typedef const nns_edge_custom_s *custom_get_instance (void);
2030

2131
/**
@@ -65,17 +75,25 @@ _load_custom_library (custom_connection_s * custom, const char *lib_path)
6575
* @brief Internal function to load custom connection from library.
6676
*/
6777
int
68-
nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
78+
nns_edge_custom_load (const char *lib_path,
79+
nns_edge_custom_connection_h * handle)
6980
{
81+
custom_connection_s *custom;
7082
nns_edge_custom_s *custom_h;
7183
int ret;
7284

7385
if (!STR_IS_VALID (lib_path))
7486
return NNS_EDGE_ERROR_INVALID_PARAMETER;
7587

76-
if (!custom)
88+
if (!handle)
7789
return NNS_EDGE_ERROR_INVALID_PARAMETER;
7890

91+
custom = (custom_connection_s *) calloc (1, sizeof (custom_connection_s));
92+
if (!custom) {
93+
nns_edge_loge ("Failed to allocate memory for edge custom connection.");
94+
return NNS_EDGE_ERROR_OUT_OF_MEMORY;
95+
}
96+
7997
ret = _load_custom_library (custom, lib_path);
8098
if (NNS_EDGE_ERROR_NONE != ret) {
8199
nns_edge_loge
@@ -91,7 +109,9 @@ nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
91109
}
92110

93111
error:
94-
if (NNS_EDGE_ERROR_NONE != ret) {
112+
if (NNS_EDGE_ERROR_NONE == ret) {
113+
*handle = custom;
114+
} else {
95115
nns_edge_custom_release (custom);
96116
}
97117

@@ -102,8 +122,9 @@ nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
102122
* @brief Internal function to release custom connection.
103123
*/
104124
int
105-
nns_edge_custom_release (custom_connection_s * custom)
125+
nns_edge_custom_release (nns_edge_custom_connection_h handle)
106126
{
127+
custom_connection_s *custom = (custom_connection_s *) handle;
107128
nns_edge_custom_s *custom_h;
108129
int ret;
109130

@@ -125,15 +146,17 @@ nns_edge_custom_release (custom_connection_s * custom)
125146
custom->instance = NULL;
126147
custom->priv = NULL;
127148

149+
free (custom);
128150
return ret;
129151
}
130152

131153
/**
132154
* @brief Internal function to start custom connection.
133155
*/
134156
int
135-
nns_edge_custom_start (custom_connection_s * custom)
157+
nns_edge_custom_start (nns_edge_custom_connection_h handle)
136158
{
159+
custom_connection_s *custom = (custom_connection_s *) handle;
137160
nns_edge_custom_s *custom_h;
138161
int ret;
139162

@@ -154,8 +177,9 @@ nns_edge_custom_start (custom_connection_s * custom)
154177
* @brief Internal function to stop custom connection.
155178
*/
156179
int
157-
nns_edge_custom_stop (custom_connection_s * custom)
180+
nns_edge_custom_stop (nns_edge_custom_connection_h handle)
158181
{
182+
custom_connection_s *custom = (custom_connection_s *) handle;
159183
nns_edge_custom_s *custom_h;
160184
int ret;
161185

@@ -176,9 +200,10 @@ nns_edge_custom_stop (custom_connection_s * custom)
176200
* @brief Internal function to set the event callback of custom connection.
177201
*/
178202
int
179-
nns_edge_custom_set_event_callback (custom_connection_s * custom,
203+
nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle,
180204
nns_edge_event_cb cb, void *user_data)
181205
{
206+
custom_connection_s *custom = (custom_connection_s *) handle;
182207
nns_edge_custom_s *custom_h;
183208
int ret;
184209

@@ -199,8 +224,9 @@ nns_edge_custom_set_event_callback (custom_connection_s * custom,
199224
* @brief Internal function to connect custom connection.
200225
*/
201226
int
202-
nns_edge_custom_connect (custom_connection_s * custom)
227+
nns_edge_custom_connect (nns_edge_custom_connection_h handle)
203228
{
229+
custom_connection_s *custom = (custom_connection_s *) handle;
204230
nns_edge_custom_s *custom_h;
205231
int ret;
206232

@@ -221,8 +247,9 @@ nns_edge_custom_connect (custom_connection_s * custom)
221247
* @brief Internal function to check custom connection.
222248
*/
223249
int
224-
nns_edge_custom_is_connected (custom_connection_s * custom)
250+
nns_edge_custom_is_connected (nns_edge_custom_connection_h handle)
225251
{
252+
custom_connection_s *custom = (custom_connection_s *) handle;
226253
nns_edge_custom_s *custom_h;
227254

228255
if (!custom || !custom->instance)
@@ -237,8 +264,10 @@ nns_edge_custom_is_connected (custom_connection_s * custom)
237264
* @brief Internal function to send data to custom connection.
238265
*/
239266
int
240-
nns_edge_custom_send_data (custom_connection_s * custom, nns_edge_data_h data_h)
267+
nns_edge_custom_send_data (nns_edge_custom_connection_h handle,
268+
nns_edge_data_h data_h)
241269
{
270+
custom_connection_s *custom = (custom_connection_s *) handle;
242271
nns_edge_custom_s *custom_h;
243272
int ret;
244273

@@ -263,9 +292,10 @@ nns_edge_custom_send_data (custom_connection_s * custom, nns_edge_data_h data_h)
263292
* @brief Internal function to set information to custom connection.
264293
*/
265294
int
266-
nns_edge_custom_set_info (custom_connection_s * custom, const char *key,
295+
nns_edge_custom_set_info (nns_edge_custom_connection_h handle, const char *key,
267296
const char *value)
268297
{
298+
custom_connection_s *custom = (custom_connection_s *) handle;
269299
nns_edge_custom_s *custom_h;
270300
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;
271301

@@ -291,9 +321,10 @@ nns_edge_custom_set_info (custom_connection_s * custom, const char *key,
291321
* @brief Internal function to get information from custom connection.
292322
*/
293323
int
294-
nns_edge_custom_get_info (custom_connection_s * custom, const char *key,
324+
nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key,
295325
char **value)
296326
{
327+
custom_connection_s *custom = (custom_connection_s *) handle;
297328
nns_edge_custom_s *custom_h;
298329
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;
299330

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,58 @@
1919
extern "C" {
2020
#endif /* __cplusplus */
2121

22-
/**
23-
* @brief Data structure for edge custom connection.
24-
*/
25-
typedef struct
26-
{
27-
void *dl_handle;
28-
nns_edge_custom_s *instance;
29-
void *priv;
30-
} custom_connection_s;
22+
typedef void *nns_edge_custom_connection_h;
3123

3224
#if defined(ENABLE_CUSTOM_CONNECTION)
3325
/**
3426
* @brief Internal function to load custom connection from library.
3527
*/
36-
int nns_edge_custom_load (custom_connection_s *custom, const char *lib_path);
28+
int nns_edge_custom_load (const char *lib_path, nns_edge_custom_connection_h *handle);
3729

3830
/**
3931
* @brief Internal function to release custom connection.
4032
*/
41-
int nns_edge_custom_release (custom_connection_s *custom);
33+
int nns_edge_custom_release (nns_edge_custom_connection_h handle);
4234

4335
/**
4436
* @brief Internal function to start custom connection.
4537
*/
46-
int nns_edge_custom_start (custom_connection_s *custom);
38+
int nns_edge_custom_start (nns_edge_custom_connection_h handle);
4739

4840
/**
4941
* @brief Internal function to stop custom connection.
5042
*/
51-
int nns_edge_custom_stop (custom_connection_s *custom);
43+
int nns_edge_custom_stop (nns_edge_custom_connection_h handle);
5244

5345
/**
5446
* @brief Internal function to set the event callback of custom connection.
5547
*/
56-
int nns_edge_custom_set_event_callback (custom_connection_s *custom, nns_edge_event_cb cb, void *user_data);
48+
int nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle, nns_edge_event_cb cb, void *user_data);
5749

5850
/**
5951
* @brief Internal function to connect custom connection.
6052
*/
61-
int nns_edge_custom_connect (custom_connection_s *custom);
53+
int nns_edge_custom_connect (nns_edge_custom_connection_h handle);
6254

6355
/**
6456
* @brief Internal function to check custom connection.
6557
*/
66-
int nns_edge_custom_is_connected (custom_connection_s *custom);
58+
int nns_edge_custom_is_connected (nns_edge_custom_connection_h handle);
6759

6860
/**
6961
* @brief Internal function to send data to custom connection.
7062
*/
71-
int nns_edge_custom_send_data (custom_connection_s *custom, nns_edge_data_h data_h);
63+
int nns_edge_custom_send_data (nns_edge_custom_connection_h handle, nns_edge_data_h data_h);
7264

7365
/**
7466
* @brief Internal function to set information to custom connection.
7567
*/
76-
int nns_edge_custom_set_info (custom_connection_s *custom, const char *key, const char *value);
68+
int nns_edge_custom_set_info (nns_edge_custom_connection_h handle, const char *key, const char *value);
7769

7870
/**
7971
* @brief Internal function to get information from custom connection.
8072
*/
81-
int nns_edge_custom_get_info (custom_connection_s *custom, const char *key, char **value);
73+
int nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key, char **value);
8274
#else
8375
#define nns_edge_custom_load(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
8476
#define nns_edge_custom_release(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef struct
7575
void *broker_h;
7676

7777
/* Data for custom connection */
78-
custom_connection_s custom;
78+
nns_edge_custom_connection_h custom_connection_h;
7979
} nns_edge_handle_s;
8080

8181
/**
@@ -919,7 +919,7 @@ _nns_edge_send_thread (void *thread_data)
919919
nns_edge_loge ("Failed to send data via MQTT connection.");
920920
break;
921921
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
922-
ret = nns_edge_custom_send_data (&eh->custom, data_h);
922+
ret = nns_edge_custom_send_data (eh->custom_connection_h, data_h);
923923
if (NNS_EDGE_ERROR_NONE != ret)
924924
nns_edge_loge ("Failed to send data via custom connection.");
925925
break;
@@ -1296,9 +1296,7 @@ _nns_edge_create_handle (const char *id, nns_edge_node_type_e node_type,
12961296
eh->sending = false;
12971297
eh->listener_fd = -1;
12981298
eh->caps_str = nns_edge_strdup ("");
1299-
eh->custom.dl_handle = NULL;
1300-
eh->custom.instance = NULL;
1301-
eh->custom.priv = NULL;
1299+
eh->custom_connection_h = NULL;
13021300

13031301
ret = nns_edge_metadata_create (&eh->metadata);
13041302
if (ret != NNS_EDGE_ERROR_NONE) {
@@ -1355,7 +1353,7 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path,
13551353
eh = (nns_edge_handle_s *) (*edge_h);
13561354
eh->connect_type = NNS_EDGE_CONNECT_TYPE_CUSTOM;
13571355

1358-
ret = nns_edge_custom_load (&eh->custom, lib_path);
1356+
ret = nns_edge_custom_load (lib_path, &eh->custom_connection_h);
13591357
if (ret != NNS_EDGE_ERROR_NONE)
13601358
nns_edge_release_handle (eh);
13611359

@@ -1427,7 +1425,7 @@ nns_edge_start (nns_edge_h edge_h)
14271425
nns_edge_lock (eh);
14281426

14291427
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
1430-
ret = nns_edge_custom_start (&eh->custom);
1428+
ret = nns_edge_custom_start (eh->custom_connection_h);
14311429
if (NNS_EDGE_ERROR_NONE == ret)
14321430
ret = _nns_edge_create_send_thread (eh);
14331431

@@ -1536,7 +1534,7 @@ nns_edge_stop (nns_edge_h edge_h)
15361534
}
15371535

15381536
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
1539-
ret = nns_edge_custom_stop (&eh->custom);
1537+
ret = nns_edge_custom_stop (eh->custom_connection_h);
15401538
}
15411539

15421540
if (NNS_EDGE_ERROR_NONE == ret)
@@ -1600,7 +1598,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
16001598
}
16011599
break;
16021600
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
1603-
if (nns_edge_custom_release (&eh->custom) !=
1601+
if (nns_edge_custom_release (eh->custom_connection_h) !=
16041602
NNS_EDGE_ERROR_NONE) {
16051603
nns_edge_logw ("Failed to close custom connection.");
16061604
}
@@ -1614,6 +1612,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
16141612
eh->event_cb = NULL;
16151613
eh->user_data = NULL;
16161614
eh->broker_h = NULL;
1615+
eh->custom_connection_h = NULL;
16171616

16181617
nns_edge_queue_destroy (eh->send_queue);
16191618
eh->send_queue = NULL;
@@ -1664,7 +1663,8 @@ nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb,
16641663
}
16651664

16661665
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
1667-
ret = nns_edge_custom_set_event_callback (&eh->custom, cb, user_data);
1666+
ret = nns_edge_custom_set_event_callback (eh->custom_connection_h,
1667+
cb, user_data);
16681668
if (NNS_EDGE_ERROR_NONE != ret) {
16691669
goto error;
16701670
}
@@ -1813,7 +1813,7 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port)
18131813
}
18141814
}
18151815
} else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
1816-
ret = nns_edge_custom_connect (&eh->custom);
1816+
ret = nns_edge_custom_connect (eh->custom_connection_h);
18171817
if (ret != NNS_EDGE_ERROR_NONE) {
18181818
goto done;
18191819
}
@@ -1880,7 +1880,7 @@ nns_edge_is_connected (nns_edge_h edge_h)
18801880
return NNS_EDGE_ERROR_NONE;
18811881

18821882
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
1883-
return nns_edge_custom_is_connected (&eh->custom);
1883+
return nns_edge_custom_is_connected (eh->custom_connection_h);
18841884
}
18851885

18861886
conn_data = (nns_edge_conn_data_s *) eh->connections;
@@ -2052,7 +2052,7 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value)
20522052
if (ret == NNS_EDGE_ERROR_NONE &&
20532053
NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
20542054
/* Pass value to custom library and ignore error. */
2055-
if (nns_edge_custom_set_info (&eh->custom, key, value) !=
2055+
if (nns_edge_custom_set_info (eh->custom_connection_h, key, value) !=
20562056
NNS_EDGE_ERROR_NONE) {
20572057
nns_edge_logw ("Failed to set info '%s' in custom connection.", key);
20582058
}
@@ -2128,7 +2128,7 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value)
21282128
NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
21292129
char *val = NULL;
21302130

2131-
if (nns_edge_custom_get_info (&eh->custom, key, &val) ==
2131+
if (nns_edge_custom_get_info (eh->custom_connection_h, key, &val) ==
21322132
NNS_EDGE_ERROR_NONE) {
21332133
/* Replace value from custom library. */
21342134
SAFE_FREE (*value);

0 commit comments

Comments
 (0)