Skip to content

Commit

Permalink
[Custom] separate custom impl
Browse files Browse the repository at this point in the history
Separate custom connection implementation and update callbacks.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
  • Loading branch information
jaeyun-jung committed Aug 22, 2024
1 parent 6c870e5 commit 4a08684
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 207 deletions.
8 changes: 4 additions & 4 deletions include/nnstreamer-edge-custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {
* The user should implement the functions and provide them using nns_edge_custom_get_instance().
* Refer to the example in nnstreamer-edge-custom-test.c for more details.
*/
typedef struct _NnsEdgeCustomDef
typedef struct
{
const char *(*nns_edge_custom_get_description) ();
int (*nns_edge_custom_create) (void **priv);
Expand All @@ -36,14 +36,14 @@ typedef struct _NnsEdgeCustomDef
int (*nns_edge_custom_is_connected) (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_option) (void *priv, const char *key, const char *value);
char *(*nns_edge_custom_get_option) (void *priv, const char *key);
int (*nns_edge_custom_set_info) (void *priv, const char *key, const char *value);
int (*nns_edge_custom_get_info) (void *priv, const char *key, char **value);
} nns_edge_custom_s;

/**
* @brief Get nns edge custom connection instance.
*/
void* nns_edge_custom_get_instance ();
const nns_edge_custom_s * nns_edge_custom_get_instance (void);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions jni/nnstreamer-edge.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NNSTREAMER_EDGE_INCLUDES := \

# nnstreamer-edge sources
NNSTREAMER_EDGE_SRCS := \
$(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c \
$(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-data.c \
$(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-event.c \
$(NNSTREAMER_EDGE_ROOT)/src/libnnstreamer-edge/nnstreamer-edge-internal.c \
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NNStreamer-Edge library
SET(NNS_EDGE_SRCS
${NNS_EDGE_SRC_DIR}/nnstreamer-edge-custom-impl.c
${NNS_EDGE_SRC_DIR}/nnstreamer-edge-metadata.c
${NNS_EDGE_SRC_DIR}/nnstreamer-edge-data.c
${NNS_EDGE_SRC_DIR}/nnstreamer-edge-event.c
Expand Down
299 changes: 299 additions & 0 deletions src/libnnstreamer-edge/nnstreamer-edge-custom-impl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/* SPDX-License-Identifier: Apache-2.0 */
/**
* Copyright (C) 2024 Samsung Electronics Co., Ltd. All Rights Reserved.
*
* @file nnstreamer-edge-custom-impl.c
* @date 14 Aug 2024
* @brief Internal interface to support communication using custom library.
* @see https://github.com/nnstreamer/nnstreamer
* @author Gichan Jang <gichan2.jang@samsung.com>
* @bug No known bugs except for NYI items
*/

#include <dlfcn.h>

#include "nnstreamer-edge-custom-impl.h"
#include "nnstreamer-edge-log.h"

typedef const nns_edge_custom_s *custom_get_instance (void);

/**
* @brief Internal function to load custom library.
*/
static int
_load_custom_library (custom_connection_s * custom, const char *lib_path)
{
void *handle;
nns_edge_custom_s *custom_h;
int ret = NNS_EDGE_ERROR_UNKNOWN;

handle = dlopen (lib_path, RTLD_LAZY);
if (NULL == handle) {
nns_edge_loge ("Failed to open custom library: %s", dlerror ());
goto error;
}

custom_get_instance *get_instance =
(custom_get_instance *) dlsym (handle, "nns_edge_custom_get_instance");
if (!get_instance) {
nns_edge_loge ("Failed to find nns_edge_custom_get_instance: %s",
dlerror ());
goto error;
}

custom_h = (nns_edge_custom_s *) get_instance ();
if (!custom_h) {
nns_edge_loge ("Failed to get custom instance from library.");
goto error;
}

custom->dl_handle = handle;
custom->instance = custom_h;
ret = NNS_EDGE_ERROR_NONE;

error:
if (NNS_EDGE_ERROR_NONE != ret) {
if (handle)
dlclose (handle);
}

return ret;
}

/**
* @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_s *custom_h;
int ret;

ret = _load_custom_library (custom, lib_path);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge
("Failed to load custom library. Please check the library path or permission.");
goto error;
}

custom_h = custom->instance;

ret = custom_h->nns_edge_custom_create (&custom->priv);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to create custom connection handle.");
}

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

return ret;
}

/**
* @brief Internal function to release custom connection.
*/
int
nns_edge_custom_release (custom_connection_s * custom)
{
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_close (custom->priv);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to stop custom connection.");
}

if (custom->dl_handle) {
dlclose (custom->dl_handle);
}

custom->dl_handle = NULL;
custom->instance = NULL;
custom->priv = NULL;

return ret;
}

/**
* @brief Internal function to start custom connection.
*/
int
nns_edge_custom_start (custom_connection_s * custom)
{
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_start (custom->priv);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to start custom connection.");
}

return ret;
}

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

return ret;
}

/**
* @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)
{
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_set_event_cb (custom->priv, cb, user_data);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to set event callback to custom connection.");
}

return ret;
}

/**
* @brief Internal function to connect custom connection.
*/
int
nns_edge_custom_connect (custom_connection_s * custom)
{
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_connect (custom->priv);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to connect custom connection.");
}

return ret;
}

/**
* @brief Internal function to check custom connection.
*/
int
nns_edge_custom_is_connected (custom_connection_s * custom)
{
nns_edge_custom_s *custom_h;

if (!custom || !custom->instance)
return NNS_EDGE_ERROR_INVALID_PARAMETER;

custom_h = custom->instance;

return custom_h->nns_edge_custom_is_connected (custom->priv);
}

/**
* @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_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_send_data (custom->priv, data_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to send data to custom connection.");
}

return ret;
}

/**
* @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)
{
nns_edge_custom_s *custom_h;
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;

if (!custom || !custom->instance)
return NNS_EDGE_ERROR_INVALID_PARAMETER;

custom_h = custom->instance;

if (custom_h->nns_edge_custom_set_info) {
ret = custom_h->nns_edge_custom_set_info (custom->priv, key, value);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to set information to custom connection.");
}
}

return ret;
}

/**
* @brief Internal function to get information from custom connection.
*/
int
nns_edge_custom_get_info (custom_connection_s * custom, const char *key,
char **value)
{
nns_edge_custom_s *custom_h;
int ret = NNS_EDGE_ERROR_NOT_SUPPORTED;

if (!custom || !custom->instance)
return NNS_EDGE_ERROR_INVALID_PARAMETER;

custom_h = custom->instance;

if (custom_h->nns_edge_custom_get_info) {
ret = custom_h->nns_edge_custom_get_info (custom->priv, key, value);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to get information from custom connection.");
}
}

return ret;
}
Loading

0 comments on commit 4a08684

Please sign in to comment.