From 6e3104571eb85ffb7d622e9f7d27a7084131017a Mon Sep 17 00:00:00 2001 From: youngman Date: Wed, 15 Jul 2015 14:53:51 +0900 Subject: [PATCH] Modify callback parameter Change-Id: I35f4f67d7248ba088213c904926fa0a46336a1d0 Signed-off-by: youngman --- daemon/icd-ioty.c | 4 +- daemon/icd-ioty.h | 6 +- lib/icl-client-crud.c | 624 +++++++++++++++++++ lib/icl-client.c | 243 ++++---- lib/icl-client.h | 4 +- lib/icl-dbus.c | 1320 ++--------------------------------------- lib/icl-dbus.h | 65 +- lib/icl-device.c | 258 +++++++- lib/icl-ioty.h | 3 +- lib/icl-presence.c | 209 +++++++ lib/icl-response.c | 23 +- lib/icl.c | 416 +++++++++---- lib/include/iotcon-constant.h | 2 +- lib/include/iotcon-struct.h | 2 +- lib/include/iotcon.h | 23 +- test/crud-test-client.c | 52 +- test/crud-test-server.c | 68 +-- test/repr-test-client.c | 15 +- test/repr-test-server.c | 15 +- 19 files changed, 1670 insertions(+), 1682 deletions(-) create mode 100644 lib/icl-client-crud.c create mode 100644 lib/icl-presence.c diff --git a/daemon/icd-ioty.c b/daemon/icd-ioty.c index c9b7cbc..f067b8b 100644 --- a/daemon/icd-ioty.c +++ b/daemon/icd-ioty.c @@ -633,7 +633,7 @@ int icd_ioty_get_platform_info(const char *host_address, unsigned int signal_num } -iotcon_presence_h icd_ioty_subscribe_presence(const char *host_address, +OCDoHandle icd_ioty_subscribe_presence(const char *host_address, const char *resource_type, unsigned int signal_number, const char *sender) { // TODO : To be implemented @@ -641,7 +641,7 @@ iotcon_presence_h icd_ioty_subscribe_presence(const char *host_address, } -int icd_ioty_unsubscribe_presence(iotcon_presence_h presence_handle) +int icd_ioty_unsubscribe_presence(OCDoHandle presence_handle) { // TODO : To be implemented return IOTCON_ERROR_NONE; diff --git a/daemon/icd-ioty.h b/daemon/icd-ioty.h index 19e0270..a8e5caa 100644 --- a/daemon/icd-ioty.h +++ b/daemon/icd-ioty.h @@ -89,10 +89,10 @@ int icd_ioty_register_platform_info(GVariant *value); int icd_ioty_get_platform_info(const char *host_address, unsigned int signal_number, const char *sender); -iotcon_presence_h icd_ioty_subscribe_presence(const char *host_address, - const char *resource_type, unsigned int signal_number, const char *sender); +void* icd_ioty_subscribe_presence(const char *host_address, const char *resource_type, + unsigned int signal_number, const char *sender); -int icd_ioty_unsubscribe_presence(iotcon_presence_h presence_handle); +int icd_ioty_unsubscribe_presence(void *presence_handle); int icd_ioty_start_presence(unsigned int time_to_live); diff --git a/lib/icl-client-crud.c b/lib/icl-client-crud.c new file mode 100644 index 0000000..f28baa4 --- /dev/null +++ b/lib/icl-client-crud.c @@ -0,0 +1,624 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "iotcon.h" +#include "ic-utils.h" +#include "icl.h" +#include "icl-options.h" +#include "icl-dbus.h" +#include "icl-dbus-type.h" +#include "icl-repr.h" +#include "icl-client.h" + +typedef struct { + iotcon_on_cru_cb cb; + void *user_data; + iotcon_client_h resource; + unsigned int id; +} icl_on_cru_s; + +typedef struct { + iotcon_on_delete_cb cb; + void *user_data; + iotcon_client_h resource; + unsigned int id; +} icl_on_delete_s; + +typedef struct { + iotcon_on_observe_cb cb; + void *user_data; + iotcon_client_h resource; +} icl_on_observe_s; + + +static void _icl_on_cru_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + int res; + GVariantIter *options; + unsigned short option_id; + char *option_data; + iotcon_options_h header_options = NULL; + iotcon_repr_h repr = NULL; + char *repr_json = NULL; + + icl_on_cru_s *cb_container = user_data; + iotcon_on_cru_cb cb = cb_container->cb; + + icl_dbus_unsubscribe_signal(cb_container->id); + + g_variant_get(parameters, "(a(qs)si)", &options, &repr_json, &res); + + 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); + + 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"); + if (header_options) + iotcon_options_free(header_options); + return; + } + } + + res = icl_dbus_convert_daemon_error(res); + + if (cb) + cb(cb_container->resource, repr, header_options, res, cb_container->user_data); + + if (repr) + iotcon_repr_free(repr); + if (header_options) + iotcon_options_free(header_options); + +} + + +static void _icl_cru_conn_cleanup(icl_on_cru_s *cb_container) +{ + iotcon_client_free(cb_container->resource); + free(cb_container); +} + + +API int iotcon_get(iotcon_client_h resource, iotcon_query_h query, + iotcon_on_cru_cb cb, void *user_data) +{ + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_on_cru_s *cb_container; + GVariant *arg_client; + GVariant *arg_query; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + signal_number = icl_dbus_generate_signal_number(); + + arg_client = icl_dbus_client_to_gvariant(resource); + arg_query = icl_dbus_query_to_gvariant(query); + + ic_dbus_call_get_sync(icl_dbus_get_object(), arg_client, arg_query, signal_number, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_get_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_query); + g_variant_unref(arg_client); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_GET, + signal_number); + + cb_container = calloc(1, sizeof(icl_on_cru_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->resource = iotcon_client_ref(resource); + if (NULL == cb_container->resource) { + ERR("iotcon_client_ref() Fail"); + free(cb_container); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, _icl_cru_conn_cleanup, + _icl_on_cru_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + _icl_cru_conn_cleanup(cb_container); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; + + return ret; +} + + +API int iotcon_put(iotcon_client_h resource, iotcon_repr_h repr, + iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) +{ + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + char *arg_repr; + icl_on_cru_s *cb_container; + GVariant *arg_client; + GVariant *arg_query; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + signal_number = icl_dbus_generate_signal_number(); + + arg_repr = icl_repr_generate_json(repr, FALSE); + if (NULL == arg_repr) { + ERR("icl_repr_generate_json() Fail"); + return IOTCON_ERROR_REPRESENTATION; + } + + arg_client = icl_dbus_client_to_gvariant(resource); + arg_query = icl_dbus_query_to_gvariant(query); + + ic_dbus_call_put_sync(icl_dbus_get_object(), arg_client, arg_repr, arg_query, + signal_number, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_put_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_query); + g_variant_unref(arg_client); + free(arg_repr); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + free(arg_repr); + return icl_dbus_convert_daemon_error(ret); + } + free(arg_repr); + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PUT, + signal_number); + + cb_container = calloc(1, sizeof(icl_on_cru_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->resource = iotcon_client_ref(resource); + if (NULL == cb_container->resource) { + ERR("iotcon_client_ref() Fail"); + free(cb_container); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, _icl_cru_conn_cleanup, + _icl_on_cru_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + _icl_cru_conn_cleanup(cb_container); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; + + return ret; +} + + +API int iotcon_post(iotcon_client_h resource, iotcon_repr_h repr, + iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) +{ + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + char *arg_repr; + icl_on_cru_s *cb_container; + GVariant *arg_client; + GVariant *arg_query; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + signal_number = icl_dbus_generate_signal_number(); + + arg_repr = icl_repr_generate_json(repr, FALSE); + if (NULL == arg_repr) { + ERR("icl_repr_generate_json() Fail"); + return IOTCON_ERROR_REPRESENTATION; + } + + arg_client = icl_dbus_client_to_gvariant(resource); + arg_query = icl_dbus_query_to_gvariant(query); + + ic_dbus_call_post_sync(icl_dbus_get_object(), arg_client, arg_repr, arg_query, + signal_number, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_post_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_client); + g_variant_unref(arg_query); + free(arg_repr); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + free(arg_repr); + return icl_dbus_convert_daemon_error(ret); + } + free(arg_repr); + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_POST, + signal_number); + + cb_container = calloc(1, sizeof(icl_on_cru_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->resource = iotcon_client_ref(resource); + if (NULL == cb_container->resource) { + ERR("iotcon_client_ref() Fail"); + free(cb_container); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, _icl_cru_conn_cleanup, + _icl_on_cru_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + _icl_cru_conn_cleanup(cb_container); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; + + return ret; +} + + +static void _icl_on_delete_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + GVariantIter *options; + unsigned short option_id; + char *option_data; + iotcon_options_h header_options = NULL; + int res; + + icl_on_delete_s *cb_container = user_data; + iotcon_on_delete_cb cb = cb_container->cb; + + g_variant_get(parameters, "(a(qs)i)", &options, &res); + + if (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); + + if (cb) + cb(cb_container->resource, header_options, res, cb_container->user_data); + + if (header_options) + iotcon_options_free(header_options); + + icl_dbus_unsubscribe_signal(cb_container->id); +} + + +static void _icl_delete_conn_cleanup(icl_on_delete_s *cb_container) +{ + iotcon_client_free(cb_container->resource); + free(cb_container); +} + + +API int iotcon_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, void *user_data) +{ + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_on_delete_s *cb_container; + GVariant *arg_client; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + signal_number = icl_dbus_generate_signal_number(); + + arg_client = icl_dbus_client_to_gvariant(resource); + + ic_dbus_call_delete_sync(icl_dbus_get_object(), arg_client, signal_number, &ret, + NULL, &error); + if (error) { + ERR("ic_dbus_call_delete_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_client); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DELETE, + signal_number); + + cb_container = calloc(1, sizeof(icl_on_delete_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->resource = iotcon_client_ref(resource); + if (NULL == cb_container->resource) { + ERR("iotcon_client_ref() Fail"); + free(cb_container); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, _icl_delete_conn_cleanup, + _icl_on_delete_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + _icl_delete_conn_cleanup(cb_container); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; + + return ret; +} + + +static void _icl_on_observe_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + FN_CALL; + int index; + GVariantIter *options; + unsigned short option_id; + char *option_data; + iotcon_options_h header_options = NULL; + iotcon_repr_h repr = NULL; + GVariantIter *repr_iter; + char *repr_json; + char *repr_uri_path; + int res; + int seq_num; + + icl_on_observe_s *cb_container = user_data; + iotcon_on_observe_cb cb = cb_container->cb; + + g_variant_get(parameters, "(a(qs)asii)", &options, &repr_iter, &res, &seq_num); + + if (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); + + for (index = 0; g_variant_iter_loop(repr_iter, "&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(repr_iter); + return; + } + 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; + else + repr->children = g_list_append(repr->children, cur_repr); + } + g_variant_iter_free(repr_iter); + + if (cb) + cb(cb_container->resource, repr, header_options, res, seq_num, + cb_container->user_data); + + if (repr) + iotcon_repr_free(repr); + if (header_options) + iotcon_options_free(header_options); +} + + +static void _icl_observe_conn_cleanup(icl_on_observe_s *cb_container) +{ + cb_container->resource->observe_handle = 0; + cb_container->resource->observe_sub_id = 0; + iotcon_client_free(cb_container->resource); + free(cb_container); +} + + +API int iotcon_observer_start(iotcon_client_h resource, + iotcon_observe_type_e observe_type, + iotcon_query_h query, + iotcon_on_observe_cb cb, + void *user_data) +{ + int ret; + int observe_handle; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_on_observe_s *cb_container; + GVariant *arg_client; + GVariant *arg_query; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + signal_number = icl_dbus_generate_signal_number(); + + arg_client = icl_dbus_client_to_gvariant(resource); + arg_query = icl_dbus_query_to_gvariant(query); + + ic_dbus_call_observer_start_sync(icl_dbus_get_object(), arg_client, observe_type, + arg_query, signal_number, &observe_handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_observer_start_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_query); + g_variant_unref(arg_client); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_OBSERVE, + signal_number); + + cb_container = calloc(1, sizeof(icl_on_observe_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->resource = iotcon_client_ref(resource); + if (NULL == cb_container->resource) { + ERR("iotcon_client_ref() Fail"); + free(cb_container); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, _icl_observe_conn_cleanup, + _icl_on_observe_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + return IOTCON_ERROR_DBUS; + } + resource->observe_sub_id = sub_id; + resource->observe_handle = observe_handle; + + return ret; +} + + +API int iotcon_observer_stop(iotcon_client_h resource) +{ + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == resource->observe_handle) { + ERR("It doesn't have a observe_handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_observer_stop_sync(icl_dbus_get_object(), + resource->observe_handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_observer_stop_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + icl_dbus_unsubscribe_signal(resource->observe_sub_id); + + return ret; +} + diff --git a/lib/icl-client.c b/lib/icl-client.c index 6c7f7dd..61b6756 100644 --- a/lib/icl-client.c +++ b/lib/icl-client.c @@ -14,30 +14,85 @@ * limitations under the License. */ #include +#include #include #include #include #include #include -#include "iotcon-struct.h" +#include "iotcon.h" #include "ic-utils.h" #include "icl.h" -#include "icl-ioty.h" #include "icl-options.h" #include "icl-resource-types.h" #include "icl-dbus.h" #include "icl-repr.h" #include "icl-client.h" +typedef struct { + iotcon_found_resource_cb cb; + void *user_data; + unsigned int id; +} icl_found_resource_s; + + +static void _icl_found_resource_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + FN_CALL; + int conn_type; + JsonParser *parser; + iotcon_client_h client; + char *payload, *host; + icl_found_resource_s *cb_container = user_data; + iotcon_found_resource_cb cb = cb_container->cb; + + g_variant_get(parameters, "(&s&si)", &payload, &host, &conn_type); + + RET_IF(NULL == payload); + RET_IF(NULL == host); + + parser = json_parser_new(); + + 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; + } + + if (cb) + cb(client, cb_container->user_data); + + iotcon_client_free(client); + + g_object_unref(parser); + + /* TODO + * When is callback removed? + */ +} + + /* 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_address, const char *resource_type, iotcon_found_resource_cb cb, void *user_data) { - FN_CALL; int ret; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + unsigned int sub_id; + icl_found_resource_s *cb_container; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); 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))) { @@ -45,9 +100,41 @@ API int iotcon_find_resource(const char *host_address, const char *resource_type return IOTCON_ERROR_INVALID_PARAMETER; } - 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); + signal_number = icl_dbus_generate_signal_number(); + + ic_dbus_call_find_resource_sync(icl_dbus_get_object(), host_address, + ic_utils_dbus_encode_str(resource_type), signal_number, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_find_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_FOUND_RESOURCE, + signal_number); + + cb_container = calloc(1, sizeof(icl_found_resource_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free, + _icl_found_resource_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; return ret; } @@ -76,6 +163,8 @@ API iotcon_client_h iotcon_client_new(const char *host, const char *uri_path, resource->types = icl_resource_types_ref(resource_types); resource->ifaces = resource_ifs; + resource->ref_count = 1; + return resource; } @@ -86,38 +175,32 @@ API void iotcon_client_free(iotcon_client_h resource) RET_IF(NULL == resource); + resource->ref_count--; + + if (0 < resource->ref_count) + return; + free(resource->uri_path); free(resource->host); free(resource->sid); + iotcon_resource_types_free(resource->types); /* null COULD be allowed */ if (resource->header_options) iotcon_options_free(resource->header_options); - iotcon_resource_types_free(resource->types); + free(resource); } -API iotcon_client_h iotcon_client_clone(iotcon_client_h resource) +API iotcon_client_h iotcon_client_ref(iotcon_client_h resource) { - iotcon_client_h clone; - RETV_IF(NULL == resource, NULL); + RETV_IF(resource->ref_count <= 0, NULL); - clone = iotcon_client_new(resource->host, - resource->uri_path, - resource->is_observable, - iotcon_resource_types_clone(resource->types), - resource->ifaces); - if (NULL == clone) { - ERR("iotcon_client_new() Fail"); - return clone; - } + resource->ref_count++; - clone->sid = resource->sid; - clone->observe_handle = resource->observe_handle; - - return clone; + return resource; } @@ -144,6 +227,8 @@ API int iotcon_client_get_host(iotcon_client_h resource, char **host) return IOTCON_ERROR_NONE; } + +/* The content of the resource should not be freed by user. */ API int iotcon_client_get_server_id(iotcon_client_h resource, char **sid) { RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); @@ -206,117 +291,6 @@ API int iotcon_client_set_options(iotcon_client_h resource, } -API int iotcon_get(iotcon_client_h resource, iotcon_query_h query, - iotcon_on_cru_cb cb, void *user_data) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_get(resource, query, cb, user_data); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_get() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_put(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_put(resource, repr, query, cb, user_data); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_put() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_post(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_post(resource, repr, query, cb, user_data); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_post() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, void *user_data) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_delete(resource, cb, user_data); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_delete() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_observer_start(iotcon_client_h resource, - iotcon_observe_type_e observe_type, - iotcon_query_h query, - iotcon_on_observe_cb cb, - void *user_data) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_observer_start(resource, observe_type, query, cb, user_data); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_observer_start() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_observer_stop(iotcon_client_h resource) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - if (NULL == resource->observe_handle) { - ERR("It doesn't have a observe_handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ret = icl_dbus_observer_stop(resource->observe_handle); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_observer_stop() Fail(%d)", ret); - return ret; - } - resource->observe_handle = NULL; - - return ret; -} - - iotcon_client_h icl_client_parse_resource_object(JsonParser *parser, char *json_string, const char *host, iotcon_connectivity_type_e conn_type) { @@ -368,6 +342,7 @@ iotcon_client_h icl_client_parse_resource_object(JsonParser *parser, char *json_ ERR("iotcon_client_new() Fail"); return NULL; } + client->ref_count = 1; client->sid = strdup(server_id); if (NULL == client->sid) { diff --git a/lib/icl-client.h b/lib/icl-client.h index 015f312..7f5a59f 100644 --- a/lib/icl-client.h +++ b/lib/icl-client.h @@ -21,6 +21,7 @@ #include struct icl_remote_resource { + int ref_count; char *uri_path; char *host; char *sid; @@ -30,7 +31,8 @@ struct icl_remote_resource { iotcon_resource_types_h types; int ifaces; iotcon_connectivity_type_e conn_type; - icl_handle_container_s *observe_handle; + int observe_handle; + unsigned int observe_sub_id; }; iotcon_client_h icl_client_parse_resource_object(JsonParser *parser, char *json_string, diff --git a/lib/icl-dbus.c b/lib/icl-dbus.c index d0ea28b..f982fa9 100644 --- a/lib/icl-dbus.c +++ b/lib/icl-dbus.c @@ -20,20 +20,14 @@ #include "iotcon.h" #include "ic-common.h" -#include "ic-utils.h" #include "ic-dbus.h" #include "icl.h" -#include "icl-dbus-type.h" #include "icl-dbus.h" -#include "icl-client.h" -#include "icl-repr.h" -#include "icl-request.h" static GDBusConnection *icl_dbus_conn; static int icl_dbus_count; static icDbus *icl_dbus_object; static GList *icl_dbus_sub_ids; -static GList *icl_dbus_handle_containers; static GList *icl_dbus_conn_changed_cbs; typedef struct { @@ -43,1281 +37,79 @@ typedef struct { } icl_cb_container_s; -static inline unsigned int _icl_dbus_generate_signal_number() +icDbus* icl_dbus_get_object() { - static unsigned int i = 0; - - return i++; -} - - -static inline int _icl_dbus_get() -{ - if (NULL == icl_dbus_conn) { - icl_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL); - if (NULL == icl_dbus_conn) { - ERR("g_bus_get_sync() Fail"); - return IOTCON_ERROR_DBUS; - } - } - - icl_dbus_count++; - - return IOTCON_ERROR_NONE; -} - - -static inline void _icl_dbus_unref() -{ - icl_dbus_count--; - - if (0 == icl_dbus_count) { - DBG("All connection is closed"); - g_object_unref(icl_dbus_conn); - icl_dbus_conn = NULL; - } -} - - -static unsigned int _icl_dbus_subscribe_signal(char *sig_name, void *cb, void *user_data, - GDBusSignalCallback sig_handler) -{ - unsigned int id; - icl_cb_container_s *cb_container; - - cb_container = calloc(1, sizeof(icl_cb_container_s)); - if (NULL == cb_container) { - ERR("calloc() Fail(%d)", errno); - return 0; - } - cb_container->cb = cb; - cb_container->user_data = user_data; - - id = g_dbus_connection_signal_subscribe(icl_dbus_conn, - NULL, - IOTCON_DBUS_INTERFACE, - sig_name, - IOTCON_DBUS_OBJPATH, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - sig_handler, - cb_container, - free); - if (0 == id) { - ERR("g_dbus_connection_signal_subscribe() Fail"); - free(cb_container); - return id; - } - - cb_container->id = id; - - return id; -} - - -static void _icl_dbus_request_handler(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - FN_CALL; - - GVariantIter *options; - unsigned short option_id; - char *option_data; - GVariantIter *query; - char *key = NULL; - char *value = NULL; - char *repr_json; - int request_handle; - int resource_handle; - struct icl_resource_request request = {0}; - icl_cb_container_s *cb_container = user_data; - iotcon_request_handler_cb cb = cb_container->cb; - - g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)", - &request.types, - &options, - &query, - &request.observation_info.action, - &request.observation_info.observer_id, - &repr_json, - &request_handle, - &resource_handle); - - if (g_variant_iter_n_children(options)) { - request.header_options = iotcon_options_new(); - while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) - iotcon_options_insert(request.header_options, option_id, option_data); - } - g_variant_iter_free(options); - - if (g_variant_iter_n_children(query)) { - request.query = iotcon_query_new(); - while (g_variant_iter_loop(query, "(&s&s)", &key, &value)) - iotcon_query_insert(request.query, key, value); - } - g_variant_iter_free(query); - - request.request_handle = GINT_TO_POINTER(request_handle); - request.resource_handle = GINT_TO_POINTER(resource_handle); - - if (ic_utils_dbus_decode_str(repr_json)) { - request.repr = icl_repr_create_repr(repr_json); - if (NULL == request.repr) { - ERR("icl_repr_create_repr() Fail"); - return; - } - } - - /* TODO remove request.uri */ - request.uri_path = "temp_uri_path"; - - if (cb) - cb(&request, cb_container->user_data); - - /* To avoid unnecessary ERR log (repr could be NULL) */ - if (request.repr) - iotcon_repr_free(request.repr); - if (request.query) - iotcon_query_free(request.query); - if (request.header_options) - iotcon_options_free(request.header_options); -} - - -icl_handle_container_s* icl_dbus_register_resource(const char *uri_path, - iotcon_resource_types_h types, - int ifaces, - uint8_t properties, - iotcon_request_handler_cb cb, - void *user_data) -{ - int signal_number; - unsigned int sub_id; - int resource_handle; - GError *error = NULL; - const gchar **res_types; - char sig_name[IC_DBUS_SIGNAL_LENGTH]; - icl_handle_container_s *resource; - - RETV_IF(NULL == icl_dbus_object, NULL); - - signal_number = _icl_dbus_generate_signal_number(); - - resource = calloc(1, sizeof(icl_handle_container_s)); - if (NULL == resource) { - ERR("calloc() Fail(%d)", errno); - return NULL; - } - - res_types = icl_dbus_resource_types_to_array(types); - if (NULL == res_types) { - ERR("icl_dbus_resource_types_to_array() Fail"); - free(resource); - return NULL; - } - - 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); - g_error_free(error); - free(res_types); - free(resource); - return NULL; - } - free(res_types); - - snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(sig_name, cb, user_data, - _icl_dbus_request_handler); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - free(resource); - return NULL; - } - - resource->handle = resource_handle; - resource->id = sub_id; - - icl_dbus_handle_containers = g_list_append(icl_dbus_handle_containers, resource); - - return resource; -} - - -int icl_dbus_unregister_resource(icl_handle_container_s *resource) -{ - FN_CALL; - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - if (0 == resource->id) { - WARN("Invalid Resource handle"); - free(resource); - return IOTCON_ERROR_NONE; - } - - ic_dbus_call_unregister_resource_sync(icl_dbus_object, resource->handle, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, resource->id); - icl_dbus_handle_containers = g_list_remove(icl_dbus_handle_containers, resource); - free(resource); - - return ret; -} - - -int icl_dbus_bind_interface(icl_handle_container_s *resource, int iface) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == resource->id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_bind_interface_sync(icl_dbus_object, resource->handle, iface, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -int icl_dbus_bind_type(icl_handle_container_s *resource, const char *type) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == resource->id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_bind_type_sync(icl_dbus_object, resource->handle, type, &ret, NULL, - &error); - if (error) { - ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -int icl_dbus_bind_resource(icl_handle_container_s *parent, icl_handle_container_s *child) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == parent->id) { - ERR("Invalid Resource handle(parent)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - if (0 == child->id) { - ERR("Invalid Resource handle(child)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_bind_resource_sync(icl_dbus_object, parent->handle, child->handle, - &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -int icl_dbus_unbind_resource(icl_handle_container_s *parent, - icl_handle_container_s *child) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == parent->id) { - ERR("Invalid Resource handle(parent)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - if (0 == child->id) { - ERR("Invalid Resource handle(child)"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_unbind_resource_sync(icl_dbus_object, parent->handle, child->handle, - &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -int icl_dbus_notify_list_of_observers(icl_handle_container_s *resource, - struct icl_notify_msg *msg, iotcon_observers_h observers) -{ - int ret; - GError *error = NULL; - GVariant *noti_msg; - GVariant *obs; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == resource->id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - noti_msg = icl_dbus_notimsg_to_gvariant(msg); - if (NULL == noti_msg) { - ERR("icl_dbus_notimsg_to_gvariant() Fail"); - return IOTCON_ERROR_REPRESENTATION; - } - obs = icl_dbus_observers_to_gvariant(observers); - - ic_dbus_call_notify_list_of_observers_sync(icl_dbus_object, resource->handle, - noti_msg, obs, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(obs); - g_variant_unref(noti_msg); - return IOTCON_ERROR_DBUS; - } - - return IOTCON_ERROR_NONE; -} - - -int icl_dbus_notify_all(icl_handle_container_s *resource) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == resource->id) { - ERR("Invalid Resource handle"); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - ic_dbus_call_notify_all_sync(icl_dbus_object, resource->handle, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - return IOTCON_ERROR_NONE; -} - - -int icl_dbus_send_response(struct icl_resource_response *response) -{ - int ret; - GError *error = NULL; - GVariant *arg_response; - - 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); - if (error) { - ERR("ic_dbus_call_send_response_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_response); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -static void _icl_dbus_found_resource(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - FN_CALL; - 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; - - g_variant_get(parameters, "(&s&si)", &payload, &host, &conn_type); - - RET_IF(NULL == payload); - RET_IF(NULL == host); - - parser = json_parser_new(); - - 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; - } - - if (cb) - cb(client, cb_container->user_data); - - iotcon_client_free(client); - - g_object_unref(parser); -} - - -int icl_dbus_find_resource(const char *host_address, const char *resource_type, - iotcon_found_resource_cb cb, void *user_data) -{ - int ret; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - ic_dbus_call_find_resource_sync(icl_dbus_object, host_address, - ic_utils_dbus_encode_str(resource_type), signal_number, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_find_resource_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_FOUND_RESOURCE, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, - _icl_dbus_found_resource); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - return ret; -} - - -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, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - FN_CALL; - int res; - GVariantIter *options; - unsigned short option_id; - char *option_data; - iotcon_options_h header_options = NULL; - iotcon_repr_h repr = NULL; - char *repr_json = NULL; - - icl_cb_container_s *cb_container = user_data; - iotcon_on_cru_cb cb = cb_container->cb; - - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, cb_container->id); - icl_dbus_sub_ids = g_list_remove(icl_dbus_sub_ids, - GUINT_TO_POINTER(cb_container->id)); - - g_variant_get(parameters, "(a(qs)si)", &options, &repr_json, &res); - - 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); - } - - 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; - } - } - - res = _icl_dbus_convert_daemon_error(res); - - if (cb) - cb(repr, header_options, res, cb_container->user_data); - - if (repr) - iotcon_repr_free(repr); - if (header_options) - iotcon_options_free(header_options); -} - - -int icl_dbus_get(iotcon_client_h resource, iotcon_query_h query, - iotcon_on_cru_cb cb, void *user_data) -{ - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - GVariant *arg_client; - GVariant *arg_query; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - arg_client = icl_dbus_client_to_gvariant(resource); - arg_query = icl_dbus_query_to_gvariant(query); - ic_dbus_call_get_sync(icl_dbus_object, arg_client, arg_query, signal_number, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_get_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_query); - g_variant_unref(arg_client); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_GET, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, _icl_dbus_on_cru); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - return ret; -} - - -int icl_dbus_put(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) -{ - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - char *arg_repr; - GVariant *arg_client; - GVariant *arg_query; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - arg_repr = icl_repr_generate_json(repr, FALSE); - if (NULL == arg_repr) { - ERR("icl_repr_generate_json() Fail"); - return IOTCON_ERROR_REPRESENTATION; - } - - arg_client = icl_dbus_client_to_gvariant(resource); - arg_query = icl_dbus_query_to_gvariant(query); - - ic_dbus_call_put_sync(icl_dbus_object, arg_client, arg_repr, arg_query, - signal_number, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_put_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_query); - g_variant_unref(arg_client); - free(arg_repr); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PUT, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, _icl_dbus_on_cru); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - free(arg_repr); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - free(arg_repr); - - return ret; -} - - -int icl_dbus_post(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) -{ - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - char *arg_repr; - GVariant *arg_client; - GVariant *arg_query; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - arg_repr = icl_repr_generate_json(repr, FALSE); - if (NULL == arg_repr) { - ERR("icl_repr_generate_json() Fail"); - return IOTCON_ERROR_REPRESENTATION; - } - - arg_client = icl_dbus_client_to_gvariant(resource); - arg_query = icl_dbus_query_to_gvariant(query); - - ic_dbus_call_post_sync(icl_dbus_object, arg_client, arg_repr, arg_query, - signal_number, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_post_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_client); - g_variant_unref(arg_query); - free(arg_repr); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_POST, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, _icl_dbus_on_cru); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - free(arg_repr); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - free(arg_repr); - - return ret; -} - - -static void _icl_dbus_on_delete(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - GVariantIter *options; - unsigned short option_id; - char *option_data; - iotcon_options_h header_options = NULL; - int res; - - icl_cb_container_s *cb_container = user_data; - iotcon_on_delete_cb cb = cb_container->cb; - - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, cb_container->id); - icl_dbus_sub_ids = g_list_remove(icl_dbus_sub_ids, - GUINT_TO_POINTER(cb_container->id)); - - g_variant_get(parameters, "(a(qs)i)", &options, &res); - - if (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); - - if (cb) - cb(header_options, res, cb_container->user_data); - - if (header_options) - iotcon_options_free(header_options); + return icl_dbus_object; } -int icl_dbus_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, - void *user_data) +inline unsigned int icl_dbus_generate_signal_number() { - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - GVariant *arg_client; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - arg_client = icl_dbus_client_to_gvariant(resource); - - ic_dbus_call_delete_sync(icl_dbus_object, arg_client, signal_number, &ret, NULL, - &error); - if (error) { - ERR("ic_dbus_call_delete_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_client); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DELETE, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, _icl_dbus_on_delete); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); + static unsigned int i = 0; - return ret; + return i++; } -static void _icl_dbus_on_observe(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) +static inline int _icl_dbus_get() { - FN_CALL; - int index; - 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_path; - int res; - int seq_num; - - icl_cb_container_s *cb_container = user_data; - iotcon_on_observe_cb cb = cb_container->cb; - - g_variant_get(parameters, "(a(qs)asii)", &options, &reprIter, &res, &seq_num); - - if (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); - - 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); - return; + if (NULL == icl_dbus_conn) { + icl_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL); + if (NULL == icl_dbus_conn) { + ERR("g_bus_get_sync() Fail"); + return IOTCON_ERROR_DBUS; } - 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; - else - repr->children = g_list_append(repr->children, cur_repr); - } - g_variant_iter_free(reprIter); - - if (cb) - cb(header_options, repr, res, seq_num, cb_container->user_data); - - if (repr) - iotcon_repr_free(repr); - if (header_options) - iotcon_options_free(header_options); -} - - -int icl_dbus_observer_start(iotcon_client_h resource, - iotcon_observe_type_e observe_type, - iotcon_query_h query, - iotcon_on_observe_cb cb, - void *user_data) -{ - int ret; - int observe_handle; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - GVariant *arg_client; - GVariant *arg_query; - icl_handle_container_s *observe; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - observe = calloc(1, sizeof(icl_handle_container_s)); - if (NULL == observe) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - arg_client = icl_dbus_client_to_gvariant(resource); - arg_query = icl_dbus_query_to_gvariant(query); - - ic_dbus_call_observer_start_sync(icl_dbus_object, arg_client, observe_type, - arg_query, signal_number, &observe_handle, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_observer_start_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_query); - g_variant_unref(arg_client); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_OBSERVE, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, _icl_dbus_on_observe); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; - } - - observe->handle = observe_handle; - observe->id = sub_id; - resource->observe_handle = observe; - - icl_dbus_handle_containers = g_list_append(icl_dbus_handle_containers, observe); - - return ret; -} - - -int icl_dbus_observer_stop(icl_handle_container_s *observe) -{ - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == observe->id) { - WARN("Invalid Client handle"); - free(observe); - return IOTCON_ERROR_NONE; - } - - ic_dbus_call_observer_stop_sync(icl_dbus_object, observe->handle, &ret, NULL, - &error); - if (error) { - ERR("ic_dbus_call_observer_stop_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - if (IOTCON_ERROR_NONE != ret) - return ret; - - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, observe->id); - icl_dbus_handle_containers = g_list_remove(icl_dbus_handle_containers, observe); - free(observe); - - return ret; -} - -#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */ -int icl_dbus_register_device_info(iotcon_device_info_s info) -{ - int ret; - GError *error = NULL; - GVariant *arg_info; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - arg_info = icl_dbus_device_info_to_gvariant(&info); - ic_dbus_call_register_device_info_sync(icl_dbus_object, arg_info, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_register_device_info_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_info); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -static void _icl_dbus_received_device_info(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - icl_cb_container_s *cb_container = user_data; - iotcon_device_info_cb cb = cb_container->cb; - - iotcon_device_info_s info = {0}; - - g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s&s)", - &info.name, - &info.host_name, - &info.uuid, - &info.content_type, - &info.version, - &info.manuf_name, - &info.manuf_url, - &info.model_number, - &info.date_of_manufacture, - &info.platform_ver, - &info.firmware_ver, - &info.support_url); - - info.name = ic_utils_dbus_decode_str(info.name); - info.host_name = ic_utils_dbus_decode_str(info.host_name); - info.uuid = ic_utils_dbus_decode_str(info.uuid); - info.content_type = ic_utils_dbus_decode_str(info.content_type); - info.version = ic_utils_dbus_decode_str(info.version); - info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name); - info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url); - info.model_number = ic_utils_dbus_decode_str(info.model_number); - info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture); - info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver); - info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver); - info.support_url = ic_utils_dbus_decode_str(info.support_url); - - if (cb) - cb(info, cb_container->user_data); -} - - -int icl_dbus_get_device_info(const char *host_address, iotcon_device_info_cb cb, - void *user_data) -{ - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - ic_dbus_call_get_device_info_sync(icl_dbus_object, host_address, - signal_number, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DEVICE, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, - _icl_dbus_received_device_info); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; - } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - return ret; -} -#endif - -int icl_dbus_register_platform_info(iotcon_platform_info_s info) -{ - int ret; - GError *error = NULL; - GVariant *arg_info; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - arg_info = icl_dbus_platform_info_to_gvariant(&info); - ic_dbus_call_register_platform_info_sync(icl_dbus_object, arg_info, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_register_platform_info_sync() Fail(%s)", error->message); - g_error_free(error); - g_variant_unref(arg_info); - return IOTCON_ERROR_DBUS; - } - - return ret; -} - - -static void _icl_dbus_received_platform_info(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - icl_cb_container_s *cb_container = user_data; - iotcon_platform_info_cb cb = cb_container->cb; - - iotcon_platform_info_s info = {0}; - - g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s)", - &info.platform_id, - &info.manuf_name, - &info.manuf_url, - &info.model_number, - &info.date_of_manufacture, - &info.platform_ver, - &info.os_ver, - &info.hardware_ver, - &info.firmware_ver, - &info.support_url, - &info.system_time); - - info.platform_id = ic_utils_dbus_decode_str(info.platform_id); - info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name); - info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url); - info.model_number = ic_utils_dbus_decode_str(info.model_number); - info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture); - info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver); - info.os_ver = ic_utils_dbus_decode_str(info.os_ver); - info.hardware_ver = ic_utils_dbus_decode_str(info.hardware_ver); - info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver); - info.support_url = ic_utils_dbus_decode_str(info.support_url); - info.system_time = ic_utils_dbus_decode_str(info.system_time); - - if (cb) - cb(info, cb_container->user_data); -} - - -int icl_dbus_get_platform_info(const char *host_address, iotcon_platform_info_cb cb, - void *user_data) -{ - int ret; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - signal_number = _icl_dbus_generate_signal_number(); - - ic_dbus_call_get_platform_info_sync(icl_dbus_object, host_address, - signal_number, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PLATFORM, - signal_number); - - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, - _icl_dbus_received_platform_info); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - return IOTCON_ERROR_DBUS; } - icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(sub_id)); - - return ret; -} - - - -int icl_dbus_start_presence(unsigned int time_to_live) -{ - int ret; - GError *error = NULL; - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - - ic_dbus_call_start_presence_sync(icl_dbus_object, time_to_live, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_start_presence_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } + icl_dbus_count++; - return ret; + return IOTCON_ERROR_NONE; } -int icl_dbus_stop_presence() +static inline void _icl_dbus_unref() { - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); + icl_dbus_count--; - ic_dbus_call_stop_presence_sync(icl_dbus_object, &ret, NULL, &error); - if (error) { - ERR("ic_dbus_call_stop_presence_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; + if (0 == icl_dbus_count) { + DBG("All connection is closed"); + g_object_unref(icl_dbus_conn); + icl_dbus_conn = NULL; } - - return ret; -} - - -static void _icl_dbus_presence_handler(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - FN_CALL; - int res; - unsigned int nonce; - char *host_address; - icl_cb_container_s *cb_container = user_data; - iotcon_presence_cb cb = cb_container->cb; - - g_variant_get(parameters, "(iu&s)", &res, &nonce, &host_address); - - if (cb) - cb(res, nonce, host_address, cb_container->user_data); } -icl_handle_container_s* icl_dbus_subscribe_presence(const char *host_address, - const char *type, iotcon_presence_cb cb, void *user_data) +unsigned int icl_dbus_subscribe_signal(char *sig_name, void *cb_container, void *cb_free, + GDBusSignalCallback sig_handler) { - int presence_handle; - GError *error = NULL; - unsigned int sub_id; - int signal_number; - char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; - icl_handle_container_s *presence; - - RETV_IF(NULL == icl_dbus_object, NULL); - - signal_number = _icl_dbus_generate_signal_number(); - - presence = calloc(1, sizeof(icl_handle_container_s)); - if (NULL == presence) { - ERR("calloc() Fail(%d)", errno); - return NULL; - } - - ic_dbus_call_subscribe_presence_sync(icl_dbus_object, host_address, type, - signal_number, &presence_handle, NULL, &error); - if (error) { - ERR("ic_dbus_call_subscribe_presence_sync() Fail(%s)", error->message); - g_error_free(error); - free(presence); - return NULL; - } - - snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PRESENCE, - signal_number); + unsigned int id; - sub_id = _icl_dbus_subscribe_signal(signal_name, cb, user_data, - _icl_dbus_presence_handler); - if (0 == sub_id) { - ERR("_icl_dbus_subscribe_signal() Fail"); - free(presence); - return NULL; + id = g_dbus_connection_signal_subscribe(icl_dbus_conn, + NULL, + IOTCON_DBUS_INTERFACE, + sig_name, + IOTCON_DBUS_OBJPATH, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + sig_handler, + cb_container, + cb_free); + if (0 == id) { + ERR("g_dbus_connection_signal_subscribe() Fail"); + return id; } - presence->handle = presence_handle; - presence->id = sub_id; - - icl_dbus_handle_containers = g_list_append(icl_dbus_handle_containers, presence); + icl_dbus_sub_ids = g_list_append(icl_dbus_sub_ids, GUINT_TO_POINTER(id)); - return presence; + return id; } -int icl_dbus_unsubscribe_presence(icl_handle_container_s *presence) +void icl_dbus_unsubscribe_signal(unsigned int id) { - int ret; - GError *error = NULL; - - RETV_IF(NULL == icl_dbus_object, IOTCON_ERROR_DBUS); - if (0 == presence->id) { - WARN("Invalid Presence handle"); - free(presence); - return IOTCON_ERROR_NONE; - } - - ic_dbus_call_unsubscribe_presence_sync(icl_dbus_object, presence->handle, &ret, - NULL, &error); - if (error) { - ERR("ic_dbus_call_unsubscribe_presence_sync() Fail(%s)", error->message); - g_error_free(error); - return IOTCON_ERROR_DBUS; - } - - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, presence->id); - icl_dbus_handle_containers = g_list_remove(icl_dbus_handle_containers, presence); - free(presence); + g_dbus_connection_signal_unsubscribe(icl_dbus_conn, id); - return ret; + icl_dbus_sub_ids = g_list_remove(icl_dbus_sub_ids, GUINT_TO_POINTER(id)); } @@ -1414,30 +206,11 @@ static void _icl_dbus_sub_id_list_free(gpointer data) } -static void _icl_dbus_handle_container_unsubscribe_signal() -{ - GList *node; - - for (node = icl_dbus_handle_containers; node; node = node->next) { - icl_handle_container_s *handle_container = node->data; - g_dbus_connection_signal_unsubscribe(icl_dbus_conn, handle_container->id); - handle_container->id = 0; - } -} - - /* Unsubscribe all signals */ static void _icl_dbus_cleanup() { g_list_free_full(icl_dbus_sub_ids, _icl_dbus_sub_id_list_free); icl_dbus_sub_ids = NULL; - - /* It makes subscription id be 0. - * Handle containers should be freed. */ - _icl_dbus_handle_container_unsubscribe_signal(); - - g_list_free(icl_dbus_handle_containers); - icl_dbus_handle_containers = NULL; } @@ -1454,6 +227,19 @@ static void _icl_dbus_name_owner_notify(GObject *object, GParamSpec *pspec, } +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; +} + + unsigned int icl_dbus_start() { int ret; diff --git a/lib/icl-dbus.h b/lib/icl-dbus.h index 0791fd7..20f52c4 100644 --- a/lib/icl-dbus.h +++ b/lib/icl-dbus.h @@ -16,70 +16,21 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_DBUS_H__ #define __IOT_CONNECTIVITY_MANAGER_DBUS_H__ -typedef struct { - int handle; - unsigned int id; -} icl_handle_container_s; +#include "ic-dbus.h" -int icl_dbus_config(const char *address, unsigned short port); -icl_handle_container_s* icl_dbus_register_resource(const char *uri_path, - iotcon_resource_types_h types, - int ifaces, - uint8_t properties, - iotcon_request_handler_cb cb, - void *user_data); -int icl_dbus_unregister_resource(icl_handle_container_s *resource); - -int icl_dbus_bind_interface(icl_handle_container_s *resource, int iface); -int icl_dbus_bind_type(icl_handle_container_s *resource, const char *type); -int icl_dbus_bind_resource(icl_handle_container_s *parent, - icl_handle_container_s *child); -int icl_dbus_unbind_resource(icl_handle_container_s *parent, - icl_handle_container_s *child); - -int icl_dbus_notify_list_of_observers(icl_handle_container_s *resource, - struct icl_notify_msg *msg, iotcon_observers_h observers); -int icl_dbus_notify_all(icl_handle_container_s *resource); -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 cb, void *user_data); - -int icl_dbus_get(iotcon_client_h resource, iotcon_query_h query, - iotcon_on_cru_cb cb, void *user_data); -int icl_dbus_put(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data); -int icl_dbus_post(iotcon_client_h resource, iotcon_repr_h repr, - iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data); -int icl_dbus_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, - void *user_data); -int icl_dbus_observer_start(iotcon_client_h resource, - iotcon_observe_type_e observe_type, - iotcon_query_h query, - iotcon_on_observe_cb cb, - void *user_data); -int icl_dbus_observer_stop(icl_handle_container_s *observe); - -#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */ -int icl_dbus_register_device_info(iotcon_device_info_s info); -int icl_dbus_get_device_info(const char *host_address, iotcon_device_info_cb cb, - void *user_data); -#endif +icDbus* icl_dbus_get_object(); +unsigned int icl_dbus_generate_signal_number(); -int icl_dbus_register_platform_info(iotcon_platform_info_s info); -int icl_dbus_get_platform_info(const char *host_address, iotcon_platform_info_cb cb, - void *user_data); - -int icl_dbus_start_presence(unsigned int time_to_live); -int icl_dbus_stop_presence(); -icl_handle_container_s* icl_dbus_subscribe_presence(const char *host_address, - const char *type, iotcon_presence_cb cb, void *user_data); -int icl_dbus_unsubscribe_presence(icl_handle_container_s *presence); +unsigned int icl_dbus_subscribe_signal(char *sig_name, void *cb_container, void *cb_free, + GDBusSignalCallback sig_handler); +void icl_dbus_unsubscribe_signal(unsigned int id); int icl_dbus_add_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data); int icl_dbus_remove_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data); +int icl_dbus_convert_daemon_error(int error); + unsigned int icl_dbus_start(); void icl_dbus_stop(); diff --git a/lib/icl-device.c b/lib/icl-device.c index b128075..3694f94 100644 --- a/lib/icl-device.c +++ b/lib/icl-device.c @@ -13,15 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include #include #include "iotcon.h" +#include "ic-utils.h" #include "icl.h" -#include "icl-ioty.h" #include "icl-dbus.h" +#include "icl-dbus-type.h" + +#ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */ +typedef struct { + iotcon_device_info_cb cb; + void *user_data; + unsigned int id; +} icl_device_info_s; +#endif + +typedef struct { + iotcon_platform_info_cb cb; + void *user_data; + unsigned int id; +} icl_platform_info_s; + #ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */ /* The length of manufacturer_name should be less than and equal to 16. @@ -29,6 +46,10 @@ int iotcon_register_device_info(iotcon_device_info_s device_info) { int ret; + GError *error = NULL; + GVariant *arg_info; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); if (device_info.manuf_name && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(device_info.manuf_name))) { @@ -42,55 +63,258 @@ int iotcon_register_device_info(iotcon_device_info_s device_info) return IOTCON_ERROR_INVALID_PARAMETER; } - ret = icl_dbus_register_device_info(device_info); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_register_device_info() Fail(%d)", ret); + arg_info = icl_dbus_device_info_to_gvariant(&device_info); + ic_dbus_call_register_device_info_sync(icl_dbus_get_object(), arg_info, &ret, + NULL, &error); + if (error) { + ERR("ic_dbus_call_register_device_info_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_info); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } return ret; } + +static void _icl_device_info_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + icl_device_info_s *cb_container = user_data; + iotcon_device_info_cb cb = cb_container->cb; + + iotcon_device_info_s info = {0}; + + g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s&s)", + &info.name, + &info.host_name, + &info.uuid, + &info.content_type, + &info.version, + &info.manuf_name, + &info.manuf_url, + &info.model_number, + &info.date_of_manufacture, + &info.platform_ver, + &info.firmware_ver, + &info.support_url); + + info.name = ic_utils_dbus_decode_str(info.name); + info.host_name = ic_utils_dbus_decode_str(info.host_name); + info.uuid = ic_utils_dbus_decode_str(info.uuid); + info.content_type = ic_utils_dbus_decode_str(info.content_type); + info.version = ic_utils_dbus_decode_str(info.version); + info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name); + info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url); + info.model_number = ic_utils_dbus_decode_str(info.model_number); + info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture); + info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver); + info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver); + info.support_url = ic_utils_dbus_decode_str(info.support_url); + + if (cb) + cb(info, cb_container->user_data); +} + + int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb, void *user_data) { - int ret = IOTCON_ERROR_NONE; + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_device_info_s *cb_container; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - ret = icl_dbus_get_device_info(host_address, cb, user_data); + signal_number = icl_dbus_generate_signal_number(); + + ic_dbus_call_get_device_info_sync(icl_dbus_get_object(), host_address, + signal_number, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_get_device_info() Fail(%d)", ret); - return ret; + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DEVICE, + signal_number); + + cb_container = calloc(1, sizeof(icl_device_info_container_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free, + _icl_device_info_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + return IOTCON_ERROR_DBUS; } - return IOTCON_ERROR_NONE; + cb_container->id = sub_id; + + return ret; } #endif + +/* The length of manufacturer_name should be less than and equal to 16. + * The length of manufacturer_url should be less than and equal to 32. */ API int iotcon_register_platform_info(iotcon_platform_info_s platform_info) { int ret; + GError *error = NULL; + GVariant *arg_info; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + + if (platform_info.manuf_name + && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(platform_info.manuf_name))) { + ERR("The length of manufacturer_name(%s) is invalid.", platform_info.manuf_name); + return IOTCON_ERROR_INVALID_PARAMETER; + } - ret = icl_dbus_register_platform_info(platform_info); - if (IOTCON_ERROR_NONE != ret) - ERR("ic_ioty_register_platform_info() Fail(%d)", ret); + if (platform_info.manuf_url + && (IOTCON_MANUFACTURER_URL_LENGTH_MAX < strlen(platform_info.manuf_url))) { + ERR("The length of manufacturer_url(%s) is invalid.", platform_info.manuf_url); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + arg_info = icl_dbus_platform_info_to_gvariant(&platform_info); + ic_dbus_call_register_platform_info_sync(icl_dbus_get_object(), arg_info, &ret, + NULL, &error); + if (error) { + ERR("ic_dbus_call_register_platform_info_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_info); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } return ret; } +static void _icl_platform_info_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + icl_platform_info_s *cb_container = user_data; + iotcon_platform_info_cb cb = cb_container->cb; + + iotcon_platform_info_s info = {0}; + + g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s)", + &info.platform_id, + &info.manuf_name, + &info.manuf_url, + &info.model_number, + &info.date_of_manufacture, + &info.platform_ver, + &info.os_ver, + &info.hardware_ver, + &info.firmware_ver, + &info.support_url, + &info.system_time); + + info.platform_id = ic_utils_dbus_decode_str(info.platform_id); + info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name); + info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url); + info.model_number = ic_utils_dbus_decode_str(info.model_number); + info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture); + info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver); + info.os_ver = ic_utils_dbus_decode_str(info.os_ver); + info.hardware_ver = ic_utils_dbus_decode_str(info.hardware_ver); + info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver); + info.support_url = ic_utils_dbus_decode_str(info.support_url); + info.system_time = ic_utils_dbus_decode_str(info.system_time); + + if (cb) + cb(info, cb_container->user_data); +} + + API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_cb cb, void *user_data) { - int ret = IOTCON_ERROR_NONE; + int ret; + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_platform_info_s *cb_container; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - ret = icl_dbus_get_platform_info(host_address, cb, user_data); + signal_number = icl_dbus_generate_signal_number(); + + ic_dbus_call_get_platform_info_sync(icl_dbus_get_object(), host_address, + signal_number, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + if (IOTCON_ERROR_NONE != ret) { - ERR("ic_ioty_get_platform_info() Fail(%d)", ret); - return ret; + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); } - return IOTCON_ERROR_NONE; + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PLATFORM, + signal_number); + + cb_container = calloc(1, sizeof(icl_platform_info_s)); + if (NULL == cb_container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + cb_container->cb = cb; + cb_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free, + _icl_platform_info_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + return IOTCON_ERROR_DBUS; + } + + cb_container->id = sub_id; + + return ret; } diff --git a/lib/icl-ioty.h b/lib/icl-ioty.h index c115ae2..1d828ef 100644 --- a/lib/icl-ioty.h +++ b/lib/icl-ioty.h @@ -30,9 +30,10 @@ struct icl_resource { bool is_observable; iotcon_resource_types_h types; int ifaces; - icl_handle_container_s *handle; iotcon_request_handler_cb cb; void *user_data; + unsigned int sub_id; + int handle; iotcon_resource_h children[IOTCON_CONTAINED_RESOURCES_MAX]; }; diff --git a/lib/icl-presence.c b/lib/icl-presence.c new file mode 100644 index 0000000..061f45d --- /dev/null +++ b/lib/icl-presence.c @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include + +#include "iotcon.h" +#include "icl.h" +#include "icl-dbus.h" + +typedef struct icl_presence { + iotcon_presence_cb cb; + void *user_data; + unsigned int id; + int handle; +} icl_presence_s; + +API int iotcon_start_presence(unsigned int time_to_live) +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + + ic_dbus_call_start_presence_sync(icl_dbus_get_object(), time_to_live, &ret, NULL, + &error); + if (error) { + ERR("ic_dbus_call_start_presence_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_stop_presence() +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + + ic_dbus_call_stop_presence_sync(icl_dbus_get_object(), &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_stop_presence_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; +} + + +static void _icl_presence_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + FN_CALL; + int res; + unsigned int nonce; + char *host_address; + icl_presence_s *presence_container = user_data; + iotcon_presence_cb cb = presence_container->cb; + + g_variant_get(parameters, "(iu&s)", &res, &nonce, &host_address); + + if (cb) + cb(res, nonce, host_address, presence_container->user_data); +} + + +static void _icl_presence_conn_cleanup(icl_presence_s *presence) +{ + presence->id = 0; + presence->handle = 0; +} + + +/* The length of resource_type should be less than or equal to 61. */ +API iotcon_presence_h iotcon_subscribe_presence(const char *host_address, + const char *resource_type, iotcon_presence_cb cb, void *user_data) +{ + GError *error = NULL; + unsigned int sub_id; + int signal_number; + char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0}; + icl_presence_s *presence_container; + + RETV_IF(NULL == icl_dbus_get_object(), NULL); + RETV_IF(NULL == host_address, NULL); + RETV_IF(NULL == cb, NULL); + if (resource_type && (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) { + ERR("The length of resource_type(%s) is invalid", resource_type); + return NULL; + } + + signal_number = icl_dbus_generate_signal_number(); + + presence_container = calloc(1, sizeof(icl_presence_s)); + if (NULL == presence_container) { + ERR("calloc() Fail(%d)", errno); + return NULL; + } + + if (NULL == resource_type) + resource_type = ""; + + ic_dbus_call_subscribe_presence_sync(icl_dbus_get_object(), host_address, + resource_type, signal_number, &(presence_container->handle), NULL, &error); + if (error) { + ERR("ic_dbus_call_subscribe_presence_sync() Fail(%s)", error->message); + g_error_free(error); + free(presence_container); + return NULL; + } + + if (0 == presence_container->handle) { + ERR("iotcon-daemon Fail"); + free(presence_container); + return NULL; + } + + snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PRESENCE, + signal_number); + + presence_container->cb = cb; + presence_container->user_data = user_data; + + sub_id = icl_dbus_subscribe_signal(signal_name, presence_container, + _icl_presence_conn_cleanup, _icl_presence_cb); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + free(presence_container); + return NULL; + } + + presence_container->id = sub_id; + + return presence_container; +} + + +API int iotcon_unsubscribe_presence(iotcon_presence_h presence) +{ + FN_CALL; + int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == presence, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == presence->id) { + WARN("Invalid Presence handle"); + free(presence); + return IOTCON_ERROR_NONE; + } + + ic_dbus_call_unsubscribe_presence_sync(icl_dbus_get_object(), presence->handle, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_unsubscribe_presence_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + icl_dbus_unsubscribe_signal(presence->id); + + free(presence); + + return IOTCON_ERROR_NONE; +} + diff --git a/lib/icl-response.c b/lib/icl-response.c index 541f18f..e480965 100644 --- a/lib/icl-response.c +++ b/lib/icl-response.c @@ -21,6 +21,7 @@ #include "ic-utils.h" #include "icl.h" #include "icl-dbus.h" +#include "icl-dbus-type.h" #include "icl-ioty.h" #include "icl-repr.h" #include "icl-options.h" @@ -130,14 +131,28 @@ API int iotcon_response_send(iotcon_response_h resp) { FN_CALL; int ret; + GError *error = NULL; + GVariant *arg_response; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == resp, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == resp->repr, IOTCON_ERROR_INVALID_PARAMETER); - ret = icl_dbus_send_response(resp); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_send_response() Fail(%d)", ret); + arg_response = icl_dbus_response_to_gvariant(resp); + ic_dbus_call_send_response_sync(icl_dbus_get_object(), arg_response, &ret, NULL, + &error); + if (error) { + ERR("ic_dbus_call_send_response_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(arg_response); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } - return ret; + return IOTCON_ERROR_NONE; } diff --git a/lib/icl.c b/lib/icl.c index 130c140..ac45abf 100644 --- a/lib/icl.c +++ b/lib/icl.c @@ -15,6 +15,7 @@ */ #include #include +#include #include #include #include @@ -24,9 +25,11 @@ #include "iotcon.h" #include "ic-utils.h" #include "icl-resource-types.h" +#include "icl-request.h" #include "icl-ioty.h" #include "icl-repr.h" #include "icl-dbus.h" +#include "icl-dbus-type.h" #include "icl.h" /** @@ -102,6 +105,89 @@ API int iotcon_remove_connection_changed_cb(iotcon_connection_changed_cb cb, } +static void _icl_request_handler(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + GVariantIter *options; + unsigned short option_id; + char *option_data; + GVariantIter *query; + char *key = NULL; + char *value = NULL; + char *repr_json; + int request_handle; + int resource_handle; + struct icl_resource_request request = {0}; + iotcon_resource_h resource = user_data; + iotcon_request_handler_cb cb = resource->cb; + + g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)", + &request.types, + &options, + &query, + &request.observation_info.action, + &request.observation_info.observer_id, + &repr_json, + &request_handle, + &resource_handle); + + if (g_variant_iter_n_children(options)) { + request.header_options = iotcon_options_new(); + while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data)) + iotcon_options_insert(request.header_options, option_id, option_data); + } + g_variant_iter_free(options); + + if (g_variant_iter_n_children(query)) { + request.query = iotcon_query_new(); + while (g_variant_iter_loop(query, "(&s&s)", &key, &value)) + iotcon_query_insert(request.query, key, value); + } + g_variant_iter_free(query); + + request.request_handle = GINT_TO_POINTER(request_handle); + request.resource_handle = GINT_TO_POINTER(resource_handle); + + if (ic_utils_dbus_decode_str(repr_json)) { + request.repr = icl_repr_create_repr(repr_json); + if (NULL == request.repr) { + ERR("icl_repr_create_repr() Fail"); + if (request.query) + iotcon_query_free(request.query); + if (request.header_options) + iotcon_options_free(request.header_options); + return; + } + } + + /* TODO remove request.uri */ + request.uri_path = "temp_uri_path"; + + if (cb) + cb(resource, &request, resource->user_data); + + /* To avoid unnecessary ERR log (repr could be NULL) */ + if (request.repr) + iotcon_repr_free(request.repr); + if (request.query) + iotcon_query_free(request.query); + if (request.header_options) + iotcon_options_free(request.header_options); +} + + +static void _icl_resource_conn_cleanup(iotcon_resource_h resource) +{ + resource->sub_id = 0; + resource->handle = 0; +} + + /* The length of uri_path should be less than or equal to 36. */ API iotcon_resource_h iotcon_register_resource(const char *uri_path, iotcon_resource_types_h res_types, @@ -111,13 +197,19 @@ API iotcon_resource_h iotcon_register_resource(const char *uri_path, void *user_data) { FN_CALL; + int signal_number; + unsigned int sub_id; + GError *error = NULL; + const gchar **types; + char sig_name[IC_DBUS_SIGNAL_LENGTH]; iotcon_resource_h resource; + RETV_IF(NULL == icl_dbus_get_object(), NULL); RETV_IF(NULL == uri_path, NULL); + RETVM_IF(IOTCON_URI_PATH_LENGTH_MAX < strlen(uri_path), NULL, "Invalid uri_path(%s)", + uri_path); RETV_IF(NULL == res_types, NULL); RETV_IF(NULL == cb, NULL); - 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) { @@ -125,10 +217,28 @@ API iotcon_resource_h iotcon_register_resource(const char *uri_path, return NULL; } - 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"); + types = icl_dbus_resource_types_to_array(res_types); + if (NULL == types) { + ERR("icl_dbus_resource_types_to_array() Fail"); + free(resource); + return NULL; + } + + signal_number = icl_dbus_generate_signal_number(); + + ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces, + properties, signal_number, &(resource->handle), NULL, &error); + if (error) { + ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message); + g_error_free(error); + free(types); + free(resource); + return NULL; + } + free(types); + + if (0 == resource->handle) { + ERR("iotcon-daemon Fail"); free(resource); return NULL; } @@ -136,35 +246,65 @@ API iotcon_resource_h iotcon_register_resource(const char *uri_path, resource->cb = cb; resource->user_data = user_data; - resource->uri_path = ic_utils_strdup(uri_path); resource->types = icl_resource_types_ref(res_types); + resource->uri_path = ic_utils_strdup(uri_path); resource->ifaces = ifaces; resource->is_observable = properties & IOTCON_OBSERVABLE; + snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER, + signal_number); + + sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup, + _icl_request_handler); + if (0 == sub_id) { + ERR("icl_dbus_subscribe_signal() Fail"); + free(resource); + return NULL; + } + + resource->sub_id = sub_id; + return resource; } -API void iotcon_unregister_resource(iotcon_resource_h resource) +API int iotcon_unregister_resource(iotcon_resource_h resource) { FN_CALL; int ret; + GError *error = NULL; + + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RET_IF(NULL == resource); + if (0 == resource->sub_id) { + WARN("Invalid Resource handle"); + iotcon_resource_types_free(resource->types); + free(resource->uri_path); + free(resource); + return IOTCON_ERROR_NONE; + } + + ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } - ret = icl_dbus_unregister_resource(resource->handle); if (IOTCON_ERROR_NONE != ret) { - ERR("icl_dbus_unregister_resource() Fail(%d)", ret); - return; + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); } - resource->handle = NULL; - free(resource->uri_path); - resource->uri_path = NULL; - free(resource->types); - resource->types = NULL; + icl_dbus_unsubscribe_signal(resource->sub_id); + iotcon_resource_types_free(resource->types); + free(resource->uri_path); free(resource); + + return IOTCON_ERROR_NONE; } @@ -172,12 +312,27 @@ API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e ifa { FN_CALL; int ret; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } - ret = icl_dbus_bind_interface(resource->handle, iface); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_bind_iface() Fail(%d)", ret); + ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle, + iface, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } return ret; } @@ -187,17 +342,33 @@ API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type) { FN_CALL; int ret; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER); if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) { - ERR("The length of resource_type(%s) is invalid", resource_type); + ERR("Invalid resource_type(%s)", resource_type); return IOTCON_ERROR_INVALID_PARAMETER; } - ret = icl_dbus_bind_type(resource->handle, resource_type); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_bind_type() Fail(%d)", ret); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type, + &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } return ret; } @@ -221,11 +392,22 @@ API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child) FN_CALL; int ret; int i; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == parent->sub_id) { + ERR("Invalid Resource handle(parent)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + if (0 == child->sub_id) { + ERR("Invalid Resource handle(child)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { if (child == parent->children[i]) { ERR("Child resource was already bound to parent resource."); @@ -235,13 +417,22 @@ API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child) for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { if (NULL == parent->children[i]) { - ret = icl_dbus_bind_resource(parent->handle, child->handle); - if (IOTCON_ERROR_NONE == ret) - parent->children[i] = child; - else - ERR("icl_dbus_bind_resource() Fail(%d)", ret); - - return ret; + ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle, + child->handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + parent->children[i] = child; + + return IOTCON_ERROR_NONE; } } @@ -254,22 +445,40 @@ API int iotcon_unbind_resource(iotcon_resource_h parent, iotcon_resource_h child { int ret; int i; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER); - ret = icl_dbus_unbind_resource(parent->handle, child->handle); - if (IOTCON_ERROR_NONE == ret) { - for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (0 == parent->sub_id) { + ERR("Invalid Resource handle(parent)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + if (0 == child->sub_id) { + ERR("Invalid Resource handle(child)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } - if (child == parent->children[i]) - parent->children[i] = NULL; - } - } else { - ERR("icl_dbus_unbind_res() Fail(%d)", ret); + ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle, + child->handle, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; } - return ret; + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) { + if (child == parent->children[i]) + parent->children[i] = NULL; + } + + return IOTCON_ERROR_NONE; } @@ -319,7 +528,8 @@ API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path /* The content of the resource should not be freed by user. */ -API int iotcon_resource_get_types(iotcon_resource_h resource, iotcon_resource_types_h *types) +API int iotcon_resource_get_types(iotcon_resource_h resource, + iotcon_resource_types_h *types) { RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER); @@ -352,72 +562,6 @@ API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observab } -API int iotcon_start_presence(unsigned int time_to_live) -{ - FN_CALL; - int ret; - - ret = icl_dbus_start_presence(time_to_live); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_start_presence() Fail(%d)", ret); - - return ret; -} - - -API int iotcon_stop_presence() -{ - FN_CALL; - int ret; - - ret = icl_dbus_stop_presence(); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_stop_presence() Fail(%d)", ret); - - return ret; -} - - -/* The length of resource_type should be less than or equal to 61. */ -API iotcon_presence_h iotcon_subscribe_presence(const char *host_address, - const char *resource_type, iotcon_presence_cb cb, void *user_data) -{ - iotcon_presence_h handle; - - RETV_IF(NULL == host_address, NULL); - RETV_IF(NULL == cb, NULL); - if (resource_type && (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) { - ERR("The length of resource_type(%s) is invalid", resource_type); - return NULL; - } - - if (NULL == resource_type) - resource_type = ""; - - handle = icl_dbus_subscribe_presence(host_address, resource_type, cb, user_data); - if (NULL == handle) - ERR("icl_dbus_subscribe_presence() Fail"); - - return handle; -} - - -API int iotcon_unsubscribe_presence(iotcon_presence_h handle) -{ - FN_CALL; - int ret; - - RETV_IF(NULL == handle, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_dbus_unsubscribe_presence(handle); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_unsubscribe_presence() Fail(%d)", ret); - - return ret; -} - - - API iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e iface) { iotcon_notimsg_h msg; @@ -452,27 +596,69 @@ API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notim iotcon_observers_h observers) { int ret; + GError *error = NULL; + GVariant *noti_msg; + GVariant *obs; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); - ret = icl_dbus_notify_list_of_observers(resource->handle, msg, observers); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_notify_list_of_observers() Fail(%d)", ret); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } - return ret; + noti_msg = icl_dbus_notimsg_to_gvariant(msg); + if (NULL == noti_msg) { + ERR("icl_dbus_notimsg_to_gvariant() Fail"); + return IOTCON_ERROR_REPRESENTATION; + } + obs = icl_dbus_observers_to_gvariant(observers); + + ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle, + noti_msg, obs, &ret, NULL, &error); + if (error) { + ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message); + g_error_free(error); + g_variant_unref(obs); + g_variant_unref(noti_msg); + return IOTCON_ERROR_DBUS; + } + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; } API int iotcon_notify_all(iotcon_resource_h resource) { int ret; + GError *error = NULL; + RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS); RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + if (0 == resource->sub_id) { + ERR("Invalid Resource handle"); + return IOTCON_ERROR_INVALID_PARAMETER; + } - ret = icl_dbus_notify_all(resource->handle); - if (IOTCON_ERROR_NONE != ret) - ERR("icl_dbus_notify_all() Fail(%d)", ret); + ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL, + &error); + if (error) { + ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message); + g_error_free(error); + return IOTCON_ERROR_DBUS; + } - return ret; + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon-daemon Fail(%d)", ret); + return icl_dbus_convert_daemon_error(ret); + } + + return IOTCON_ERROR_NONE; } diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index d30aa54..014f39d 100644 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -30,7 +30,7 @@ #define IOTCON_OPTIONS_MAX 2 #define IOTCON_OPTION_DATA_LENGTH_MAX 16 -#define IOTCON_URI_LENGTH_MAX 36 +#define IOTCON_URI_PATH_LENGTH_MAX 36 #define IOTCON_QUERY_LENGTH_MAX 64 diff --git a/lib/include/iotcon-struct.h b/lib/include/iotcon-struct.h index c28acf5..9939a29 100644 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -26,7 +26,7 @@ typedef struct icl_repr_s* iotcon_repr_h; typedef struct icl_notify_msg* iotcon_notimsg_h; -typedef void* iotcon_presence_h; +typedef struct icl_presence* iotcon_presence_h; #ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */ typedef struct _device_info { diff --git a/lib/include/iotcon.h b/lib/include/iotcon.h index 5ccad1e..4253367 100644 --- a/lib/include/iotcon.h +++ b/lib/include/iotcon.h @@ -49,14 +49,15 @@ int iotcon_add_connection_changed_cb(iotcon_connection_changed_cb cb, void *user int iotcon_remove_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data); -typedef void (*iotcon_request_handler_cb)(iotcon_request_h request, void *user_data); +typedef void (*iotcon_request_handler_cb)(iotcon_resource_h resource, + iotcon_request_h request, void *user_data); iotcon_resource_h iotcon_register_resource(const char *uri_path, iotcon_resource_types_h res_types, int ifaces, uint8_t properties, iotcon_request_handler_cb cb, void *user_data); -void iotcon_unregister_resource(iotcon_resource_h resource_handle); +int iotcon_unregister_resource(iotcon_resource_h resource_handle); int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface); @@ -94,10 +95,14 @@ int iotcon_find_resource(const char *host_address, const char *resource_type, 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_client_h iotcon_client_ref(iotcon_client_h resource); -typedef void (*iotcon_on_observe_cb)(iotcon_options_h header_options, iotcon_repr_h repr, - int response_result, int sequence_number, void *user_data); +typedef void (*iotcon_on_observe_cb)(iotcon_client_h resource, + iotcon_repr_h repr, + iotcon_options_h header_options, + int response_result, + int sequence_number, + void *user_data); int iotcon_observer_start(iotcon_client_h resource, iotcon_observe_type_e observe_type, iotcon_query_h query, @@ -113,8 +118,8 @@ int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h iotcon_observers_h observers); int iotcon_notify_all(iotcon_resource_h resource); -typedef void (*iotcon_on_cru_cb)(iotcon_repr_h repr, iotcon_options_h header_options, - int response_result, void *user_data); +typedef void (*iotcon_on_cru_cb)(iotcon_client_h resource, iotcon_repr_h repr, + iotcon_options_h 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); @@ -124,8 +129,8 @@ int iotcon_put(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h quer int iotcon_post(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data); -typedef void (*iotcon_on_delete_cb)(iotcon_options_h header_options, int response_result, - void *user_data); +typedef void (*iotcon_on_delete_cb)(iotcon_client_h resource, iotcon_options_h options, + int response_result, void *user_data); int iotcon_delete(iotcon_client_h resource, iotcon_on_delete_cb cb, void *user_data); diff --git a/test/crud-test-client.c b/test/crud-test-client.c index 9b5e1c8..8519831 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -23,16 +23,18 @@ static const char* const door_uri_path = "/a/door"; static char *door_resource_sid; -static iotcon_client_h door_resource = NULL; - void _print_repr_info(iotcon_repr_h repr) { if (0 < iotcon_repr_get_keys_count(repr)) DBG("rep : \n%s", iotcon_repr_generate_json(repr)); } -static void _on_observe(iotcon_options_h header_options, iotcon_repr_h recv_repr, - int response_result, int sequence_number, void *user_data) +static void _on_observe(iotcon_client_h resource, + iotcon_repr_h recv_repr, + iotcon_options_h header_options, + int response_result, + int sequence_number, + void *user_data) { INFO("_on_observe"); @@ -40,12 +42,14 @@ static void _on_observe(iotcon_options_h header_options, iotcon_repr_h recv_repr i++; if (2 == i) - iotcon_observer_stop(door_resource); + iotcon_observer_stop(resource); } -static void _on_delete(iotcon_options_h header_options, int response_result, - void *user_data) +static void _on_delete(iotcon_client_h resource, iotcon_options_h header_options, + int response_result, void *user_data) { + iotcon_client_h door_resource = user_data; + RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result && IOTCON_RESPONSE_RESULT_RESOURCE_DELETED != response_result, "_on_delete Response error(%d)", response_result); @@ -54,14 +58,16 @@ static void _on_delete(iotcon_options_h header_options, int response_result, /* delete callback operations */ iotcon_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL, _on_observe, NULL); + iotcon_client_free(door_resource); } -static void _on_post(iotcon_repr_h recv_repr, iotcon_options_h header_options, - int response_result, void *user_data) +static void _on_post(iotcon_client_h resource, iotcon_repr_h recv_repr, + iotcon_options_h header_options, int response_result, void *user_data) { int ret; char *created_uri_path = NULL; iotcon_client_h new_door_resource = NULL; + iotcon_client_h door_resource = NULL; char *host = NULL; iotcon_resource_types_h types = NULL; int ifaces = 0; @@ -82,33 +88,34 @@ static void _on_post(iotcon_repr_h recv_repr, iotcon_options_h header_options, DBG("New resource created : %s", created_uri_path); - ret = iotcon_client_get_host(door_resource, &host); + ret = iotcon_client_get_host(resource, &host); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_host() Fail(%d)", ret); return; } - ret = iotcon_client_get_types(door_resource, &types); + ret = iotcon_client_get_types(resource, &types); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_types() Fail(%d)", ret); return; } - ret = iotcon_client_get_interfaces(door_resource, &ifaces); + ret = iotcon_client_get_interfaces(resource, &ifaces); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_client_get_ifaces() Fail(%d)", ret); return; } new_door_resource = iotcon_client_new(host, created_uri_path, true, types, ifaces); + door_resource = iotcon_client_ref(resource); - iotcon_delete(new_door_resource, _on_delete, NULL); + iotcon_delete(new_door_resource, _on_delete, door_resource); iotcon_client_free(new_door_resource); } -static void _on_put(iotcon_repr_h recv_repr, iotcon_options_h header_options, - int response_result, void *user_data) +static void _on_put(iotcon_client_h resource, 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)", response_result); @@ -119,13 +126,13 @@ static void _on_put(iotcon_repr_h recv_repr, iotcon_options_h header_options, iotcon_repr_h send_repr = iotcon_repr_new(); /* send POST request */ - iotcon_post(door_resource, send_repr, NULL, _on_post, NULL); + iotcon_post(resource, send_repr, NULL, _on_post, NULL); iotcon_repr_free(send_repr); } -static void _on_get(iotcon_repr_h recv_repr, iotcon_options_h header_options, - int response_result, void *user_data) +static void _on_get(iotcon_client_h resource, 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)", response_result); @@ -137,7 +144,7 @@ static void _on_get(iotcon_repr_h recv_repr, iotcon_options_h header_options, iotcon_repr_set_bool(send_repr, "opened", true); /* send PUT request */ - iotcon_put(door_resource, send_repr, NULL, _on_put, NULL); + iotcon_put(resource, send_repr, NULL, _on_put, NULL); iotcon_repr_free(send_repr); } @@ -238,9 +245,12 @@ static void _found_resource(iotcon_client_h resource, void *user_data) iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL); if (TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) { - door_resource = iotcon_client_clone(resource); - iotcon_query_h query = iotcon_query_new(); + ret = iotcon_query_insert(query, "query_key", "query_value"); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_insert() Fail(%d)", ret); + return; + } /* send GET Request */ iotcon_get(resource, query, _on_get, NULL); diff --git a/test/crud-test-server.c b/test/crud-test-server.c index 4180219..309b6fc 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -32,14 +32,12 @@ typedef struct _door_resource_s { static door_resource_s my_door; static bool resource_created = false; -static iotcon_resource_h door_handle; -static iotcon_resource_h new_door_handle; - static iotcon_observers_h observers = NULL; -static void _request_handler(iotcon_request_h request, void *user_data); +static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data); -static iotcon_error_e _set_door_resource() +static int _set_door_resource() { my_door.state = false; my_door.type = strdup("core.door"); @@ -147,8 +145,26 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res iotcon_repr_free(resp_repr); } -static void _request_handler_post(iotcon_response_h response) +static gboolean _notifier(gpointer user_data) { + static int i = 0; + if ((5 == i++) || !(observers)) + return FALSE; + + INFO("NOTIFY!"); + iotcon_repr_h repr = iotcon_repr_new(); + iotcon_notimsg_h msg = iotcon_notimsg_new(repr, IOTCON_INTERFACE_DEFAULT); + iotcon_notify_list_of_observers(user_data, msg, observers); + + iotcon_notimsg_free(msg); + iotcon_repr_free(repr); + + return TRUE; +} + +static void _request_handler_post(iotcon_resource_h resource, iotcon_response_h response) +{ + iotcon_resource_h new_door_handle; iotcon_repr_h resp_repr = NULL; INFO("POST request"); @@ -172,40 +188,24 @@ static void _request_handler_post(iotcon_response_h response) _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); iotcon_repr_free(resp_repr); -} -static gboolean _notifier(gpointer user_data) -{ - static int i = 0; - if ((5 == i++) || !(observers)) - return FALSE; - - INFO("NOTIFY!"); - iotcon_repr_h repr = iotcon_repr_new(); - iotcon_notimsg_h msg = iotcon_notimsg_new(repr, IOTCON_INTERFACE_DEFAULT); - iotcon_notify_list_of_observers(user_data, msg, observers); - - iotcon_notimsg_free(msg); - iotcon_repr_free(repr); - - return TRUE; + /* add observe */ + g_timeout_add_seconds(5, _notifier, resource); } -static void _request_handler_delete(iotcon_response_h response) +static void _request_handler_delete(iotcon_resource_h resource, + iotcon_response_h response) { iotcon_repr_h resp_repr = NULL; iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK; INFO("DELETE request"); - iotcon_unregister_resource(new_door_handle); + iotcon_unregister_resource(resource); resp_repr = iotcon_repr_new(); result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED; _send_response(response, resp_repr, result); - /* add observe */ - g_timeout_add_seconds(5, _notifier, door_handle); - iotcon_repr_free(resp_repr); } @@ -217,7 +217,8 @@ static int _query_fn(const char *key, const char *value, void *user_data) return IOTCON_FUNC_CONTINUE; } -static void _request_handler(iotcon_request_h request, void *user_data) +static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) { int ret; int types; @@ -255,10 +256,10 @@ static void _request_handler(iotcon_request_h request, void *user_data) _request_handler_put(request, response); else if (IOTCON_REQUEST_POST & types) - _request_handler_post(response); + _request_handler_post(resource, response); else if (IOTCON_REQUEST_DELETE & types) - _request_handler_delete(response); + _request_handler_delete(resource, response); iotcon_response_free(response); @@ -306,9 +307,10 @@ int main(int argc, char **argv) { FN_CALL; GMainLoop *loop; + iotcon_resource_h door_handle; iotcon_interface_e door_interfaces = IOTCON_INTERFACE_DEFAULT; iotcon_resource_property_e resource_properties = IOTCON_DISCOVERABLE; - iotcon_error_e iotcon_error = IOTCON_ERROR_NONE; + int ret = IOTCON_ERROR_NONE; loop = g_main_loop_new(NULL, FALSE); @@ -316,8 +318,8 @@ int main(int argc, char **argv) iotcon_initialize(); /* set local door resource */ - iotcon_error = _set_door_resource(); - if (IOTCON_ERROR_NONE != iotcon_error) { + ret = _set_door_resource(); + if (IOTCON_ERROR_NONE != ret) { ERR("_set_door_resource() Fail"); return -1; } diff --git a/test/repr-test-client.c b/test/repr-test-client.c index c94dd32..b390bff 100644 --- a/test/repr-test-client.c +++ b/test/repr-test-client.c @@ -22,8 +22,6 @@ static const char* const room_uri_path = "/a/room"; static char *room_resource_sid; -static iotcon_client_h room_resource = NULL; - static int _get_int_list_fn(int pos, const int value, void *user_data) { DBG("%d°C", value); @@ -99,14 +97,14 @@ static void _on_get(iotcon_repr_h recv_repr, int response_result) } } -static void _on_get_2nd(iotcon_repr_h recv_repr, iotcon_options_h header_options, - int response_result, void *user_data) +static void _on_get_2nd(iotcon_client_h resource, 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_repr_h recv_repr, iotcon_options_h header_options, - int response_result, void *user_data) +static void _on_get_1st(iotcon_client_h resource, iotcon_repr_h recv_repr, + iotcon_options_h header_options, int response_result, void *user_data) { iotcon_query_h query_params; @@ -116,7 +114,7 @@ static void _on_get_1st(iotcon_repr_h recv_repr, iotcon_options_h header_options iotcon_query_insert(query_params, "if", "oc.mi.b"); /* send GET request again with BATCH interface */ - iotcon_get(room_resource, query_params, _on_get_2nd, NULL); + iotcon_get(resource, query_params, _on_get_2nd, NULL); iotcon_query_free(query_params); } @@ -199,9 +197,6 @@ static void _found_resource(iotcon_client_h resource, void *user_data) iotcon_resource_types_foreach(resource_types, _get_res_type_fn, resource_uri_path); if (TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) { - /* copy resource to use elsewhere */ - room_resource = iotcon_client_clone(resource); - /* send GET request */ iotcon_get(resource, NULL, _on_get_1st, NULL); } diff --git a/test/repr-test-server.c b/test/repr-test-server.c index 6e39442..623db58 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -20,7 +20,8 @@ #include #include "test.h" -static void _room_request_handler(iotcon_request_h request, void *user_data); +static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data); static void _send_response(iotcon_response_h response, iotcon_repr_h repr, iotcon_interface_e interface) @@ -162,7 +163,8 @@ static void _request_handler_delete(iotcon_response_h response) } -static void _light_request_handler(iotcon_request_h request, void *user_data) +static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) { int ret; int types; @@ -198,7 +200,8 @@ static void _light_request_handler(iotcon_request_h request, void *user_data) iotcon_response_free(response); } -static void _room_request_handler(iotcon_request_h request, void *user_data) +static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) { int ret; int types; @@ -240,7 +243,7 @@ int main(int argc, char **argv) GMainLoop *loop; iotcon_resource_types_h room_rtypes = NULL; iotcon_resource_types_h light_rtypes = NULL; - iotcon_error_e iotcon_error = IOTCON_ERROR_NONE; + int ret = IOTCON_ERROR_NONE; loop = g_main_loop_new(NULL, FALSE); @@ -276,8 +279,8 @@ int main(int argc, char **argv) return -1; } - iotcon_error = iotcon_bind_resource(room_handle, light_handle); - if (IOTCON_ERROR_NONE != iotcon_error) { + ret = iotcon_bind_resource(room_handle, light_handle); + if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_bind_resource() Fail"); return -1; } -- 2.7.4