From: Youngjae Shin Date: Sun, 27 Sep 2015 10:53:06 +0000 (+0900) Subject: revise X-Git-Tag: accepted/tizen/mobile/20151201.031626~87 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=190ad46a3751e4a4ce10569fd68b77905da8136f;p=platform%2Fcore%2Fiot%2Fiotcon.git revise Change-Id: Ifc245aa459b0e08e4a0000e95fe088d86c33fff0 Signed-off-by: youngman --- diff --git a/common/ic-dbus.xml b/common/ic-dbus.xml index 1e01ec2..b36c0b8 100644 --- a/common/ic-dbus.xml +++ b/common/ic-dbus.xml @@ -10,7 +10,6 @@ - diff --git a/daemon/icd-dbus.c b/daemon/icd-dbus.c index 3664818..50a1c8e 100644 --- a/daemon/icd-dbus.c +++ b/daemon/icd-dbus.c @@ -388,7 +388,7 @@ static gboolean _dbus_handle_register_resource(icDbus *object, FN_CALL; int ret; const gchar *sender; - OCResourceHandle handle = NULL; + OCResourceHandle handle; handle = icd_ioty_register_resource(uri_path, resource_types, ifaces, properties); if (handle) { @@ -401,6 +401,8 @@ static gboolean _dbus_handle_register_resource(icDbus *object, ret = icd_ioty_unregister_resource(handle); if (IOTCON_ERROR_NONE != ret) ERR("icd_ioty_unregister_resource(%u) Fail(%d)", handle, ret); + + handle = NULL; } } @@ -423,7 +425,7 @@ static gboolean _dbus_handle_unregister_resource(icDbus *object, _icd_dbus_resource_handle_free(ICD_INT64_TO_POINTER(resource)); - ic_dbus_complete_unregister_resource(object, invocation, ret); + ic_dbus_complete_unregister_resource(object, invocation); return TRUE; } diff --git a/daemon/icd-ioty.c b/daemon/icd-ioty.c index 7fc6886..2111f9c 100644 --- a/daemon/icd-ioty.c +++ b/daemon/icd-ioty.c @@ -688,7 +688,7 @@ OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_type, GVarian OCConnectivityType oic_conn_type; OCDevAddr dev_addr = {0}; - g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure, &options, + g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure, &options, &conn_type); uri = _icd_ioty_resource_generate_uri(uri_path, query); diff --git a/doc/iotcon_doc.h b/doc/iotcon_doc.h index 31995d4..348618e 100644 --- a/doc/iotcon_doc.h +++ b/doc/iotcon_doc.h @@ -254,7 +254,7 @@ static void _request_handler(iotcon_request_h request, void *user_data) if (IOTCON_ERROR_NONE != ret) { return; } - ret = iotcon_observers_append(observers, observer_id, &observers); + ret = iotcon_observers_insert(observers, observer_id); if (IOTCON_ERROR_NONE != ret) { return; } @@ -263,7 +263,7 @@ static void _request_handler(iotcon_request_h request, void *user_data) if (IOTCON_ERROR_NONE != ret) { return; } - ret = iotcon_observers_remove(observers, observer_id, &observers); + ret = iotcon_observers_delete(observers, observer_id); if (IOTCON_ERROR_NONE != ret) { return; } diff --git a/lib/icl-client-crud.c b/lib/icl-client-crud.c index 61c7d8a..e91a4db 100644 --- a/lib/icl-client-crud.c +++ b/lib/icl-client-crud.c @@ -462,7 +462,7 @@ API int iotcon_observer_start(iotcon_client_h resource, int observe_type, GError *error = NULL; int64_t observe_handle; icl_on_observe_s *cb_container; - int ret, signal_number, error_code; + int ret, signal_number; char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -478,11 +478,11 @@ API int iotcon_observer_start(iotcon_client_h resource, int observe_type, arg_query, signal_number, &observe_handle, NULL, &error); if (error) { ERR("ic_dbus_call_observer_start_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); g_variant_unref(arg_query); g_variant_unref(arg_client); - return error_code; + return ret; } if (0 == observe_handle) { @@ -513,6 +513,8 @@ API int iotcon_observer_start(iotcon_client_h resource, int observe_type, _icl_observe_conn_cleanup, _icl_on_observe_cb); if (0 == sub_id) { ERR("icl_dbus_subscribe_signal() Fail"); + iotcon_client_destroy(cb_container->resource); + free(cb_container); return IOTCON_ERROR_DBUS; } resource->observe_sub_id = sub_id; @@ -524,7 +526,7 @@ API int iotcon_observer_start(iotcon_client_h resource, int observe_type, API int iotcon_observer_stop(iotcon_client_h resource) { - int ret, error_code; + int ret; GError *error = NULL; GVariant *arg_options; @@ -541,9 +543,9 @@ API int iotcon_observer_stop(iotcon_client_h resource) arg_options, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_observer_stop_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { ERR("iotcon-daemon Fail(%d)", ret); diff --git a/lib/icl-client.c b/lib/icl-client.c index 72edbb7..816ba26 100644 --- a/lib/icl-client.c +++ b/lib/icl-client.c @@ -81,7 +81,7 @@ API int iotcon_find_resource(const char *host_address, const char *resource_type { unsigned int sub_id; GError *error = NULL; - int ret, error_code, signal_number; + int ret, signal_number; icl_found_resource_s *cb_container; char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; @@ -99,9 +99,9 @@ API int iotcon_find_resource(const char *host_address, const char *resource_type ic_utils_dbus_encode_str(resource_type), signal_number, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_find_resource_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -148,6 +148,7 @@ API int iotcon_client_create(const char *host, RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == resource_types, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == client_handle, IOTCON_ERROR_INVALID_PARAMETER); resource = calloc(1, sizeof(struct icl_remote_resource)); if (NULL == resource) { diff --git a/lib/icl-dbus-type.c b/lib/icl-dbus-type.c index 01a5524..45657e8 100644 --- a/lib/icl-dbus-type.c +++ b/lib/icl-dbus-type.c @@ -30,6 +30,7 @@ #include "icl-client.h" #include "icl-repr.h" #include "icl-payload.h" +#include "icl-observation.h" #include "icl-dbus-type.h" const char** icl_dbus_resource_types_to_array(iotcon_resource_types_h types) @@ -201,7 +202,7 @@ GVariant* icl_dbus_options_to_gvariant(iotcon_options_h options) } } - return g_variant_new("a(qs)", builder); + return g_variant_builder_end(&builder); } @@ -211,7 +212,7 @@ GVariant* icl_dbus_observers_to_gvariant(iotcon_observers_h observers) GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE("ai")); - for (node = observers; node; node = node->next) + for (node = observers->observers_list; node; node = node->next) g_variant_builder_add(&builder, "i", GPOINTER_TO_INT(node->data)); return g_variant_builder_end(&builder); diff --git a/lib/icl-dbus.c b/lib/icl-dbus.c index ee4dfb8..1a46f89 100644 --- a/lib/icl-dbus.c +++ b/lib/icl-dbus.c @@ -42,7 +42,7 @@ icDbus* icl_dbus_get_object() } -inline unsigned int icl_dbus_generate_signal_number() +unsigned int icl_dbus_generate_signal_number() { static unsigned int i = 0; @@ -267,17 +267,17 @@ int icl_dbus_start() } -int icl_dbus_stop() +void icl_dbus_stop() { if (0 == icl_dbus_count) { - ERR("dbus not initialized"); - return IOTCON_ERROR_DBUS; + WARN("dbus not initialized"); + return; } icl_dbus_count--; if (0 < icl_dbus_count) - return IOTCON_ERROR_NONE; + return; DBG("All connection is closed"); @@ -289,5 +289,5 @@ int icl_dbus_stop() g_object_unref(icl_dbus_object); icl_dbus_object = NULL; - return IOTCON_ERROR_NONE; + return; } diff --git a/lib/icl-dbus.h b/lib/icl-dbus.h index 9bdb1d1..c381943 100644 --- a/lib/icl-dbus.h +++ b/lib/icl-dbus.h @@ -33,6 +33,6 @@ int icl_dbus_convert_daemon_error(int error); int icl_dbus_convert_dbus_error(int error); int icl_dbus_start(); -int icl_dbus_stop(); +void icl_dbus_stop(); #endif /* __IOT_CONNECTIVITY_MANAGER_DBUS_H__ */ diff --git a/lib/icl-device.c b/lib/icl-device.c index 4b25012..120758b 100644 --- a/lib/icl-device.c +++ b/lib/icl-device.c @@ -56,7 +56,7 @@ typedef struct { API int iotcon_register_device_info(const char *device_name) { - int ret, error_code; + int ret; GError *error = NULL; GVariant *arg_info; @@ -68,10 +68,10 @@ API int iotcon_register_device_info(const char *device_name) NULL, &error); if (error) { ERR("ic_dbus_call_register_device_info_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); g_variant_unref(arg_info); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -108,7 +108,7 @@ API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb c { GError *error = NULL; unsigned int sub_id; - int ret, signal_number, error_code; + int ret, signal_number; char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; icl_device_info_s *cb_container; @@ -122,9 +122,9 @@ API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb c signal_number, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -148,6 +148,7 @@ API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb c _icl_device_info_cb); if (0 == sub_id) { ERR("icl_dbus_subscribe_signal() Fail"); + free(cb_container); return IOTCON_ERROR_DBUS; } @@ -161,7 +162,7 @@ API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb c * The length of manufacturer_url should be less than and equal to 32. */ API int iotcon_register_platform_info(iotcon_platform_info_s *platform_info) { - int ret, error_code; + int ret; GError *error = NULL; GVariant *arg_info; @@ -199,10 +200,10 @@ API int iotcon_register_platform_info(iotcon_platform_info_s *platform_info) NULL, &error); if (error) { ERR("ic_dbus_call_register_platform_info_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); g_variant_unref(arg_info); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -223,7 +224,7 @@ static void _icl_platform_info_cb(GDBusConnection *connection, gpointer user_data) { char *uri_path; - iotcon_platform_info_s *info = NULL; + iotcon_platform_info_s *info; icl_platform_info_s *cb_container = user_data; iotcon_platform_info_cb cb = cb_container->cb; @@ -261,7 +262,7 @@ API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_ { GError *error = NULL; unsigned int sub_id; - int ret, signal_number, error_code; + int ret, signal_number; char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; icl_platform_info_s *cb_container; @@ -275,9 +276,9 @@ API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_ signal_number, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -301,6 +302,7 @@ API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_ _icl_platform_info_cb); if (0 == sub_id) { ERR("icl_dbus_subscribe_signal() Fail"); + free(cb_container); return IOTCON_ERROR_DBUS; } diff --git a/lib/icl-observation.c b/lib/icl-observation.c index b91ea0c..6edeab2 100644 --- a/lib/icl-observation.c +++ b/lib/icl-observation.c @@ -19,29 +19,50 @@ #include "iotcon-struct.h" #include "icl.h" +#include "icl-observation.h" + +API int iotcon_observers_create(iotcon_observers_h *ret_observers) +{ + RETV_IF(NULL == ret_observers, IOTCON_ERROR_INVALID_PARAMETER); + + iotcon_observers_h observers = calloc(1, sizeof(struct icl_observers)); + if (NULL == observers) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + *ret_observers = observers; + + return IOTCON_ERROR_NONE; +} + API void iotcon_observers_destroy(iotcon_observers_h observers) { RET_IF(NULL == observers); - g_list_free(observers); + g_list_free(observers->observers_list); + free(observers); } -/* If you want to make a new list, then you should set observers is NULL. */ -API int iotcon_observers_append(iotcon_observers_h observers, int obs_id, - iotcon_observers_h *ret_observers) +API int iotcon_observers_insert(iotcon_observers_h observers, int obs_id) { - *ret_observers = g_list_append(observers, GUINT_TO_POINTER(obs_id)); + RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); + + observers->observers_list = g_list_append(observers->observers_list, + GUINT_TO_POINTER(obs_id)); return IOTCON_ERROR_NONE; } -API int iotcon_observers_remove(iotcon_observers_h observers, int obs_id, - iotcon_observers_h *ret_observers) +API int iotcon_observers_delete(iotcon_observers_h observers, int obs_id) { - *ret_observers = g_list_remove(observers, GUINT_TO_POINTER(obs_id)); + RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); + + observers->observers_list = g_list_remove(observers->observers_list, + GUINT_TO_POINTER(obs_id)); return IOTCON_ERROR_NONE; } diff --git a/lib/icl-observation.h b/lib/icl-observation.h new file mode 100644 index 0000000..d9ec2e8 --- /dev/null +++ b/lib/icl-observation.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __IOT_CONNECTIVITY_MANAGER_LIBRARY_OBSERVATION_H__ +#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_OBSERVATION_H__ + +#include + +struct icl_observers { + GList *observers_list; +}; + +#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_OBSERVATION_H__ */ diff --git a/lib/icl-options.c b/lib/icl-options.c index 832dccb..7b12320 100644 --- a/lib/icl-options.c +++ b/lib/icl-options.c @@ -68,7 +68,11 @@ iotcon_options_h icl_options_ref(iotcon_options_h options) API int iotcon_options_create(iotcon_options_h *ret_options) { - iotcon_options_h options = calloc(1, sizeof(struct icl_options)); + iotcon_options_h options; + + RETV_IF(NULL == ret_options, IOTCON_ERROR_INVALID_PARAMETER); + + options = calloc(1, sizeof(struct icl_options)); if (NULL == options) { ERR("calloc() Fail(%d)", errno); return IOTCON_ERROR_OUT_OF_MEMORY; @@ -123,14 +127,14 @@ API int iotcon_options_insert(iotcon_options_h options, unsigned short id, API int iotcon_options_delete(iotcon_options_h options, unsigned short id) { - gboolean ret; + gboolean is_removed; RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(1 < options->ref_count, IOTCON_ERROR_INVALID_PARAMETER, "Don't modify it. It is already set."); - ret = g_hash_table_remove(options->hash, GUINT_TO_POINTER(id)); - if (FALSE == ret) { + is_removed = g_hash_table_remove(options->hash, GUINT_TO_POINTER(id)); + if (FALSE == is_removed) { ERR("g_hash_table_remove() Fail"); return IOTCON_ERROR_INVALID_PARAMETER; } @@ -138,18 +142,20 @@ API int iotcon_options_delete(iotcon_options_h options, unsigned short id) } -API int iotcon_options_lookup(iotcon_options_h options, unsigned short id, - const char **data) +API int iotcon_options_lookup(iotcon_options_h options, unsigned short id, char **data) { - const char *ret; + char *value; RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == data, IOTCON_ERROR_INVALID_PARAMETER); - ret = g_hash_table_lookup(options->hash, GUINT_TO_POINTER(id)); - if (NULL == ret) + value = g_hash_table_lookup(options->hash, GUINT_TO_POINTER(id)); + if (NULL == value) { ERR("g_hash_table_lookup() Fail"); + return IOTCON_ERROR_INVALID_PARAMETER; + } - *data = ret; + *data = value; return IOTCON_ERROR_NONE; } diff --git a/lib/icl-options.h b/lib/icl-options.h index 8069546..4dbf3b9 100644 --- a/lib/icl-options.h +++ b/lib/icl-options.h @@ -16,6 +16,7 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_LIBRARY_OPTIONS_H__ #define __IOT_CONNECTIVITY_MANAGER_LIBRARY_OPTIONS_H__ +#include #include "iotcon-struct.h" struct icl_options { diff --git a/lib/icl-payload.c b/lib/icl-payload.c index f5608fe..3c3f291 100644 --- a/lib/icl-payload.c +++ b/lib/icl-payload.c @@ -345,14 +345,6 @@ iotcon_representation_h icl_representation_from_gvariant(GVariant *var) return NULL; } - ret = iotcon_representation_set_state(repr, state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(state); - iotcon_representation_destroy(repr); - return NULL; - } - DBG("repr : %s", g_variant_print(var, FALSE)); g_variant_get(var, "(&siasa{sv}av)", &uri_path, &repr->interfaces, @@ -382,6 +374,15 @@ iotcon_representation_h icl_representation_from_gvariant(GVariant *var) /* attribute */ _icl_state_from_gvariant(state, repr_gvar); + ret = iotcon_representation_set_state(repr, state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; + } + iotcon_state_destroy(state); + /* children */ while (g_variant_iter_loop(children, "v", &child)) { repr->children = g_list_append(repr->children, @@ -389,7 +390,5 @@ iotcon_representation_h icl_representation_from_gvariant(GVariant *var) } g_variant_iter_free(children); - iotcon_state_destroy(state); - return repr; } diff --git a/lib/icl-presence.c b/lib/icl-presence.c index 50578f4..dcb7b0f 100644 --- a/lib/icl-presence.c +++ b/lib/icl-presence.c @@ -37,7 +37,7 @@ typedef struct icl_presence { API int iotcon_start_presence(unsigned int time_to_live) { - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -47,9 +47,9 @@ API int iotcon_start_presence(unsigned int time_to_live) &error); if (error) { ERR("ic_dbus_call_start_presence_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -64,7 +64,7 @@ API int iotcon_start_presence(unsigned int time_to_live) API int iotcon_stop_presence(void) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -72,9 +72,9 @@ API int iotcon_stop_presence(void) ic_dbus_call_stop_presence_sync(icl_dbus_get_object(), &ret, NULL, &error); if (error) { ERR("ic_dbus_call_stop_presence_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -130,7 +130,7 @@ API int iotcon_subscribe_presence(const char *host_address, const char *resource FN_CALL; GError *error = NULL; unsigned int sub_id; - int signal_number, error_code; + int signal_number, ret; char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; icl_presence_s *presence_container; @@ -157,10 +157,10 @@ API int iotcon_subscribe_presence(const char *host_address, const char *resource resource_type, signal_number, &(presence_container->handle), NULL, &error); if (error) { ERR("ic_dbus_call_subscribe_presence_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); free(presence_container); - return error_code; + return ret; } if (0 == presence_container->handle) { @@ -194,7 +194,7 @@ API int iotcon_subscribe_presence(const char *host_address, const char *resource API int iotcon_unsubscribe_presence(iotcon_presence_h presence) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -210,9 +210,9 @@ API int iotcon_unsubscribe_presence(iotcon_presence_h presence) &ret, NULL, &error); if (error) { ERR("ic_dbus_call_unsubscribe_presence_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { diff --git a/lib/icl-query.c b/lib/icl-query.c index 32b7b34..3924f88 100644 --- a/lib/icl-query.c +++ b/lib/icl-query.c @@ -27,6 +27,8 @@ API int iotcon_query_create(iotcon_query_h *ret_query) { + RETV_IF(NULL == ret_query, IOTCON_ERROR_INVALID_PARAMETER); + iotcon_query_h query = calloc(1, sizeof(struct icl_query)); if (NULL == query) { ERR("calloc() Fail(%d)", errno); @@ -76,7 +78,7 @@ API int iotcon_query_insert(iotcon_query_h query, const char *key, const char *v API int iotcon_query_delete(iotcon_query_h query, const char *key) { - gboolean ret; + gboolean is_removed; int query_len; char *value; @@ -91,8 +93,8 @@ API int iotcon_query_delete(iotcon_query_h query, const char *key) query_len = strlen(key) + strlen(value) + 2; - ret = g_hash_table_remove(query->hash, key); - if (FALSE == ret) { + is_removed = g_hash_table_remove(query->hash, key); + if (FALSE == is_removed) { ERR("g_hash_table_remove() Fail"); return IOTCON_ERROR_INVALID_PARAMETER; } @@ -102,16 +104,19 @@ API int iotcon_query_delete(iotcon_query_h query, const char *key) } -API int iotcon_query_lookup(iotcon_query_h query, const char *key, const char **data) +API int iotcon_query_lookup(iotcon_query_h query, const char *key, char **data) { - const char *value = NULL; + char *value = NULL; RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == data, IOTCON_ERROR_INVALID_PARAMETER); value = g_hash_table_lookup(query->hash, key); - if (NULL == value) + if (NULL == value) { ERR("g_hash_table_lookup() Fail"); + return IOTCON_ERROR_INVALID_PARAMETER; + } *data = value; diff --git a/lib/icl-query.h b/lib/icl-query.h index 421ba43..d36e671 100644 --- a/lib/icl-query.h +++ b/lib/icl-query.h @@ -16,7 +16,7 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_LIBRARY_QUERY_H__ #define __IOT_CONNECTIVITY_MANAGER_LIBRARY_QUERY_H__ -#include "iotcon-struct.h" +#include /** * @brief The maximum length which can be held in a query. diff --git a/lib/icl-repr-list.c b/lib/icl-repr-list.c index 6640b47..22fb4cf 100644 --- a/lib/icl-repr-list.c +++ b/lib/icl-repr-list.c @@ -72,9 +72,11 @@ API int iotcon_list_create(iotcon_types_e type, iotcon_list_h *ret_list) int ret; iotcon_list_h list; + RETV_IF(NULL == ret_list, IOTCON_ERROR_INVALID_PARAMETER); + if (type < IOTCON_TYPE_INT || IOTCON_TYPE_STATE < type) { ERR("Invalid Type(%d)", type); - return IOTCON_ERROR_INVALID_PARAMETER; + return IOTCON_ERROR_INVALID_TYPE; } ret = _icl_list_create(type, &list); @@ -286,10 +288,10 @@ API int iotcon_list_get_nth_double(iotcon_list_h list, int pos, double *val) } -API int iotcon_list_get_nth_str(iotcon_list_h list, int pos, const char **val) +API int iotcon_list_get_nth_str(iotcon_list_h list, int pos, char **val) { int ret; - const char *strval; + char *strval; iotcon_value_h value; RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER); diff --git a/lib/icl-repr-state.c b/lib/icl-repr-state.c index 8edd31f..419e5fa 100644 --- a/lib/icl-repr-state.c +++ b/lib/icl-repr-state.c @@ -35,20 +35,14 @@ void icl_state_inc_ref_count(iotcon_state_h val) } -bool icl_state_dec_ref_count(iotcon_state_h val) +int icl_state_dec_ref_count(iotcon_state_h val) { - bool ret; - RETV_IF(NULL == val, -1); - RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count); + RETVM_IF(val->ref_count <= 0, 0, "Invalid Count(%d)", val->ref_count); val->ref_count--; - if (0 == val->ref_count) - ret = true; - else - ret = false; - return ret; + return val->ref_count; } @@ -57,6 +51,8 @@ API int iotcon_state_create(iotcon_state_h *ret_state) errno = 0; iotcon_state_h state; + RETV_IF(NULL == ret_state, IOTCON_ERROR_INVALID_PARAMETER); + state = calloc(1, sizeof(struct icl_state_s)); if (NULL == state) { ERR("calloc() Fail(%d)", errno); @@ -77,12 +73,10 @@ API void iotcon_state_destroy(iotcon_state_h state) { RET_IF(NULL == state); - if (false == icl_state_dec_ref_count(state)) { - return; + if (0 == icl_state_dec_ref_count(state)) { + g_hash_table_destroy(state->hash_table); + free(state); } - - g_hash_table_destroy(state->hash_table); - free(state); } diff --git a/lib/icl-repr-state.h b/lib/icl-repr-state.h index 15791e9..b796e8d 100644 --- a/lib/icl-repr-state.h +++ b/lib/icl-repr-state.h @@ -21,7 +21,7 @@ #include "icl-repr-value.h" void icl_state_inc_ref_count(iotcon_state_h val); -bool icl_state_dec_ref_count(iotcon_state_h val); +int icl_state_dec_ref_count(iotcon_state_h val); int icl_state_del_value(iotcon_state_h state, const char *key, iotcon_types_e value_type); diff --git a/lib/icl-repr-value.c b/lib/icl-repr-value.c index 2397aaa..214cd5e 100644 --- a/lib/icl-repr-value.c +++ b/lib/icl-repr-value.c @@ -204,7 +204,7 @@ int icl_value_get_double(iotcon_value_h value, double *val) return IOTCON_ERROR_NONE; } -int icl_value_get_str(iotcon_value_h value, const char **val) +int icl_value_get_str(iotcon_value_h value, char **val) { icl_basic_s *real = (icl_basic_s*)value; diff --git a/lib/icl-repr-value.h b/lib/icl-repr-value.h index 01d4a20..b3f4993 100644 --- a/lib/icl-repr-value.h +++ b/lib/icl-repr-value.h @@ -76,7 +76,7 @@ iotcon_value_h icl_value_create_state(iotcon_state_h val); int icl_value_get_int(iotcon_value_h value, int *val); int icl_value_get_bool(iotcon_value_h value, bool *val); int icl_value_get_double(iotcon_value_h value, double *val); -int icl_value_get_str(iotcon_value_h value, const char **val); +int icl_value_get_str(iotcon_value_h value, char **val); int icl_value_get_list(iotcon_value_h value, iotcon_list_h *list); int icl_value_get_state(iotcon_value_h value, iotcon_state_h *state); diff --git a/lib/icl-repr.c b/lib/icl-repr.c index ac711a2..f70e23f 100644 --- a/lib/icl-repr.c +++ b/lib/icl-repr.c @@ -65,6 +65,8 @@ API int iotcon_representation_create(iotcon_representation_h *ret_repr) errno = 0; iotcon_representation_h repr; + RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER); + repr = calloc(1, sizeof(struct icl_representation_s)); if (NULL == repr) { ERR("calloc() Fail(%d)", errno); @@ -104,7 +106,7 @@ API void iotcon_representation_destroy(iotcon_representation_h repr) } -API int iotcon_representation_get_uri_path(iotcon_representation_h repr, const char **uri_path) +API int iotcon_representation_get_uri_path(iotcon_representation_h repr, char **uri_path) { RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER); @@ -114,7 +116,8 @@ API int iotcon_representation_get_uri_path(iotcon_representation_h repr, const c return IOTCON_ERROR_NONE; } -API int iotcon_representation_set_uri_path(iotcon_representation_h repr, const char *uri_path) +API int iotcon_representation_set_uri_path(iotcon_representation_h repr, + const char *uri_path) { RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); diff --git a/lib/icl-resource-types.c b/lib/icl-resource-types.c index 4e400eb..eab91cf 100644 --- a/lib/icl-resource-types.c +++ b/lib/icl-resource-types.c @@ -37,7 +37,11 @@ iotcon_resource_types_h icl_resource_types_ref(iotcon_resource_types_h types) API int iotcon_resource_types_create(iotcon_resource_types_h *ret_types) { - iotcon_resource_types_h types = calloc(1, sizeof(struct icl_resource_types)); + iotcon_resource_types_h types; + + RETV_IF(NULL == ret_types, IOTCON_ERROR_INVALID_PARAMETER); + + types = calloc(1, sizeof(struct icl_resource_types)); if (NULL == types) { ERR("calloc() Fail(%d)", errno); return IOTCON_ERROR_OUT_OF_MEMORY; @@ -73,13 +77,13 @@ static int _icl_resource_types_strcmp(const void *a, const void *b) static bool _icl_resource_types_duplicate_check(iotcon_resource_types_h types, const char *type) { - GList *ret = NULL; + GList *found_node = NULL; RETV_IF(NULL == types, false); RETV_IF(NULL == type, false); - ret = g_list_find_custom(types->type_list, type, _icl_resource_types_strcmp); - if (NULL == ret) + found_node = g_list_find_custom(types->type_list, type, _icl_resource_types_strcmp); + if (NULL == found_node) return false; return true; diff --git a/lib/icl-resource-types.h b/lib/icl-resource-types.h index acae138..11050e3 100644 --- a/lib/icl-resource-types.h +++ b/lib/icl-resource-types.h @@ -16,6 +16,7 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_TYPES_H__ #define __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_TYPES_H__ +#include #include "iotcon-struct.h" #include "icl-query.h" diff --git a/lib/icl-resource.c b/lib/icl-resource.c index 2b91906..125f6b7 100644 --- a/lib/icl-resource.c +++ b/lib/icl-resource.c @@ -160,10 +160,10 @@ API int iotcon_register_resource(const char *uri_path, GError *error = NULL; const gchar **types; iotcon_resource_h resource; - int signal_number, error_code; + int signal_number, ret; char sig_name[IC_DBUS_SIGNAL_LENGTH]; - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path), IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path); @@ -189,11 +189,11 @@ API int iotcon_register_resource(const char *uri_path, properties, signal_number, &(resource->handle), NULL, &error); if (error) { ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); free(types); free(resource); - return error_code; + return ret; } free(types); @@ -218,6 +218,8 @@ API int iotcon_register_resource(const char *uri_path, _icl_request_handler); if (0 == sub_id) { ERR("icl_dbus_subscribe_signal() Fail"); + iotcon_resource_types_destroy(res_types); + free(resource->uri_path); free(resource); return IOTCON_ERROR_DBUS; } @@ -233,7 +235,7 @@ API int iotcon_register_resource(const char *uri_path, API int iotcon_unregister_resource(iotcon_resource_h resource) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -247,19 +249,15 @@ API int iotcon_unregister_resource(iotcon_resource_h resource) return IOTCON_ERROR_NONE; } - ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, - &ret, NULL, &error); + ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL, + &error); if (error) { ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } resource->handle = 0; icl_dbus_unsubscribe_signal(resource->sub_id); @@ -271,7 +269,7 @@ API int iotcon_unregister_resource(iotcon_resource_h resource) API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -285,9 +283,9 @@ API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface) iface, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -302,7 +300,7 @@ API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface) API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -322,9 +320,9 @@ API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resour &ret, NULL, &error); if (error) { ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -353,7 +351,7 @@ API int iotcon_resource_bind_child_resource(iotcon_resource_h parent, iotcon_resource_h child) { GError *error = NULL; - int i, ret, error_code; + int i, ret; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); @@ -382,9 +380,9 @@ API int iotcon_resource_bind_child_resource(iotcon_resource_h parent, child->handle, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -407,7 +405,7 @@ API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent, iotcon_resource_h child) { GError *error = NULL; - int i, ret, error_code; + int i, ret; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); @@ -426,9 +424,9 @@ API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent, child->handle, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -531,6 +529,7 @@ API int iotcon_notimsg_create(iotcon_representation_h repr, int iface, iotcon_notimsg_h msg; RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == notimsg_handle, IOTCON_ERROR_INVALID_PARAMETER); msg = calloc(1, sizeof(struct icl_notify_msg)); if (NULL == msg) { @@ -561,7 +560,7 @@ API void iotcon_notimsg_destroy(iotcon_notimsg_h msg) API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg, iotcon_observers_h observers) { - int ret, error_code; + int ret; GError *error = NULL; GVariant *noti_msg; GVariant *obs; @@ -586,11 +585,11 @@ API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notim noti_msg, obs, &ret, NULL, &error); if (error) { ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); g_variant_unref(obs); g_variant_unref(noti_msg); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { @@ -604,7 +603,7 @@ API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notim API int iotcon_resource_notify_all(iotcon_resource_h resource) { - int ret, error_code; + int ret; GError *error = NULL; RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); @@ -618,9 +617,9 @@ API int iotcon_resource_notify_all(iotcon_resource_h resource) &error); if (error) { ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { diff --git a/lib/icl-response.c b/lib/icl-response.c index b78c721..92c891a 100644 --- a/lib/icl-response.c +++ b/lib/icl-response.c @@ -36,7 +36,8 @@ API int iotcon_response_create(iotcon_request_h request, { FN_CALL; - RETV_IF(NULL == request, TIZEN_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == response, IOTCON_ERROR_INVALID_PARAMETER); iotcon_response_h resp = calloc(1, sizeof(struct icl_resource_response)); if (NULL == resp) { @@ -209,7 +210,7 @@ static int _icl_response_check_representation_visibility(iotcon_response_h resp) API int iotcon_response_send(iotcon_response_h resp) { FN_CALL; - int ret, error_code; + int ret; GError *error = NULL; GVariant *arg_response; @@ -228,10 +229,10 @@ API int iotcon_response_send(iotcon_response_h resp) &error); if (error) { ERR("ic_dbus_call_send_response_sync() Fail(%s)", error->message); - error_code = icl_dbus_convert_dbus_error(error->code); + ret = icl_dbus_convert_dbus_error(error->code); g_error_free(error); g_variant_unref(arg_response); - return error_code; + return ret; } if (IOTCON_ERROR_NONE != ret) { diff --git a/lib/icl.c b/lib/icl.c index 9228d98..3d9f835 100644 --- a/lib/icl.c +++ b/lib/icl.c @@ -35,13 +35,7 @@ API int iotcon_open(void) } -API int iotcon_close(void) +API void iotcon_close(void) { - int ret; - - ret = icl_dbus_stop(); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_stop() Fail(%d)", ret); - - return ret; + icl_dbus_stop(); } diff --git a/lib/include/iotcon-representation.h b/lib/include/iotcon-representation.h index bc9f735..972b9aa 100644 --- a/lib/include/iotcon-representation.h +++ b/lib/include/iotcon-representation.h @@ -37,6 +37,7 @@ * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * * @see iotcon_representation_destroy() */ @@ -69,7 +70,8 @@ void iotcon_representation_destroy(iotcon_representation_h repr); * @retval iotcon_representation_h Success * @retval NULL Failure */ -int iotcon_representation_clone(const iotcon_representation_h src, iotcon_representation_h *dest); +int iotcon_representation_clone(const iotcon_representation_h src, + iotcon_representation_h *dest); /** * @ingroup CAPI_IOT_CONNECTIVITY_MODULE @@ -87,7 +89,8 @@ int iotcon_representation_clone(const iotcon_representation_h src, iotcon_repres * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_set_uri_path(iotcon_representation_h repr, const char *uri_path); +int iotcon_representation_set_uri_path(iotcon_representation_h repr, + const char *uri_path); /** * @brief Gets an URI path from the representation. @@ -101,7 +104,7 @@ int iotcon_representation_set_uri_path(iotcon_representation_h repr, const char * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_get_uri_path(iotcon_representation_h repr, const char **uri_path); +int iotcon_representation_get_uri_path(iotcon_representation_h repr, char **uri_path); /** * @ingroup CAPI_IOT_CONNECTIVITY_MODULE @@ -117,7 +120,8 @@ int iotcon_representation_get_uri_path(iotcon_representation_h repr, const char * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_set_resource_types(iotcon_representation_h repr, iotcon_resource_types_h types); +int iotcon_representation_set_resource_types(iotcon_representation_h repr, + iotcon_resource_types_h types); /** * @brief Gets list of resource type from the representation. @@ -131,7 +135,8 @@ int iotcon_representation_set_resource_types(iotcon_representation_h repr, iotco * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_get_resource_types(iotcon_representation_h repr, iotcon_resource_types_h *types); +int iotcon_representation_get_resource_types(iotcon_representation_h repr, + iotcon_resource_types_h *types); /** * @brief Sets interfaces to the representation. @@ -148,7 +153,8 @@ int iotcon_representation_get_resource_types(iotcon_representation_h repr, iotco * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr, int ifaces); +int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr, + int ifaces); /** * @brief Gets resource interfaces from the representation. @@ -162,7 +168,8 @@ int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr, * @retval #IOTCON_INTERFACE_NONE Not set * @retval Bitwise OR value which consists of iotcon_interface_e items */ -int iotcon_representation_get_resource_interfaces(iotcon_representation_h repr, int *ifaces); +int iotcon_representation_get_resource_interfaces(iotcon_representation_h repr, + int *ifaces); /** * @brief Sets a new state handle into the representation. @@ -216,6 +223,7 @@ int iotcon_representation_del_state(iotcon_representation_h repr); * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * * @see iotcon_state_destroy() */ @@ -668,8 +676,8 @@ typedef bool (*iotcon_children_cb)(iotcon_representation_h child, void *user_dat * * @see iotcon_children_cb() */ -int iotcon_representation_foreach_children(iotcon_representation_h parent, iotcon_children_cb cb, - void *user_data); +int iotcon_representation_foreach_children(iotcon_representation_h parent, + iotcon_children_cb cb, void *user_data); /** * @brief Gets the number of children representation in the parent representation @@ -683,7 +691,8 @@ int iotcon_representation_foreach_children(iotcon_representation_h parent, iotco * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter */ -int iotcon_representation_get_children_count(iotcon_representation_h parent, unsigned int *count); +int iotcon_representation_get_children_count(iotcon_representation_h parent, + unsigned int *count); /** * @brief Gets the child representation at the given position. @@ -700,7 +709,8 @@ int iotcon_representation_get_children_count(iotcon_representation_h parent, uns * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_NO_DATA No data available */ -int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos, iotcon_representation_h *child); +int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos, + iotcon_representation_h *child); /** * @brief Specifies the type of function passed to iotcon_state_foreach() @@ -764,9 +774,11 @@ int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count); * @param[in] type The type of list * @param[out] list A newly allocated list handle * - * @return A newly allocated list handle, otherwise NULL on failure. - * @retval iotcon_list_h Success - * @retval NULL Failure + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type */ int iotcon_list_create(iotcon_types_e type, iotcon_list_h *list); @@ -966,7 +978,7 @@ int iotcon_list_get_nth_double(iotcon_list_h list, int pos, double *val); * @retval #IOTCON_ERROR_NO_DATA No data available * @retval #IOTCON_ERROR_REPRESENTATION Representation errors */ -int iotcon_list_get_nth_str(iotcon_list_h list, int pos, const char **val); +int iotcon_list_get_nth_str(iotcon_list_h list, int pos, char **val); /** * @brief Gets the list value at the given position. diff --git a/lib/include/iotcon-struct.h b/lib/include/iotcon-struct.h index 299b820..c0ce269 100644 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -130,6 +130,7 @@ typedef struct icl_options* iotcon_options_h; * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * * @see iotcon_options_destroy() * @see iotcon_options_insert() @@ -216,7 +217,7 @@ int iotcon_options_delete(iotcon_options_h options, unsigned short id); * @see iotcon_options_insert() * @see iotcon_options_delete() */ -int iotcon_options_lookup(iotcon_options_h options, unsigned short id, const char **data); +int iotcon_options_lookup(iotcon_options_h options, unsigned short id, char **data); /** * @brief Specifies the type of function passed to iotcon_options_foreach() @@ -368,7 +369,7 @@ int iotcon_query_delete(iotcon_query_h query, const char *key); * @see iotcon_query_insert() * @see iotcon_query_delete() */ -int iotcon_query_lookup(iotcon_query_h query, const char *key, const char **data); +int iotcon_query_lookup(iotcon_query_h query, const char *key, char **data); /** * @brief Specifies the type of function passed to iotcon_query_foreach() @@ -432,6 +433,7 @@ typedef struct icl_resource_types* iotcon_resource_types_h; * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * * @see iotcon_resource_types_destroy() * @see iotcon_resource_types_insert() @@ -565,7 +567,25 @@ int iotcon_resource_types_clone(iotcon_resource_types_h src, * * @since_tizen 3.0 */ -typedef void* iotcon_observers_h; +typedef struct icl_observers* iotcon_observers_h; + +/** + * @brief Creates a new observers handle. + * + * @since_tizen 3.0 + * + * @param[out] ret_observers A newly allocated list of observers handle + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * + * @see iotcon_observers_destroy() + * @see iotcon_observers_insert() + * @see iotcon_observers_delete() + */ +int iotcon_observers_create(iotcon_observers_h *ret_observers); /** * @brief Free a observers handle. @@ -576,8 +596,9 @@ typedef void* iotcon_observers_h; * * @return void * - * @see iotcon_observers_append() - * @see iotcon_observers_remove() + * @see iotcon_observers_create() + * @see iotcon_observers_insert() + * @see iotcon_observers_delete() */ void iotcon_observers_destroy(iotcon_observers_h observers); @@ -593,12 +614,13 @@ void iotcon_observers_destroy(iotcon_observers_h observers); * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * + * @see iotcon_observers_create() * @see iotcon_observers_destroy() - * @see iotcon_observers_remove() + * @see iotcon_observers_delete() */ -int iotcon_observers_append(iotcon_observers_h observers, int obs_id, - iotcon_observers_h *ret_observers); +int iotcon_observers_insert(iotcon_observers_h observers, int obs_id); /** * @brief Remove id from the observers. @@ -611,12 +633,13 @@ int iotcon_observers_append(iotcon_observers_h observers, int obs_id, * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * + * @see iotcon_observers_create() * @see iotcon_observers_destroy() - * @see iotcon_observers_append() + * @see iotcon_observers_insert() */ -int iotcon_observers_remove(iotcon_observers_h observers, int obs_id, - iotcon_observers_h *ret_observers); +int iotcon_observers_delete(iotcon_observers_h observers, int obs_id); /** * @brief The handle of resource. @@ -968,7 +991,8 @@ int iotcon_request_get_uri_path(iotcon_request_h request, char **uri_path); * @see iotcon_request_get_observer_action() * @see iotcon_request_get_observer_id() */ -int iotcon_request_get_representation(iotcon_request_h request, iotcon_representation_h *repr); +int iotcon_request_get_representation(iotcon_request_h request, + iotcon_representation_h *repr); /** * @brief Get types of the request @@ -1171,7 +1195,8 @@ int iotcon_response_set_result(iotcon_response_h resp, int result); * @see iotcon_response_create() * @see iotcon_response_destroy() */ -int iotcon_response_set_representation(iotcon_response_h resp, iotcon_representation_h repr); +int iotcon_response_set_representation(iotcon_response_h resp, + iotcon_representation_h repr); /** * @brief Sets header options into the response diff --git a/lib/include/iotcon.h b/lib/include/iotcon.h index c899b2c..7dd0b49 100644 --- a/lib/include/iotcon.h +++ b/lib/include/iotcon.h @@ -67,7 +67,7 @@ int iotcon_open(void); * * @see iotcon_open() */ -int iotcon_close(void); +void iotcon_close(void); /** * @brief Specifies the type of function passed to iotcon_add_connection_changed_cb() and @@ -181,6 +181,7 @@ typedef void (*iotcon_request_handler_cb)(iotcon_resource_h resource, * @retval #IOTCON_ERROR_IOTIVITY Iotivity errors * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the resource receive CRUD request, iotcon_request_handler_cb() will be called. * @@ -207,12 +208,17 @@ int iotcon_register_resource(const char *uri_path, * @privlevel public * @privilege %http://tizen.org/privilege/internet * + * @remarks When a normal variable is used, there are only dbus error and permission\n + * denied error. If the errors of this API are not handled, then you must check\n + * whether dbus is running and an application have the privileges for the API. + * * @param[in] resource_handle The handle of the resource to be unregistered * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_register_resource() * @see iotcon_resource_bind_interface() @@ -242,6 +248,7 @@ int iotcon_unregister_resource(iotcon_resource_h resource_handle); * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_register_resource() * @see iotcon_unregister_resource() @@ -268,6 +275,7 @@ int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface); * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_register_resource() * @see iotcon_unregister_resource() @@ -277,7 +285,8 @@ int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface); * @see iotcon_resource_unbind_child_resource() * @see iotcon_request_handler_cb() */ -int iotcon_resource_bind_type(iotcon_resource_h resource_handle, const char *resource_type); +int iotcon_resource_bind_type(iotcon_resource_h resource_handle, + const char *resource_type); /** * @brief Binds a request handler to the resource @@ -303,7 +312,8 @@ int iotcon_resource_bind_type(iotcon_resource_h resource_handle, const char *res * @see iotcon_resource_unbind_child_resource() * @see iotcon_request_handler_cb() */ -int iotcon_resource_bind_request_handler(iotcon_resource_h resource, iotcon_request_handler_cb cb); +int iotcon_resource_bind_request_handler(iotcon_resource_h resource, + iotcon_request_handler_cb cb); /** * @brief Binds a child resource into the parent resource. @@ -321,6 +331,7 @@ int iotcon_resource_bind_request_handler(iotcon_resource_h resource, iotcon_requ * @retval #IOTCON_ERROR_ALREADY Already done * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_register_resource() * @see iotcon_unregister_resource() @@ -330,7 +341,8 @@ int iotcon_resource_bind_request_handler(iotcon_resource_h resource, iotcon_requ * @see iotcon_resource_unbind_child_resource() * @see iotcon_request_handler_cb() */ -int iotcon_resource_bind_child_resource(iotcon_resource_h parent, iotcon_resource_h child); +int iotcon_resource_bind_child_resource(iotcon_resource_h parent, + iotcon_resource_h child); /** * @brief Unbinds a child resource from the parent resource. @@ -347,6 +359,7 @@ int iotcon_resource_bind_child_resource(iotcon_resource_h parent, iotcon_resourc * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_register_resource() * @see iotcon_unregister_resource() @@ -356,7 +369,8 @@ int iotcon_resource_bind_child_resource(iotcon_resource_h parent, iotcon_resourc * @see iotcon_resource_bind_child_resource() * @see iotcon_request_handler_cb() */ -int iotcon_resource_unbind_child_resource(iotcon_resource_h parent, iotcon_resource_h child); +int iotcon_resource_unbind_child_resource(iotcon_resource_h parent, + iotcon_resource_h child); /** * @brief Register device information in a server. @@ -407,6 +421,7 @@ typedef void (*iotcon_device_info_cb)(const char *device_name, const char *sid, * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post iotcon_device_info_cb() will be called when success on getting device information. * @@ -464,6 +479,7 @@ typedef void (*iotcon_platform_info_cb)(iotcon_platform_info_s *info, void *user * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post iotcon_platform_info_cb() will be called when success on getting device information. * @@ -491,6 +507,7 @@ int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_cb c * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_stop_presence() * @see iotcon_subscribe_presence() @@ -511,6 +528,7 @@ int iotcon_start_presence(unsigned int time_to_live); * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_start_presence() * @see iotcon_subscribe_presence() @@ -558,6 +576,7 @@ typedef void (*iotcon_presence_cb)(int result, unsigned int nonce, * @retval #IOTCON_ERROR_IOTIVITY Iotivity errors * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the resource receive presence, iotcon_presence_cb() will be called. * @@ -584,6 +603,7 @@ int iotcon_subscribe_presence(const char *host_address, const char *resource_typ * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_start_presence() * @see iotcon_stop_presence() @@ -627,6 +647,7 @@ typedef void (*iotcon_found_resource_cb)(iotcon_client_h resource, void *user_da * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the resource is found, iotcon_found_resource_cb() will be called. * @@ -747,6 +768,7 @@ typedef void (*iotcon_on_observe_cb)(iotcon_client_h resource, * @retval #IOTCON_ERROR_IOTIVITY Iotivity errors * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the @a resource receive notification message, iotcon_on_observe_cb() will be called. * @@ -773,6 +795,7 @@ int iotcon_observer_start(iotcon_client_h resource, int observe_type, * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_on_observe_cb() * @see iotcon_observer_start() @@ -796,6 +819,7 @@ int iotcon_observer_stop(iotcon_client_h resource); * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied */ int iotcon_response_send(iotcon_response_h resp); @@ -853,6 +877,7 @@ void iotcon_notimsg_destroy(iotcon_notimsg_h msg); * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_REPRESENTATION Representation error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_on_observe_cb() * @see iotcon_observer_start() @@ -877,6 +902,7 @@ int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus error * @retval #IOTCON_ERROR_SYSTEM System error + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_on_observe_cb() * @see iotcon_observer_start() @@ -925,6 +951,7 @@ typedef void (*iotcon_on_cru_cb)(iotcon_client_h resource, iotcon_representation * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the client receive get response, iotcon_on_cru_cb() will be called. * @@ -954,6 +981,7 @@ int iotcon_get(iotcon_client_h resource, iotcon_query_h query, * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the client receive put response, iotcon_on_cru_cb() will be called. * @@ -961,8 +989,8 @@ int iotcon_get(iotcon_client_h resource, iotcon_query_h query, * @see iotcon_get() * @see iotcon_post() */ -int iotcon_put(iotcon_client_h resource, iotcon_representation_h repr, iotcon_query_h query, - iotcon_on_cru_cb cb, void *user_data); +int iotcon_put(iotcon_client_h resource, iotcon_representation_h repr, + iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data); /** * @brief Posts on a resource @@ -983,6 +1011,7 @@ int iotcon_put(iotcon_client_h resource, iotcon_representation_h repr, iotcon_qu * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the client receive post response, iotcon_on_cru_cb() will be called. * @@ -990,8 +1019,8 @@ int iotcon_put(iotcon_client_h resource, iotcon_representation_h repr, iotcon_qu * @see iotcon_get() * @see iotcon_put() */ -int iotcon_post(iotcon_client_h resource, iotcon_representation_h repr, iotcon_query_h query, - iotcon_on_cru_cb cb, void *user_data); +int iotcon_post(iotcon_client_h resource, iotcon_representation_h repr, + iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data); /** * @brief Specifies the type of function passed to iotcon_delete() @@ -1028,6 +1057,7 @@ typedef void (*iotcon_on_delete_cb)(iotcon_client_h resource, iotcon_options_h o * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter * @retval #IOTCON_ERROR_DBUS Dbus errors * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @post When the client receive delete response, iotcon_on_delete_cb() will be called. * diff --git a/test/crud-test-client.c b/test/crud-test-client.c index 7c9b481..3c7b4c1 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -20,8 +20,10 @@ #include #include "test.h" -static const char* const door_uri_path = "/a/door"; static char *door_resource_sid; +static GList *resource_sid_list; + +static const char* const door_uri_path = "/a/door"; static void _on_observe(iotcon_client_h resource, iotcon_representation_h recv_repr, @@ -42,6 +44,7 @@ static void _on_observe(iotcon_client_h resource, static void _on_delete(iotcon_client_h resource, iotcon_options_h header_options, int response_result, void *user_data) { + int ret; iotcon_client_h door_resource = user_data; RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result @@ -51,15 +54,18 @@ static void _on_delete(iotcon_client_h resource, iotcon_options_h header_options /* delete callback operations */ - iotcon_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL, _on_observe, NULL); + ret = iotcon_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL, _on_observe, + NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_observer_start() Fail(%d)", ret); + iotcon_client_destroy(door_resource); } static void _on_post(iotcon_client_h resource, iotcon_representation_h recv_repr, iotcon_options_h header_options, int response_result, void *user_data) { - int ret; - int ifaces = 0; + int ret, ifaces = 0; iotcon_state_h recv_state; char *host, *created_uri_path; iotcon_resource_types_h types = NULL; @@ -70,14 +76,17 @@ static void _on_post(iotcon_client_h resource, iotcon_representation_h recv_repr "_on_post Response error(%d)", response_result); INFO("POST request was successful"); - iotcon_representation_get_state(recv_repr, &recv_state); - iotcon_state_get_str(recv_state, "createduripath", &created_uri_path); - - if (NULL == created_uri_path) { - ERR("created_uri_path is NULL"); + ret = iotcon_representation_get_state(recv_repr, &recv_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_state() Fail(%d)", ret); return; } + ret = iotcon_state_get_str(recv_state, "createduripath", &created_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_str() Fail(%d)", ret); + return; + } DBG("New resource created : %s", created_uri_path); ret = iotcon_client_get_host(resource, &host); @@ -112,7 +121,13 @@ static void _on_post(iotcon_client_h resource, iotcon_representation_h recv_repr return; } - iotcon_delete(new_door_resource, _on_delete, door_resource); + ret = iotcon_delete(new_door_resource, _on_delete, door_resource); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_delete() Fail(%d)", ret); + iotcon_client_destroy(door_resource); + iotcon_client_destroy(new_door_resource); + return; + } iotcon_client_destroy(new_door_resource); } @@ -134,7 +149,9 @@ static void _on_put(iotcon_client_h resource, iotcon_representation_h recv_repr, } /* send POST request */ - iotcon_post(resource, send_repr, NULL, _on_post, NULL); + ret = iotcon_post(resource, send_repr, NULL, _on_post, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_post() Fail(%d)", ret); iotcon_representation_destroy(send_repr); } @@ -152,8 +169,17 @@ static void _on_get(iotcon_client_h resource, iotcon_representation_h recv_repr, response_result); INFO("GET request was successful"); - iotcon_representation_get_state(recv_repr, &recv_state); - iotcon_state_get_bool(recv_state, "opened", &opened); + ret = iotcon_representation_get_state(recv_repr, &recv_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_state() Fail(%d)", ret); + return; + } + + ret = iotcon_state_get_bool(recv_state, "opened", &opened); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_bool() Fail(%d)", ret); + return; + } ret = iotcon_representation_create(&send_repr); if (IOTCON_ERROR_NONE != ret) { @@ -168,14 +194,29 @@ static void _on_get(iotcon_client_h resource, iotcon_representation_h recv_repr, return; } - iotcon_state_set_bool(send_state, "opened", true); + ret = iotcon_state_set_bool(send_state, "opened", true); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_bool() Fail(%d)", ret); + iotcon_state_destroy(send_state); + iotcon_representation_destroy(send_repr); + return; + } + + ret = iotcon_representation_set_state(send_repr, send_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(send_state); + iotcon_representation_destroy(send_repr); + return; + } - iotcon_representation_set_state(send_repr, send_state); + iotcon_state_destroy(send_state); /* send PUT request */ - iotcon_put(resource, send_repr, NULL, _on_put, NULL); + ret = iotcon_put(resource, send_repr, NULL, _on_put, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_put() Fail(%d)", ret); - iotcon_state_destroy(send_state); iotcon_representation_destroy(send_repr); } @@ -197,15 +238,21 @@ static void _presence_handler(int result, unsigned int nonce, INFO("host_address : %s", host_address); } +static int _sid_compare(const void *a, const void *b) +{ + return strcmp(a, b); +} + static void _found_resource(iotcon_client_h resource, void *user_data) { int ret; - char *resource_sid = NULL; - char *resource_host = NULL; - char *resource_uri_path = NULL; + GList *node; + char *resource_sid; + char *resource_host; + char *resource_uri_path; + int resource_interfaces; iotcon_presence_h presence_handle; - iotcon_resource_types_h resource_types = NULL; - int resource_interfaces = 0; + iotcon_resource_types_h resource_types; if (NULL == resource) return; @@ -228,18 +275,27 @@ static void _found_resource(iotcon_client_h resource, void *user_data) } DBG("[%s] resource server id : %s", resource_uri_path, resource_sid); - if (door_resource_sid && TEST_STR_EQUAL == strcmp(door_resource_sid, resource_sid) - && TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) { + node = g_list_find_custom(resource_sid_list, resource_sid, _sid_compare); + + if (node && TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) { DBG("uri_path \"%s\" already found. skip !", resource_uri_path); return; } door_resource_sid = strdup(resource_sid); + if (NULL == door_resource_sid) { + ERR("strdup(door_resource_sid) Fail"); + return; + } + + resource_sid_list = g_list_append(resource_sid_list, door_resource_sid); /* get the resource host address */ ret = iotcon_client_get_host(resource, &resource_host); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_host() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } DBG("[%s] resource host : %s", resource_uri_path, resource_host); @@ -248,6 +304,8 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ret = iotcon_client_get_interfaces(resource, &resource_interfaces); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_interfaces() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } if (IOTCON_INTERFACE_DEFAULT & resource_interfaces) @@ -263,6 +321,8 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ret = iotcon_client_get_types(resource, &resource_types); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_types() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } @@ -270,6 +330,8 @@ static void _found_resource(iotcon_client_h resource, void *user_data) resource_uri_path); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_types_foreach() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } @@ -282,6 +344,8 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ret = iotcon_query_create(&query); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_query_create() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } @@ -289,31 +353,51 @@ static void _found_resource(iotcon_client_h resource, void *user_data) if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_query_insert() Fail(%d)", ret); iotcon_query_destroy(query); + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); return; } /* send GET Request */ - iotcon_get(resource, query, _on_get, NULL); + ret = iotcon_get(resource, query, _on_get, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_get() Fail(%d)", ret); + iotcon_query_destroy(query); } + + resource_sid_list = g_list_remove(resource_sid_list, door_resource_sid); + free(door_resource_sid); } int main(int argc, char **argv) { FN_CALL; + int ret; GMainLoop *loop; + loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + return -1; + } /* find door typed resources */ - iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.door", &_found_resource, NULL); + ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.door", &_found_resource, + NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_find_resource() Fail(%d)", ret); + iotcon_close(); + return -1; + } g_main_loop_run(loop); g_main_loop_unref(loop); - free(door_resource_sid); + g_list_free_full(resource_sid_list, free); /* iotcon close */ iotcon_close(); diff --git a/test/crud-test-server.c b/test/crud-test-server.c index 93cf9c5..3b35b16 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -26,45 +26,68 @@ typedef struct _door_resource_s { bool state; char *uri_path; char *type; + int ifaces; + int properties; + iotcon_resource_h handle; + iotcon_observers_h observers; iotcon_representation_h repr; } door_resource_s; -static door_resource_s my_door; -static bool resource_created = false; - -static iotcon_observers_h observers = NULL; +static bool resource_created; static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data); -static int _set_door_resource() +static int _set_door_resource(door_resource_s *door) { - my_door.state = false; - my_door.type = strdup("core.door"); - if (NULL == my_door.type) { + int ret; + + door->state = false; + + door->uri_path = strdup("/a/door"); + if (NULL == door->uri_path) { + ERR("strdup(/a/door) Fail"); + return -1; + } + + door->type = strdup("core.door"); + if (NULL == door->type) { ERR("strdup(core.door) Fail"); - return IOTCON_ERROR_OUT_OF_MEMORY; + free(door->uri_path); + return -1; } - my_door.uri_path = strdup("/a/door"); - if (NULL == my_door.uri_path) { - ERR("strdup(/a/door) Fail"); - return IOTCON_ERROR_OUT_OF_MEMORY; + door->ifaces = IOTCON_INTERFACE_DEFAULT; + door->properties = IOTCON_DISCOVERABLE; + + ret = iotcon_observers_create(&door->observers); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_observers_create() Fail"); + free(door->type); + free(door->uri_path); + return -1; } - return IOTCON_ERROR_NONE; + return 0; +} + +static void _free_door_resource(door_resource_s *door) +{ + iotcon_observers_destroy(door->observers); + free(door->type); + free(door->uri_path); } -static void _check_door_state() +static void _check_door_state(door_resource_s door) { - if (false == my_door.state) + if (false == door.state) INFO("[Door] closed."); else INFO("[Door] opened."); } -static iotcon_resource_h _create_door_resource(char *uri_path, - iotcon_interface_e interfaces, iotcon_resource_property_e properties) +static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int ifaces, + int properties, void *user_data) { int ret; iotcon_resource_h handle; @@ -76,7 +99,7 @@ static iotcon_resource_h _create_door_resource(char *uri_path, return NULL; } - ret = iotcon_resource_types_insert(resource_types, my_door.type); + ret = iotcon_resource_types_insert(resource_types, type); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_types_destroy(resource_types); ERR("iotcon_resource_types_insert() Fail(%d)", ret); @@ -84,8 +107,8 @@ static iotcon_resource_h _create_door_resource(char *uri_path, } /* register door resource */ - ret = iotcon_register_resource(uri_path, resource_types, - interfaces, properties, _request_handler, NULL, &handle); + ret = iotcon_register_resource(uri_path, resource_types, ifaces, properties, + _request_handler, user_data, &handle); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_types_destroy(resource_types); ERR("iotcon_register_resource() Fail"); @@ -97,17 +120,34 @@ static iotcon_resource_h _create_door_resource(char *uri_path, return handle; } -static void _send_response(iotcon_response_h response, iotcon_representation_h repr, +static int _send_response(iotcon_response_h response, iotcon_representation_h repr, iotcon_response_result_e result) { - iotcon_response_set_result(response, result); - iotcon_response_set_representation(response, repr); + int ret; + + ret = iotcon_response_set_result(response, result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_set_result() Fail(%d)", ret); + return -1; + } + + ret = iotcon_response_set_representation(response, repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_set_representation() Fail(%d)", ret); + return -1; + } /* send Representation to the client */ - iotcon_response_send(response); + ret = iotcon_response_send(response); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_send() Fail(%d)", ret); + return -1; + } + + return 0; } -static void _request_handler_get(iotcon_response_h response) +static void _request_handler_get(door_resource_s *door, iotcon_response_h response) { int ret; iotcon_representation_h resp_repr; @@ -125,23 +165,48 @@ static void _request_handler_get(iotcon_response_h response) ret = iotcon_state_create(&resp_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return; + } + + ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); return; } - iotcon_representation_set_uri_path(resp_repr, my_door.uri_path); - iotcon_state_set_bool(resp_state, "opened", my_door.state); + ret = iotcon_state_set_bool(resp_state, "opened", door->state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_bool() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); + return; + } + + ret = iotcon_representation_set_state(resp_repr, resp_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); + return; + } - iotcon_representation_set_state(resp_repr, resp_state); + iotcon_state_destroy(resp_state); - _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); } -static void _request_handler_put(iotcon_request_h request, iotcon_response_h response) +static void _request_handler_put(door_resource_s *door, iotcon_request_h request, + iotcon_response_h response) { - bool bval; int ret; + bool bval; iotcon_representation_h req_repr, resp_repr; iotcon_state_h req_state, resp_state;; INFO("PUT request"); @@ -152,12 +217,21 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res return; } - iotcon_representation_get_state(req_repr, &req_state); + ret = iotcon_representation_get_state(req_repr, &req_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_state() Fail(%d)", ret); + return; + } + + ret = iotcon_state_get_bool(req_state, "opened", &bval); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_bool() Fail(%d)", ret); + return; + } - iotcon_state_get_bool(req_state, "opened", &bval); - my_door.state = bval; + door->state = bval; - _check_door_state(); + _check_door_state(*door); ret = iotcon_representation_create(&resp_repr); if (IOTCON_ERROR_NONE != ret) { @@ -165,18 +239,41 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res return; } + ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return; + } + ret = iotcon_state_create(&resp_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return; + } + + ret = iotcon_state_set_bool(resp_state, "opened", door->state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_bool() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); return; } - iotcon_representation_set_uri_path(resp_repr, my_door.uri_path); - iotcon_state_set_bool(resp_state, "opened", my_door.state); + ret = iotcon_representation_set_state(resp_repr, resp_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); + return; + } - iotcon_representation_set_state(resp_repr, resp_state); + iotcon_state_destroy(resp_state); - _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); } @@ -184,11 +281,14 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res static gboolean _notifier(gpointer user_data) { int ret; - iotcon_representation_h repr; + static int i = 0; iotcon_notimsg_h msg; + door_resource_s *door; + iotcon_representation_h repr; - static int i = 0; - if ((5 == i++) || !(observers)) + door = user_data; + + if ((5 == i++) || !(door->observers)) return FALSE; INFO("NOTIFY!"); @@ -201,10 +301,17 @@ static gboolean _notifier(gpointer user_data) ret = iotcon_notimsg_create(repr, IOTCON_INTERFACE_DEFAULT, &msg); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_notimsg_create() Fail(%d)", ret); + iotcon_representation_destroy(repr); return FALSE; } - iotcon_notify_list_of_observers(user_data, msg, observers); + ret = iotcon_notify_list_of_observers(door->handle, msg, door->observers); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_notify_list_of_observers() Fail(%d)", ret); + iotcon_notimsg_destroy(msg); + iotcon_representation_destroy(repr); + return FALSE; + } iotcon_notimsg_destroy(msg); iotcon_representation_destroy(repr); @@ -212,7 +319,7 @@ static gboolean _notifier(gpointer user_data) return TRUE; } -static void _request_handler_post(iotcon_resource_h resource, iotcon_response_h response) +static void _request_handler_post(door_resource_s *door, iotcon_response_h response) { int ret; iotcon_state_h resp_state; @@ -225,8 +332,8 @@ static void _request_handler_post(iotcon_resource_h resource, iotcon_response_h return; } - new_door_handle = _create_door_resource("/a/door1", IOTCON_INTERFACE_DEFAULT, - (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE)); + new_door_handle = _create_door_resource("/a/door1", door->type, + IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL); if (NULL == new_door_handle) { ERR("_create_door_resource() Fail"); return; @@ -243,19 +350,39 @@ static void _request_handler_post(iotcon_resource_h resource, iotcon_response_h ret = iotcon_state_create(&resp_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); return; } - iotcon_state_set_str(resp_state, "createduripath", "/a/door1"); + ret = iotcon_state_set_str(resp_state, "createduripath", "/a/door1"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_str() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); + return; + } + + ret = iotcon_representation_set_state(resp_repr, resp_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(resp_state); + iotcon_representation_destroy(resp_repr); + return; + } - iotcon_representation_set_state(resp_repr, resp_state); + iotcon_state_destroy(resp_state); - _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); + ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); + if (0 != ret) { + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return; + } iotcon_representation_destroy(resp_repr); /* add observe */ - g_timeout_add_seconds(5, _notifier, resource); + g_timeout_add_seconds(5, _notifier, door); } static void _request_handler_delete(iotcon_resource_h resource, @@ -266,24 +393,30 @@ static void _request_handler_delete(iotcon_resource_h resource, iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK; INFO("DELETE request"); - iotcon_unregister_resource(resource); - ret = iotcon_representation_create(&resp_repr); + ret = iotcon_unregister_resource(resource); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); + ERR("iotcon_unregiser_resource() Fail(%d)", ret); return; } result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED; - _send_response(response, resp_repr, result); + ret = iotcon_representation_create(&resp_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return; + } + + ret = _send_response(response, resp_repr, result); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); } static int _query_cb(const char *key, const char *value, void *user_data) { - INFO("key : %s", key); - INFO("value : %s", value); + INFO("key : %s, value : %s", key, value); return IOTCON_FUNC_CONTINUE; } @@ -291,12 +424,15 @@ static int _query_cb(const char *key, const char *value, void *user_data) static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { + door_resource_s *door; iotcon_query_h query; iotcon_response_h response = NULL; int ret, types, observer_id, observer_action; RET_IF(NULL == request); + door = user_data; + ret = iotcon_request_get_query(request, &query); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_query() Fail(%d)", ret); @@ -318,13 +454,13 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques } if (IOTCON_REQUEST_GET & types) - _request_handler_get(response); + _request_handler_get(door, response); else if (IOTCON_REQUEST_PUT & types) - _request_handler_put(request, response); + _request_handler_put(door, request, response); else if (IOTCON_REQUEST_POST & types) - _request_handler_post(resource, response); + _request_handler_post(door, response); else if (IOTCON_REQUEST_DELETE & types) _request_handler_delete(resource, response); @@ -344,14 +480,14 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques ERR("iotcon_request_get_observer_id() Fail(%d)", ret); return; } - ret = iotcon_observers_append(observers, observer_id, &observers); + ret = iotcon_observers_insert(door->observers, observer_id); } else if (IOTCON_OBSERVE_DEREGISTER == observer_action) { ret = iotcon_request_get_observer_id(request, &observer_id); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_observer_id() Fail(%d)", ret); return; } - ret = iotcon_observers_remove(observers, observer_id, &observers); + ret = iotcon_observers_delete(door->observers, observer_id); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_observers_remove() Fail(%d)", ret); return; @@ -378,44 +514,52 @@ static gboolean _presence_timer(gpointer user_data) int main(int argc, char **argv) { FN_CALL; + int ret; GMainLoop *loop; - iotcon_resource_h door_handle; - iotcon_interface_e door_interfaces = IOTCON_INTERFACE_DEFAULT; - iotcon_resource_property_e resource_properties = IOTCON_DISCOVERABLE; - int ret = IOTCON_ERROR_NONE; + door_resource_s my_door = {0}; loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + return -1; + } /* set local door resource */ - ret = _set_door_resource(); - if (IOTCON_ERROR_NONE != ret) { + ret = _set_door_resource(&my_door); + if (0 != ret) { ERR("_set_door_resource() Fail"); + iotcon_close(); return -1; } /* add resource options */ - door_interfaces |= IOTCON_INTERFACE_BATCH; - resource_properties |= IOTCON_OBSERVABLE; + my_door.ifaces |= IOTCON_INTERFACE_BATCH; + my_door.properties |= IOTCON_OBSERVABLE; /* add presence */ g_timeout_add_seconds(10, _presence_timer, NULL); /* create new door resource */ - door_handle = _create_door_resource("/a/door", door_interfaces, resource_properties); - if (NULL == door_handle) { + my_door.handle = _create_door_resource(my_door.uri_path, my_door.type, my_door.ifaces, + my_door.properties, &my_door); + if (NULL == my_door.handle) { ERR("_create_door_resource() Fail"); + _free_door_resource(&my_door); + iotcon_close(); return -1; } - _check_door_state(); + _check_door_state(my_door); g_main_loop_run(loop); g_main_loop_unref(loop); - iotcon_unregister_resource(door_handle); + iotcon_unregister_resource(my_door.handle); + + _free_door_resource(&my_door); /* iotcon close */ iotcon_close(); diff --git a/test/device-test-client.c b/test/device-test-client.c index 9c315d5..b27216c 100644 --- a/test/device-test-client.c +++ b/test/device-test-client.c @@ -46,16 +46,31 @@ static void _get_device_info(const char *device_name, const char *sid, int main() { + int ret; GMainLoop *loop; loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + return -1; + } - iotcon_get_platform_info(IOTCON_MULTICAST_ADDRESS, _get_platform_info, NULL); + ret = iotcon_get_platform_info(IOTCON_MULTICAST_ADDRESS, _get_platform_info, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_get_platform_info() Fail(%d)", ret); + iotcon_close(); + return -1; + } - iotcon_get_device_info(IOTCON_MULTICAST_ADDRESS, _get_device_info, NULL); + ret = iotcon_get_device_info(IOTCON_MULTICAST_ADDRESS, _get_device_info, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_get_device_info() Fail(%d)", ret); + iotcon_close(); + return -1; + } g_main_loop_run(loop); diff --git a/test/device-test-server.c b/test/device-test-server.c index 71cb979..3ff9c73 100644 --- a/test/device-test-server.c +++ b/test/device-test-server.c @@ -15,6 +15,7 @@ */ #include +#include #include #include #include "test.h" @@ -28,7 +29,7 @@ int main() iotcon_platform_info_s *platform_info = calloc(1, sizeof(iotcon_platform_info_s)); if (NULL == platform_info) { - ERR("calloc() Fail(%d)", errno); + ERR("calloc(platform_info) Fail(%d)", errno); return -1; } @@ -49,11 +50,17 @@ int main() loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + free(platform_info); + return -1; + } ret = iotcon_register_platform_info(platform_info); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_register_platform_info() Fail(%d)", ret); + iotcon_close(); free(platform_info); return -1; } @@ -63,6 +70,7 @@ int main() ret = iotcon_register_device_info(device_name); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_register_platform_info() Fail(%d)", ret); + iotcon_close(); return -1; } diff --git a/test/repr-test-client.c b/test/repr-test-client.c index 519c205..81d6a1c 100644 --- a/test/repr-test-client.c +++ b/test/repr-test-client.c @@ -19,8 +19,10 @@ #include #include "test.h" -static const char* const room_uri_path = "/a/room"; static char *room_resource_sid; +static GList *resource_sid_list; + +static const char* const room_uri_path = "/a/room"; static int _get_int_list_cb(int pos, const int value, void *user_data) { @@ -31,24 +33,31 @@ static int _get_int_list_cb(int pos, const int value, void *user_data) static void _on_get(iotcon_representation_h recv_repr, int response_result) { - int i, ret; - bool is_null; - iotcon_list_h list; - const char *uri_path; + int i, ret, int_val; + bool is_null, bool_val; + char *uri_path, *str_val; + iotcon_list_h list_val; iotcon_representation_h child_repr; iotcon_state_h recv_state, child_state; unsigned int key_count, children_count; - RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, - "_on_get Response error(%d)", response_result); + RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)", + response_result); INFO("GET request was successful"); DBG("[ parent representation ]"); - iotcon_representation_get_uri_path(recv_repr, &uri_path); - if (uri_path) - DBG("uri_path : %s", uri_path); + ret = iotcon_representation_get_uri_path(recv_repr, &uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_uri_path() Fail(%d)", ret); + return; + } + DBG("uri_path : %s", uri_path); - iotcon_representation_get_state(recv_repr, &recv_state); + ret = iotcon_representation_get_state(recv_repr, &recv_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_state() Fail(%d)", ret); + return; + } ret = iotcon_state_get_keys_count(recv_state, &key_count); if (IOTCON_ERROR_NONE != ret) { @@ -57,17 +66,32 @@ static void _on_get(iotcon_representation_h recv_repr, int response_result) } if (key_count) { - char *str; - iotcon_state_get_str(recv_state, "name", &str); - if (str) - DBG("name : %s", str); + ret = iotcon_state_get_str(recv_state, "name", &str_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_str() Fail(%d)", ret); + return; + } + DBG("name : %s", str_val); - iotcon_state_get_list(recv_state, "today_temp", &list); + ret = iotcon_state_get_list(recv_state, "today_temp", &list_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_list() Fail(%d)", ret); + return; + } DBG("today's temperature :"); - iotcon_list_foreach_int(list, _get_int_list_cb, NULL); + ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_foreach_int() Fail(%d)", ret); + return; + } ret = iotcon_state_is_null(recv_state, "null value", &is_null); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_is_null() Fail(%d)", ret); + return; + } + if (is_null) DBG("null value is null"); } @@ -80,7 +104,6 @@ static void _on_get(iotcon_representation_h recv_repr, int response_result) for (i = 0; i < children_count; i++) { DBG("[ child representation ]"); - const char *uri_path; ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr); if (IOTCON_ERROR_NONE != ret) { @@ -88,35 +111,47 @@ static void _on_get(iotcon_representation_h recv_repr, int response_result) continue; } - iotcon_representation_get_uri_path(child_repr, &uri_path); - if (NULL == uri_path) + ret = iotcon_representation_get_uri_path(child_repr, &uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_uri_path() Fail(%d)", ret); continue; - + } DBG("uri_path : %s", uri_path); - iotcon_representation_get_state(child_repr, &child_state); + ret = iotcon_representation_get_state(child_repr, &child_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_state() Fail(%d)", ret); + continue; + } + if (TEST_STR_EQUAL == strcmp("/a/light", uri_path)) { ret = iotcon_state_get_keys_count(child_state, &key_count); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_get_keys_count() Fail(%d)", ret); - return; + continue; } if (key_count) { - int brightness; - iotcon_state_get_int(child_state, "brightness", &brightness); - DBG("brightness : %d", brightness); + ret = iotcon_state_get_int(child_state, "brightness", &int_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_int() Fail(%d)", ret); + continue; + } + DBG("brightness : %d", int_val); } } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri_path)) { ret = iotcon_state_get_keys_count(child_state, &key_count); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_get_keys_count() Fail(%d)", ret); - return; + continue; } if (key_count) { - bool bswitch; - iotcon_state_get_bool(child_state, "switch", &bswitch); - DBG("switch : %d", bswitch); + ret = iotcon_state_get_bool(child_state, "switch", &bool_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_get_bool() Fail(%d)", ret); + continue; + } + DBG("switch : %d", bool_val); } } } @@ -142,10 +177,20 @@ static void _on_get_1st(iotcon_client_h resource, iotcon_representation_h recv_r return; } - iotcon_query_insert(query_params, "if", "oic.if.b"); + ret = iotcon_query_insert(query_params, "if", "oic.if.b"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_insert() Fail(%d)", ret); + iotcon_query_destroy(query_params); + return; + } /* send GET request again with BATCH interface */ - iotcon_get(resource, query_params, _on_get_2nd, NULL); + ret = iotcon_get(resource, query_params, _on_get_2nd, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_get() Fail(%d)", ret); + iotcon_query_destroy(query_params); + return; + } iotcon_query_destroy(query_params); } @@ -159,14 +204,20 @@ static int _get_res_type_fn(const char *string, void *user_data) return IOTCON_FUNC_CONTINUE; } +static int _sid_compare(const void *a, const void *b) +{ + return strcmp(a, b); +} + static void _found_resource(iotcon_client_h resource, void *user_data) { int ret; - char *resource_uri_path; + GList *node; + char *resource_sid; char *resource_host; - char *resource_sid = NULL; - iotcon_resource_types_h resource_types = NULL; - int resource_interfaces = 0; + char *resource_uri_path; + int resource_interfaces; + iotcon_resource_types_h resource_types; if (NULL == resource) return; @@ -188,18 +239,27 @@ static void _found_resource(iotcon_client_h resource, void *user_data) } DBG("[%s] resource server id : %s", resource_uri_path, resource_sid); - if (room_resource_sid && TEST_STR_EQUAL == strcmp(room_resource_sid, resource_sid) - && TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) { + node = g_list_find_custom(resource_sid_list, resource_sid, _sid_compare); + + if (node && TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) { DBG("uri_path \"%s\" already found. skip !", resource_uri_path); return; } room_resource_sid = strdup(resource_sid); + if (NULL == room_resource_sid) { + ERR("strdup(room_resource_sid) Fail"); + return; + } + + resource_sid_list = g_list_append(resource_sid_list, room_resource_sid); /* get the resource host address */ ret = iotcon_client_get_host(resource, &resource_host); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_host() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, room_resource_sid); + free(room_resource_sid); return; } DBG("[%s] resource host : %s", resource_uri_path, resource_host); @@ -208,6 +268,8 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ret = iotcon_client_get_interfaces(resource, &resource_interfaces); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_interfaces() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, room_resource_sid); + free(room_resource_sid); return; } if (IOTCON_INTERFACE_DEFAULT & resource_interfaces) @@ -223,31 +285,59 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ret = iotcon_client_get_types(resource, &resource_types); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_types() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, room_resource_sid); + free(room_resource_sid); + return; + } + ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn, + resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_types_foreach() Fail(%d)", ret); + resource_sid_list = g_list_remove(resource_sid_list, room_resource_sid); + free(room_resource_sid); return; } - iotcon_resource_types_foreach(resource_types, _get_res_type_fn, resource_uri_path); if (TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) { /* send GET request */ - iotcon_get(resource, NULL, _on_get_1st, NULL); + ret = iotcon_get(resource, NULL, _on_get_1st, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_get() Fail(%d)", ret); } + + resource_sid_list = g_list_remove(resource_sid_list, room_resource_sid); + free(room_resource_sid); } int main(int argc, char **argv) { FN_CALL; + int ret; GMainLoop *loop; + loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + return -1; + } /* find room typed resources */ - iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.room", &_found_resource, NULL); + ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.room", &_found_resource, + NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_find_resource() Fail(%d)", ret); + iotcon_close(); + return -1; + } g_main_loop_run(loop); g_main_loop_unref(loop); + g_list_free_full(resource_sid_list, free); + /* iotcon close */ iotcon_close(); diff --git a/test/repr-test-server.c b/test/repr-test-server.c index f49f190..f7d6090 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -20,18 +20,37 @@ #include #include "test.h" -static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request, - void *user_data); - -static void _send_response(iotcon_response_h response, iotcon_representation_h repr, - iotcon_interface_e interface) +static int _send_response(iotcon_response_h response, iotcon_representation_h repr, + iotcon_interface_e iface) { - iotcon_response_set_representation(response, repr); - iotcon_response_set_interface(response, interface); - iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK); + int ret; + + ret = iotcon_response_set_representation(response, repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_set_representation() Fail(%d)", ret); + return -1; + } + + ret = iotcon_response_set_interface(response, iface); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_set_interface() Fail(%d)", ret); + return -1; + } + + ret = iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_set_result() Fail(%d)", ret); + return -1; + } /* send Representation to the client */ - iotcon_response_send(response); + ret = iotcon_response_send(response); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_send() Fail(%d)", ret); + return -1; + } + + return 0; } static void _light_request_handler_get(iotcon_response_h response) @@ -48,18 +67,11 @@ static void _light_request_handler_get(iotcon_response_h response) return; } - _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - iotcon_representation_destroy(resp_repr); -} - -static int _query_foreach_cb(const char *key, const char *value, void *user_data) -{ - char **interface_str = user_data; - - if (TEST_STR_EQUAL == strcmp("if", key)) - *interface_str = (char*)value; + ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); - return IOTCON_FUNC_CONTINUE; + iotcon_representation_destroy(resp_repr); } static void _room_request_handler_get(iotcon_request_h request, @@ -73,7 +85,7 @@ static void _room_request_handler_get(iotcon_request_h request, iotcon_query_h query; char *query_str = NULL; - iotcon_interface_e interface; + iotcon_interface_e iface; INFO("GET request - Room"); @@ -92,11 +104,30 @@ static void _room_request_handler_get(iotcon_request_h request, return; } - iotcon_representation_set_uri_path(room_repr, "/a/room"); - iotcon_state_set_str(room_state, "name", "Michael's Room"); + ret = iotcon_representation_set_uri_path(room_repr, "/a/room"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + + ret = iotcon_state_set_str(room_state, "name", "Michael's Room"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_str() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } /* set null */ - iotcon_state_set_null(room_state, "null value"); + ret = iotcon_state_set_null(room_state, "null value"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_null() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } ret = iotcon_list_create(IOTCON_TYPE_INT, &temperature_list); if (IOTCON_ERROR_NONE != ret) { @@ -106,17 +137,65 @@ static void _room_request_handler_get(iotcon_request_h request, return; } - iotcon_list_insert_int(temperature_list, 22, -1); - iotcon_list_insert_int(temperature_list, 23, -1); - iotcon_list_insert_int(temperature_list, 24, -1); - iotcon_list_insert_int(temperature_list, 25, -1); - iotcon_list_insert_int(temperature_list, 26, -1); - iotcon_state_set_list(room_state, "today_temp", temperature_list); + ret = iotcon_list_insert_int(temperature_list, 22, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_insert_int() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } - /* Set a room state into room Representation */ - iotcon_representation_set_state(room_repr, room_state); + ret = iotcon_list_insert_int(temperature_list, 23, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_insert_int() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + + ret = iotcon_list_insert_int(temperature_list, 24, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_insert_int() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + + ret = iotcon_list_insert_int(temperature_list, 25, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_insert_int() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + + ret = iotcon_list_insert_int(temperature_list, 26, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_insert_int() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + + ret = iotcon_state_set_list(room_state, "today_temp", temperature_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_list() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } iotcon_list_destroy(temperature_list); + + /* Set a room state into room Representation */ + ret = iotcon_representation_set_state(room_repr, room_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(room_state); + iotcon_representation_destroy(room_repr); + return; + } + iotcon_state_destroy(room_state); /* create a light Representation */ @@ -127,24 +206,53 @@ static void _room_request_handler_get(iotcon_request_h request, return; } + ret = iotcon_representation_set_uri_path(light_repr, "/a/light"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_representation_destroy(light_repr); + iotcon_representation_destroy(room_repr); + return; + } + /* create a light state */ ret = iotcon_state_create(&light_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); + iotcon_state_destroy(light_state); iotcon_representation_destroy(light_repr); iotcon_representation_destroy(room_repr); return; } - iotcon_representation_set_uri_path(light_repr, "/a/light"); - iotcon_state_set_int(light_state, "brightness", 50); + ret = iotcon_state_set_int(light_state, "brightness", 50); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_int() Fail(%d)", ret); + iotcon_state_destroy(light_state); + iotcon_representation_destroy(light_repr); + iotcon_representation_destroy(room_repr); + return; + } /* Set a light state into light Representation */ - iotcon_representation_set_state(light_repr, light_state); - - iotcon_representation_append_child(room_repr, light_repr); + ret = iotcon_representation_set_state(light_repr, light_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(light_state); + iotcon_representation_destroy(light_repr); + iotcon_representation_destroy(room_repr); + return; + } iotcon_state_destroy(light_state); + + ret = iotcon_representation_append_child(room_repr, light_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_append_child() Fail(%d)", ret); + iotcon_representation_destroy(light_repr); + iotcon_representation_destroy(room_repr); + return; + } + iotcon_representation_destroy(light_repr); /* create a switch Representation */ @@ -155,23 +263,52 @@ static void _room_request_handler_get(iotcon_request_h request, return; } + ret = iotcon_representation_set_uri_path(switch_repr, "/a/switch"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_representation_destroy(switch_repr); + iotcon_representation_destroy(room_repr); + return; + } + /* create a switch state */ ret = iotcon_state_create(&switch_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); iotcon_representation_destroy(switch_repr); + iotcon_representation_destroy(room_repr); return; } - iotcon_representation_set_uri_path(switch_repr, "/a/switch"); - iotcon_state_set_bool(switch_state, "switch", false); + ret = iotcon_state_set_bool(switch_state, "switch", false); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_bool() Fail(%d)", ret); + iotcon_state_destroy(switch_state); + iotcon_representation_destroy(switch_repr); + iotcon_representation_destroy(room_repr); + return; + } /* Set a light state into light Representation */ - iotcon_representation_set_state(switch_repr, switch_state); - iotcon_representation_append_child(room_repr, switch_repr); + ret = iotcon_representation_set_state(switch_repr, switch_state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(switch_state); + iotcon_representation_destroy(switch_repr); + iotcon_representation_destroy(room_repr); + return; + } iotcon_state_destroy(switch_state); + + ret = iotcon_representation_append_child(room_repr, switch_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_append_child() Fail(%d)", ret); + iotcon_representation_destroy(switch_repr); + iotcon_representation_destroy(room_repr); + return; + } + iotcon_representation_destroy(switch_repr); ret = iotcon_request_get_query(request, &query); @@ -180,18 +317,28 @@ static void _room_request_handler_get(iotcon_request_h request, iotcon_representation_destroy(room_repr); return; } - if (query) - iotcon_query_foreach(query, _query_foreach_cb, &query_str); + + if (query) { + ret = iotcon_query_lookup(query, "if", &query_str); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_lookup() Fail(%d)", ret); + iotcon_representation_destroy(room_repr); + return; + } + } if (query_str && (TEST_STR_EQUAL == strcmp("oic.if.b", query_str))) { DBG("operation for BATCH interface"); - interface = IOTCON_INTERFACE_BATCH; + iface = IOTCON_INTERFACE_BATCH; } else { DBG("operation for DEFAULT interface"); - interface = IOTCON_INTERFACE_DEFAULT; + iface = IOTCON_INTERFACE_DEFAULT; } - _send_response(response, room_repr, interface); + ret = _send_response(response, room_repr, iface); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(room_repr); } @@ -210,7 +357,10 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res /* do PUT operation */ - _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); + ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); } @@ -229,9 +379,11 @@ static void _request_handler_post(iotcon_response_h response) /* do POST operation */ - _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - iotcon_representation_destroy(resp_repr); + ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); } static void _request_handler_delete(iotcon_response_h response) @@ -249,9 +401,11 @@ static void _request_handler_delete(iotcon_response_h response) /* do DELETE operation */ - _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - iotcon_representation_destroy(resp_repr); + ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); + if (0 != ret) + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); } static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request, @@ -260,7 +414,6 @@ static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h int ret; int types; iotcon_response_h response; - FN_CALL; RET_IF(NULL == request); @@ -338,36 +491,56 @@ int main(int argc, char **argv) loop = g_main_loop_new(NULL, FALSE); /* iotcon open */ - iotcon_open(); + ret = iotcon_open(); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_open() Fail(%d)", ret); + return -1; + } /* register room resource */ ret = iotcon_resource_types_create(&room_rtypes); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_types_create() Fail(%d)", ret); + iotcon_close(); + return -1; + } + ret = iotcon_resource_types_insert(room_rtypes, "core.room"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_types_insert(%s) Fail(%d)", "core.room", ret); + iotcon_resource_types_destroy(room_rtypes); + iotcon_close(); return -1; } - iotcon_resource_types_insert(room_rtypes, "core.room"); ret = iotcon_register_resource("/a/room", room_rtypes, (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH), (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler, NULL, &room_handle); - if (NULL == room_handle) { - ERR("iotcon_register_resource() Fail"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_register_resource() Fail(%d)", ret); iotcon_resource_types_destroy(room_rtypes); + iotcon_close(); return -1; } + iotcon_resource_types_destroy(room_rtypes); /* register light resource */ ret = iotcon_resource_types_create(&light_rtypes); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_types_create() Fail(%d)", ret); - iotcon_resource_types_destroy(room_rtypes); iotcon_unregister_resource(room_handle); + iotcon_close(); + return -1; + } + ret = iotcon_resource_types_insert(light_rtypes, "core.light"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_types_insert(%s) Fail(%d)", "core.light", ret); + iotcon_resource_types_destroy(light_rtypes); + iotcon_unregister_resource(room_handle); + iotcon_close(); return -1; } - iotcon_resource_types_insert(light_rtypes, "core.light"); ret = iotcon_register_resource("/a/light", light_rtypes, (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH), (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler, @@ -375,24 +548,21 @@ int main(int argc, char **argv) if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_register_resource() Fail"); iotcon_resource_types_destroy(light_rtypes); - iotcon_resource_types_destroy(room_rtypes); iotcon_unregister_resource(room_handle); + iotcon_close(); return -1; } + iotcon_resource_types_destroy(light_rtypes); ret = iotcon_resource_bind_child_resource(room_handle, light_handle); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_bind_child_resource() Fail"); - iotcon_resource_types_destroy(light_rtypes); iotcon_unregister_resource(light_handle); - iotcon_resource_types_destroy(room_rtypes); iotcon_unregister_resource(room_handle); + iotcon_close(); return -1; } - iotcon_resource_types_destroy(light_rtypes); - iotcon_resource_types_destroy(room_rtypes); - g_main_loop_run(loop); g_main_loop_unref(loop);