Skip to content

Commit

Permalink
[Custom] handle for custom connection
Browse files Browse the repository at this point in the history
Define handle for custom connection to hide internal structure.

Signed-off-by: Jaeyun Jung <[email protected]>
  • Loading branch information
jaeyun-jung committed Jan 6, 2025
1 parent e0d3b31 commit 44741a5
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 45 deletions.
55 changes: 43 additions & 12 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include "nnstreamer-edge-log.h"
#include "nnstreamer-edge-util.h"

/**
* @brief Internal data structure for edge custom connection.
*/
typedef struct
{
void *dl_handle;
nns_edge_custom_s *instance;
void *priv;
} custom_connection_s;

typedef const nns_edge_custom_s *custom_get_instance (void);

/**
Expand Down Expand Up @@ -65,17 +75,25 @@ _load_custom_library (custom_connection_s * custom, const char *lib_path)
* @brief Internal function to load custom connection from library.
*/
int
nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
nns_edge_custom_load (const char *lib_path,
nns_edge_custom_connection_h * handle)
{
custom_connection_s *custom;
nns_edge_custom_s *custom_h;
int ret;

if (!STR_IS_VALID (lib_path))
return NNS_EDGE_ERROR_INVALID_PARAMETER;

if (!custom)
if (!handle)
return NNS_EDGE_ERROR_INVALID_PARAMETER;

custom = (custom_connection_s *) calloc (1, sizeof (custom_connection_s));
if (!custom) {
nns_edge_loge ("Failed to allocate memory for edge custom connection.");
return NNS_EDGE_ERROR_OUT_OF_MEMORY;
}

ret = _load_custom_library (custom, lib_path);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge
Expand All @@ -91,7 +109,9 @@ nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
}

error:
if (NNS_EDGE_ERROR_NONE != ret) {
if (NNS_EDGE_ERROR_NONE == ret) {
*handle = custom;
} else {
nns_edge_custom_release (custom);
}

Expand All @@ -102,8 +122,9 @@ nns_edge_custom_load (custom_connection_s * custom, const char *lib_path)
* @brief Internal function to release custom connection.
*/
int
nns_edge_custom_release (custom_connection_s * custom)
nns_edge_custom_release (nns_edge_custom_connection_h handle)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

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

free (custom);
return ret;
}

/**
* @brief Internal function to start custom connection.
*/
int
nns_edge_custom_start (custom_connection_s * custom)
nns_edge_custom_start (nns_edge_custom_connection_h handle)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

Expand All @@ -154,8 +177,9 @@ nns_edge_custom_start (custom_connection_s * custom)
* @brief Internal function to stop custom connection.
*/
int
nns_edge_custom_stop (custom_connection_s * custom)
nns_edge_custom_stop (nns_edge_custom_connection_h handle)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

Expand All @@ -176,9 +200,10 @@ nns_edge_custom_stop (custom_connection_s * custom)
* @brief Internal function to set the event callback of custom connection.
*/
int
nns_edge_custom_set_event_callback (custom_connection_s * custom,
nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle,
nns_edge_event_cb cb, void *user_data)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

