#define IC_DBUS_SIGNAL_PLATFORM "PLATFORM"
#define IC_DBUS_SIGNAL_PRESENCE "PRESENCE"
+#define IC_INTERFACE_DEFAULT "oic.if.baseline";
+#define IC_INTERFACE_LINK "oic.if.ll";
+#define IC_INTERFACE_BATCH "oic.if.b";
+#define IC_INTERFACE_GROUP "oc.mi.grp";
+
+#define IC_JSON_KEY_OC "oc"
+#define IC_JSON_KEY_URI_PATH "href"
+#define IC_JSON_KEY_OBSERVABLE "obs"
+#define IC_JSON_KEY_RESOURCETYPES "rt"
+#define IC_JSON_KEY_INTERFACES "if"
+#define IC_JSON_KEY_PROPERTY "prop"
+#define IC_JSON_KEY_REP "rep"
+#define IC_JSON_KEY_SECURE "sec"
+#define IC_JSON_KEY_PORT "port"
+#define IC_JSON_KEY_SERVERID "sid"
+
#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__ */
<node>
<interface name="org.tizen.iotcon.dbus">
<method name="registerResource">
- <arg type="s" name="uri" direction="in"/>
+ <arg type="s" name="uri_path" direction="in"/>
<arg type="as" name="resource_types" direction="in"/>
<arg type="i" name="ifaces" direction="in"/>
<arg type="y" name="properties" direction="in"/>
<arg type="i" name="ret" direction="out"/>
</method>
<method name="get">
- <arg type="(ssba(qs)asii)" name="client" direction="in"/>
+ <arg type="(ssba(qs)iii)" name="client" direction="in"/>
<arg type="a(ss)" name="query" direction="in"/>
<arg type="u" name="signal_number" direction="in"/>
<arg type="i" name="ret" direction="out"/>
} \
} while (0)
-#define WARN_IF(expr) \
+#define WARN_IF(expr, fmt, arg...) \
do { \
if (expr) { \
- WARN("(%s)", #expr); \
+ WARN(fmt, ##arg); \
} \
} while (0)
SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/ic-dbus.c
PROPERTIES GENERATED TRUE)
-pkg_check_modules(daemon_pkgs REQUIRED gio-2.0 dlog gio-unix-2.0)
+pkg_check_modules(daemon_pkgs REQUIRED gio-2.0 dlog gio-unix-2.0 json-glib-1.0)
INCLUDE_DIRECTORIES(${daemon_pkgs_INCLUDE_DIRS})
LINK_DIRECTORIES(${daemon_pkgs_LIBRARY_DIRS})
return IOTCON_ERROR_NO_DATA;
}
-int icd_dbus_emit_signal(const char *signal_name, const char *sender, GVariant *value)
+int icd_dbus_emit_signal(const char *dest, const char *signal_name, GVariant *value)
{
gboolean ret;
GError *error = NULL;
DBG("SIG : %s, %s", signal_name, g_variant_print(value, FALSE));
ret = g_dbus_connection_emit_signal(icd_dbus_conn,
- sender,
+ dest,
IOTCON_DBUS_OBJPATH,
IOTCON_DBUS_INTERFACE,
signal_name,
static gboolean _dbus_handle_register_resource(icDbus *object,
GDBusMethodInvocation *invocation,
- const gchar *uri,
+ const gchar *uri_path,
const gchar* const *resource_types,
gint ifaces,
guchar properties,
const gchar *sender;
void *handle = NULL;
- handle = icd_ioty_register_resource(uri, resource_types, ifaces, properties);
+ handle = icd_ioty_register_resource(uri_path, resource_types, ifaces, properties);
if (handle) {
sender = g_dbus_method_invocation_get_sender(invocation);
ret = _icd_dbus_resource_list_append_handle(sender, handle, signal_number);
#define __IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__
int icd_dbus_bus_list_get_info(int handle, unsigned int *sig_num, const gchar **sender);
-int icd_dbus_emit_signal(const char *sig_name, const char *sender, GVariant *value);
+int icd_dbus_emit_signal(const char *dest, const char *sig_name, GVariant *value);
unsigned int icd_dbus_init();
void icd_dbus_deinit(unsigned int 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 <stdlib.h>
+#include <unistd.h> /* for usleep() */
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#include <ocstack.h>
+
+#include "iotcon.h"
+#include "ic-utils.h"
+#include "icd.h"
+#include "icd-dbus.h"
+#include "icd-ioty.h"
+#include "icd-ioty-ocprocess.h"
+
+static int icd_ioty_alive;
+
+typedef int (*_ocprocess_fn)(void *user_data);
+
+struct icd_ioty_worker
+{
+ void *ctx;
+ _ocprocess_fn fn;
+};
+
+
+enum _icd_secure_type
+{
+ ICD_TRANSPORT_IPV4_SECURE,
+ ICD_TRANSPORT_IPV4
+};
+
+
+struct icd_find_context {
+ unsigned int signum;
+ char *sender;
+ char *payload;
+ OCDevAddr *dev_addr;
+ int conn_type;
+};
+
+
+struct icd_get_context {
+ unsigned int signum;
+ int res;
+ char *sender;
+ char *payload;
+ GVariantBuilder *options;
+};
+
+void icd_ioty_ocprocess_stop()
+{
+ icd_ioty_alive = 0;
+}
+
+static void* _ocprocess_worker_thread(void *data)
+{
+ int ret;
+ struct icd_ioty_worker *worker = data;
+
+ if (NULL == data) {
+ ERR("worker is NULL");
+ return NULL;
+ }
+
+ ret = worker->fn(worker->ctx);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("fn() Fail(%d)", ret);
+
+ /* worker was allocated from _ocprocess_worker_start() */
+ free(worker);
+
+ /* GCC warning happen if use g_thread_exit() */
+ return NULL;
+}
+
+
+static int _ocprocess_worker_start(_ocprocess_fn fn, void *ctx)
+{
+ GError *error;
+ GThread *thread;
+ struct icd_ioty_worker *worker;
+
+ RETV_IF(NULL == fn, IOTCON_ERROR_INVALID_PARAMETER);
+
+ worker = calloc(1, sizeof(struct icd_ioty_worker));
+ if (NULL == worker) {
+ ERR("calloc() Fail(%d)", errno);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ worker->fn = fn;
+ worker->ctx = ctx;
+
+ /* TODO : consider thread pool mechanism */
+ thread = g_thread_try_new("worker_thread", _ocprocess_worker_thread, worker, &error);
+ if (NULL == thread) {
+ ERR("g_thread_try_new() Fail(%s)", error->message);
+ g_error_free(error);
+ free(worker);
+ return IOTCON_ERROR_SYSTEM;
+ }
+
+ /* DO NOT join thread. It was already detached by calling g_thread_unref() */
+ g_thread_unref(thread);
+
+ /* DO NOT FREE worker. It MUST be freed in the _ocprocess_worker_thread() */
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
+ OCEntityHandlerRequest *entityHandlerRequest)
+{
+ FN_CALL;
+
+ DBG("reqJSONPayload : %s", entityHandlerRequest->reqJSONPayload);
+
+ return OC_EH_OK;
+}
+
+
+gpointer icd_ioty_ocprocess_thread(gpointer data)
+{
+ FN_CALL;
+ OCStackResult result;
+
+ icd_ioty_alive = 1;
+ while (icd_ioty_alive) {
+ icd_ioty_csdk_lock();
+ result = OCProcess();
+ icd_ioty_csdk_unlock();
+ if (OC_STACK_OK != result) {
+ ERR("OCProcess() Fail(%d)", result);
+ break;
+ }
+
+ /* TODO : SHOULD revise time or usleep */
+ usleep(10);
+ }
+
+ return NULL;
+}
+
+
+static int _ocprocess_response_signal(const char *dest, const char *signal,
+ unsigned int signum, GVariant *value)
+{
+ int ret;
+ char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
+
+ ret = snprintf(sig_name, sizeof(sig_name), "%s_%u", signal, signum);
+ if (ret <= 0 || sizeof(sig_name) <= ret) {
+ ERR("snprintf() Fail(%d)", ret);
+ return IOTCON_ERROR_UNKNOWN;
+ }
+
+ ret = icd_dbus_emit_signal(dest, sig_name, value);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icd_dbus_emit_signal() Fail(%d)", ret);
+ return ret;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/*
+ * returned string SHOULD be released by you
+ */
+static inline char* _find_cb_get_address(OCDevAddr *address, int sec_type, int sec_port)
+{
+ FN_CALL;
+ int ret;
+ uint16_t port;
+ uint8_t a, b, c, d;
+ char addr[1024] = {0};
+
+ RETVM_IF(ICD_TRANSPORT_IPV4 != sec_type && ICD_TRANSPORT_IPV4_SECURE != sec_type,
+ NULL, "Invalid secure type(%d)", sec_type);
+
+ ret = OCDevAddrToIPv4Addr(address, &a, &b, &c, &d);
+ if (OC_STACK_OK != ret) {
+ ERR("OCDevAddrToIPv4Addr() Fail(%d)", ret);
+ return NULL;
+ }
+
+ if (ICD_TRANSPORT_IPV4_SECURE == sec_type) {
+ if (sec_port <= 0 || 65535 < sec_port) {
+ SECURE_ERR("Invalid secure port(%d)", sec_port);
+ return NULL;
+ }
+
+ ret = snprintf(addr, sizeof(addr), ICD_IOTY_COAPS"%d.%d.%d.%d:%d", a, b, c, d,
+ sec_port);
+ } else {
+ ret = OCDevAddrToPort(address, &port);
+ if (OC_STACK_OK != ret) {
+ ERR("OCDevAddrToPort() Fail(%d)", ret);
+ return NULL;
+ }
+
+ ret = snprintf(addr, sizeof(addr), ICD_IOTY_COAP"%d.%d.%d.%d:%d", a, b, c, d,
+ port);
+ }
+
+ WARN_IF(ret <= 0 || sizeof(addr) <= ret, "snprintf() Fail(%d)", ret);
+
+ return ic_utils_strdup(addr);
+}
+
+
+static inline int _find_cb_response(JsonObject *rsrc_obj,
+ struct icd_find_context *ctx)
+{
+ GVariant *value;
+ JsonGenerator *gen;
+ JsonNode *root_node;
+ char *host, *json_data;
+ JsonObject *property_obj;
+ int ret, secure, secure_type, secure_port;
+
+ RETV_IF(NULL == rsrc_obj, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
+
+ /* parse secure secure_port */
+ property_obj = json_object_get_object_member(rsrc_obj, IC_JSON_KEY_PROPERTY);
+ if (NULL == property_obj) {
+ ERR("json_object_get_object_member() Fail");
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ secure = json_object_get_int_member(property_obj, IC_JSON_KEY_SECURE);
+ if (0 == secure) {
+ secure_type = ICD_TRANSPORT_IPV4;
+ secure_port = 0;
+ } else {
+ secure_type = ICD_TRANSPORT_IPV4_SECURE;
+ secure_port = json_object_get_int_member(property_obj, IC_JSON_KEY_PORT);
+ }
+
+ host = _find_cb_get_address(ctx->dev_addr, secure_type, secure_port);
+ if (NULL == host) {
+ ERR("_find_cb_get_address() Fail");
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ gen = json_generator_new();
+ root_node = json_node_new(JSON_NODE_OBJECT);
+ json_node_set_object(root_node, rsrc_obj);
+ json_generator_set_root(gen, root_node);
+
+ json_data = json_generator_to_data(gen, NULL);
+ json_node_free(root_node);
+ g_object_unref(gen);
+
+ value = g_variant_new("(ssi)", json_data, host, ctx->conn_type);
+ free(json_data);
+ free(host);
+
+ /* TODO : If one device has multi resources, it comes as bulk data.
+ * To reduce the number of emit_signal, let's send signal only one time for one device.
+ * for ex, client list. */
+ ret = _ocprocess_response_signal(ctx->sender, IC_DBUS_SIGNAL_FOUND_RESOURCE,
+ ctx->signum, value);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icd_dbus_emit_signal() Fail(%d)", ret);
+ return ret;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static inline int _find_cb_handle_context(struct icd_find_context *ctx)
+{
+ int ret;
+ JsonParser *parser;
+ GError *error = NULL;
+ JsonObject *root_obj;
+ JsonArray *rsrc_array;
+ unsigned int rsrc_count, rsrc_index;
+
+ RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == ctx->payload, IOTCON_ERROR_INVALID_PARAMETER);
+
+ parser = json_parser_new();
+ ret = json_parser_load_from_data(parser, ctx->payload, strlen(ctx->payload), &error);
+ if (FALSE == ret) {
+ ERR("json_parser_load_from_data() Fail(%s)", error->message);
+ g_error_free(error);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ /* parse 'oc' prefix */
+ root_obj = json_node_get_object(json_parser_get_root(parser));
+ rsrc_array = json_object_get_array_member(root_obj, IC_JSON_KEY_OC);
+ if (NULL == rsrc_array) {
+ ERR("json_object_get_array_member() Fail");
+ g_object_unref(parser);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ rsrc_count = json_array_get_length(rsrc_array);
+ if (0 == rsrc_count) {
+ ERR("Invalid count(%d)", rsrc_count);
+ g_object_unref(parser);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ for (rsrc_index = 0; rsrc_index < rsrc_count; rsrc_index++) {
+ JsonObject *rsrc_obj = json_array_get_object_element(rsrc_array, rsrc_index);
+
+ ret = _find_cb_response(rsrc_obj, ctx);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_find_cb_response() Fail(%d)", ret);
+ g_object_unref(parser);
+ return ret;
+ }
+ }
+
+ g_object_unref(parser);
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _worker_find_cb(void *context)
+{
+ int ret;
+ struct icd_find_context *ctx = context;
+
+ RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
+
+ ret = _find_cb_handle_context(ctx);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_find_cb_handle_context() Fail(%d)", ret);
+
+ /* ctx was allocated from icd_ioty_ocprocess_find_cb() */
+ free(ctx->sender);
+ free(ctx->payload);
+ free(ctx->dev_addr);
+ free(ctx);
+
+ return ret;
+}
+
+
+OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
+ OCClientResponse *clientResponse)
+{
+ int ret;
+ OCDevAddr *dev_addr;
+ struct icd_find_context *find_ctx;
+ icd_sig_ctx_s *sig_context = ctx;
+
+ RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
+ RETV_IF(NULL == clientResponse, OC_STACK_KEEP_TRANSACTION);
+ RETV_IF(NULL == clientResponse->resJSONPayload, OC_STACK_KEEP_TRANSACTION);
+
+ DBG("JSON Payload : %s", clientResponse->resJSONPayload);
+
+ find_ctx = calloc(1, sizeof(struct icd_find_context));
+ if (NULL == find_ctx) {
+ ERR("calloc() Fail(%d)", errno);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ dev_addr = calloc(1, sizeof(OCDevAddr));
+ if (NULL == dev_addr) {
+ ERR("calloc() Fail(%d)", errno);
+ free(find_ctx);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ memcpy(dev_addr, clientResponse->addr, sizeof(OCDevAddr));
+
+ find_ctx->signum = sig_context->signum;
+ find_ctx->sender = ic_utils_strdup(sig_context->sender);
+ find_ctx->payload = ic_utils_strdup(clientResponse->resJSONPayload);
+ find_ctx->dev_addr = dev_addr;
+ find_ctx->conn_type = clientResponse->connType;
+
+ ret = _ocprocess_worker_start(_worker_find_cb, find_ctx);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ocprocess_worker_start() Fail(%d)", ret);
+ free(find_ctx->sender);
+ free(find_ctx->payload);
+ free(find_ctx->dev_addr);
+ free(find_ctx);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
+ /* DO NOT FREE find_ctx. It MUST be freed in the _worker_find_cb func */
+
+ return OC_STACK_KEEP_TRANSACTION;
+}
+
+
+static int _worker_get_cb(void *context)
+{
+ int ret;
+ GVariant *value;
+ struct icd_get_context *ctx = context;
+
+ RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
+
+ value = g_variant_new("(a(qs)si)", ctx->options, ctx->payload, ctx->res);
+
+ ret = _ocprocess_response_signal(ctx->sender, IC_DBUS_SIGNAL_GET, ctx->signum, value);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ocprocess_response_signal() Fail(%d)", ret);
+
+ /* ctx was allocated from icd_ioty_ocprocess_get_cb() */
+ free(ctx->sender);
+ free(ctx->payload);
+ g_variant_builder_unref(ctx->options);
+ free(ctx);
+
+ return ret;
+}
+
+
+static void _get_cb_response_error(const char *dest, unsigned int signum, int ret_val)
+{
+ int ret;
+ GVariant *value;
+
+ value = g_variant_new("(a(qs)si)", NULL, IC_STR_NULL, ret_val);
+
+ ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_GET, signum, value);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("_ocprocess_response_signal() Fail(%d)", ret);
+}
+
+
+static inline GVariantBuilder* _ocprocess_parse_header_options(OCClientResponse *resp)
+{
+ int i;
+ GVariantBuilder *options;
+
+ RETV_IF(NULL == resp, NULL);
+
+ if (0 == resp->numRcvdVendorSpecificHeaderOptions)
+ return NULL;
+
+ options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
+ for (i = 0; i < resp->numRcvdVendorSpecificHeaderOptions; i++) {
+ g_variant_builder_add(options, "(qs)",
+ resp->rcvdVendorSpecificHeaderOptions[i].optionID,
+ resp->rcvdVendorSpecificHeaderOptions[i].optionData);
+ }
+
+ return options;
+}
+
+
+OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
+ OCClientResponse *resp)
+{
+ FN_CALL;
+ int ret, res;
+ OCStackResult result;
+ GVariantBuilder *options;
+ struct icd_get_context *get_ctx;
+ icd_sig_ctx_s *sig_context = ctx;
+
+ RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
+
+ if (NULL == resp->resJSONPayload || '\0' == resp->resJSONPayload[0]) {
+ ERR("json payload is empty");
+ _get_cb_response_error(sig_context->sender, sig_context->signum,
+ IOTCON_ERROR_IOTIVITY);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ get_ctx = calloc(1, sizeof(struct icd_get_context));
+ if (NULL == get_ctx) {
+ ERR("calloc() Fail(%d)", errno);
+ _get_cb_response_error(sig_context->sender, sig_context->signum,
+ IOTCON_ERROR_OUT_OF_MEMORY);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ result = resp->result;
+ if (result == OC_STACK_OK) {
+ res = IOTCON_RESPONSE_RESULT_OK;
+ options = _ocprocess_parse_header_options(resp);
+ } else {
+ WARN("resp error(%d)", result);
+ res = IOTCON_RESPONSE_RESULT_ERROR;
+ options = NULL;
+ }
+
+ get_ctx->payload = strdup(resp->resJSONPayload);
+ get_ctx->signum = sig_context->signum;
+ get_ctx->res = res;
+ get_ctx->sender = ic_utils_strdup(sig_context->sender);
+ get_ctx->options = options;
+
+ ret = _ocprocess_worker_start(_worker_get_cb, get_ctx);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ocprocess_worker_start() Fail(%d)", ret);
+ _get_cb_response_error(sig_context->sender, sig_context->signum, ret);
+ free(get_ctx->sender);
+ free(get_ctx->payload);
+ g_variant_builder_unref(get_ctx->options);
+ free(get_ctx);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
+ /* DO NOT FREE get_ctx. It MUST be freed in the _worker_get_cb func */
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
--- /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_IOTIVITY_THREAD_H__
+#define __IOT_CONNECTIVITY_MANAGER_DAEMON_IOTIVITY_THREAD_H__
+
+#include <glib.h>
+#include <octypes.h>
+
+void icd_ioty_ocprocess_stop();
+
+OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
+ OCEntityHandlerRequest *entityHandlerRequest);
+
+gpointer icd_ioty_ocprocess_thread(gpointer data);
+
+OCStackApplicationResult icd_ioty_ocprocess_find_cb(void* ctx, OCDoHandle handle,
+ OCClientResponse* clientResponse);
+
+OCStackApplicationResult icd_ioty_ocprocess_get_cb(void* ctx, OCDoHandle handle,
+ OCClientResponse* clientResponse);
+
+#endif /*__IOT_CONNECTIVITY_MANAGER_DAEMON_IOTIVITY_THREAD_H__*/
#ifndef __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_REPRESENTATION_H__
#define __IOT_CONNECTIVITY_MANAGER_LIBRARY_IOTIVITY_REPRESENTATION_H__
-#include <OCRepresentation.h>
-#include <glib.h>
-#include "iotcon-struct.h"
-
-int icd_ioty_repr_parse_json(const char *repr_json, OC::OCRepresentation &ocRep);
-int icd_ioty_repr_generate_gvariant_builder(const OC::OCRepresentation &ocRep,
- GVariantBuilder **builder);
-
#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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h> /* for uint8_t etc */
+#include <stdbool.h>
+#include <errno.h>
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#include <octypes.h>
+#include <ocstack.h>
+
+#include "iotcon.h"
+#include "ic-utils.h"
+#include "icd.h"
+#include "icd-ioty-repr.h"
+#include "icd-ioty.h"
+#include "icd-ioty-ocprocess.h"
+
+GHashTable *icd_worker_table;
+
+GMutex icd_csdk_mutex;
+
+void icd_ioty_csdk_lock()
+{
+ g_mutex_lock(&icd_csdk_mutex);
+}
+
+
+void icd_ioty_csdk_unlock()
+{
+ g_mutex_unlock(&icd_csdk_mutex);
+}
+
+
+GThread* icd_ioty_init(const char *addr, unsigned short port)
+{
+ FN_CALL;
+ GError *error;
+ GThread *thread;
+
+ OCStackResult result = OCInit(addr, port, OC_CLIENT_SERVER);
+ if (OC_STACK_OK != result) {
+ ERR("OCInit() Fail(%d)", result);
+ return NULL;
+ }
+
+ DBG("OCInit() Success");
+
+ thread = g_thread_try_new("packet_receive_thread", icd_ioty_ocprocess_thread,
+ NULL, &error);
+ if (NULL == thread) {
+ ERR("g_thread_try_new() Fail(%s)", error->message);
+ g_error_free(error);
+ return NULL;
+ }
+
+ return thread;
+}
+
+void icd_ioty_deinit(GThread *thread)
+{
+ OCStackResult result;
+
+ icd_ioty_ocprocess_stop();
+ g_thread_join(thread);
+
+ result = OCStop();
+ if (OC_STACK_OK != result)
+ ERR("OCStop() Fail(%d)", result);
+}
+
+
+OCResourceHandle icd_ioty_register_resource(const char *uri_path,
+ const char* const* res_types, int ifaces, uint8_t properties)
+{
+ FN_CALL;
+ int i;
+ OCStackResult ret;
+ OCResourceHandle handle;
+ const char *resInterface = NULL;
+
+ if (IOTCON_INTERFACE_DEFAULT & ifaces) {
+ resInterface = IC_INTERFACE_DEFAULT;
+ ifaces ^= IOTCON_INTERFACE_DEFAULT;
+ } else if (IOTCON_INTERFACE_LINK & ifaces) {
+ resInterface = IC_INTERFACE_LINK;
+ ifaces ^= IOTCON_INTERFACE_LINK;
+ } else if (IOTCON_INTERFACE_BATCH & ifaces) {
+ resInterface = IC_INTERFACE_BATCH;
+ ifaces ^= IOTCON_INTERFACE_BATCH;
+ } else if (IOTCON_INTERFACE_GROUP & ifaces) {
+ resInterface = IC_INTERFACE_GROUP;
+ ifaces ^= IOTCON_INTERFACE_GROUP;
+ } else {
+ ERR("Invalid interface type(%d)", ifaces);
+ return NULL;
+ }
+
+ icd_ioty_csdk_lock();
+ ret = OCCreateResource(&handle, res_types[0], resInterface, uri_path, EntityHandlerWrapper,
+ properties);
+ icd_ioty_csdk_unlock();
+ if (OC_STACK_OK != ret) {
+ ERR("OCCreateResource() Fail(%d)", ret);
+ return NULL;
+ }
+
+ for (i = 1; res_types[i]; i++)
+ icd_ioty_bind_type(handle, res_types[i]);
+
+ if (IOTCON_INTERFACE_DEFAULT & ifaces)
+ icd_ioty_bind_interface(handle, IOTCON_INTERFACE_DEFAULT);
+ if (IOTCON_INTERFACE_LINK & ifaces)
+ icd_ioty_bind_interface(handle, IOTCON_INTERFACE_LINK);
+ if (IOTCON_INTERFACE_BATCH & ifaces)
+ icd_ioty_bind_interface(handle, IOTCON_INTERFACE_BATCH);
+ if (IOTCON_INTERFACE_GROUP & ifaces)
+ icd_ioty_bind_interface(handle, IOTCON_INTERFACE_GROUP);
+
+ return handle;
+}
+
+
+int icd_ioty_unregister_resource(OCResourceHandle resource_handle)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _ioty_convert_interface_flag(iotcon_interface_e src, const char **dest)
+{
+ switch (src) {
+ case IOTCON_INTERFACE_DEFAULT:
+ *dest = IC_INTERFACE_DEFAULT;
+ break;
+ case IOTCON_INTERFACE_LINK:
+ *dest = IC_INTERFACE_LINK;
+ break;
+ case IOTCON_INTERFACE_BATCH:
+ *dest = IC_INTERFACE_BATCH;
+ break;
+ case IOTCON_INTERFACE_GROUP:
+ *dest = IC_INTERFACE_GROUP;
+ break;
+ case IOTCON_INTERFACE_NONE:
+ default:
+ ERR("Invalid interface(%d)", src);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_bind_interface(OCResourceHandle resourceHandle, iotcon_interface_e iface)
+{
+ int ret;
+ OCStackResult result;
+ const char *resource_interface;
+
+ ret = _ioty_convert_interface_flag(iface, &resource_interface);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
+ return ret;
+ }
+
+ result = OCBindResourceInterfaceToResource(resourceHandle, resource_interface);
+ if (OC_STACK_OK != result) {
+ ERR("OCBindResourceInterfaceToResource() Fail(%d)", result);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+int icd_ioty_bind_type(OCResourceHandle resource_handle, const char *resource_type)
+{
+ OCStackResult ret;
+
+ ret = OCBindResourceTypeToResource(resource_handle, resource_type);
+ if (OC_STACK_OK != ret) {
+ ERR("OCBindResourceTypeToResource() Fail(%d)", ret);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_bind_resource(OCResourceHandle parent, OCResourceHandle child)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_unbind_resource(OCResourceHandle parent, OCResourceHandle child)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_notify_list_of_observers(int resHandle, GVariant *msg, GVariant *observers)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_notify_all(int resHandle)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_send_response(GVariant *resp)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+void _ioty_free_signal_context(void *data)
+{
+ icd_sig_ctx_s *context = data;
+ free(context->sender);
+ free(context);
+}
+
+int icd_ioty_find_resource(const char *host_address, const char *resource_type,
+ unsigned int signum, const char *sender)
+{
+ int len;
+ OCStackResult result;
+ OCCallbackData cbdata = {0};
+ icd_sig_ctx_s *context;
+ char uri[PATH_MAX] = {0};
+ iotcon_connectivity_type_e conn_type = IOTCON_CONNECTIVITY_IPV4;
+
+ if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address)) {
+ len = snprintf(uri, sizeof(uri), "%s", OC_MULTICAST_DISCOVERY_URI);
+ conn_type = IOTCON_CONNECTIVITY_ALL;
+ } else {
+ len = snprintf(uri, sizeof(uri), ICD_IOTY_COAP"%s%s", host_address,
+ OC_MULTICAST_DISCOVERY_URI);
+ }
+ if (len <= 0 || sizeof(uri) <= len) {
+ ERR("snprintf() Fail(%d)", len);
+ return IOTCON_ERROR_UNKNOWN;
+ }
+
+ if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
+ snprintf(uri + len, sizeof(uri), "?rt=%s", resource_type);
+
+ context = calloc(1, sizeof(icd_sig_ctx_s));
+ if (NULL == context) {
+ ERR("calloc() Fail(%d)", errno);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+
+ context->sender = ic_utils_strdup(sender);
+ context->signum = signum;
+
+ cbdata.context = context;
+ cbdata.cb = icd_ioty_ocprocess_find_cb;
+ cbdata.cd = _ioty_free_signal_context;
+
+ icd_ioty_csdk_lock();
+ result = OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL, conn_type, OC_LOW_QOS,
+ &cbdata, NULL, 0);
+ icd_ioty_csdk_unlock();
+
+ if (OC_STACK_OK != result) {
+ ERR("OCDoResource() Fail(%d)", result);
+ free(context->sender);
+ free(context);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static int _ioty_get_header_options(GVariantIter *src, int src_size,
+ OCHeaderOption dest[], int dest_size)
+{
+ gsize len;
+ char *option_data;
+ unsigned short option_id;
+
+ RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+ if (dest_size < src_size) {
+ ERR("Exceed Size(%d)", src_size);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ while (g_variant_iter_loop(src, "(q&s)", &option_id, &option_data)) {
+ dest[i].protocolID = OC_COAP_ID;
+ dest[i].optionID = option_id;
+ dest[i].optionLength = strlen(option_data)+1;
+ memcpy(dest[i].optionData, option_data, dest[i].optionLength);
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+/*
+ * returned string SHOULD be released by you
+ */
+static char* _icd_ioty_resource_generate_uri(char *host, char *uri_path, GVariant *query)
+{
+ int len;
+ bool loop_first = true;
+ char *key, *value;
+ GVariantIter *queryIter;
+ char uri_buf[PATH_MAX] = {0};
+
+ len = snprintf(uri_buf, sizeof(uri_buf), "%s%s", host, uri_path);
+ if (len < 0) {
+ ERR("snprintf() Fail");
+ return NULL;
+ }
+
+ /* remove suffix '/' */
+ if ('/' == uri_buf[strlen(uri_buf) - 1]) {
+ uri_buf[strlen(uri_buf) - 1] = '\0';
+ len--;
+ }
+
+ g_variant_get(query, "a(ss)", &queryIter);
+
+ while (g_variant_iter_loop(queryIter, "(&s&s)", &key, &value)) {
+ int query_len;
+
+ DBG("query exist. key(%s), value(%s)", key, value);
+
+ if (true == loop_first) {
+ query_len = snprintf(uri_buf + len, sizeof(uri_buf), "?%s=%s", key, value);
+ loop_first = false;
+ } else {
+ query_len = snprintf(uri_buf + len, sizeof(uri_buf), "&%s=%s", key, value);
+ }
+
+ if (0 > query_len) {
+ ERR("snprintf() Fail");
+ g_variant_iter_free(queryIter);
+ return NULL;
+ }
+
+ len += query_len;
+ }
+ g_variant_iter_free(queryIter);
+
+ return strdup(uri_buf);
+}
+
+
+int icd_ioty_get(GVariant *resource, GVariant *query, unsigned int signal_number,
+ const char *sender)
+{
+ FN_CALL;
+ int conn_type, options_size;
+ char *uri_path, *host, *uri;
+ OCStackResult result;
+ GVariantIter *options;
+ OCCallbackData cbdata = {0};
+ icd_sig_ctx_s *context;
+ int is_observable, ifaces, observe_handle;
+ OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
+
+ g_variant_get(resource, "(&s&sba(qs)iii)", &uri_path, &host, &is_observable, &options,
+ &ifaces, &observe_handle, &conn_type);
+
+ uri = _icd_ioty_resource_generate_uri(host, uri_path, query);
+ if (NULL == uri) {
+ ERR("_icd_ioty_resource_generate_uri() Fail");
+ g_variant_iter_free(options);
+ return IOTCON_ERROR_INVALID_PARAMETER;
+ }
+
+ context = calloc(1, sizeof(icd_sig_ctx_s));
+ if (NULL == context) {
+ ERR("calloc() Fail(%d)", errno);
+ g_variant_iter_free(options);
+ return IOTCON_ERROR_OUT_OF_MEMORY;
+ }
+ context->sender = ic_utils_strdup(sender);
+ context->signum = signal_number;
+
+ cbdata.context = context;
+ cbdata.cb = icd_ioty_ocprocess_get_cb;
+ cbdata.cd = _ioty_free_signal_context;
+
+ options_size = g_variant_iter_n_children(options);
+ if (0 != options_size) {
+ int ret = _ioty_get_header_options(options, options_size, oic_options,
+ sizeof(oic_options) / sizeof(oic_options[0]));
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("_ioty_get_header_options() Fail(%d)", ret);
+ free(context->sender);
+ free(context);
+ free(uri);
+ g_variant_iter_free(options);
+ return ret;
+ }
+ }
+ g_variant_iter_free(options);
+
+ icd_ioty_csdk_lock();
+ /* TODO : QoS is come from lib. And user can set QoS to client structure. */
+ result = OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL, conn_type, OC_HIGH_QOS,
+ &cbdata, options_size?oic_options:NULL, options_size);
+ icd_ioty_csdk_unlock();
+
+ free(uri);
+
+ if (OC_STACK_OK != result) {
+ ERR("OCDoResource() Fail(%d)", result);
+ free(context->sender);
+ free(context);
+ return IOTCON_ERROR_IOTIVITY;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_put(GVariant *resource, const char *repr, GVariant *query,
+ unsigned int signal_number, const char *sender)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_post(GVariant *resource, const char *repr, GVariant *query,
+ unsigned int signal_number, const char *sender)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_delete(GVariant *resource, unsigned int signal_number,
+ const char *sender)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_observer_start(GVariant *resource, int observe_type,
+ GVariant *query, unsigned int signal_number, const char *sender, int *observe_h)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_observer_stop(void *observe_h)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */
+int icd_ioty_register_device_info(GVariant *value)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_get_device_info(const char *host_address,
+ unsigned int signal_number, const char *sender)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+#endif
+
+
+int icd_ioty_register_platform_info(GVariant *value)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_get_platform_info(const char *host_address, unsigned int signal_number,
+ const char *sender)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+iotcon_presence_h icd_ioty_subscribe_presence(const char *host_address,
+ const char *resource_type, unsigned int signal_number, const char *sender)
+{
+ // TODO : To be implemented
+ return NULL;
+}
+
+
+int icd_ioty_unsubscribe_presence(iotcon_presence_h presence_handle)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_start_presence(unsigned int time_to_live)
+{
+ // TODO : To be implemented
+ return IOTCON_ERROR_NONE;
+}
+
+
+int icd_ioty_stop_presence()
+{
+ // TODO : To be implemented
+ 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 <stdbool.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <glib.h>
-#include <OCApi.h>
-#include <OCPlatform.h>
-
-extern "C" {
-#include "iotcon.h"
-#include "ic-utils.h"
-#include "icd.h"
-#include "icd-ioty.h"
-#include "icd-dbus.h"
-#include "icd-ioty-repr.h"
-}
-
-#define ICD_COAP "coap://"
-
-using namespace std;
-using namespace OC;
-using namespace OCPlatform;
-
-struct resource_handle {
- OCResource::Ptr ocResource;
-};
-
-namespace icdIotivityHandler {
- class getObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- getObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void onGet(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res, ret;
- GVariant *value;
- GVariantBuilder *options;
- GVariantBuilder *repr;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- } else {
- ERR("get() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- g_variant_builder_add(options, "(qs)", option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- ret = icd_ioty_repr_generate_gvariant_builder(ocRep, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_generate_gvariant_builder() Fail(%d)", ret);
- g_variant_builder_unref(options);
- return;
- }
-
- value = g_variant_new("(a(qs)asi)", options, repr, res);
- g_variant_builder_unref(options);
- g_variant_builder_unref(repr);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_GET,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class putObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- putObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void onPut(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res, ret;
- GVariant *value;
- GVariantBuilder *options;
- GVariantBuilder *repr;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- } else {
- ERR("put() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
-
- options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- g_variant_builder_add(options, "(qs)", option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- ret = icd_ioty_repr_generate_gvariant_builder(ocRep, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_generate_gvariant_builder() Fail(%d)", ret);
- g_variant_builder_unref(options);
- return;
- }
-
- value = g_variant_new("(a(qs)asi)", options, repr, res);
- g_variant_builder_unref(options);
- g_variant_builder_unref(repr);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PUT,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class postObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- postObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void onPost(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode)
- {
- FN_CALL;
- int res, ret;
- GVariant *value;
- GVariantBuilder *options;
- GVariantBuilder *repr;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- 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 = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- g_variant_builder_add(options, "(qs)", option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- ret = icd_ioty_repr_generate_gvariant_builder(ocRep, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_generate_gvariant_builder() Fail(%d)", ret);
- g_variant_builder_unref(options);
- return;
- }
-
- value = g_variant_new("(a(qs)asi)", options, repr, res);
- g_variant_builder_unref(options);
- g_variant_builder_unref(repr);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_POST,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class deleteObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- deleteObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void onDelete(const HeaderOptions& headerOptions, const int eCode)
- {
- int res;
- GVariant *value;
- GVariantBuilder *options;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- 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 = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- g_variant_builder_add(options, "(qs)", option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- value = g_variant_new("(a(qs)i)", options, res);
- g_variant_builder_unref(options);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DELETE,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class observeObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- observeObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void onObserve(const HeaderOptions& headerOptions, const OCRepresentation& ocRep,
- const int eCode, const int sequenceNumber)
- {
- FN_CALL;
- int res, ret;
- GVariant *value;
- GVariantBuilder *options;
- GVariantBuilder *repr;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- if (OC_STACK_OK == eCode) {
- res = IOTCON_RESPONSE_RESULT_OK;
- } else {
- ERR("observe() Fail(%d)", eCode);
- res = IOTCON_RESPONSE_RESULT_ERROR;
- }
- options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- for (HeaderOption::OCHeaderOption option : headerOptions) {
- g_variant_builder_add(options, "(qs)", option.getOptionID(),
- option.getOptionData().c_str());
- }
-
- ret = icd_ioty_repr_generate_gvariant_builder(ocRep, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_generate_gvariant_builder() Fail(%d)", ret);
- g_variant_builder_unref(options);
- return;
- }
-
- value = g_variant_new("(a(qs)asii)", options, repr, res, sequenceNumber);
- g_variant_builder_unref(options);
- g_variant_builder_unref(repr);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_OBSERVE,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class findObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- findObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void foundResource(shared_ptr<OCResource> resource)
- {
- FN_CALL;
- int ifaces = 0;
- GVariant *value;
- GVariantBuilder *builder;
- string resource_host;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
- vector<string> resource_types = resource->getResourceTypes();
- if (0 < resource_types.size()) {
- for (string &resource_type : resource_types)
- g_variant_builder_add(builder, "s", resource_type.c_str());
- }
-
- vector<string> resource_interfaces = resource->getResourceInterfaces();
- for (string &resource_interface : resource_interfaces) {
- if (IC_STR_EQUAL == resource_interface.compare(DEFAULT_INTERFACE))
- ifaces |= IOTCON_INTERFACE_DEFAULT;
-
- if (IC_STR_EQUAL == resource_interface.compare(BATCH_INTERFACE))
- ifaces |= IOTCON_INTERFACE_BATCH;
-
- if (IC_STR_EQUAL == resource_interface.compare(LINK_INTERFACE))
- ifaces |= IOTCON_INTERFACE_LINK;
-
- if (IC_STR_EQUAL == resource_interface.compare(GROUP_INTERFACE))
- ifaces |= IOTCON_INTERFACE_GROUP;
- }
-
- resource_host = resource->host();
- resource_host.erase(0, strlen(ICD_COAP));
-
- value = g_variant_new("(sssiasi)",
- resource->uri().c_str(),
- resource_host.c_str(),
- resource->sid().c_str(),
- resource->isObservable(),
- builder,
- ifaces);
- g_variant_builder_unref(builder);
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_FOUND_RESOURCE,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
-#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */
- class deviceObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- deviceObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void receivedDeviceInfo(const OCRepresentation& ocRep)
- {
- FN_CALL;
- GVariant *value;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- string contentType;
- string dateOfManufacture;
- string deviceName;
- string deviceUUID;
- string firmwareVersion;
- string hostName;
- string manufacturerName;
- string manufacturerUrl;
- string modelNumber;
- string platformVersion;
- string supportUrl;
- string version;
-
- ocRep.getValue("dn", deviceName);
- ocRep.getValue("hn", hostName);
- ocRep.getValue("di", deviceUUID);
- ocRep.getValue("ct", contentType);
- ocRep.getValue("icv", version);
- ocRep.getValue("mnmn", manufacturerName);
- ocRep.getValue("mnml", manufacturerUrl);
- ocRep.getValue("mnmo", modelNumber);
- ocRep.getValue("mndt", dateOfManufacture);
- ocRep.getValue("mnpv", platformVersion);
- ocRep.getValue("mnfv", firmwareVersion);
- ocRep.getValue("mnsl", supportUrl);
-
- value = g_variant_new("(ssssssssssss)",
- deviceName.c_str(),
- hostName.c_str(),
- deviceUUID.c_str(),
- contentType.c_str(),
- version.c_str(),
- manufacturerName.c_str(),
- manufacturerUrl.c_str(),
- modelNumber.c_str(),
- dateOfManufacture.c_str(),
- platformVersion.c_str(),
- firmwareVersion.c_str(),
- supportUrl.c_str());
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DEVICE,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-#endif
-
- class platformObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- platformObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void receivedPlatformInfo(const OCRepresentation& ocRep)
- {
- FN_CALL;
- GVariant *value;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- string platformId;;
- string manufacturerName;
- string manufacturerUrl;
- string modelNumber;
- string dateOfManufacture;
- string platformVersion;
- string osVersion;
- string hardwardVersion;
- string firmwareVersion;
- string supportUrl;
- string systemTime;
-
- ocRep.getValue("pi", platformId);
- ocRep.getValue("mnmn", manufacturerName);
- ocRep.getValue("mnml", manufacturerUrl);
- ocRep.getValue("mnmo", modelNumber);
- ocRep.getValue("mndt", dateOfManufacture);
- ocRep.getValue("mnpv", platformVersion);
- ocRep.getValue("mnos", osVersion);
- ocRep.getValue("mnhw", hardwardVersion);
- ocRep.getValue("mnfv", firmwareVersion);
- ocRep.getValue("mnsl", supportUrl);
- ocRep.getValue("st", systemTime);
-
- value = g_variant_new("(sssssssssss)",
- platformId.c_str(),
- manufacturerName.c_str(),
- manufacturerUrl.c_str(),
- modelNumber.c_str(),
- dateOfManufacture.c_str(),
- platformVersion.c_str(),
- osVersion.c_str(),
- hardwardVersion.c_str(),
- firmwareVersion.c_str(),
- supportUrl.c_str(),
- systemTime.c_str());
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PLATFORM,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-
- class presenceObject
- {
- private:
- unsigned int m_signalNumber;
- string m_sender;
-
- public:
- presenceObject(unsigned int signalNumber, const char *sender)
- {
- m_signalNumber = signalNumber;
- m_sender = sender;
- }
-
- void presenceHandler(OCStackResult result, const unsigned int nonce,
- const string& hostAddress)
- {
- int res;
- GVariant *value;
- char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
- 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;
- }
-
- value = g_variant_new("(ius)", res, nonce, hostAddress.c_str());
-
- snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PRESENCE,
- m_signalNumber);
- icd_dbus_emit_signal(signal_name, m_sender.c_str(), value);
- }
- };
-}
-
-
-extern "C" void icd_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 _icd_ioty_request_handler(
- shared_ptr<OCResourceRequest> request)
-{
- FN_CALL;
- int types = 0;
- int ret, observer_id, observe_action, request_handle, resource_handle;
- unsigned int signal_number;
- char sig_name[100] = {0};
- const gchar *sender = NULL;
- const char *uri = NULL;
- const char *request_type = NULL;
- GVariant *value;
- GVariantBuilder *options, *query, *repr;
- HeaderOptions headerOptions;
- QueryParamsMap queryParams;
- OCRepresentation ocRep;
-
- /* request type */
- if (RequestFlag & request->getRequestHandlerFlag()) {
- request_type = request->getRequestType().c_str();
- if (NULL == request_type) {
- ERR("request_type is NULL");
- return OC_EH_ERROR;
- }
-
- if (IC_STR_EQUAL == strcmp("GET", request_type))
- types = IOTCON_REQUEST_GET;
- else if (IC_STR_EQUAL == strcmp("PUT", request_type))
- types = IOTCON_REQUEST_PUT;
- else if (IC_STR_EQUAL == strcmp("POST", request_type))
- types = IOTCON_REQUEST_POST;
- else if (IC_STR_EQUAL == strcmp("DELETE", request_type))
- types = IOTCON_REQUEST_DELETE;
- }
-
- if (ObserverFlag & request->getRequestHandlerFlag())
- types |= IOTCON_REQUEST_OBSERVE;
-
- /* uri */
- uri = request->getResourceUri().c_str();
- if (NULL == uri) {
- ERR("uri is NULL");
- return OC_EH_ERROR;
- }
-
- /* header options */
- headerOptions = request->getHeaderOptions();
- options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
- if (0 < headerOptions.size()) {
- for (auto it : headerOptions) {
- DBG("OptionID = %d, OptionData = %s",
- it.getOptionID(), it.getOptionData().c_str());
- g_variant_builder_add(options, "(qs)", it.getOptionID(),
- it.getOptionData().c_str());
- }
- }
-
- /* query */
- queryParams = request->getQueryParameters();
- query = g_variant_builder_new(G_VARIANT_TYPE("a(ss)"));
- if (0 < queryParams.size()) {
- for (auto it : queryParams) {
- DBG("key = %s value = %s", it.first.c_str(), it.second.c_str());
- g_variant_builder_add(query, "(ss)", it.first.c_str(), it.second.c_str());
- }
- }
-
- /* observation info */
- ObservationInfo observationInfo = request->getObservationInfo();
- observe_action = (int)observationInfo.action;
- observer_id = observationInfo.obsId;
- DBG("obs_info.obsId=%d", observationInfo.obsId);
-
- /* Representation */
- ocRep = request->getResourceRepresentation();
- ret = icd_ioty_repr_generate_gvariant_builder(ocRep, &repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_generate_gvariant_builder() Fail(%d)", ret);
- g_variant_builder_unref(options);
- g_variant_builder_unref(query);
- return OC_EH_ERROR;
- }
-
- /* handle */
- request_handle = GPOINTER_TO_INT(request->getRequestHandle());
- resource_handle = GPOINTER_TO_INT(request->getResourceHandle());
-
- value = g_variant_new("(isa(qs)a(ss)iiasii)", types, uri, options, query,
- observe_action, observer_id, repr, request_handle, resource_handle);
-
- g_variant_builder_unref(options);
- g_variant_builder_unref(query);
- g_variant_builder_unref(repr);
-
- ret = icd_dbus_bus_list_get_info(resource_handle, &signal_number, &sender);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_dbus_bus_list_get_info() Fail(%d)", ret);
- return OC_EH_ERROR;
- }
-
- snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
- signal_number);
- icd_dbus_emit_signal(sig_name, sender, value);
-
- return OC_EH_OK;
-}
-
-
-extern "C" OCResourceHandle icd_ioty_register_resource(const char *uri,
- const char* const* res_types, int ifaces, uint8_t properties)
-{
- OCStackResult ret = OC_STACK_OK;
- string resUri;
- string resType;
- string resInterface;
- OCResourceHandle handle;
-
- resUri = uri;
- resType = string(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;
- }
-
- try {
- ret = registerResource(handle, resUri, resType, resInterface,
- _icd_ioty_request_handler, properties);
- } catch (OCException& e) {
- ERR("registerResource() Fail(%s)", e.reason().c_str());
- return NULL;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("registerResource() Fail(%d)", ret);
- return NULL;
- }
-
- for (int i = 1; res_types[i]; i++)
- icd_ioty_bind_type(handle, res_types[i]);
-
- if (IOTCON_INTERFACE_DEFAULT & ifaces)
- icd_ioty_bind_interface(handle, IOTCON_INTERFACE_DEFAULT);
-
- if (IOTCON_INTERFACE_LINK & ifaces)
- icd_ioty_bind_interface(handle, IOTCON_INTERFACE_LINK);
-
- if (IOTCON_INTERFACE_BATCH & ifaces)
- icd_ioty_bind_interface(handle, IOTCON_INTERFACE_BATCH);
-
- if (IOTCON_INTERFACE_GROUP & ifaces)
- icd_ioty_bind_interface(handle, IOTCON_INTERFACE_GROUP);
-
- return handle;
-}
-
-extern "C" int icd_ioty_unregister_resource(OCResourceHandle resource_handle)
-{
- OCStackResult result;
- try {
- result = unregisterResource(resource_handle);
- } catch (OCException& e) {
- ERR("unregisterResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != result) {
- ERR("unregisterResource() Fail(%d)", result);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static int _icd_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(%d)", src);
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_bind_interface(OCResourceHandle resourceHandle,
- iotcon_interface_e iface)
-{
- int ret;
- OCStackResult ocRet;
- string resource_interface;
-
- ret = _icd_ioty_convert_interface_flag(iface, resource_interface);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_icd_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
- return ret;
- }
-
- try {
- ocRet = bindInterfaceToResource(resourceHandle, resource_interface);
- } catch (OCException& e) {
- ERR("bindInterfaceToResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ocRet) {
- ERR("bindInterfaceToResource() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_bind_type(OCResourceHandle resource_handle,
- const char *resource_type)
-{
- OCStackResult ret;
- OCResourceHandle resourceHandle = resource_handle;
-
- try {
- ret = bindTypeToResource(resourceHandle, resource_type);
- } catch (OCException& e) {
- ERR("bindTypeToResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("bindTypeToResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_bind_resource(OCResourceHandle parent, OCResourceHandle child)
-{
- OCStackResult ret;
-
- try {
- ret = bindResource(parent, child);
- } catch (OCException& e) {
- ERR("bindResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("bindResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_unbind_resource(OCResourceHandle parent, OCResourceHandle child)
-{
- OCStackResult ret;
-
- try {
- ret = unbindResource(parent, child);
- } catch (OCException& e) {
- ERR("unbindResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("unbindResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_notify_list_of_observers(int resHandle, GVariant *msg,
- GVariant *observers)
-{
- int ret;
- int error_code;
- int obs_id;
- char *repr_json = NULL;
- iotcon_interface_e iface;
- GVariantIter *msgIter;
- GVariantIter *obsIter;
- ObservationIds obsIds;
- string iface_str;
- OCStackResult ocRet;
- OCRepresentation ocRep;
-
- g_variant_get(observers, "ai", &obsIter);
- while (g_variant_iter_loop(obsIter, "i", &obs_id))
- obsIds.push_back(obs_id);
- g_variant_iter_free(obsIter);
-
- shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
-
- g_variant_get(msg, "a(iis)", &msgIter);
- if (g_variant_iter_loop(msgIter, "(ii&s)", &error_code, &iface, &repr_json)) {
- resourceResponse->setErrorCode(error_code);
-
- if (IOTCON_INTERFACE_NONE != iface) {
- ret = _icd_ioty_convert_interface_flag(iface, iface_str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_icd_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
- g_variant_iter_free(msgIter);
- return ret;
- }
- } else {
- iface_str = DEFAULT_INTERFACE;
- }
-
- ret = icd_ioty_repr_parse_json(repr_json, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_parse_json() Fail(%d)", ret);
- g_variant_iter_free(msgIter);
- return ret;
- }
-
- resourceResponse->setResourceRepresentation(ocRep, iface_str);
- }
- g_variant_iter_free(msgIter);
-
- try {
- ocRet = notifyListOfObservers(GINT_TO_POINTER(resHandle), obsIds,
- resourceResponse);
- } catch (OCException& e) {
- ERR("notifyListOfObservers() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_NO_OBSERVERS == ocRet) {
- WARN("No observers. Stop notifying");
- return IOTCON_ERROR_NONE;
- } else if (OC_STACK_OK != ocRet) {
- ERR("notifyListOfObservers() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_notify_all(int resHandle)
-{
- OCStackResult ocRet;
-
- try {
- ocRet = notifyAllObservers(GINT_TO_POINTER(resHandle));
- } catch (OCException& e) {
- ERR("notifyAllObservers() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_NO_OBSERVERS == ocRet) {
- WARN("No observers. Stop notifying");
- return IOTCON_ERROR_NONE;
- } else if (OC_STACK_OK != ocRet) {
- ERR("notifyAllObservers() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_send_response(GVariant *resp)
-{
- int ret;
- int result;
- int error_code;
- int request_handle;
- int resource_handle;
- unsigned short option_id;
- char *option_data;
- char *new_uri;
- const char *repr_json;
- GVariantIter *options;
- iotcon_interface_e iface;
- string iface_str;
- HeaderOptions header_options;
- OCStackResult ocRet;
- OCRepresentation ocRep;
-
- g_variant_get(resp, "(&sia(qs)ii&sii)",
- &new_uri,
- &error_code,
- &options,
- &iface,
- &result,
- &repr_json,
- &request_handle,
- &resource_handle);
-
- auto pResponse = make_shared<OCResourceResponse>();
- if (NULL == pResponse) {
- ERR("NULL == pResponse");
- return IOTCON_ERROR_OUT_OF_MEMORY;
- }
-
- pResponse->setRequestHandle(GINT_TO_POINTER(request_handle));
- pResponse->setResourceHandle(GINT_TO_POINTER(resource_handle));
- pResponse->setErrorCode(error_code);
- pResponse->setResponseResult((OCEntityHandlerResult)result);
-
- if (IC_STR_EQUAL != strcmp(new_uri, IC_STR_NULL))
- pResponse->setNewResourceUri(new_uri);
-
- if (IOTCON_INTERFACE_NONE != iface) {
- ret = _icd_ioty_convert_interface_flag(iface, iface_str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("_icd_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret);
- return ret;
- }
- } else {
- iface_str = DEFAULT_INTERFACE;
- }
-
- ret = icd_ioty_repr_parse_json(repr_json, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_parse_json() Fail(%d)", ret);
- return ret;
- }
-
- pResponse->setResourceRepresentation(ocRep, iface_str);
-
- while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) {
- HeaderOption::OCHeaderOption option(option_id, option_data);
- header_options.push_back(option);
- }
- g_variant_iter_free(options);
-
- pResponse->setHeaderOptions(header_options);
-
- try {
- ocRet = sendResponse(pResponse);
- } catch (OCException& e) {
- ERR("sendResponse() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ocRet) {
- ERR("sendResponse() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_find_resource(const char *host_address, const char *resource_type,
- unsigned int signal_number, const char *sender)
-{
- OCConnectivityType conn_type = OC_IPV4;
- OCStackResult ret;
- ostringstream requestHost;
-
- if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address)) {
- requestHost << OC_MULTICAST_DISCOVERY_URI;
- conn_type = OC_ALL;
- } else {
- requestHost << ICD_COAP << host_address << OC_MULTICAST_DISCOVERY_URI;
- }
-
- if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
- requestHost << "?rt=" << resource_type;
-
- shared_ptr<icdIotivityHandler::findObject> object
- = make_shared<icdIotivityHandler::findObject>(signal_number, sender);
- FindCallback findCallback = bind(&icdIotivityHandler::findObject::foundResource,
- object, placeholders::_1);
-
- try {
- ret = findResource("", requestHost.str(), conn_type, findCallback);
- } catch (OCException& e) {
- ERR("findResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
- if (OC_STACK_OK != ret) {
- ERR("findResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-static OCResource::Ptr _icd_ioty_create_oc_resource(GVariant *client)
-{
- int ifaces;
- int observe_handle;
- int is_observable;
- unsigned short option_id;
- char *type;
- char *host;
- char *uri;
- char *option_data;
- GVariantIter *options;
- GVariantIter *types;
-
- string resource_host;
- vector<string> resource_types;
- vector<string> resource_ifs;
- HeaderOptions header_options;
-
- g_variant_get(client, "(&s&sba(qs)asii)", &uri, &host, &is_observable, &options,
- &types, &ifaces, &observe_handle);
-
- resource_host = string(ICD_COAP) + string(host);
-
- while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) {
- HeaderOption::OCHeaderOption option(option_id, option_data);
- header_options.push_back(option);
- }
- g_variant_iter_free(options);
-
- while (g_variant_iter_loop(types, "&s", &type))
- resource_types.push_back(type);
- g_variant_iter_free(types);
-
- if (IOTCON_INTERFACE_NONE == ifaces) {
- resource_ifs.push_back(DEFAULT_INTERFACE);
- } else {
- if (IOTCON_INTERFACE_DEFAULT & ifaces)
- resource_ifs.push_back(DEFAULT_INTERFACE);
-
- if (IOTCON_INTERFACE_LINK & ifaces)
- resource_ifs.push_back(LINK_INTERFACE);
-
- if (IOTCON_INTERFACE_BATCH & ifaces)
- resource_ifs.push_back(BATCH_INTERFACE);
-
- if (IOTCON_INTERFACE_GROUP & ifaces)
- resource_ifs.push_back(GROUP_INTERFACE);
- }
-
- /* TODO : OC_ALL has wrong behaviour in iotivity version 0.9.1.
- * Therefore, OC_IPV4 SHOULD be changed to OC_ALL later.
- */
- OCResource::Ptr ocResource = constructResourceObject(resource_host, uri, OC_IPV4,
- is_observable, resource_types, resource_ifs);
-
- ocResource->setHeaderOptions(header_options);
-
- return ocResource;
-}
-
-
-extern "C" int icd_ioty_get(GVariant *resource, GVariant *query,
- unsigned int signal_number, const char *sender)
-{
- OCStackResult ret;
- OCResource::Ptr ocResource;
- QueryParamsMap queryParams;
- GVariantIter *queryIter;
- char *key, *value;
-
- g_variant_get(query, "a(ss)", &queryIter);
- while (g_variant_iter_loop(queryIter, "(&s&s)", &key, &value))
- queryParams[key] = value;
- g_variant_iter_free(queryIter);
-
- ocResource = _icd_ioty_create_oc_resource(resource);
-
- shared_ptr<icdIotivityHandler::getObject> object
- = make_shared<icdIotivityHandler::getObject>(signal_number, sender);
- GetCallback getCallback = bind(&icdIotivityHandler::getObject::onGet, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- try {
- ret = ocResource->get(queryParams, getCallback);
- } catch (OCException& e) {
- ERR("get() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
- if (OC_STACK_OK != ret) {
- ERR("get() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_put(GVariant *resource, const char *repr, GVariant *query,
- unsigned int signal_number, const char *sender)
-{
- int ret;
- OCStackResult ocRet;
- OCResource::Ptr ocResource;
- QueryParamsMap queryParams;
- OCRepresentation ocRep;
- GVariantIter *queryIter;
- char *key, *value;
-
- g_variant_get(query, "a(ss)", &queryIter);
- while (g_variant_iter_loop(queryIter, "(&s&s)", &key, &value))
- queryParams[key] = value;
- g_variant_iter_free(queryIter);
-
- ret = icd_ioty_repr_parse_json(repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_parse_json() Fail(%d)", ret);
- return ret;
- }
-
- ocResource = _icd_ioty_create_oc_resource(resource);
-
- shared_ptr<icdIotivityHandler::putObject> object
- = make_shared<icdIotivityHandler::putObject>(signal_number, sender);
- PutCallback putCallback = bind(&icdIotivityHandler::putObject::onPut, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- try {
- ocRet = ocResource->put(ocRep, queryParams, putCallback);
- } catch (OCException& e) {
- ERR("put() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ocRet) {
- ERR("put() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_post(GVariant *resource, const char *repr, GVariant *query,
- unsigned int signal_number, const char *sender)
-{
- int ret;
- OCStackResult ocRet;
- OCResource::Ptr ocResource;
- QueryParamsMap queryParams;
- OCRepresentation ocRep;
- GVariantIter *queryIter;
- char *key, *value;
-
- g_variant_get(query, "a(ss)", &queryIter);
- while (g_variant_iter_loop(queryIter, "(&s&s)", &key, &value))
- queryParams[key] = value;
- g_variant_iter_free(queryIter);
-
- ret = icd_ioty_repr_parse_json(repr, ocRep);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icd_ioty_repr_parse_json() Fail(%d)", ret);
- return ret;
- }
-
- ocResource = _icd_ioty_create_oc_resource(resource);
-
- shared_ptr<icdIotivityHandler::postObject> object
- = make_shared<icdIotivityHandler::postObject>(signal_number, sender);
- PostCallback postCallback = bind(&icdIotivityHandler::postObject::onPost, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- try {
- ocRet = ocResource->post(ocRep, queryParams, postCallback);
- } catch (OCException& e) {
- ERR("post() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ocRet) {
- ERR("post() Fail(%d)", ocRet);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_delete(GVariant *resource, unsigned int signal_number,
- const char *sender)
-{
- OCStackResult ret;
- OCResource::Ptr ocResource;
-
- ocResource = _icd_ioty_create_oc_resource(resource);
-
- shared_ptr<icdIotivityHandler::deleteObject> object
- = make_shared<icdIotivityHandler::deleteObject>(signal_number, sender);
- DeleteCallback deleteCallback = bind(&icdIotivityHandler::deleteObject::onDelete,
- object, placeholders::_1, placeholders::_2);
-
- try {
- ret = ocResource->deleteResource(deleteCallback);
- } catch (OCException& e) {
- ERR("deleteResource() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("deleteResource() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_observer_start(GVariant *resource, int observe_type,
- GVariant *query, unsigned int signal_number, const char *sender, int *observe_h)
-{
- OCStackResult ret;
- OCResource::Ptr ocResource;
- ObserveType observeType;
- QueryParamsMap queryParams;
- OCRepresentation ocRep;
- GVariantIter *queryIter;
- char *key, *value;
-
- g_variant_get(query, "a(ss)", &queryIter);
- while (g_variant_iter_loop(queryIter, "(&s&s)", &key, &value))
- queryParams[key] = value;
- g_variant_iter_free(queryIter);
-
- 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 = _icd_ioty_create_oc_resource(resource);
-
- resource_handle *obs_h = new resource_handle();
- obs_h->ocResource = ocResource;
- *observe_h = GPOINTER_TO_INT((void*)obs_h);
-
- shared_ptr<icdIotivityHandler::observeObject> object
- = make_shared<icdIotivityHandler::observeObject>(signal_number, sender);
- ObserveCallback observeCallback = bind(&icdIotivityHandler::observeObject::onObserve,
- object, placeholders::_1, placeholders::_2, placeholders::_3,
- placeholders::_4);
-
- try {
- ret = ocResource->observe(observeType, queryParams, observeCallback);
- } catch (OCException& e) {
- ERR("observe() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("observe() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_observer_stop(void *observe_h)
-{
- OCStackResult ret;
-
- resource_handle *resource_h = (resource_handle*)observe_h;
-
- OCResource::Ptr ocResource = resource_h->ocResource;
- delete (resource_handle*)observe_h;
-
- try {
- ret = ocResource->cancelObserve(QualityOfService::HighQos);
- } catch (OCException& e) {
- ERR("cancelObserve() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("cancelObserve() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */
-extern "C" int icd_ioty_register_device_info(GVariant *value)
-{
- OCStackResult ret;
-
- OCDeviceInfo info = {0};
-
- g_variant_get(value, "(&s&s&s&s&s&s&s&s&s&s&s&s)",
- &info.deviceName,
- &info.hostName,
- &info.deviceUUID,
- &info.contentType,
- &info.version,
- &info.manufacturerName,
- &info.manufacturerUrl,
- &info.modelNumber,
- &info.dateOfManufacture,
- &info.platformVersion,
- &info.firmwareVersion,
- &info.supportUrl);
-
- try {
- ret = registerDeviceInfo(info);
- } catch (OCException& e) {
- ERR("registerDeviceInfo() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("registerDeviceInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_get_device_info(const char *host_address,
- unsigned int signal_number, const char *sender)
-{
- OCStackResult ret;
- ostringstream requestHost;
- std::string deviceDiscoveryURI = "/oic/d";
-
- if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address))
- requestHost << host_address << ":" << OC_MULTICAST_PORT << deviceDiscoveryURI;
- else
- requestHost << ICD_COAP << host_address << deviceDiscoveryURI;
-
- shared_ptr<icdIotivityHandler::deviceObject> object
- = make_shared<icdIotivityHandler::deviceObject>(signal_number, sender);
- FindDeviceCallback findDeviceCallback = bind(
- &icdIotivityHandler::deviceObject::receivedDeviceInfo,
- object,
- placeholders::_1);
-
- try {
- /* TODO : OC_ALL has wrong behaviour in iotivity version 0.9.1.
- * Therefore, OC_IPV4 SHOULD be changed to OC_ALL later.
- */
- ret = getDeviceInfo("", requestHost, OC_IPV4, findDeviceCallback);
- } catch (OCException& e) {
- ERR("getDeviceInfo() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("getDeviceInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-#endif
-
-extern "C" int icd_ioty_register_platform_info(GVariant *value)
-{
- OCStackResult ret;
- OCPlatformInfo info = {0};
-
- g_variant_get(value, "(&s&s&s&s&s&s&s&s&s&s&s)",
- &info.platformID,
- &info.manufacturerName,
- &info.manufacturerUrl,
- &info.modelNumber,
- &info.dateOfManufacture,
- &info.platformVersion,
- &info.operatingSystemVersion,
- &info.hardwareVersion,
- &info.firmwareVersion,
- &info.supportUrl,
- &info.systemTime);
-
- try {
- ret = registerPlatformInfo(info);
- } catch (OCException& e) {
- ERR("registerPlatformInfo() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("registerPlatformInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_get_platform_info(const char *host_address,
- unsigned int signal_number, const char *sender)
-{
- OCStackResult ret;
- ostringstream requestHost;
- std::string platformDiscoveryURI = "/oic/p";
-
- if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address))
- requestHost << host_address << ":" << OC_MULTICAST_PORT << platformDiscoveryURI;
- else
- requestHost << ICD_COAP << host_address << platformDiscoveryURI;
-
- shared_ptr<icdIotivityHandler::platformObject> object
- = make_shared<icdIotivityHandler::platformObject>(signal_number, sender);
- FindPlatformCallback findPlatformCallback = bind(
- &icdIotivityHandler::platformObject::receivedPlatformInfo,
- object,
- placeholders::_1);
-
- try {
- /* TODO : OC_ALL has wrong behaviour in iotivity version 0.9.1.
- * Therefore, OC_IPV4 SHOULD be changed to OC_ALL later.
- */
- ret = getPlatformInfo("", requestHost.str(), OC_IPV4, findPlatformCallback);
- } catch (OCException& e) {
- ERR("getPlatformInfo() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("getPlatformInfo() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" iotcon_presence_h icd_ioty_subscribe_presence(const char *host_address,
- const char *resource_type, unsigned int signal_number, const char *sender)
-{
- OCStackResult ret;
- iotcon_presence_h presence_handle = NULL;
- string host;
-
- shared_ptr<icdIotivityHandler::presenceObject> object
- = make_shared<icdIotivityHandler::presenceObject>(signal_number, sender);
- SubscribeCallback subscribeCallback
- = bind(&icdIotivityHandler::presenceObject::presenceHandler, object,
- placeholders::_1, placeholders::_2, placeholders::_3);
-
- host = string(ICD_COAP) + string(host_address);
-
- try {
- /* TODO : OC_ALL has wrong behaviour in iotivity version 0.9.1.
- * Therefore, OC_IPV4 SHOULD be changed to OC_ALL later.
- */
- ret = subscribePresence(presence_handle, host, resource_type, OC_IPV4,
- subscribeCallback);
- } catch (OCException& e) {
- ERR("subscribePresence() Fail(%s)", e.reason().c_str());
- return NULL;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("subscribePresence() Fail(%d)", ret);
- return NULL;
- }
-
- return presence_handle;
-}
-
-
-extern "C" int icd_ioty_unsubscribe_presence(iotcon_presence_h presence_handle)
-{
- OCStackResult ret;
-
- try {
- ret = unsubscribePresence(presence_handle);
- } catch (OCException& e) {
- ERR("unsubscribePresence() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("unsubscribePresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_start_presence(unsigned int time_to_live)
-{
- OCStackResult ret;
-
- try {
- ret = startPresence(time_to_live);
- } catch (OCException& e) {
- ERR("startPresence() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("startPresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-extern "C" int icd_ioty_stop_presence()
-{
- OCStackResult ret;
-
- try {
- ret = stopPresence();
- } catch (OCException& e) {
- ERR("stopPresence() Fail(%s)", e.reason().c_str());
- return IOTCON_ERROR_IOTIVITY;
- }
-
- if (OC_STACK_OK != ret) {
- ERR("stopPresence() Fail(%d)", ret);
- return IOTCON_ERROR_IOTIVITY;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
#ifndef __IOT_CONNECTIVITY_MANAGER_DAEMON_IOTIVITY_H__
#define __IOT_CONNECTIVITY_MANAGER_DAEMON_IOTIVITY_H__
+#include <stdio.h>
#include <stdint.h>
+#include <glib.h>
#include "iotcon.h"
-void icd_ioty_config(const char *addr, unsigned short port);
+#define ICD_IOTY_COAP "coap://"
+#define ICD_IOTY_COAPS "coaps://"
-void* icd_ioty_register_resource(const char *uri, const char* const* res_types,
+typedef struct {
+ unsigned int signum;
+ char *sender;
+} icd_sig_ctx_s;
+
+
+void icd_ioty_csdk_lock();
+
+void icd_ioty_csdk_unlock();
+
+GThread* icd_ioty_init(const char *addr, unsigned short port);
+
+void icd_ioty_deinit();
+
+void* icd_ioty_register_resource(const char *uri_path, const char* const* res_types,
int ifaces, uint8_t properties);
int icd_ioty_unregister_resource(void *resource_handle);
#include "icd.h"
#include "icd-dbus.h"
#include "icd-ioty.h"
+#include "icd-ioty-ocprocess.h"
#define ICD_ALL_INTERFACES "0.0.0.0"
#define ICD_RANDOM_PORT 0
int main(int argc, char **argv)
{
guint id;
+ GThread *thread;
GMainLoop *loop;
#if !GLIB_CHECK_VERSION(2,35,0)
loop = g_main_loop_new(NULL, FALSE);
id = icd_dbus_init();
- icd_ioty_config(ICD_ALL_INTERFACES, ICD_RANDOM_PORT);
+ thread = icd_ioty_init(ICD_ALL_INTERFACES, ICD_RANDOM_PORT);
+ if (NULL == thread) {
+ ERR("icd_ioty_init() Fail");
+ icd_dbus_deinit(id);
+ return -1;
+ }
g_main_loop_run(loop);
+ icd_ioty_deinit(thread);
icd_dbus_deinit(id);
g_main_loop_unref(loop);
#include <string.h>
#include <errno.h>
#include <glib.h>
+#include <json-glib/json-glib.h>
#include "iotcon-struct.h"
#include "ic-utils.h"
#include "icl-options.h"
#include "icl-resource-types.h"
#include "icl-dbus.h"
+#include "icl-repr.h"
#include "icl-client.h"
/* 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,
+API int iotcon_find_resource(const char *host_address, 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 == host_address, 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 = icl_dbus_find_resource(host_addr, resource_type, cb, user_data);
+ ret = icl_dbus_find_resource(host_address, resource_type, cb, user_data);
if (IOTCON_ERROR_NONE != ret)
ERR("icl_dbus_find_resource() Fail(%d)", 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,
+API iotcon_client_h iotcon_client_new(const char *host, const char *uri_path,
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 == uri_path, NULL);
RETV_IF(NULL == resource_types, NULL);
resource = calloc(1, sizeof(struct icl_remote_resource));
}
resource->host = ic_utils_strdup(host);
- resource->uri = ic_utils_strdup(uri);
+ resource->uri_path = ic_utils_strdup(uri_path);
resource->is_observable = is_observable;
resource->types = icl_resource_types_ref(resource_types);
resource->ifaces = resource_ifs;
RET_IF(NULL == resource);
- free(resource->uri);
+ free(resource->uri_path);
free(resource->host);
free(resource->sid);
RETV_IF(NULL == resource, NULL);
clone = iotcon_client_new(resource->host,
- resource->uri,
+ resource->uri_path,
resource->is_observable,
iotcon_resource_types_clone(resource->types),
resource->ifaces);
/* The content of the resource should not be freed by user. */
-API int iotcon_client_get_uri(iotcon_client_h resource, char **uri)
+API int iotcon_client_get_uri_path(iotcon_client_h resource, char **uri_path)
{
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
- *uri = resource->uri;
+ *uri_path = resource->uri_path;
return IOTCON_ERROR_NONE;
}
return IOTCON_ERROR_NONE;
}
-
API int iotcon_client_get_server_id(iotcon_client_h resource, char **sid)
{
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
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)
{
return ret;
}
+
+
+iotcon_client_h icl_client_parse_resource_object(JsonParser *parser, char *json_string,
+ const char *host, iotcon_connectivity_type_e conn_type)
+{
+ FN_CALL;
+ int ret, observable;
+ GError *error = NULL;
+ iotcon_client_h client;
+ const char *uri_path, *server_id;
+ int ifaces = IOTCON_INTERFACE_NONE;
+ JsonObject *rsrc_obj, *property_obj;
+ iotcon_resource_types_h res_types = NULL;
+
+ DBG("input str : %s", json_string);
+
+ 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);
+ return NULL;
+ }
+
+ rsrc_obj = json_node_get_object(json_parser_get_root(parser));
+
+ uri_path = json_object_get_string_member(rsrc_obj, IC_JSON_KEY_URI_PATH);
+ server_id = json_object_get_string_member(rsrc_obj, IC_JSON_KEY_SERVERID);
+ if (NULL == server_id) {
+ ERR("Invalid Server ID");
+ return NULL;
+ }
+
+ /* parse resources type and interfaces */
+ property_obj = json_object_get_object_member(rsrc_obj, IC_JSON_KEY_PROPERTY);
+ if (property_obj) {
+ ret = icl_repr_parse_resource_property(property_obj, &res_types, &ifaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icl_repr_parse_resource_property() Fail(%d)", ret);
+ return NULL;
+ }
+ }
+
+ /* parse observable */
+ observable = json_object_get_int_member(rsrc_obj, IC_JSON_KEY_OBSERVABLE);
+
+ client = iotcon_client_new(host, uri_path, !!observable, res_types, ifaces);
+ if (res_types)
+ iotcon_resource_types_free(res_types);
+
+ if (NULL == client) {
+ ERR("iotcon_client_new() Fail");
+ return NULL;
+ }
+
+ client->sid = strdup(server_id);
+ if (NULL == client->sid) {
+ ERR("strdup(sid) Fail(%d)", errno);
+ iotcon_client_free(client);
+ return NULL;
+ }
+ client->conn_type = conn_type;
+
+ return client;
+}
#include "iotcon-struct.h"
#include "icl-options.h"
+#include <json-glib/json-glib.h>
struct icl_remote_resource {
- char *uri;
+ char *uri_path;
char *host;
char *sid;
bool is_observable;
iotcon_options_h header_options;
iotcon_resource_types_h types;
int ifaces;
+ iotcon_connectivity_type_e conn_type;
icl_handle_container_s *observe_handle;
};
+iotcon_client_h icl_client_parse_resource_object(JsonParser *parser, char *json_string,
+ const char *host, iotcon_connectivity_type_e conn_type);
+
#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_CLIENT_H__ */
}
value = g_variant_new("(sia(qs)iisii)",
- ic_utils_dbus_encode_str(response->new_uri),
+ ic_utils_dbus_encode_str(response->new_uri_path),
response->error_code,
options,
response->iface,
GVariantBuilder *options;
GHashTableIter iter;
gpointer option_id, option_data;
- GVariantBuilder *types;
- GList *node;
options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
if (resource->header_options) {
g_variant_builder_add(options, "(qs)", GPOINTER_TO_INT(option_id), option_data);
}
- types = g_variant_builder_new(G_VARIANT_TYPE("as"));
- for (node = resource->types->type_list; node; node = node->next)
- g_variant_builder_add(types, "s", node->data);
-
- value = g_variant_new("(ssba(qs)asii)",
- resource->uri,
+ value = g_variant_new("(ssba(qs)iii)",
+ resource->uri_path,
resource->host,
resource->is_observable,
options,
- types,
resource->ifaces,
+ resource->conn_type,
GPOINTER_TO_INT(resource->observe_handle));
g_variant_builder_unref(options);
- g_variant_builder_unref(types);
return value;
}
}
query_value = g_variant_new("a(ss)", builder);
+
g_variant_builder_unref(builder);
return query_value;
g_variant_builder_add(builder, "i", GPOINTER_TO_INT(node->data));
obs = g_variant_new("ai", builder);
+
g_variant_builder_unref(builder);
return obs;
char *value = NULL;
GVariantIter *repr;
char *repr_json;
- char *repr_uri;
+ char *repr_uri_path;
int request_handle;
int resource_handle;
struct icl_resource_request request = {0};
g_variant_get(parameters, "(i&sa(qs)a(ss)iiasii)",
&request.types,
- &request.uri,
+ &request.uri_path,
&options,
&query,
&request.observation_info.action,
g_variant_iter_free(repr);
return;
}
- repr_uri = icl_repr_json_get_uri(repr_json);
- iotcon_repr_set_uri(cur_repr, repr_uri);
- free(repr_uri);
+ repr_uri_path = icl_repr_json_get_uri_path(repr_json);
+ iotcon_repr_set_uri_path(cur_repr, repr_uri_path);
+ free(repr_uri_path);
if (0 == index)
request.repr = cur_repr;
}
-icl_handle_container_s* icl_dbus_register_resource(const char *uri,
+icl_handle_container_s* icl_dbus_register_resource(const char *uri_path,
iotcon_resource_types_h types,
int ifaces,
uint8_t properties,
return NULL;
}
- ic_dbus_call_register_resource_sync(icl_dbus_object, uri, res_types,
+ ic_dbus_call_register_resource_sync(icl_dbus_object, uri_path, res_types,
ifaces, properties, signal_number, &resource_handle, NULL, &error);
if (error) {
ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS);
arg_response = icl_dbus_response_to_gvariant(response);
- ic_dbus_call_send_response_sync(icl_dbus_object, arg_response, &ret, NULL, &error);
+ ic_dbus_call_send_response_sync(icl_dbus_object, arg_response,
+ &ret, NULL, &error);
if (error) {
ERR("ic_dbus_call_send_response_sync() Fail(%s)", error->message);
g_error_free(error);
gpointer user_data)
{
FN_CALL;
- char *type;
- GVariantIter *types;
-
+ int conn_type;
+ JsonParser *parser;
+ iotcon_client_h client;
+ char *payload, *host;
icl_cb_container_s *cb_container = user_data;
iotcon_found_resource_cb cb = cb_container->cb;
- struct icl_remote_resource resource = {0};
+ g_variant_get(parameters, "(&s&si)", &payload, &host, &conn_type);
+
+ RET_IF(NULL == payload);
+ RET_IF(NULL == host);
- g_variant_get(parameters, "(&s&s&siasi)",
- &resource.uri,
- &resource.host,
- &resource.sid,
- &resource.is_observable,
- &types,
- &resource.ifaces);
+ parser = json_parser_new();
- if (g_variant_iter_n_children(types)) {
- resource.types = iotcon_resource_types_new();
- while (g_variant_iter_loop(types, "&s", &type))
- iotcon_resource_types_insert(resource.types, type);
+ client = icl_client_parse_resource_object(parser, payload, host, conn_type);
+ if (NULL == client) {
+ ERR("icl_client_parse_resource_object() Fail");
+ g_object_unref(parser);
+ return;
}
- g_variant_iter_free(types);
if (cb)
- cb(&resource, cb_container->user_data);
+ cb(client, cb_container->user_data);
+
+ iotcon_client_free(client);
- if (resource.types)
- iotcon_resource_types_free(resource.types);
+ g_object_unref(parser);
}
}
+static inline int _icl_dbus_convert_daemon_error(int error)
+{
+ int ret;
+
+ if (IOTCON_ERROR_INVALID_PARAMETER == error)
+ ret = IOTCON_ERROR_SYSTEM;
+ else
+ ret = error;
+
+ return ret;
+}
+
+
static void _icl_dbus_on_cru(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
gpointer user_data)
{
FN_CALL;
- int index;
+ int res;
GVariantIter *options;
unsigned short option_id;
char *option_data;
iotcon_options_h header_options = NULL;
iotcon_repr_h repr = NULL;
- GVariantIter *reprIter;
- char *repr_json;
- char *repr_uri;
- int res;
+ char *repr_json = NULL;
icl_cb_container_s *cb_container = user_data;
iotcon_on_cru_cb cb = cb_container->cb;
icl_dbus_sub_ids = g_list_remove(icl_dbus_sub_ids,
GUINT_TO_POINTER(cb_container->id));
- g_variant_get(parameters, "(a(qs)asi)", &options, &reprIter, &res);
+ g_variant_get(parameters, "(a(qs)si)", &options, &repr_json, &res);
- if (g_variant_iter_n_children(options)) {
+ if (IOTCON_ERROR_NONE == res && g_variant_iter_n_children(options)) {
header_options = iotcon_options_new();
while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data))
iotcon_options_insert(header_options, option_id, option_data);
+ g_variant_iter_free(options);
}
- g_variant_iter_free(options);
- for (index = 0; g_variant_iter_loop(reprIter, "&s", &repr_json); index++) {
- iotcon_repr_h cur_repr = icl_repr_parse_json(repr_json);
- if (NULL == cur_repr) {
- ERR("icl_repr_parse_json() Fail");
- iotcon_options_free(header_options);
- if (repr)
- iotcon_repr_free(repr);
- g_variant_iter_free(reprIter);
+ if (IC_STR_EQUAL == strcmp(IC_STR_NULL, repr_json))
+ repr = iotcon_repr_new();
+ else {
+ repr = icl_repr_create_repr(repr_json);
+ if (NULL == repr) {
+ ERR("icl_repr_create_repr() Fail");
return;
}
- repr_uri = icl_repr_json_get_uri(repr_json);
- iotcon_repr_set_uri(cur_repr, repr_uri);
- free(repr_uri);
-
- if (0 == index)
- repr = cur_repr;
- else
- repr->children = g_list_append(repr->children, cur_repr);
}
- g_variant_iter_free(reprIter);
+
+ res = _icl_dbus_convert_daemon_error(res);
if (cb)
- cb(header_options, repr, res, cb_container->user_data);
+ cb(repr, header_options, res, cb_container->user_data);
if (repr)
iotcon_repr_free(repr);
iotcon_repr_h repr = NULL;
GVariantIter *reprIter;
char *repr_json;
- char *repr_uri;
+ char *repr_uri_path;
int res;
int seq_num;
g_variant_iter_free(reprIter);
return;
}
- repr_uri = icl_repr_json_get_uri(repr_json);
- iotcon_repr_set_uri(cur_repr, repr_uri);
- free(repr_uri);
+ repr_uri_path = icl_repr_json_get_uri_path(repr_json);
+ iotcon_repr_set_uri_path(cur_repr, repr_uri_path);
+ free(repr_uri_path);
if (0 == index)
repr = cur_repr;
} icl_handle_container_s;
int icl_dbus_config(const char *address, unsigned short port);
-icl_handle_container_s* icl_dbus_register_resource(const char *uri,
+icl_handle_container_s* icl_dbus_register_resource(const char *uri_path,
iotcon_resource_types_h types,
int ifaces,
uint8_t properties,
int icl_dbus_send_response(struct icl_resource_response *response);
int icl_dbus_find_resource(const char *host_address, const char *resource_type,
- iotcon_found_resource_cb found_resource_cb, void *user_data);
+ iotcon_found_resource_cb cb, void *user_data);
int icl_dbus_get(iotcon_client_h resource, iotcon_query_h query,
iotcon_on_cru_cb cb, void *user_data);
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)) {
+ if (IC_STR_EQUAL == strcmp(DEFAULT_INTERFACE.c_str(), src)) {
*dest = IOTCON_INTERFACE_DEFAULT;
- } else if (IC_STR_EQUAL == LINK_INTERFACE.compare(interface_str)) {
+ } else if (IC_STR_EQUAL == strcmp(LINK_INTERFACE.c_str(), src)) {
*dest = IOTCON_INTERFACE_LINK;
- } else if (IC_STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) {
+ } else if (IC_STR_EQUAL == strcmp(BATCH_INTERFACE.c_str(), src)) {
*dest = IOTCON_INTERFACE_BATCH;
- } else if (IC_STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) {
+ } else if (IC_STR_EQUAL == strcmp(GROUP_INTERFACE.c_str(), src)) {
*dest = IOTCON_INTERFACE_GROUP;
} else {
ERR("Invalid interface");
};
struct icl_resource {
- char *uri;
+ char *uri_path;
bool is_observable;
iotcon_resource_types_h types;
int ifaces;
}
/*
-* A general result : {"rep":{"string":"Hello","intlist":[1,2,3]}}
-*/
+ * A general result : {"rep":{"string":"Hello","intlist":[1,2,3]}}
+ */
JsonObject* icl_obj_to_json(iotcon_repr_h repr)
{
int ret;
parent_obj = json_object_new();
if (json_obj)
- json_object_set_object_member(parent_obj, IOTCON_KEY_REP, json_obj);
+ json_object_set_object_member(parent_obj, IC_JSON_KEY_REP, json_obj);
return parent_obj;
}
}
/*
-* A general input : {"rep:"{"string":"Hello","intlist":[1,2,3]}}
-*/
+ * A general input : {"rep:"{"string":"Hello","intlist":[1,2,3]}}
+ */
iotcon_repr_h icl_obj_from_json(JsonObject *json_repr)
{
int ret;
RETV_IF(NULL == json_repr, NULL);
- obj = json_object_get_object_member(json_repr, IOTCON_KEY_REP);
+ obj = json_object_get_object_member(json_repr, IC_JSON_KEY_REP);
key_list = json_object_get_members(obj);
return ret_val;
}
-API int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri)
+API int iotcon_repr_get_uri_path(iotcon_repr_h repr, const char **uri_path)
{
RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
- *uri = repr->uri;
+ *uri_path = repr->uri_path;
return IOTCON_ERROR_NONE;
}
-API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri)
+API int iotcon_repr_set_uri_path(iotcon_repr_h repr, const char *uri_path)
{
RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- free(repr->uri);
- repr->uri = NULL;
+ free(repr->uri_path);
+ repr->uri_path = NULL;
- if (NULL == uri)
+ if (NULL == uri_path)
return IOTCON_ERROR_INVALID_PARAMETER;
- repr->uri = strdup(uri);
- if (NULL == repr->uri) {
+ repr->uri_path = strdup(uri_path);
+ if (NULL == repr->uri_path) {
ERR("strdup() Fail");
return IOTCON_ERROR_OUT_OF_MEMORY;
}
{
int i, ret, ifaces;
char *iface_str;
- const char *uri;
+ const char *uri_path;
JsonObject *repr_obj = NULL;
unsigned int rt_count = 0;
JsonObject *prop_obj = NULL;
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->uri_path) {
+ iotcon_repr_get_uri_path(cur_repr, &uri_path);
+ json_object_set_string_member(repr_obj, IC_JSON_KEY_URI_PATH, uri_path);
}
if (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);
+ json_object_set_object_member(repr_obj, IC_JSON_KEY_PROPERTY, prop_obj);
}
if (0 < rt_count) {
json_object_unref(repr_obj);
return NULL;
}
- json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array);
+ json_object_set_array_member(prop_obj, IC_JSON_KEY_RESOURCETYPES, rt_array);
}
if (IOTCON_INTERFACE_NONE != cur_repr->interfaces) {
json_array_add_string_element(if_array, iface_str);
}
- json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array);
+ json_object_set_array_member(prop_obj, IC_JSON_KEY_INTERFACES, if_array);
}
- FN_END;
return repr_obj;
}
*/
static JsonObject* _icl_repr_generate_json(iotcon_repr_h repr)
{
- JsonObject *repr_obj = NULL;
- JsonObject *root_obj = NULL;
- JsonArray *root_array = NULL;
+ unsigned int child_index;
unsigned int child_count = 0;
- unsigned int child_index = 0;
+ JsonArray *root_array = NULL;
+ JsonObject *repr_obj, *root_obj;
iotcon_repr_h child_repr = NULL;
RETV_IF(NULL == repr, NULL);
json_array_add_object_element(root_array, repr_obj);
}
- json_object_set_array_member(root_obj, IOTCON_KEY_OC, root_array);
+ json_object_set_array_member(root_obj, IC_JSON_KEY_OC, root_array);
return root_obj;
}
-char* icl_repr_generate_json(iotcon_repr_h repr, bool set_pretty)
+/*
+ * returned string SHOULD be released by you
+ */
+gchar* _icl_repr_obj_to_json(JsonObject *obj, bool set_pretty)
{
+ gchar *json_data;
JsonNode *root_node = NULL;
- char *json_data = NULL;
-
- JsonObject *obj = _icl_repr_generate_json(repr);
- if (NULL == obj) {
- ERR("icl_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);
}
+char* icl_repr_generate_json(iotcon_repr_h repr, bool set_pretty)
+{
+ char *json_data;
+ JsonObject *obj;
+
+ obj = _icl_repr_generate_json(repr);
+ if (NULL == obj) {
+ ERR("icl_repr_generate_json() Fail");
+ return NULL;
+ }
+
+ json_data = _icl_repr_obj_to_json(obj, set_pretty);
+ if (NULL == json_data) {
+ ERR("_icl_repr_obj_to_json() Fail");
+ return NULL;
+ }
+
+ return json_data;
+}
+
+
/*
- * returned string SHOULD be released
+ * returned string SHOULD be released by you
*/
-char* icl_repr_json_get_uri(const char *json_string)
+char* icl_repr_json_get_uri_path(const char *json_string)
{
GError *error = NULL;
gboolean ret = FALSE;
- char *uri = NULL;
- const char *uri_value = NULL;
+ char *uri_path = NULL;
+ const char *str_value = NULL;
JsonParser *parser;
JsonObject *root_obj;
}
root_obj = json_node_get_object(json_parser_get_root(parser));
-
- if (json_object_has_member(root_obj, IOTCON_KEY_URI)) {
- uri_value = json_object_get_string_member(root_obj, IOTCON_KEY_URI);
- if (NULL == uri_value) {
+ if (json_object_has_member(root_obj, IC_JSON_KEY_URI_PATH)) {
+ str_value = json_object_get_string_member(root_obj, IC_JSON_KEY_URI_PATH);
+ if (NULL == str_value) {
ERR("json_object_get_string_member() Fail");
return NULL;
}
- uri = strdup(uri_value);
- if (NULL == uri) {
+ uri_path = strdup(str_value);
+ if (NULL == uri_path) {
ERR("strdup() Fail");
return NULL;
}
}
- return uri;
+ return uri_path;
}
/*
* A general input : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
* "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
+ *
+ * Result : A iotcon_repr_h handle including uri_path
*/
-iotcon_repr_h icl_repr_parse_json(const char *json_string)
+static iotcon_repr_h _icl_repr_create_repr(JsonObject *rsrc_obj)
{
- const char *iface_str = NULL;
- const char *rtype_str = NULL;
- const char *uri_value = NULL;
- iotcon_interface_e iface_flag;
+ FN_CALL;
+ char *json_data;
+ iotcon_repr_h repr;
- RETV_IF(NULL == json_string, NULL);
+ json_data = _icl_repr_obj_to_json(rsrc_obj, false);
+ if (NULL == json_data) {
+ ERR("json_data is NULL");
+ return NULL;
+ }
- DBG("input str : %s", json_string);
+ repr = icl_repr_parse_json(json_data);
+ if (NULL == repr) {
+ ERR("icl_repr_parse_json() Fail");
+ g_free(json_data);
+ return NULL;
+ }
+
+ free(json_data);
+
+ return repr;
+}
+
+/*
+ * 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"]}}]}
+ *
+ * Result : iotcon_repr_h handles(parent and child) including uri_path
+ */
+iotcon_repr_h icl_repr_create_repr(const char *json_string)
+{
+ FN_CALL;
+ gboolean ret;
+ JsonParser *parser;
GError *error = NULL;
- gboolean ret = FALSE;
- JsonParser *parser = json_parser_new();
+ JsonArray *rsrc_array;
+ iotcon_repr_h repr_cur;
+ iotcon_repr_h repr_parent = NULL;
+ JsonObject *root_obj, *rsrc_obj;
+ unsigned int rsrc_count, rsrc_index;
+
+ 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));
+ 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 = icl_obj_from_json(root_obj);
- if (NULL == repr) {
- ERR("icl_obj_from_json() Fail()");
- g_object_unref(parser);
+ /* parse 'oc' prefix */
+ rsrc_array = json_object_get_array_member(root_obj, IC_JSON_KEY_OC);
+ if (NULL == rsrc_array) {
+ ERR("json_object_get_array_member() Fail");
+ return NULL;
+ }
+
+ rsrc_count = json_array_get_length(rsrc_array);
+ for (rsrc_index = 0; rsrc_index < rsrc_count; rsrc_index++) {
+ rsrc_obj = json_array_get_object_element(rsrc_array, rsrc_index);
+ repr_cur = _icl_repr_create_repr(rsrc_obj);
+ if (NULL == repr_cur) {
+ ERR("_icl_repr_create_repr() Fail");
+ if (0 < rsrc_index) /* parent was already made */
+ iotcon_repr_free(repr_parent);
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 (0 == rsrc_index) { /* parent representation */
+ repr_parent = repr_cur;
+ } else { /* child representation */
+ ret = iotcon_repr_append_child(repr_parent, repr_cur);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_repr_append_child() Fail(%d)", ret);
+ iotcon_repr_free(repr_parent);
+ return NULL;
}
}
- 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 = icl_ioty_convert_interface_string(iface_str, &iface_flag);
- ifaces |= iface_flag;
+ }
+
+ return repr_parent;
+}
+
+
+int icl_repr_parse_resource_property(JsonObject *prop_obj,
+ iotcon_resource_types_h *types, int *ifaces)
+{
+ int ret;
+ int ret_ifaces = IOTCON_INTERFACE_NONE;
+ JsonArray *iface_array, *rtye_array;
+ iotcon_resource_types_h res_types = NULL;
+
+ RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
+
+ rtye_array = json_object_get_array_member(prop_obj, IC_JSON_KEY_RESOURCETYPES);
+ if (rtye_array) {
+ unsigned int rt_count, rt_index;
+
+ rt_count = json_array_get_length(rtye_array);
+ if (0 < rt_count) {
+ res_types = iotcon_resource_types_new();
+ if (NULL == res_types) {
+ ERR("iotcon_resource_types_new() Fail");
+ return IOTCON_ERROR_OUT_OF_MEMORY;
}
- iotcon_repr_set_resource_interfaces(repr, ifaces);
+ for (rt_index = 0; rt_index < rt_count; rt_index++) {
+ const char *rtype_str;
+
+ rtype_str = json_array_get_string_element(rtye_array, rt_index);
+ iotcon_resource_types_insert(res_types, rtype_str);
+ }
}
}
+ iface_array = json_object_get_array_member(prop_obj, IC_JSON_KEY_INTERFACES);
+ if (iface_array) {
+ iotcon_interface_e iface_flag;
+ unsigned int if_count, if_index;
+
+ if_count = json_array_get_length(iface_array);
+ for (if_index = 0; if_index < if_count; if_index++) {
+ const char *iface_str = json_array_get_string_element(iface_array, if_index);
+ ret = icl_ioty_convert_interface_string(iface_str, &iface_flag);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icl_ioty_convert_interface_string() Fail(%d)", ret);
+ if (res_types)
+ iotcon_resource_types_free(res_types);
+ return ret;
+ }
+
+ ret_ifaces |= iface_flag;
+ }
+ }
+
+ *types = res_types;
+ *ifaces = ret_ifaces;
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+/*
+ * A general input : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]},
+ * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}
+ */
+iotcon_repr_h icl_repr_parse_json(const char *json_string)
+{
+ int ret;
+ JsonParser *parser;
+ GError *error = NULL;
+ const char *str_value;
+ iotcon_repr_h repr = NULL;
+ iotcon_resource_types_h res_types;
+ int ifaces = IOTCON_INTERFACE_NONE;
+ JsonObject *root_obj, *property_obj;
+
+ RETV_IF(NULL == json_string, NULL);
+
+ DBG("input str : %s", json_string);
+
+ 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;
+ }
+
+ root_obj = json_node_get_object(json_parser_get_root(parser));
+ if (NULL == root_obj) {
+ ERR("json_node_get_object() Fail");
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ repr = icl_obj_from_json(root_obj);
if (NULL == repr) {
- ERR("repr is NULL");
+ ERR("icl_obj_from_json() Fail()");
g_object_unref(parser);
return NULL;
}
- g_object_unref(parser);
+ str_value = json_object_get_string_member(root_obj, IC_JSON_KEY_URI_PATH);
+ ret = iotcon_repr_set_uri_path(repr, str_value);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_repr_set_uri_path() Fail(%d)", ret);
+ iotcon_repr_free(repr);
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ property_obj = json_object_get_object_member(root_obj, IC_JSON_KEY_PROPERTY);
+ if (NULL == property_obj) {
+ /* Here is normal condition */
+ g_object_unref(parser);
+ return repr;
+ }
+
+ ret = icl_repr_parse_resource_property(property_obj, &res_types, &ifaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icl_repr_parse_resource_property() Fail(%d)", ret);
+ iotcon_repr_free(repr);
+ g_object_unref(parser);
+ return NULL;
+ }
+
+ iotcon_repr_set_resource_types(repr, res_types);
+ iotcon_repr_set_resource_interfaces(repr, ifaces);
- FN_END;
+ g_object_unref(parser);
return repr;
}
if (false == _icl_repr_dec_ref_count(repr))
return;
- free(repr->uri);
+ free(repr->uri_path);
/* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */
g_list_free_full(repr->children, (GDestroyNotify)iotcon_repr_free);
iotcon_resource_types_free(repr->res_types);
g_hash_table_destroy(repr->hash_table);
free(repr);
-
- FN_END;
}
static void _icl_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h dest_repr)
return NULL;
}
- if (src->uri) {
- dest->uri = strdup(src->uri);
- if (NULL == dest->uri) {
+ if (src->uri_path) {
+ dest->uri_path = strdup(src->uri_path);
+ if (NULL == dest->uri_path) {
ERR("strdup() Fail");
iotcon_repr_free(dest);
return NULL;
#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 icl_repr_s {
- char *uri;
+ char *uri_path;
int ref_count;
int interfaces;
GHashTable *hash_table;
*/
char* icl_repr_generate_json(iotcon_repr_h repr, bool set_pretty);
-char* icl_repr_json_get_uri(const char *json_string);
+char* icl_repr_json_get_uri_path(const char *json_string);
+iotcon_repr_h icl_repr_create_repr(const char *json_string);
iotcon_repr_h icl_repr_parse_json(const char *json_string);
void icl_repr_inc_ref_count(iotcon_repr_h val);
+int icl_repr_parse_resource_property(JsonObject *prop_obj,
+ iotcon_resource_types_h *types, int *ifaces);
+
#endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_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)
+API int iotcon_request_get_uri(iotcon_request_h request, char **uri_path)
{
RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
- *uri = request->uri;
+ *uri_path = request->uri_path;
return IOTCON_ERROR_NONE;
}
struct icl_resource_request {
int types;
- char *uri;
+ char *uri_path;
iotcon_options_h header_options;
iotcon_query_h query;
struct icl_observe_info observation_info;
if (resp->repr)
iotcon_repr_free(resp->repr);
- if (resp->new_uri)
- free(resp->new_uri);
+ if (resp->new_uri_path)
+ free(resp->new_uri_path);
if (resp->header_options)
iotcon_options_free(resp->header_options);
free(resp);
{
int value;
va_list args;
- char *new_resource_uri = NULL;
+ char *new_uri_path = NULL;
iotcon_options_h options = NULL;
va_start(args, prop);
}
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 = strdup(new_resource_uri);
- if (NULL == resp->new_uri) {
+ case IOTCON_RESPONSE_NEW_URI_PATH:
+ new_uri_path = va_arg(args, char*);
+ if (resp->new_uri_path)
+ free(resp->new_uri_path);
+
+ if (new_uri_path) {
+ resp->new_uri_path = strdup(new_uri_path);
+ if (NULL == resp->new_uri_path) {
ERR("strdup() Fail(%d)", errno);
va_end(args);
return IOTCON_ERROR_OUT_OF_MEMORY;
}
} else {
- resp->new_uri = NULL;
+ resp->new_uri_path = NULL;
}
break;
case IOTCON_RESPONSE_HEADER_OPTIONS:
#include "icl-request.h"
struct icl_resource_response {
- char *new_uri;
+ char *new_uri_path;
int error_code;
iotcon_options_h header_options;
iotcon_interface_e iface;
}
-/* The length of uri should be less than or equal to 36. */
-API iotcon_resource_h iotcon_register_resource(const char *uri,
+/* The length of uri_path should be less than or equal to 36. */
+API iotcon_resource_h iotcon_register_resource(const char *uri_path,
iotcon_resource_types_h res_types,
int ifaces,
uint8_t properties,
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 == uri_path, NULL);
RETV_IF(NULL == res_types, NULL);
RETV_IF(NULL == cb, NULL);
+ RETVM_IF(IOTCON_URI_LENGTH_MAX < strlen(uri_path), NULL, "Invalid uri_path(%s)",
+ uri_path);
resource = calloc(1, sizeof(struct icl_resource));
if (NULL == resource) {
return NULL;
}
- resource->handle = icl_dbus_register_resource(uri, res_types, ifaces,
- properties, cb, user_data);
+ resource->handle = icl_dbus_register_resource(uri_path, res_types, ifaces, properties, cb,
+ user_data);
if (NULL == resource->handle) {
ERR("icl_dbus_register_resource() Fail");
free(resource);
resource->cb = cb;
resource->user_data = user_data;
- resource->uri = ic_utils_strdup(uri);
+ resource->uri_path = ic_utils_strdup(uri_path);
resource->types = icl_resource_types_ref(res_types);
resource->ifaces = ifaces;
resource->is_observable = properties & IOTCON_OBSERVABLE;
}
resource->handle = NULL;
- free(resource->uri);
- resource->uri = NULL;
+ free(resource->uri_path);
+ resource->uri_path = NULL;
free(resource->types);
resource->types = NULL;
/* The content of the resource should not be freed by user. */
-API int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri)
+API int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri_path)
{
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == uri, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
- *uri = resource->uri;
+ *uri_path = resource->uri_path;
return IOTCON_ERROR_NONE;
}
IOTCON_INTERFACE_DEFAULT = (1 << 0), /* default interface */
IOTCON_INTERFACE_LINK = (1 << 1), /* discovers children of the parent resource */
IOTCON_INTERFACE_BATCH = (1 << 2), /* requests CRUD to children of the parent resource */
- IOTCON_INTERFACE_GROUP = (1 << 3), /* requests CRUD to remote resources of a group. */
+ IOTCON_INTERFACE_GROUP = (1 << 3), /* requests CRUD to remote resources of a group */
} iotcon_interface_e;
+/**
+ * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
+ * @brief Enumerations of Iotcon connectivity types.
+ * @since_tizen 3.0
+ */
+typedef enum {
+ IOTCON_CONNECTIVITY_IPV4 = 0,
+ IOTCON_CONNECTIVITY_IPV6,
+ IOTCON_CONNECTIVITY_EDR,
+ IOTCON_CONNECTIVITY_LE,
+ IOTCON_CONNECTIVITY_ALL, /* sends over all the interfaces */
+} iotcon_connectivity_type_e;
+
typedef enum {
IOTCON_HIDDEN = 0,
IOTCON_ACTIVE = (1 << 0),
} iotcon_resource_property_e;
typedef enum {
- IOTCON_RESPONSE_RESOURCE_URI = 1,
+ IOTCON_RESPONSE_NEW_URI_PATH = 1,
IOTCON_RESPONSE_RESULT = 2,
IOTCON_RESPONSE_REPRESENTATION = 3,
IOTCON_RESPONSE_HEADER_OPTIONS = 4,
IOTCON_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
IOTCON_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
IOTCON_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA, /**< No data available */
+ IOTCON_ERROR_UNKNOWN = TIZEN_ERROR_UNKNOWN, /**< Daemon operation error */
IOTCON_ERROR_IOTIVITY = TIZEN_ERROR_IOTCON | 0x01, /**< Iotivity errors */
IOTCON_ERROR_REPRESENTATION = TIZEN_ERROR_IOTCON | 0x02, /**< Representation errors */
IOTCON_ERROR_INVALID_TYPE = TIZEN_ERROR_IOTCON | 0x03, /**< Invalid type */
IOTCON_ERROR_ALREADY = TIZEN_ERROR_IOTCON | 0x04, /**< Already */
IOTCON_ERROR_DBUS = TIZEN_ERROR_IOTCON | 0x05, /**< D-Bus errors */
+ IOTCON_ERROR_SYSTEM = TIZEN_ERROR_IOTCON | 0x06, /**< System errors */
}iotcon_error_e;
#endif /* __IOT_CONNECTIVITY_MANAGER_ERRORS_H__ */
* @ingroup CAPI_IOT_CONNECTIVITY_MODULE
* @brief Appends resource type name.
* @since_tizen 3.0
- * @remarks Stored string is replaced with @a uri. If @a uri is NULL, stored string is set
+ * @remarks Stored string is replaced with @a uri_path. If @a uri_path is NULL, stored string is set
* by NULL.
*
* @param[in] repr The handle to the Representation
- * @param[in] uri The URI of resource
+ * @param[in] uri_path The URI of resource
*
* @return 0 on success, otherwise a negative error value.
* @retval #IOTCON_ERROR_NONE Successful
* @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
* @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
*/
-int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri);
-int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri);
+int iotcon_repr_set_uri_path(iotcon_repr_h repr, const char *uri_path);
+int iotcon_repr_get_uri_path(iotcon_repr_h repr, const char **uri_path);
/**
* @ingroup CAPI_IOT_CONNECTIVITY_MODULE
int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number);
int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
iotcon_resource_h *child);
-int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri);
+int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri_path);
int iotcon_resource_get_types(iotcon_resource_h resource, iotcon_resource_types_h *types);
int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces);
int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable);
typedef struct icl_remote_resource* iotcon_client_h;
-int iotcon_client_get_uri(iotcon_client_h resource, char **uri);
+int iotcon_client_get_uri_path(iotcon_client_h resource, char **uri_path);
int iotcon_client_get_host(iotcon_client_h resource, char **host);
int iotcon_client_get_server_id(iotcon_client_h resource, char **sid);
int iotcon_client_get_types(iotcon_client_h resource, iotcon_resource_types_h *types);
int iotcon_client_set_options(iotcon_client_h resource, iotcon_options_h header_options);
typedef struct icl_resource_request* iotcon_request_h;
-int iotcon_request_get_uri(iotcon_request_h request, char **uri);
+int iotcon_request_get_uri(iotcon_request_h request, char **uri_path);
int iotcon_request_get_representation(iotcon_request_h request, iotcon_repr_h *repr);
int iotcon_request_get_types(iotcon_request_h request, int *types);
int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options);
void *user_data);
typedef void (*iotcon_request_handler_cb)(iotcon_request_h request, void *user_data);
-iotcon_resource_h iotcon_register_resource(const char *uri,
+iotcon_resource_h iotcon_register_resource(const char *uri_path,
iotcon_resource_types_h res_types,
int ifaces,
uint8_t properties,
typedef void (*iotcon_found_resource_cb)(iotcon_client_h resource, void *user_data);
int iotcon_find_resource(const char *host_address, const char *resource_type,
iotcon_found_resource_cb cb, void *user_data);
-iotcon_client_h iotcon_client_new(const char *host, const char *uri, bool is_observable,
+iotcon_client_h iotcon_client_new(const char *host, const char *uri_path, bool is_observable,
iotcon_resource_types_h resource_types, int resource_interfaces);
void iotcon_client_free(iotcon_client_h resource);
iotcon_client_h iotcon_client_clone(iotcon_client_h resource);
iotcon_observers_h observers);
int iotcon_notify_all(iotcon_resource_h resource);
-typedef void (*iotcon_on_cru_cb)(iotcon_options_h header_options, iotcon_repr_h repr,
+typedef void (*iotcon_on_cru_cb)(iotcon_repr_h repr, iotcon_options_h header_options,
int response_result, void *user_data);
int iotcon_get(iotcon_client_h resource, iotcon_query_h query,
iotcon_on_cru_cb cb, void *user_data);
#include <iotcon.h>
#include "test.h"
-static const char* const door_uri = "/a/door";
+static const char* const door_uri_path = "/a/door";
static char *door_resource_sid;
static iotcon_client_h door_resource = NULL;
iotcon_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL, _on_observe, NULL);
}
-static void _on_post(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+static void _on_post(iotcon_repr_h recv_repr, iotcon_options_h header_options,
int response_result, void *user_data)
{
int ret;
- char *created_uri = NULL;
+ char *created_uri_path = NULL;
iotcon_client_h new_door_resource = NULL;
char *host = NULL;
iotcon_resource_types_h types = NULL;
_print_repr_info(recv_repr);
- iotcon_repr_get_str(recv_repr, "createduri", &created_uri);
+ iotcon_repr_get_str(recv_repr, "createduripath", &created_uri_path);
- if (NULL == created_uri) {
- ERR("created_uri is NULL");
+ if (NULL == created_uri_path) {
+ ERR("created_uri_path is NULL");
return;
}
- DBG("New resource created : %s", created_uri);
+ DBG("New resource created : %s", created_uri_path);
ret = iotcon_client_get_host(door_resource, &host);
if (IOTCON_ERROR_NONE != ret) {
return;
}
- new_door_resource = iotcon_client_new(host, created_uri, true, types, ifaces);
+ new_door_resource = iotcon_client_new(host, created_uri_path, true, types, ifaces);
iotcon_delete(new_door_resource, _on_delete, NULL);
iotcon_client_free(new_door_resource);
}
-static void _on_put(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+static void _on_put(iotcon_repr_h recv_repr, iotcon_options_h header_options,
int response_result, void *user_data)
{
RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_put Response error(%d)",
iotcon_repr_free(send_repr);
}
-static void _on_get(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+static void _on_get(iotcon_repr_h recv_repr, iotcon_options_h header_options,
int response_result, void *user_data)
{
RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)",
static int _get_res_type_fn(const char *string, void *user_data)
{
- char *resource_uri = user_data;
+ char *resource_uri_path = user_data;
- DBG("[%s] resource type : %s", resource_uri, string);
+ DBG("[%s] resource type : %s", resource_uri_path, string);
return IOTCON_FUNC_CONTINUE;
}
static void _found_resource(iotcon_client_h resource, void *user_data)
{
int ret;
- char *resource_uri = NULL;
+ char *resource_uri_path = NULL;
char *resource_host = NULL;
char *resource_sid = NULL;
iotcon_resource_types_h resource_types = NULL;
INFO("===== resource found =====");
/* get the resource URI */
- ret = iotcon_client_get_uri(resource, &resource_uri);
+ ret = iotcon_client_get_uri_path(resource, &resource_uri_path);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_client_get_uri() Fail(%d)", ret);
+ ERR("iotcon_client_get_uri_path() Fail(%d)", ret);
return;
}
ERR("iotcon_client_get_server_id() Fail(%d)", ret);
return;
}
- DBG("[%s] resource server id : %s", resource_uri, resource_sid);
+ DBG("[%s] resource server id : %s", resource_uri_path, resource_sid);
- if (door_resource_sid && TEST_STR_EQUAL == strcmp(door_resource_sid, resource_sid)) {
- DBG("sid \"%s\" already found. skip !", resource_sid);
+ if (door_resource_sid && TEST_STR_EQUAL == strcmp(door_resource_sid, resource_sid)
+ && TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
+ DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
return;
}
ERR("iotcon_client_get_host() Fail(%d)", ret);
return;
}
- DBG("[%s] resource host : %s", resource_uri, resource_host);
+ DBG("[%s] resource host : %s", resource_uri_path, resource_host);
/* get the resource interfaces */
ret = iotcon_client_get_interfaces(resource, &resource_interfaces);
return;
}
if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
- DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_LINK & resource_interfaces)
- DBG("[%s] resource interface : LINK_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_BATCH & resource_interfaces)
- DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_GROUP & resource_interfaces)
- DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
/* get the resource types */
ret = iotcon_client_get_types(resource, &resource_types);
}
ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
- resource_uri);
+ resource_uri_path);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
return;
iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL);
- if (TEST_STR_EQUAL == strcmp(door_uri, resource_uri)) {
+ if (TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
door_resource = iotcon_client_clone(resource);
iotcon_query_h query = iotcon_query_new();
g_main_loop_run(loop);
g_main_loop_unref(loop);
+ free(door_resource_sid);
+
/* iotcon deinitialize */
iotcon_deinitialize();
/* Door Resource */
typedef struct _door_resource_s {
bool state;
- char *uri;
+ char *uri_path;
char *type;
iotcon_repr_h repr;
} door_resource_s;
return IOTCON_ERROR_OUT_OF_MEMORY;
}
- my_door.uri = strdup("/a/door");
- if (NULL == my_door.uri) {
+ my_door.uri_path = strdup("/a/door");
+ if (NULL == my_door.uri_path) {
ERR("strdup(/a/door) Fail");
return IOTCON_ERROR_OUT_OF_MEMORY;
}
INFO("[Door] opened.");
}
-static iotcon_resource_h _create_door_resource(char *uri, iotcon_interface_e interfaces,
+static iotcon_resource_h _create_door_resource(char *uri_path, iotcon_interface_e interfaces,
iotcon_resource_property_e properties)
{
iotcon_resource_types_h resource_types = iotcon_resource_types_new();
}
/* register door resource */
- iotcon_resource_h handle = iotcon_register_resource(uri, resource_types,
+ iotcon_resource_h handle = iotcon_register_resource(uri_path, resource_types,
interfaces, properties, _request_handler, NULL);
if (NULL == handle) {
iotcon_resource_types_free(resource_types);
/* create a door Representation */
resp_repr = iotcon_repr_new();
- iotcon_repr_set_uri(resp_repr, my_door.uri);
+ iotcon_repr_set_uri_path(resp_repr, my_door.uri_path);
iotcon_repr_set_bool(resp_repr, "opened", my_door.state);
_send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
_check_door_state();
resp_repr = iotcon_repr_new();
- iotcon_repr_set_uri(resp_repr, my_door.uri);
+ iotcon_repr_set_uri_path(resp_repr, my_door.uri_path);
iotcon_repr_set_bool(resp_repr, "opened", my_door.state);
_send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
/* send information that new resource was created */
resp_repr = iotcon_repr_new();
- iotcon_repr_set_str(resp_repr, "createduri", "/a/door1");
+ iotcon_repr_set_str(resp_repr, "createduripath", "/a/door1");
_send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED);
#include <iotcon.h>
#include "test.h"
-static const char* const room_uri = "/a/room";
+static const char* const room_uri_path = "/a/room";
static char *room_resource_sid;
static iotcon_client_h room_resource = NULL;
{
int i, ret;
unsigned int key_count, children_count;
- const char *uri;
+ const char *uri_path;
iotcon_repr_h child_repr;
iotcon_list_h list;
INFO("GET request was successful");
DBG("[ parent representation ]");
- iotcon_repr_get_uri(recv_repr, &uri);
- if (uri)
- DBG("uri : %s", uri);
+ iotcon_repr_get_uri_path(recv_repr, &uri_path);
+ if (uri_path)
+ DBG("uri_path : %s", uri_path);
key_count = iotcon_repr_get_keys_count(recv_repr);
if (key_count) {
char *str;
for (i = 0; i < children_count; i++) {
DBG("[ child representation ]");
- const char *uri;
+ const char *uri_path;
ret = iotcon_repr_get_nth_child(recv_repr, i, &child_repr);
if (IOTCON_ERROR_NONE != ret) {
continue;
}
- iotcon_repr_get_uri(child_repr, &uri);
- if (NULL == uri)
+ iotcon_repr_get_uri_path(child_repr, &uri_path);
+ if (NULL == uri_path)
continue;
- DBG("uri : %s", uri);
+ DBG("uri_path : %s", uri_path);
- if (TEST_STR_EQUAL == strcmp("/a/light", uri)) {
+ if (TEST_STR_EQUAL == strcmp("/a/light", uri_path)) {
key_count = iotcon_repr_get_keys_count(child_repr);
if (key_count) {
int brightness;
iotcon_repr_get_int(child_repr, "brightness", &brightness);
DBG("brightness : %d", brightness);
}
- } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri)) {
+ } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri_path)) {
key_count = iotcon_repr_get_keys_count(child_repr);
if (key_count) {
bool bswitch;
}
}
-static void _on_get_2nd(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+static void _on_get_2nd(iotcon_repr_h recv_repr, iotcon_options_h header_options,
int response_result, void *user_data)
{
_on_get(recv_repr, response_result);
}
-static void _on_get_1st(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+static void _on_get_1st(iotcon_repr_h recv_repr, iotcon_options_h header_options,
int response_result, void *user_data)
{
iotcon_query_h query_params;
static int _get_res_type_fn(const char *string, void *user_data)
{
- char *resource_uri = user_data;
+ char *resource_uri_path = user_data;
- DBG("[%s] resource type : %s", resource_uri, string);
+ DBG("[%s] resource type : %s", resource_uri_path, string);
return IOTCON_FUNC_CONTINUE;
}
static void _found_resource(iotcon_client_h resource, void *user_data)
{
int ret;
- char *resource_uri;
+ char *resource_uri_path;
char *resource_host;
char *resource_sid = NULL;
iotcon_resource_types_h resource_types = NULL;
INFO("===== resource found =====");
/* get the resource URI */
- ret = iotcon_client_get_uri(resource, &resource_uri);
+ ret = iotcon_client_get_uri_path(resource, &resource_uri_path);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_client_get_uri() Fail(%d)", ret);
+ ERR("iotcon_client_get_uri_path() Fail(%d)", ret);
return;
}
ERR("iotcon_client_get_server_id() Fail(%d)", ret);
return;
}
- DBG("[%s] resource server id : %s", resource_uri, resource_sid);
+ DBG("[%s] resource server id : %s", resource_uri_path, resource_sid);
- if (room_resource_sid && TEST_STR_EQUAL == strcmp(room_resource_sid, resource_sid)) {
- DBG("sid \"%s\" already found. skip !", resource_sid);
+ if (room_resource_sid && TEST_STR_EQUAL == strcmp(room_resource_sid, resource_sid)
+ && TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
+ DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
return;
}
ERR("iotcon_client_get_host() Fail(%d)", ret);
return;
}
- DBG("[%s] resource host : %s", resource_uri, resource_host);
+ DBG("[%s] resource host : %s", resource_uri_path, resource_host);
/* get the resource interfaces */
ret = iotcon_client_get_interfaces(resource, &resource_interfaces);
return;
}
if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
- DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_LINK & resource_interfaces)
- DBG("[%s] resource interface : LINK_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_BATCH & resource_interfaces)
- DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
if (IOTCON_INTERFACE_GROUP & resource_interfaces)
- DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri);
+ DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
/* get the resource types */
ret = iotcon_client_get_types(resource, &resource_types);
ERR("iotcon_client_get_types() Fail(%d)", ret);
return;
}
- iotcon_resource_types_foreach(resource_types, _get_res_type_fn, (void*)resource_uri);
+ iotcon_resource_types_foreach(resource_types, _get_res_type_fn, resource_uri_path);
- if (TEST_STR_EQUAL == strcmp(room_uri, resource_uri)) {
+ if (TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
/* copy resource to use elsewhere */
room_resource = iotcon_client_clone(resource);
/* create a room Representation */
room_repr = iotcon_repr_new();
- iotcon_repr_set_uri(room_repr, "/a/room");
+ iotcon_repr_set_uri_path(room_repr, "/a/room");
iotcon_repr_set_str(room_repr, "name", "Michael's Room");
/* set null */
/* create a light Representation */
light_repr = iotcon_repr_new();
- iotcon_repr_set_uri(light_repr, "/a/light");
+ iotcon_repr_set_uri_path(light_repr, "/a/light");
iotcon_repr_set_int(light_repr, "brightness", 50);
iotcon_repr_append_child(room_repr, light_repr);
iotcon_repr_free(light_repr);
/* create a switch Representation */
switch_repr = iotcon_repr_new();
- iotcon_repr_set_uri(switch_repr, "/a/switch");
+ iotcon_repr_set_uri_path(switch_repr, "/a/switch");
iotcon_repr_set_bool(switch_repr, "switch", false);
iotcon_repr_append_child(room_repr, switch_repr);
iotcon_repr_free(switch_repr);
_send_response(response, room_repr, interface);
iotcon_repr_free(room_repr);
-
- FN_END;
}
static void _request_handler_put(iotcon_request_h request, iotcon_response_h response)
if (NULL == response) {
ERR("iotcon_response_new() Fail");
return;
- }
+ }
if (IOTCON_REQUEST_GET & types)
_light_request_handler_get(response);
if (NULL == response) {
ERR("iotcon_response_new() Fail");
return;
- }
+ }
if (IOTCON_REQUEST_GET & types)
_room_request_handler_get(request, response);
} \
} while (0)
-#define WARN_IF(expr) \
+#define WARN_IF(expr, fmt, arg...) \
do { \
if (expr) { \
- WARN("(%s)", #expr); \
+ WARN(fmt, ##arg); \
} \
} while (0)