case NNS_EDGE_CONNECT_TYPE_CUSTOM:
{
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- ret = custom_h->nns_edge_custom_send_data (eh->custom.priv, data_h);
- if (NNS_EDGE_ERROR_NONE != ret)
- nns_edge_loge ("Failed to send data via nns edge custom connection.");
+
+ if (custom_h) {
+ ret = custom_h->nns_edge_custom_send_data (eh->custom.priv, data_h);
+ if (NNS_EDGE_ERROR_NONE != ret)
+ nns_edge_loge ("Failed to send data via custom connection.");
+ }
break;
}
default:
goto error;
}
- *edge_h = eh;
- return NNS_EDGE_ERROR_NONE;
-
error:
- nns_edge_release_handle (eh);
+ if (ret == NNS_EDGE_ERROR_NONE)
+ *edge_h = eh;
+ else
+ nns_edge_release_handle (eh);
+
return ret;
}
* @brief Load custom lib and get edge custom instance.
*/
static int
-_nns_edge_load_custom_library (nns_edge_h edge_h, const char *lib_path)
+_nns_edge_load_custom_library (nns_edge_handle_s * eh, const char *lib_path)
{
- nns_edge_handle_s *eh = (nns_edge_handle_s *) edge_h;
+ void *handle;
+ nns_edge_custom_s *custom_h;
- eh->custom.dl_handle = dlopen (lib_path, RTLD_LAZY);
- if (NULL == eh->custom.dl_handle) {
+ handle = dlopen (lib_path, RTLD_LAZY);
+ if (NULL == handle) {
nns_edge_loge ("Failed to open custom lib: %s", dlerror ());
return NNS_EDGE_ERROR_UNKNOWN;
}
- void *(*getCustomHandle) () =
- (void *(*)()) dlsym (eh->custom.dl_handle,
- "nns_edge_custom_get_instance");
- if (!getCustomHandle) {
+ void *(*custom_get_instance) () =
+ (void *(*)()) dlsym (handle, "nns_edge_custom_get_instance");
+ if (!custom_get_instance) {
nns_edge_loge ("Failed to find nns_edge_custom_get_instance: %s",
dlerror ());
- dlclose (eh->custom.dl_handle);
- eh->custom.dl_handle = NULL;
+ dlclose (handle);
+ return NNS_EDGE_ERROR_UNKNOWN;
+ }
+
+ custom_h = (nns_edge_custom_s *) custom_get_instance ();
+ if (!custom_h) {
+ nns_edge_loge ("Failed to get custom instance from library.");
+ dlclose (handle);
return NNS_EDGE_ERROR_UNKNOWN;
}
- eh->custom.instance = (nns_edge_custom_s *) getCustomHandle ();
- return 0;
+ eh->custom.dl_handle = handle;
+ eh->custom.instance = custom_h;
+
+ return NNS_EDGE_ERROR_NONE;
}
/**
nns_edge_loge ("Failed to create edge handle.");
return ret;
}
- eh = (nns_edge_handle_s *) * edge_h;
+
+ eh = (nns_edge_handle_s *) (*edge_h);
eh->connect_type = NNS_EDGE_CONNECT_TYPE_CUSTOM;
- ret = _nns_edge_load_custom_library (*edge_h, lib_path);
+ ret = _nns_edge_load_custom_library (eh, lib_path);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge
("Failed to load custom lib. Please check the custom lib path or permission.");
- return ret;
+ goto error;
}
custom_h = (nns_edge_custom_s *) eh->custom.instance;
nns_edge_loge ("Failed to create custom connection handle.");
}
+error:
+ if (ret != NNS_EDGE_ERROR_NONE)
+ nns_edge_release_handle (eh);
+
return ret;
}
int ret = NNS_EDGE_ERROR_NONE;
nns_edge_handle_s *eh;
- if (connect_type < 0 || connect_type >= NNS_EDGE_CONNECT_TYPE_UNKNOWN) {
+ if (connect_type < 0 || connect_type >= NNS_EDGE_CONNECT_TYPE_UNKNOWN ||
+ connect_type == NNS_EDGE_CONNECT_TYPE_CUSTOM) {
nns_edge_loge ("Invalid param, set valid connect type.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
static int
_nns_edge_custom_start (nns_edge_handle_s * eh)
{
- int ret = NNS_EDGE_ERROR_NONE;
+ int ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- ret = custom_h->nns_edge_custom_start (eh->custom.priv);
+ if (custom_h)
+ ret = custom_h->nns_edge_custom_start (eh->custom.priv);
+
if (NNS_EDGE_ERROR_NONE != ret) {
- nns_edge_loge ("Failed to start edge custom connection");
+ nns_edge_loge ("Failed to start edge custom connection.");
return ret;
}
static int
_nns_edge_custom_stop (nns_edge_handle_s * eh)
{
- int ret = NNS_EDGE_ERROR_NONE;
+ int ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- ret = custom_h->nns_edge_custom_stop (eh->custom.priv);
+
+ if (custom_h)
+ ret = custom_h->nns_edge_custom_stop (eh->custom.priv);
+
if (NNS_EDGE_ERROR_NONE != ret) {
- nns_edge_loge ("Failed to stop nns edge custom connection");
+ nns_edge_loge ("Failed to stop nns edge custom connection.");
}
return ret;
static void
_nns_edge_custom_release (nns_edge_handle_s * eh)
{
+ int ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- int ret = custom_h->nns_edge_custom_close (eh->custom.priv);
+
+ if (custom_h)
+ ret = custom_h->nns_edge_custom_close (eh->custom.priv);
+
if (NNS_EDGE_ERROR_NONE != ret) {
- nns_edge_loge ("Failed to stop nns edge custom connection");
+ nns_edge_loge ("Failed to stop nns edge custom connection.");
}
- dlclose (eh->custom.dl_handle);
- eh->custom.instance = NULL;
+
+ if (eh->custom.dl_handle)
+ dlclose (eh->custom.dl_handle);
+
eh->custom.dl_handle = NULL;
+ eh->custom.instance = NULL;
+ eh->custom.priv = NULL;
}
/**
}
break;
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
- {
_nns_edge_custom_release (eh);
break;
- }
default:
break;
}
}
} else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- ret = custom_h->nns_edge_custom_connect (eh->custom.priv);
+
+ if (custom_h)
+ ret = custom_h->nns_edge_custom_connect (eh->custom.priv);
+ else
+ ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
+
if (ret != NNS_EDGE_ERROR_NONE) {
nns_edge_loge ("Failed to connect to custom connection.");
goto done;
if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- return custom_h->nns_edge_custom_is_connected (eh->custom.priv);
+
+ if (custom_h)
+ return custom_h->nns_edge_custom_is_connected (eh->custom.priv);
+ else
+ return NNS_EDGE_ERROR_CONNECTION_FAILURE;
}
conn_data = (nns_edge_conn_data_s *) eh->connections;
ret = nns_edge_metadata_set (eh->metadata, key, value);
}
- if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
+ if (ret == NNS_EDGE_ERROR_NONE &&
+ NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- ret = custom_h->nns_edge_custom_set_option (eh->custom.priv, key, value);
+
+ if (!custom_h) {
+ ret = NNS_EDGE_ERROR_UNKNOWN;
+ } else if (custom_h->nns_edge_custom_set_option) {
+ ret = custom_h->nns_edge_custom_set_option (eh->custom.priv, key, value);
+ }
}
nns_edge_unlock (eh);
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
+ /* Init null */
+ *value = NULL;
+
if (!nns_edge_handle_is_valid (eh)) {
nns_edge_loge ("Invalid param, given edge handle is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
ret = nns_edge_metadata_get (eh->metadata, key, value);
}
- if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
+ if (ret == NNS_EDGE_ERROR_NONE &&
+ NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom.instance;
- *value = custom_h->nns_edge_custom_get_option (eh->custom.priv, key);
+
+ if (!custom_h) {
+ ret = NNS_EDGE_ERROR_UNKNOWN;
+ } else if (custom_h->nns_edge_custom_get_option) {
+ /* Release old value, then get from custom library. */
+ SAFE_FREE (*value);
+ *value = custom_h->nns_edge_custom_get_option (eh->custom.priv, key);
+ }
}
nns_edge_unlock (eh);
+
+ if (ret != NNS_EDGE_ERROR_NONE)
+ SAFE_FREE (*value);
+
return ret;
}