INCLUDE(FindPkgConfig)
-SET(EXTRA_CFLAGS "-Wall -Werror-implicit-function-declaration -fvisibility=hidden")
+SET(EXTRA_CFLAGS "-Wall -Werror-implicit-function-declaration -fvisibility=hidden -fPIE")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
-SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--hash-style=both")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fvisibility=hidden")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--hash-style=both -pie")
SET(CLIENT ${PROJECT_NAME})
SET(DAEMON "${PROJECT_NAME}-daemon")
SET(DBUS_INTERFACE "org.tizen.${PROJECT_NAME}")
ADD_SUBDIRECTORY(lib)
-#ADD_SUBDIRECTORY(daemon)
+ADD_SUBDIRECTORY(daemon)
ADD_SUBDIRECTORY(test)
--- /dev/null
+/*
+ * 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_INTERNAL_COMMON_H__
+#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__
+
+#include "iotcon-errors.h"
+
+#ifndef IOTCON_DBUS_INTERFACE
+#define IOTCON_DBUS_INTERFACE "org.tizen.IoTCon"
+#warning "IOTCON_DBUS_INTERFACE is redefined"
+#endif
+
+#define IOTCON_DBUS_OBJPATH "/org/tizen/IoTCon/GDBus"
+
+#define IOTCON_DBUS_METHOD1 "subscribe"
+#define IOTCON_DBUS_METHOD2 "subscribe"
+#define IOTCON_DBUS_METHOD3 "subscribe"
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__ */
--- /dev/null
+/*
+ * 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_INTERNAL_LOG_H__
+#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__
+
+#define IC_LOG_RED "\033[0;31m"
+#define IC_LOG_GREEN "\033[0;32m"
+#define IC_LOG_BROWN "\033[0;33m"
+#define IC_LOG_BLUE "\033[0;34m"
+#define IC_LOG_END "\033[0;m"
+
+#undef _DBG
+#undef _INFO
+#undef _WARN
+#undef _ERR
+
+#undef DBG
+#undef INFO
+#undef WARN
+#undef ERR
+
+#define TIZEN_DEBUG_ENABLE
+#define LOG_TAG "IOTCON"
+#include <dlog.h>
+
+#ifdef IC_DAEMON
+
+#define _DBG(fmt, arg...) SLOGD(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
+#define _INFO(fmt, arg...) SLOGI(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
+#define _WARN(fmt, arg...) SLOGW(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
+#define _ERR(fmt, arg...) SLOGE(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
+
+#else /* IC_DAEMON */
+
+#define _DBG(fmt, arg...) SLOGD(fmt, ##arg)
+#define _INFO(fmt, arg...) SLOGI(fmt, ##arg)
+#define _WARN(fmt, arg...) SLOGW(fmt, ##arg)
+#define _ERR(fmt, arg...) SLOGE(fmt, ##arg)
+
+#endif /* IC_DAEMON */
+
+#if 0
+#define _DBG(fmt, arg...) \
+ printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
+#define _INFO(fmt, arg...) \
+ printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
+#define _WARN(fmt, arg...) \
+ printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
+#define _ERR(fmt, arg...) \
+ printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
+#endif
+
+#define IC_DEBUGGING
+
+#ifdef IC_DEBUGGING
+
+#define FN_CALL _INFO(">>>>>>>> called")
+#define FN_END _INFO("<<<<<<<< ended")
+#define DBG(fmt, arg...) _DBG(fmt, ##arg)
+#define WARN(fmt, arg...) _WARN(IC_LOG_BROWN fmt IC_LOG_END, ##arg)
+#define ERR(fmt, arg...) _ERR(IC_LOG_RED fmt IC_LOG_END, ##arg)
+#define INFO(fmt, arg...) _INFO(IC_LOG_BLUE fmt IC_LOG_END, ##arg)
+#define SECURE_DBG(fmt, arg...) SECURE_SLOGD(fmt, ##arg)
+#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg)
+
+#else /* IC_DEBUGGING */
+
+#define FN_CALL
+#define FN_END
+#define DBG(fmt, arg...)
+#define WARN(fmt, arg...)
+#define ERR(fmt, arg...) _ERR(fmt, ##arg)
+#define INFO(fmt, arg...)
+#define SECURE_DBG(fmt, arg...)
+#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg)
+
+#endif /* IC_DEBUGGING */
+
+#define RET_IF(expr) \
+ do { \
+ if (expr) { \
+ ERR("(%s)", #expr); \
+ return; \
+ }\
+ } while(0)
+
+#define RETV_IF(expr, val) \
+ do {\
+ if (expr) { \
+ ERR("(%s)", #expr); \
+ return (val); \
+ } \
+ } while(0)
+
+#define RETM_IF(expr, fmt, arg...) \
+ do {\
+ if (expr) { \
+ ERR(fmt, ##arg); \
+ return; \
+ }\
+ } while(0)
+
+#define RETVM_IF(expr, val, fmt, arg...) \
+ do {\
+ if (expr) { \
+ ERR(fmt, ##arg); \
+ return (val); \
+ } \
+ } while(0)
+
+#define ERR_IF(expr) \
+ do { \
+ if (expr) { \
+ ERR("(%s)", #expr); \
+ } \
+ } while (0)
+
+#define WARN_IF(expr) \
+ do { \
+ if (expr) { \
+ WARN("(%s)", #expr); \
+ } \
+ } while (0)
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__ */
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common/)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/include)
SET(OCLIB "oc")
SET(OCLOGGER "oc_logger")
INCLUDE_DIRECTORIES(${daemon_pkgs_INCLUDE_DIRS})
LINK_DIRECTORIES(${daemon_pkgs_LIBRARY_DIRS})
-ADD_DEFINITIONS("-DENOTICE_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"")
+ADD_DEFINITIONS("-DIOTCON_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"")
ADD_EXECUTABLE(${DAEMON} ${DAEMON_SRCS})
TARGET_LINK_LIBRARIES(${DAEMON} ${daemon_pkgs_LIBRARIES} ${OCLIB} ${OCLOGGER}
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <gio/gio.h>
+
+#include "icd.h"
+#include "icd-dbus.h"
+
+static GDBusConnection *icd_dbus_conn;
+
+int icd_dbus_publish(const char *key, const char *data)
+{
+ gboolean ret;
+ GError *error = NULL;
+ int res = 0;
+ char *path = NULL;
+
+ ret = g_dbus_connection_emit_signal(icd_dbus_conn,
+ NULL, /* destination bus name */
+ "/",
+ IOTCON_DBUS_INTERFACE,
+ key,
+ g_variant_new("(s)", data),
+ &error);
+ if (FALSE == ret)
+ {
+ ERR("g_dbus_connection_emit_signal() Fail(%s)", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ if (FALSE == g_dbus_connection_flush_sync(icd_dbus_conn, NULL, &error))
+ {
+ ERR("g_dbus_connection_flush_sync() Fail(%s)", error->message);
+ g_error_free(error);
+ }
+
+ if (path)
+ free(path);
+
+ return 0;
+}
+
+static void _dbus_handle_method_call(GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ const gchar *key = NULL;
+
+ if (0 == g_strcmp0(method_name, IOTCON_DBUS_METHOD1)) {
+ g_variant_get(parameters, "(&s)", &key);
+ if (NULL == key) {
+ ERR("key is NULL");
+ // TODO: handle error
+ }
+
+ //icd_handler_subscribe(key);
+
+ // TODO: handle disconnect without unsubscribe
+
+ g_dbus_method_invocation_return_value(invocation, NULL);
+ }
+ else if (0 == g_strcmp0(method_name, IOTCON_DBUS_METHOD2)) {
+ g_variant_get(parameters, "(&s)", &key);
+ if (NULL == key) {
+ ERR("key is NULL");
+ // TODO: handle error
+ }
+
+ //icd_handler_unsubscribe(key);
+
+ g_dbus_method_invocation_return_value(invocation, NULL);
+ }
+ else if (0 == g_strcmp0(method_name, IOTCON_DBUS_METHOD3)) {
+ const gchar *data = NULL;
+
+ g_variant_get(parameters, "(&s&s)", &key, &data);
+ if (NULL == key) {
+ ERR("key is NULL");
+ // TODO: handle error
+ }
+
+ //icd_handler_publish(key, data);
+
+ g_dbus_method_invocation_return_value(invocation, NULL);
+ }
+ else {
+ g_dbus_method_invocation_return_error(invocation,
+ G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD,
+ "Method %s is not implemented on interface %s", method_name, interface_name);
+ }
+}
+
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+ _dbus_handle_method_call,
+ NULL,
+ NULL
+};
+
+
+static void _dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name,
+ gpointer user_data)
+{
+ guint registration_id;
+ GError *error = NULL;
+ GDBusNodeInfo *introspection_data = user_data;
+
+ FN_CALL;
+
+ icd_dbus_conn = conn;
+
+ registration_id = g_dbus_connection_register_object(conn,
+ IOTCON_DBUS_OBJPATH,
+ introspection_data->interfaces[0],
+ &interface_vtable,
+ NULL,/* user_data */
+ NULL,/* user_data_free_func */
+ &error);
+ if (0 == registration_id)
+ {
+ ERR("g_dbus_connection_register_object() Fail(%s)", error->message);
+ g_error_free(error);
+ }
+}
+
+static void _dbus_on_name_lost(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ DBG("Lost the name %s", name);
+}
+
+static void _dbus_on_name_acquired(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ DBG("Acquired the name %s", name);
+}
+
+unsigned int icd_dbus_init()
+{
+ guint id;
+ GError *error = NULL;
+ GDBusNodeInfo *introspection_data = NULL;
+
+ const gchar introspection_xml[] =
+ "<node>"
+ " <interface name='"IOTCON_DBUS_INTERFACE"'>"
+ " <method name='"IOTCON_DBUS_METHOD1"'>"
+ " <arg type='s' name='key' direction='in'/>"
+ " </method>"
+ " <method name='"IOTCON_DBUS_METHOD2"'>"
+ " <arg type='s' name='key' direction='in'/>"
+ " </method>"
+ " <method name='"IOTCON_DBUS_METHOD3"'>"
+ " <arg type='s' name='key' direction='in'/>"
+ " <arg type='s' name='data' direction='in'/>"
+ " </method>"
+ " </interface>"
+ "</node>";
+
+ introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
+ if (NULL == introspection_data)
+ {
+ ERR("g_dbus_node_info_new_for_xml() Fail(%s)", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ IOTCON_DBUS_INTERFACE,
+ G_BUS_NAME_OWNER_FLAGS_REPLACE,
+ _dbus_on_bus_acquired,
+ _dbus_on_name_acquired,
+ _dbus_on_name_lost,
+ introspection_data,
+ (GDestroyNotify)g_dbus_node_info_unref);
+ if (0 == id)
+ {
+ ERR("g_bus_own_name() Fail");
+ return 0;
+ }
+
+ return id;
+}
+
+void icd_dbus_deinit(unsigned int id)
+{
+ g_bus_unown_name(id);
+}
+
--- /dev/null
+/*
+ * 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_DAEMON_DBUS_H__
+#define __IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__
+
+int icd_dbus_publish(const char *key, const char *data);
+unsigned int icd_dbus_init();
+void icd_dbus_deinit(unsigned int id);
+
+
+#endif //__IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__
--- /dev/null
+/*
+ * 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 <gio/gio.h>
+
+#include "icd.h"
+#include "icd-dbus.h"
+
+
+int main(int argc, char **argv)
+{
+ guint id;
+ GMainLoop *loop;
+
+#if !GLIB_CHECK_VERSION(2,35,0)
+ g_type_init();
+#endif
+
+ loop = g_main_loop_new(NULL, FALSE);
+
+ id = icd_dbus_init();
+
+ g_main_loop_run(loop);
+
+ icd_dbus_deinit(id);
+ g_main_loop_unref(loop);
+
+ return 0;
+}
--- /dev/null
+/*
+ * 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_DAEMON_H__
+#define __IOT_CONNECTIVITY_MANAGER_DAEMON_H__
+
+#include "ic-common.h"
+
+#define IC_DAEMON
+#include "ic-log.h"
+
+
+#endif //__IOT_CONNECTIVITY_MANAGER_DAEMON_H__
--- /dev/null
+[D-BUS Service]
+Name=@DBUS_INTERFACE@
+Exec=@BIN_INSTALL_DIR@/@DAEMON@
\ No newline at end of file
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common/)
INCLUDE_DIRECTORIES(include)
FILE(GLOB CLIENT_SRCS *.c *.cpp)
INCLUDE_DIRECTORIES(${client_pkgs_INCLUDE_DIRS})
LINK_DIRECTORIES(${client_pkgs_LIBRARY_DIRS})
-ADD_DEFINITIONS("-DENOTICE_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"")
+ADD_DEFINITIONS("-DIOTCON_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"")
ADD_LIBRARY(${CLIENT} SHARED ${CLIENT_SRCS})
-TARGET_LINK_LIBRARIES(${CLIENT}
- ${client_pkgs_LIBRARIES}
+TARGET_LINK_LIBRARIES(${CLIENT} ${client_pkgs_LIBRARIES}
oc octbstack coap oc_logger_core oc_logger pthread)
SET_TARGET_PROPERTIES(${CLIENT} PROPERTIES VERSION ${FULLVER} SOVERSION ${MAJORVER})
INSTALL(TARGETS ${CLIENT} DESTINATION ${LIB_INSTALL_DIR})
SET(PREFIX ${CMAKE_INSTALL_PREFIX})
CONFIGURE_FILE(${CLIENT}.pc.in ${CLIENT}.pc @ONLY)
-INSTALL(FILES ${CLIENT}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
+INSTALL(FILES ${CLIENT}.pc DESTINATION lib/pkgconfig)
FILE(GLOB CLIENT_HEADER include/*.h)
INSTALL(FILES ${CLIENT_HEADER} DESTINATION ${INCLUDE_INSTALL_DIR}/${CLIENT})
+++ /dev/null
-/*
- * 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 <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon-struct.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-ioty.h"
-#include "ic-options.h"
-#include "ic-resource-types.h"
-#include "ic-client.h"
-
-/* host address should begin with "coap://"
- * The length of resource_type should be less than or equal to 61.
- * If resource_type is NULL, then All resources in host are discovered. */
-API int iotcon_find_resource(const char *host_addr, const char *resource_type,
- iotcon_found_resource_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == host_addr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
- if (resource_type && (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) {
- ERR("The length of resource_type(%s) is invalid", resource_type);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- ret = ic_ioty_find_resource(host_addr, resource_type, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_find_resource() Fail(%d)", ret);
-
- return ret;
-}
-
-
-/* If you know the information of resource, then you can make a proxy of the resource. */
-API iotcon_client_h iotcon_client_new(const char *host, const char *uri,
- bool is_observable, iotcon_resource_types_h resource_types, int resource_ifs)
-{
- FN_CALL;
- iotcon_client_h resource = NULL;
-
- RETV_IF(NULL == host, NULL);
- RETV_IF(NULL == uri, NULL);
- RETV_IF(NULL == resource_types, NULL);
-
- resource = calloc(1, sizeof(struct ic_remote_resource));
- if (NULL == resource) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- resource->host = ic_utils_strdup(host);
- resource->uri = ic_utils_strdup(uri);
- resource->is_observable = is_observable;
- resource->types = ic_resource_types_ref(resource_types);
- resource->ifaces = resource_ifs;
-
- return resource;
-}
-
-
-API void iotcon_client_free(iotcon_client_h resource)
-{
- FN_CALL;
-
- RET_IF(NULL == resource);
-
- free(resource->uri);
- free(resource->host);
-
- /* null COULD be allowed */
- if (resource->header_options)
- ic_options_free(resource->header_options);
- iotcon_resource_types_free(resource->types);
- free(resource);
-}
-
-
-API iotcon_client_h iotcon_client_clone(iotcon_client_h resource)
-{
- iotcon_client_h clone;
-
- RETV_IF(NULL == resource, NULL);
-
- clone = iotcon_client_new(resource->host,
- resource->uri,
- resource->is_observable,
- iotcon_resource_types_clone(resource->types),
- resource->ifaces);
- if (NULL == clone) {
- ERR("iotcon_client_new() Fail");
- return clone;
- }
-
- clone->observe_handle = resource->observe_handle;
-
- return clone;
-}
-
-
-/* The content of the resource should not be freed by user. */
-API int iotcon_client_get_uri(iotcon_client_h resource, char **uri)
-{
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
-
- *uri = resource->uri;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the resource should not be freed by user. */
-API int iotcon_client_get_host(iotcon_client_h resource, char **host)
-{
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER);
-
- *host = resource->host;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the resource should not be freed by user. */
-API int iotcon_client_get_types(iotcon_client_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_client_get_interfaces(iotcon_client_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_client_is_observable(iotcon_client_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;
-}
-
-
-/* if header_options is NULL, then client's header_options is unset */
-API int iotcon_client_set_options(iotcon_client_h resource,
- iotcon_options_h header_options)
-{
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
-
- if (resource->header_options)
- iotcon_options_free(resource->header_options);
-
- if (NULL == header_options) {
- resource->header_options = NULL;
- return IOTCON_ERROR_NONE;
- }
-
- if (true == header_options->has_parent)
- resource->header_options = ic_options_ref(header_options);
- else
- resource->header_options = header_options;
- if (NULL == resource->header_options) {
- ERR("header_options is NULL");
- return IOTCON_ERROR_NO_DATA;
- }
-
- resource->header_options->has_parent = true;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_get(iotcon_client_h resource, iotcon_query_h query,
- iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_get(resource, query, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_get() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_put(iotcon_client_h resource, iotcon_repr_h repr,
- iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_put(resource, repr, query, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_put() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_post(iotcon_client_h resource, iotcon_repr_h repr,
- iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_post(resource, repr, query, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_post() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_delete_res(resource, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_delete_res() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_observer_start(iotcon_client_h resource,
- iotcon_observe_type_e observe_type,
- iotcon_query_h query,
- iotcon_on_observe_cb cb,
- void *user_data)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_observe(resource, observe_type, query, cb, user_data);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_observe() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_observer_stop(iotcon_client_h resource)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_cancel_observe(resource);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_cancel_observe() Fail(%d)", ret);
-
- return ret;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_CLIENT_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_CLIENT_H__
-
-#include "iotcon-struct.h"
-#include "ic-options.h"
-
-typedef void* iotcon_observe_h;
-
-struct ic_remote_resource {
- char *uri;
- char *host;
- bool is_observable;
- bool is_collection;
- iotcon_options_h header_options;
- iotcon_resource_types_h types;
- int ifaces;
- iotcon_observe_h observe_handle;
-};
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_CLIENT_H__ */
+++ /dev/null
-/*
- * 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_INTERNAL_COMMON_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__
-
-#include "iotcon-errors.h"
-#include "ic-log.h"
-
-#ifdef API
-#undef API
-#endif
-#define API __attribute__((visibility("default")))
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon.h"
-#include "ic-common.h"
-#include "ic-ioty.h"
-
-/* The length of manufacturer_name should be less than and equal to 16.
- * The length of manufacturer_url should be less than and equal to 32. */
-API int iotcon_register_device_info(iotcon_device_info_s device_info)
-{
- int ret;
-
- if (device_info.manuf_name
- && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(device_info.manuf_name))) {
- ERR("The length of manufacturer_name(%s) is invalid.", device_info.manuf_name);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- if (device_info.manuf_url
- && (IOTCON_MANUFACTURER_URL_LENGTH_MAX < strlen(device_info.manuf_url))) {
- ERR("The length of manufacturer_url(%s) is invalid.", device_info.manuf_url);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
-
- ret = ic_ioty_register_device_info(device_info);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_register_device_info() Fail(%d)", ret);
-
- return ret;
-}
-
-
-/* host_address should begin with "coap://" */
-API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb,
- void *user_data)
-{
- FN_CALL;
- int ret = IOTCON_ERROR_NONE;
-
- RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_get_device_info(host_address, cb, user_data);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_get_device_info() Fail(%d)", ret);
- return ret;
- }
-
- return IOTCON_ERROR_NONE;
-}
+++ /dev/null
-/*
- * 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 <OCApi.h>
-#include <OCPlatform.h>
-
-extern "C" {
-#include "iotcon-struct.h"
-#include "ic.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-repr.h"
-#include "ic-ioty-repr.h"
-}
-
-using namespace OC;
-using namespace std;
-
-static iotcon_repr_h _ic_ioty_repr_create_repr(const OCRepresentation& ocRep)
-{
- FN_CALL;
-
- string jsonStr = ocRep.getJSONRepresentation();
- iotcon_repr_h repr = ic_repr_parse_json(jsonStr.c_str());
-
- string uriStr = ocRep.getUri();
- if (!uriStr.empty())
- iotcon_repr_set_uri(repr, uriStr.c_str());
-
- return repr;
-}
-
-static iotcon_repr_h _ic_ioty_repr_create_parent(const OCRepresentation& ocRep)
-{
- FN_CALL;
- return _ic_ioty_repr_create_repr(ocRep);
-}
-
-static iotcon_repr_h _ic_ioty_repr_create_child(const OCRepresentation& ocRep)
-{
- FN_CALL;
- return _ic_ioty_repr_create_repr(ocRep);
-}
-
-
-/*
- * A general input : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}},
- * {"href":"/a/child","rep":{"string":"World","double_val":5.7},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]}
- */
-iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep)
-{
- FN_CALL;
- unsigned int i = 0;
- OCRepresentation ocChild;
-
- iotcon_repr_h repr_parent = _ic_ioty_repr_create_parent(ocRep);
- if (NULL == repr_parent) {
- ERR("_ic_ioty_repr_create_parent() Fail");
- iotcon_repr_free(repr_parent);
- return NULL;
- }
-
- vector<OCRepresentation> childList = ocRep.getChildren();
-
- for (i = 0; i < childList.size(); i++) {
- ocChild = childList.at(i);
- iotcon_repr_h repr_child = _ic_ioty_repr_create_child(ocChild);
- if (NULL == repr_child) {
- ERR("_ic_ioty_repr_create_child() Fail");
- /* free parent because adding child is failed.
- * this func also will free children */
- iotcon_repr_free(repr_parent);
- return NULL;
- }
-
- repr_parent->children = g_list_append(repr_parent->children, repr_child);
- }
-
- return repr_parent;
-}
-
-int ic_ioty_repr_parse(iotcon_repr_h repr, OCRepresentation &ocRep)
-{
- FN_CALL;
-
- int ret = IOTCON_ERROR_NONE;
- MessageContainer info;
-
- /* TODO: It's better that iotcon_repr_h is changed to OCRepresentation at once. */
- char *repr_json = ic_repr_generate_json(repr, false);
-
- try {
- info.setJSONRepresentation(repr_json);
-
- const vector<OCRepresentation> &reps = info.representations();
- if (0 < reps.size()) {
- vector<OCRepresentation>::const_iterator itr = reps.begin();
- vector<OCRepresentation>::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;
- }
-
- free(repr_json);
- return ret;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_IOTIVITY_REPRESENTATION_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_REPRESENTATION_H__
-
-#include <OCRepresentation.h>
-#include "iotcon-struct.h"
-
-void ic_ioty_repr_found_device_cb(const OC::OCRepresentation& ocRep);
-iotcon_repr_h ic_ioty_repr_generate_repr(const OC::OCRepresentation& ocRep);
-int ic_ioty_repr_parse(iotcon_repr_h repr, OC::OCRepresentation &ocRep);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_REPRESENTATION_H__ */
-
+++ /dev/null
-/*
- * 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 <stdbool.h>
-#include <stdint.h>
-#include <glib.h>
-#include <OCApi.h>
-#include <OCPlatform.h>
-
-extern "C" {
-#include "iotcon-struct.h"
-#include "ic.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-client.h"
-#include "ic-request.h"
-#include "ic-response.h"
-#include "ic-resource-types.h"
-#include "ic-repr.h"
-#include "ic-ioty-repr.h"
-#include "ic-ioty.h"
-}
-
-#define IC_UNICAST_RESOURCE_DISCOVERY ":5683/oc/core"
-#define IC_MULTICAST_RESOURCE_DISCOVERY "/oc/core"
-#define IC_DEVICE_DISCOVERY "/oc/core/d"
-
-using namespace std;
-using namespace OC;
-using namespace OCPlatform;
-
-struct resource_handle {
- OCResource::Ptr ocResource;
-};
-
-namespace icIotivityHandler {
-
- class presenceObject
- {
- private:
- iotcon_presence_cb presence_handler;
- void *cb_data;
-
- public:
- presenceObject(iotcon_presence_cb user_cb, void *user_data)
- {
- presence_handler = user_cb;
- cb_data = user_data;
- }
-
- void presenceHandler(OCStackResult result, const unsigned int nonce,
- const string& hostAddress)
- {
- int res;
-
- switch (result) {
- case OC_STACK_OK:
- res = IOTCON_PRESENCE_OK;
- break;
- case OC_STACK_PRESENCE_STOPPED:
- res = IOTCON_PRESENCE_STOPPED;
- break;
- case OC_STACK_PRESENCE_TIMEOUT:
- res = IOTCON_PRESENCE_TIMEOUT;
- break;
- case OC_STACK_ERROR:
- default:
- ERR("subscribePresence() Fail(%d)", result);
- res = IOTCON_PRESENCE_ERROR;
- }
-
- if (presence_handler)
- presence_handler(res, nonce, hostAddress.c_str(), cb_data);
- }
- };
-
- class findObject
- {
- private:
- iotcon_found_resource_cb found_resource;
- void *cb_data;
-
- public:
- findObject(iotcon_found_resource_cb user_cb, void *user_data)
- {
- found_resource = user_cb;
- cb_data = user_data;
- }
-
- void foundResource(shared_ptr<OCResource> resource)
- {
- struct ic_remote_resource resource_s = {0};
- resource_s.types = NULL;
-
- vector<string> resource_types = resource->getResourceTypes();
- if (0 < resource_types.size()) {
- resource_s.types = iotcon_resource_types_new();
- if (NULL == resource_s.types) {
- ERR("iotcon_resource_types_new() Fail");
- return;
- }
-
- for (string &resource_type : resource_types)
- iotcon_resource_types_insert(resource_s.types, resource_type.c_str());
- }
-
- resource_s.uri = ic_utils_strdup(resource->uri().c_str());
- resource_s.host = ic_utils_strdup(resource->host().c_str());
- resource_s.is_observable = resource->isObservable();
-
- vector<string> resource_interfaces = resource->getResourceInterfaces();
- for (string &resource_interface : resource_interfaces) {
- if (IC_STR_EQUAL == resource_interface.compare(DEFAULT_INTERFACE))
- resource_s.ifaces |= IOTCON_INTERFACE_DEFAULT;
-
- if (IC_STR_EQUAL == resource_interface.compare(BATCH_INTERFACE))
- resource_s.ifaces |= IOTCON_INTERFACE_BATCH;
-
- if (IC_STR_EQUAL == resource_interface.compare(LINK_INTERFACE))
- resource_s.ifaces |= IOTCON_INTERFACE_LINK;
-
- if (IC_STR_EQUAL == resource_interface.compare(GROUP_INTERFACE))
- resource_s.ifaces |= IOTCON_INTERFACE_GROUP;
- }
-
- if (found_resource)
- found_resource(&resource_s, cb_data);
-
- free(resource_s.uri);
- free(resource_s.host);
- iotcon_resource_types_free(resource_s.types);
- }
- };
-
- class getObject
- {
- private:
- iotcon_on_cru_cb on_get;
- void *cb_data;
-
- public:
- getObject(iotcon_on_cru_cb user_cb, void *user_data)
- {
- on_get = user_cb;
- cb_data = user_data;
- }
-
- void onGet(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res;
- iotcon_options_h options;
- iotcon_repr_h repr = NULL;
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- }
- else {
- ERR("get() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = iotcon_options_new();
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- iotcon_options_insert(options, option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- repr = ic_ioty_repr_generate_repr(ocRep);
-
- if (on_get)
- on_get(options, repr, res, cb_data);
-
- iotcon_repr_free(repr);
- iotcon_options_free(options);
- }
- };
-
- class putObject
- {
- private:
- iotcon_on_cru_cb on_put;
- void *cb_data;
-
- public:
- putObject(iotcon_on_cru_cb user_cb, void *user_data)
- {
- on_put = user_cb;
- cb_data = user_data;
- }
-
- void onPut(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res;
- iotcon_options_h options;
- iotcon_repr_h repr = NULL;
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- }
- else {
- ERR("put() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = iotcon_options_new();
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- iotcon_options_insert(options, option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- repr = ic_ioty_repr_generate_repr(ocRep);
-
- if (on_put)
- on_put(options, repr, res, cb_data);
-
- iotcon_repr_free(repr);
- iotcon_options_free(options);
- }
- };
-
- class postObject
- {
- private:
- iotcon_on_cru_cb on_post;
- void *cb_data;
-
- public:
- postObject(iotcon_on_cru_cb user_cb, void *user_data)
- {
- on_post = user_cb;
- cb_data = user_data;
- }
-
- void onPost(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res;
- iotcon_options_h options;
- iotcon_repr_h repr = NULL;
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- }
- else if (OC_STACK_RESOURCE_CREATED == eCode) {
- res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
- }
- else {
- ERR("post() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = iotcon_options_new();
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- iotcon_options_insert(options, option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- repr = ic_ioty_repr_generate_repr(ocRep);
-
- if (on_post)
- on_post(options, repr, res, cb_data);
-
- iotcon_repr_free(repr);
- iotcon_options_free(options);
- }
- };
-
- class deleteObject
- {
- private:
- iotcon_on_delete_cb on_delete;
- void *cb_data;
-
- public:
- deleteObject(iotcon_on_delete_cb user_cb, void *user_data)
- {
- on_delete = user_cb;
- cb_data = user_data;
- }
-
- void onDelete(const HeaderOptions& headerOptions, const int eCode)
- {
- int res;
- iotcon_options_h options;
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- }
- else if (OC_STACK_RESOURCE_DELETED == eCode) {
- res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
- }
- else {
- ERR("deleteResource() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = iotcon_options_new();
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- iotcon_options_insert(options, option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- if (on_delete)
- on_delete(options, res, cb_data);
-
- iotcon_options_free(options);
- }
- };
-
- class observeObject
- {
- private:
- iotcon_on_observe_cb on_observe;
- void *cb_data;
-
- public:
- observeObject(iotcon_on_observe_cb user_cb, void *user_data)
- {
- on_observe = user_cb;
- cb_data = user_data;
- }
-
- void onObserve(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode, const int sequenceNumber)
- {
- FN_CALL;
- int res;
- iotcon_options_h options;
- iotcon_repr_h repr = NULL;
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- }
- else {
- ERR("observe() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = iotcon_options_new();
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- iotcon_options_insert(options, option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- repr = ic_ioty_repr_generate_repr(ocRep);
-
- if (on_observe)
- on_observe(options, repr, res, sequenceNumber, cb_data);
-
- iotcon_repr_free(repr);
- iotcon_options_free(options);
- }
- };
-
- class deviceObject
- {
- private:
- iotcon_device_info_cb found_cb;
- void *cb_data;
-
- public:
- deviceObject(iotcon_device_info_cb user_cb, void *user_data)
- {
- found_cb = user_cb;
- cb_data = user_data;
- }
-
- void receivedDeviceInfo(const OCRepresentation& ocRep)
- {
- iotcon_device_info_s info = {0};
- string readbuf;
-
- if (ocRep.getValue("ct", readbuf))
- info.content_type = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mndt", readbuf))
- info.date_of_manufacture = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("dn", readbuf))
- info.name = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("di", readbuf))
- info.uuid = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnfv", readbuf))
- info.firmware_ver = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("hn", readbuf))
- info.host_name = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnmn", readbuf))
- info.manuf_name = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnml", readbuf))
- info.manuf_url = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnmo", readbuf))
- info.model_number = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnpv", readbuf))
- info.platform_ver = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("mnsl", readbuf))
- info.support_url = ic_utils_strdup(readbuf.c_str());
- if (ocRep.getValue("icv", readbuf))
- info.version = ic_utils_strdup(readbuf.c_str());
-
- if (found_cb)
- found_cb(info, cb_data);
-
- free(info.name);
- free(info.host_name);
- free(info.uuid);
- free(info.content_type);
- free(info.version);
- free(info.manuf_name);
- free(info.manuf_url);
- free(info.model_number);
- free(info.date_of_manufacture);
- free(info.platform_ver);
- free(info.firmware_ver);
- free(info.support_url);
- }
- };
-}
-
-extern "C" void ic_ioty_config(const char *addr, unsigned short port)
-{
- PlatformConfig cfg {
- ServiceType::InProc,
- ModeType::Both,
- string(addr),
- port,
- QualityOfService::HighQos
- };
- Configure(cfg);
- DBG("Created a platform");
-}
-
-static OCEntityHandlerResult _ic_ioty_request_handler(
- shared_ptr<OCResourceRequest> request)
-{
- FN_CALL;
- const char *request_type = NULL;
- HeaderOptions headerOptions;
- QueryParamsMap queryParams;
- iotcon_resource_h temp_res = NULL;
- struct ic_resource_request request_s = {0};
-
- temp_res = ic_get_resource_handler_data(request->getResourceHandle());
- if (NULL == temp_res) {
- ERR("No Resource Handler");
- return OC_EH_ERROR;
- }
-
- queryParams = request->getQueryParameters();
- if (0 < queryParams.size()) {
- request_s.query = iotcon_query_new();
- if (NULL == request_s.query) {
- ERR("iotcon_query_new() Fail");
- return OC_EH_ERROR;
- }
-
- for (auto it : queryParams) {
- DBG("key = %s value = %s", it.first.c_str(), it.second.c_str());
- iotcon_query_insert(request_s.query, it.first.c_str(), it.second.c_str());
- }
- }
-
- headerOptions = request->getHeaderOptions();
- if (0 < headerOptions.size()) {
- request_s.header_options = iotcon_options_new();
- if (NULL == request_s.header_options) {
- ERR("iotcon_options_new() Fail");
- if (request_s.query)
- iotcon_query_free(request_s.query);
- return OC_EH_ERROR;
- }
-
- for (auto it : headerOptions) {
- DBG("OptionID = %d, OptionData = %s",
- it.getOptionID(), it.getOptionData().c_str());
- iotcon_options_insert(request_s.header_options, it.getOptionID(),
- it.getOptionData().c_str());
- }
- }
-
- OCRepresentation ocRep = request->getResourceRepresentation();
- if (0 < ocRep.numberOfAttributes()) {
- DBG("numberOfAttributes : %d", ocRep.numberOfAttributes());
- request_s.repr = ic_ioty_repr_generate_repr(ocRep);
- if (NULL == request_s.repr) {
- ERR("request_s.repr is NULL");
- if (request_s.header_options)
- iotcon_options_free(request_s.header_options);
- if (request_s.query)
- iotcon_query_free(request_s.query);
- return OC_EH_ERROR;
- }
- }
-
- if (RequestFlag & request->getRequestHandlerFlag()) {
- request_type = ic_utils_strdup(request->getRequestType().c_str());
- if (NULL == request_type) {
- ERR("request_type is NULL");
- if (request_s.repr)
- iotcon_repr_free(request_s.repr);
- if (request_s.header_options)
- iotcon_options_free(request_s.header_options);
- if (request_s.query)
- iotcon_query_free(request_s.query);
- return OC_EH_ERROR;
- }
-
- if (IC_STR_EQUAL == strcmp("GET", request_type))
- request_s.types = IOTCON_REQUEST_GET;
- else if (IC_STR_EQUAL == strcmp("PUT", request_type))
- request_s.types = IOTCON_REQUEST_PUT;
- else if (IC_STR_EQUAL == strcmp("POST", request_type))
- request_s.types = IOTCON_REQUEST_POST;
- else if (IC_STR_EQUAL == strcmp("DELETE", request_type))
- request_s.types = IOTCON_REQUEST_DELETE;
- }
-
- if (ObserverFlag & request->getRequestHandlerFlag())
- request_s.types |= IOTCON_REQUEST_OBSERVE;
-
- request_s.uri = ic_utils_strdup(request->getResourceUri().c_str());
- if (NULL == request_s.uri) {
- ERR("ic_utils_strdup() Fail");
- if (request_s.repr)
- iotcon_repr_free(request_s.repr);
- if (request_s.header_options)
- iotcon_options_free(request_s.header_options);
- if (request_s.query)
- iotcon_query_free(request_s.query);
- return OC_EH_ERROR;
- }
-
- request_s.request_handle = (iotcon_request_h)request->getRequestHandle();
- request_s.resource_handle = (iotcon_client_h)request->getResourceHandle();
-
- ObservationInfo observationInfo = request->getObservationInfo();
- request_s.observation_info.action = (iotcon_observe_action_e)observationInfo.action;
- request_s.observation_info.observer_id = observationInfo.obsId;
- DBG("obs_info.obsId=%d", observationInfo.obsId);
-
- /* call handler_cb */
- if (temp_res->cb) {
- temp_res->cb(&request_s, temp_res->user_data);
- }
- else {
- WARN("temp_res->request_handler_cb is null");
- }
-
- free(request_s.uri);
-
- /* To avoid unnecessary ERR log (repr could be NULL) */
- if (request_s.repr)
- iotcon_repr_free(request_s.repr);
- if (request_s.header_options)
- iotcon_options_free(request_s.header_options);
- if (request_s.query)
- iotcon_query_free(request_s.query);
-
- return OC_EH_OK;
-}
-
-
-extern "C" OCResourceHandle ic_ioty_register_res(const char *uri,
- iotcon_resource_types_h res_types, int ifaces, uint8_t properties)
-{
- FN_CALL;
- unsigned int i;
- OCStackResult ret;
- string resUri;
- string resType;
- string resInterface;
- OCResourceHandle handle;
-
- resUri = uri;
-
- resType = ic_resource_types_get_nth_data(res_types, 0);
-
- if (IOTCON_INTERFACE_DEFAULT & ifaces) {
- resInterface = DEFAULT_INTERFACE;
- ifaces ^= IOTCON_INTERFACE_DEFAULT;
- }
- else if (IOTCON_INTERFACE_LINK & ifaces) {
- resInterface = LINK_INTERFACE;
- ifaces ^= IOTCON_INTERFACE_LINK;
- }
- else if (IOTCON_INTERFACE_BATCH & ifaces) {
- resInterface = BATCH_INTERFACE;
- ifaces ^= IOTCON_INTERFACE_BATCH;
- }
- else if (IOTCON_INTERFACE_GROUP & ifaces) {
- resInterface = GROUP_INTERFACE;
- ifaces ^= IOTCON_INTERFACE_GROUP;
- }
-
- ret = registerResource(handle, resUri, resType, resInterface,
- _ic_ioty_request_handler, properties);
- if (OC_STACK_OK != ret) {
- ERR("registerResource Fail(%d)", ret);
- return NULL;
- }
- for (i = 1; i < ic_resource_types_get_length(res_types); i++)
- ic_ioty_bind_type_to_res(handle, ic_resource_types_get_nth_data(res_types, i));
-
- if (IOTCON_INTERFACE_DEFAULT & ifaces)
- ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_DEFAULT);
-
- if (IOTCON_INTERFACE_LINK & ifaces)
- ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_LINK);
-
- if (IOTCON_INTERFACE_BATCH & ifaces)
- ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_BATCH);
-
- if (IOTCON_INTERFACE_GROUP & ifaces)
- ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_GROUP);
-
- return handle;
-}
-
-
-extern "C" int ic_ioty_unregister_res(iotcon_resource_h resource_handle)
-{
- OCResourceHandle resourceHandle = resource_handle;
-
- OCStackResult result = unregisterResource(resourceHandle);
- if(OC_STACK_OK != result) {
- ERR("unregisterResource Fail(%d)", result);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_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);
-
- string interface_str(src);
-
- if (IC_STR_EQUAL == DEFAULT_INTERFACE.compare(interface_str)) {
- *dest = IOTCON_INTERFACE_DEFAULT;
- }
- else if (IC_STR_EQUAL == LINK_INTERFACE.compare(interface_str)) {
- *dest = IOTCON_INTERFACE_LINK;
- }
- else if (IC_STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) {
- *dest = IOTCON_INTERFACE_BATCH;
- }
- else if (IC_STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) {
- *dest = IOTCON_INTERFACE_GROUP;
- }
- else {
- ERR("Invalid interface");
- *dest = IOTCON_INTERFACE_NONE;
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-static int _ic_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 ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest)
-{
- FN_CALL;
- int ret;
- string iface_str;
-
- ret = _ic_ioty_convert_interface_flag(src, iface_str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_ioty_convert_interface_flag() Fail(%d)", ret);
- *dest = NULL;
- return ret;
- }
-
- *dest = ic_utils_strdup(iface_str.c_str());
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_bind_iface_to_res(OCResourceHandle resourceHandle,
- iotcon_interface_e iface)
-{
- int ret;
- OCStackResult ocRet;
- string resource_interface;
-
- ret = _ic_ioty_convert_interface_flag(iface, resource_interface);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
- return ret;
- }
-
- ocRet = bindInterfaceToResource(resourceHandle, resource_interface);
- if (OC_STACK_OK != ocRet) {
- ERR("bindInterfaceToResource() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int ic_ioty_bind_type_to_res(OCResourceHandle resource_handle,
- const char *resource_type)
-{
- OCStackResult ret;
- OCResourceHandle resourceHandle = resource_handle;
-
- ret = bindTypeToResource(resourceHandle, resource_type);
- if (OC_STACK_OK != ret) {
- ERR("bindTypeToResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_bind_res(OCResourceHandle parent, OCResourceHandle child)
-{
- OCStackResult ret;
-
- ret = bindResource(parent, child);
- if (OC_STACK_OK != ret) {
- ERR("bindResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_unbind_res(OCResourceHandle parent, OCResourceHandle child)
-{
- OCStackResult ret;
-
- ret = unbindResource(parent, child);
- if (OC_STACK_OK != ret) {
- ERR("unbindResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_register_device_info(iotcon_device_info_s device_info)
-{
- FN_CALL;
- OCStackResult ret;
-
- OCDeviceInfo deviceInfo = {0};
- deviceInfo.deviceName = device_info.name;
- deviceInfo.hostName = device_info.host_name;
- deviceInfo.deviceUUID = device_info.uuid;
- deviceInfo.contentType = device_info.content_type;
- deviceInfo.version = device_info.version;
- deviceInfo.manufacturerName = device_info.manuf_name;
- deviceInfo.manufacturerUrl = device_info.manuf_url;
- deviceInfo.modelNumber = device_info.model_number;
- deviceInfo.dateOfManufacture = device_info.date_of_manufacture;
- deviceInfo.platformVersion = device_info.platform_ver;
- deviceInfo.firmwareVersion = device_info.firmware_ver;
- deviceInfo.supportUrl = device_info.support_url;
-
- ret = registerDeviceInfo(deviceInfo);
- if (OC_STACK_OK != ret) {
- ERR("registerDeviceInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int ic_ioty_get_device_info(const char *host_address,
- iotcon_device_info_cb cb, void *user_data)
-{
- OCStackResult ret;
- string resHost = host_address + string(IC_DEVICE_DISCOVERY);
-
- shared_ptr<icIotivityHandler::deviceObject> object
- = make_shared<icIotivityHandler::deviceObject>(cb, user_data);
- FindDeviceCallback findDeviceCallback = bind(
- &icIotivityHandler::deviceObject::receivedDeviceInfo,
- object,
- placeholders::_1);
-
- ret = getDeviceInfo("", resHost, findDeviceCallback);
- if (OC_STACK_OK != ret) {
- ERR("getDeviceInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_msg *msg,
- iotcon_observers_h observers)
-{
- int ret;
- OCStackResult ocRet;
- ObservationIds obsIds;
- OCRepresentation ocRep;
- string iface;
-
- GList *node = g_list_first((GList*)observers);
- while (node) {
- int obs_id = GPOINTER_TO_UINT(node->data);
- obsIds.push_back(obs_id);
-
- node = node->next;
- }
-
- shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
- resourceResponse->setErrorCode(msg->error_code);
-
- ret = ic_ioty_repr_parse(msg->repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_repr_parse() Fail(%d)", ret);
- return ret;
- }
-
- if (IOTCON_INTERFACE_NONE != msg->iface) {
- ret = _ic_ioty_convert_interface_flag(msg->iface, iface);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", msg->iface, ret);
- return ret;
- }
- }
- else {
- iface = DEFAULT_INTERFACE;
- }
-
- resourceResponse->setResourceRepresentation(ocRep, iface);
-
- ocRet = notifyListOfObservers(resHandle, obsIds, resourceResponse);
- if (OC_STACK_OK != ocRet) {
- ERR("notifyListOfObservers() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp)
-{
- FN_CALL;
- string iface;
- int ret;
- OCStackResult ocRet;
- OCRepresentation ocRep;
-
- ret = ic_ioty_repr_parse(resp->repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_repr_parse() Fail(%d)", ret);
- return ret;
- }
-
- auto pResponse = make_shared<OCResourceResponse>();
- if (pResponse) {
- pResponse->setRequestHandle(resp->request_handle);
- pResponse->setResourceHandle(resp->resource_handle);
- pResponse->setErrorCode(resp->error_code);
- pResponse->setResponseResult((OCEntityHandlerResult)resp->result);
-
- if (IOTCON_INTERFACE_NONE != resp->iface) {
- ret = _ic_ioty_convert_interface_flag(resp->iface, iface);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", resp->iface, ret);
- return ret;
- }
- }
- else {
- iface = DEFAULT_INTERFACE;
- }
-
- pResponse->setResourceRepresentation(ocRep, iface);
-
- ocRet = sendResponse(pResponse);
- if (OC_STACK_OK != ocRet) {
- ERR("sendResponse() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
- }
-
- FN_END;
-
- return IOTCON_ERROR_NONE;
-
-}
-
-extern "C" const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_address,
- const char *resource_type,
- iotcon_presence_cb cb,
- void *user_data)
-{
- OCStackResult ret;
- iotcon_presence_h presence_handle = NULL;
-
- shared_ptr<icIotivityHandler::presenceObject> object
- = make_shared<icIotivityHandler::presenceObject>(cb, user_data);
- SubscribeCallback subscribeCallback
- = bind(&icIotivityHandler::presenceObject::presenceHandler, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- ret = subscribePresence(presence_handle, host_address, resource_type,
- subscribeCallback);
-
- if (OC_STACK_OK != ret) {
- ERR("subscribePresence() Fail(%d)", ret);
- return NULL;
- }
-
- return presence_handle;
-}
-
-extern "C" int ic_ioty_unsubscribe_presence(iotcon_presence_h presence_handle)
-{
- OCStackResult ret;
-
- ret = unsubscribePresence(presence_handle);
- if (OC_STACK_OK != ret) {
- ERR("unsubscribePresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_start_presence(unsigned int time_to_live)
-{
- OCStackResult ret;
-
- ret = startPresence(time_to_live);
- if (OC_STACK_OK != ret) {
- ERR("startPresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_stop_presence()
-{
- OCStackResult ret;
-
- ret = stopPresence();
- if (OC_STACK_OK != ret) {
- ERR("stopPresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_find_resource(const char *host_address, const char *resource_type,
- iotcon_found_resource_cb cb, void *user_data)
-{
- OCStackResult ret;
- ostringstream resource_name;
-
- if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address))
- resource_name << host_address << IC_MULTICAST_RESOURCE_DISCOVERY;
- else
- resource_name << host_address << IC_UNICAST_RESOURCE_DISCOVERY;
-
- if (resource_type)
- resource_name << "?rt=" << resource_type;
-
- shared_ptr<icIotivityHandler::findObject> object
- = make_shared<icIotivityHandler::findObject>(cb, user_data);
- FindCallback findCallback = bind(&icIotivityHandler::findObject::foundResource,
- object, placeholders::_1);
-
- ret = findResource("", resource_name.str(), findCallback);
- if (OC_STACK_OK != ret) {
- ERR("findResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-static int _ic_ioty_accumulate_options_vector(unsigned short id, const char *data,
- void *user_data)
-{
- HeaderOptions *options = static_cast<HeaderOptions*>(user_data);
- HeaderOption::OCHeaderOption option(id, data);
- (*options).push_back(option);
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-
-static int _ic_ioty_accumulate_res_types(const char *type, void *user_data)
-{
- vector<string> *types = static_cast<vector<string>*>(user_data);
- (*types).push_back(type);
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-
-static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource)
-{
- string host;
- string uri;
- vector<string> resource_types;
- vector<string> resource_ifs;
-
- HeaderOptions header_options;
-
- RETV_IF(NULL == resource, NULL);
- RETV_IF(NULL == resource->host, NULL);
- RETV_IF(NULL == resource->uri, NULL);
- RETV_IF(NULL == resource->types, NULL);
-
- host = resource->host;
- uri = resource->uri;
-
- iotcon_resource_types_foreach(resource->types, _ic_ioty_accumulate_res_types,
- (void*)&resource_types);
-
- if (IOTCON_INTERFACE_NONE == resource->ifaces) {
- resource_ifs.push_back(DEFAULT_INTERFACE);
- }
- else {
- if (IOTCON_INTERFACE_DEFAULT & resource->ifaces)
- resource_ifs.push_back(DEFAULT_INTERFACE);
-
- if (IOTCON_INTERFACE_LINK & resource->ifaces)
- resource_ifs.push_back(LINK_INTERFACE);
-
- if (IOTCON_INTERFACE_BATCH & resource->ifaces)
- resource_ifs.push_back(BATCH_INTERFACE);
-
- if (IOTCON_INTERFACE_GROUP & resource->ifaces)
- resource_ifs.push_back(GROUP_INTERFACE);
- }
-
- OCResource::Ptr ocResource = constructResourceObject(host, uri,
- resource->is_observable, resource_types, resource_ifs);
-
- if (resource->header_options) {
- iotcon_options_foreach(resource->header_options,
- _ic_ioty_accumulate_options_vector, (void*)&header_options);
- ocResource->setHeaderOptions(header_options);
- }
-
- return ocResource;
-}
-
-
-static int _ic_ioty_accumulate_query_map(const char *key, const char *value,
- void *user_data)
-{
- QueryParamsMap *queryParams = static_cast<QueryParamsMap*>(user_data);
- string keyStr = key;
- string valueStr = value;
- (*queryParams)[keyStr] = valueStr;
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-
-extern "C" int ic_ioty_get(iotcon_client_h resource, iotcon_query_h query,
- iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- OCStackResult ret;
- OCResource::Ptr ocResource;
- QueryParamsMap queryParams;
-
- if (query)
- iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void *)&queryParams);
-
- ocResource = _ic_ioty_create_oc_resource(resource);
-
- shared_ptr<icIotivityHandler::getObject> object
- = make_shared<icIotivityHandler::getObject>(cb, user_data);
- GetCallback getCallback = bind(&icIotivityHandler::getObject::onGet, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
- ret = ocResource->get(queryParams, getCallback);
- if (OC_STACK_OK != ret) {
- ERR("get() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr,
- iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
- OCStackResult result;
- OCResource::Ptr ocResource;
- OCRepresentation ocRep;
- QueryParamsMap queryParams;
-
- if (query)
- iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
-
- ret = ic_ioty_repr_parse(repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_repr_parse() Fail(%d)", ret);
- return ret;
- }
-
- ocResource = _ic_ioty_create_oc_resource(resource);
-
- shared_ptr<icIotivityHandler::putObject> object
- = make_shared<icIotivityHandler::putObject>(cb, user_data);
- PutCallback putCallback = bind(&icIotivityHandler::putObject::onPut, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- result = ocResource->put(ocRep, queryParams, putCallback);
- if (OC_STACK_OK != result) {
- ERR("put() Fail(%d)", result);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr,
- iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
-{
- FN_CALL;
- int ret;
- OCStackResult ocRet;
- QueryParamsMap queryParams;
- OCRepresentation ocRep;
- OCResource::Ptr ocResource;
-
- if (query)
- iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
-
- ret = ic_ioty_repr_parse(repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_repr_parse() Fail(%d)", ret);
- return ret;
- }
-
- ocResource = _ic_ioty_create_oc_resource(resource);
-
- shared_ptr<icIotivityHandler::postObject> object
- = make_shared<icIotivityHandler::postObject>(cb, user_data);
- PostCallback postCallback = bind(&icIotivityHandler::postObject::onPost, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- ocRet = ocResource->post(ocRep, queryParams, postCallback);
- if (OC_STACK_OK != ocRet) {
- ERR("post() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_delete_res(iotcon_client_h resource,
- iotcon_on_delete_cb cb, void *user_data)
-{
- FN_CALL;
- OCStackResult ret;
- OCResource::Ptr ocResource;
-
- ocResource = _ic_ioty_create_oc_resource(resource);
-
- shared_ptr<icIotivityHandler::deleteObject> object
- = make_shared<icIotivityHandler::deleteObject>(cb, user_data);
- DeleteCallback deleteCallback = bind(&icIotivityHandler::deleteObject::onDelete,
- object, placeholders::_1, placeholders::_2);
-
- ret = ocResource->deleteResource(deleteCallback);
- if (OC_STACK_OK != ret) {
- ERR("deleteResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_observe(iotcon_client_h resource,
- iotcon_observe_type_e observe_type,
- iotcon_query_h query,
- iotcon_on_observe_cb cb,
- void *user_data)
-{
- OCStackResult ret;
- OCResource::Ptr ocResource;
- ObserveType observeType;
- QueryParamsMap queryParams;
-
- if (query)
- iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
-
- if (IOTCON_OBSERVE == observe_type) {
- observeType = ObserveType::Observe;
- }
- else if (IOTCON_OBSERVE_ALL == observe_type) {
- observeType = ObserveType::ObserveAll;
- }
- else {
- ERR("Invalid observe_type");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- ocResource = _ic_ioty_create_oc_resource(resource);
-
- resource_handle *obs_h = new resource_handle();
- obs_h->ocResource = ocResource;
- resource->observe_handle = (void*)obs_h;
-
- shared_ptr<icIotivityHandler::observeObject> object
- = make_shared<icIotivityHandler::observeObject>(cb, user_data);
- ObserveCallback observeCallback = bind(&icIotivityHandler::observeObject::onObserve,
- object, placeholders::_1, placeholders::_2, placeholders::_3,
- placeholders::_4);
- ret = ocResource->observe(observeType, queryParams, observeCallback);
- if (OC_STACK_OK != ret) {
- ERR("observe() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-extern "C" int ic_ioty_cancel_observe(iotcon_client_h resource)
-{
- OCStackResult ret;
- resource_handle *resource_h = (resource_handle *)resource->observe_handle;
-
- OCResource::Ptr ocResource = resource_h->ocResource;
- delete (resource_handle *)resource->observe_handle;
- resource->observe_handle = NULL;
-
- ret = ocResource->cancelObserve();
- if (OC_STACK_OK != ret) {
- ERR("cancelObserve() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_IOTIVITY_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_H__
-
-#include <stdint.h>
-
-#include "iotcon.h"
-#include "ic.h"
-#include "ic-response.h"
-
-void ic_ioty_config(const char *addr, unsigned short port);
-
-void* ic_ioty_register_res(const char *uri, iotcon_resource_types_h res_types, int ifaces,
- uint8_t properties);
-
-int ic_ioty_unregister_res(iotcon_resource_h resource_handle);
-
-int ic_ioty_bind_iface_to_res(void *resource_handle, iotcon_interface_e iface);
-
-int ic_ioty_bind_type_to_res(void *resource_handle, const char *resource_type);
-
-int ic_ioty_bind_res(void *parent, void *child);
-
-int ic_ioty_unbind_res(void *parent, void *child);
-
-int ic_ioty_register_device_info(iotcon_device_info_s device_info);
-
-int ic_ioty_get_device_info(const char *host_address, iotcon_device_info_cb cb,
- void *user_data);
-
-int ic_ioty_send_notify(void *resource, struct ic_notify_msg *msg,
- iotcon_observers_h observers);
-
-int ic_ioty_send_res_response_data(struct ic_resource_response *resp);
-
-const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_address,
- const char *resource_type,
- iotcon_presence_cb cb,
- void *user_data);
-int ic_ioty_unsubscribe_presence(iotcon_presence_h presence_handle);
-int ic_ioty_start_presence(unsigned int time_to_live);
-int ic_ioty_stop_presence();
-
-int ic_ioty_find_resource(const char *host_address, const char *resource_type,
- iotcon_found_resource_cb cb, void *user_data);
-
-int ic_ioty_get(iotcon_client_h resource, iotcon_query_h query,
- iotcon_on_cru_cb cb, void *user_data);
-
-int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query,
- iotcon_on_cru_cb cb, void *user_data);
-
-int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query,
- iotcon_on_cru_cb cb, void *user_data);
-
-int ic_ioty_delete_res(iotcon_client_h resource,
- iotcon_on_delete_cb cb, void *user_data);
-
-int ic_ioty_observe(iotcon_client_h resource, iotcon_observe_type_e observe_type,
- iotcon_query_h query, iotcon_on_observe_cb cb, void *user_data);
-
-int ic_ioty_cancel_observe(iotcon_client_h resource);
-
-int ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest);
-int ic_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest);
-
-
-#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_H__
+++ /dev/null
-/*
- * 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_INTERNAL_LOG_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__
-
-#define IC_LOG_RED "\033[0;31m"
-#define IC_LOG_GREEN "\033[0;32m"
-#define IC_LOG_BROWN "\033[0;33m"
-#define IC_LOG_BLUE "\033[0;34m"
-#define IC_LOG_END "\033[0;m"
-
-#undef _DBG
-#undef _INFO
-#undef _WARN
-#undef _ERR
-
-#undef DBG
-#undef INFO
-#undef WARN
-#undef ERR
-
-#define TIZEN_DEBUG_ENABLE
-#define LOG_TAG "IOTCON"
-#include <dlog.h>
-
-#ifdef IC_DAEMON
-
-#define _DBG(fmt, arg...) SLOGD(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
-#define _INFO(fmt, arg...) SLOGI(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
-#define _WARN(fmt, arg...) SLOGW(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
-#define _ERR(fmt, arg...) SLOGE(IC_LOG_GREEN "<Daemon>" IC_LOG_END fmt, ##arg)
-
-#else /* IC_DAEMON */
-
-#define _DBG(fmt, arg...) SLOGD(fmt, ##arg)
-#define _INFO(fmt, arg...) SLOGI(fmt, ##arg)
-#define _WARN(fmt, arg...) SLOGW(fmt, ##arg)
-#define _ERR(fmt, arg...) SLOGE(fmt, ##arg)
-
-#endif /* IC_DAEMON */
-
-#if 0
-#define _DBG(fmt, arg...) \
- printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
-#define _INFO(fmt, arg...) \
- printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
-#define _WARN(fmt, arg...) \
- printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
-#define _ERR(fmt, arg...) \
- printf("[IoTCon]%s(%d):" fmt "\n", __FUNCTION__, __LINE__, ##arg)
-#endif
-
-#define IC_DEBUGGING
-
-#ifdef IC_DEBUGGING
-
-#define FN_CALL _INFO(">>>>>>>> called")
-#define FN_END _INFO("<<<<<<<< ended")
-#define DBG(fmt, arg...) _DBG(fmt, ##arg)
-#define WARN(fmt, arg...) _WARN(IC_LOG_BROWN fmt IC_LOG_END, ##arg)
-#define ERR(fmt, arg...) _ERR(IC_LOG_RED fmt IC_LOG_END, ##arg)
-#define INFO(fmt, arg...) _INFO(IC_LOG_BLUE fmt IC_LOG_END, ##arg)
-#define SECURE_DBG(fmt, arg...) SECURE_SLOGD(fmt, ##arg)
-#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg)
-
-#else /* IC_DEBUGGING */
-
-#define FN_CALL
-#define FN_END
-#define DBG(fmt, arg...)
-#define WARN(fmt, arg...)
-#define ERR(fmt, arg...) _ERR(fmt, ##arg)
-#define INFO(fmt, arg...)
-#define SECURE_DBG(fmt, arg...)
-#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg)
-
-#endif /* IC_DEBUGGING */
-
-#define RET_IF(expr) \
- do { \
- if (expr) { \
- ERR("(%s)", #expr); \
- return; \
- }\
- } while(0)
-
-#define RETV_IF(expr, val) \
- do {\
- if (expr) { \
- ERR("(%s)", #expr); \
- return (val); \
- } \
- } while(0)
-
-#define RETM_IF(expr, fmt, arg...) \
- do {\
- if (expr) { \
- ERR(fmt, ##arg); \
- return; \
- }\
- } while(0)
-
-#define RETVM_IF(expr, val, fmt, arg...) \
- do {\
- if (expr) { \
- ERR(fmt, ##arg); \
- return (val); \
- } \
- } while(0)
-
-#define ERR_IF(expr) \
- do { \
- if (expr) { \
- ERR("(%s)", #expr); \
- } \
- } while (0)
-
-#define WARN_IF(expr) \
- do { \
- if (expr) { \
- WARN("(%s)", #expr); \
- } \
- } while (0)
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon-struct.h"
-#include "ic-common.h"
-
-API void iotcon_observers_free(iotcon_observers_h observers)
-{
- RET_IF(NULL == observers);
-
- g_list_free(observers);
-}
-
-
-/* If you want to make a new list, then you should set observers is NULL. */
-API iotcon_observers_h iotcon_observers_append(iotcon_observers_h observers,
- int obs_id)
-{
- return g_list_append(observers, GUINT_TO_POINTER(obs_id));
-}
-
-
-API iotcon_observers_h iotcon_observers_remove(iotcon_observers_h observers,
- int obs_id)
-{
- RETV_IF(NULL == observers, observers);
-
- return g_list_remove(observers, GUINT_TO_POINTER(obs_id));
-}
-
+++ /dev/null
-/*
- * 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 <stdbool.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon-struct.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-options.h"
-
-API iotcon_options_h iotcon_options_new()
-{
- iotcon_options_h options = calloc(1, sizeof(struct ic_options));
- if (NULL == options) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- options->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free);
- return options;
-}
-
-
-void ic_options_free(iotcon_options_h options)
-{
- RET_IF(NULL == options);
-
- g_hash_table_unref(options->hash);
- free(options);
-}
-
-
-API void iotcon_options_free(iotcon_options_h options)
-{
- RET_IF(NULL == options);
- RETM_IF(true == options->has_parent, "iotcon_options has parent");
-
- ic_options_free(options);
-}
-
-
-/* iotcon_options_h can have up to 2 options.
- * option id is always situated between 2048 and 3000.
- * Length of option data is less than or equal to 15. */
-API int iotcon_options_insert(iotcon_options_h options, unsigned short id,
- const char *data)
-{
- FN_CALL;
-
- RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(options->has_parent, IOTCON_ERROR_INVALID_PARAMETER,
- "Don't modify it. It is already set.");
- RETVM_IF(IOTCON_OPTIONS_MAX <= g_hash_table_size(options->hash),
- IOTCON_ERROR_OUT_OF_MEMORY, "Options already have maximum elements.");
-
- RETVM_IF(((id < IOTCON_OPTIONID_MIN) || (IOTCON_OPTIONID_MAX < id)),
- IOTCON_ERROR_INVALID_PARAMETER, "Invalid id(%d)", id);
-
- RETV_IF(NULL == data, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_OPTION_DATA_LENGTH_MAX < strlen(data), IOTCON_ERROR_INVALID_PARAMETER,
- "The length of option data(%s) is invalid.", data);
-
- g_hash_table_insert(options->hash, GUINT_TO_POINTER(id), ic_utils_strdup(data));
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_options_delete(iotcon_options_h options, unsigned short id)
-{
- gboolean ret;
-
- RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(options->has_parent, 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) {
- ERR("g_hash_table_remove() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- return IOTCON_ERROR_NONE;
-}
-
-
-API const char* iotcon_options_lookup(iotcon_options_h options, unsigned short id)
-{
- const char *ret;
-
- RETV_IF(NULL == options, NULL);
-
- ret = g_hash_table_lookup(options->hash, GUINT_TO_POINTER(id));
- if (NULL == ret)
- ERR("g_hash_table_lookup() Fail");
-
- return ret;
-}
-
-
-API int iotcon_options_foreach(iotcon_options_h options,
- iotcon_options_foreach_cb cb, void *user_data)
-{
- GHashTableIter iter;
- gpointer key, value;
-
- RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- g_hash_table_iter_init(&iter, options->hash);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- if (false == cb(GPOINTER_TO_UINT(key), value, user_data))
- break;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-iotcon_options_h ic_options_ref(iotcon_options_h options)
-{
- iotcon_options_h ref;
-
- RETV_IF(NULL == options, NULL);
-
- ref = calloc(1, sizeof(struct ic_options));
- if (NULL == ref) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- ref->hash = g_hash_table_ref(options->hash);
-
- return ref;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_OPTIONS_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_OPTIONS_H__
-
-#include "iotcon-struct.h"
-
-struct ic_options {
- bool has_parent;
- GHashTable *hash;
-};
-
-void ic_options_free(iotcon_options_h options);
-iotcon_options_h ic_options_ref(iotcon_options_h options);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_OPTIONS_H__ */
+++ /dev/null
-/*
- * 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 <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon-struct.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-query.h"
-
-API iotcon_query_h iotcon_query_new()
-{
- iotcon_query_h query = calloc(1, sizeof(struct ic_query));
- if (NULL == query) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- query->hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
- return query;
-}
-
-
-API void iotcon_query_free(iotcon_query_h query)
-{
- RET_IF(NULL == query);
-
- g_hash_table_unref(query->hash);
- free(query);
-}
-
-
-/* The full length of query should be less than or equal to 64. */
-API int iotcon_query_insert(iotcon_query_h query, const char *key, const char *value)
-{
- int query_len;
-
- RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
-
- /* first query : ?key=value
- * Rest of query : &key=value */
- query_len = strlen(key) + strlen(value) + 2;
- if (IOTCON_QUERY_LENGTH_MAX < (query->len + query_len)) {
- ERR("Length of query is invalid.");
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_insert(query->hash, ic_utils_strdup(key), ic_utils_strdup(value));
- query->len += query_len;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_query_delete(iotcon_query_h query, const char *key)
-{
- gboolean ret;
- int query_len;
- char *value;
-
- RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(query->hash, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- query_len = strlen(key) + strlen(value) + 2;
-
- ret = g_hash_table_remove(query->hash, key);
- if (FALSE == ret) {
- ERR("g_hash_table_remove() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- query->len -= query_len;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API const char* iotcon_query_lookup(iotcon_query_h query, const char *key)
-{
- const char *ret = NULL;
-
- RETV_IF(NULL == query, NULL);
- RETV_IF(NULL == key, NULL);
-
- ret = g_hash_table_lookup(query->hash, key);
- if (NULL == ret)
- ERR("g_hash_table_lookup() Fail");
-
- return ret;
-}
-
-API int iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb,
- void *user_data)
-{
- GHashTableIter iter;
- gpointer key, value;
-
- RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- g_hash_table_iter_init(&iter, query->hash);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- if (false == cb(key, value, user_data))
- break;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_QUERY_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_QUERY_H__
-
-#include "iotcon-struct.h"
-
-struct ic_query {
- int len;
- GHashTable *hash;
-};
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_QUERY_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <errno.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-#include "iotcon-representation.h"
-#include "ic-common.h"
-#include "ic-repr-obj.h"
-#include "ic-repr.h"
-#include "ic-repr-value.h"
-#include "ic-repr-list.h"
-
-void ic_list_inc_ref_count(iotcon_list_h val)
-{
- RET_IF(NULL == val);
- RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
-
- val->ref_count++;
-}
-
-static bool _ic_list_dec_ref_count(iotcon_list_h val)
-{
- bool ret;
-
- RETV_IF(NULL == val, false);
- RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
-
- val->ref_count--;
- if (0 == val->ref_count)
- ret = true;
- else
- ret = false;
-
- return ret;
-}
-
-static iotcon_list_h _ic_list_new(iotcon_types_e type)
-{
- iotcon_list_h list;
-
- list = calloc(1, sizeof(struct ic_list_s));
- if (NULL == list) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
- ic_list_inc_ref_count(list);
- list->type = type;
-
- return list;
-}
-
-API iotcon_list_h iotcon_list_new(iotcon_types_e type)
-{
- if (type < IOTCON_TYPE_INT || IOTCON_TYPE_REPR < type) {
- ERR("Invalid Type(%d)", type);
- return NULL;
- }
-
- return _ic_list_new(type);
-}
-
-
-API int iotcon_list_insert_int(iotcon_list_h list, int val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_int(val);
- if (NULL == value) {
- ERR("ic_value_new_int(%d) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_bool(val);
- if (NULL == value) {
- ERR("ic_value_new_bool(%d) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_insert_double(iotcon_list_h list, double val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_double(val);
- if (NULL == value) {
- ERR("ic_value_new_double(%f) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_insert_str(iotcon_list_h list, char *val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_str(val);
- if (NULL == value) {
- ERR("ic_value_new_str(%s) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_list(val);
- if (NULL == value) {
- ERR("ic_value_new_list(%p) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- ic_list_inc_ref_count(val);
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
-
- value = ic_value_new_repr(val);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
- ic_repr_inc_ref_count(val);
-
- return ic_list_insert(list, value, pos);
-}
-
-
-API int iotcon_list_get_nth_int(iotcon_list_h list, int pos, int *val)
-{
- int ival, ret;
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_int(value, &ival);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_int() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *val = ival;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_get_nth_bool(iotcon_list_h list, int pos, bool *val)
-{
- int ret;
- bool bval;
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_bool(value, &bval);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_bool() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *val = bval;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_get_nth_double(iotcon_list_h list, int pos, double *val)
-{
- int ret;
- double dbval;
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_double(value, &dbval);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_double() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *val = dbval;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_get_nth_str(iotcon_list_h list, int pos, const char **val)
-{
- int ret;
- const char *strval;
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_str(value, &strval);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_str() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *val = strval;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_get_nth_list(iotcon_list_h src, int pos, iotcon_list_h *dest)
-{
- int ret;
- iotcon_value_h value;
- iotcon_list_h list_val;
-
- RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == src->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(src->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_list(value, &list_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *dest = list_val;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_get_nth_repr(iotcon_list_h list, int pos, iotcon_repr_h *repr)
-{
- int ret;
- iotcon_value_h value;
- iotcon_repr_h repr_val;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ret = ic_value_get_repr(value, &repr_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- *repr = repr_val;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_types_e value_type)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(value_type != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "IOTCON_ERROR_PARAMETER(%d)", list->type);
-
- value = g_list_nth_data(list->list, pos);
- if (NULL == value) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- if (IOTCON_TYPE_STR == value->type) {
- ic_basic_s *real = (ic_basic_s*)value;
- free(real->val.s);
- }
- else if (IOTCON_TYPE_LIST == value->type) {
- ic_val_list_s *real = (ic_val_list_s*)value;
- iotcon_list_free(real->list);
- }
- else if (IOTCON_TYPE_REPR == value->type) {
- ic_val_repr_s *real = (ic_val_repr_s*)value;
- iotcon_repr_free(real->repr);
- }
-
- ic_list_remove(list, value);
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_list_del_nth_int(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_INT);
- if (IOTCON_ERROR_NONE != ret)
- ERR("iotcon_list_del_nth_int() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_del_nth_bool(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_BOOL);
- if (IOTCON_ERROR_NONE != ret)
- ERR("_ic_list_del_nth_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_del_nth_double(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_DOUBLE);
- if (IOTCON_ERROR_NONE != ret)
- ERR("_ic_list_del_nth_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_del_nth_str(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_STR);
- if (IOTCON_ERROR_NONE != ret)
- ERR("_ic_list_del_nth_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_del_nth_list(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_LIST);
- if (IOTCON_ERROR_NONE != ret)
- ERR("_ic_list_del_nth_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_del_nth_repr(iotcon_list_h list, int pos)
-{
- int ret;
-
- ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_REPR);
- if (IOTCON_ERROR_NONE != ret)
- ERR("_ic_list_del_nth_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_list_get_type(iotcon_list_h list, int *type)
-{
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
-
- *type = list->type;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API unsigned int iotcon_list_get_length(iotcon_list_h list)
-{
- RETV_IF(NULL == list, 0);
- RETV_IF(NULL == list->list, 0);
-
- return g_list_length(list->list);
-}
-
-
-int ic_list_remove(iotcon_list_h list, iotcon_value_h val)
-{
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
-
- list->list = g_list_remove(list->list, val);
-
- return IOTCON_ERROR_NONE;
-}
-
-
-int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos)
-{
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
-
- list->list = g_list_insert(list->list, value, pos);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn,
- void *user_data)
-{
- GList *cur;
- int index = 0;
- ic_basic_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->val.i, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn,
- void *user_data)
-{
- GList *cur;
- int index = 0;
- ic_basic_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->val.b, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn,
- void *user_data)
-{
- GList *cur;
- int index = 0;
- ic_basic_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->val.d, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn,
- void *user_data)
-{
- GList *cur;
- int index = 0;
- ic_basic_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->val.s, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_list(iotcon_list_h list, iotcon_list_list_fn fn,
- void *user_data)
-{
- int index = 0;
- GList *cur = NULL;
- ic_val_list_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->list, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data)
-{
- int index = 0;
- GList *cur = NULL;
- ic_val_repr_s *real = NULL;
-
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", list->type);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);;
-
- cur = list->list;
- while (cur) {
- GList *next = cur->next;
- real = cur->data;
- if (IOTCON_FUNC_STOP == fn(index, real->repr, user_data))
- break;
- index++;
- cur = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static iotcon_value_h _ic_list_get_nth_value(iotcon_list_h list, int pos)
-{
- RETV_IF(NULL == list, NULL);
- RETV_IF(NULL == list->list, NULL);
-
- return g_list_nth_data(list->list, pos);
-}
-
-
-
-/*
-* A general result : [1,2,3]
-*/
-JsonArray* ic_list_to_json(iotcon_list_h list)
-{
- int i, ret, count;
- JsonArray *parray = NULL;
- JsonNode *child_node = NULL;
- JsonObject *child_obj = NULL;
- JsonArray *child_array = NULL;
- iotcon_repr_h child_repr = NULL;
- iotcon_list_h child_list = NULL;
- iotcon_value_h child_value = NULL;
-
- RETV_IF(NULL == list, NULL);
- RETV_IF(NULL == list->list, NULL);
-
- count = g_list_length(list->list);
-
- parray = json_array_new();
- for (i = 0; i < count; i++) {
- child_value = _ic_list_get_nth_value(list, i);
-
- int type = child_value->type;
- switch (type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- child_node = ic_value_to_json(child_value);
- if (NULL == child_node) {
- ERR("ic_value_to_json(child_value) Fail");
- json_array_unref(parray);
- return NULL;
- }
- json_array_add_element(parray, child_node);
- break;
- case IOTCON_TYPE_LIST:
- ret = ic_value_get_list(child_value, &child_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- json_array_unref(parray);
- return NULL;
- }
-
- child_array = ic_list_to_json(child_list);
- if (NULL == child_array) {
- ERR("ic_list_to_json(child_list) Fail");
- json_array_unref(parray);
- return NULL;
- }
- child_node = json_node_new(JSON_NODE_ARRAY);
- json_node_set_array(child_node, child_array);
- json_array_add_element(parray, child_node);
- break;
- case IOTCON_TYPE_REPR:
- ret = ic_value_get_repr(child_value, &child_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_repr() Fail(%d)", ret);
- json_array_unref(parray);
- return NULL;
- }
-
- child_obj = ic_obj_to_json(child_repr);
- if (NULL == child_obj) {
- ERR("ic_obj_to_json(child_repr) Fail");
- json_array_unref(parray);
- return NULL;
- }
- child_node = json_node_new(JSON_NODE_OBJECT);
- json_node_set_object(child_node, child_obj);
- json_array_add_element(parray, child_node);
- }
- }
-
- return parray;
-}
-
-/*
-* A general input : [1,2,3]
-*/
-iotcon_list_h ic_list_from_json(JsonArray *parray)
-{
- int i, ret;
- int count = json_array_get_length(parray);
-
- iotcon_list_h list = _ic_list_new(IOTCON_TYPE_NONE);
- if (NULL == list) {
- ERR("_ic_list_new() Fail");
- return NULL;
- }
-
- for (i = 0; i < count; i++) {
- JsonNode *child_node = json_array_get_element(parray, i);
- if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) {
- iotcon_value_h value = ic_value_from_json(child_node);
- if (NULL == value) {
- ERR("ic_value_from_json() Fail");
- iotcon_list_free(list);
- return NULL;
- }
-
- ic_basic_s *real = (ic_basic_s*)value;
- if (IOTCON_TYPE_NONE != list->type && list->type != real->type) {
- ERR("Type matching Fail(list:%d,value:%d)", list->type, real->type);
- ic_value_free(value);
- iotcon_list_free(list);
- return NULL;
- }
-
- ret = ic_list_insert(list, value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert() Fail(%d)", ret);
- ic_value_free(value);
- iotcon_list_free(list);
- return NULL;
- }
- list->type = real->type;
- }
- else if (JSON_NODE_HOLDS_ARRAY(child_node)) {
- if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_LIST != list->type) {
- ERR("Type matching Fail(%d)", list->type);
- iotcon_list_free(list);
- return NULL;
- }
-
- JsonArray *child_array = json_node_get_array(child_node);
- iotcon_list_h parsed_list = ic_list_from_json(child_array);
- if (NULL == parsed_list) {
- ERR("ic_list_from_json() Fail(NULL == parsed_list)");
- iotcon_list_free(list);
- return NULL;
- }
-
- iotcon_value_h value = ic_value_new_list(parsed_list);
- if (NULL == value) {
- ERR("ic_value_new_list(%p) Fail", parsed_list);
- iotcon_list_free(parsed_list);
- iotcon_list_free(list);
- return NULL;
- }
-
- ret = ic_list_insert(list, value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert() Fail(%d)", ret);
- iotcon_list_free(parsed_list);
- iotcon_list_free(list);
- return NULL;
- }
- list->type = IOTCON_TYPE_LIST;
- }
- else if (JSON_NODE_HOLDS_OBJECT(child_node)) {
- if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_REPR != list->type) {
- ERR("Type matching Fail(%d)", list->type);
- iotcon_list_free(list);
- return NULL;
- }
-
- JsonObject *child_obj = json_node_get_object(child_node);
- iotcon_repr_h ret_repr = ic_obj_from_json(child_obj);
- if (NULL == ret_repr) {
- ERR("ic_obj_from_json() Fail(NULL == ret_repr)");
- iotcon_list_free(list);
- return NULL;
- }
-
- iotcon_value_h value = ic_value_new_repr(ret_repr);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", ret_repr);
- iotcon_repr_free(ret_repr);
- iotcon_list_free(list);
- return NULL;
- }
-
- ret = ic_list_insert(list, value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert() Fail(%d)", ret);
- iotcon_repr_free(ret_repr);
- iotcon_list_free(list);
- return NULL;
- }
- list->type = IOTCON_TYPE_REPR;
- }
- }
-
- return list;
-}
-
-API void iotcon_list_free(iotcon_list_h list)
-{
- FN_CALL;
- GList *cur = NULL;
-
- RET_IF(NULL == list);
-
- if (false == _ic_list_dec_ref_count(list))
- return;
-
- cur = list->list;
- while (cur) {
- ic_value_free(cur->data);
- cur = cur->next;
- }
- free(list);
-}
-
-static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list)
-{
- int i, ret, count;
- iotcon_value_h value, copied_value;
-
- count = g_list_length(list->list);
- for (i = 0; i < count; i++) {
- value = _ic_list_get_nth_value(list, i);
- if (NULL == value) {
- ERR("_ic_list_get_nth_value() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- if (list->type != value->type) {
- ERR("Type Mismatching(list:%d, value:%d)", list->type, value->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- copied_value = ic_value_clone(value);
- if (NULL == copied_value) {
- ERR("ic_value_clone() Fail");
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- ret = ic_list_insert(ret_list, copied_value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert() Fail");
- ic_value_free(copied_value);
- return IOTCON_ERROR_REPRESENTATION;
- }
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list)
-{
- int i, ret, count;
-
- iotcon_value_h value;
- iotcon_list_h list_val, copied_list;
-
- count = g_list_length(list->list);
- for (i = 0; i < count; i++) {
- ret = iotcon_list_get_nth_list(list, i, &list_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_get_nth_list() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- copied_list = ic_list_clone(list_val);
- if (NULL == copied_list) {
- ERR("ic_list_clone() Fail");
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- value = ic_value_new_list(copied_list);
- if (NULL == value) {
- ERR("ic_value_new_list(%p) Fail", copied_list);
- iotcon_list_free(copied_list);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- ret = ic_list_insert(ret_list, value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert(%d) Fail", ret);
- ic_value_free(value);
- return IOTCON_ERROR_REPRESENTATION;
- }
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list)
-{
- int i, ret, count;
- iotcon_value_h value;
- iotcon_repr_h repr_val, copied_repr;
-
- count = g_list_length(list->list);
- for (i = 0; i < count; i++) {
- ret = iotcon_list_get_nth_repr(list, i, &repr_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_get_nth_repr() Fail");
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- copied_repr = iotcon_repr_clone(repr_val);
- if (NULL == copied_repr) {
- ERR("_ic_repr_clone_repr() Fail");
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- value = ic_value_new_repr(copied_repr);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", copied_repr);
- iotcon_repr_free(copied_repr);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- ret = ic_list_insert(ret_list, value, -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_list_insert(%d) Fail", ret);
- ic_value_free(value);
- return IOTCON_ERROR_REPRESENTATION;
- }
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-iotcon_list_h ic_list_clone(iotcon_list_h list)
-{
- int ret;
- iotcon_list_h ret_list = NULL;
-
- RETV_IF(NULL == list, NULL);
- RETV_IF(NULL == list->list, NULL);
-
- ret_list = iotcon_list_new(list->type);
- if (NULL == ret_list) {
- ERR("iotcon_list_new(%d) Fail", list->type);
- return NULL;
- }
-
- switch (list->type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- ret = _ic_list_clone_value(list, ret_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_list_clone_value() Fail(%d)", ret);
- iotcon_list_free(ret_list);
- return NULL;
- }
- break;
- case IOTCON_TYPE_LIST:
- ret = _ic_list_clone_list(list, ret_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_list_clone_list() Fail(%d)", ret);
- iotcon_list_free(ret_list);
- return NULL;
- }
- break;
- case IOTCON_TYPE_REPR:
- ret = _ic_list_clone_repr(list, ret_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_list_clone_repr() Fail(%d)", ret);
- iotcon_list_free(ret_list);
- return NULL;
- }
- break;
- default:
- ERR("Invalid type(%d)", list->type);
- iotcon_list_free(ret_list);
- return NULL;
- }
-
- return ret_list;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_REPRESENTATION_LIST_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_LIST_H__
-
-#include <glib.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-
-struct ic_list_s {
- int type;
- int ref_count;
- GList *list;
-};
-
-int ic_list_remove(iotcon_list_h list, iotcon_value_h val);
-int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos);
-
-JsonArray* ic_list_to_json(iotcon_list_h list);
-iotcon_list_h ic_list_from_json(JsonArray *parray);
-
-iotcon_list_h ic_list_clone(iotcon_list_h list);
-
-void ic_list_inc_ref_count(iotcon_list_h val);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_LIST_H__ */
+++ /dev/null
-/* 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 <glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-#include "iotcon-representation.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-repr-list.h"
-#include "ic-repr-value.h"
-#include "ic-repr.h"
-#include "ic-repr-obj.h"
-
-int ic_obj_del_value(iotcon_repr_h repr, const char *key,
- iotcon_types_e value_type)
-{
- gboolean ret = FALSE;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup(%s) Fail", key);
- return IOTCON_ERROR_NO_DATA;
- }
-
- if (value_type != value->type) {
- ERR("Type matching Fail(input:%d, saved:%d)", value_type, value->type);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- ret = g_hash_table_remove(repr->hash_table, key);
- if (FALSE == ret) {
- ERR("g_hash_table_remove(%s) Fail", key);
- return IOTCON_ERROR_NO_DATA;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key, int *val)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- ic_basic_s *real = (ic_basic_s*)value;
- if (IOTCON_TYPE_INT != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *val = real->val.i;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int val)
-{
- iotcon_value_h value;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_int(val);
- if (NULL == value) {
- ERR("ic_value_new_int(%d) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_int(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_INT);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API int iotcon_repr_get_bool(iotcon_repr_h repr, const char *key, bool *val)
-{
- ic_basic_s *real = NULL;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- real = (ic_basic_s*)value;
- if (IOTCON_TYPE_BOOL != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *val = real->val.b;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool val)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_bool(val);
- if (NULL == value) {
- ERR("ic_value_new_bool(%d) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_bool(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_BOOL);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API int iotcon_repr_get_double(iotcon_repr_h repr, const char *key, double *val)
-{
- ic_basic_s *real = NULL;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- real = (ic_basic_s*)value;
- if (IOTCON_TYPE_DOUBLE != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *val = real->val.d;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double val)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_double(val);
- if (NULL == value) {
- ERR("ic_value_new_double(%f) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_double(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_DOUBLE);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API int iotcon_repr_get_str(iotcon_repr_h repr, const char *key, char **val)
-{
- ic_basic_s *real = NULL;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- real = (ic_basic_s*)value;
- if (IOTCON_TYPE_STR != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *val = real->val.s;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_str(val);
- if (NULL == value) {
- ERR("ic_value_new_str(%s) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_str(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_STR);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key)
-{
- ic_basic_s *real = NULL;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, false);
- RETV_IF(NULL == key, false);
-
- value = (iotcon_value_h) g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return false;
- }
-
- real = (ic_basic_s*)value;
-
- return (IOTCON_TYPE_NULL == real->type) ? true : false;
-}
-
-API int iotcon_repr_set_null(iotcon_repr_h repr, const char *key)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_null();
- if (NULL == value) {
- ERR("ic_value_new_null() Fail");
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_null(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_NULL);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API int iotcon_repr_get_list(iotcon_repr_h repr, const char *key, iotcon_list_h *list)
-{
- iotcon_value_h value = NULL;
- ic_val_list_s *real = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- real = (ic_val_list_s*)value;
- if (IOTCON_TYPE_LIST != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *list = real->list;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h list)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_list(list);
- if (NULL == value) {
- ERR("ic_value_new_list() Fail");
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
- ic_list_inc_ref_count(list);
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_list(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_LIST);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_repr_get_repr(iotcon_repr_h src, const char *key, iotcon_repr_h *dest)
-{
- ic_val_repr_s *real = NULL;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(src->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- real = (ic_val_repr_s*)value;
- if (IOTCON_TYPE_REPR != real->type) {
- ERR("Invalid Type(%d)", real->type);
- return IOTCON_ERROR_INVALID_TYPE;
- }
-
- *dest = real->repr;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_repr(iotcon_repr_h repr, const char *key, iotcon_repr_h val)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = ic_value_new_repr(val);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", val);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
- ic_repr_inc_ref_count(val);
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_del_repr(iotcon_repr_h repr, const char *key)
-{
- int ret;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_obj_del_value(repr, key, IOTCON_TYPE_REPR);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_obj_del_value() Fail(%d)", ret);
-
- return ret;
-}
-
-API int iotcon_repr_get_type(iotcon_repr_h repr, const char *key, int *type)
-{
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
-
- value = g_hash_table_lookup(repr->hash_table, key);
- if (NULL == value) {
- ERR("g_hash_table_lookup() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
- *type = value->type;
-
- return IOTCON_ERROR_NONE;
-}
-
-int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
-
- g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
-
- return IOTCON_ERROR_NONE;
-}
-
-static inline int _ic_obj_to_json(const char *key, iotcon_value_h value,
- JsonObject *json_obj)
-{
- FN_CALL;
- int type, ret;
- iotcon_repr_h child_repr = NULL;
- iotcon_list_h child_list = NULL;
-
- JsonObject *child_obj = NULL;
- JsonNode *child_node = NULL;
- JsonArray *child_array = NULL;
-
- RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
-
- type = value->type;
- switch (type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- child_node = ic_value_to_json(value);
- if (NULL == child_node) {
- ERR("ic_value_to_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- json_object_set_member(json_obj, key, child_node);
- break;
- case IOTCON_TYPE_LIST:
- ret = ic_value_get_list(value, &child_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- child_array = ic_list_to_json(child_list);
- if (NULL == child_array) {
- ERR("ic_list_to_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- child_node = json_node_new(JSON_NODE_ARRAY);
- json_node_set_array(child_node, child_array);
- json_object_set_member(json_obj, key, child_node);
- break;
- case IOTCON_TYPE_REPR:
- ret = ic_value_get_repr(value, &child_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_repr() Fail(%d)", ret);
- return IOTCON_ERROR_REPRESENTATION;
- }
-
- child_obj = ic_obj_to_json(child_repr);
- if (NULL == child_obj) {
- ERR("ic_obj_to_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- child_node = json_node_new(JSON_NODE_OBJECT);
- json_node_set_object(child_node, child_obj);
- json_object_set_member(json_obj, key, child_node);
- break;
- default:
- ERR("Invalid type(%d)", type);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-/*
-* A general result : {"rep":{"string":"Hello","intlist":[1,2,3]}}
-*/
-JsonObject* ic_obj_to_json(iotcon_repr_h repr)
-{
- int ret;
- int key_count;
- JsonObject *json_obj = NULL;
- JsonObject *parent_obj = NULL;
-
- GHashTableIter iter;
- gpointer key, value;
-
- RETV_IF(NULL == repr, NULL);
- RETV_IF(NULL == repr->hash_table, NULL);
-
- key_count = iotcon_repr_get_keys_count(repr);
- if (key_count) {
- json_obj = json_object_new();
-
- g_hash_table_iter_init(&iter, repr->hash_table);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- ret = _ic_obj_to_json(key, value, json_obj);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_obj_to_json() Fail(%d)", ret);
- json_object_unref(json_obj);
- return NULL;
- }
- }
- }
-
- parent_obj = json_object_new();
-
- if (json_obj)
- json_object_set_object_member(parent_obj, IOTCON_KEY_REP, json_obj);
-
- return parent_obj;
-}
-
-static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned int index,
- iotcon_repr_h ret_repr)
-{
- char *key;
-
- RETV_IF(NULL == obj, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == key_list, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- key = g_list_nth_data(key_list, index);
-
- /* search child object recursively */
- JsonNode *child_node = json_object_get_member(obj, key);
- if (NULL == child_node) {
- ERR("json_object_get_member() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) {
- iotcon_value_h value = ic_value_from_json(child_node);
- if (NULL == value) {
- ERR("ic_value_from_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- ic_obj_set_value(ret_repr, key, value);
- }
- else if (JSON_NODE_HOLDS_ARRAY(child_node)) {
- iotcon_value_h value;
- JsonArray *child_array = json_node_get_array(child_node);
-
- iotcon_list_h list = ic_list_from_json(child_array);
- if (NULL == list) {
- ERR("ic_list_from_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- value = ic_value_new_list(list);
- if (NULL == value) {
- ERR("ic_value_new_list() Fail");
- iotcon_list_free(list);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- ic_obj_set_value(ret_repr, key, value);
- }
- else if (JSON_NODE_HOLDS_OBJECT(child_node)) {
- iotcon_value_h value;
- JsonObject *child_obj = json_node_get_object(child_node);
-
- iotcon_repr_h repr = ic_obj_from_json(child_obj);
- if (NULL == repr) {
- ERR("ic_obj_from_json() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- value = ic_value_new_repr(repr);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", repr);
- iotcon_repr_free(repr);
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- ic_obj_set_value(ret_repr, key, value);
- }
- else {
- ERR("node type(%d) Fail", json_node_get_node_type(child_node));
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-/*
-* A general input : {"rep:"{"string":"Hello","intlist":[1,2,3]}}
-*/
-iotcon_repr_h ic_obj_from_json(JsonObject *json_repr)
-{
- int ret;
- unsigned int i = 0;
- GList *key_list = NULL;
- iotcon_repr_h repr = NULL;
- JsonObject *obj = NULL;
-
- RETV_IF(NULL == json_repr, NULL);
-
- obj = json_object_get_object_member(json_repr, IOTCON_KEY_REP);
-
- key_list = json_object_get_members(obj);
-
- repr = iotcon_repr_new();
- if (key_list) {
- for (i = 0; i < g_list_length(key_list); i++) {
- ret = _ic_obj_from_json(obj, key_list, i, repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_ic_obj_from_json() Fail(%d)", ret);
- iotcon_repr_free(repr);
- g_list_free(key_list);
- return NULL;
- }
- }
- g_list_free(key_list);
- }
-
- return repr;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_REPRESENTATION_OBJECT_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_OBJECT_H__
-
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-
-int ic_obj_del_value(iotcon_repr_h repr, const char *key,
- iotcon_types_e value_type);
-
-int ic_repr_obj_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval);
-int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value);
-
-JsonObject* ic_obj_to_json(iotcon_repr_h repr);
-iotcon_repr_h ic_obj_from_json(JsonObject *obj);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_OBJECT_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <errno.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-representation.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-repr.h"
-#include "ic-repr-list.h"
-#include "ic-repr-value.h"
-
-static iotcon_value_h _ic_value_new(int type)
-{
- iotcon_value_h ret_val;
-
- switch (type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- ret_val = calloc(1, sizeof(ic_basic_s));
- break;
- case IOTCON_TYPE_LIST:
- ret_val = calloc(1, sizeof(ic_val_list_s));
- break;
- case IOTCON_TYPE_REPR:
- ret_val = calloc(1, sizeof(ic_val_repr_s));
- break;
- default:
- ERR("Invalid Type(%d)", type);
- return NULL;
- }
-
- if (NULL == ret_val) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- ret_val->type = type;
-
- return ret_val;
-}
-
-
-iotcon_value_h ic_value_new_null()
-{
- iotcon_value_h value;
-
- value = _ic_value_new(IOTCON_TYPE_NULL);
- if (NULL == value) {
- ERR("_ic_value_new(NULL) Fail");
- return NULL;
- }
-
- return value;
-}
-
-iotcon_value_h ic_value_new_int(int val)
-{
- ic_basic_s *value;
-
- value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_INT);
- if (NULL == value) {
- ERR("_ic_value_new(INT:%d) Fail", val);
- return NULL;
- }
-
- value->val.i = val;
-
- return (iotcon_value_h)value;
-}
-
-iotcon_value_h ic_value_new_bool(bool val)
-{
- ic_basic_s *value;
-
- value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_BOOL);
- if (NULL == value) {
- ERR("_ic_value_new(BOOL:%d) Fail", val);
- return NULL;
- }
-
- value->val.b = val;
-
- return (iotcon_value_h)value;
-}
-
-iotcon_value_h ic_value_new_double(double val)
-{
- ic_basic_s *value;
-
- value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_DOUBLE);
- if (NULL == value) {
- ERR("_ic_value_new(DOUBLE:%f) Fail", val);
- return NULL;
- }
-
- value->val.d = val;
-
- return (iotcon_value_h)value;
-}
-
-iotcon_value_h ic_value_new_str(char *val)
-{
- ic_basic_s *value;
-
- RETV_IF(NULL == val, NULL);
-
- value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_STR);
- if (NULL == value) {
- ERR("_ic_value_new(STR:%s) Fail", val);
- return NULL;
- }
-
- value->val.s = ic_utils_strdup(val);
-
- return (iotcon_value_h)value;
-}
-
-
-iotcon_value_h ic_value_new_list(iotcon_list_h val)
-{
- ic_val_list_s *value;
-
- value = (ic_val_list_s*)_ic_value_new(IOTCON_TYPE_LIST);
- if (NULL == value) {
- ERR("_ic_value_new(LIST) Fail");
- return NULL;
- }
-
- value->list = val;
-
- return (iotcon_value_h)value;
-}
-
-iotcon_value_h ic_value_new_repr(iotcon_repr_h val)
-{
- ic_val_repr_s *value;
-
- value = (ic_val_repr_s*)_ic_value_new(IOTCON_TYPE_REPR);
- if (NULL == value) {
- ERR("_ic_value_new(REPR) Fail");
- return NULL;
- }
-
- value->repr = val;
-
- return (iotcon_value_h)value;
-}
-
-int ic_value_get_int(iotcon_value_h value, int *val)
-{
- ic_basic_s *real = (ic_basic_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *val = real->val.i;
-
- return IOTCON_ERROR_NONE;
-}
-
-int ic_value_get_bool(iotcon_value_h value, bool *val)
-{
- ic_basic_s *real = (ic_basic_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *val = real->val.b;
-
- return IOTCON_ERROR_NONE;
-}
-
-int ic_value_get_double(iotcon_value_h value, double *val)
-{
- ic_basic_s *real = (ic_basic_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *val = real->val.d;
-
- return IOTCON_ERROR_NONE;
-}
-
-int ic_value_get_str(iotcon_value_h value, const char **val)
-{
- ic_basic_s *real = (ic_basic_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *val = real->val.s;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list)
-{
- ic_val_list_s *real = (ic_val_list_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *list = real->list;
-
- return IOTCON_ERROR_NONE;
-}
-
-int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr)
-{
- ic_val_repr_s *real = (ic_val_repr_s*)value;
-
- RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERROR_INVALID_PARAMETER,
- "Invalid Type(%d)", real->type);
-
- *repr = real->repr;
-
- return IOTCON_ERROR_NONE;
-}
-
-/*
-* A general result : 1
-* : true
-* : 5.5
-* : "Hello"
-*/
-JsonNode* ic_value_to_json(iotcon_value_h value)
-{
- JsonNode *node;
- ic_basic_s *real = (ic_basic_s*)value;
-
- RETV_IF(NULL == value, NULL);
-
- if (IOTCON_TYPE_NULL == value->type)
- node = json_node_new(JSON_NODE_NULL);
- else
- node = json_node_new(JSON_NODE_VALUE);
-
- if (NULL == node) {
- ERR("json_node_new(%d) Fail", value->type);
- return NULL;
- }
-
- switch (value->type) {
- case IOTCON_TYPE_INT:
- json_node_set_int(node, real->val.i);
- break;
- case IOTCON_TYPE_BOOL:
- json_node_set_boolean(node, real->val.b);
- break;
- case IOTCON_TYPE_DOUBLE:
- json_node_set_double(node, real->val.d);
- break;
- case IOTCON_TYPE_STR:
- json_node_set_string(node, real->val.s);
- break;
- case IOTCON_TYPE_NULL:
- break;
- default:
- ERR("Invalid type(%d)", value->type);
- break;
- }
-
- return node;
-}
-
-/*
-* A general result : 1
-* : true
-* : 5.5
-* : "Hello"
-*/
-API iotcon_value_h ic_value_from_json(JsonNode *node)
-{
- gint64 ival64;
- GType gtype = 0;
- iotcon_value_h value = NULL;
-
- RETV_IF(NULL == node, NULL);
-
- if (JSON_NODE_HOLDS_NULL(node)) {
- value = ic_value_new_null();
- if (NULL == value)
- ERR("ic_value_new_null() Fail");
- return value;
- }
-
- gtype = json_node_get_value_type(node);
- switch (gtype) {
- case G_TYPE_INT64:
- ival64 = json_node_get_int(node);
- if (INT_MAX < ival64 || ival64 < INT_MIN) {
- ERR("value SHOULD NOT exceeds the integer range. ival64(%lld)", ival64);
- return NULL;
- }
- value = ic_value_new_int(ival64);
- if (NULL == value)
- ERR("ic_value_new_int(%ll) Fail", ival64);
- break;
- case G_TYPE_BOOLEAN:
- value = ic_value_new_bool(json_node_get_boolean(node));
- if (NULL == value)
- ERR("ic_value_new_bool() Fail");
- break;
- case G_TYPE_DOUBLE:
- value = ic_value_new_double(json_node_get_double(node));
- if (NULL == value)
- ERR("ic_value_new_double() Fail");
- break;
- case G_TYPE_STRING:
- value = ic_value_new_str(ic_utils_strdup(json_node_get_string(node)));
- if (NULL == value)
- ERR("ic_value_new_str() Fail");
- break;
- default:
- ERR("Invalid type(%d)", gtype);
- break;
- }
-
- return value;
-}
-
-void ic_value_free(gpointer data)
-{
- FN_CALL;
- int ret;
- const char *str;
- iotcon_value_h value;
- iotcon_list_h list;
- iotcon_repr_h repr;
-
- RET_IF(NULL == data);
-
- value = data;
-
- int type = value->type;
- switch (type) {
- case IOTCON_TYPE_STR:
- ret = ic_value_get_str(value, &str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_str() Fail(%d)", ret);
- break;
- }
- free((char*)str);
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_NULL:
- break;
- case IOTCON_TYPE_LIST:
- DBG("value is list");
- ret = ic_value_get_list(value, &list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- break;
- }
- iotcon_list_free(list);
- break;
- case IOTCON_TYPE_REPR:
- DBG("value is Repr");
- ret = ic_value_get_repr(value, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_repr() Fail(%d)", ret);
- break;
- }
- iotcon_repr_free(repr);
- break;
- default:
- ERR("Invalid type(%d)", type);
- break;
- }
- free(value);
-}
-
-
-iotcon_value_h ic_value_clone(iotcon_value_h src)
-{
- iotcon_value_h dest = NULL;
- ic_basic_s *real = (ic_basic_s*)src;
-
- RETV_IF(NULL == src, NULL);
-
- switch (src->type) {
- case IOTCON_TYPE_INT:
- dest = ic_value_new_int(real->val.i);
- break;
- case IOTCON_TYPE_BOOL:
- dest = ic_value_new_bool(real->val.b);
- break;
- case IOTCON_TYPE_DOUBLE:
- dest = ic_value_new_double(real->val.d);
- break;
- case IOTCON_TYPE_STR:
- dest = ic_value_new_str(ic_utils_strdup(real->val.s));
- break;
- case IOTCON_TYPE_NULL:
- dest = ic_value_new_null();
- break;
- default:
- ERR("Invalid type(%d)", src->type);
- break;
- }
-
- if (NULL == dest)
- ERR("ic_value_new_xxx(%d) Fail", src->type);
-
- return dest;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_REPRESENTATION_VALUE_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_VALUE_H__
-
-#include <stdbool.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-
-struct ic_value_s {
- int type;
-};
-
-typedef struct {
- int type;
- union {
- int i;
- bool b;
- double d;
- char *s;
- } val;
-} ic_basic_s;
-
-typedef struct {
- int type;
- struct ic_list_s *list;
-} ic_val_list_s;
-
-typedef struct {
- int type;
- struct ic_repr_s *repr;
-} ic_val_repr_s;
-
-iotcon_value_h ic_value_new_null();
-iotcon_value_h ic_value_new_int(int val);
-iotcon_value_h ic_value_new_bool(bool val);
-iotcon_value_h ic_value_new_double(double val);
-iotcon_value_h ic_value_new_str(char *val);
-iotcon_value_h ic_value_new_list(iotcon_list_h val);
-iotcon_value_h ic_value_new_repr(iotcon_repr_h val);
-
-
-int ic_value_get_int(iotcon_value_h value, int *val);
-int ic_value_get_bool(iotcon_value_h value, bool *val);
-int ic_value_get_double(iotcon_value_h value, double *val);
-int ic_value_get_str(iotcon_value_h value, const char **val);
-int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list);
-int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr);
-
-
-JsonNode* ic_value_to_json(iotcon_value_h value);
-iotcon_value_h ic_value_from_json(JsonNode *node);
-void ic_value_free(gpointer data);
-
-iotcon_value_h ic_value_clone(iotcon_value_h src);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_VALUE_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <limits.h>
-#include <glib.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-representation.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-resource-types.h"
-#include "ic-ioty.h"
-#include "ic.h"
-#include "ic-repr-list.h"
-#include "ic-repr-value.h"
-#include "ic-repr-obj.h"
-#include "ic-repr.h"
-
-void ic_repr_inc_ref_count(iotcon_repr_h val)
-{
- RET_IF(NULL == val);
- RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
-
- val->ref_count++;
-}
-
-static bool _ic_repr_dec_ref_count(iotcon_repr_h val)
-{
- bool ret;
-
- RETV_IF(NULL == val, -1);
- RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
-
- val->ref_count--;
- if (0 == val->ref_count)
- ret = true;
- else
- ret = false;
-
- return ret;
-}
-
-API iotcon_repr_h iotcon_repr_new()
-{
- iotcon_repr_h ret_val;
- errno = 0;
-
- ret_val = calloc(1, sizeof(struct ic_repr_s));
- if (NULL == ret_val) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- ret_val->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, free,
- ic_value_free);
- ic_repr_inc_ref_count(ret_val);
-
- return ret_val;
-}
-
-API int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
-
- *uri = repr->uri;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- free(repr->uri);
- repr->uri = NULL;
-
- if (NULL == uri)
- return IOTCON_ERROR_NONE;
-
- repr->uri = strdup(uri);
- if (NULL == repr->uri) {
- ERR("strdup() Fail");
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resource_types_h *types)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
-
- *types = repr->res_types;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_set_resource_types(iotcon_repr_h repr, iotcon_resource_types_h types)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- iotcon_resource_types_free(repr->res_types);
- repr->res_types = NULL;
-
- if (types)
- repr->res_types = ic_resource_types_ref(types);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_get_resource_interfaces(iotcon_repr_h repr)
-{
- RETV_IF(NULL == repr, IOTCON_INTERFACE_NONE);
-
- return repr->interfaces;
-}
-
-API int iotcon_repr_set_resource_interfaces(iotcon_repr_h repr, int ifaces)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- RETV_IF(ifaces <= IOTCON_INTERFACE_NONE || IC_INTERFACE_MAX < ifaces,
- IOTCON_ERROR_INVALID_PARAMETER);
-
- repr->interfaces = ifaces;
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child)
-{
- RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
-
- ic_repr_inc_ref_count(child);
- parent->children = g_list_append(parent->children, child);
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_foreach_children(iotcon_repr_h parent, iotcon_children_fn fn,
- void *user_data)
-{
- GList *list, *next;
-
- RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- list = parent->children;
- while (list) {
- next = list->next;
- if (IOTCON_FUNC_STOP == fn(list->data, user_data))
- break;
- list = next;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API unsigned int iotcon_repr_get_children_count(iotcon_repr_h parent)
-{
- RETV_IF(NULL == parent, 0);
- RETV_IF(NULL == parent->children, 0);
-
- return g_list_length(parent->children);
-}
-
-API int iotcon_repr_get_nth_child(iotcon_repr_h parent, int pos, iotcon_repr_h *child)
-{
- RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
-
- *child = g_list_nth_data(parent->children, pos);
- if (NULL == *child) {
- ERR("g_list_nth_data() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_foreach_keys(iotcon_repr_h repr, iotcon_repr_key_fn fn,
- void *user_data)
-{
- GHashTableIter iter;
- gpointer key, value;
-
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
-
- g_hash_table_iter_init(&iter, repr->hash_table);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- if (IOTCON_FUNC_STOP == fn(key, value, user_data))
- break;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-API int iotcon_repr_get_keys_count(iotcon_repr_h repr)
-{
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == repr->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
-
- return g_hash_table_size(repr->hash_table);
-}
-
-static int _ic_repr_get_res_type_fn(const char *res_type, void *user_data)
-{
- JsonArray *rt_array = user_data;
- json_array_add_string_element(rt_array, res_type);
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-/*
- * A general result : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
- */
-static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr,
- unsigned int child_index)
-{
- int i, ret, ifaces;
- char *iface_str;
- const char *uri;
- JsonObject *repr_obj = NULL;
- unsigned int rt_count = 0;
- JsonObject *prop_obj = NULL;
- iotcon_resource_types_h resource_types = NULL;
-
- RETV_IF(NULL == cur_repr, NULL);
-
- if (0 < iotcon_repr_get_keys_count(cur_repr)) {
- repr_obj = ic_obj_to_json(cur_repr);
- if (NULL == repr_obj) {
- ERR("ic_obj_to_json() Fail");
- json_object_unref(repr_obj);
- return NULL;
- }
- }
- else {
- repr_obj = json_object_new();
- }
-
- if (cur_repr->uri) {
- iotcon_repr_get_uri(cur_repr, &uri);
- json_object_set_string_member(repr_obj, IOTCON_KEY_URI, uri);
- }
-
- if (cur_repr->res_types)
- rt_count = ic_resource_types_get_length(cur_repr->res_types);
-
- if (0 < rt_count || IOTCON_INTERFACE_NONE != cur_repr->interfaces) {
- prop_obj = json_object_new();
- json_object_set_object_member(repr_obj, IOTCON_KEY_PROPERTY, prop_obj);
- }
-
- if (0 < rt_count) {
- JsonArray *rt_array = json_array_new();
-
- ret = iotcon_repr_get_resource_types(cur_repr, &resource_types);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_repr_get_resource_types() Fail(%d)", ret);
- json_object_unref(repr_obj);
- return NULL;
- }
-
- ret = iotcon_resource_types_foreach(resource_types, _ic_repr_get_res_type_fn,
- rt_array);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_foreach() Fail");
- json_object_unref(repr_obj);
- return NULL;
- }
- json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array);
- }
-
- if (IOTCON_INTERFACE_NONE != cur_repr->interfaces) {
- JsonArray *if_array = json_array_new();
- ifaces = iotcon_repr_get_resource_interfaces(cur_repr);
- for (i = 1; i <= IC_INTERFACE_MAX; i = i << 1) {
- if (IOTCON_INTERFACE_NONE == (ifaces & i)) /* this interface not exist */
- continue;
- ret = ic_ioty_convert_interface_flag((ifaces & i), &iface_str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_ioty_convert_interface_flag(%d) Fail(%d)", i, ret);
- json_object_unref(repr_obj);
- json_array_unref(if_array);
- return NULL;
- }
- json_array_add_string_element(if_array, iface_str);
- }
-
- json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array);
- }
-
- FN_END;
- return repr_obj;
-}
-
-static JsonObject* _ic_repr_data_generate_parent(iotcon_repr_h cur_repr,
- unsigned int child_index)
-{
- FN_CALL;
- JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index);
- if (NULL == obj) {
- ERR("_ic_repr_data_generate_json() Fail");
- return NULL;
- }
-
- return obj;
-}
-
-static JsonObject* _ic_repr_data_generate_child(iotcon_repr_h cur_repr,
- unsigned int child_index)
-{
- FN_CALL;
- JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index);
- if (NULL == obj) {
- ERR("_ic_repr_data_generate_json() Fail");
- return NULL;
- }
-
- return obj;
-}
-
-/*
- * A general result : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}},
- * {"href":"/a/child","rep":{"string":"World","double_val":5.7},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]}
- */
-static JsonObject* _ic_repr_generate_json(iotcon_repr_h repr)
-{
- JsonObject *repr_obj = NULL;
- JsonObject *root_obj = NULL;
- JsonArray *root_array = NULL;
- unsigned int child_count = 0;
- unsigned int child_index = 0;
- iotcon_repr_h child_repr = NULL;
-
- RETV_IF(NULL == repr, NULL);
-
- root_obj = json_object_new();
- root_array = json_array_new();
-
- if (repr->children) {
- child_count = iotcon_repr_get_children_count(repr);
- }
-
- repr_obj = _ic_repr_data_generate_parent(repr, child_index);
- if (NULL == repr_obj) {
- ERR("_ic_repr_data_generate_parent() Fali(NULL == repr_obj)");
- json_object_unref(root_obj);
- json_array_unref(root_array);
- return NULL;
- }
- json_array_add_object_element(root_array, repr_obj);
-
- for (child_index = 0; child_index < child_count; child_index++) {
- iotcon_repr_get_nth_child(repr, child_index, &child_repr);
- repr_obj = _ic_repr_data_generate_child(child_repr, child_index);
- if (NULL == repr_obj) {
- ERR("_ic_repr_data_generate_child() Fali(NULL == repr_obj)");
- json_object_unref(root_obj);
- json_array_unref(root_array);
- return NULL;
- }
- json_array_add_object_element(root_array, repr_obj);
- }
-
- json_object_set_array_member(root_obj, IOTCON_KEY_OC, root_array);
-
- return root_obj;
-}
-
-char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty)
-{
- JsonNode *root_node = NULL;
- char *json_data = NULL;
-
- JsonObject *obj = _ic_repr_generate_json(repr);
- if (NULL == obj) {
- ERR("ic_repr_generate_json() Fail");
- return NULL;
- }
-
- JsonGenerator *gen = json_generator_new();
-#if JSON_CHECK_VERSION(0,14,0)
- json_generator_set_pretty(gen, set_pretty);
-#endif
-
- root_node = json_node_new(JSON_NODE_OBJECT);
- json_node_set_object(root_node, obj);
- json_generator_set_root(gen, root_node);
-
- json_data = json_generator_to_data(gen, NULL);
- DBG("result : %s", json_data);
-
- return json_data;
-}
-
-/*
- * A general input : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
- * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
- */
-iotcon_repr_h ic_repr_parse_json(const char *json_string)
-{
- const char *iface_str = NULL;
- const char *rtype_str = NULL;
- const char *uri_value = NULL;
- iotcon_interface_e iface_flag;
-
- RETV_IF(NULL == json_string, NULL);
-
- DBG("input str : %s", json_string);
-
- GError *error = NULL;
- gboolean ret = FALSE;
- JsonParser *parser = json_parser_new();
- ret = json_parser_load_from_data(parser, json_string, strlen(json_string), &error);
- if (FALSE == ret) {
- ERR("json_parser_load_from_data() Fail(%s)", error->message);
- g_error_free(error);
- g_object_unref(parser);
- return NULL;
- }
-
- JsonObject *root_obj = json_node_get_object(json_parser_get_root(parser));
-
- iotcon_repr_h repr = NULL;
- if (json_object_has_member(root_obj, IOTCON_KEY_REP)) {
- repr = ic_obj_from_json(root_obj);
- if (NULL == repr) {
- ERR("ic_obj_from_json() Fail()");
- g_object_unref(parser);
- return NULL;
- }
- }
- else {
- repr = iotcon_repr_new();
- }
-
- if (json_object_has_member(root_obj, IOTCON_KEY_URI)) {
- uri_value = json_object_get_string_member(root_obj, IOTCON_KEY_URI);
- iotcon_repr_set_uri(repr, uri_value);
- }
-
- if (json_object_has_member(root_obj, IOTCON_KEY_PROPERTY)) {
- JsonObject *property_obj = json_object_get_object_member(root_obj,
- IOTCON_KEY_PROPERTY);
-
- if (json_object_has_member(property_obj, IOTCON_KEY_RESOURCETYPES)) {
- iotcon_resource_types_h res_types = NULL;
- JsonArray *rt_array = json_object_get_array_member(property_obj,
- IOTCON_KEY_RESOURCETYPES);
- unsigned int rt_index = 0;
- unsigned int rt_count = json_array_get_length(rt_array);
-
- if (0 < rt_count) {
- res_types = iotcon_resource_types_new();
- if (NULL == res_types) {
- ERR("iotcon_resource_types_new() Fail");
- iotcon_repr_free(repr);
- g_object_unref(parser);
- return NULL;
- }
-
- for (rt_index = 0; rt_index < rt_count; rt_index++) {
- rtype_str = json_array_get_string_element(rt_array, rt_index);
- iotcon_resource_types_insert(res_types, rtype_str);
- }
- iotcon_repr_set_resource_types(repr, res_types);
- }
- }
- if (json_object_has_member(property_obj, IOTCON_KEY_INTERFACES)) {
- JsonArray *if_array = json_object_get_array_member(property_obj,
- IOTCON_KEY_INTERFACES);
- unsigned int if_count = json_array_get_length(if_array);
- unsigned int if_index;
- int ifaces = IOTCON_INTERFACE_NONE;
-
- for (if_index = 0; if_index < if_count; if_index++) {
- iface_str = json_array_get_string_element(if_array, if_index);
- ret = ic_ioty_convert_interface_string(iface_str, &iface_flag);
- ifaces |= iface_flag;
- }
- iotcon_repr_set_resource_interfaces(repr, ifaces);
-
- }
- }
-
- if (NULL == repr) {
- ERR("repr is NULL");
- g_object_unref(parser);
- return NULL;
- }
-
- g_object_unref(parser);
-
- FN_END;
-
- return repr;
-}
-
-
-API void iotcon_repr_free(iotcon_repr_h repr)
-{
- FN_CALL;
- RET_IF(NULL == repr);
-
- if (false == _ic_repr_dec_ref_count(repr))
- return;
-
- free(repr->uri);
-
- /* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */
- g_list_free_full(repr->children, (GDestroyNotify)iotcon_repr_free);
-
- /* null COULD be allowed */
- if (repr->res_types)
- iotcon_resource_types_free(repr->res_types);
- g_hash_table_destroy(repr->hash_table);
- free(repr);
-
- FN_END;
-}
-
-static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h dest_repr)
-{
- FN_CALL;
- int type, ret;
- char *dup_key;
- iotcon_value_h value, copied_val;
- iotcon_list_h child_list, copied_list;
- iotcon_repr_h child_repr, copied_repr;
-
- type = src_val->type;
- switch (type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- copied_val = ic_value_clone(src_val);
- if (NULL == copied_val) {
- ERR("ic_value_clone() Fail");
- return;
- }
-
- dup_key = ic_utils_strdup(key);
- if (NULL == dup_key) {
- ERR("dupic_utils_strdup() Fail");
- return;
- }
-
- ic_obj_set_value(dest_repr, dup_key, copied_val);
- break;
- case IOTCON_TYPE_LIST:
- ret = ic_value_get_list(src_val, &child_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_list() Fail(%d)", ret);
- return;
- }
-
- copied_list = ic_list_clone(child_list);
- if (NULL == copied_list) {
- ERR("ic_list_clone() Fail");
- return;
- }
-
- value = ic_value_new_list(copied_list);
- if (NULL == value) {
- ERR("ic_value_new_list() Fail");
- iotcon_list_free(copied_list);
- return;
- }
-
- ic_obj_set_value(dest_repr, key, value);
- break;
- case IOTCON_TYPE_REPR:
- ret = ic_value_get_repr(src_val, &child_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("ic_value_get_repr() Fail(%d)", ret);
- return;
- }
-
- copied_repr = iotcon_repr_clone(child_repr);
- if (NULL == copied_repr) {
- ERR("ic_list_clone() Fail");
- return;
- }
-
- value = ic_value_new_repr(copied_repr);
- if (NULL == value) {
- ERR("ic_value_new_repr(%p) Fail", copied_repr);
- return;
- }
-
- ic_obj_set_value(dest_repr, key, value);
- break;
- default:
- ERR("Invalid type(%d)", type);
- return;
- }
-}
-
-API iotcon_repr_h iotcon_repr_clone(const iotcon_repr_h src)
-{
- FN_CALL;
- GList *node;
- iotcon_repr_h dest, copied_repr;
- iotcon_resource_types_h list;
-
- RETV_IF(NULL == src, NULL);
-
- dest = iotcon_repr_new();
- if (NULL == dest) {
- ERR("iotcon_repr_new() Fail");
- return NULL;
- }
-
- if (src->uri) {
- dest->uri = strdup(src->uri);
- if (NULL == dest->uri) {
- ERR("strdup() Fail");
- iotcon_repr_free(dest);
- return NULL;
- }
- }
-
- if (src->interfaces)
- dest->interfaces = src->interfaces;
-
- if (src->res_types) {
- list = iotcon_resource_types_clone(src->res_types);
- if (NULL == list) {
- ERR("iotcon_resource_types_clone() Fail");
- iotcon_repr_free(dest);
- return NULL;
- }
- dest->res_types = list;
- }
-
- for (node = g_list_first(src->children); node; node = node->next) {
- copied_repr = iotcon_repr_clone((iotcon_repr_h)node->data);
- if (NULL == copied_repr) {
- ERR("iotcon_repr_clone(child) Fail");
- iotcon_repr_free(dest);
- return NULL;
- }
- dest->children = g_list_append(dest->children, copied_repr);
- }
-
- g_hash_table_foreach(src->hash_table, (GHFunc)_ic_repr_obj_clone, dest);
-
- return dest;
-}
-
-API char* iotcon_repr_generate_json(iotcon_repr_h repr)
-{
- return ic_repr_generate_json(repr, TRUE);
-}
+++ /dev/null
-/*
- * 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_INTERNAL_REPRESENTATION_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_H__
-
-#include <stdbool.h>
-#include <glib.h>
-#include <json-glib/json-glib.h>
-
-#include "iotcon-struct.h"
-
-#define IOTCON_KEY_OC "oc"
-#define IOTCON_KEY_URI "href"
-#define IOTCON_KEY_RESOURCETYPES "rt"
-#define IOTCON_KEY_INTERFACES "if"
-#define IOTCON_KEY_PROPERTY "prop"
-#define IOTCON_KEY_REP "rep"
-
-struct ic_repr_s {
- char *uri;
- int ref_count;
- int interfaces;
- GHashTable *hash_table;
- GList *children;
- iotcon_resource_types_h res_types;
-};
-
-/**
- * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
- * @brief Generates JSON string from Representation.
- * @since_tizen 3.0
- * @remarks The returned string must be released with free() by you.
- *
- * @param[in] repr The handle to the Representation
- * @param[in] set_pretty Whether the generated JSON string should be pretty printed
- *
- * @return Generated JSON string, otherwise a null pointer if a parse error
- */
-char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty);
-
-iotcon_repr_h ic_repr_parse_json(const char *json_string);
-
-void ic_repr_inc_ref_count(iotcon_repr_h val);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_H__ */
+++ /dev/null
-/*
- * 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 "iotcon-struct.h"
-#include "iotcon-constant.h"
-#include "ic-common.h"
-#include "ic-request.h"
-
-/* The content of the request should not be freed by user. */
-API int iotcon_request_get_uri(iotcon_request_h request, char **uri)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
-
- *uri = request->uri;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the request should not be freed by user. */
-API int iotcon_request_get_representation(iotcon_request_h request, iotcon_repr_h *repr)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- *repr = request->repr;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_request_get_types(iotcon_request_h request, int *types)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
-
- *types = request->types;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the request should not be freed by user. */
-API int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
-
- *options = request->header_options;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the request should not be freed by user. */
-API int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
-
- *query = request->query;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_request_get_observer_action(iotcon_request_h request,
- iotcon_observe_action_e *action)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == action, IOTCON_ERROR_INVALID_PARAMETER);
-
- *action = request->observation_info.action;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_request_get_observer_id(iotcon_request_h request, int *observer_id)
-{
- RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == observer_id, IOTCON_ERROR_INVALID_PARAMETER);
-
- *observer_id = request->observation_info.observer_id;
-
- return IOTCON_ERROR_NONE;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_REQUEST_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REQUEST_H__
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-
-typedef void* oc_request_h;
-typedef void* oc_resource_h;
-
-struct ic_observe_info {
- iotcon_observe_action_e action;
- int observer_id;
-};
-
-struct ic_resource_request {
- int types;
- char *uri;
- iotcon_options_h header_options;
- iotcon_query_h query;
- int request_handler_flag;
- struct ic_observe_info observation_info;
- iotcon_repr_h repr;
- oc_request_h request_handle;
- oc_resource_h resource_handle;
-};
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REQUEST_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-resource-types.h"
-
-iotcon_resource_types_h ic_resource_types_ref(iotcon_resource_types_h types)
-{
- RETV_IF(NULL == types, NULL);
- RETV_IF(types->ref_count <= 0, NULL);
-
- types->ref_count++;
-
- return types;
-}
-
-
-API iotcon_resource_types_h iotcon_resource_types_new()
-{
- iotcon_resource_types_h types = calloc(1, sizeof(struct ic_resource_types));
- if (NULL == types) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- types->ref_count = 1;
-
- return types;
-}
-
-
-API void iotcon_resource_types_free(iotcon_resource_types_h types)
-{
- RET_IF(NULL == types);
-
- types->ref_count--;
-
- if (0 == types->ref_count) {
- g_list_free_full(types->type_list, free);
- free(types);
- }
-}
-
-
-static int _ic_resource_types_strcmp(const void *a, const void *b)
-{
- return strcmp(a, b);
-}
-
-
-static bool _ic_resource_types_duplicate_check(iotcon_resource_types_h types,
- const char *type)
-{
- GList *ret = NULL;
-
- RETV_IF(NULL == types, false);
- RETV_IF(NULL == type, false);
-
- ret = g_list_find_custom(types->type_list, type, _ic_resource_types_strcmp);
- if (NULL == ret)
- return false;
-
- return true;
-}
-
-
-/* The length of resource type should be less than or equal to 61.
- * Duplicate strings are not allowed. */
-API int iotcon_resource_types_insert(iotcon_resource_types_h types, const char *type)
-{
- char *resource_type;
-
- RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(1 < types->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
- "Don't modify it. It is already set.");
-
- if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(type)) {
- ERR("The length of type(%s) should be less than or equal to %d.", type,
- IOTCON_RESOURCE_TYPE_LENGTH_MAX);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- if (true == _ic_resource_types_duplicate_check(types, type)) {
- ERR("%s is already contained.", type);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- resource_type = strdup(type);
- if (NULL == resource_type) {
- ERR("strdup() Fail");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- types->type_list = g_list_append(types->type_list, resource_type);
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_resource_types_delete(iotcon_resource_types_h types, const char *type)
-{
- GList *found_node;
-
- RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
- RETVM_IF(1 < types->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
- "Don't modify it. It is already set.");
-
- found_node = g_list_find_custom(types->type_list, type, _ic_resource_types_strcmp);
- if (NULL == found_node) {
- ERR("g_list_find_custom() Fail");
- return IOTCON_ERROR_NO_DATA;
- }
-
- free(found_node->data);
- types->type_list = g_list_delete_link(types->type_list, found_node);
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_resource_types_foreach(iotcon_resource_types_h types,
- iotcon_resource_types_foreach_cb cb, void *user_data)
-{
- GList *node;
-
- RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- for (node = types->type_list; node; node = node->next) {
- if (IOTCON_FUNC_STOP == cb((const char*)node->data, user_data))
- break;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API iotcon_resource_types_h iotcon_resource_types_clone(iotcon_resource_types_h types)
-{
- GList * node;
- char *resource_type;
- iotcon_resource_types_h clone;
-
- RETV_IF(NULL == types, NULL);
-
- clone = calloc(1, sizeof(struct ic_resource_types));
- if (NULL == clone) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- for (node = types->type_list; node; node = node->next) {
- resource_type = ic_utils_strdup(node->data);
- if (NULL == resource_type) {
- iotcon_resource_types_free(clone);
- ERR("ic_utils_strdup() Fail");
- return NULL;
- }
- clone->type_list = g_list_append(clone->type_list, resource_type);
- }
-
- clone->ref_count = 1;
-
- return clone;
-}
-
-
-/* counting from 0 */
-const char* ic_resource_types_get_nth_data(iotcon_resource_types_h types, int index)
-{
- return g_list_nth_data(types->type_list, index);
-}
-
-
-unsigned int ic_resource_types_get_length(iotcon_resource_types_h types)
-{
- return g_list_length(types->type_list);
-}
+++ /dev/null
-/*
- * 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_INTERNAL_RESOURCE_TYPES_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_RESOURCE_TYPES_H__
-
-#include "iotcon-struct.h"
-
-struct ic_resource_types {
- int ref_count;
- GList *type_list;
-};
-
-iotcon_resource_types_h ic_resource_types_ref(iotcon_resource_types_h res_types);
-const char* ic_resource_types_get_nth_data(iotcon_resource_types_h res_types, int index);
-unsigned int ic_resource_types_get_length(iotcon_resource_types_h res_types);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_RESOURCE_TYPES_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "iotcon.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-ioty.h"
-#include "ic-repr.h"
-#include "ic-options.h"
-#include "ic-request.h"
-#include "ic-response.h"
-
-API iotcon_response_h iotcon_response_new(iotcon_request_h request_h)
-{
- FN_CALL;
-
- RETV_IF(NULL == request_h, NULL);
-
- iotcon_response_h resp = calloc(1, sizeof(struct ic_resource_response));
- if (NULL == resp) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- resp->request_handle = request_h->request_handle;
- resp->resource_handle = request_h->resource_handle;
- resp->error_code = 200;
-
- return resp;
-}
-
-
-API void iotcon_response_free(iotcon_response_h resp)
-{
- FN_CALL;
-
- RET_IF(NULL == resp);
-
- if (resp->repr)
- iotcon_repr_free(resp->repr);
- if (resp->new_uri)
- free(resp->new_uri);
- if (resp->header_options)
- ic_options_free(resp->header_options);
- free(resp);
-}
-
-
-API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e prop, ...)
-{
- int value;
- va_list args;
- char *new_resource_uri = NULL;
- iotcon_options_h options = NULL;
-
- va_start(args, prop);
-
- switch (prop) {
- case IOTCON_RESPONSE_INTERFACE:
- resp->iface = va_arg(args, int);
- break;
- case IOTCON_RESPONSE_REPRESENTATION:
- resp->repr = va_arg(args, iotcon_repr_h);
- ic_repr_inc_ref_count(resp->repr);
- break;
- case IOTCON_RESPONSE_RESULT:
- value = va_arg(args, int);
- if (value < IOTCON_RESPONSE_RESULT_OK || IOTCON_RESPONSE_RESULT_MAX <= value) {
- ERR("Invalid value(%d)", value);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- resp->result = value;
- break;
- case IOTCON_RESPONSE_RESOURCE_URI:
- new_resource_uri = va_arg(args, char*);
- if (resp->new_uri)
- free(resp->new_uri);
-
- if (new_resource_uri)
- resp->new_uri = ic_utils_strdup(new_resource_uri);
- else
- resp->new_uri = NULL;
- break;
- case IOTCON_RESPONSE_HEADER_OPTIONS:
- options = va_arg(args, iotcon_options_h);
- if (resp->header_options)
- ic_options_free(resp->header_options);
- if (NULL == options) {
- resp->header_options = NULL;
- break;
- }
- if (true == options->has_parent)
- resp->header_options = ic_options_ref(options);
- else
- resp->header_options = options;
- if (NULL == resp->header_options) {
- ERR("header_options is NULL");
- return IOTCON_ERROR_NO_DATA;
- }
-
- resp->header_options->has_parent = true;
- break;
- case IOTCON_RESPONSE_NONE:
- default:
- break;
- }
-
- va_end(args);
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_response_send(iotcon_response_h resp)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resp, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == resp->repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_send_res_response_data(resp);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_send_res_response_data() Fail(%d)", ret);
-
- return ret;
-}
-
+++ /dev/null
-/*
- * 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_INTERNAL_RESPONSE_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_RESPONSE_H__
-
-#include "iotcon-struct.h"
-#include "iotcon-constant.h"
-#include "ic-request.h"
-
-struct ic_resource_response {
- char *new_uri;
- int error_code;
- iotcon_options_h header_options;
- iotcon_interface_e iface;
- iotcon_response_result_e result;
- iotcon_repr_h repr;
- oc_request_h request_handle;
- oc_resource_h resource_handle;
-};
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_RESPONSE_H__ */
+++ /dev/null
-/*
- * 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include "iotcon.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-
-char* ic_utils_strdup(const char *src)
-{
- char *dest = NULL;
-
- RETV_IF(NULL == src, NULL);
-
- errno = 0;
- dest = strdup(src);
- if (NULL == dest) {
- ERR("strdup() Fail(%d)", errno);
- return NULL;
- }
-
- return dest;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_UTILITY_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_UTILITY_H__
-
-#define IC_STR_EQUAL 0
-
-char* ic_utils_strdup(const char *src);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_UTILITY_H__ */
+++ /dev/null
-/*
- * 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 <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <glib.h>
-#include <glib-object.h>
-
-#include "iotcon.h"
-#include "ic-common.h"
-#include "ic-utils.h"
-#include "ic-resource-types.h"
-#include "ic-ioty.h"
-#include "ic-repr.h"
-#include "ic.h"
-
-/**
- * @brief global context
- */
-static GHashTable *ic_request_cb_hash;
-static bool ic_is_init = false;
-
-static void _free_resource(gpointer data)
-{
- int ret;
- iotcon_resource_h resource = data;
-
- RET_IF(NULL == data);
-
- ret = ic_ioty_unregister_res(resource->handle);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_unregister_res() Fail(%d)", ret);
-
- free(resource);
-}
-
-/* Host address should begin with "coap://" */
-API void iotcon_initialize(const char *addr, unsigned short port)
-{
- FN_CALL;
-
- RETM_IF(true == ic_is_init, "already initialized");
- RET_IF(NULL == addr);
-
- ic_ioty_config(addr, port);
- ic_request_cb_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
- _free_resource);
-
-#if !GLIB_CHECK_VERSION(2,35,0)
- g_type_init();
-#endif
- ic_is_init = true;
-}
-
-
-API void iotcon_deinitialize()
-{
- FN_CALL;
-
- RETM_IF(false == ic_is_init, "Not initialized");
-
- g_hash_table_destroy(ic_request_cb_hash);
- ic_request_cb_hash = NULL;
-
- ic_is_init = false;
-}
-
-
-static gboolean _find_valid_resource(gpointer key, gpointer value, gpointer user_data)
-{
- return (key == user_data);
-}
-
-
-iotcon_resource_h ic_get_resource_handler_data(void *handle)
-{
- return g_hash_table_find(ic_request_cb_hash, _find_valid_resource, handle);
-}
-
-
-/* The length of uri should be less than or equal to 36. */
-API iotcon_resource_h iotcon_register_resource(const char *uri,
- iotcon_resource_types_h res_types,
- int ifaces,
- uint8_t properties,
- iotcon_request_handler_cb cb,
- void *user_data)
-{
- FN_CALL;
- iotcon_resource_h resource;
-
- RETV_IF(NULL == uri, NULL);
- RETVM_IF(IOTCON_URI_LENGTH_MAX < strlen(uri), NULL,
- "The length of uri(%s) is invalid", uri);
- RETV_IF(NULL == res_types, NULL);
- RETV_IF(NULL == cb, NULL);
-
- resource = calloc(1, sizeof(struct ic_resource));
- if (NULL == resource) {
- ERR("calloc Fail(%d)", errno);
- return NULL;
- }
-
- resource->handle = ic_ioty_register_res(uri, res_types, ifaces, properties);
- if (NULL == resource->handle) {
- ERR("ic_ioty_register_res() Fail");
- free(resource);
- return NULL;
- }
-
- resource->cb = cb;
- resource->user_data = user_data;
-
- resource->uri = ic_utils_strdup(uri);
- resource->types = ic_resource_types_ref(res_types);
- resource->ifaces = ifaces;
- resource->is_observable = properties & IOTCON_OBSERVABLE;
-
- g_hash_table_insert(ic_request_cb_hash, resource->handle, resource);
-
- return resource;
-}
-
-
-API void iotcon_unregister_resource(iotcon_resource_h resource)
-{
- FN_CALL;
-
- RET_IF(NULL == resource);
-
- g_hash_table_remove(ic_request_cb_hash, resource->handle);
-}
-
-
-API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_bind_iface_to_res(resource->handle, iface);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_bind_iface_to_res() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type)
-{
- FN_CALL;
- int ret;
-
- 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("The length of resource_type(%s) is invalid", resource_type);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- ret = ic_ioty_bind_type_to_res(resource->handle, resource_type);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_bind_type_to_res() Fail(%d)", 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;
-
- RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(parent == child, 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;
- }
- if (NULL == parent->children[i]) {
- ret = ic_ioty_bind_res(parent->handle, child->handle);
- if (IOTCON_ERROR_NONE == ret)
- parent->children[i] = child;
- else
- ERR("ic_ioty_bind_res() Fail(%d)", ret);
-
- return ret;
- }
- }
-
- 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;
-
- RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_unbind_res(parent->handle, child->handle);
- if (IOTCON_ERROR_NONE == ret) {
- for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
- if (child == parent->children[i])
- parent->children[i] = NULL;
- }
- }
- else
- ERR("ic_ioty_unbind_res() Fail(%d)", ret);
-
- return ret;
-}
-
-
-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(iotcon_resource_h resource, char **uri)
-{
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
-
- *uri = resource->uri;
-
- return IOTCON_ERROR_NONE;
-}
-
-
-/* The content of the resource should not be freed by user. */
-API int iotcon_resource_get_host(iotcon_resource_h resource, char **host)
-{
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER);
-
- *host = resource->host;
-
- 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 int iotcon_start_presence(unsigned int time_to_live)
-{
- FN_CALL;
- int ret;
-
- ret = ic_ioty_start_presence(time_to_live);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_start_presence() Fail(%d)", ret);
-
- return ret;
-}
-
-
-API int iotcon_stop_presence()
-{
- FN_CALL;
- int ret;
-
- ret = ic_ioty_stop_presence();
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_stop_presence() Fail(%d)", ret);
-
- return ret;
-}
-
-
-/* The length of resource_type should be less than or equal to 61. */
-API iotcon_presence_h iotcon_subscribe_presence(const char *host_address,
- const char *resource_type, iotcon_presence_cb cb, void *user_data)
-{
- iotcon_presence_h handle;
-
- RETV_IF(NULL == host_address, NULL);
- RETV_IF(NULL == cb, NULL);
- if (resource_type &&(IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) {
- ERR("The length of resource_type(%s) is invalid", resource_type);
- return NULL;
- }
-
- if (NULL == resource_type)
- resource_type = "";
-
- handle = ic_ioty_subscribe_presence(host_address, resource_type, cb, user_data);
- if (NULL == handle)
- ERR("ic_ioty_subscribe_presence() Fail");
-
- return handle;
-}
-
-
-API int iotcon_unsubscribe_presence(iotcon_presence_h handle)
-{
- FN_CALL;
- int ret;
-
- RETV_IF(NULL == handle, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_unsubscribe_presence(handle);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_unsubscribe_presence() Fail(%d)", ret);
-
- return ret;
-}
-
-
-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 ic_notify_msg));
- if (NULL == msg) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- msg->repr = 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(iotcon_resource_h resource, iotcon_notimsg_h msg,
- iotcon_observers_h observers)
-{
- int ret;
-
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == msg, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == msg->repr, IOTCON_ERROR_INVALID_PARAMETER);
-
- ret = ic_ioty_send_notify(resource->handle, msg, observers);
- if (IOTCON_ERROR_NONE != ret)
- ERR("ic_ioty_send_notify() Fail(%d)", ret);
-
- return ret;
-}
+++ /dev/null
-/*
- * 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_INTERNAL_H__
-#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_H__
-
-#include "iotcon.h"
-
-#define IC_INTERFACE_MAX (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_LINK | \
- IOTCON_INTERFACE_BATCH | IOTCON_INTERFACE_GROUP)
-
-struct ic_resource {
- char *uri;
- char *host;
- bool is_observable;
- iotcon_resource_types_h types;
- int ifaces;
- void *handle;
- iotcon_request_handler_cb cb;
- void *user_data;
- iotcon_resource_h children[IOTCON_CONTAINED_RESOURCES_MAX];
-};
-
-struct ic_notify_msg {
- int error_code;
- iotcon_interface_e iface;
- iotcon_repr_h repr;
-};
-
-iotcon_resource_h ic_get_resource_handler_data(void *handle);
-
-#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_H__ */
--- /dev/null
+/*
+ * 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 <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon-struct.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-ioty.h"
+#include "icl-options.h"
+#include "icl-resource-types.h"
+#include "icl-client.h"
+
+/* host address should begin with "coap://"
+ * The length of resource_type should be less than or equal to 61.
+ * If resource_type is NULL, then All resources in host are discovered. */
+API int iotcon_find_resource(const char *host_addr, const char *resource_type,
+ iotcon_found_resource_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == host_addr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+ if (resource_type && (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) {
+ ERR("The length of resource_type(%s) is invalid", resource_type);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ ret = ic_ioty_find_resource(host_addr, resource_type, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_find_resource() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+/* If you know the information of resource, then you can make a proxy of the resource. */
+API iotcon_client_h iotcon_client_new(const char *host, const char *uri,
+ bool is_observable, iotcon_resource_types_h resource_types, int resource_ifs)
+{
+ FN_CALL;
+ iotcon_client_h resource = NULL;
+
+ RETV_IF(NULL == host, NULL);
+ RETV_IF(NULL == uri, NULL);
+ RETV_IF(NULL == resource_types, NULL);
+
+ resource = calloc(1, sizeof(struct ic_remote_resource));
+ if (NULL == resource) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ resource->host = ic_utils_strdup(host);
+ resource->uri = ic_utils_strdup(uri);
+ resource->is_observable = is_observable;
+ resource->types = ic_resource_types_ref(resource_types);
+ resource->ifaces = resource_ifs;
+
+ return resource;
+}
+
+
+API void iotcon_client_free(iotcon_client_h resource)
+{
+ FN_CALL;
+
+ RET_IF(NULL == resource);
+
+ free(resource->uri);
+ free(resource->host);
+
+ /* null COULD be allowed */
+ if (resource->header_options)
+ ic_options_free(resource->header_options);
+ iotcon_resource_types_free(resource->types);
+ free(resource);
+}
+
+
+API iotcon_client_h iotcon_client_clone(iotcon_client_h resource)
+{
+ iotcon_client_h clone;
+
+ RETV_IF(NULL == resource, NULL);
+
+ clone = iotcon_client_new(resource->host,
+ resource->uri,
+ resource->is_observable,
+ iotcon_resource_types_clone(resource->types),
+ resource->ifaces);
+ if (NULL == clone) {
+ ERR("iotcon_client_new() Fail");
+ return clone;
+ }
+
+ clone->observe_handle = resource->observe_handle;
+
+ return clone;
+}
+
+
+/* The content of the resource should not be freed by user. */
+API int iotcon_client_get_uri(iotcon_client_h resource, char **uri)
+{
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *uri = resource->uri;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the resource should not be freed by user. */
+API int iotcon_client_get_host(iotcon_client_h resource, char **host)
+{
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *host = resource->host;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the resource should not be freed by user. */
+API int iotcon_client_get_types(iotcon_client_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_client_get_interfaces(iotcon_client_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_client_is_observable(iotcon_client_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;
+}
+
+
+/* if header_options is NULL, then client's header_options is unset */
+API int iotcon_client_set_options(iotcon_client_h resource,
+ iotcon_options_h header_options)
+{
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+ if (resource->header_options)
+ iotcon_options_free(resource->header_options);
+
+ if (NULL == header_options) {
+ resource->header_options = NULL;
+ return IOTCON_ERROR_NONE;
+ }
+
+ if (true == header_options->has_parent)
+ resource->header_options = ic_options_ref(header_options);
+ else
+ resource->header_options = header_options;
+ if (NULL == resource->header_options) {
+ ERR("header_options is NULL");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ resource->header_options->has_parent = true;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_get(iotcon_client_h resource, iotcon_query_h query,
+ iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_get(resource, query, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_get() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_put(iotcon_client_h resource, iotcon_repr_h repr,
+ iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_put(resource, repr, query, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_put() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_post(iotcon_client_h resource, iotcon_repr_h repr,
+ iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_post(resource, repr, query, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_post() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_delete_res(resource, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_delete_res() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_observer_start(iotcon_client_h resource,
+ iotcon_observe_type_e observe_type,
+ iotcon_query_h query,
+ iotcon_on_observe_cb cb,
+ void *user_data)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_observe(resource, observe_type, query, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_observe() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_observer_stop(iotcon_client_h resource)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_cancel_observe(resource);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_cancel_observe() Fail(%d)", ret);
+
+ return ret;
+}
--- /dev/null
+/*
+ * 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_CLIENT_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_CLIENT_H__
+
+#include "iotcon-struct.h"
+#include "icl-options.h"
+
+typedef void* iotcon_observe_h;
+
+struct ic_remote_resource {
+ char *uri;
+ char *host;
+ bool is_observable;
+ bool is_collection;
+ iotcon_options_h header_options;
+ iotcon_resource_types_h types;
+ int ifaces;
+ iotcon_observe_h observe_handle;
+};
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_CLIENT_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon.h"
+#include "icl.h"
+#include "icl-ioty.h"
+
+/* The length of manufacturer_name should be less than and equal to 16.
+ * The length of manufacturer_url should be less than and equal to 32. */
+API int iotcon_register_device_info(iotcon_device_info_s device_info)
+{
+ int ret;
+
+ if (device_info.manuf_name
+ && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(device_info.manuf_name))) {
+ ERR("The length of manufacturer_name(%s) is invalid.", device_info.manuf_name);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ if (device_info.manuf_url
+ && (IOTCON_MANUFACTURER_URL_LENGTH_MAX < strlen(device_info.manuf_url))) {
+ ERR("The length of manufacturer_url(%s) is invalid.", device_info.manuf_url);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+
+ ret = ic_ioty_register_device_info(device_info);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_register_device_info() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+/* host_address should begin with "coap://" */
+API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb,
+ void *user_data)
+{
+ FN_CALL;
+ int ret = IOTCON_ERROR_NONE;
+
+ RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_get_device_info(host_address, cb, user_data);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_get_device_info() Fail(%d)", ret);
+ return ret;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
--- /dev/null
+/*
+ * 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 <OCApi.h>
+#include <OCPlatform.h>
+
+extern "C" {
+#include "iotcon-struct.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-repr.h"
+#include "icl-ioty-repr.h"
+}
+
+using namespace OC;
+using namespace std;
+
+static iotcon_repr_h _ic_ioty_repr_create_repr(const OCRepresentation& ocRep)
+{
+ FN_CALL;
+
+ string jsonStr = ocRep.getJSONRepresentation();
+ iotcon_repr_h repr = ic_repr_parse_json(jsonStr.c_str());
+
+ string uriStr = ocRep.getUri();
+ if (!uriStr.empty())
+ iotcon_repr_set_uri(repr, uriStr.c_str());
+
+ return repr;
+}
+
+static iotcon_repr_h _ic_ioty_repr_create_parent(const OCRepresentation& ocRep)
+{
+ FN_CALL;
+ return _ic_ioty_repr_create_repr(ocRep);
+}
+
+static iotcon_repr_h _ic_ioty_repr_create_child(const OCRepresentation& ocRep)
+{
+ FN_CALL;
+ return _ic_ioty_repr_create_repr(ocRep);
+}
+
+
+/*
+ * A general input : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}},
+ * {"href":"/a/child","rep":{"string":"World","double_val":5.7},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]}
+ */
+iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep)
+{
+ FN_CALL;
+ unsigned int i = 0;
+ OCRepresentation ocChild;
+
+ iotcon_repr_h repr_parent = _ic_ioty_repr_create_parent(ocRep);
+ if (NULL == repr_parent) {
+ ERR("_ic_ioty_repr_create_parent() Fail");
+ iotcon_repr_free(repr_parent);
+ return NULL;
+ }
+
+ vector<OCRepresentation> childList = ocRep.getChildren();
+
+ for (i = 0; i < childList.size(); i++) {
+ ocChild = childList.at(i);
+ iotcon_repr_h repr_child = _ic_ioty_repr_create_child(ocChild);
+ if (NULL == repr_child) {
+ ERR("_ic_ioty_repr_create_child() Fail");
+ /* free parent because adding child is failed.
+ * this func also will free children */
+ iotcon_repr_free(repr_parent);
+ return NULL;
+ }
+
+ repr_parent->children = g_list_append(repr_parent->children, repr_child);
+ }
+
+ return repr_parent;
+}
+
+int ic_ioty_repr_parse(iotcon_repr_h repr, OCRepresentation &ocRep)
+{
+ FN_CALL;
+
+ int ret = IOTCON_ERROR_NONE;
+ MessageContainer info;
+
+ /* TODO: It's better that iotcon_repr_h is changed to OCRepresentation at once. */
+ char *repr_json = ic_repr_generate_json(repr, false);
+
+ try {
+ info.setJSONRepresentation(repr_json);
+
+ const vector<OCRepresentation> &reps = info.representations();
+ if (0 < reps.size()) {
+ vector<OCRepresentation>::const_iterator itr = reps.begin();
+ vector<OCRepresentation>::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;
+ }
+
+ free(repr_json);
+ return ret;
+}
+
--- /dev/null
+/*
+ * 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__
+
+#include <OCRepresentation.h>
+#include "iotcon-struct.h"
+
+void ic_ioty_repr_found_device_cb(const OC::OCRepresentation& ocRep);
+iotcon_repr_h ic_ioty_repr_generate_repr(const OC::OCRepresentation& ocRep);
+int ic_ioty_repr_parse(iotcon_repr_h repr, OC::OCRepresentation &ocRep);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_REPRESENTATION_H__ */
+
--- /dev/null
+/*
+ * 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 <stdbool.h>
+#include <stdint.h>
+#include <glib.h>
+#include <OCApi.h>
+#include <OCPlatform.h>
+
+extern "C" {
+#include "iotcon-struct.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-client.h"
+#include "icl-request.h"
+#include "icl-response.h"
+#include "icl-resource-types.h"
+#include "icl-repr.h"
+#include "icl-ioty-repr.h"
+#include "icl-ioty.h"
+}
+
+#define IC_UNICAST_RESOURCE_DISCOVERY ":5683/oc/core"
+#define IC_MULTICAST_RESOURCE_DISCOVERY "/oc/core"
+#define IC_DEVICE_DISCOVERY "/oc/core/d"
+
+using namespace std;
+using namespace OC;
+using namespace OCPlatform;
+
+struct resource_handle {
+ OCResource::Ptr ocResource;
+};
+
+namespace icIotivityHandler {
+
+ class presenceObject
+ {
+ private:
+ iotcon_presence_cb presence_handler;
+ void *cb_data;
+
+ public:
+ presenceObject(iotcon_presence_cb user_cb, void *user_data)
+ {
+ presence_handler = user_cb;
+ cb_data = user_data;
+ }
+
+ void presenceHandler(OCStackResult result, const unsigned int nonce,
+ const string& hostAddress)
+ {
+ int res;
+
+ switch (result) {
+ case OC_STACK_OK:
+ res = IOTCON_PRESENCE_OK;
+ break;
+ case OC_STACK_PRESENCE_STOPPED:
+ res = IOTCON_PRESENCE_STOPPED;
+ break;
+ case OC_STACK_PRESENCE_TIMEOUT:
+ res = IOTCON_PRESENCE_TIMEOUT;
+ break;
+ case OC_STACK_ERROR:
+ default:
+ ERR("subscribePresence() Fail(%d)", result);
+ res = IOTCON_PRESENCE_ERROR;
+ }
+
+ if (presence_handler)
+ presence_handler(res, nonce, hostAddress.c_str(), cb_data);
+ }
+ };
+
+ class findObject
+ {
+ private:
+ iotcon_found_resource_cb found_resource;
+ void *cb_data;
+
+ public:
+ findObject(iotcon_found_resource_cb user_cb, void *user_data)
+ {
+ found_resource = user_cb;
+ cb_data = user_data;
+ }
+
+ void foundResource(shared_ptr<OCResource> resource)
+ {
+ struct ic_remote_resource resource_s = {0};
+ resource_s.types = NULL;
+
+ vector<string> resource_types = resource->getResourceTypes();
+ if (0 < resource_types.size()) {
+ resource_s.types = iotcon_resource_types_new();
+ if (NULL == resource_s.types) {
+ ERR("iotcon_resource_types_new() Fail");
+ return;
+ }
+
+ for (string &resource_type : resource_types)
+ iotcon_resource_types_insert(resource_s.types, resource_type.c_str());
+ }
+
+ resource_s.uri = ic_utils_strdup(resource->uri().c_str());
+ resource_s.host = ic_utils_strdup(resource->host().c_str());
+ resource_s.is_observable = resource->isObservable();
+
+ vector<string> resource_interfaces = resource->getResourceInterfaces();
+ for (string &resource_interface : resource_interfaces) {
+ if (IC_STR_EQUAL == resource_interface.compare(DEFAULT_INTERFACE))
+ resource_s.ifaces |= IOTCON_INTERFACE_DEFAULT;
+
+ if (IC_STR_EQUAL == resource_interface.compare(BATCH_INTERFACE))
+ resource_s.ifaces |= IOTCON_INTERFACE_BATCH;
+
+ if (IC_STR_EQUAL == resource_interface.compare(LINK_INTERFACE))
+ resource_s.ifaces |= IOTCON_INTERFACE_LINK;
+
+ if (IC_STR_EQUAL == resource_interface.compare(GROUP_INTERFACE))
+ resource_s.ifaces |= IOTCON_INTERFACE_GROUP;
+ }
+
+ if (found_resource)
+ found_resource(&resource_s, cb_data);
+
+ free(resource_s.uri);
+ free(resource_s.host);
+ iotcon_resource_types_free(resource_s.types);
+ }
+ };
+
+ class getObject
+ {
+ private:
+ iotcon_on_cru_cb on_get;
+ void *cb_data;
+
+ public:
+ getObject(iotcon_on_cru_cb user_cb, void *user_data)
+ {
+ on_get = user_cb;
+ cb_data = user_data;
+ }
+
+ void onGet(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
+ const int eCode)
+ {
+ FN_CALL;
+ int res;
+ iotcon_options_h options;
+ iotcon_repr_h repr = NULL;
+
+ if (OC_STACK_OK == eCode) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ }
+ else {
+ ERR("get() Fail(%d)", eCode);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ }
+
+ options = iotcon_options_new();
+ for (HeaderOption::OCHeaderOption option : headerOptions) {
+ iotcon_options_insert(options, option.getOptionID(),
+ option.getOptionData().c_str());
+ }
+
+ repr = ic_ioty_repr_generate_repr(ocRep);
+
+ if (on_get)
+ on_get(options, repr, res, cb_data);
+
+ iotcon_repr_free(repr);
+ iotcon_options_free(options);
+ }
+ };
+
+ class putObject
+ {
+ private:
+ iotcon_on_cru_cb on_put;
+ void *cb_data;
+
+ public:
+ putObject(iotcon_on_cru_cb user_cb, void *user_data)
+ {
+ on_put = user_cb;
+ cb_data = user_data;
+ }
+
+ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
+ const int eCode)
+ {
+ FN_CALL;
+ int res;
+ iotcon_options_h options;
+ iotcon_repr_h repr = NULL;
+
+ if (OC_STACK_OK == eCode) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ }
+ else {
+ ERR("put() Fail(%d)", eCode);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ }
+
+ options = iotcon_options_new();
+ for (HeaderOption::OCHeaderOption option : headerOptions) {
+ iotcon_options_insert(options, option.getOptionID(),
+ option.getOptionData().c_str());
+ }
+
+ repr = ic_ioty_repr_generate_repr(ocRep);
+
+ if (on_put)
+ on_put(options, repr, res, cb_data);
+
+ iotcon_repr_free(repr);
+ iotcon_options_free(options);
+ }
+ };
+
+ class postObject
+ {
+ private:
+ iotcon_on_cru_cb on_post;
+ void *cb_data;
+
+ public:
+ postObject(iotcon_on_cru_cb user_cb, void *user_data)
+ {
+ on_post = user_cb;
+ cb_data = user_data;
+ }
+
+ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
+ const int eCode)
+ {
+ FN_CALL;
+ int res;
+ iotcon_options_h options;
+ iotcon_repr_h repr = NULL;
+
+ if (OC_STACK_OK == eCode) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ }
+ else if (OC_STACK_RESOURCE_CREATED == eCode) {
+ res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
+ }
+ else {
+ ERR("post() Fail(%d)", eCode);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ }
+
+ options = iotcon_options_new();
+ for (HeaderOption::OCHeaderOption option : headerOptions) {
+ iotcon_options_insert(options, option.getOptionID(),
+ option.getOptionData().c_str());
+ }
+
+ repr = ic_ioty_repr_generate_repr(ocRep);
+
+ if (on_post)
+ on_post(options, repr, res, cb_data);
+
+ iotcon_repr_free(repr);
+ iotcon_options_free(options);
+ }
+ };
+
+ class deleteObject
+ {
+ private:
+ iotcon_on_delete_cb on_delete;
+ void *cb_data;
+
+ public:
+ deleteObject(iotcon_on_delete_cb user_cb, void *user_data)
+ {
+ on_delete = user_cb;
+ cb_data = user_data;
+ }
+
+ void onDelete(const HeaderOptions& headerOptions, const int eCode)
+ {
+ int res;
+ iotcon_options_h options;
+
+ if (OC_STACK_OK == eCode) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ }
+ else if (OC_STACK_RESOURCE_DELETED == eCode) {
+ res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
+ }
+ else {
+ ERR("deleteResource() Fail(%d)", eCode);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ }
+
+ options = iotcon_options_new();
+ for (HeaderOption::OCHeaderOption option : headerOptions) {
+ iotcon_options_insert(options, option.getOptionID(),
+ option.getOptionData().c_str());
+ }
+
+ if (on_delete)
+ on_delete(options, res, cb_data);
+
+ iotcon_options_free(options);
+ }
+ };
+
+ class observeObject
+ {
+ private:
+ iotcon_on_observe_cb on_observe;
+ void *cb_data;
+
+ public:
+ observeObject(iotcon_on_observe_cb user_cb, void *user_data)
+ {
+ on_observe = user_cb;
+ cb_data = user_data;
+ }
+
+ void onObserve(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
+ const int eCode, const int sequenceNumber)
+ {
+ FN_CALL;
+ int res;
+ iotcon_options_h options;
+ iotcon_repr_h repr = NULL;
+
+ if (OC_STACK_OK == eCode) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ }
+ else {
+ ERR("observe() Fail(%d)", eCode);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ }
+
+ options = iotcon_options_new();
+ for (HeaderOption::OCHeaderOption option : headerOptions) {
+ iotcon_options_insert(options, option.getOptionID(),
+ option.getOptionData().c_str());
+ }
+
+ repr = ic_ioty_repr_generate_repr(ocRep);
+
+ if (on_observe)
+ on_observe(options, repr, res, sequenceNumber, cb_data);
+
+ iotcon_repr_free(repr);
+ iotcon_options_free(options);
+ }
+ };
+
+ class deviceObject
+ {
+ private:
+ iotcon_device_info_cb found_cb;
+ void *cb_data;
+
+ public:
+ deviceObject(iotcon_device_info_cb user_cb, void *user_data)
+ {
+ found_cb = user_cb;
+ cb_data = user_data;
+ }
+
+ void receivedDeviceInfo(const OCRepresentation& ocRep)
+ {
+ iotcon_device_info_s info = {0};
+ string readbuf;
+
+ if (ocRep.getValue("ct", readbuf))
+ info.content_type = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mndt", readbuf))
+ info.date_of_manufacture = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("dn", readbuf))
+ info.name = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("di", readbuf))
+ info.uuid = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnfv", readbuf))
+ info.firmware_ver = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("hn", readbuf))
+ info.host_name = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnmn", readbuf))
+ info.manuf_name = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnml", readbuf))
+ info.manuf_url = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnmo", readbuf))
+ info.model_number = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnpv", readbuf))
+ info.platform_ver = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("mnsl", readbuf))
+ info.support_url = ic_utils_strdup(readbuf.c_str());
+ if (ocRep.getValue("icv", readbuf))
+ info.version = ic_utils_strdup(readbuf.c_str());
+
+ if (found_cb)
+ found_cb(info, cb_data);
+
+ free(info.name);
+ free(info.host_name);
+ free(info.uuid);
+ free(info.content_type);
+ free(info.version);
+ free(info.manuf_name);
+ free(info.manuf_url);
+ free(info.model_number);
+ free(info.date_of_manufacture);
+ free(info.platform_ver);
+ free(info.firmware_ver);
+ free(info.support_url);
+ }
+ };
+}
+
+extern "C" void ic_ioty_config(const char *addr, unsigned short port)
+{
+ PlatformConfig cfg {
+ ServiceType::InProc,
+ ModeType::Both,
+ string(addr),
+ port,
+ QualityOfService::HighQos
+ };
+ Configure(cfg);
+ DBG("Created a platform");
+}
+
+static OCEntityHandlerResult _ic_ioty_request_handler(
+ shared_ptr<OCResourceRequest> request)
+{
+ FN_CALL;
+ const char *request_type = NULL;
+ HeaderOptions headerOptions;
+ QueryParamsMap queryParams;
+ iotcon_resource_h temp_res = NULL;
+ struct ic_resource_request request_s = {0};
+
+ temp_res = ic_get_resource_handler_data(request->getResourceHandle());
+ if (NULL == temp_res) {
+ ERR("No Resource Handler");
+ return OC_EH_ERROR;
+ }
+
+ queryParams = request->getQueryParameters();
+ if (0 < queryParams.size()) {
+ request_s.query = iotcon_query_new();
+ if (NULL == request_s.query) {
+ ERR("iotcon_query_new() Fail");
+ return OC_EH_ERROR;
+ }
+
+ for (auto it : queryParams) {
+ DBG("key = %s value = %s", it.first.c_str(), it.second.c_str());
+ iotcon_query_insert(request_s.query, it.first.c_str(), it.second.c_str());
+ }
+ }
+
+ headerOptions = request->getHeaderOptions();
+ if (0 < headerOptions.size()) {
+ request_s.header_options = iotcon_options_new();
+ if (NULL == request_s.header_options) {
+ ERR("iotcon_options_new() Fail");
+ if (request_s.query)
+ iotcon_query_free(request_s.query);
+ return OC_EH_ERROR;
+ }
+
+ for (auto it : headerOptions) {
+ DBG("OptionID = %d, OptionData = %s",
+ it.getOptionID(), it.getOptionData().c_str());
+ iotcon_options_insert(request_s.header_options, it.getOptionID(),
+ it.getOptionData().c_str());
+ }
+ }
+
+ OCRepresentation ocRep = request->getResourceRepresentation();
+ if (0 < ocRep.numberOfAttributes()) {
+ DBG("numberOfAttributes : %d", ocRep.numberOfAttributes());
+ request_s.repr = ic_ioty_repr_generate_repr(ocRep);
+ if (NULL == request_s.repr) {
+ ERR("request_s.repr is NULL");
+ if (request_s.header_options)
+ iotcon_options_free(request_s.header_options);
+ if (request_s.query)
+ iotcon_query_free(request_s.query);
+ return OC_EH_ERROR;
+ }
+ }
+
+ if (RequestFlag & request->getRequestHandlerFlag()) {
+ request_type = ic_utils_strdup(request->getRequestType().c_str());
+ if (NULL == request_type) {
+ ERR("request_type is NULL");
+ if (request_s.repr)
+ iotcon_repr_free(request_s.repr);
+ if (request_s.header_options)
+ iotcon_options_free(request_s.header_options);
+ if (request_s.query)
+ iotcon_query_free(request_s.query);
+ return OC_EH_ERROR;
+ }
+
+ if (IC_STR_EQUAL == strcmp("GET", request_type))
+ request_s.types = IOTCON_REQUEST_GET;
+ else if (IC_STR_EQUAL == strcmp("PUT", request_type))
+ request_s.types = IOTCON_REQUEST_PUT;
+ else if (IC_STR_EQUAL == strcmp("POST", request_type))
+ request_s.types = IOTCON_REQUEST_POST;
+ else if (IC_STR_EQUAL == strcmp("DELETE", request_type))
+ request_s.types = IOTCON_REQUEST_DELETE;
+ }
+
+ if (ObserverFlag & request->getRequestHandlerFlag())
+ request_s.types |= IOTCON_REQUEST_OBSERVE;
+
+ request_s.uri = ic_utils_strdup(request->getResourceUri().c_str());
+ if (NULL == request_s.uri) {
+ ERR("ic_utils_strdup() Fail");
+ if (request_s.repr)
+ iotcon_repr_free(request_s.repr);
+ if (request_s.header_options)
+ iotcon_options_free(request_s.header_options);
+ if (request_s.query)
+ iotcon_query_free(request_s.query);
+ return OC_EH_ERROR;
+ }
+
+ request_s.request_handle = (iotcon_request_h)request->getRequestHandle();
+ request_s.resource_handle = (iotcon_client_h)request->getResourceHandle();
+
+ ObservationInfo observationInfo = request->getObservationInfo();
+ request_s.observation_info.action = (iotcon_observe_action_e)observationInfo.action;
+ request_s.observation_info.observer_id = observationInfo.obsId;
+ DBG("obs_info.obsId=%d", observationInfo.obsId);
+
+ /* call handler_cb */
+ if (temp_res->cb) {
+ temp_res->cb(&request_s, temp_res->user_data);
+ }
+ else {
+ WARN("temp_res->request_handler_cb is null");
+ }
+
+ free(request_s.uri);
+
+ /* To avoid unnecessary ERR log (repr could be NULL) */
+ if (request_s.repr)
+ iotcon_repr_free(request_s.repr);
+ if (request_s.header_options)
+ iotcon_options_free(request_s.header_options);
+ if (request_s.query)
+ iotcon_query_free(request_s.query);
+
+ return OC_EH_OK;
+}
+
+
+extern "C" OCResourceHandle ic_ioty_register_res(const char *uri,
+ iotcon_resource_types_h res_types, int ifaces, uint8_t properties)
+{
+ FN_CALL;
+ unsigned int i;
+ OCStackResult ret;
+ string resUri;
+ string resType;
+ string resInterface;
+ OCResourceHandle handle;
+
+ resUri = uri;
+
+ resType = ic_resource_types_get_nth_data(res_types, 0);
+
+ if (IOTCON_INTERFACE_DEFAULT & ifaces) {
+ resInterface = DEFAULT_INTERFACE;
+ ifaces ^= IOTCON_INTERFACE_DEFAULT;
+ }
+ else if (IOTCON_INTERFACE_LINK & ifaces) {
+ resInterface = LINK_INTERFACE;
+ ifaces ^= IOTCON_INTERFACE_LINK;
+ }
+ else if (IOTCON_INTERFACE_BATCH & ifaces) {
+ resInterface = BATCH_INTERFACE;
+ ifaces ^= IOTCON_INTERFACE_BATCH;
+ }
+ else if (IOTCON_INTERFACE_GROUP & ifaces) {
+ resInterface = GROUP_INTERFACE;
+ ifaces ^= IOTCON_INTERFACE_GROUP;
+ }
+
+ ret = registerResource(handle, resUri, resType, resInterface,
+ _ic_ioty_request_handler, properties);
+ if (OC_STACK_OK != ret) {
+ ERR("registerResource Fail(%d)", ret);
+ return NULL;
+ }
+ for (i = 1; i < ic_resource_types_get_length(res_types); i++)
+ ic_ioty_bind_type_to_res(handle, ic_resource_types_get_nth_data(res_types, i));
+
+ if (IOTCON_INTERFACE_DEFAULT & ifaces)
+ ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_DEFAULT);
+
+ if (IOTCON_INTERFACE_LINK & ifaces)
+ ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_LINK);
+
+ if (IOTCON_INTERFACE_BATCH & ifaces)
+ ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_BATCH);
+
+ if (IOTCON_INTERFACE_GROUP & ifaces)
+ ic_ioty_bind_iface_to_res(handle, IOTCON_INTERFACE_GROUP);
+
+ return handle;
+}
+
+
+extern "C" int ic_ioty_unregister_res(iotcon_resource_h resource_handle)
+{
+ OCResourceHandle resourceHandle = resource_handle;
+
+ OCStackResult result = unregisterResource(resourceHandle);
+ if(OC_STACK_OK != result) {
+ ERR("unregisterResource Fail(%d)", result);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_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);
+
+ string interface_str(src);
+
+ if (IC_STR_EQUAL == DEFAULT_INTERFACE.compare(interface_str)) {
+ *dest = IOTCON_INTERFACE_DEFAULT;
+ }
+ else if (IC_STR_EQUAL == LINK_INTERFACE.compare(interface_str)) {
+ *dest = IOTCON_INTERFACE_LINK;
+ }
+ else if (IC_STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) {
+ *dest = IOTCON_INTERFACE_BATCH;
+ }
+ else if (IC_STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) {
+ *dest = IOTCON_INTERFACE_GROUP;
+ }
+ else {
+ ERR("Invalid interface");
+ *dest = IOTCON_INTERFACE_NONE;
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+static int _ic_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 ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest)
+{
+ FN_CALL;
+ int ret;
+ string iface_str;
+
+ ret = _ic_ioty_convert_interface_flag(src, iface_str);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_ioty_convert_interface_flag() Fail(%d)", ret);
+ *dest = NULL;
+ return ret;
+ }
+
+ *dest = ic_utils_strdup(iface_str.c_str());
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_bind_iface_to_res(OCResourceHandle resourceHandle,
+ iotcon_interface_e iface)
+{
+ int ret;
+ OCStackResult ocRet;
+ string resource_interface;
+
+ ret = _ic_ioty_convert_interface_flag(iface, resource_interface);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
+ return ret;
+ }
+
+ ocRet = bindInterfaceToResource(resourceHandle, resource_interface);
+ if (OC_STACK_OK != ocRet) {
+ ERR("bindInterfaceToResource() Fail(%d)", ocRet);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+extern "C" int ic_ioty_bind_type_to_res(OCResourceHandle resource_handle,
+ const char *resource_type)
+{
+ OCStackResult ret;
+ OCResourceHandle resourceHandle = resource_handle;
+
+ ret = bindTypeToResource(resourceHandle, resource_type);
+ if (OC_STACK_OK != ret) {
+ ERR("bindTypeToResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_bind_res(OCResourceHandle parent, OCResourceHandle child)
+{
+ OCStackResult ret;
+
+ ret = bindResource(parent, child);
+ if (OC_STACK_OK != ret) {
+ ERR("bindResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_unbind_res(OCResourceHandle parent, OCResourceHandle child)
+{
+ OCStackResult ret;
+
+ ret = unbindResource(parent, child);
+ if (OC_STACK_OK != ret) {
+ ERR("unbindResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_register_device_info(iotcon_device_info_s device_info)
+{
+ FN_CALL;
+ OCStackResult ret;
+
+ OCDeviceInfo deviceInfo = {0};
+ deviceInfo.deviceName = device_info.name;
+ deviceInfo.hostName = device_info.host_name;
+ deviceInfo.deviceUUID = device_info.uuid;
+ deviceInfo.contentType = device_info.content_type;
+ deviceInfo.version = device_info.version;
+ deviceInfo.manufacturerName = device_info.manuf_name;
+ deviceInfo.manufacturerUrl = device_info.manuf_url;
+ deviceInfo.modelNumber = device_info.model_number;
+ deviceInfo.dateOfManufacture = device_info.date_of_manufacture;
+ deviceInfo.platformVersion = device_info.platform_ver;
+ deviceInfo.firmwareVersion = device_info.firmware_ver;
+ deviceInfo.supportUrl = device_info.support_url;
+
+ ret = registerDeviceInfo(deviceInfo);
+ if (OC_STACK_OK != ret) {
+ ERR("registerDeviceInfo() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+extern "C" int ic_ioty_get_device_info(const char *host_address,
+ iotcon_device_info_cb cb, void *user_data)
+{
+ OCStackResult ret;
+ string resHost = host_address + string(IC_DEVICE_DISCOVERY);
+
+ shared_ptr<icIotivityHandler::deviceObject> object
+ = make_shared<icIotivityHandler::deviceObject>(cb, user_data);
+ FindDeviceCallback findDeviceCallback = bind(
+ &icIotivityHandler::deviceObject::receivedDeviceInfo,
+ object,
+ placeholders::_1);
+
+ ret = getDeviceInfo("", resHost, findDeviceCallback);
+ if (OC_STACK_OK != ret) {
+ ERR("getDeviceInfo() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_msg *msg,
+ iotcon_observers_h observers)
+{
+ int ret;
+ OCStackResult ocRet;
+ ObservationIds obsIds;
+ OCRepresentation ocRep;
+ string iface;
+
+ GList *node = g_list_first((GList*)observers);
+ while (node) {
+ int obs_id = GPOINTER_TO_UINT(node->data);
+ obsIds.push_back(obs_id);
+
+ node = node->next;
+ }
+
+ shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
+ resourceResponse->setErrorCode(msg->error_code);
+
+ ret = ic_ioty_repr_parse(msg->repr, ocRep);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_repr_parse() Fail(%d)", ret);
+ return ret;
+ }
+
+ if (IOTCON_INTERFACE_NONE != msg->iface) {
+ ret = _ic_ioty_convert_interface_flag(msg->iface, iface);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", msg->iface, ret);
+ return ret;
+ }
+ }
+ else {
+ iface = DEFAULT_INTERFACE;
+ }
+
+ resourceResponse->setResourceRepresentation(ocRep, iface);
+
+ ocRet = notifyListOfObservers(resHandle, obsIds, resourceResponse);
+ if (OC_STACK_OK != ocRet) {
+ ERR("notifyListOfObservers() Fail(%d)", ocRet);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp)
+{
+ FN_CALL;
+ string iface;
+ int ret;
+ OCStackResult ocRet;
+ OCRepresentation ocRep;
+
+ ret = ic_ioty_repr_parse(resp->repr, ocRep);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_repr_parse() Fail(%d)", ret);
+ return ret;
+ }
+
+ auto pResponse = make_shared<OCResourceResponse>();
+ if (pResponse) {
+ pResponse->setRequestHandle(resp->request_handle);
+ pResponse->setResourceHandle(resp->resource_handle);
+ pResponse->setErrorCode(resp->error_code);
+ pResponse->setResponseResult((OCEntityHandlerResult)resp->result);
+
+ if (IOTCON_INTERFACE_NONE != resp->iface) {
+ ret = _ic_ioty_convert_interface_flag(resp->iface, iface);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", resp->iface, ret);
+ return ret;
+ }
+ }
+ else {
+ iface = DEFAULT_INTERFACE;
+ }
+
+ pResponse->setResourceRepresentation(ocRep, iface);
+
+ ocRet = sendResponse(pResponse);
+ if (OC_STACK_OK != ocRet) {
+ ERR("sendResponse() Fail(%d)", ocRet);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+ }
+
+ FN_END;
+
+ return IOTCON_ERROR_NONE;
+
+}
+
+extern "C" const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_address,
+ const char *resource_type,
+ iotcon_presence_cb cb,
+ void *user_data)
+{
+ OCStackResult ret;
+ iotcon_presence_h presence_handle = NULL;
+
+ shared_ptr<icIotivityHandler::presenceObject> object
+ = make_shared<icIotivityHandler::presenceObject>(cb, user_data);
+ SubscribeCallback subscribeCallback
+ = bind(&icIotivityHandler::presenceObject::presenceHandler, object,
+ placeholders::_1, placeholders::_2, placeholders::_3);
+
+ ret = subscribePresence(presence_handle, host_address, resource_type,
+ subscribeCallback);
+
+ if (OC_STACK_OK != ret) {
+ ERR("subscribePresence() Fail(%d)", ret);
+ return NULL;
+ }
+
+ return presence_handle;
+}
+
+extern "C" int ic_ioty_unsubscribe_presence(iotcon_presence_h presence_handle)
+{
+ OCStackResult ret;
+
+ ret = unsubscribePresence(presence_handle);
+ if (OC_STACK_OK != ret) {
+ ERR("unsubscribePresence() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_start_presence(unsigned int time_to_live)
+{
+ OCStackResult ret;
+
+ ret = startPresence(time_to_live);
+ if (OC_STACK_OK != ret) {
+ ERR("startPresence() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_stop_presence()
+{
+ OCStackResult ret;
+
+ ret = stopPresence();
+ if (OC_STACK_OK != ret) {
+ ERR("stopPresence() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_find_resource(const char *host_address, const char *resource_type,
+ iotcon_found_resource_cb cb, void *user_data)
+{
+ OCStackResult ret;
+ ostringstream resource_name;
+
+ if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address))
+ resource_name << host_address << IC_MULTICAST_RESOURCE_DISCOVERY;
+ else
+ resource_name << host_address << IC_UNICAST_RESOURCE_DISCOVERY;
+
+ if (resource_type)
+ resource_name << "?rt=" << resource_type;
+
+ shared_ptr<icIotivityHandler::findObject> object
+ = make_shared<icIotivityHandler::findObject>(cb, user_data);
+ FindCallback findCallback = bind(&icIotivityHandler::findObject::foundResource,
+ object, placeholders::_1);
+
+ ret = findResource("", resource_name.str(), findCallback);
+ if (OC_STACK_OK != ret) {
+ ERR("findResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+static int _ic_ioty_accumulate_options_vector(unsigned short id, const char *data,
+ void *user_data)
+{
+ HeaderOptions *options = static_cast<HeaderOptions*>(user_data);
+ HeaderOption::OCHeaderOption option(id, data);
+ (*options).push_back(option);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+
+static int _ic_ioty_accumulate_res_types(const char *type, void *user_data)
+{
+ vector<string> *types = static_cast<vector<string>*>(user_data);
+ (*types).push_back(type);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+
+static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource)
+{
+ string host;
+ string uri;
+ vector<string> resource_types;
+ vector<string> resource_ifs;
+
+ HeaderOptions header_options;
+
+ RETV_IF(NULL == resource, NULL);
+ RETV_IF(NULL == resource->host, NULL);
+ RETV_IF(NULL == resource->uri, NULL);
+ RETV_IF(NULL == resource->types, NULL);
+
+ host = resource->host;
+ uri = resource->uri;
+
+ iotcon_resource_types_foreach(resource->types, _ic_ioty_accumulate_res_types,
+ (void*)&resource_types);
+
+ if (IOTCON_INTERFACE_NONE == resource->ifaces) {
+ resource_ifs.push_back(DEFAULT_INTERFACE);
+ }
+ else {
+ if (IOTCON_INTERFACE_DEFAULT & resource->ifaces)
+ resource_ifs.push_back(DEFAULT_INTERFACE);
+
+ if (IOTCON_INTERFACE_LINK & resource->ifaces)
+ resource_ifs.push_back(LINK_INTERFACE);
+
+ if (IOTCON_INTERFACE_BATCH & resource->ifaces)
+ resource_ifs.push_back(BATCH_INTERFACE);
+
+ if (IOTCON_INTERFACE_GROUP & resource->ifaces)
+ resource_ifs.push_back(GROUP_INTERFACE);
+ }
+
+ OCResource::Ptr ocResource = constructResourceObject(host, uri,
+ resource->is_observable, resource_types, resource_ifs);
+
+ if (resource->header_options) {
+ iotcon_options_foreach(resource->header_options,
+ _ic_ioty_accumulate_options_vector, (void*)&header_options);
+ ocResource->setHeaderOptions(header_options);
+ }
+
+ return ocResource;
+}
+
+
+static int _ic_ioty_accumulate_query_map(const char *key, const char *value,
+ void *user_data)
+{
+ QueryParamsMap *queryParams = static_cast<QueryParamsMap*>(user_data);
+ string keyStr = key;
+ string valueStr = value;
+ (*queryParams)[keyStr] = valueStr;
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+
+extern "C" int ic_ioty_get(iotcon_client_h resource, iotcon_query_h query,
+ iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ OCStackResult ret;
+ OCResource::Ptr ocResource;
+ QueryParamsMap queryParams;
+
+ if (query)
+ iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void *)&queryParams);
+
+ ocResource = _ic_ioty_create_oc_resource(resource);
+
+ shared_ptr<icIotivityHandler::getObject> object
+ = make_shared<icIotivityHandler::getObject>(cb, user_data);
+ GetCallback getCallback = bind(&icIotivityHandler::getObject::onGet, object,
+ placeholders::_1, placeholders::_2, placeholders::_3);
+ ret = ocResource->get(queryParams, getCallback);
+ if (OC_STACK_OK != ret) {
+ ERR("get() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr,
+ iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+ OCStackResult result;
+ OCResource::Ptr ocResource;
+ OCRepresentation ocRep;
+ QueryParamsMap queryParams;
+
+ if (query)
+ iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
+
+ ret = ic_ioty_repr_parse(repr, ocRep);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_repr_parse() Fail(%d)", ret);
+ return ret;
+ }
+
+ ocResource = _ic_ioty_create_oc_resource(resource);
+
+ shared_ptr<icIotivityHandler::putObject> object
+ = make_shared<icIotivityHandler::putObject>(cb, user_data);
+ PutCallback putCallback = bind(&icIotivityHandler::putObject::onPut, object,
+ placeholders::_1, placeholders::_2, placeholders::_3);
+
+ result = ocResource->put(ocRep, queryParams, putCallback);
+ if (OC_STACK_OK != result) {
+ ERR("put() Fail(%d)", result);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr,
+ iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data)
+{
+ FN_CALL;
+ int ret;
+ OCStackResult ocRet;
+ QueryParamsMap queryParams;
+ OCRepresentation ocRep;
+ OCResource::Ptr ocResource;
+
+ if (query)
+ iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
+
+ ret = ic_ioty_repr_parse(repr, ocRep);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_repr_parse() Fail(%d)", ret);
+ return ret;
+ }
+
+ ocResource = _ic_ioty_create_oc_resource(resource);
+
+ shared_ptr<icIotivityHandler::postObject> object
+ = make_shared<icIotivityHandler::postObject>(cb, user_data);
+ PostCallback postCallback = bind(&icIotivityHandler::postObject::onPost, object,
+ placeholders::_1, placeholders::_2, placeholders::_3);
+
+ ocRet = ocResource->post(ocRep, queryParams, postCallback);
+ if (OC_STACK_OK != ocRet) {
+ ERR("post() Fail(%d)", ocRet);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_delete_res(iotcon_client_h resource,
+ iotcon_on_delete_cb cb, void *user_data)
+{
+ FN_CALL;
+ OCStackResult ret;
+ OCResource::Ptr ocResource;
+
+ ocResource = _ic_ioty_create_oc_resource(resource);
+
+ shared_ptr<icIotivityHandler::deleteObject> object
+ = make_shared<icIotivityHandler::deleteObject>(cb, user_data);
+ DeleteCallback deleteCallback = bind(&icIotivityHandler::deleteObject::onDelete,
+ object, placeholders::_1, placeholders::_2);
+
+ ret = ocResource->deleteResource(deleteCallback);
+ if (OC_STACK_OK != ret) {
+ ERR("deleteResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_observe(iotcon_client_h resource,
+ iotcon_observe_type_e observe_type,
+ iotcon_query_h query,
+ iotcon_on_observe_cb cb,
+ void *user_data)
+{
+ OCStackResult ret;
+ OCResource::Ptr ocResource;
+ ObserveType observeType;
+ QueryParamsMap queryParams;
+
+ if (query)
+ iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams);
+
+ if (IOTCON_OBSERVE == observe_type) {
+ observeType = ObserveType::Observe;
+ }
+ else if (IOTCON_OBSERVE_ALL == observe_type) {
+ observeType = ObserveType::ObserveAll;
+ }
+ else {
+ ERR("Invalid observe_type");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ ocResource = _ic_ioty_create_oc_resource(resource);
+
+ resource_handle *obs_h = new resource_handle();
+ obs_h->ocResource = ocResource;
+ resource->observe_handle = (void*)obs_h;
+
+ shared_ptr<icIotivityHandler::observeObject> object
+ = make_shared<icIotivityHandler::observeObject>(cb, user_data);
+ ObserveCallback observeCallback = bind(&icIotivityHandler::observeObject::onObserve,
+ object, placeholders::_1, placeholders::_2, placeholders::_3,
+ placeholders::_4);
+ ret = ocResource->observe(observeType, queryParams, observeCallback);
+ if (OC_STACK_OK != ret) {
+ ERR("observe() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+extern "C" int ic_ioty_cancel_observe(iotcon_client_h resource)
+{
+ OCStackResult ret;
+ resource_handle *resource_h = (resource_handle *)resource->observe_handle;
+
+ OCResource::Ptr ocResource = resource_h->ocResource;
+ delete (resource_handle *)resource->observe_handle;
+ resource->observe_handle = NULL;
+
+ ret = ocResource->cancelObserve();
+ if (OC_STACK_OK != ret) {
+ ERR("cancelObserve() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
--- /dev/null
+/*
+ * 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 <stdint.h>
+
+#include "iotcon.h"
+#include "icl-response.h"
+
+struct ic_notify_msg {
+ int error_code;
+ iotcon_interface_e iface;
+ iotcon_repr_h repr;
+};
+
+struct ic_resource {
+ char *uri;
+ char *host;
+ bool is_observable;
+ iotcon_resource_types_h types;
+ int ifaces;
+ void *handle;
+ iotcon_request_handler_cb cb;
+ void *user_data;
+ iotcon_resource_h children[IOTCON_CONTAINED_RESOURCES_MAX];
+};
+
+void ic_ioty_config(const char *addr, unsigned short port);
+
+void* ic_ioty_register_res(const char *uri, iotcon_resource_types_h res_types, int ifaces,
+ uint8_t properties);
+
+int ic_ioty_unregister_res(iotcon_resource_h resource_handle);
+
+int ic_ioty_bind_iface_to_res(void *resource_handle, iotcon_interface_e iface);
+
+int ic_ioty_bind_type_to_res(void *resource_handle, const char *resource_type);
+
+int ic_ioty_bind_res(void *parent, void *child);
+
+int ic_ioty_unbind_res(void *parent, void *child);
+
+int ic_ioty_register_device_info(iotcon_device_info_s device_info);
+
+int ic_ioty_get_device_info(const char *host_address, iotcon_device_info_cb cb,
+ void *user_data);
+
+int ic_ioty_send_notify(void *resource, struct ic_notify_msg *msg,
+ iotcon_observers_h observers);
+int ic_ioty_send_res_response_data(struct ic_resource_response *resp);
+
+const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_address,
+ const char *resource_type,
+ iotcon_presence_cb cb,
+ void *user_data);
+int ic_ioty_unsubscribe_presence(iotcon_presence_h presence_handle);
+int ic_ioty_start_presence(unsigned int time_to_live);
+int ic_ioty_stop_presence();
+
+int ic_ioty_find_resource(const char *host_address, const char *resource_type,
+ iotcon_found_resource_cb cb, void *user_data);
+
+int ic_ioty_get(iotcon_client_h resource, iotcon_query_h query,
+ iotcon_on_cru_cb cb, void *user_data);
+
+int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query,
+ iotcon_on_cru_cb cb, void *user_data);
+
+int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query,
+ iotcon_on_cru_cb cb, void *user_data);
+
+int ic_ioty_delete_res(iotcon_client_h resource,
+ iotcon_on_delete_cb cb, void *user_data);
+
+int ic_ioty_observe(iotcon_client_h resource, iotcon_observe_type_e observe_type,
+ iotcon_query_h query, iotcon_on_observe_cb cb, void *user_data);
+
+int ic_ioty_cancel_observe(iotcon_client_h resource);
+
+int ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest);
+int ic_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest);
+
+
+#endif //__IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_H__
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon-struct.h"
+#include "icl.h"
+
+API void iotcon_observers_free(iotcon_observers_h observers)
+{
+ RET_IF(NULL == observers);
+
+ g_list_free(observers);
+}
+
+
+/* If you want to make a new list, then you should set observers is NULL. */
+API iotcon_observers_h iotcon_observers_append(iotcon_observers_h observers,
+ int obs_id)
+{
+ return g_list_append(observers, GUINT_TO_POINTER(obs_id));
+}
+
+
+API iotcon_observers_h iotcon_observers_remove(iotcon_observers_h observers,
+ int obs_id)
+{
+ RETV_IF(NULL == observers, observers);
+
+ return g_list_remove(observers, GUINT_TO_POINTER(obs_id));
+}
+
--- /dev/null
+/*
+ * 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 <stdbool.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon-struct.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-options.h"
+
+API iotcon_options_h iotcon_options_new()
+{
+ iotcon_options_h options = calloc(1, sizeof(struct ic_options));
+ if (NULL == options) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ options->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free);
+ return options;
+}
+
+
+void ic_options_free(iotcon_options_h options)
+{
+ RET_IF(NULL == options);
+
+ g_hash_table_unref(options->hash);
+ free(options);
+}
+
+
+API void iotcon_options_free(iotcon_options_h options)
+{
+ RET_IF(NULL == options);
+ RETM_IF(true == options->has_parent, "iotcon_options has parent");
+
+ ic_options_free(options);
+}
+
+
+/* iotcon_options_h can have up to 2 options.
+ * option id is always situated between 2048 and 3000.
+ * Length of option data is less than or equal to 15. */
+API int iotcon_options_insert(iotcon_options_h options, unsigned short id,
+ const char *data)
+{
+ FN_CALL;
+
+ RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(options->has_parent, IOTCON_ERROR_INVALID_PARAMETER,
+ "Don't modify it. It is already set.");
+ RETVM_IF(IOTCON_OPTIONS_MAX <= g_hash_table_size(options->hash),
+ IOTCON_ERROR_OUT_OF_MEMORY, "Options already have maximum elements.");
+
+ RETVM_IF(((id < IOTCON_OPTIONID_MIN) || (IOTCON_OPTIONID_MAX < id)),
+ IOTCON_ERROR_INVALID_PARAMETER, "Invalid id(%d)", id);
+
+ RETV_IF(NULL == data, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_OPTION_DATA_LENGTH_MAX < strlen(data), IOTCON_ERROR_INVALID_PARAMETER,
+ "The length of option data(%s) is invalid.", data);
+
+ g_hash_table_insert(options->hash, GUINT_TO_POINTER(id), ic_utils_strdup(data));
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_options_delete(iotcon_options_h options, unsigned short id)
+{
+ gboolean ret;
+
+ RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(options->has_parent, 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) {
+ ERR("g_hash_table_remove() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ return IOTCON_ERROR_NONE;
+}
+
+
+API const char* iotcon_options_lookup(iotcon_options_h options, unsigned short id)
+{
+ const char *ret;
+
+ RETV_IF(NULL == options, NULL);
+
+ ret = g_hash_table_lookup(options->hash, GUINT_TO_POINTER(id));
+ if (NULL == ret)
+ ERR("g_hash_table_lookup() Fail");
+
+ return ret;
+}
+
+
+API int iotcon_options_foreach(iotcon_options_h options,
+ iotcon_options_foreach_cb cb, void *user_data)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+
+ RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ g_hash_table_iter_init(&iter, options->hash);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ if (false == cb(GPOINTER_TO_UINT(key), value, user_data))
+ break;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+iotcon_options_h ic_options_ref(iotcon_options_h options)
+{
+ iotcon_options_h ref;
+
+ RETV_IF(NULL == options, NULL);
+
+ ref = calloc(1, sizeof(struct ic_options));
+ if (NULL == ref) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ ref->hash = g_hash_table_ref(options->hash);
+
+ return ref;
+}
+
--- /dev/null
+/*
+ * 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_OPTIONS_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_OPTIONS_H__
+
+#include "iotcon-struct.h"
+
+struct ic_options {
+ bool has_parent;
+ GHashTable *hash;
+};
+
+void ic_options_free(iotcon_options_h options);
+iotcon_options_h ic_options_ref(iotcon_options_h options);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_OPTIONS_H__ */
--- /dev/null
+/*
+ * 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 <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon-struct.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-query.h"
+
+API iotcon_query_h iotcon_query_new()
+{
+ iotcon_query_h query = calloc(1, sizeof(struct ic_query));
+ if (NULL == query) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ query->hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
+ return query;
+}
+
+
+API void iotcon_query_free(iotcon_query_h query)
+{
+ RET_IF(NULL == query);
+
+ g_hash_table_unref(query->hash);
+ free(query);
+}
+
+
+/* The full length of query should be less than or equal to 64. */
+API int iotcon_query_insert(iotcon_query_h query, const char *key, const char *value)
+{
+ int query_len;
+
+ RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+
+ /* first query : ?key=value
+ * Rest of query : &key=value */
+ query_len = strlen(key) + strlen(value) + 2;
+ if (IOTCON_QUERY_LENGTH_MAX < (query->len + query_len)) {
+ ERR("Length of query is invalid.");
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_insert(query->hash, ic_utils_strdup(key), ic_utils_strdup(value));
+ query->len += query_len;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_query_delete(iotcon_query_h query, const char *key)
+{
+ gboolean ret;
+ int query_len;
+ char *value;
+
+ RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(query->hash, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ query_len = strlen(key) + strlen(value) + 2;
+
+ ret = g_hash_table_remove(query->hash, key);
+ if (FALSE == ret) {
+ ERR("g_hash_table_remove() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ query->len -= query_len;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API const char* iotcon_query_lookup(iotcon_query_h query, const char *key)
+{
+ const char *ret = NULL;
+
+ RETV_IF(NULL == query, NULL);
+ RETV_IF(NULL == key, NULL);
+
+ ret = g_hash_table_lookup(query->hash, key);
+ if (NULL == ret)
+ ERR("g_hash_table_lookup() Fail");
+
+ return ret;
+}
+
+API int iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb,
+ void *user_data)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+
+ RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ g_hash_table_iter_init(&iter, query->hash);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ if (false == cb(key, value, user_data))
+ break;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
--- /dev/null
+/*
+ * 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_QUERY_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_QUERY_H__
+
+#include "iotcon-struct.h"
+
+struct ic_query {
+ int len;
+ GHashTable *hash;
+};
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_QUERY_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <errno.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+#include "iotcon-representation.h"
+#include "icl.h"
+#include "icl-repr-obj.h"
+#include "icl-repr.h"
+#include "icl-repr-value.h"
+#include "icl-repr-list.h"
+
+void ic_list_inc_ref_count(iotcon_list_h val)
+{
+ RET_IF(NULL == val);
+ RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+
+ val->ref_count++;
+}
+
+static bool _ic_list_dec_ref_count(iotcon_list_h val)
+{
+ bool ret;
+
+ RETV_IF(NULL == val, false);
+ RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+
+ val->ref_count--;
+ if (0 == val->ref_count)
+ ret = true;
+ else
+ ret = false;
+
+ return ret;
+}
+
+static iotcon_list_h _ic_list_new(iotcon_types_e type)
+{
+ iotcon_list_h list;
+
+ list = calloc(1, sizeof(struct ic_list_s));
+ if (NULL == list) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+ ic_list_inc_ref_count(list);
+ list->type = type;
+
+ return list;
+}
+
+API iotcon_list_h iotcon_list_new(iotcon_types_e type)
+{
+ if (type < IOTCON_TYPE_INT || IOTCON_TYPE_REPR < type) {
+ ERR("Invalid Type(%d)", type);
+ return NULL;
+ }
+
+ return _ic_list_new(type);
+}
+
+
+API int iotcon_list_insert_int(iotcon_list_h list, int val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_int(val);
+ if (NULL == value) {
+ ERR("ic_value_new_int(%d) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_bool(val);
+ if (NULL == value) {
+ ERR("ic_value_new_bool(%d) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_insert_double(iotcon_list_h list, double val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_double(val);
+ if (NULL == value) {
+ ERR("ic_value_new_double(%f) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_insert_str(iotcon_list_h list, char *val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_str(val);
+ if (NULL == value) {
+ ERR("ic_value_new_str(%s) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_list(val);
+ if (NULL == value) {
+ ERR("ic_value_new_list(%p) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ ic_list_inc_ref_count(val);
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+
+ value = ic_value_new_repr(val);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+ ic_repr_inc_ref_count(val);
+
+ return ic_list_insert(list, value, pos);
+}
+
+
+API int iotcon_list_get_nth_int(iotcon_list_h list, int pos, int *val)
+{
+ int ival, ret;
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_int(value, &ival);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_int() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *val = ival;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_get_nth_bool(iotcon_list_h list, int pos, bool *val)
+{
+ int ret;
+ bool bval;
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_bool(value, &bval);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_bool() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *val = bval;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_get_nth_double(iotcon_list_h list, int pos, double *val)
+{
+ int ret;
+ double dbval;
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_double(value, &dbval);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_double() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *val = dbval;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_get_nth_str(iotcon_list_h list, int pos, const char **val)
+{
+ int ret;
+ const char *strval;
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_str(value, &strval);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_str() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *val = strval;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_get_nth_list(iotcon_list_h src, int pos, iotcon_list_h *dest)
+{
+ int ret;
+ iotcon_value_h value;
+ iotcon_list_h list_val;
+
+ RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == src->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(src->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_list(value, &list_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *dest = list_val;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_get_nth_repr(iotcon_list_h list, int pos, iotcon_repr_h *repr)
+{
+ int ret;
+ iotcon_value_h value;
+ iotcon_repr_h repr_val;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ret = ic_value_get_repr(value, &repr_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ *repr = repr_val;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_types_e value_type)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(value_type != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "IOTCON_ERROR_PARAMETER(%d)", list->type);
+
+ value = g_list_nth_data(list->list, pos);
+ if (NULL == value) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ if (IOTCON_TYPE_STR == value->type) {
+ ic_basic_s *real = (ic_basic_s*)value;
+ free(real->val.s);
+ }
+ else if (IOTCON_TYPE_LIST == value->type) {
+ ic_val_list_s *real = (ic_val_list_s*)value;
+ iotcon_list_free(real->list);
+ }
+ else if (IOTCON_TYPE_REPR == value->type) {
+ ic_val_repr_s *real = (ic_val_repr_s*)value;
+ iotcon_repr_free(real->repr);
+ }
+
+ ic_list_remove(list, value);
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_list_del_nth_int(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_INT);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("iotcon_list_del_nth_int() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_del_nth_bool(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_BOOL);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ic_list_del_nth_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_del_nth_double(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_DOUBLE);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ic_list_del_nth_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_del_nth_str(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_STR);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ic_list_del_nth_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_del_nth_list(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_LIST);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ic_list_del_nth_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_del_nth_repr(iotcon_list_h list, int pos)
+{
+ int ret;
+
+ ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_REPR);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ic_list_del_nth_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_list_get_type(iotcon_list_h list, int *type)
+{
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *type = list->type;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API unsigned int iotcon_list_get_length(iotcon_list_h list)
+{
+ RETV_IF(NULL == list, 0);
+ RETV_IF(NULL == list->list, 0);
+
+ return g_list_length(list->list);
+}
+
+
+int ic_list_remove(iotcon_list_h list, iotcon_value_h val)
+{
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+
+ list->list = g_list_remove(list->list, val);
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos)
+{
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+
+ list->list = g_list_insert(list->list, value, pos);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn,
+ void *user_data)
+{
+ GList *cur;
+ int index = 0;
+ ic_basic_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->val.i, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn,
+ void *user_data)
+{
+ GList *cur;
+ int index = 0;
+ ic_basic_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->val.b, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn,
+ void *user_data)
+{
+ GList *cur;
+ int index = 0;
+ ic_basic_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->val.d, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn,
+ void *user_data)
+{
+ GList *cur;
+ int index = 0;
+ ic_basic_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->val.s, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_list(iotcon_list_h list, iotcon_list_list_fn fn,
+ void *user_data)
+{
+ int index = 0;
+ GList *cur = NULL;
+ ic_val_list_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->list, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data)
+{
+ int index = 0;
+ GList *cur = NULL;
+ ic_val_repr_s *real = NULL;
+
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", list->type);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);;
+
+ cur = list->list;
+ while (cur) {
+ GList *next = cur->next;
+ real = cur->data;
+ if (IOTCON_FUNC_STOP == fn(index, real->repr, user_data))
+ break;
+ index++;
+ cur = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static iotcon_value_h _ic_list_get_nth_value(iotcon_list_h list, int pos)
+{
+ RETV_IF(NULL == list, NULL);
+ RETV_IF(NULL == list->list, NULL);
+
+ return g_list_nth_data(list->list, pos);
+}
+
+
+
+/*
+* A general result : [1,2,3]
+*/
+JsonArray* ic_list_to_json(iotcon_list_h list)
+{
+ int i, ret, count;
+ JsonArray *parray = NULL;
+ JsonNode *child_node = NULL;
+ JsonObject *child_obj = NULL;
+ JsonArray *child_array = NULL;
+ iotcon_repr_h child_repr = NULL;
+ iotcon_list_h child_list = NULL;
+ iotcon_value_h child_value = NULL;
+
+ RETV_IF(NULL == list, NULL);
+ RETV_IF(NULL == list->list, NULL);
+
+ count = g_list_length(list->list);
+
+ parray = json_array_new();
+ for (i = 0; i < count; i++) {
+ child_value = _ic_list_get_nth_value(list, i);
+
+ int type = child_value->type;
+ switch (type) {
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_STR:
+ case IOTCON_TYPE_NULL:
+ child_node = ic_value_to_json(child_value);
+ if (NULL == child_node) {
+ ERR("ic_value_to_json(child_value) Fail");
+ json_array_unref(parray);
+ return NULL;
+ }
+ json_array_add_element(parray, child_node);
+ break;
+ case IOTCON_TYPE_LIST:
+ ret = ic_value_get_list(child_value, &child_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ json_array_unref(parray);
+ return NULL;
+ }
+
+ child_array = ic_list_to_json(child_list);
+ if (NULL == child_array) {
+ ERR("ic_list_to_json(child_list) Fail");
+ json_array_unref(parray);
+ return NULL;
+ }
+ child_node = json_node_new(JSON_NODE_ARRAY);
+ json_node_set_array(child_node, child_array);
+ json_array_add_element(parray, child_node);
+ break;
+ case IOTCON_TYPE_REPR:
+ ret = ic_value_get_repr(child_value, &child_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_repr() Fail(%d)", ret);
+ json_array_unref(parray);
+ return NULL;
+ }
+
+ child_obj = ic_obj_to_json(child_repr);
+ if (NULL == child_obj) {
+ ERR("ic_obj_to_json(child_repr) Fail");
+ json_array_unref(parray);
+ return NULL;
+ }
+ child_node = json_node_new(JSON_NODE_OBJECT);
+ json_node_set_object(child_node, child_obj);
+ json_array_add_element(parray, child_node);
+ }
+ }
+
+ return parray;
+}
+
+/*
+* A general input : [1,2,3]
+*/
+iotcon_list_h ic_list_from_json(JsonArray *parray)
+{
+ int i, ret;
+ int count = json_array_get_length(parray);
+
+ iotcon_list_h list = _ic_list_new(IOTCON_TYPE_NONE);
+ if (NULL == list) {
+ ERR("_ic_list_new() Fail");
+ return NULL;
+ }
+
+ for (i = 0; i < count; i++) {
+ JsonNode *child_node = json_array_get_element(parray, i);
+ if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) {
+ iotcon_value_h value = ic_value_from_json(child_node);
+ if (NULL == value) {
+ ERR("ic_value_from_json() Fail");
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ ic_basic_s *real = (ic_basic_s*)value;
+ if (IOTCON_TYPE_NONE != list->type && list->type != real->type) {
+ ERR("Type matching Fail(list:%d,value:%d)", list->type, real->type);
+ ic_value_free(value);
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ ret = ic_list_insert(list, value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert() Fail(%d)", ret);
+ ic_value_free(value);
+ iotcon_list_free(list);
+ return NULL;
+ }
+ list->type = real->type;
+ }
+ else if (JSON_NODE_HOLDS_ARRAY(child_node)) {
+ if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_LIST != list->type) {
+ ERR("Type matching Fail(%d)", list->type);
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ JsonArray *child_array = json_node_get_array(child_node);
+ iotcon_list_h parsed_list = ic_list_from_json(child_array);
+ if (NULL == parsed_list) {
+ ERR("ic_list_from_json() Fail(NULL == parsed_list)");
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ iotcon_value_h value = ic_value_new_list(parsed_list);
+ if (NULL == value) {
+ ERR("ic_value_new_list(%p) Fail", parsed_list);
+ iotcon_list_free(parsed_list);
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ ret = ic_list_insert(list, value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert() Fail(%d)", ret);
+ iotcon_list_free(parsed_list);
+ iotcon_list_free(list);
+ return NULL;
+ }
+ list->type = IOTCON_TYPE_LIST;
+ }
+ else if (JSON_NODE_HOLDS_OBJECT(child_node)) {
+ if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_REPR != list->type) {
+ ERR("Type matching Fail(%d)", list->type);
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ JsonObject *child_obj = json_node_get_object(child_node);
+ iotcon_repr_h ret_repr = ic_obj_from_json(child_obj);
+ if (NULL == ret_repr) {
+ ERR("ic_obj_from_json() Fail(NULL == ret_repr)");
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ iotcon_value_h value = ic_value_new_repr(ret_repr);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", ret_repr);
+ iotcon_repr_free(ret_repr);
+ iotcon_list_free(list);
+ return NULL;
+ }
+
+ ret = ic_list_insert(list, value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert() Fail(%d)", ret);
+ iotcon_repr_free(ret_repr);
+ iotcon_list_free(list);
+ return NULL;
+ }
+ list->type = IOTCON_TYPE_REPR;
+ }
+ }
+
+ return list;
+}
+
+API void iotcon_list_free(iotcon_list_h list)
+{
+ FN_CALL;
+ GList *cur = NULL;
+
+ RET_IF(NULL == list);
+
+ if (false == _ic_list_dec_ref_count(list))
+ return;
+
+ cur = list->list;
+ while (cur) {
+ ic_value_free(cur->data);
+ cur = cur->next;
+ }
+ free(list);
+}
+
+static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list)
+{
+ int i, ret, count;
+ iotcon_value_h value, copied_value;
+
+ count = g_list_length(list->list);
+ for (i = 0; i < count; i++) {
+ value = _ic_list_get_nth_value(list, i);
+ if (NULL == value) {
+ ERR("_ic_list_get_nth_value() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ if (list->type != value->type) {
+ ERR("Type Mismatching(list:%d, value:%d)", list->type, value->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ copied_value = ic_value_clone(value);
+ if (NULL == copied_value) {
+ ERR("ic_value_clone() Fail");
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ ret = ic_list_insert(ret_list, copied_value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert() Fail");
+ ic_value_free(copied_value);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list)
+{
+ int i, ret, count;
+
+ iotcon_value_h value;
+ iotcon_list_h list_val, copied_list;
+
+ count = g_list_length(list->list);
+ for (i = 0; i < count; i++) {
+ ret = iotcon_list_get_nth_list(list, i, &list_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_get_nth_list() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ copied_list = ic_list_clone(list_val);
+ if (NULL == copied_list) {
+ ERR("ic_list_clone() Fail");
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ value = ic_value_new_list(copied_list);
+ if (NULL == value) {
+ ERR("ic_value_new_list(%p) Fail", copied_list);
+ iotcon_list_free(copied_list);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ ret = ic_list_insert(ret_list, value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert(%d) Fail", ret);
+ ic_value_free(value);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list)
+{
+ int i, ret, count;
+ iotcon_value_h value;
+ iotcon_repr_h repr_val, copied_repr;
+
+ count = g_list_length(list->list);
+ for (i = 0; i < count; i++) {
+ ret = iotcon_list_get_nth_repr(list, i, &repr_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_get_nth_repr() Fail");
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ copied_repr = iotcon_repr_clone(repr_val);
+ if (NULL == copied_repr) {
+ ERR("_ic_repr_clone_repr() Fail");
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ value = ic_value_new_repr(copied_repr);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", copied_repr);
+ iotcon_repr_free(copied_repr);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ ret = ic_list_insert(ret_list, value, -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_list_insert(%d) Fail", ret);
+ ic_value_free(value);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+iotcon_list_h ic_list_clone(iotcon_list_h list)
+{
+ int ret;
+ iotcon_list_h ret_list = NULL;
+
+ RETV_IF(NULL == list, NULL);
+ RETV_IF(NULL == list->list, NULL);
+
+ ret_list = iotcon_list_new(list->type);
+ if (NULL == ret_list) {
+ ERR("iotcon_list_new(%d) Fail", list->type);
+ return NULL;
+ }
+
+ switch (list->type) {
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_STR:
+ case IOTCON_TYPE_NULL:
+ ret = _ic_list_clone_value(list, ret_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_list_clone_value() Fail(%d)", ret);
+ iotcon_list_free(ret_list);
+ return NULL;
+ }
+ break;
+ case IOTCON_TYPE_LIST:
+ ret = _ic_list_clone_list(list, ret_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_list_clone_list() Fail(%d)", ret);
+ iotcon_list_free(ret_list);
+ return NULL;
+ }
+ break;
+ case IOTCON_TYPE_REPR:
+ ret = _ic_list_clone_repr(list, ret_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_list_clone_repr() Fail(%d)", ret);
+ iotcon_list_free(ret_list);
+ return NULL;
+ }
+ break;
+ default:
+ ERR("Invalid type(%d)", list->type);
+ iotcon_list_free(ret_list);
+ return NULL;
+ }
+
+ return ret_list;
+}
--- /dev/null
+/*
+ * 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_REPRESENTATION_LIST_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_LIST_H__
+
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+
+struct ic_list_s {
+ int type;
+ int ref_count;
+ GList *list;
+};
+
+int ic_list_remove(iotcon_list_h list, iotcon_value_h val);
+int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos);
+
+JsonArray* ic_list_to_json(iotcon_list_h list);
+iotcon_list_h ic_list_from_json(JsonArray *parray);
+
+iotcon_list_h ic_list_clone(iotcon_list_h list);
+
+void ic_list_inc_ref_count(iotcon_list_h val);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_LIST_H__ */
--- /dev/null
+/* 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 <glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+#include "iotcon-representation.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-repr-list.h"
+#include "icl-repr-value.h"
+#include "icl-repr.h"
+#include "icl-repr-obj.h"
+
+int ic_obj_del_value(iotcon_repr_h repr, const char *key,
+ iotcon_types_e value_type)
+{
+ gboolean ret = FALSE;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup(%s) Fail", key);
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ if (value_type != value->type) {
+ ERR("Type matching Fail(input:%d, saved:%d)", value_type, value->type);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ ret = g_hash_table_remove(repr->hash_table, key);
+ if (FALSE == ret) {
+ ERR("g_hash_table_remove(%s) Fail", key);
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key, int *val)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ ic_basic_s *real = (ic_basic_s*)value;
+ if (IOTCON_TYPE_INT != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *val = real->val.i;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int val)
+{
+ iotcon_value_h value;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_int(val);
+ if (NULL == value) {
+ ERR("ic_value_new_int(%d) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_int(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_INT);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API int iotcon_repr_get_bool(iotcon_repr_h repr, const char *key, bool *val)
+{
+ ic_basic_s *real = NULL;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ real = (ic_basic_s*)value;
+ if (IOTCON_TYPE_BOOL != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *val = real->val.b;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool val)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_bool(val);
+ if (NULL == value) {
+ ERR("ic_value_new_bool(%d) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_bool(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_BOOL);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API int iotcon_repr_get_double(iotcon_repr_h repr, const char *key, double *val)
+{
+ ic_basic_s *real = NULL;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ real = (ic_basic_s*)value;
+ if (IOTCON_TYPE_DOUBLE != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *val = real->val.d;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double val)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_double(val);
+ if (NULL == value) {
+ ERR("ic_value_new_double(%f) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_double(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_DOUBLE);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API int iotcon_repr_get_str(iotcon_repr_h repr, const char *key, char **val)
+{
+ ic_basic_s *real = NULL;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ real = (ic_basic_s*)value;
+ if (IOTCON_TYPE_STR != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *val = real->val.s;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_str(val);
+ if (NULL == value) {
+ ERR("ic_value_new_str(%s) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_str(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_STR);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key)
+{
+ ic_basic_s *real = NULL;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, false);
+ RETV_IF(NULL == key, false);
+
+ value = (iotcon_value_h) g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return false;
+ }
+
+ real = (ic_basic_s*)value;
+
+ return (IOTCON_TYPE_NULL == real->type) ? true : false;
+}
+
+API int iotcon_repr_set_null(iotcon_repr_h repr, const char *key)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_null();
+ if (NULL == value) {
+ ERR("ic_value_new_null() Fail");
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_null(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_NULL);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API int iotcon_repr_get_list(iotcon_repr_h repr, const char *key, iotcon_list_h *list)
+{
+ iotcon_value_h value = NULL;
+ ic_val_list_s *real = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ real = (ic_val_list_s*)value;
+ if (IOTCON_TYPE_LIST != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *list = real->list;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h list)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_list(list);
+ if (NULL == value) {
+ ERR("ic_value_new_list() Fail");
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+ ic_list_inc_ref_count(list);
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_list(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_LIST);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_repr_get_repr(iotcon_repr_h src, const char *key, iotcon_repr_h *dest)
+{
+ ic_val_repr_s *real = NULL;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(src->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ real = (ic_val_repr_s*)value;
+ if (IOTCON_TYPE_REPR != real->type) {
+ ERR("Invalid Type(%d)", real->type);
+ return IOTCON_ERROR_INVALID_TYPE;
+ }
+
+ *dest = real->repr;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_repr(iotcon_repr_h repr, const char *key, iotcon_repr_h val)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = ic_value_new_repr(val);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", val);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+ ic_repr_inc_ref_count(val);
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_del_repr(iotcon_repr_h repr, const char *key)
+{
+ int ret;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_obj_del_value(repr, key, IOTCON_TYPE_REPR);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_obj_del_value() Fail(%d)", ret);
+
+ return ret;
+}
+
+API int iotcon_repr_get_type(iotcon_repr_h repr, const char *key, int *type)
+{
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_hash_table_lookup(repr->hash_table, key);
+ if (NULL == value) {
+ ERR("g_hash_table_lookup() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+ *type = value->type;
+
+ return IOTCON_ERROR_NONE;
+}
+
+int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+
+ g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
+
+ return IOTCON_ERROR_NONE;
+}
+
+static inline int _ic_obj_to_json(const char *key, iotcon_value_h value,
+ JsonObject *json_obj)
+{
+ FN_CALL;
+ int type, ret;
+ iotcon_repr_h child_repr = NULL;
+ iotcon_list_h child_list = NULL;
+
+ JsonObject *child_obj = NULL;
+ JsonNode *child_node = NULL;
+ JsonArray *child_array = NULL;
+
+ RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+
+ type = value->type;
+ switch (type) {
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_STR:
+ case IOTCON_TYPE_NULL:
+ child_node = ic_value_to_json(value);
+ if (NULL == child_node) {
+ ERR("ic_value_to_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ json_object_set_member(json_obj, key, child_node);
+ break;
+ case IOTCON_TYPE_LIST:
+ ret = ic_value_get_list(value, &child_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ child_array = ic_list_to_json(child_list);
+ if (NULL == child_array) {
+ ERR("ic_list_to_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ child_node = json_node_new(JSON_NODE_ARRAY);
+ json_node_set_array(child_node, child_array);
+ json_object_set_member(json_obj, key, child_node);
+ break;
+ case IOTCON_TYPE_REPR:
+ ret = ic_value_get_repr(value, &child_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_repr() Fail(%d)", ret);
+ return IOTCON_ERROR_REPRESENTATION;
+ }
+
+ child_obj = ic_obj_to_json(child_repr);
+ if (NULL == child_obj) {
+ ERR("ic_obj_to_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ child_node = json_node_new(JSON_NODE_OBJECT);
+ json_node_set_object(child_node, child_obj);
+ json_object_set_member(json_obj, key, child_node);
+ break;
+ default:
+ ERR("Invalid type(%d)", type);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+/*
+* A general result : {"rep":{"string":"Hello","intlist":[1,2,3]}}
+*/
+JsonObject* ic_obj_to_json(iotcon_repr_h repr)
+{
+ int ret;
+ int key_count;
+ JsonObject *json_obj = NULL;
+ JsonObject *parent_obj = NULL;
+
+ GHashTableIter iter;
+ gpointer key, value;
+
+ RETV_IF(NULL == repr, NULL);
+ RETV_IF(NULL == repr->hash_table, NULL);
+
+ key_count = iotcon_repr_get_keys_count(repr);
+ if (key_count) {
+ json_obj = json_object_new();
+
+ g_hash_table_iter_init(&iter, repr->hash_table);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ ret = _ic_obj_to_json(key, value, json_obj);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_obj_to_json() Fail(%d)", ret);
+ json_object_unref(json_obj);
+ return NULL;
+ }
+ }
+ }
+
+ parent_obj = json_object_new();
+
+ if (json_obj)
+ json_object_set_object_member(parent_obj, IOTCON_KEY_REP, json_obj);
+
+ return parent_obj;
+}
+
+static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned int index,
+ iotcon_repr_h ret_repr)
+{
+ char *key;
+
+ RETV_IF(NULL == obj, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == key_list, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ key = g_list_nth_data(key_list, index);
+
+ /* search child object recursively */
+ JsonNode *child_node = json_object_get_member(obj, key);
+ if (NULL == child_node) {
+ ERR("json_object_get_member() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) {
+ iotcon_value_h value = ic_value_from_json(child_node);
+ if (NULL == value) {
+ ERR("ic_value_from_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ ic_obj_set_value(ret_repr, key, value);
+ }
+ else if (JSON_NODE_HOLDS_ARRAY(child_node)) {
+ iotcon_value_h value;
+ JsonArray *child_array = json_node_get_array(child_node);
+
+ iotcon_list_h list = ic_list_from_json(child_array);
+ if (NULL == list) {
+ ERR("ic_list_from_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ value = ic_value_new_list(list);
+ if (NULL == value) {
+ ERR("ic_value_new_list() Fail");
+ iotcon_list_free(list);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ ic_obj_set_value(ret_repr, key, value);
+ }
+ else if (JSON_NODE_HOLDS_OBJECT(child_node)) {
+ iotcon_value_h value;
+ JsonObject *child_obj = json_node_get_object(child_node);
+
+ iotcon_repr_h repr = ic_obj_from_json(child_obj);
+ if (NULL == repr) {
+ ERR("ic_obj_from_json() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ value = ic_value_new_repr(repr);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", repr);
+ iotcon_repr_free(repr);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ ic_obj_set_value(ret_repr, key, value);
+ }
+ else {
+ ERR("node type(%d) Fail", json_node_get_node_type(child_node));
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+/*
+* A general input : {"rep:"{"string":"Hello","intlist":[1,2,3]}}
+*/
+iotcon_repr_h ic_obj_from_json(JsonObject *json_repr)
+{
+ int ret;
+ unsigned int i = 0;
+ GList *key_list = NULL;
+ iotcon_repr_h repr = NULL;
+ JsonObject *obj = NULL;
+
+ RETV_IF(NULL == json_repr, NULL);
+
+ obj = json_object_get_object_member(json_repr, IOTCON_KEY_REP);
+
+ key_list = json_object_get_members(obj);
+
+ repr = iotcon_repr_new();
+ if (key_list) {
+ for (i = 0; i < g_list_length(key_list); i++) {
+ ret = _ic_obj_from_json(obj, key_list, i, repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ic_obj_from_json() Fail(%d)", ret);
+ iotcon_repr_free(repr);
+ g_list_free(key_list);
+ return NULL;
+ }
+ }
+ g_list_free(key_list);
+ }
+
+ return repr;
+}
+
--- /dev/null
+/*
+ * 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_REPRESENTATION_OBJECT_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_OBJECT_H__
+
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+
+int ic_obj_del_value(iotcon_repr_h repr, const char *key,
+ iotcon_types_e value_type);
+
+int ic_repr_obj_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval);
+int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value);
+
+JsonObject* ic_obj_to_json(iotcon_repr_h repr);
+iotcon_repr_h ic_obj_from_json(JsonObject *obj);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_OBJECT_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <errno.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-representation.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-repr.h"
+#include "icl-repr-list.h"
+#include "icl-repr-value.h"
+
+static iotcon_value_h _ic_value_new(int type)
+{
+ iotcon_value_h ret_val;
+
+ switch (type) {
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_STR:
+ case IOTCON_TYPE_NULL:
+ ret_val = calloc(1, sizeof(ic_basic_s));
+ break;
+ case IOTCON_TYPE_LIST:
+ ret_val = calloc(1, sizeof(ic_val_list_s));
+ break;
+ case IOTCON_TYPE_REPR:
+ ret_val = calloc(1, sizeof(ic_val_repr_s));
+ break;
+ default:
+ ERR("Invalid Type(%d)", type);
+ return NULL;
+ }
+
+ if (NULL == ret_val) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ ret_val->type = type;
+
+ return ret_val;
+}
+
+
+iotcon_value_h ic_value_new_null()
+{
+ iotcon_value_h value;
+
+ value = _ic_value_new(IOTCON_TYPE_NULL);
+ if (NULL == value) {
+ ERR("_ic_value_new(NULL) Fail");
+ return NULL;
+ }
+
+ return value;
+}
+
+iotcon_value_h ic_value_new_int(int val)
+{
+ ic_basic_s *value;
+
+ value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_INT);
+ if (NULL == value) {
+ ERR("_ic_value_new(INT:%d) Fail", val);
+ return NULL;
+ }
+
+ value->val.i = val;
+
+ return (iotcon_value_h)value;
+}
+
+iotcon_value_h ic_value_new_bool(bool val)
+{
+ ic_basic_s *value;
+
+ value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_BOOL);
+ if (NULL == value) {
+ ERR("_ic_value_new(BOOL:%d) Fail", val);
+ return NULL;
+ }
+
+ value->val.b = val;
+
+ return (iotcon_value_h)value;
+}
+
+iotcon_value_h ic_value_new_double(double val)
+{
+ ic_basic_s *value;
+
+ value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_DOUBLE);
+ if (NULL == value) {
+ ERR("_ic_value_new(DOUBLE:%f) Fail", val);
+ return NULL;
+ }
+
+ value->val.d = val;
+
+ return (iotcon_value_h)value;
+}
+
+iotcon_value_h ic_value_new_str(char *val)
+{
+ ic_basic_s *value;
+
+ RETV_IF(NULL == val, NULL);
+
+ value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_STR);
+ if (NULL == value) {
+ ERR("_ic_value_new(STR:%s) Fail", val);
+ return NULL;
+ }
+
+ value->val.s = ic_utils_strdup(val);
+
+ return (iotcon_value_h)value;
+}
+
+
+iotcon_value_h ic_value_new_list(iotcon_list_h val)
+{
+ ic_val_list_s *value;
+
+ value = (ic_val_list_s*)_ic_value_new(IOTCON_TYPE_LIST);
+ if (NULL == value) {
+ ERR("_ic_value_new(LIST) Fail");
+ return NULL;
+ }
+
+ value->list = val;
+
+ return (iotcon_value_h)value;
+}
+
+iotcon_value_h ic_value_new_repr(iotcon_repr_h val)
+{
+ ic_val_repr_s *value;
+
+ value = (ic_val_repr_s*)_ic_value_new(IOTCON_TYPE_REPR);
+ if (NULL == value) {
+ ERR("_ic_value_new(REPR) Fail");
+ return NULL;
+ }
+
+ value->repr = val;
+
+ return (iotcon_value_h)value;
+}
+
+int ic_value_get_int(iotcon_value_h value, int *val)
+{
+ ic_basic_s *real = (ic_basic_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *val = real->val.i;
+
+ return IOTCON_ERROR_NONE;
+}
+
+int ic_value_get_bool(iotcon_value_h value, bool *val)
+{
+ ic_basic_s *real = (ic_basic_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *val = real->val.b;
+
+ return IOTCON_ERROR_NONE;
+}
+
+int ic_value_get_double(iotcon_value_h value, double *val)
+{
+ ic_basic_s *real = (ic_basic_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *val = real->val.d;
+
+ return IOTCON_ERROR_NONE;
+}
+
+int ic_value_get_str(iotcon_value_h value, const char **val)
+{
+ ic_basic_s *real = (ic_basic_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *val = real->val.s;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list)
+{
+ ic_val_list_s *real = (ic_val_list_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *list = real->list;
+
+ return IOTCON_ERROR_NONE;
+}
+
+int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr)
+{
+ ic_val_repr_s *real = (ic_val_repr_s*)value;
+
+ RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERROR_INVALID_PARAMETER,
+ "Invalid Type(%d)", real->type);
+
+ *repr = real->repr;
+
+ return IOTCON_ERROR_NONE;
+}
+
+/*
+* A general result : 1
+* : true
+* : 5.5
+* : "Hello"
+*/
+JsonNode* ic_value_to_json(iotcon_value_h value)
+{
+ JsonNode *node;
+ ic_basic_s *real = (ic_basic_s*)value;
+
+ RETV_IF(NULL == value, NULL);
+
+ if (IOTCON_TYPE_NULL == value->type)
+ node = json_node_new(JSON_NODE_NULL);
+ else
+ node = json_node_new(JSON_NODE_VALUE);
+
+ if (NULL == node) {
+ ERR("json_node_new(%d) Fail", value->type);
+ return NULL;
+ }
+
+ switch (value->type) {
+ case IOTCON_TYPE_INT:
+ json_node_set_int(node, real->val.i);
+ break;
+ case IOTCON_TYPE_BOOL:
+ json_node_set_boolean(node, real->val.b);
+ break;
+ case IOTCON_TYPE_DOUBLE:
+ json_node_set_double(node, real->val.d);
+ break;
+ case IOTCON_TYPE_STR:
+ json_node_set_string(node, real->val.s);
+ break;
+ case IOTCON_TYPE_NULL:
+ break;
+ default:
+ ERR("Invalid type(%d)", value->type);
+ break;
+ }
+
+ return node;
+}
+
+/*
+* A general result : 1
+* : true
+* : 5.5
+* : "Hello"
+*/
+API iotcon_value_h ic_value_from_json(JsonNode *node)
+{
+ gint64 ival64;
+ GType gtype = 0;
+ iotcon_value_h value = NULL;
+
+ RETV_IF(NULL == node, NULL);
+
+ if (JSON_NODE_HOLDS_NULL(node)) {
+ value = ic_value_new_null();
+ if (NULL == value)
+ ERR("ic_value_new_null() Fail");
+ return value;
+ }
+
+ gtype = json_node_get_value_type(node);
+ switch (gtype) {
+ case G_TYPE_INT64:
+ ival64 = json_node_get_int(node);
+ if (INT_MAX < ival64 || ival64 < INT_MIN) {
+ ERR("value SHOULD NOT exceeds the integer range. ival64(%lld)", ival64);
+ return NULL;
+ }
+ value = ic_value_new_int(ival64);
+ if (NULL == value)
+ ERR("ic_value_new_int(%ll) Fail", ival64);
+ break;
+ case G_TYPE_BOOLEAN:
+ value = ic_value_new_bool(json_node_get_boolean(node));
+ if (NULL == value)
+ ERR("ic_value_new_bool() Fail");
+ break;
+ case G_TYPE_DOUBLE:
+ value = ic_value_new_double(json_node_get_double(node));
+ if (NULL == value)
+ ERR("ic_value_new_double() Fail");
+ break;
+ case G_TYPE_STRING:
+ value = ic_value_new_str(ic_utils_strdup(json_node_get_string(node)));
+ if (NULL == value)
+ ERR("ic_value_new_str() Fail");
+ break;
+ default:
+ ERR("Invalid type(%d)", gtype);
+ break;
+ }
+
+ return value;
+}
+
+void ic_value_free(gpointer data)
+{
+ FN_CALL;
+ int ret;
+ const char *str;
+ iotcon_value_h value;
+ iotcon_list_h list;
+ iotcon_repr_h repr;
+
+ RET_IF(NULL == data);
+
+ value = data;
+
+ int type = value->type;
+ switch (type) {
+ case IOTCON_TYPE_STR:
+ ret = ic_value_get_str(value, &str);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_str() Fail(%d)", ret);
+ break;
+ }
+ free((char*)str);
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_NULL:
+ break;
+ case IOTCON_TYPE_LIST:
+ DBG("value is list");
+ ret = ic_value_get_list(value, &list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ break;
+ }
+ iotcon_list_free(list);
+ break;
+ case IOTCON_TYPE_REPR:
+ DBG("value is Repr");
+ ret = ic_value_get_repr(value, &repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_repr() Fail(%d)", ret);
+ break;
+ }
+ iotcon_repr_free(repr);
+ break;
+ default:
+ ERR("Invalid type(%d)", type);
+ break;
+ }
+ free(value);
+}
+
+
+iotcon_value_h ic_value_clone(iotcon_value_h src)
+{
+ iotcon_value_h dest = NULL;
+ ic_basic_s *real = (ic_basic_s*)src;
+
+ RETV_IF(NULL == src, NULL);
+
+ switch (src->type) {
+ case IOTCON_TYPE_INT:
+ dest = ic_value_new_int(real->val.i);
+ break;
+ case IOTCON_TYPE_BOOL:
+ dest = ic_value_new_bool(real->val.b);
+ break;
+ case IOTCON_TYPE_DOUBLE:
+ dest = ic_value_new_double(real->val.d);
+ break;
+ case IOTCON_TYPE_STR:
+ dest = ic_value_new_str(ic_utils_strdup(real->val.s));
+ break;
+ case IOTCON_TYPE_NULL:
+ dest = ic_value_new_null();
+ break;
+ default:
+ ERR("Invalid type(%d)", src->type);
+ break;
+ }
+
+ if (NULL == dest)
+ ERR("ic_value_new_xxx(%d) Fail", src->type);
+
+ return dest;
+}
--- /dev/null
+/*
+ * 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_REPRESENTATION_VALUE_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_VALUE_H__
+
+#include <stdbool.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+
+struct ic_value_s {
+ int type;
+};
+
+typedef struct {
+ int type;
+ union {
+ int i;
+ bool b;
+ double d;
+ char *s;
+ } val;
+} ic_basic_s;
+
+typedef struct {
+ int type;
+ struct ic_list_s *list;
+} ic_val_list_s;
+
+typedef struct {
+ int type;
+ struct ic_repr_s *repr;
+} ic_val_repr_s;
+
+iotcon_value_h ic_value_new_null();
+iotcon_value_h ic_value_new_int(int val);
+iotcon_value_h ic_value_new_bool(bool val);
+iotcon_value_h ic_value_new_double(double val);
+iotcon_value_h ic_value_new_str(char *val);
+iotcon_value_h ic_value_new_list(iotcon_list_h val);
+iotcon_value_h ic_value_new_repr(iotcon_repr_h val);
+
+
+int ic_value_get_int(iotcon_value_h value, int *val);
+int ic_value_get_bool(iotcon_value_h value, bool *val);
+int ic_value_get_double(iotcon_value_h value, double *val);
+int ic_value_get_str(iotcon_value_h value, const char **val);
+int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list);
+int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr);
+
+
+JsonNode* ic_value_to_json(iotcon_value_h value);
+iotcon_value_h ic_value_from_json(JsonNode *node);
+void ic_value_free(gpointer data);
+
+iotcon_value_h ic_value_clone(iotcon_value_h src);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_VALUE_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-representation.h"
+#include "icl.h"
+#include "icl-utils.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"
+#include "icl-repr.h"
+
+void ic_repr_inc_ref_count(iotcon_repr_h val)
+{
+ RET_IF(NULL == val);
+ RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+
+ val->ref_count++;
+}
+
+static bool _ic_repr_dec_ref_count(iotcon_repr_h val)
+{
+ bool ret;
+
+ RETV_IF(NULL == val, -1);
+ RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+
+ val->ref_count--;
+ if (0 == val->ref_count)
+ ret = true;
+ else
+ ret = false;
+
+ return ret;
+}
+
+API iotcon_repr_h iotcon_repr_new()
+{
+ iotcon_repr_h ret_val;
+ errno = 0;
+
+ ret_val = calloc(1, sizeof(struct ic_repr_s));
+ if (NULL == ret_val) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ ret_val->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, free,
+ ic_value_free);
+ ic_repr_inc_ref_count(ret_val);
+
+ return ret_val;
+}
+
+API int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *uri = repr->uri;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ free(repr->uri);
+ repr->uri = NULL;
+
+ if (NULL == uri)
+ return IOTCON_ERROR_NONE;
+
+ repr->uri = strdup(uri);
+ if (NULL == repr->uri) {
+ ERR("strdup() Fail");
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resource_types_h *types)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *types = repr->res_types;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_set_resource_types(iotcon_repr_h repr, iotcon_resource_types_h types)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ iotcon_resource_types_free(repr->res_types);
+ repr->res_types = NULL;
+
+ if (types)
+ repr->res_types = ic_resource_types_ref(types);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_get_resource_interfaces(iotcon_repr_h repr)
+{
+ RETV_IF(NULL == repr, IOTCON_INTERFACE_NONE);
+
+ return repr->interfaces;
+}
+
+API int iotcon_repr_set_resource_interfaces(iotcon_repr_h repr, int ifaces)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ RETV_IF(ifaces <= IOTCON_INTERFACE_NONE || IC_INTERFACE_MAX < ifaces,
+ IOTCON_ERROR_INVALID_PARAMETER);
+
+ repr->interfaces = ifaces;
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child)
+{
+ RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ic_repr_inc_ref_count(child);
+ parent->children = g_list_append(parent->children, child);
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_foreach_children(iotcon_repr_h parent, iotcon_children_fn fn,
+ void *user_data)
+{
+ GList *list, *next;
+
+ RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ list = parent->children;
+ while (list) {
+ next = list->next;
+ if (IOTCON_FUNC_STOP == fn(list->data, user_data))
+ break;
+ list = next;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API unsigned int iotcon_repr_get_children_count(iotcon_repr_h parent)
+{
+ RETV_IF(NULL == parent, 0);
+ RETV_IF(NULL == parent->children, 0);
+
+ return g_list_length(parent->children);
+}
+
+API int iotcon_repr_get_nth_child(iotcon_repr_h parent, int pos, iotcon_repr_h *child)
+{
+ RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *child = g_list_nth_data(parent->children, pos);
+ if (NULL == *child) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_foreach_keys(iotcon_repr_h repr, iotcon_repr_key_fn fn,
+ void *user_data)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ g_hash_table_iter_init(&iter, repr->hash_table);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ if (IOTCON_FUNC_STOP == fn(key, value, user_data))
+ break;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+API int iotcon_repr_get_keys_count(iotcon_repr_h repr)
+{
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == repr->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
+
+ return g_hash_table_size(repr->hash_table);
+}
+
+static int _ic_repr_get_res_type_fn(const char *res_type, void *user_data)
+{
+ JsonArray *rt_array = user_data;
+ json_array_add_string_element(rt_array, res_type);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+/*
+ * A general result : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
+ */
+static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr,
+ unsigned int child_index)
+{
+ int i, ret, ifaces;
+ char *iface_str;
+ const char *uri;
+ JsonObject *repr_obj = NULL;
+ unsigned int rt_count = 0;
+ JsonObject *prop_obj = NULL;
+ iotcon_resource_types_h resource_types = NULL;
+
+ RETV_IF(NULL == cur_repr, NULL);
+
+ if (0 < iotcon_repr_get_keys_count(cur_repr)) {
+ repr_obj = ic_obj_to_json(cur_repr);
+ if (NULL == repr_obj) {
+ ERR("ic_obj_to_json() Fail");
+ json_object_unref(repr_obj);
+ return NULL;
+ }
+ }
+ else {
+ repr_obj = json_object_new();
+ }
+
+ if (cur_repr->uri) {
+ iotcon_repr_get_uri(cur_repr, &uri);
+ json_object_set_string_member(repr_obj, IOTCON_KEY_URI, uri);
+ }
+
+ if (cur_repr->res_types)
+ rt_count = ic_resource_types_get_length(cur_repr->res_types);
+
+ if (0 < rt_count || IOTCON_INTERFACE_NONE != cur_repr->interfaces) {
+ prop_obj = json_object_new();
+ json_object_set_object_member(repr_obj, IOTCON_KEY_PROPERTY, prop_obj);
+ }
+
+ if (0 < rt_count) {
+ JsonArray *rt_array = json_array_new();
+
+ ret = iotcon_repr_get_resource_types(cur_repr, &resource_types);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_repr_get_resource_types() Fail(%d)", ret);
+ json_object_unref(repr_obj);
+ return NULL;
+ }
+
+ ret = iotcon_resource_types_foreach(resource_types, _ic_repr_get_res_type_fn,
+ rt_array);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_types_foreach() Fail");
+ json_object_unref(repr_obj);
+ return NULL;
+ }
+ json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array);
+ }
+
+ if (IOTCON_INTERFACE_NONE != cur_repr->interfaces) {
+ JsonArray *if_array = json_array_new();
+ ifaces = iotcon_repr_get_resource_interfaces(cur_repr);
+ for (i = 1; i <= IC_INTERFACE_MAX; i = i << 1) {
+ if (IOTCON_INTERFACE_NONE == (ifaces & i)) /* this interface not exist */
+ continue;
+ ret = ic_ioty_convert_interface_flag((ifaces & i), &iface_str);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_ioty_convert_interface_flag(%d) Fail(%d)", i, ret);
+ json_object_unref(repr_obj);
+ json_array_unref(if_array);
+ return NULL;
+ }
+ json_array_add_string_element(if_array, iface_str);
+ }
+
+ json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array);
+ }
+
+ FN_END;
+ return repr_obj;
+}
+
+static JsonObject* _ic_repr_data_generate_parent(iotcon_repr_h cur_repr,
+ unsigned int child_index)
+{
+ FN_CALL;
+ JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index);
+ if (NULL == obj) {
+ ERR("_ic_repr_data_generate_json() Fail");
+ return NULL;
+ }
+
+ return obj;
+}
+
+static JsonObject* _ic_repr_data_generate_child(iotcon_repr_h cur_repr,
+ unsigned int child_index)
+{
+ FN_CALL;
+ JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index);
+ if (NULL == obj) {
+ ERR("_ic_repr_data_generate_json() Fail");
+ return NULL;
+ }
+
+ return obj;
+}
+
+/*
+ * A general result : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}},
+ * {"href":"/a/child","rep":{"string":"World","double_val":5.7},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]}
+ */
+static JsonObject* _ic_repr_generate_json(iotcon_repr_h repr)
+{
+ JsonObject *repr_obj = NULL;
+ JsonObject *root_obj = NULL;
+ JsonArray *root_array = NULL;
+ unsigned int child_count = 0;
+ unsigned int child_index = 0;
+ iotcon_repr_h child_repr = NULL;
+
+ RETV_IF(NULL == repr, NULL);
+
+ root_obj = json_object_new();
+ root_array = json_array_new();
+
+ if (repr->children) {
+ child_count = iotcon_repr_get_children_count(repr);
+ }
+
+ repr_obj = _ic_repr_data_generate_parent(repr, child_index);
+ if (NULL == repr_obj) {
+ ERR("_ic_repr_data_generate_parent() Fali(NULL == repr_obj)");
+ json_object_unref(root_obj);
+ json_array_unref(root_array);
+ return NULL;
+ }
+ json_array_add_object_element(root_array, repr_obj);
+
+ for (child_index = 0; child_index < child_count; child_index++) {
+ iotcon_repr_get_nth_child(repr, child_index, &child_repr);
+ repr_obj = _ic_repr_data_generate_child(child_repr, child_index);
+ if (NULL == repr_obj) {
+ ERR("_ic_repr_data_generate_child() Fali(NULL == repr_obj)");
+ json_object_unref(root_obj);
+ json_array_unref(root_array);
+ return NULL;
+ }
+ json_array_add_object_element(root_array, repr_obj);
+ }
+
+ json_object_set_array_member(root_obj, IOTCON_KEY_OC, root_array);
+
+ return root_obj;
+}
+
+char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty)
+{
+ JsonNode *root_node = NULL;
+ char *json_data = NULL;
+
+ JsonObject *obj = _ic_repr_generate_json(repr);
+ if (NULL == obj) {
+ ERR("ic_repr_generate_json() Fail");
+ return NULL;
+ }
+
+ JsonGenerator *gen = json_generator_new();
+#if JSON_CHECK_VERSION(0,14,0)
+ json_generator_set_pretty(gen, set_pretty);
+#endif
+
+ root_node = json_node_new(JSON_NODE_OBJECT);
+ json_node_set_object(root_node, obj);
+ json_generator_set_root(gen, root_node);
+
+ json_data = json_generator_to_data(gen, NULL);
+ DBG("result : %s", json_data);
+
+ return json_data;
+}
+
+/*
+ * A general input : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
+ */
+iotcon_repr_h ic_repr_parse_json(const char *json_string)
+{
+ const char *iface_str = NULL;
+ const char *rtype_str = NULL;
+ const char *uri_value = NULL;
+ iotcon_interface_e iface_flag;
+
+ RETV_IF(NULL == json_string, NULL);
+
+ DBG("input str : %s", json_string);
+
+ GError *error = NULL;
+ gboolean ret = FALSE;
+ JsonParser *parser = json_parser_new();
+ ret = json_parser_load_from_data(parser, json_string, strlen(json_string), &error);
+ if (FALSE == ret) {
+ ERR("json_parser_load_from_data() Fail(%s)", error->message);
+ g_error_free(error);
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ JsonObject *root_obj = json_node_get_object(json_parser_get_root(parser));
+
+ iotcon_repr_h repr = NULL;
+ if (json_object_has_member(root_obj, IOTCON_KEY_REP)) {
+ repr = ic_obj_from_json(root_obj);
+ if (NULL == repr) {
+ ERR("ic_obj_from_json() Fail()");
+ g_object_unref(parser);
+ return NULL;
+ }
+ }
+ else {
+ repr = iotcon_repr_new();
+ }
+
+ if (json_object_has_member(root_obj, IOTCON_KEY_URI)) {
+ uri_value = json_object_get_string_member(root_obj, IOTCON_KEY_URI);
+ iotcon_repr_set_uri(repr, uri_value);
+ }
+
+ if (json_object_has_member(root_obj, IOTCON_KEY_PROPERTY)) {
+ JsonObject *property_obj = json_object_get_object_member(root_obj,
+ IOTCON_KEY_PROPERTY);
+
+ if (json_object_has_member(property_obj, IOTCON_KEY_RESOURCETYPES)) {
+ iotcon_resource_types_h res_types = NULL;
+ JsonArray *rt_array = json_object_get_array_member(property_obj,
+ IOTCON_KEY_RESOURCETYPES);
+ unsigned int rt_index = 0;
+ unsigned int rt_count = json_array_get_length(rt_array);
+
+ if (0 < rt_count) {
+ res_types = iotcon_resource_types_new();
+ if (NULL == res_types) {
+ ERR("iotcon_resource_types_new() Fail");
+ iotcon_repr_free(repr);
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ for (rt_index = 0; rt_index < rt_count; rt_index++) {
+ rtype_str = json_array_get_string_element(rt_array, rt_index);
+ iotcon_resource_types_insert(res_types, rtype_str);
+ }
+ iotcon_repr_set_resource_types(repr, res_types);
+ }
+ }
+ if (json_object_has_member(property_obj, IOTCON_KEY_INTERFACES)) {
+ JsonArray *if_array = json_object_get_array_member(property_obj,
+ IOTCON_KEY_INTERFACES);
+ unsigned int if_count = json_array_get_length(if_array);
+ unsigned int if_index;
+ int ifaces = IOTCON_INTERFACE_NONE;
+
+ for (if_index = 0; if_index < if_count; if_index++) {
+ iface_str = json_array_get_string_element(if_array, if_index);
+ ret = ic_ioty_convert_interface_string(iface_str, &iface_flag);
+ ifaces |= iface_flag;
+ }
+ iotcon_repr_set_resource_interfaces(repr, ifaces);
+
+ }
+ }
+
+ if (NULL == repr) {
+ ERR("repr is NULL");
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ g_object_unref(parser);
+
+ FN_END;
+
+ return repr;
+}
+
+
+API void iotcon_repr_free(iotcon_repr_h repr)
+{
+ FN_CALL;
+ RET_IF(NULL == repr);
+
+ if (false == _ic_repr_dec_ref_count(repr))
+ return;
+
+ free(repr->uri);
+
+ /* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */
+ g_list_free_full(repr->children, (GDestroyNotify)iotcon_repr_free);
+
+ /* null COULD be allowed */
+ if (repr->res_types)
+ iotcon_resource_types_free(repr->res_types);
+ g_hash_table_destroy(repr->hash_table);
+ free(repr);
+
+ FN_END;
+}
+
+static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h dest_repr)
+{
+ FN_CALL;
+ int type, ret;
+ char *dup_key;
+ iotcon_value_h value, copied_val;
+ iotcon_list_h child_list, copied_list;
+ iotcon_repr_h child_repr, copied_repr;
+
+ type = src_val->type;
+ switch (type) {
+ case IOTCON_TYPE_INT:
+ case IOTCON_TYPE_BOOL:
+ case IOTCON_TYPE_DOUBLE:
+ case IOTCON_TYPE_STR:
+ case IOTCON_TYPE_NULL:
+ copied_val = ic_value_clone(src_val);
+ if (NULL == copied_val) {
+ ERR("ic_value_clone() Fail");
+ return;
+ }
+
+ dup_key = ic_utils_strdup(key);
+ if (NULL == dup_key) {
+ ERR("dupic_utils_strdup() Fail");
+ return;
+ }
+
+ ic_obj_set_value(dest_repr, dup_key, copied_val);
+ break;
+ case IOTCON_TYPE_LIST:
+ ret = ic_value_get_list(src_val, &child_list);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_list() Fail(%d)", ret);
+ return;
+ }
+
+ copied_list = ic_list_clone(child_list);
+ if (NULL == copied_list) {
+ ERR("ic_list_clone() Fail");
+ return;
+ }
+
+ value = ic_value_new_list(copied_list);
+ if (NULL == value) {
+ ERR("ic_value_new_list() Fail");
+ iotcon_list_free(copied_list);
+ return;
+ }
+
+ ic_obj_set_value(dest_repr, key, value);
+ break;
+ case IOTCON_TYPE_REPR:
+ ret = ic_value_get_repr(src_val, &child_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("ic_value_get_repr() Fail(%d)", ret);
+ return;
+ }
+
+ copied_repr = iotcon_repr_clone(child_repr);
+ if (NULL == copied_repr) {
+ ERR("ic_list_clone() Fail");
+ return;
+ }
+
+ value = ic_value_new_repr(copied_repr);
+ if (NULL == value) {
+ ERR("ic_value_new_repr(%p) Fail", copied_repr);
+ return;
+ }
+
+ ic_obj_set_value(dest_repr, key, value);
+ break;
+ default:
+ ERR("Invalid type(%d)", type);
+ return;
+ }
+}
+
+API iotcon_repr_h iotcon_repr_clone(const iotcon_repr_h src)
+{
+ FN_CALL;
+ GList *node;
+ iotcon_repr_h dest, copied_repr;
+ iotcon_resource_types_h list;
+
+ RETV_IF(NULL == src, NULL);
+
+ dest = iotcon_repr_new();
+ if (NULL == dest) {
+ ERR("iotcon_repr_new() Fail");
+ return NULL;
+ }
+
+ if (src->uri) {
+ dest->uri = strdup(src->uri);
+ if (NULL == dest->uri) {
+ ERR("strdup() Fail");
+ iotcon_repr_free(dest);
+ return NULL;
+ }
+ }
+
+ if (src->interfaces)
+ dest->interfaces = src->interfaces;
+
+ if (src->res_types) {
+ list = iotcon_resource_types_clone(src->res_types);
+ if (NULL == list) {
+ ERR("iotcon_resource_types_clone() Fail");
+ iotcon_repr_free(dest);
+ return NULL;
+ }
+ dest->res_types = list;
+ }
+
+ for (node = g_list_first(src->children); node; node = node->next) {
+ copied_repr = iotcon_repr_clone((iotcon_repr_h)node->data);
+ if (NULL == copied_repr) {
+ ERR("iotcon_repr_clone(child) Fail");
+ iotcon_repr_free(dest);
+ return NULL;
+ }
+ dest->children = g_list_append(dest->children, copied_repr);
+ }
+
+ g_hash_table_foreach(src->hash_table, (GHFunc)_ic_repr_obj_clone, dest);
+
+ return dest;
+}
+
+API char* iotcon_repr_generate_json(iotcon_repr_h repr)
+{
+ return ic_repr_generate_json(repr, TRUE);
+}
--- /dev/null
+/*
+ * 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_REPRESENTATION_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_H__
+
+#include <stdbool.h>
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#include "iotcon-struct.h"
+
+#define IOTCON_KEY_OC "oc"
+#define IOTCON_KEY_URI "href"
+#define IOTCON_KEY_RESOURCETYPES "rt"
+#define IOTCON_KEY_INTERFACES "if"
+#define IOTCON_KEY_PROPERTY "prop"
+#define IOTCON_KEY_REP "rep"
+
+struct ic_repr_s {
+ char *uri;
+ int ref_count;
+ int interfaces;
+ GHashTable *hash_table;
+ GList *children;
+ iotcon_resource_types_h res_types;
+};
+
+/**
+ * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
+ * @brief Generates JSON string from Representation.
+ * @since_tizen 3.0
+ * @remarks The returned string must be released with free() by you.
+ *
+ * @param[in] repr The handle to the Representation
+ * @param[in] set_pretty Whether the generated JSON string should be pretty printed
+ *
+ * @return Generated JSON string, otherwise a null pointer if a parse error
+ */
+char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty);
+
+iotcon_repr_h ic_repr_parse_json(const char *json_string);
+
+void ic_repr_inc_ref_count(iotcon_repr_h val);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_H__ */
--- /dev/null
+/*
+ * 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 "iotcon-struct.h"
+#include "iotcon-constant.h"
+#include "icl.h"
+#include "icl-request.h"
+
+/* The content of the request should not be freed by user. */
+API int iotcon_request_get_uri(iotcon_request_h request, char **uri)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *uri = request->uri;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the request should not be freed by user. */
+API int iotcon_request_get_representation(iotcon_request_h request, iotcon_repr_h *repr)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *repr = request->repr;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_request_get_types(iotcon_request_h request, int *types)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *types = request->types;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the request should not be freed by user. */
+API int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *options = request->header_options;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the request should not be freed by user. */
+API int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == query, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *query = request->query;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_request_get_observer_action(iotcon_request_h request,
+ iotcon_observe_action_e *action)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == action, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *action = request->observation_info.action;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_request_get_observer_id(iotcon_request_h request, int *observer_id)
+{
+ RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == observer_id, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *observer_id = request->observation_info.observer_id;
+
+ return IOTCON_ERROR_NONE;
+}
+
--- /dev/null
+/*
+ * 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_REQUEST_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_REQUEST_H__
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+
+typedef void* oc_request_h;
+typedef void* oc_resource_h;
+
+struct ic_observe_info {
+ iotcon_observe_action_e action;
+ int observer_id;
+};
+
+struct ic_resource_request {
+ int types;
+ char *uri;
+ iotcon_options_h header_options;
+ iotcon_query_h query;
+ int request_handler_flag;
+ struct ic_observe_info observation_info;
+ iotcon_repr_h repr;
+ oc_request_h request_handle;
+ oc_resource_h resource_handle;
+};
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REQUEST_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-resource-types.h"
+
+iotcon_resource_types_h ic_resource_types_ref(iotcon_resource_types_h types)
+{
+ RETV_IF(NULL == types, NULL);
+ RETV_IF(types->ref_count <= 0, NULL);
+
+ types->ref_count++;
+
+ return types;
+}
+
+
+API iotcon_resource_types_h iotcon_resource_types_new()
+{
+ iotcon_resource_types_h types = calloc(1, sizeof(struct ic_resource_types));
+ if (NULL == types) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ types->ref_count = 1;
+
+ return types;
+}
+
+
+API void iotcon_resource_types_free(iotcon_resource_types_h types)
+{
+ RET_IF(NULL == types);
+
+ types->ref_count--;
+
+ if (0 == types->ref_count) {
+ g_list_free_full(types->type_list, free);
+ free(types);
+ }
+}
+
+
+static int _ic_resource_types_strcmp(const void *a, const void *b)
+{
+ return strcmp(a, b);
+}
+
+
+static bool _ic_resource_types_duplicate_check(iotcon_resource_types_h types,
+ const char *type)
+{
+ GList *ret = NULL;
+
+ RETV_IF(NULL == types, false);
+ RETV_IF(NULL == type, false);
+
+ ret = g_list_find_custom(types->type_list, type, _ic_resource_types_strcmp);
+ if (NULL == ret)
+ return false;
+
+ return true;
+}
+
+
+/* The length of resource type should be less than or equal to 61.
+ * Duplicate strings are not allowed. */
+API int iotcon_resource_types_insert(iotcon_resource_types_h types, const char *type)
+{
+ char *resource_type;
+
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(1 < types->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
+ "Don't modify it. It is already set.");
+
+ if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(type)) {
+ ERR("The length of type(%s) should be less than or equal to %d.", type,
+ IOTCON_RESOURCE_TYPE_LENGTH_MAX);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ if (true == _ic_resource_types_duplicate_check(types, type)) {
+ ERR("%s is already contained.", type);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ resource_type = strdup(type);
+ if (NULL == resource_type) {
+ ERR("strdup() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ types->type_list = g_list_append(types->type_list, resource_type);
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_resource_types_delete(iotcon_resource_types_h types, const char *type)
+{
+ GList *found_node;
+
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(1 < types->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
+ "Don't modify it. It is already set.");
+
+ found_node = g_list_find_custom(types->type_list, type, _ic_resource_types_strcmp);
+ if (NULL == found_node) {
+ ERR("g_list_find_custom() Fail");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ free(found_node->data);
+ types->type_list = g_list_delete_link(types->type_list, found_node);
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_resource_types_foreach(iotcon_resource_types_h types,
+ iotcon_resource_types_foreach_cb cb, void *user_data)
+{
+ GList *node;
+
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+ for (node = types->type_list; node; node = node->next) {
+ if (IOTCON_FUNC_STOP == cb((const char*)node->data, user_data))
+ break;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API iotcon_resource_types_h iotcon_resource_types_clone(iotcon_resource_types_h types)
+{
+ GList * node;
+ char *resource_type;
+ iotcon_resource_types_h clone;
+
+ RETV_IF(NULL == types, NULL);
+
+ clone = calloc(1, sizeof(struct ic_resource_types));
+ if (NULL == clone) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ for (node = types->type_list; node; node = node->next) {
+ resource_type = ic_utils_strdup(node->data);
+ if (NULL == resource_type) {
+ iotcon_resource_types_free(clone);
+ ERR("ic_utils_strdup() Fail");
+ return NULL;
+ }
+ clone->type_list = g_list_append(clone->type_list, resource_type);
+ }
+
+ clone->ref_count = 1;
+
+ return clone;
+}
+
+
+/* counting from 0 */
+const char* ic_resource_types_get_nth_data(iotcon_resource_types_h types, int index)
+{
+ return g_list_nth_data(types->type_list, index);
+}
+
+
+unsigned int ic_resource_types_get_length(iotcon_resource_types_h types)
+{
+ return g_list_length(types->type_list);
+}
--- /dev/null
+/*
+ * 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_TYPES_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_TYPES_H__
+
+#include "iotcon-struct.h"
+
+struct ic_resource_types {
+ int ref_count;
+ GList *type_list;
+};
+
+iotcon_resource_types_h ic_resource_types_ref(iotcon_resource_types_h res_types);
+const char* ic_resource_types_get_nth_data(iotcon_resource_types_h res_types, int index);
+unsigned int ic_resource_types_get_length(iotcon_resource_types_h res_types);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESOURCE_TYPES_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon.h"
+#include "icl.h"
+#include "icl-utils.h"
+#include "icl-ioty.h"
+#include "icl-repr.h"
+#include "icl-options.h"
+#include "icl-request.h"
+#include "icl-response.h"
+
+API iotcon_response_h iotcon_response_new(iotcon_request_h request_h)
+{
+ FN_CALL;
+
+ RETV_IF(NULL == request_h, NULL);
+
+ iotcon_response_h resp = calloc(1, sizeof(struct ic_resource_response));
+ if (NULL == resp) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ resp->request_handle = request_h->request_handle;
+ resp->resource_handle = request_h->resource_handle;
+ resp->error_code = 200;
+
+ return resp;
+}
+
+
+API void iotcon_response_free(iotcon_response_h resp)
+{
+ FN_CALL;
+
+ RET_IF(NULL == resp);
+
+ if (resp->repr)
+ iotcon_repr_free(resp->repr);
+ if (resp->new_uri)
+ free(resp->new_uri);
+ if (resp->header_options)
+ ic_options_free(resp->header_options);
+ free(resp);
+}
+
+
+API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e prop, ...)
+{
+ int value;
+ va_list args;
+ char *new_resource_uri = NULL;
+ iotcon_options_h options = NULL;
+
+ va_start(args, prop);
+
+ switch (prop) {
+ case IOTCON_RESPONSE_INTERFACE:
+ resp->iface = va_arg(args, int);
+ break;
+ case IOTCON_RESPONSE_REPRESENTATION:
+ resp->repr = va_arg(args, iotcon_repr_h);
+ ic_repr_inc_ref_count(resp->repr);
+ break;
+ case IOTCON_RESPONSE_RESULT:
+ value = va_arg(args, int);
+ if (value < IOTCON_RESPONSE_RESULT_OK || IOTCON_RESPONSE_RESULT_MAX <= value) {
+ ERR("Invalid value(%d)", value);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ resp->result = value;
+ break;
+ case IOTCON_RESPONSE_RESOURCE_URI:
+ new_resource_uri = va_arg(args, char*);
+ if (resp->new_uri)
+ free(resp->new_uri);
+
+ if (new_resource_uri)
+ resp->new_uri = ic_utils_strdup(new_resource_uri);
+ else
+ resp->new_uri = NULL;
+ break;
+ case IOTCON_RESPONSE_HEADER_OPTIONS:
+ options = va_arg(args, iotcon_options_h);
+ if (resp->header_options)
+ ic_options_free(resp->header_options);
+ if (NULL == options) {
+ resp->header_options = NULL;
+ break;
+ }
+ if (true == options->has_parent)
+ resp->header_options = ic_options_ref(options);
+ else
+ resp->header_options = options;
+ if (NULL == resp->header_options) {
+ ERR("header_options is NULL");
+ return IOTCON_ERROR_NO_DATA;
+ }
+
+ resp->header_options->has_parent = true;
+ break;
+ case IOTCON_RESPONSE_NONE:
+ default:
+ break;
+ }
+
+ va_end(args);
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_response_send(iotcon_response_h resp)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resp, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == resp->repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_send_res_response_data(resp);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_send_res_response_data() Fail(%d)", ret);
+
+ return ret;
+}
+
--- /dev/null
+/*
+ * 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_RESPONSE_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESPONSE_H__
+
+#include "iotcon-struct.h"
+#include "iotcon-constant.h"
+#include "icl-request.h"
+
+struct ic_resource_response {
+ char *new_uri;
+ int error_code;
+ iotcon_options_h header_options;
+ iotcon_interface_e iface;
+ iotcon_response_result_e result;
+ iotcon_repr_h repr;
+ oc_request_h request_handle;
+ oc_resource_h resource_handle;
+};
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_RESPONSE_H__ */
--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "iotcon.h"
+#include "icl.h"
+#include "icl-utils.h"
+
+char* ic_utils_strdup(const char *src)
+{
+ char *dest = NULL;
+
+ RETV_IF(NULL == src, NULL);
+
+ errno = 0;
+ dest = strdup(src);
+ if (NULL == dest) {
+ ERR("strdup() Fail(%d)", errno);
+ return NULL;
+ }
+
+ return dest;
+}
--- /dev/null
+/*
+ * 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_UTILITY_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_UTILITY_H__
+
+#define IC_STR_EQUAL 0
+
+char* ic_utils_strdup(const char *src);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_UTILITY_H__ */
--- /dev/null
+/*
+ * 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 <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+#include <glib-object.h>
+
+#include "iotcon.h"
+#include "icl-utils.h"
+#include "icl-resource-types.h"
+#include "icl-ioty.h"
+#include "icl-repr.h"
+#include "icl.h"
+
+/**
+ * @brief global context
+ */
+static GHashTable *ic_request_cb_hash;
+static bool ic_is_init = false;
+
+static void _free_resource(gpointer data)
+{
+ int ret;
+ iotcon_resource_h resource = data;
+
+ RET_IF(NULL == data);
+
+ ret = ic_ioty_unregister_res(resource->handle);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_unregister_res() Fail(%d)", ret);
+
+ free(resource);
+}
+
+/* Host address should begin with "coap://" */
+API void iotcon_initialize(const char *addr, unsigned short port)
+{
+ FN_CALL;
+
+ RETM_IF(true == ic_is_init, "already initialized");
+ RET_IF(NULL == addr);
+
+ ic_ioty_config(addr, port);
+ ic_request_cb_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
+ _free_resource);
+
+#if !GLIB_CHECK_VERSION(2,35,0)
+ g_type_init();
+#endif
+ ic_is_init = true;
+}
+
+
+API void iotcon_deinitialize()
+{
+ FN_CALL;
+
+ RETM_IF(false == ic_is_init, "Not initialized");
+
+ g_hash_table_destroy(ic_request_cb_hash);
+ ic_request_cb_hash = NULL;
+
+ ic_is_init = false;
+}
+
+
+static gboolean _find_valid_resource(gpointer key, gpointer value, gpointer user_data)
+{
+ return (key == user_data);
+}
+
+
+iotcon_resource_h ic_get_resource_handler_data(void *handle)
+{
+ return g_hash_table_find(ic_request_cb_hash, _find_valid_resource, handle);
+}
+
+
+/* The length of uri should be less than or equal to 36. */
+API iotcon_resource_h iotcon_register_resource(const char *uri,
+ iotcon_resource_types_h res_types,
+ int ifaces,
+ uint8_t properties,
+ iotcon_request_handler_cb cb,
+ void *user_data)
+{
+ FN_CALL;
+ iotcon_resource_h resource;
+
+ RETV_IF(NULL == uri, NULL);
+ RETVM_IF(IOTCON_URI_LENGTH_MAX < strlen(uri), NULL,
+ "The length of uri(%s) is invalid", uri);
+ RETV_IF(NULL == res_types, NULL);
+ RETV_IF(NULL == cb, NULL);
+
+ resource = calloc(1, sizeof(struct ic_resource));
+ if (NULL == resource) {
+ ERR("calloc Fail(%d)", errno);
+ return NULL;
+ }
+
+ resource->handle = ic_ioty_register_res(uri, res_types, ifaces, properties);
+ if (NULL == resource->handle) {
+ ERR("ic_ioty_register_res() Fail");
+ free(resource);
+ return NULL;
+ }
+
+ resource->cb = cb;
+ resource->user_data = user_data;
+
+ resource->uri = ic_utils_strdup(uri);
+ resource->types = ic_resource_types_ref(res_types);
+ resource->ifaces = ifaces;
+ resource->is_observable = properties & IOTCON_OBSERVABLE;
+
+ g_hash_table_insert(ic_request_cb_hash, resource->handle, resource);
+
+ return resource;
+}
+
+
+API void iotcon_unregister_resource(iotcon_resource_h resource)
+{
+ FN_CALL;
+
+ RET_IF(NULL == resource);
+
+ g_hash_table_remove(ic_request_cb_hash, resource->handle);
+}
+
+
+API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_bind_iface_to_res(resource->handle, iface);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_bind_iface_to_res() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type)
+{
+ FN_CALL;
+ int ret;
+
+ 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("The length of resource_type(%s) is invalid", resource_type);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ ret = ic_ioty_bind_type_to_res(resource->handle, resource_type);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_bind_type_to_res() Fail(%d)", 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;
+
+ RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(parent == child, 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;
+ }
+ if (NULL == parent->children[i]) {
+ ret = ic_ioty_bind_res(parent->handle, child->handle);
+ if (IOTCON_ERROR_NONE == ret)
+ parent->children[i] = child;
+ else
+ ERR("ic_ioty_bind_res() Fail(%d)", ret);
+
+ return ret;
+ }
+ }
+
+ 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;
+
+ RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_unbind_res(parent->handle, child->handle);
+ if (IOTCON_ERROR_NONE == ret) {
+ for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
+ if (child == parent->children[i])
+ parent->children[i] = NULL;
+ }
+ }
+ else
+ ERR("ic_ioty_unbind_res() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+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(iotcon_resource_h resource, char **uri)
+{
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *uri = resource->uri;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/* The content of the resource should not be freed by user. */
+API int iotcon_resource_get_host(iotcon_resource_h resource, char **host)
+{
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER);
+
+ *host = resource->host;
+
+ 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 int iotcon_start_presence(unsigned int time_to_live)
+{
+ FN_CALL;
+ int ret;
+
+ ret = ic_ioty_start_presence(time_to_live);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_start_presence() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+API int iotcon_stop_presence()
+{
+ FN_CALL;
+ int ret;
+
+ ret = ic_ioty_stop_presence();
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_stop_presence() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+/* The length of resource_type should be less than or equal to 61. */
+API iotcon_presence_h iotcon_subscribe_presence(const char *host_address,
+ const char *resource_type, iotcon_presence_cb cb, void *user_data)
+{
+ iotcon_presence_h handle;
+
+ RETV_IF(NULL == host_address, NULL);
+ RETV_IF(NULL == cb, NULL);
+ if (resource_type &&(IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) {
+ ERR("The length of resource_type(%s) is invalid", resource_type);
+ return NULL;
+ }
+
+ if (NULL == resource_type)
+ resource_type = "";
+
+ handle = ic_ioty_subscribe_presence(host_address, resource_type, cb, user_data);
+ if (NULL == handle)
+ ERR("ic_ioty_subscribe_presence() Fail");
+
+ return handle;
+}
+
+
+API int iotcon_unsubscribe_presence(iotcon_presence_h handle)
+{
+ FN_CALL;
+ int ret;
+
+ RETV_IF(NULL == handle, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_unsubscribe_presence(handle);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_unsubscribe_presence() Fail(%d)", ret);
+
+ return ret;
+}
+
+
+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 ic_notify_msg));
+ if (NULL == msg) {
+ ERR("calloc() Fail(%d)", errno);
+ return NULL;
+ }
+
+ msg->repr = 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(iotcon_resource_h resource, iotcon_notimsg_h msg,
+ iotcon_observers_h observers)
+{
+ int ret;
+
+ RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == msg, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == msg->repr, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = ic_ioty_send_notify(resource->handle, msg, observers);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("ic_ioty_send_notify() Fail(%d)", ret);
+
+ return ret;
+}
--- /dev/null
+/*
+ * 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_H__
+#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_H__
+
+#include "iotcon.h"
+#include "ic-common.h"
+#include "ic-log.h"
+
+#ifdef API
+#undef API
+#endif
+#define API __attribute__((visibility("default")))
+
+
+#define IC_INTERFACE_MAX (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_LINK | \
+ IOTCON_INTERFACE_BATCH | IOTCON_INTERFACE_GROUP)
+
+
+iotcon_resource_h ic_get_resource_handler_data(void *handle);
+
+#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_H__ */
<smack permit="dbus" type="rwx" />
<smack permit="system::use_internet" type="rw" />
<smack permit="connman::get" type="rw" />
+ <smack permit="libiotcon" type="rwx" />
</permit>
<request>
<smack request="connman::get" type="rw" />
<smack request="dbus" type="rwx" />
+ <smack request="libiotcon" type="rwxt" />
<smack request="_" type="rw" />
<smack request="system::use_internet" type="rw" />
</request>
</define>
- <assign>
- <filesystem path="/usr/lib/libiotcon.so*" label="_" exec_label="_"/>
- </assign>
-
<request>
<domain name="iotcon" />
</request>
--- /dev/null
+[Unit]
+Description=IoT Connectivity Manager
+After=system-server.service
+Requires=system-server.service
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/iotcon-daemon
+KillSignal=SIGKILL
+
+[Install]
+WantedBy=multi-user.target
Group: Network & Connectivity/Other
License: Apache-2.0
Source0: %{name}-%{version}.tar.gz
+Source1: %{name}.service
Source1001: %{name}.manifest
+Source1002: lib%{name}.manifest
BuildRequires: cmake
BuildRequires: boost-devel
BuildRequires: iotivity-devel
BuildRequires: pkgconfig(json-glib-1.0)
BuildRequires: pkgconfig(dlog)
BuildRequires: pkgconfig(capi-base-common)
-
+%define _unitdir /usr/lib/systemd/system
%description
-IoT Connectivity Manager and Library
+IoT Connectivity Manager Daemon
+
+
+%package lib
+Summary: IoT Connectivity Library
+Group: Network & Connectivity/Libraries
+Requires: %{name} = %{version}
+
+%description lib
+Tizen event notification service Client library for applications.
+
%package devel
Summary: IoT Connectivity Manager (devel)
Group: Network & Connectivity/Development
-Requires: %{name} = %{version}
+Requires: %{name}-lib = %{version}
%description devel
IoT Connectivity Manager development Kit
+
+%package test
+Summary: IoT Connectivity Manager (test)
+Group: Network & Connectivity/Development
+Requires: %{name}-lib = %{version}
+
+%description test
+IoT Connectivity Manager Test Programs
+
+
%prep
%setup -q
cp %{SOURCE1001} .
+cp %{SOURCE1002} .
+
%build
MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
%install
+rm -rf %{buildroot}/BUILD/%{name}*
%make_install
-%post -p /sbin/ldconfig
+mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants
+cp -af %{SOURCE1} %{buildroot}%{_unitdir}/
+ln -s ../%{name}.service %{buildroot}%{_unitdir}/multi-user.target.wants/%{name}.service
+
+%post
+systemctl daemon-reload
+if [ $1 == 1 ]; then
+ systemctl restart %{name}.service
+fi
+
+%postun
+/sbin/ldconfig
+if [ $1 == 0 ]; then
+ systemctl stop %{name}.service
+fi
+systemctl daemon-reload
+
+
+%post lib -p /sbin/ldconfig
+%postun lib -p /sbin/ldconfig
-%postun -p /sbin/ldconfig
%files
%manifest %{name}.manifest
%defattr(-,root,root,-)
+%{_unitdir}/%{name}.service
+%{_unitdir}/multi-user.target.wants/%{name}.service
+%{_bindir}/%{name}-daemon
+%{_datadir}/dbus-1/services/org.tizen.%{name}.service
+%license LICENSE.APLv2
+
+%files lib
+%manifest lib%{name}.manifest
+%defattr(-,root,root,-)
%{_libdir}/lib%{name}.so.*
%license LICENSE.APLv2
-%{_bindir}/crud-test-client
-%{_bindir}/crud-test-server
-%{_bindir}/device-test-client
-%{_bindir}/device-test-server
-%{_bindir}/repr-test-client
-%{_bindir}/repr-test-server
%files devel
+%defattr(-,root,root,-)
%{_libdir}/lib%{name}.so
%{_libdir}/pkgconfig/%{name}.pc
%{_includedir}/%{name}/*.h
+%files test
+%defattr(-,root,root,-)
+%{_bindir}/crud-test-client
+%{_bindir}/crud-test-server
+%{_bindir}/device-test-client
+%{_bindir}/device-test-server
+%{_bindir}/repr-test-client
+%{_bindir}/repr-test-server
--- /dev/null
+<manifest>
+ <define>
+ <domain name="libiotcon" />
+ <permit>
+ <smack permit="_" type="rw" />
+ <smack permit="dbus" type="rwx" />
+ </permit>
+ <request>
+ <smack request="dbus" type="rwx" />
+ <smack request="_" type="rw" />
+ <smack request="enotice" type="rwxat" />
+ </request>
+ </define>
+ <assign>
+ <filesystem path="/usr/lib/libiotcon.so*" label="_" exec_label="_"/>
+ </assign>
+ <request>
+ <domain name="libiotcon" />
+ </request>
+</manifest>