From b7e2bca6557cf1b01cca6a870750191e2cfdf6e6 Mon Sep 17 00:00:00 2001 From: youngman Date: Tue, 17 Nov 2015 15:26:07 +0900 Subject: [PATCH] Set monitoring/caching time interval, internally. Change-Id: I91aa4284f6d527c02681235ee82efbb2e30cabdb Signed-off-by: youngman --- lib/icl-remote-resource-caching.c | 26 +++++++++------------- lib/icl-remote-resource-monitoring.c | 43 ++++++++++++++++++------------------ lib/icl-remote-resource.c | 30 +++++++++++++++++++++++++ lib/icl-remote-resource.h | 5 ----- lib/include/iotcon-internal.h | 40 +++++++++++++++++++++++++++++++++ lib/include/iotcon-remote-resource.h | 23 +++++++------------ test/iotcon-test-encap-client.c | 4 ++-- 7 files changed, 112 insertions(+), 59 deletions(-) diff --git a/lib/icl-remote-resource-caching.c b/lib/icl-remote-resource-caching.c index 11ef098..11e5a14 100644 --- a/lib/icl-remote-resource-caching.c +++ b/lib/icl-remote-resource-caching.c @@ -20,6 +20,7 @@ #include #include "iotcon.h" +#include "iotcon-internal.h" #include "ic-dbus.h" #include "ic-utils.h" #include "icl.h" @@ -329,16 +330,12 @@ static gboolean _caching_get_timer(gpointer user_data) API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource, - int caching_interval, - iotcon_remote_resource_cached_representation_changed_cb cb, - void *user_data) + iotcon_remote_resource_cached_representation_changed_cb cb, void *user_data) { - int ret; + int ret, time_interval; unsigned int get_timer_id; RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < caching_interval, - IOTCON_ERROR_INVALID_PARAMETER); if (resource->caching_handle) { ERR("Already Start Caching"); @@ -353,14 +350,6 @@ API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource, return IOTCON_ERROR_OUT_OF_MEMORY; } - if (caching_interval <= 0) { - WARN("Because time interval is negative, it sets default time interval.(10 sec)"); - resource->caching_handle->get_timer_interval - = ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL; - } else { - resource->caching_handle->get_timer_interval = caching_interval; - } - _caching_get_timer(resource); /* OBSERVE METHOD */ @@ -376,8 +365,13 @@ API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource, resource->caching_handle->cb = cb; resource->caching_handle->user_data = user_data; - get_timer_id = g_timeout_add_seconds(resource->caching_handle->get_timer_interval, - _caching_get_timer, resource); + ret = iotcon_remote_resource_get_time_interval(&time_interval); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_time_interval() Fail(%d)", ret); + return ret; + } + + get_timer_id = g_timeout_add_seconds(time_interval, _caching_get_timer, resource); resource->caching_handle->get_timer_id = get_timer_id; return IOTCON_ERROR_NONE; diff --git a/lib/icl-remote-resource-monitoring.c b/lib/icl-remote-resource-monitoring.c index 579c1e0..b032ba1 100644 --- a/lib/icl-remote-resource-monitoring.c +++ b/lib/icl-remote-resource-monitoring.c @@ -18,6 +18,7 @@ #include #include "iotcon.h" +#include "iotcon-internal.h" #include "icl.h" #include "icl-remote-resource.h" @@ -75,10 +76,10 @@ static gboolean _monitoring_get_timer(gpointer user_data) static void _monitoring_presence_cb(iotcon_presence_h presence, iotcon_error_e err, iotcon_presence_response_h response, void *user_data) { - int ret; + int ret, time_interval; unsigned int get_timer_id; - iotcon_presence_trigger_e trigger; iotcon_presence_result_e result; + iotcon_presence_trigger_e trigger; iotcon_remote_resource_h resource = user_data; RET_IF(NULL == resource); @@ -102,29 +103,30 @@ static void _monitoring_presence_cb(iotcon_presence_h presence, iotcon_error_e e return; } + ret = iotcon_remote_resource_get_time_interval(&time_interval); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_time_interval() Fail(%d)", ret); + return; + } + g_source_remove(resource->monitoring_handle->get_timer_id); _monitoring_get_timer(resource); - get_timer_id = g_timeout_add_seconds(resource->caching_handle->get_timer_interval, - _monitoring_get_timer, resource); + get_timer_id = g_timeout_add_seconds(time_interval, _monitoring_get_timer, resource); resource->monitoring_handle->get_timer_id = get_timer_id; } API int iotcon_remote_resource_start_monitoring(iotcon_remote_resource_h resource, - int monitoring_interval, - iotcon_remote_resource_state_changed_cb cb, - void *user_data) + iotcon_remote_resource_state_changed_cb cb, void *user_data) { char *host_address; - int ret; - iotcon_connectivity_type_e connectivity_type; + int ret, time_interval; unsigned int get_timer_id; + iotcon_connectivity_type_e connectivity_type; RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < monitoring_interval, - IOTCON_ERROR_INVALID_PARAMETER); if (resource->monitoring_handle) { ERR("Already Start Monitoring"); @@ -139,22 +141,21 @@ API int iotcon_remote_resource_start_monitoring(iotcon_remote_resource_h resourc return IOTCON_ERROR_OUT_OF_MEMORY; } - if (monitoring_interval <= 0) { - WARN("Because time interval is negative, it sets default time interval.(10 sec)"); - resource->monitoring_handle->get_timer_interval - = ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL; - } else { - resource->monitoring_handle->get_timer_interval = monitoring_interval; - } - _monitoring_get_timer(resource); /* GET METHOD (Resource Presence) */ resource->monitoring_handle->cb = cb; resource->monitoring_handle->user_data = user_data; - get_timer_id = g_timeout_add_seconds(resource->monitoring_handle->get_timer_interval, - _monitoring_get_timer, resource); + ret = iotcon_remote_resource_get_time_interval(&time_interval); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_time_interval() Fail(%d)", ret); + free(resource->monitoring_handle); + resource->monitoring_handle = NULL; + return ret; + } + + get_timer_id = g_timeout_add_seconds(time_interval, _monitoring_get_timer, resource); resource->monitoring_handle->get_timer_id = get_timer_id; /* Device Presence */ diff --git a/lib/icl-remote-resource.c b/lib/icl-remote-resource.c index 838b0bf..45f179d 100644 --- a/lib/icl-remote-resource.c +++ b/lib/icl-remote-resource.c @@ -30,6 +30,9 @@ #include "icl-resource-types.h" #include "icl-payload.h" +#define ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL 10 /* 10 sec */ +#define ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL 3600 /* 60 min */ + typedef struct { iotcon_found_resource_cb cb; void *user_data; @@ -37,6 +40,9 @@ typedef struct { int timeout_id; } icl_found_resource_s; +static int icl_remote_resource_time_interval; + + static iotcon_remote_resource_h _icl_remote_resource_from_gvariant(GVariant *payload, iotcon_connectivity_type_e connectivity_type); @@ -472,3 +478,27 @@ static iotcon_remote_resource_h _icl_remote_resource_from_gvariant(GVariant *pay return resource; } + +API int iotcon_remote_resource_get_time_interval(int *time_interval) +{ + RETV_IF(NULL == time_interval, IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == icl_remote_resource_time_interval) + *time_interval = ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL; + else + *time_interval = icl_remote_resource_time_interval; + + return IOTCON_ERROR_NONE; +} + + +API int iotcon_remote_resource_set_time_interval(int time_interval) +{ + RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < time_interval || time_interval <= 0, + IOTCON_ERROR_INVALID_PARAMETER); + + icl_remote_resource_time_interval = time_interval; + + return IOTCON_ERROR_NONE; +} + diff --git a/lib/icl-remote-resource.h b/lib/icl-remote-resource.h index 514c86e..db32585 100644 --- a/lib/icl-remote-resource.h +++ b/lib/icl-remote-resource.h @@ -24,9 +24,6 @@ #include "icl-options.h" -#define ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL 10 /* 10 sec */ -#define ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL 3600 /* 60 min */ - typedef enum { ICL_DEVICE_STATE_ALIVE, ICL_DEVICE_STATE_LOST_SIGNAL, @@ -34,7 +31,6 @@ typedef enum { struct icl_remote_resource_caching { unsigned int get_timer_id; - int get_timer_interval; iotcon_representation_h repr; iotcon_remote_resource_cached_representation_changed_cb cb; void *user_data; @@ -44,7 +40,6 @@ struct icl_remote_resource_caching { struct icl_remote_resource_monitoring { unsigned int get_timer_id; - int get_timer_interval; iotcon_remote_resource_state_e resource_state; iotcon_remote_resource_state_changed_cb cb; void *user_data; diff --git a/lib/include/iotcon-internal.h b/lib/include/iotcon-internal.h index f1997a5..f2af0d4 100644 --- a/lib/include/iotcon-internal.h +++ b/lib/include/iotcon-internal.h @@ -72,6 +72,46 @@ int iotcon_start_presence(unsigned int time_to_live); */ int iotcon_stop_presence(void); +/** + * @brief Gets the time interval of monitoring & caching API of remote resource. + * @details This API get the time interval of iotcon_remote_resource_start_monitoring(), + * and iotcon_remote_resource_start_caching().\n + * The functions operate GET method, every saved time interval. + * Default time interval is 10 seconds. + * + * @since_tizen 3.0 + * + * @param[out] time_interval Seconds for time interval + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * + * @see iotcon_remote_resource_set_time_interval() + * @see iotcon_remote_resource_start_monitoring() + * @see iotcon_remote_resource_start_caching() + */ +int iotcon_remote_resource_get_time_interval(int *time_interval); + +/** + * @brief Sets the time interval of monitoring & caching API of remote resource. + * @details This API set the time interval of iotcon_remote_resource_start_monitoring(), + * and iotcon_remote_resource_start_caching(). + * + * @since_tizen 3.0 + * + * @param[in] time_interval Seconds for time interval (must be in range from 1 to 3600) + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * + * @see iotcon_remote_resource_get_time_interval() + * @see iotcon_remote_resource_start_monitoring() + * @see iotcon_remote_resource_start_caching() + */ +int iotcon_remote_resource_set_time_interval(int time_interval); + #ifdef __cplusplus } #endif diff --git a/lib/include/iotcon-remote-resource.h b/lib/include/iotcon-remote-resource.h index 0f0396f..0b1bf4f 100644 --- a/lib/include/iotcon-remote-resource.h +++ b/lib/include/iotcon-remote-resource.h @@ -425,16 +425,15 @@ typedef void (*iotcon_remote_resource_cached_representation_changed_cb)( /** * @brief Starts caching of a remote resource. * @details Use this function to start caching the resource's attribute.\n - * Default caching time interval is 10 seconds. - * Internally, it operates GET method, periodically, and it observes the remote resource. + * Although, remote resource is not observable, it keeps the representation up-to-date. + * Because It checks whether representation is changed, periodically. + * The default checking interval is 10 seconds, But it may be changed by a administrator. * * @since_tizen 3.0 * @privlevel public * @privilege %http://tizen.org/privilege/internet * * @param[in] resource The handle of the remote resource to be cached - * @param[in] caching_interval Seconds for caching time interval.\n - * If value is 0, then it sets 10 seconds(default caching time). * @param[in] cb The callback function to add into callback list * @param[in] user_data The user data to pass to the callback function * @@ -451,9 +450,7 @@ typedef void (*iotcon_remote_resource_cached_representation_changed_cb)( * @see iotcon_remote_resource_cached_representation_changed_cb() */ int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource, - int caching_interval, - iotcon_remote_resource_cached_representation_changed_cb cb, - void *user_data); + iotcon_remote_resource_cached_representation_changed_cb cb, void *user_data); /** * @brief Stops caching of a remote resource. @@ -496,17 +493,15 @@ typedef void (*iotcon_remote_resource_state_changed_cb)(iotcon_remote_resource_h /** * @brief Starts monitoring of a remote resource. - * @details When remote resource's state are changed, registered callbacks will be called\n - * in turn. Default monitoring time interval is 10 seconds. - * Internally, it operates GET method, periodically, and it subscribes the devices's presence. + * @details When remote resource's state are changed, registered callbacks will be called + * in turn. Internally, it checks the state of resource, periodically. Thus, it may not + * receive the state, immediately. * * @since_tizen 3.0 * @privlevel public * @privilege %http://tizen.org/privilege/internet * * @param[in] resource The handle of the remote resource - * @param[in] monitoring_interval Seconds for monitoring time interval.\n - * If value is 0, then it sets 10 seconds(default monitoring time). * @param[in] cb The callback function to add into callback list * @param[in] user_data The user data to pass to the callback function * @@ -520,9 +515,7 @@ typedef void (*iotcon_remote_resource_state_changed_cb)(iotcon_remote_resource_h * @see iotcon_remote_resource_state_changed_cb() */ int iotcon_remote_resource_start_monitoring(iotcon_remote_resource_h resource, - int monitoring_interval, - iotcon_remote_resource_state_changed_cb cb, - void *user_data); + iotcon_remote_resource_state_changed_cb cb, void *user_data); /** * @brief Stops monitoring of a remote resource. diff --git a/test/iotcon-test-encap-client.c b/test/iotcon-test-encap-client.c index 99b182d..ef9c2ce 100644 --- a/test/iotcon-test-encap-client.c +++ b/test/iotcon-test-encap-client.c @@ -192,7 +192,7 @@ static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e re } /* Start Monitoring */ - ret = iotcon_remote_resource_start_monitoring(cloned_resource, 10, _state_changed_cb, + ret = iotcon_remote_resource_start_monitoring(cloned_resource, _state_changed_cb, NULL); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_remote_resource_start_monitoring() Fail(%d)", ret); @@ -202,7 +202,7 @@ static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e re } /* Start Caching */ - ret = iotcon_remote_resource_start_caching(cloned_resource, 10, + ret = iotcon_remote_resource_start_caching(cloned_resource, _representation_changed_cb, NULL); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_remote_resource_start_caching() Fail(%d)", ret); -- 2.7.4