From: sung.goo.kim Date: Thu, 12 Nov 2015 09:31:37 +0000 (+0900) Subject: Fix observe API (set_notify_cb -> observe_register) X-Git-Tag: submit/tizen/20151130.235229~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8b2429764269b6ed9c7a76752c00bdacb87166f5;p=platform%2Fcore%2Fiot%2Fiotcon.git Fix observe API (set_notify_cb -> observe_register) Change-Id: Idc3a21e6f2e7f9553cb3cfde8c9d0bd7c54f5b66 --- diff --git a/daemon/icd-dbus.c b/daemon/icd-dbus.c index a5f43ae..e19a0d7 100644 --- a/daemon/icd-dbus.c +++ b/daemon/icd-dbus.c @@ -517,7 +517,7 @@ static gboolean _dbus_handle_find_resource(icDbus *object, static gboolean _dbus_handle_observer_start(icDbus *object, GDBusMethodInvocation *invocation, GVariant *resource, - gint observe_type, + gint observe_policy, GVariant *query, guint signal_number) { @@ -525,7 +525,7 @@ static gboolean _dbus_handle_observer_start(icDbus *object, const gchar *sender; sender = g_dbus_method_invocation_get_sender(invocation); - observe_h = icd_ioty_observer_start(resource, observe_type, query, + observe_h = icd_ioty_observer_start(resource, observe_policy, query, signal_number, sender); if (NULL == observe_h) ERR("icd_ioty_observer_start() Fail"); diff --git a/daemon/icd-ioty-ocprocess.c b/daemon/icd-ioty-ocprocess.c index 1215a9c..c71ecc8 100644 --- a/daemon/icd-ioty-ocprocess.c +++ b/daemon/icd-ioty-ocprocess.c @@ -45,9 +45,9 @@ struct icd_ioty_worker struct icd_req_context { unsigned int signal_number; char *bus_name; - int types; + int request_type; int observe_id; - int observe_action; + int observe_type; OCRequestHandle request_h; OCResourceHandle resource_h; GVariant *payload; @@ -224,7 +224,7 @@ static int _ioty_oic_action_to_ioty_action(int oic_action) case OC_OBSERVE_NO_OPTION: default: ERR("Invalid action (%d)", oic_action); - action = IOTCON_OBSERVE_NO_OPTION; + action = IOTCON_OBSERVE_TYPE_NONE; } return action; } @@ -246,7 +246,7 @@ static int _worker_req_handler(void *context) { GVariant *value; char *host_address; - int ret, conn_type, action; + int ret, conn_type; GVariantBuilder payload_builder; struct icd_req_context *ctx = context; @@ -266,15 +266,13 @@ static int _worker_req_handler(void *context) return ret; } - action = _ioty_oic_action_to_ioty_action(ctx->observe_action); - value = g_variant_new("(siia(qs)a(ss)iiavxx)", host_address, conn_type, - ctx->types, + ctx->request_type, ctx->options, ctx->query, - action, + ctx->observe_type, ctx->observe_id, &payload_builder, ICD_POINTER_TO_INT64(ctx->request_h), @@ -341,26 +339,19 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag, if (OC_REQUEST_FLAG & flag) { switch (request->method) { case OC_REST_GET: - req_ctx->types = IOTCON_REQUEST_GET; + req_ctx->request_type = IOTCON_REQUEST_GET; req_ctx->payload = NULL; - - if (OC_OBSERVE_FLAG & flag) { - req_ctx->types |= IOTCON_REQUEST_OBSERVE; - /* observation info*/ - req_ctx->observe_id = request->obsInfo.obsId; - req_ctx->observe_action = request->obsInfo.action; - } break; case OC_REST_PUT: - req_ctx->types = IOTCON_REQUEST_PUT; + req_ctx->request_type = IOTCON_REQUEST_PUT; req_ctx->payload = icd_payload_to_gvariant(request->payload); break; case OC_REST_POST: - req_ctx->types = IOTCON_REQUEST_POST; + req_ctx->request_type = IOTCON_REQUEST_POST; req_ctx->payload = icd_payload_to_gvariant(request->payload); break; case OC_REST_DELETE: - req_ctx->types = IOTCON_REQUEST_DELETE; + req_ctx->request_type = IOTCON_REQUEST_DELETE; req_ctx->payload = NULL; break; default: @@ -370,6 +361,14 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag, } } + if (OC_OBSERVE_FLAG & flag) { + /* observation info*/ + req_ctx->observe_id = request->obsInfo.obsId; + req_ctx->observe_type = _ioty_oic_action_to_ioty_action(request->obsInfo.action); + } else { + req_ctx->observe_type = IOTCON_OBSERVE_TYPE_NONE; + } + /* header options */ req_ctx->options = _ocprocess_parse_header_options( request->rcvdVendorSpecificHeaderOptions, diff --git a/daemon/icd-ioty.c b/daemon/icd-ioty.c index 331f390..5a23167 100644 --- a/daemon/icd-ioty.c +++ b/daemon/icd-ioty.c @@ -706,7 +706,7 @@ gboolean icd_ioty_delete(icDbus *object, GDBusMethodInvocation *invocation, } -OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_type, +OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_policy, GVariant *query, unsigned int signal_number, const char *bus_name) { bool is_secure; @@ -733,9 +733,9 @@ OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_type, return NULL; } - if (IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER == observe_type) + if (IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER == observe_policy) method = OC_REST_OBSERVE; - else if (IOTCON_OBSERVE_ACCEPT_OUT_OF_ORDER == observe_type) + else if (IOTCON_OBSERVE_ACCEPT_OUT_OF_ORDER == observe_policy) method = OC_REST_OBSERVE_ALL; else method = OC_REST_OBSERVE_ALL; diff --git a/doc/iotcon_doc.h b/doc/iotcon_doc.h index 48a02b9..1884041 100644 --- a/doc/iotcon_doc.h +++ b/doc/iotcon_doc.h @@ -51,15 +51,15 @@ static void _request_handler(iotcon_request_h request, void *user_data) { int ret; - int types; + iotcon_request_type_e type; iotcon_response_h response; iotcon_representation_h resp_repr; - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_request_type(request, &type); if (IOTCON_ERROR_NONE != ret) return; - if (IOTCON_REQUEST_GET & types) { + if (IOTCON_REQUEST_GET == type) { ret = iotcon_response_create(request, &response); if (IOTCON_ERROR_NONE != ret) return; @@ -237,37 +237,32 @@ static void _request_handler(iotcon_request_h request, void *user_data) int ret; int types; int observe_id; - iotcon_observe_action_e observe_action; + iotcon_observe_type_e observe_type; - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_observe_type(request, &observe_type); if (IOTCON_ERROR_NONE != ret) { return; } - if (IOTCON_REQUEST_OBSERVE & types) { - ret = iotcon_request_get_observe_action(request, &observe_action); + if (IOTCON_OBSERVE_REGISTER == observe_type) { + ret = iotcon_request_get_observe_id(request, &observe_id); + if (IOTCON_ERROR_NONE != ret) { + return; + } + + ret = iotcon_observers_add(observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + return; + } + } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) { + ret = iotcon_request_get_observe_id(request, &observe_id); if (IOTCON_ERROR_NONE != ret) { return; } - if (IOTCON_OBSERVE_REGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - return; - } - ret = iotcon_observers_add(observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - return; - } - } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - return; - } - ret = iotcon_observers_remove(observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - return; - } + ret = iotcon_observers_remove(observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + return; } } } @@ -334,7 +329,7 @@ static void _on_resopnse_observe(iotcon_remote_resource_h resource, iotcon_error ... { int ret; - ret = iotcon_remote_resource_set_notify_cb(door_resource, IOTCON_OBSERVE_ACCEPT_OUT_OF_ORDER, NULL, + ret = iotcon_remote_resource_observe_register(door_resource, IOTCON_OBSERVE_ACCEPT_OUT_OF_ORDER, NULL, _on_resopnse_observe, NULL); if (IOTCON_ERROR_NONE != ret) return; diff --git a/lib/icl-lite-resource.c b/lib/icl-lite-resource.c index 90bb996..b494af0 100644 --- a/lib/icl-lite-resource.c +++ b/lib/icl-lite-resource.c @@ -138,7 +138,7 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection, gpointer user_data) { GVariant *repr_gvar; - int ret, request_types; + int ret, request_type; iotcon_representation_h repr; iotcon_state_h recv_state = NULL; GVariantIter *repr_iter, *state_iter; @@ -165,7 +165,7 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection, g_variant_get(parameters, "(siia(qs)a(ss)iiavxx)", NULL, /* host address */ NULL, /* connectivity type */ - &request_types, + &request_type, NULL, /* header options */ NULL, /* query */ NULL, /* observe action */ @@ -174,9 +174,8 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection, &oic_request_h, &oic_resource_h); - switch (request_types) { + switch (request_type) { case IOTCON_REQUEST_GET: - case (IOTCON_REQUEST_GET | IOTCON_REQUEST_OBSERVE): break; case IOTCON_REQUEST_PUT: if (FALSE == g_variant_iter_loop(repr_iter, "v", &repr_gvar)) { @@ -242,7 +241,7 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection, return; } - if (IOTCON_REQUEST_PUT == request_types) { + if (IOTCON_REQUEST_PUT == request_type) { ret = _icl_lite_resource_notify(resource); if (IOTCON_ERROR_NONE != ret) WARN("_icl_lite_resource_notify() Fail(%d)", ret); diff --git a/lib/icl-remote-resource-crud.c b/lib/icl-remote-resource-crud.c index d3e7a9b..b41f87f 100644 --- a/lib/icl-remote-resource-crud.c +++ b/lib/icl-remote-resource-crud.c @@ -38,6 +38,12 @@ typedef struct { iotcon_remote_resource_h resource; } icl_on_response_s; +typedef struct { + iotcon_remote_resource_observe_cb cb; + void *user_data; + iotcon_remote_resource_h resource; +} icl_on_observe_s; + static GList *icl_crud_cb_list = NULL; void icl_remote_resource_crud_stop(iotcon_remote_resource_h resource) @@ -54,38 +60,46 @@ void icl_remote_resource_crud_stop(iotcon_remote_resource_h resource) } } +static iotcon_options_h _icl_parse_options_iter(GVariantIter *iter) +{ + int ret; + iotcon_options_h options = NULL; + + if (NULL == iter) + return NULL; + + if (g_variant_iter_n_children(iter)) { + unsigned short option_id; + char *option_data; + + ret = iotcon_options_create(&options); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_options_create() Fail(%d)", ret); + return NULL; + } + while (g_variant_iter_loop(iter, "(q&s)", &option_id, &option_data)) + iotcon_options_add(options, option_id, option_data); + } + return options; +} + static int _icl_parse_crud_gvariant(iotcon_request_type_e request_type, GVariant *gvar, iotcon_response_h *response) { int res; - int ret; - int seq_number = -1; GVariantIter *options_iter = NULL; GVariant *repr_gvar = NULL; iotcon_response_h resp = NULL; iotcon_options_h options = NULL; iotcon_representation_h repr = NULL; - if (IOTCON_REQUEST_OBSERVE == request_type) - g_variant_get(gvar, "(a(qs)vii)", &options_iter, &repr_gvar, &res, &seq_number); - else if (IOTCON_REQUEST_DELETE == request_type) + if (IOTCON_REQUEST_DELETE == request_type) g_variant_get(gvar, "(a(qs)i)", &options_iter, &res); else g_variant_get(gvar, "(a(qs)vi)", &options_iter, &repr_gvar, &res); if (options_iter) { - if (g_variant_iter_n_children(options_iter)) { - unsigned short option_id; - char *option_data; - - ret = iotcon_options_create(&options); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_options_create() Fail(%d)", ret); - return ret; - } - while (g_variant_iter_loop(options_iter, "(q&s)", &option_id, &option_data)) - iotcon_options_add(options, option_id, option_data); - } + options = _icl_parse_options_iter(options_iter); g_variant_iter_free(options_iter); } @@ -113,7 +127,6 @@ static int _icl_parse_crud_gvariant(iotcon_request_type_e request_type, resp->result = res; resp->repr = repr; resp->header_options = options; - resp->seq_number = seq_number; *response = resp; @@ -372,24 +385,59 @@ static void _icl_on_observe_cb(GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - int ret; + int res; + int seq_number = -1; + GVariantIter *options_iter = NULL; + GVariant *repr_gvar = NULL; iotcon_response_h response = NULL; - icl_on_response_s *cb_container = user_data; + icl_on_observe_s *cb_container = user_data; + iotcon_options_h options = NULL; + iotcon_representation_h repr = NULL; RET_IF(NULL == cb_container); - ret = _icl_parse_crud_gvariant(IOTCON_REQUEST_OBSERVE, parameters, &response); - if (IOTCON_ERROR_NONE != ret) { - ERR("_icl_parse_crud_gvariant() Fail(%d)", ret); - if (cb_container->cb) { - cb_container->cb(cb_container->resource, ret, IOTCON_REQUEST_OBSERVE, NULL, - cb_container->user_data); + g_variant_get(parameters, "(a(qs)vii)", &options_iter, &repr_gvar, &res, &seq_number); + + if (options_iter) { + options = _icl_parse_options_iter(options_iter); + g_variant_iter_free(options_iter); + } + + if (repr_gvar) { + repr = icl_representation_from_gvariant(repr_gvar); + if (NULL == repr) { + ERR("icl_representation_from_gvariant() Fail"); + if (options) + iotcon_options_destroy(options); + + if (cb_container->cb) + cb_container->cb(cb_container->resource, IOTCON_ERROR_SYSTEM, -1, + NULL, cb_container->user_data); + return; } + } + + res = icl_dbus_convert_daemon_error(res); + + response = calloc(1, sizeof(struct icl_resource_response)); + if (NULL == response) { + ERR("calloc() Fail(%d)", errno); + if (repr) + iotcon_representation_destroy(repr); + if (options) + iotcon_options_destroy(options); + + if (cb_container->cb) + cb_container->cb(cb_container->resource, IOTCON_ERROR_OUT_OF_MEMORY, -1, + NULL, cb_container->user_data); return; } + response->result = res; + response->repr = repr; + response->header_options = options; if (cb_container->cb) - cb_container->cb(cb_container->resource, IOTCON_ERROR_NONE, IOTCON_REQUEST_OBSERVE, + cb_container->cb(cb_container->resource, IOTCON_ERROR_NONE, seq_number, response, cb_container->user_data); if (response) @@ -397,7 +445,7 @@ static void _icl_on_observe_cb(GDBusConnection *connection, } -static void _icl_observe_conn_cleanup(icl_on_response_s *cb_container) +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; @@ -406,7 +454,7 @@ static void _icl_observe_conn_cleanup(icl_on_response_s *cb_container) int icl_remote_resource_observer_start(iotcon_remote_resource_h resource, - iotcon_observe_type_e observe_type, + iotcon_observe_policy_e observe_policy, iotcon_query_h query, GDBusSignalCallback sig_handler, void *cb_container, @@ -428,7 +476,7 @@ int icl_remote_resource_observer_start(iotcon_remote_resource_h resource, arg_query = icl_dbus_query_to_gvariant(query); ic_dbus_call_observer_start_sync(icl_dbus_get_object(), arg_remote_resource, - observe_type, arg_query, signal_number, observe_handle, NULL, &error); + observe_policy, arg_query, signal_number, observe_handle, NULL, &error); if (error) { ERR("ic_dbus_call_observer_start_sync() Fail(%s)", error->message); ret = icl_dbus_convert_dbus_error(error->code); @@ -456,22 +504,22 @@ int icl_remote_resource_observer_start(iotcon_remote_resource_h resource, } -API int iotcon_remote_resource_set_notify_cb(iotcon_remote_resource_h resource, - iotcon_observe_type_e observe_type, +API int iotcon_remote_resource_observe_register(iotcon_remote_resource_h resource, + iotcon_observe_policy_e observe_policy, iotcon_query_h query, - iotcon_remote_resource_response_cb cb, + iotcon_remote_resource_observe_cb cb, void *user_data) { int ret; unsigned int sub_id; int64_t observe_handle; - icl_on_response_s *cb_container; + icl_on_observe_s *cb_container; RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(resource->observe_handle || resource->observe_sub_id, IOTCON_ERROR_ALREADY); - cb_container = calloc(1, sizeof(icl_on_response_s)); + cb_container = calloc(1, sizeof(icl_on_observe_s)); if (NULL == cb_container) { ERR("calloc() Fail(%d)", errno); return IOTCON_ERROR_OUT_OF_MEMORY; @@ -482,7 +530,7 @@ API int iotcon_remote_resource_set_notify_cb(iotcon_remote_resource_h resource, cb_container->user_data = user_data; ret = icl_remote_resource_observer_start(resource, - observe_type, + observe_policy, query, _icl_on_observe_cb, cb_container, @@ -532,7 +580,7 @@ int icl_remote_resource_stop_observing(iotcon_remote_resource_h resource, } -API int iotcon_remote_resource_unset_notify_cb(iotcon_remote_resource_h resource) +API int iotcon_remote_resource_observe_deregister(iotcon_remote_resource_h resource) { int ret; diff --git a/lib/icl-remote-resource.c b/lib/icl-remote-resource.c index e7cd598..7325587 100644 --- a/lib/icl-remote-resource.c +++ b/lib/icl-remote-resource.c @@ -203,7 +203,7 @@ API void iotcon_remote_resource_destroy(iotcon_remote_resource_h resource) RET_IF(NULL == resource); if (resource->observe_handle) - iotcon_remote_resource_unset_notify_cb(resource); + iotcon_remote_resource_observe_deregister(resource); icl_remote_resource_crud_stop(resource); diff --git a/lib/icl-remote-resource.h b/lib/icl-remote-resource.h index cab7a74..8f9b4af 100644 --- a/lib/icl-remote-resource.h +++ b/lib/icl-remote-resource.h @@ -72,7 +72,7 @@ struct icl_remote_resource { void icl_remote_resource_crud_stop(iotcon_remote_resource_h resource); int icl_remote_resource_observer_start(iotcon_remote_resource_h resource, - iotcon_observe_type_e observe_type, + iotcon_observe_policy_e observe_policy, iotcon_query_h query, GDBusSignalCallback sig_handler, void *cb_container, diff --git a/lib/icl-request.c b/lib/icl-request.c index f8a5ee8..da1794d 100644 --- a/lib/icl-request.c +++ b/lib/icl-request.c @@ -55,12 +55,13 @@ API int iotcon_request_get_representation(iotcon_request_h request, } -API int iotcon_request_get_types(iotcon_request_h request, int *types) +API int iotcon_request_get_request_type(iotcon_request_h request, + iotcon_request_type_e *type) { RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER); - *types = request->types; + *type = request->type; return IOTCON_ERROR_NONE; } @@ -90,13 +91,13 @@ API int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query } -API int iotcon_request_get_observe_action(iotcon_request_h request, - iotcon_observe_action_e *action) +API int iotcon_request_get_observe_type(iotcon_request_h request, + iotcon_observe_type_e *observe_type) { RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == action, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == observe_type, IOTCON_ERROR_INVALID_PARAMETER); - *action = request->observation_info.action; + *observe_type = request->observation_info.action; return IOTCON_ERROR_NONE; } diff --git a/lib/icl-request.h b/lib/icl-request.h index b48b428..c2cba92 100644 --- a/lib/icl-request.h +++ b/lib/icl-request.h @@ -21,14 +21,14 @@ #include "iotcon-constant.h" struct icl_observe_info { - iotcon_observe_action_e action; + iotcon_observe_type_e action; int observe_id; }; struct icl_resource_request { char *host_address; iotcon_connectivity_type_e connectivity_type; - int types; + iotcon_request_type_e type; iotcon_options_h header_options; iotcon_query_h query; struct icl_observe_info observation_info; diff --git a/lib/icl-resource.c b/lib/icl-resource.c index ec8f3ef..d0c2760 100644 --- a/lib/icl-resource.c +++ b/lib/icl-resource.c @@ -57,7 +57,7 @@ static void _icl_request_handler(GDBusConnection *connection, g_variant_get(parameters, "(siia(qs)a(ss)iiavxx)", &request.host_address, &request.connectivity_type, - &request.types, + &request.type, &options, &query, &request.observation_info.action, @@ -111,15 +111,12 @@ static void _icl_request_handler(GDBusConnection *connection, g_variant_iter_free(repr_iter); /* for iotcon_resource_notify */ - if (IOTCON_REQUEST_OBSERVE & request.types) { - int observe_id = request.observation_info.observe_id; - if (IOTCON_OBSERVE_REGISTER == request.observation_info.action) { - if (NULL == resource->observers) - iotcon_observers_create(&resource->observers); - iotcon_observers_add(resource->observers, observe_id); - } else if (IOTCON_OBSERVE_DEREGISTER == request.observation_info.action) { - iotcon_observers_remove(resource->observers, observe_id); - } + if (IOTCON_OBSERVE_REGISTER == request.observation_info.action) { + if (NULL == resource->observers) + iotcon_observers_create(&resource->observers); + iotcon_observers_add(resource->observers, request.observation_info.observe_id); + } else if (IOTCON_OBSERVE_DEREGISTER == request.observation_info.action) { + iotcon_observers_remove(resource->observers, request.observation_info.observe_id); } if (cb) diff --git a/lib/icl-response.h b/lib/icl-response.h index bc07daf..6a67c9b 100644 --- a/lib/icl-response.h +++ b/lib/icl-response.h @@ -28,7 +28,6 @@ typedef enum { struct icl_resource_response { - int seq_number; iotcon_options_h header_options; iotcon_interface_e iface; int result; diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index 9c351b5..0489f5a 100644 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -236,25 +236,25 @@ typedef struct icl_state_s* iotcon_state_h; #define IOTCON_FUNC_CONTINUE true /** - * @brief Enumeration for action of observation. + * @brief Enumeration for type of observation. * * @since_tizen 3.0 */ typedef enum { IOTCON_OBSERVE_REGISTER = 0, /**< Indicates action of registering observation*/ IOTCON_OBSERVE_DEREGISTER = 1, /**< Indicates action of unregistering observation */ - IOTCON_OBSERVE_NO_OPTION = 2 /**< Indicates no option */ -} iotcon_observe_action_e; + IOTCON_OBSERVE_TYPE_NONE = 2, /**< Indicates no option */ +} iotcon_observe_type_e; /** - * @brief Enumeration for type of observation. + * @brief Enumeration for policy of observation. * * @since_tizen 3.0 */ typedef enum { IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER = 0, /**< Indicates observation request for most up-to-date notifications only */ IOTCON_OBSERVE_ACCEPT_OUT_OF_ORDER = 1 /**< Indicates observation request for all notifications including stale notifications */ -} iotcon_observe_type_e; +} iotcon_observe_policy_e; /** * @brief Enumeration for type of interfaces which can be held in a resource. @@ -303,11 +303,11 @@ typedef enum { * @since_tizen 3.0 */ typedef enum { - IOTCON_REQUEST_GET = (1 << 0), /**< Indicates get method of request */ - IOTCON_REQUEST_PUT = (1 << 1), /**< Indicates put method of request */ - IOTCON_REQUEST_POST = (1 << 2), /**< Indicates post method of request */ - IOTCON_REQUEST_DELETE = (1 << 3), /**< Indicates delete method of request */ - IOTCON_REQUEST_OBSERVE = (1 << 4), /**< Indicates observe method of request */ + IOTCON_REQUEST_NONE = 0, /**< Indicates none */ + IOTCON_REQUEST_GET = 1, /**< Indicates get method of request */ + IOTCON_REQUEST_PUT = 2, /**< Indicates put method of request */ + IOTCON_REQUEST_POST = 3, /**< Indicates post method of request */ + IOTCON_REQUEST_DELETE = 4, /**< Indicates delete method of request */ } iotcon_request_type_e; /** diff --git a/lib/include/iotcon-remote-resource.h b/lib/include/iotcon-remote-resource.h index af5b715..8c47ece 100644 --- a/lib/include/iotcon-remote-resource.h +++ b/lib/include/iotcon-remote-resource.h @@ -39,7 +39,7 @@ * @brief Creates a new resource handle. * @details Creates a resource proxy object so that iotcon_remote_resource_get(), * iotcon_remote_resource_put(), iotcon_remote_resource_post(), - * iotcon_remote_resource_delete(), iotcon_remote_resource_set_notify_cb(), + * iotcon_remote_resource_delete(), iotcon_remote_resource_observe_register(), * iotcon_remote_resource_start_caching() and iotcon_remote_resource_start_monitoring() * API can be used without discovering the object in advance.\n * To use this API, you should provide all of the details required to correctly contact and @@ -113,10 +113,9 @@ void iotcon_remote_resource_destroy(iotcon_remote_resource_h resource); */ int iotcon_remote_resource_clone(iotcon_remote_resource_h src, iotcon_remote_resource_h *dest); - /** * @brief Specifies the type of response function. - * @details The function passed to iotcon_remote_resource_set_notify_cb(), + * @details The function passed to iotcon_remote_resource_observe_register(), * iotcon_remote_resource_get(), iotcon_remote_resource_put(), iotcon_remote_resource_post(), * iotcon_remote_resource_delete(). * The @a err could be one of #iotcon_error_e. @@ -124,28 +123,25 @@ int iotcon_remote_resource_clone(iotcon_remote_resource_h src, iotcon_remote_res * * @since_tizen 3.0 * - * @param[in] resource The handle of the resource + * @param[in] resource The handle of the remote resource * @param[in] err The error code - * @param[in] request_type The request type + * @param[in] sequence_number The sequence number of observe * @param[in] response The handle of the response * @param[in] user_data The user data passed from the callback registration function * - * @pre The callback must be registered using iotcon_remote_resource_set_notify_cb(), + * @pre The callback must be registered using iotcon_remote_resource_observe_register(), * iotcon_remote_resource_get(), iotcon_remote_resource_put(), iotcon_remote_resource_post(), * iotcon_remote_resource_delete() * - * @see iotcon_remote_resource_set_notify_cb() + * @see iotcon_remote_resource_observe_register() */ -typedef void (*iotcon_remote_resource_response_cb)(iotcon_remote_resource_h resource, - iotcon_error_e err, - iotcon_request_type_e request_type, - iotcon_response_h response, - void *user_data); +typedef void (*iotcon_remote_resource_observe_cb)(iotcon_remote_resource_h resource, + iotcon_error_e err, int sequence_number, iotcon_response_h response, void *user_data); /** - * @brief Sets notify callback on the resource + * @brief Registers observe callback on the resource * @details When server sends notification message, iotcon_remote_resource_response_cb() will be called. - * The @a observe_type could be one of #iotcon_observe_type_e. + * The @a observe_type could be one of #iotcon_observe_policy_e. * * @since_tizen 3.0 * @privlevel public @@ -168,17 +164,17 @@ typedef void (*iotcon_remote_resource_response_cb)(iotcon_remote_resource_h reso * @post When the @a resource receive notification message, iotcon_remote_resource_response_cb() will be called. * * @see iotcon_remote_resource_response_cb() - * @see iotcon_remote_resource_unset_notify_cb() + * @see iotcon_remote_resource_observe_deregister() * @see iotcon_resource_notify() */ -int iotcon_remote_resource_set_notify_cb(iotcon_remote_resource_h resource, - iotcon_observe_type_e observe_type, +int iotcon_remote_resource_observe_register(iotcon_remote_resource_h resource, + iotcon_observe_policy_e observe_policy, iotcon_query_h query, - iotcon_remote_resource_response_cb cb, + iotcon_remote_resource_observe_cb cb, void *user_data); /** - * @brief Unsets notify callback on the resource + * @brief Deregisters observe callback on the resource * * @since_tizen 3.0 * @privlevel public @@ -194,10 +190,38 @@ int iotcon_remote_resource_set_notify_cb(iotcon_remote_resource_h resource, * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_remote_resource_response_cb() - * @see iotcon_remote_resource_set_notify_cb() + * @see iotcon_remote_resource_observe_register() * @see iotcon_resource_notify() */ -int iotcon_remote_resource_unset_notify_cb(iotcon_remote_resource_h resource); +int iotcon_remote_resource_observe_deregister(iotcon_remote_resource_h resource); + +/** + * @brief Specifies the type of response function. + * @details The function passed to iotcon_remote_resource_observe_register(), + * iotcon_remote_resource_get(), iotcon_remote_resource_put(), iotcon_remote_resource_post(), + * iotcon_remote_resource_delete(). + * The @a err could be one of #iotcon_error_e. + * The @a request_type could be one of #iotcon_request_type_e. + * + * @since_tizen 3.0 + * + * @param[in] resource The handle of the resource + * @param[in] err The error code + * @param[in] request_type The request type + * @param[in] response The handle of the response + * @param[in] user_data The user data passed from the callback registration function + * + * @pre The callback must be registered using iotcon_remote_resource_observe_register(), + * iotcon_remote_resource_get(), iotcon_remote_resource_put(), iotcon_remote_resource_post(), + * iotcon_remote_resource_delete() + * + * @see iotcon_remote_resource_observe_register() + */ +typedef void (*iotcon_remote_resource_response_cb)(iotcon_remote_resource_h resource, + iotcon_error_e err, + iotcon_request_type_e request_type, + iotcon_response_h response, + void *user_data); /** * @brief Gets the attributes of a resource, asynchronously. diff --git a/lib/include/iotcon-request.h b/lib/include/iotcon-request.h index f67f29f..24fd208 100644 --- a/lib/include/iotcon-request.h +++ b/lib/include/iotcon-request.h @@ -50,10 +50,10 @@ * * @see iotcon_request_get_connectivity_type() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ int iotcon_request_get_host_address(iotcon_request_h request, @@ -73,10 +73,10 @@ int iotcon_request_get_host_address(iotcon_request_h request, * * @see iotcon_request_get_host_address() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ int iotcon_request_get_connectivity_type(iotcon_request_h request, @@ -98,23 +98,23 @@ int iotcon_request_get_connectivity_type(iotcon_request_h request, * * @see iotcon_request_get_host_address() * @see iotcon_request_get_connectivity_type() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ int iotcon_request_get_representation(iotcon_request_h request, iotcon_representation_h *repr); /** - * @brief Gets types of the request - * @details @a types could be oring the #iotcon_request_type_e + * @brief Gets type of the request + * @details @a type could be one of the #iotcon_request_type_e * * @since_tizen 3.0 * * @param[in] request The handle of the request - * @param[out] types The types of the request. + * @param[out] type The types of the request. * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful @@ -125,10 +125,10 @@ int iotcon_request_get_representation(iotcon_request_h request, * @see iotcon_request_get_representation() * @see iotcon_request_get_options() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ -int iotcon_request_get_types(iotcon_request_h request, int *types); +int iotcon_request_get_request_type(iotcon_request_h request, iotcon_request_type_e *type); /** * @brief Gets options of the request @@ -147,9 +147,9 @@ int iotcon_request_get_types(iotcon_request_h request, int *types); * @see iotcon_request_get_host_address() * @see iotcon_request_get_connectivity_type() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options); @@ -171,9 +171,9 @@ int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *optio * @see iotcon_request_get_host_address() * @see iotcon_request_get_connectivity_type() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() * @see iotcon_request_get_observe_id() */ int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query); @@ -182,10 +182,10 @@ int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query); * @brief Gets observation action of the request * * @since_tizen 3.0 - * @details The @a action could be one of #iotcon_observe_action_e. + * @details The @a observe_type could be one of #iotcon_observe_type_e. * * @param[in] request The handle of the request - * @param[out] action The observation action of the request + * @param[out] observe_type The observation type of the request * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful @@ -194,13 +194,13 @@ int iotcon_request_get_query(iotcon_request_h request, iotcon_query_h *query); * @see iotcon_request_get_host_address() * @see iotcon_request_get_connectivity_type() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() * @see iotcon_request_get_query() * @see iotcon_request_get_observe_id() */ -int iotcon_request_get_observe_action(iotcon_request_h request, - iotcon_observe_action_e *action); +int iotcon_request_get_observe_type(iotcon_request_h request, + iotcon_observe_type_e *observe_type); /** * @brief Gets observation id of the request @@ -217,10 +217,10 @@ int iotcon_request_get_observe_action(iotcon_request_h request, * @see iotcon_request_get_host_address() * @see iotcon_request_get_connectivity_type() * @see iotcon_request_get_representation() - * @see iotcon_request_get_types() + * @see iotcon_request_get_request_type() * @see iotcon_request_get_options() * @see iotcon_request_get_query() - * @see iotcon_request_get_observe_action() + * @see iotcon_request_get_observe_type() */ int iotcon_request_get_observe_id(iotcon_request_h request, int *observe_id); diff --git a/lib/include/iotcon-resource.h b/lib/include/iotcon-resource.h index b005533..7f43359 100644 --- a/lib/include/iotcon-resource.h +++ b/lib/include/iotcon-resource.h @@ -308,8 +308,8 @@ int iotcon_resource_unbind_child_resource(iotcon_resource_h parent, * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied * * @see iotcon_remote_resource_response_cb() - * @see iotcon_remote_resource_set_notify_cb() - * @see iotcon_remote_resource_unset_notify_cb() + * @see iotcon_remote_resource_observe_register() + * @see iotcon_remote_resource_observe_deregister() * @see iotcon_observers_create() * @see iotcon_observers_destroy() * @see iotcon_observers_add() diff --git a/test/iotcon-test-basic-client.c b/test/iotcon-test-basic-client.c index ee34db4..9676047 100644 --- a/test/iotcon-test-basic-client.c +++ b/test/iotcon-test-basic-client.c @@ -29,8 +29,8 @@ static GList *device_id_list; static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, iotcon_request_type_e request_type, iotcon_response_h response, void *user_data); -static void _on_response_notify(iotcon_remote_resource_h resource, - iotcon_response_h response, void *user_data) +static void _on_observe(iotcon_remote_resource_h resource, iotcon_error_e err, + int sequece_number, iotcon_response_h response, void *user_data) { int ret; bool opened; @@ -81,7 +81,7 @@ static void _on_response_notify(iotcon_remote_resource_h resource, } if (5 == i++) { - iotcon_remote_resource_unset_notify_cb(resource); + iotcon_remote_resource_observe_deregister(resource); iotcon_remote_resource_destroy(resource); } } @@ -424,9 +424,6 @@ static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, case IOTCON_REQUEST_DELETE: _on_response_delete(resource, response, user_data); break; - case IOTCON_REQUEST_OBSERVE: - _on_response_notify(resource, response, user_data); - break; default: ERR("Invalid request type (%d)", request_type); return; @@ -569,11 +566,11 @@ static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e re return; } - /* Set NOTIFY callback */ - ret = iotcon_remote_resource_set_notify_cb(resource_clone, - IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _on_response, NULL); + ret = iotcon_remote_resource_observe_register(resource_clone, IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, + _on_observe, NULL); + if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_remote_resource_set_notify_cb() Fail(%d)", ret); + ERR("iotcon_remote_resource_observe_register() Fail(%d)", ret); device_id_list = g_list_remove(device_id_list, door_resource_device_id); free(door_resource_device_id); return; diff --git a/test/iotcon-test-basic-server.c b/test/iotcon-test-basic-server.c index b235ec7..b8c59b0 100644 --- a/test/iotcon-test-basic-server.c +++ b/test/iotcon-test-basic-server.c @@ -355,7 +355,7 @@ static int _request_handler_post(door_resource_s *door, iotcon_request_h request } new_door_handle = _create_door_resource(DOOR_RESOURCE_URI2, door->type, - IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL); + IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), door); if (NULL == new_door_handle) { ERR("_create_door_resource() Fail"); return -1; @@ -426,45 +426,6 @@ static int _request_handler_delete(iotcon_resource_h resource, iotcon_request_h return 0; } -static int _request_handler_observe(door_resource_s *door, iotcon_request_h request) -{ - int ret; - int observe_id; - iotcon_observe_action_e observe_action; - - ret = iotcon_request_get_observe_action(request, &observe_action); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_action() Fail(%d)", ret); - return -1; - } - - if (IOTCON_OBSERVE_REGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_id() Fail(%d)", ret); - return -1; - } - ret = iotcon_observers_add(door->observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_observers_add() Fail(%d)", ret); - return -1; - } - } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_id() Fail(%d)", ret); - return -1; - } - ret = iotcon_observers_remove(door->observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_observers_remove() Fail(%d)", ret); - return -1; - } - } - - return 0; -} - static bool _query_cb(const char *key, const char *value, void *user_data) { INFO("key : %s, value : %s", key, value); @@ -475,10 +436,12 @@ static bool _query_cb(const char *key, const char *value, void *user_data) static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { - int ret, types; - char *host_address; - iotcon_query_h query; door_resource_s *door; + iotcon_query_h query; + int ret, observe_id; + iotcon_request_type_e type; + iotcon_observe_type_e observe_type; + char *host_address; RET_IF(NULL == request); @@ -499,25 +462,26 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques if (query) iotcon_query_foreach(query, _query_cb, NULL); - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_request_type(request, &type); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR); return; } + door = user_data; - if (IOTCON_REQUEST_GET & types) + if (IOTCON_REQUEST_GET == type) ret = _request_handler_get(door, request); - else if (IOTCON_REQUEST_PUT & types) + else if (IOTCON_REQUEST_PUT == type) ret = _request_handler_put(door, request); - else if (IOTCON_REQUEST_POST & types) + else if (IOTCON_REQUEST_POST == type) ret = _request_handler_post(door, request); - else if (IOTCON_REQUEST_DELETE & types) + else if (IOTCON_REQUEST_DELETE == type) ret = _request_handler_delete(resource, request); if (0 != ret) { @@ -525,8 +489,36 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques return; } - if (IOTCON_REQUEST_OBSERVE & types) - _request_handler_observe(door,request); + ret = iotcon_request_get_observe_type(request, &observe_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observe_type() Fail(%d)", ret); + return; + } + + if (IOTCON_OBSERVE_REGISTER == observe_type) { + ret = iotcon_request_get_observe_id(request, &observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observe_id() Fail(%d)", ret); + return; + } + + ret = iotcon_observers_add(door->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_observers_add() Fail(%d)", ret); + return; + } + } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) { + ret = iotcon_request_get_observe_id(request, &observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observe_id() Fail(%d)", ret); + return; + } + ret = iotcon_observers_remove(door->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_observers_remove() Fail(%d)", ret); + return; + } + } } static gboolean _presence_timer(gpointer user_data) diff --git a/test/iotcon-test-repr-server.c b/test/iotcon-test-repr-server.c index a924150..049b30b 100644 --- a/test/iotcon-test-repr-server.c +++ b/test/iotcon-test-repr-server.c @@ -591,20 +591,21 @@ static int _room_request_handler_get(room_resource_s *room, iotcon_request_h req static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { - int ret, types; + int ret; + iotcon_request_type_e type; light_resource_s *light = user_data; int iface = IOTCON_INTERFACE_DEFAULT; RET_IF(NULL == request); - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_request_type(request, &type); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } - if (IOTCON_REQUEST_GET & types) { + if (IOTCON_REQUEST_GET == type) { ret = _light_request_handler_get(light, request); if (0 != ret) _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); @@ -616,20 +617,21 @@ static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { - int ret, types; + int ret; + iotcon_request_type_e type; fan_resource_s *fan = user_data; int iface = IOTCON_INTERFACE_DEFAULT; RET_IF(NULL == request); - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_request_type(request, &type); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } - if (IOTCON_REQUEST_GET & types) { + if (IOTCON_REQUEST_GET == type) { ret = _fan_request_handler_get(fan, request); if (0 != ret) _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); @@ -642,7 +644,8 @@ static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h r void *user_data) { FN_CALL; - int ret, types; + int ret; + iotcon_request_type_e type; char *host_address; room_resource_s *room = user_data; int iface = IOTCON_INTERFACE_DEFAULT; @@ -657,14 +660,14 @@ static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h r } INFO("host address : %s", host_address); - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_request_type(request, &type); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } - if (IOTCON_REQUEST_GET & types) { + if (IOTCON_REQUEST_GET == type) { ret = _room_request_handler_get(room, request); if (0 != ret) _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);