From: youngman Date: Fri, 24 Jul 2015 11:04:19 +0000 (+0900) Subject: Remove all cpp code X-Git-Tag: submit/tizen/20151130.235229~102 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1b46f0df37f96e590a59952bc72b56cc92c3f86e;p=platform%2Fcore%2Fiot%2Fiotcon.git Remove all cpp code Change-Id: Ie64381f2731294c348ba3072007854db89608e21 Signed-off-by: youngman --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d58b4e..946d8ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,11 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(iotcon) +PROJECT(iotcon C) INCLUDE(FindPkgConfig) SET(EXTRA_CFLAGS "-Wall -Werror-implicit-function-declaration -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fvisibility=hidden") - SET(CLIENT ${PROJECT_NAME}) SET(DAEMON "${PROJECT_NAME}-daemon") SET(DBUS_INTERFACE "org.tizen.${PROJECT_NAME}.dbus") diff --git a/common/ic-common.h b/common/ic-common.h index af41829..d926e05 100644 --- a/common/ic-common.h +++ b/common/ic-common.h @@ -38,10 +38,10 @@ #define IC_DBUS_SIGNAL_PLATFORM "PLATFORM" #define IC_DBUS_SIGNAL_PRESENCE "PRESENCE" -#define IC_INTERFACE_DEFAULT "oic.if.baseline"; -#define IC_INTERFACE_LINK "oic.if.ll"; -#define IC_INTERFACE_BATCH "oic.if.b"; -#define IC_INTERFACE_GROUP "oc.mi.grp"; +#define IC_INTERFACE_DEFAULT "oic.if.baseline" +#define IC_INTERFACE_LINK "oic.if.ll" +#define IC_INTERFACE_BATCH "oic.if.b" +#define IC_INTERFACE_GROUP "oc.mi.grp" #define IC_JSON_KEY_OC "oc" #define IC_JSON_KEY_URI_PATH "href" diff --git a/common/ic-utils.c b/common/ic-utils.c index 7be7660..9701818 100644 --- a/common/ic-utils.c +++ b/common/ic-utils.c @@ -16,6 +16,8 @@ #include #include +#include "iotcon-constant.h" +#include "ic-common.h" #include "ic-log.h" #include "ic-utils.h" @@ -53,3 +55,28 @@ char* ic_utils_dbus_decode_str(char *src) } +int ic_utils_convert_interface_flag(iotcon_interface_e src, char **dest) +{ + switch (src) { + case IOTCON_INTERFACE_DEFAULT: + *dest = IC_INTERFACE_DEFAULT; + break; + case IOTCON_INTERFACE_LINK: + *dest = IC_INTERFACE_LINK; + break; + case IOTCON_INTERFACE_BATCH: + *dest = IC_INTERFACE_BATCH; + break; + case IOTCON_INTERFACE_GROUP: + *dest = IC_INTERFACE_GROUP; + break; + case IOTCON_INTERFACE_NONE: + default: + ERR("Invalid interface(%d)", src); + return IOTCON_ERROR_INVALID_PARAMETER; + } + return IOTCON_ERROR_NONE; +} + + + diff --git a/common/ic-utils.h b/common/ic-utils.h index 423bcaa..3cb76f2 100644 --- a/common/ic-utils.h +++ b/common/ic-utils.h @@ -16,11 +16,14 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_UTILITY_H__ #define __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_UTILITY_H__ +#include "iotcon-constant.h" + #define IC_STR_EQUAL 0 #define IC_STR_NULL "(NULL)" char* ic_utils_strdup(const char *src); const char* ic_utils_dbus_encode_str(const char *src); char* ic_utils_dbus_decode_str(char *src); +int ic_utils_convert_interface_flag(iotcon_interface_e src, char **dest); #endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_UTILITY_H__ */ diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 45af5bd..a31ac9a 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -1,7 +1,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/include) -FILE(GLOB DAEMON_SRCS *.c *.cpp ${CMAKE_SOURCE_DIR}/common/*.c) +FILE(GLOB DAEMON_SRCS *.c ${CMAKE_SOURCE_DIR}/common/*.c) SET(DAEMON_SRCS ${DAEMON_SRCS} ${CMAKE_SOURCE_DIR}/common/ic-dbus.c) SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/ic-dbus.c @@ -12,7 +12,6 @@ INCLUDE_DIRECTORIES(${daemon_pkgs_INCLUDE_DIRS}) LINK_DIRECTORIES(${daemon_pkgs_LIBRARY_DIRS}) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE") SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--hash-style=both -pie") ADD_DEFINITIONS("-DIOTCON_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"") diff --git a/daemon/icd-ioty-repr.cpp b/daemon/icd-ioty-repr.cpp deleted file mode 100644 index ed2c93c..0000000 --- a/daemon/icd-ioty-repr.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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. - */ -#include -#include - -extern "C" { -#include -#include "iotcon-struct.h" -#include "ic-common.h" -#include "ic-utils.h" -#include "icd.h" -#include "icd-ioty-repr.h" -} - -using namespace std; -using namespace OC; - -int icd_ioty_repr_parse_json(const char *repr_json, OCRepresentation &ocRep) -{ - int ret = IOTCON_ERROR_NONE; - MessageContainer info; - - try { - info.setJSONRepresentation(repr_json); - - const vector &reps = info.representations(); - if (0 < reps.size()) { - vector::const_iterator itr = reps.begin(); - vector::const_iterator back = reps.end(); - ocRep = *itr; - ++itr; - - for (; itr != back; ++itr) - ocRep.addChild(*itr); - } else { - ERR("Invalid parameter(%s)", repr_json); - ret = IOTCON_ERROR_INVALID_PARAMETER; - } - } catch (exception &e) { - ERR("setJSONRepresentation() Fail(%s)", e.what()); - ret = IOTCON_ERROR_INVALID_PARAMETER; - } - - return ret; -} - - -int icd_ioty_repr_generate_json(OCRepresentation ocRep, char **json) -{ - RETV_IF(NULL == json, IOTCON_ERROR_INVALID_PARAMETER); - - *json = ic_utils_strdup(ocRep.getJSONRepresentation().c_str()); - if (NULL == *json) { - ERR("ic_utils_strdup() Fail"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - return IOTCON_ERROR_NONE; -} - - -int icd_ioty_repr_generate_gvariant_builder(const OCRepresentation &ocRep, - GVariantBuilder **builder) -{ - int ret; - unsigned int i; - char *repr_json; - OCRepresentation ocChild; - - *builder = g_variant_builder_new(G_VARIANT_TYPE("as")); - - DBG("numberOfAttributes : %d", ocRep.numberOfAttributes()); - ret = icd_ioty_repr_generate_json(ocRep, &repr_json); - if (IOTCON_ERROR_NONE != ret) { - ERR("icd_ioty_repr_generate_json() Fail(%d)", ret); - g_variant_builder_unref(*builder); - return ret; - } - g_variant_builder_add(*builder, "s", repr_json); - free(repr_json); - - vector childList = ocRep.getChildren(); - - for (i = 0; i < childList.size(); i++) { - ocChild = childList.at(i); - ret = icd_ioty_repr_generate_json(ocChild, &repr_json); - if (IOTCON_ERROR_NONE != ret) { - ERR("icd_ioty_repr_generate_json() Fail(%d)", ret); - g_variant_builder_unref(*builder); - return ret; - } - g_variant_builder_add(*builder, "s", repr_json); - free(repr_json); - } - - return IOTCON_ERROR_NONE; -} diff --git a/daemon/icd-ioty-repr.h b/daemon/icd-ioty-repr.h deleted file mode 100644 index 6f67793..0000000 --- a/daemon/icd-ioty-repr.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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_IOTIVITY_REPRESENTATION_H__ -#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_REPRESENTATION_H__ - -#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_REPRESENTATION_H__ */ - diff --git a/daemon/icd-ioty.c b/daemon/icd-ioty.c index f067b8b..35c464a 100644 --- a/daemon/icd-ioty.c +++ b/daemon/icd-ioty.c @@ -28,7 +28,6 @@ #include "iotcon.h" #include "ic-utils.h" #include "icd.h" -#include "icd-ioty-repr.h" #include "icd-ioty.h" #include "icd-ioty-ocprocess.h" @@ -152,39 +151,15 @@ int icd_ioty_unregister_resource(OCResourceHandle resource_handle) } -static int _ioty_convert_interface_flag(iotcon_interface_e src, const char **dest) -{ - switch (src) { - case IOTCON_INTERFACE_DEFAULT: - *dest = IC_INTERFACE_DEFAULT; - break; - case IOTCON_INTERFACE_LINK: - *dest = IC_INTERFACE_LINK; - break; - case IOTCON_INTERFACE_BATCH: - *dest = IC_INTERFACE_BATCH; - break; - case IOTCON_INTERFACE_GROUP: - *dest = IC_INTERFACE_GROUP; - break; - case IOTCON_INTERFACE_NONE: - default: - ERR("Invalid interface(%d)", src); - return IOTCON_ERROR_INVALID_PARAMETER; - } - return IOTCON_ERROR_NONE; -} - - int icd_ioty_bind_interface(OCResourceHandle resourceHandle, iotcon_interface_e iface) { int ret; OCStackResult result; - const char *resource_interface; + char *resource_interface; - ret = _ioty_convert_interface_flag(iface, &resource_interface); + ret = ic_utils_convert_interface_flag(iface, &resource_interface); if (IOTCON_ERROR_NONE != ret) { - ERR("_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret); + ERR("ic_utils_convert_interface_flag(%d) Fail(%d)", iface, ret); return ret; } @@ -199,6 +174,7 @@ int icd_ioty_bind_interface(OCResourceHandle resourceHandle, iotcon_interface_e return IOTCON_ERROR_NONE; } + int icd_ioty_bind_type(OCResourceHandle resource_handle, const char *resource_type) { OCStackResult ret; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 3831ec6..016f7a2 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,7 +1,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common/) INCLUDE_DIRECTORIES(include) -FILE(GLOB CLIENT_SRCS *.c *.cpp ${CMAKE_SOURCE_DIR}/common/*.c) +FILE(GLOB CLIENT_SRCS *.c ${CMAKE_SOURCE_DIR}/common/*.c) SET(CLIENT_SRCS ${CLIENT_SRCS} ${CMAKE_SOURCE_DIR}/common/ic-dbus.c) SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/ic-dbus.c diff --git a/lib/icl-dbus-type.c b/lib/icl-dbus-type.c index d8578eb..a21ed79 100644 --- a/lib/icl-dbus-type.c +++ b/lib/icl-dbus-type.c @@ -21,8 +21,8 @@ #include "iotcon.h" #include "ic-utils.h" #include "icl.h" +#include "icl-resource.h" #include "icl-resource-types.h" -#include "icl-ioty.h" #include "icl-options.h" #include "icl-query.h" #include "icl-request.h" diff --git a/lib/icl-ioty.cpp b/lib/icl-ioty.cpp deleted file mode 100644 index d7d5e4e..0000000 --- a/lib/icl-ioty.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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. - */ -#include - -extern "C" { -#include "iotcon-constant.h" -#include "ic-utils.h" -#include "icl.h" -} - -using namespace std; -using namespace OC; - -static int _icl_ioty_convert_interface_flag(iotcon_interface_e src, string &dest) -{ - switch (src) { - case IOTCON_INTERFACE_GROUP: - dest = GROUP_INTERFACE; - break; - case IOTCON_INTERFACE_BATCH: - dest = BATCH_INTERFACE; - break; - case IOTCON_INTERFACE_LINK: - dest = LINK_INTERFACE; - break; - case IOTCON_INTERFACE_DEFAULT: - dest = DEFAULT_INTERFACE; - break; - case IOTCON_INTERFACE_NONE: - default: - ERR("Invalid interface"); - dest = ""; - return IOTCON_ERROR_INVALID_PARAMETER; - } - return IOTCON_ERROR_NONE; -} - - -extern "C" int icl_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest) -{ - RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER); - - if (IC_STR_EQUAL == strcmp(DEFAULT_INTERFACE.c_str(), src)) { - *dest = IOTCON_INTERFACE_DEFAULT; - } else if (IC_STR_EQUAL == strcmp(LINK_INTERFACE.c_str(), src)) { - *dest = IOTCON_INTERFACE_LINK; - } else if (IC_STR_EQUAL == strcmp(BATCH_INTERFACE.c_str(), src)) { - *dest = IOTCON_INTERFACE_BATCH; - } else if (IC_STR_EQUAL == strcmp(GROUP_INTERFACE.c_str(), src)) { - *dest = IOTCON_INTERFACE_GROUP; - } else { - ERR("Invalid interface"); - *dest = IOTCON_INTERFACE_NONE; - return IOTCON_ERROR_INVALID_PARAMETER; - } - - return IOTCON_ERROR_NONE; -} - -extern "C" int icl_ioty_convert_interface_flag(iotcon_interface_e src, char **dest) -{ - FN_CALL; - int ret; - string iface_str; - - ret = _icl_ioty_convert_interface_flag(src, iface_str); - if (IOTCON_ERROR_NONE != ret) { - ERR("_icd_ioty_convert_interface_flag() Fail(%d)", ret); - *dest = NULL; - return ret; - } - - *dest = ic_utils_strdup(iface_str.c_str()); - if (NULL == *dest) { - ERR("ic_utils_strdup() Fail"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - return IOTCON_ERROR_NONE; -} - diff --git a/lib/icl-ioty.h b/lib/icl-ioty.h deleted file mode 100644 index 1d828ef..0000000 --- a/lib/icl-ioty.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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_IOTIVITY_H__ -#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_H__ - -#include "iotcon.h" -#include "icl-dbus.h" - -struct icl_notify_msg { - int error_code; - iotcon_interface_e iface; - iotcon_repr_h repr; -}; - -struct icl_resource { - char *uri_path; - bool is_observable; - iotcon_resource_types_h types; - int ifaces; - iotcon_request_handler_cb cb; - void *user_data; - unsigned int sub_id; - int handle; - iotcon_resource_h children[IOTCON_CONTAINED_RESOURCES_MAX]; -}; - -int icl_ioty_convert_interface_flag(iotcon_interface_e src, char **dest); -int icl_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest); - -#endif /*__IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_H__*/ diff --git a/lib/icl-repr.c b/lib/icl-repr.c index aa022b0..9a76ef0 100644 --- a/lib/icl-repr.c +++ b/lib/icl-repr.c @@ -25,8 +25,8 @@ #include "iotcon-representation.h" #include "ic-utils.h" #include "icl.h" +#include "icl-resource.h" #include "icl-resource-types.h" -#include "icl-ioty.h" #include "icl-repr-list.h" #include "icl-repr-value.h" #include "icl-repr-obj.h" @@ -298,9 +298,9 @@ static JsonObject* _icl_repr_data_generate_json(iotcon_repr_h cur_repr, for (i = 1; i <= ICL_INTERFACE_MAX; i = i << 1) { if (IOTCON_INTERFACE_NONE == (ifaces & i)) /* this interface not exist */ continue; - ret = icl_ioty_convert_interface_flag((ifaces & i), &iface_str); + ret = ic_utils_convert_interface_flag((ifaces & i), &iface_str); if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_convert_interface_flag(%d) Fail(%d)", i, ret); + ERR("ic_utils_convert_interface_flag(%d) Fail(%d)", i, ret); json_object_unref(repr_obj); json_array_unref(if_array); return NULL; @@ -570,6 +570,26 @@ iotcon_repr_h icl_repr_create_repr(const char *json_string) } +static int _icl_repr_convert_interface_string(const char *src, iotcon_interface_e *dest) +{ + RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER); + + if (IC_STR_EQUAL == strcmp(IC_INTERFACE_DEFAULT, src)) { + *dest = IOTCON_INTERFACE_DEFAULT; + } else if (IC_STR_EQUAL == strcmp(IC_INTERFACE_LINK, src)) { + *dest = IOTCON_INTERFACE_LINK; + } else if (IC_STR_EQUAL == strcmp(IC_INTERFACE_BATCH, src)) { + *dest = IOTCON_INTERFACE_BATCH; + } else { + ERR("Invalid Interface"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + return IOTCON_ERROR_NONE; +} + + int icl_repr_parse_resource_property(JsonObject *prop_obj, iotcon_resource_types_h *types, int *ifaces) { @@ -609,7 +629,7 @@ int icl_repr_parse_resource_property(JsonObject *prop_obj, if_count = json_array_get_length(iface_array); for (if_index = 0; if_index < if_count; if_index++) { const char *iface_str = json_array_get_string_element(iface_array, if_index); - ret = icl_ioty_convert_interface_string(iface_str, &iface_flag); + ret = _icl_repr_convert_interface_string(iface_str, &iface_flag); if (IOTCON_ERROR_NONE != ret) { ERR("icl_ioty_convert_interface_string() Fail(%d)", ret); if (res_types) diff --git a/lib/icl-resource.c b/lib/icl-resource.c new file mode 100644 index 0000000..0cd5504 --- /dev/null +++ b/lib/icl-resource.c @@ -0,0 +1,664 @@ +/* + * 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iotcon.h" +#include "ic-utils.h" +#include "icl-resource-types.h" +#include "icl-resource.h" +#include "icl-request.h" +#include "icl-repr.h" +#include "icl-dbus.h" +#include "icl-dbus-type.h" +#include "icl.h" + +/** + * @brief global context + */ +static bool icl_is_init = false; + +API int iotcon_initialize() +{ + FN_CALL; + int ret; + + RETVM_IF(true == icl_is_init, IOTCON_ERROR_ALREADY, "already initialized"); + +#if !GLIB_CHECK_VERSION(2, 35, 0) + g_type_init(); +#endif + + ret = icl_dbus_start(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_dbus_start() Fail(%d)", ret); + return ret; + } + + icl_is_init = true; + + return IOTCON_ERROR_NONE; +} + + +API void iotcon_deinitialize() +{ + FN_CALL; + + RETM_IF(false == icl_is_init, "Not initialized"); + + icl_dbus_stop(); + + icl_is_init = false; +} + + +API int iotcon_add_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data) +{ + int ret; + + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + ret = icl_dbus_add_connection_changed_cb(cb, user_data); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_dbus_add_connection_changed_cb() Fail(%d)", ret); + return ret; + } + + return ret; +} + + +API int iotcon_remove_connection_changed_cb(iotcon_connection_changed_cb cb, + void *user_data) +{ + int ret; + + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + ret = icl_dbus_remove_connection_changed_cb(cb, user_data); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_dbus_remove_connection_changed_cb() Fail(%d)", ret); + return ret; + } + + return ret; +} + + +static void _icl_request_handler(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + GVariantIter *options; + unsigned short option_id; + char *option_data; + GVariantIter *query; + char *key = NULL; + char *value = NULL; + char *repr_json; + int request_handle; + int resource_handle; + struct icl_resource_request request = {0}; + iotcon_resource_h resource = user_data; + iotcon_request_handler_cb cb = resource->cb; + + g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)", + &request.types, + &options, + &query, + &request.observation_info.action, + &request.observation_info.observer_id, + &repr_json, + &request_handle, + &resource_handle); + + if (g_variant_iter_n_children(options)) { + request.header_options = iotcon_options_new(); + while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) + iotcon_options_insert(request.header_options, option_id, option_data); + } + g_variant_iter_free(options); + + if (g_variant_iter_n_children(query)) { + request.query = iotcon_query_new(); + while (g_variant_iter_loop(query, "(&s&s)", &key, &value)) + iotcon_query_insert(request.query, key, value); + } + g_variant_iter_free(query); + + request.request_handle = GINT_TO_POINTER(request_handle); + request.resource_handle = GINT_TO_POINTER(resource_handle); + + if (ic_utils_dbus_decode_str(repr_json)) { + request.repr = icl_repr_create_repr(repr_json); + if (NULL == request.repr) { + ERR("icl_repr_create_repr() Fail"); + if (request.query) + iotcon_query_free(request.query); + if (request.header_options) + iotcon_options_free(request.header_options); + return; + } + } + + /* TODO remove request.uri */ + request.uri_path = "temp_uri_path"; + + if (cb) + cb(resource, &request, resource->user_data); + + /* To avoid unnecessary ERR log (repr could be NULL) */ + if (request.repr) + iotcon_repr_free(request.repr); + if (request.query) + iotcon_query_free(request.query); + if (request.header_options) + iotcon_options_free(request.header_options); +} + + +static void _icl_resource_conn_cleanup(iotcon_resource_h resource) +{ + resource->sub_id = 0; + resource->handle = 0; +} + + +/* The length of uri_path should be less than or equal to 36. */ +API iotcon_resource_h iotcon_register_resource(const char *uri_path, + iotcon_resource_types_h res_types, + int ifaces, + uint8_t properties, + iotcon_request_handler_cb cb, + void *user_data) +{ + FN_CALL; + int signal_number; + unsigned int sub_id; + GError *error = NULL; + const gchar **types; + char sig_name[IC_DBUS_SIGNAL_LENGTH]; + iotcon_resource_h resource; + + RETV_IF(NULL == icl_dbus_get_object(), NULL); + RETV_IF(NULL == uri_path, NULL); + RETVM_IF(IOTCON_URI_PATH_LENGTH_MAX < strlen(uri_path), NULL, "Invalid uri_path(%s)", + uri_path); + RETV_IF(NULL == res_types, NULL); + RETV_IF(NULL == cb, NULL); + + resource = calloc(1, sizeof(struct icl_resource)); + if (NULL == resource) { + ERR("calloc() Fail(%d)", errno); + return NULL; + } + + types = icl_dbus_resource_types_to_array(res_types); + if (NULL == types) { + ERR("icl_dbus_resource_types_to_array() Fail"); + free(resource); + return NULL; + } + + signal_number = icl_dbus_generate_signal_number(); + + ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces, + properties, signal_number, &(resource->handle), NULL, &error); + if (error) { + ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message); + g_error_free(error); + free(types); + free(resource); + return NULL; + } + free(types); + + if (0 == resource->handle) { + ERR("iotcon-daemon Fail"); + free(resource); + return NULL; + } + + resource->cb = cb; + resource->user_data = user_data; + + resource->types = icl_resource_types_ref(res_types); + resource->uri_path = ic_utils_strdup(uri_path); + resource->ifaces = ifaces; + resource->is_observable = properties & IOTCON_OBSERVABLE; + + snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER, + signal_number); + + sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup, + _icl_request_handler); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + free(resource); + return NULL; + } + + resource->sub_id = sub_id; + + return resource; +} + + +API int iotcon_unregister_resource(iotcon_resource_h resource) +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == resource->sub_id) { + WARN("Invalid Resource handle"); + iotcon_resource_types_free(resource->types); + free(resource->uri_path); + free(resource); + return IOTCON_ERROR_NONE; + } + + ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + icl_dbus_unsubscribe_signal(resource->sub_id); + + iotcon_resource_types_free(resource->types); + free(resource->uri_path); + free(resource); + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface) +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle, + iface, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return ret; +} + + +API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type) +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER); + if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) { + ERR("Invalid resource_type(%s)", resource_type); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return ret; +} + + +API int iotcon_bind_request_handler(iotcon_resource_h resource, + iotcon_request_handler_cb cb) +{ + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + WARN("Request handler is changed"); + resource->cb = cb; + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child) +{ + FN_CALL; + int ret; + int i; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == parent->sub_id) { + ERR("Invalid Resource handle(parent)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + if (0 == child->sub_id) { + ERR("Invalid Resource handle(child)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (child == parent->children[i]) { + ERR("Child resource was already bound to parent resource."); + return IOTCON_ERROR_ALREADY; + } + } + + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (NULL == parent->children[i]) { + ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle, + child->handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + parent->children[i] = child; + + return IOTCON_ERROR_NONE; + } + } + + ERR("There is no slot to bind a child resource"); + return IOTCON_ERROR_OUT_OF_MEMORY; +} + + +API int iotcon_unbind_resource(iotcon_resource_h parent, iotcon_resource_h child) +{ + int ret; + int i; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == parent->sub_id) { + ERR("Invalid Resource handle(parent)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + if (0 == child->sub_id) { + ERR("Invalid Resource handle(child)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle, + child->handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (child == parent->children[i]) + parent->children[i] = NULL; + } + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number) +{ + int i; + + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER); + + *number = 0; + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (resource->children[i]) + *number += 1; + } + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index, + iotcon_resource_h *child) +{ + RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); + if ((index < 0) || (IOTCON_CONTAINED_RESOURCES_MAX <= index)) { + ERR("Invalid index(%d)", index); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + *child = parent->children[index]; + + return IOTCON_ERROR_NONE; +} + + +/* The content of the resource should not be freed by user. */ +API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path) +{ + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER); + + *uri_path = resource->uri_path; + + return IOTCON_ERROR_NONE; +} + + +/* The content of the resource should not be freed by user. */ +API int iotcon_resource_get_types(iotcon_resource_h resource, + iotcon_resource_types_h *types) +{ + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER); + + *types = resource->types; + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces) +{ + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER); + + *ifaces = resource->ifaces; + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable) +{ + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER); + + *observable = resource->is_observable; + + return IOTCON_ERROR_NONE; +} + + +API iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e iface) +{ + iotcon_notimsg_h msg; + + RETV_IF(NULL == repr, NULL); + + msg = calloc(1, sizeof(struct icl_notify_msg)); + if (NULL == msg) { + ERR("calloc() Fail(%d)", errno); + return NULL; + } + + msg->repr = repr; + icl_repr_inc_ref_count(msg->repr); + msg->iface = iface; + msg->error_code = 200; + + return msg; +} + + +API void iotcon_notimsg_free(iotcon_notimsg_h msg) +{ + RET_IF(NULL == msg); + + iotcon_repr_free(msg->repr); + free(msg); +} + + +API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg, + iotcon_observers_h observers) +{ + int ret; + GError *error = NULL; + GVariant *noti_msg; + GVariant *obs; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + noti_msg = icl_dbus_notimsg_to_gvariant(msg); + if (NULL == noti_msg) { + ERR("icl_dbus_notimsg_to_gvariant() Fail"); + return IOTCON_ERROR_REPRESENTATION; + } + obs = icl_dbus_observers_to_gvariant(observers); + + ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle, + noti_msg, obs, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(obs); + g_variant_unref(noti_msg); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_notify_all(iotcon_resource_h resource) +{ + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL, + &error); + if (error) { + ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; +} diff --git a/lib/icl-resource.h b/lib/icl-resource.h new file mode 100644 index 0000000..36c04e5 --- /dev/null +++ b/lib/icl-resource.h @@ -0,0 +1,40 @@ +/* + * 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_RESOURCE_H__ +#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_H__ + +#include "iotcon.h" +#include "icl-dbus.h" + +struct icl_notify_msg { + int error_code; + iotcon_interface_e iface; + iotcon_repr_h repr; +}; + +struct icl_resource { + char *uri_path; + bool is_observable; + iotcon_resource_types_h types; + int ifaces; + iotcon_request_handler_cb cb; + void *user_data; + unsigned int sub_id; + int handle; + iotcon_resource_h children[IOTCON_CONTAINED_RESOURCES_MAX]; +}; + +#endif /*__IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_H__*/ diff --git a/lib/icl-response.c b/lib/icl-response.c index e480965..0a5c70f 100644 --- a/lib/icl-response.c +++ b/lib/icl-response.c @@ -22,7 +22,7 @@ #include "icl.h" #include "icl-dbus.h" #include "icl-dbus-type.h" -#include "icl-ioty.h" +#include "icl-resource.h" #include "icl-repr.h" #include "icl-options.h" #include "icl-request.h" diff --git a/lib/icl.c b/lib/icl.c deleted file mode 100644 index ac45abf..0000000 --- a/lib/icl.c +++ /dev/null @@ -1,664 +0,0 @@ -/* - * 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. - */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "iotcon.h" -#include "ic-utils.h" -#include "icl-resource-types.h" -#include "icl-request.h" -#include "icl-ioty.h" -#include "icl-repr.h" -#include "icl-dbus.h" -#include "icl-dbus-type.h" -#include "icl.h" - -/** - * @brief global context - */ -static bool icl_is_init = false; - -API int iotcon_initialize() -{ - FN_CALL; - int ret; - - RETVM_IF(true == icl_is_init, IOTCON_ERROR_ALREADY, "already initialized"); - -#if !GLIB_CHECK_VERSION(2, 35, 0) - g_type_init(); -#endif - - ret = icl_dbus_start(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_start() Fail(%d)", ret); - return ret; - } - - icl_is_init = true; - - return IOTCON_ERROR_NONE; -} - - -API void iotcon_deinitialize() -{ - FN_CALL; - - RETM_IF(false == icl_is_init, "Not initialized"); - - icl_dbus_stop(); - - icl_is_init = false; -} - - -API int iotcon_add_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data) -{ - int ret; - - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_add_connection_changed_cb(cb, user_data); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_add_connection_changed_cb() Fail(%d)", ret); - return ret; - } - - return ret; -} - - -API int iotcon_remove_connection_changed_cb(iotcon_connection_changed_cb cb, - void *user_data) -{ - int ret; - - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_remove_connection_changed_cb(cb, user_data); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_remove_connection_changed_cb() Fail(%d)", ret); - return ret; - } - - return ret; -} - - -static void _icl_request_handler(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - GVariantIter *options; - unsigned short option_id; - char *option_data; - GVariantIter *query; - char *key = NULL; - char *value = NULL; - char *repr_json; - int request_handle; - int resource_handle; - struct icl_resource_request request = {0}; - iotcon_resource_h resource = user_data; - iotcon_request_handler_cb cb = resource->cb; - - g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)", - &request.types, - &options, - &query, - &request.observation_info.action, - &request.observation_info.observer_id, - &repr_json, - &request_handle, - &resource_handle); - - if (g_variant_iter_n_children(options)) { - request.header_options = iotcon_options_new(); - while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) - iotcon_options_insert(request.header_options, option_id, option_data); - } - g_variant_iter_free(options); - - if (g_variant_iter_n_children(query)) { - request.query = iotcon_query_new(); - while (g_variant_iter_loop(query, "(&s&s)", &key, &value)) - iotcon_query_insert(request.query, key, value); - } - g_variant_iter_free(query); - - request.request_handle = GINT_TO_POINTER(request_handle); - request.resource_handle = GINT_TO_POINTER(resource_handle); - - if (ic_utils_dbus_decode_str(repr_json)) { - request.repr = icl_repr_create_repr(repr_json); - if (NULL == request.repr) { - ERR("icl_repr_create_repr() Fail"); - if (request.query) - iotcon_query_free(request.query); - if (request.header_options) - iotcon_options_free(request.header_options); - return; - } - } - - /* TODO remove request.uri */ - request.uri_path = "temp_uri_path"; - - if (cb) - cb(resource, &request, resource->user_data); - - /* To avoid unnecessary ERR log (repr could be NULL) */ - if (request.repr) - iotcon_repr_free(request.repr); - if (request.query) - iotcon_query_free(request.query); - if (request.header_options) - iotcon_options_free(request.header_options); -} - - -static void _icl_resource_conn_cleanup(iotcon_resource_h resource) -{ - resource->sub_id = 0; - resource->handle = 0; -} - - -/* The length of uri_path should be less than or equal to 36. */ -API iotcon_resource_h iotcon_register_resource(const char *uri_path, - iotcon_resource_types_h res_types, - int ifaces, - uint8_t properties, - iotcon_request_handler_cb cb, - void *user_data) -{ - FN_CALL; - int signal_number; - unsigned int sub_id; - GError *error = NULL; - const gchar **types; - char sig_name[IC_DBUS_SIGNAL_LENGTH]; - iotcon_resource_h resource; - - RETV_IF(NULL == icl_dbus_get_object(), NULL); - RETV_IF(NULL == uri_path, NULL); - RETVM_IF(IOTCON_URI_PATH_LENGTH_MAX < strlen(uri_path), NULL, "Invalid uri_path(%s)", - uri_path); - RETV_IF(NULL == res_types, NULL); - RETV_IF(NULL == cb, NULL); - - resource = calloc(1, sizeof(struct icl_resource)); - if (NULL == resource) { - ERR("calloc() Fail(%d)", errno); - return NULL; - } - - types = icl_dbus_resource_types_to_array(res_types); - if (NULL == types) { - ERR("icl_dbus_resource_types_to_array() Fail"); - free(resource); - return NULL; - } - - signal_number = icl_dbus_generate_signal_number(); - - ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces, - properties, signal_number, &(resource->handle), NULL, &error); - if (error) { - ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message); - g_error_free(error); - free(types); - free(resource); - return NULL; - } - free(types); - - if (0 == resource->handle) { - ERR("iotcon-daemon Fail"); - free(resource); - return NULL; - } - - resource->cb = cb; - resource->user_data = user_data; - - resource->types = icl_resource_types_ref(res_types); - resource->uri_path = ic_utils_strdup(uri_path); - resource->ifaces = ifaces; - resource->is_observable = properties & IOTCON_OBSERVABLE; - - snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER, - signal_number); - - sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup, - _icl_request_handler); - if (0 == sub_id) { - ERR("icl_dbus_subscribe_signal() Fail"); - free(resource); - return NULL; - } - - resource->sub_id = sub_id; - - return resource; -} - - -API int iotcon_unregister_resource(iotcon_resource_h resource) -{ - FN_CALL; - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - - if (0 == resource->sub_id) { - WARN("Invalid Resource handle"); - iotcon_resource_types_free(resource->types); - free(resource->uri_path); - free(resource); - return IOTCON_ERROR_NONE; - } - - ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, - &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - icl_dbus_unsubscribe_signal(resource->sub_id); - - iotcon_resource_types_free(resource->types); - free(resource->uri_path); - free(resource); - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface) -{ - FN_CALL; - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - if (0 == resource->sub_id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle, - iface, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - return ret; -} - - -API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type) -{ - FN_CALL; - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER); - if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) { - ERR("Invalid resource_type(%s)", resource_type); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - if (0 == resource->sub_id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type, - &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - return ret; -} - - -API int iotcon_bind_request_handler(iotcon_resource_h resource, - iotcon_request_handler_cb cb) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - WARN("Request handler is changed"); - resource->cb = cb; - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child) -{ - FN_CALL; - int ret; - int i; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER); - - if (0 == parent->sub_id) { - ERR("Invalid Resource handle(parent)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - if (0 == child->sub_id) { - ERR("Invalid Resource handle(child)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { - if (child == parent->children[i]) { - ERR("Child resource was already bound to parent resource."); - return IOTCON_ERROR_ALREADY; - } - } - - for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { - if (NULL == parent->children[i]) { - ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle, - child->handle, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - parent->children[i] = child; - - return IOTCON_ERROR_NONE; - } - } - - ERR("There is no slot to bind a child resource"); - return IOTCON_ERROR_OUT_OF_MEMORY; -} - - -API int iotcon_unbind_resource(iotcon_resource_h parent, iotcon_resource_h child) -{ - int ret; - int i; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); - - if (0 == parent->sub_id) { - ERR("Invalid Resource handle(parent)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - if (0 == child->sub_id) { - ERR("Invalid Resource handle(child)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle, - child->handle, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { - if (child == parent->children[i]) - parent->children[i] = NULL; - } - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number) -{ - int i; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER); - - *number = 0; - for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { - if (resource->children[i]) - *number += 1; - } - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index, - iotcon_resource_h *child) -{ - RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); - if ((index < 0) || (IOTCON_CONTAINED_RESOURCES_MAX <= index)) { - ERR("Invalid index(%d)", index); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - *child = parent->children[index]; - - return IOTCON_ERROR_NONE; -} - - -/* The content of the resource should not be freed by user. */ -API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER); - - *uri_path = resource->uri_path; - - return IOTCON_ERROR_NONE; -} - - -/* The content of the resource should not be freed by user. */ -API int iotcon_resource_get_types(iotcon_resource_h resource, - iotcon_resource_types_h *types) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER); - - *types = resource->types; - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER); - - *ifaces = resource->ifaces; - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER); - - *observable = resource->is_observable; - - return IOTCON_ERROR_NONE; -} - - -API iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e iface) -{ - iotcon_notimsg_h msg; - - RETV_IF(NULL == repr, NULL); - - msg = calloc(1, sizeof(struct icl_notify_msg)); - if (NULL == msg) { - ERR("calloc() Fail(%d)", errno); - return NULL; - } - - msg->repr = repr; - icl_repr_inc_ref_count(msg->repr); - msg->iface = iface; - msg->error_code = 200; - - return msg; -} - - -API void iotcon_notimsg_free(iotcon_notimsg_h msg) -{ - RET_IF(NULL == msg); - - iotcon_repr_free(msg->repr); - free(msg); -} - - -API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg, - iotcon_observers_h observers) -{ - int ret; - GError *error = NULL; - GVariant *noti_msg; - GVariant *obs; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); - - if (0 == resource->sub_id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - noti_msg = icl_dbus_notimsg_to_gvariant(msg); - if (NULL == noti_msg) { - ERR("icl_dbus_notimsg_to_gvariant() Fail"); - return IOTCON_ERROR_REPRESENTATION; - } - obs = icl_dbus_observers_to_gvariant(observers); - - ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle, - noti_msg, obs, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(obs); - g_variant_unref(noti_msg); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_notify_all(iotcon_resource_h resource) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - if (0 == resource->sub_id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL, - &error); - if (error) { - ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon-daemon Fail(%d)", ret); - return icl_dbus_convert_daemon_error(ret); - } - - return IOTCON_ERROR_NONE; -}