Expand All @@ -199,8 +224,9 @@ nns_edge_custom_set_event_callback (custom_connection_s * custom,
* @brief Internal function to connect custom connection.
*/
int
nns_edge_custom_connect (custom_connection_s * custom)
nns_edge_custom_connect (nns_edge_custom_connection_h handle)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

Expand All @@ -221,8 +247,9 @@ nns_edge_custom_connect (custom_connection_s * custom)
* @brief Internal function to check custom connection.
*/
int
nns_edge_custom_is_connected (custom_connection_s * custom)
nns_edge_custom_is_connected (nns_edge_custom_connection_h handle)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;

if (!custom || !custom->instance)
Expand All @@ -237,8 +264,10 @@ nns_edge_custom_is_connected (custom_connection_s * custom)
* @brief Internal function to send data to custom connection.
*/
int
nns_edge_custom_send_data (custom_connection_s * custom, nns_edge_data_h data_h)
nns_edge_custom_send_data (nns_edge_custom_connection_h handle,
nns_edge_data_h data_h)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret;

Expand All @@ -263,9 +292,10 @@ nns_edge_custom_send_data (custom_connection_s * custom, nns_edge_data_h data_h)
* @brief Internal function to set information to custom connection.
*/
int
nns_edge_custom_set_info (custom_connection_s * custom, const char *key,
nns_edge_custom_set_info (nns_edge_custom_connection_h handle, const char *key,
const char *value)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;

Expand All @@ -291,9 +321,10 @@ nns_edge_custom_set_info (custom_connection_s * custom, const char *key,
* @brief Internal function to get information from custom connection.
*/
int
nns_edge_custom_get_info (custom_connection_s * custom, const char *key,
nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key,
char **value)
{
custom_connection_s *custom = (custom_connection_s *) handle;
nns_edge_custom_s *custom_h;
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;

Expand Down
30 changes: 11 additions & 19 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,58 @@
extern "C" {
#endif /* __cplusplus */

/**
* @brief Data structure for edge custom connection.
*/
typedef struct
{
void *dl_handle;
nns_edge_custom_s *instance;
void *priv;
} custom_connection_s;
typedef void *nns_edge_custom_connection_h;

#if defined(ENABLE_CUSTOM_CONNECTION)
/**
* @brief Internal function to load custom connection from library.
*/
int nns_edge_custom_load (custom_connection_s *custom, const char *lib_path);
int nns_edge_custom_load (const char *lib_path, nns_edge_custom_connection_h *handle);

/**
* @brief Internal function to release custom connection.
*/
int nns_edge_custom_release (custom_connection_s *custom);
int nns_edge_custom_release (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to start custom connection.
*/
int nns_edge_custom_start (custom_connection_s *custom);
int nns_edge_custom_start (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to stop custom connection.
*/
int nns_edge_custom_stop (custom_connection_s *custom);
int nns_edge_custom_stop (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to set the event callback of custom connection.
*/
int nns_edge_custom_set_event_callback (custom_connection_s *custom, nns_edge_event_cb cb, void *user_data);
int nns_edge_custom_set_event_callback (nns_edge_custom_connection_h handle, nns_edge_event_cb cb, void *user_data);

/**
* @brief Internal function to connect custom connection.
*/
int nns_edge_custom_connect (custom_connection_s *custom);
int nns_edge_custom_connect (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to check custom connection.
*/
int nns_edge_custom_is_connected (custom_connection_s *custom);
int nns_edge_custom_is_connected (nns_edge_custom_connection_h handle);

/**
* @brief Internal function to send data to custom connection.
*/
int nns_edge_custom_send_data (custom_connection_s *custom, nns_edge_data_h data_h);
int nns_edge_custom_send_data (nns_edge_custom_connection_h handle, nns_edge_data_h data_h);

/**
* @brief Internal function to set information to custom connection.
*/
int nns_edge_custom_set_info (custom_connection_s *custom, const char *key, const char *value);
int nns_edge_custom_set_info (nns_edge_custom_connection_h handle, const char *key, const char *value);

/**
* @brief Internal function to get information from custom connection.
*/
int nns_edge_custom_get_info (custom_connection_s *custom, const char *key, char **value);
int nns_edge_custom_get_info (nns_edge_custom_connection_h handle, const char *key, char **value);
#else
#define nns_edge_custom_load(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
#define nns_edge_custom_release(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
Expand Down
28 changes: 14 additions & 14 deletions src/libnnstreamer-edge/nnstreamer-edge-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ typedef struct
void *broker_h;

/* Data for custom connection */
custom_connection_s custom;
nns_edge_custom_connection_h custom_connection_h;
} nns_edge_handle_s;

/**
Expand Down Expand Up @@ -919,7 +919,7 @@ _nns_edge_send_thread (void *thread_data)
nns_edge_loge ("Failed to send data via MQTT connection.");
break;
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
ret = nns_edge_custom_send_data (&eh->custom, data_h);
ret = nns_edge_custom_send_data (eh->custom_connection_h, data_h);
if (NNS_EDGE_ERROR_NONE != ret)
nns_edge_loge ("Failed to send data via custom connection.");
break;
Expand Down Expand Up @@ -1296,9 +1296,7 @@ _nns_edge_create_handle (const char *id, nns_edge_node_type_e node_type,
eh->sending = false;
eh->listener_fd = -1;
eh->caps_str = nns_edge_strdup ("");
eh->custom.dl_handle = NULL;
eh->custom.instance = NULL;
eh->custom.priv = NULL;
eh->custom_connection_h = NULL;

ret = nns_edge_metadata_create (&eh->metadata);
if (ret != NNS_EDGE_ERROR_NONE) {
Expand Down Expand Up @@ -1355,7 +1353,7 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path,
eh = (nns_edge_handle_s *) (*edge_h);
eh->connect_type = NNS_EDGE_CONNECT_TYPE_CUSTOM;

ret = nns_edge_custom_load (&eh->custom, lib_path);
ret = nns_edge_custom_load (lib_path, &eh->custom_connection_h);
if (ret != NNS_EDGE_ERROR_NONE)
nns_edge_release_handle (eh);

Expand Down Expand Up @@ -1427,7 +1425,7 @@ nns_edge_start (nns_edge_h edge_h)
nns_edge_lock (eh);

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
ret = nns_edge_custom_start (&eh->custom);
ret = nns_edge_custom_start (eh->custom_connection_h);
if (NNS_EDGE_ERROR_NONE == ret)
ret = _nns_edge_create_send_thread (eh);

Expand Down Expand Up @@ -1536,7 +1534,7 @@ nns_edge_stop (nns_edge_h edge_h)
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
ret = nns_edge_custom_stop (&eh->custom);
ret = nns_edge_custom_stop (eh->custom_connection_h);
}

if (NNS_EDGE_ERROR_NONE == ret)
Expand Down Expand Up @@ -1600,7 +1598,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
}
break;
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
if (nns_edge_custom_release (&eh->custom) !=
if (nns_edge_custom_release (eh->custom_connection_h) !=
NNS_EDGE_ERROR_NONE) {
nns_edge_logw ("Failed to close custom connection.");
}
Expand All @@ -1614,6 +1612,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
eh->event_cb = NULL;
eh->user_data = NULL;
eh->broker_h = NULL;
eh->custom_connection_h = NULL;

nns_edge_queue_destroy (eh->send_queue);
eh->send_queue = NULL;
Expand Down Expand Up @@ -1664,7 +1663,8 @@ nns_edge_set_event_callback (nns_edge_h edge_h, nns_edge_event_cb cb,
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
ret = nns_edge_custom_set_event_callback (&eh->custom, cb, user_data);
ret = nns_edge_custom_set_event_callback (eh->custom_connection_h,
cb, user_data);
if (NNS_EDGE_ERROR_NONE != ret) {
goto error;
}
Expand Down Expand Up @@ -1813,7 +1813,7 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port)
}
}
} else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
ret = nns_edge_custom_connect (&eh->custom);
ret = nns_edge_custom_connect (eh->custom_connection_h);
if (ret != NNS_EDGE_ERROR_NONE) {
goto done;
}
Expand Down Expand Up @@ -1880,7 +1880,7 @@ nns_edge_is_connected (nns_edge_h edge_h)
return NNS_EDGE_ERROR_NONE;

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
return nns_edge_custom_is_connected (&eh->custom);
return nns_edge_custom_is_connected (eh->custom_connection_h);
}

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

if (nns_edge_custom_get_info (&eh->custom, key, &val) ==
if (nns_edge_custom_get_info (eh->custom_connection_h, key, &val) ==
NNS_EDGE_ERROR_NONE) {
/* Replace value from custom library. */
SAFE_FREE (*value);
Expand Down
Loading

0 comments on commit 44741a5

Please sign in to comment.