From ef7284452b2059ee0341e2340a7e001607861124 Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Thu, 14 Nov 2019 18:59:11 +0900 Subject: [PATCH] Change the timestamp value to use the monotonic timer - Problem: real-time timestamp causes various problems. - Cause: If the time zone is changed or the system time is initialized, the wrong timestamp may be generated. - Solution: 1. Change to use the monotonic timer when creating a timestamp. 2. Add new plugin API that enables the BLE plugin can save real-time timestamp. Change-Id: I24c1dbbec39417f6f13ec87dfff8c3239572722c Signed-off-by: saerome.kim --- packaging/ua-manager.spec | 2 +- ua-daemon/include/ua-manager-common.h | 2 ++ ua-daemon/include/ua-manager-core.h | 2 ++ ua-daemon/src/pm/ua-ble-plugin-handler.c | 41 ++++++++++++++++++++++++++++- ua-daemon/src/pm/ua-light-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-motion-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-wifi-plugin-handler.c | 3 ++- ua-daemon/src/ua-manager-common.c | 13 +++++++++ ua-daemon/src/ua-manager-core.c | 30 ++++++--------------- ua-plugins/include/ua-plugin.h | 13 ++++++++- 10 files changed, 84 insertions(+), 28 deletions(-) diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index 7b22a4f..1ad90f1 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -1,6 +1,6 @@ Name: ua-manager Summary: User awareness manager -Version: 0.12.18 +Version: 0.12.19 Release: 1 License: Apache-2.0 Source0: %{name}-%{version}.tar.gz diff --git a/ua-daemon/include/ua-manager-common.h b/ua-daemon/include/ua-manager-common.h index 5e577a2..188df77 100644 --- a/ua-daemon/include/ua-manager-common.h +++ b/ua-daemon/include/ua-manager-common.h @@ -129,6 +129,8 @@ int _uam_deregister_app_info(char *sender, uam_app_info_s app_info); size_t _uam_get_file_size(const char* filename); +unsigned long long _uam_get_timestamp(void); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 1742e67..bc4e7f5 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -235,6 +235,8 @@ int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv); void _uam_core_handle_status_changed(unsigned int sensor, void *info); +void __send_sensor_absence_event(uam_sensor_info_s *sensor_info, unsigned int sensor); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-ble-plugin-handler.c b/ua-daemon/src/pm/ua-ble-plugin-handler.c index da94841..1450f07 100644 --- a/ua-daemon/src/pm/ua-ble-plugin-handler.c +++ b/ua-daemon/src/pm/ua-ble-plugin-handler.c @@ -20,6 +20,7 @@ #include #include +#include #include #include "ua-pm-util.h" @@ -114,6 +115,43 @@ static void ble_device_added_callback(int status, uas_device_info_t *device) FUNC_EXIT; } +void ble_device_update_timestamp_callback( + unsigned long long timestamp, uas_device_info_t *device) +{ + FUNC_ENTRY; + + int ret; + uam_device_info_s *dev_info; + + ret_if(NULL == device); + + dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device); + ret_if(NULL == dev_info); + + dev_info->type = UAM_TECH_TYPE_BLE; + + ret = __uam_db_begin_transaction(); + if (UAM_ERROR_NONE != ret) { + UAM_WARN("_uam_device_db_update_device_timestamp failed"); + g_free(dev_info); + return; + } + + /* Update database (presence state & timestamp) */ + ret = _uam_device_db_update_device_timestamp(dev_info->device_id, + dev_info->type, dev_info->mac, timestamp); + + if (UAM_ERROR_NONE != ret) { + UAM_WARN("_uam_device_db_update_device_timestamp failed"); + __uam_db_end_transaction(0); + } + + __uam_db_end_transaction(1); + g_free(dev_info); + + FUNC_EXIT; +} + uas_callbacks_t ble_cbs = { .state_changed_cb = ble_state_changed_callback, .detection_state_cb = ble_detection_state_changed_cb, @@ -121,5 +159,6 @@ uas_callbacks_t ble_cbs = { .detected_cb = ble_lpm_detection_callback, .device_detected_cb = ble_device_detection_callback, .device_added_cb = ble_device_added_callback, - .active_scan_cb = NULL + .device_active_scan_cb = NULL, + .device_update_timestamp_cb = ble_device_update_timestamp_callback }; diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c index e3742c4..f50ee78 100644 --- a/ua-daemon/src/pm/ua-light-plugin-handler.c +++ b/ua-daemon/src/pm/ua-light-plugin-handler.c @@ -77,5 +77,6 @@ uas_callbacks_t light_cbs = { .detected_cb = light_detection_callback, .device_detected_cb = NULL, .device_added_cb = NULL, - .active_scan_cb = NULL, + .device_active_scan_cb = NULL, + .device_update_timestamp_cb = NULL }; diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c index 14ade6f..4f95b9a 100644 --- a/ua-daemon/src/pm/ua-motion-plugin-handler.c +++ b/ua-daemon/src/pm/ua-motion-plugin-handler.c @@ -100,5 +100,6 @@ uas_callbacks_t motion_cbs = { .detected_cb = motion_detection_callback, .device_detected_cb = NULL, .device_added_cb = NULL, - .active_scan_cb = NULL, + .device_active_scan_cb = NULL, + .device_update_timestamp_cb = NULL }; diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index ee5155c..14ba4e1 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -146,5 +146,6 @@ uas_callbacks_t wifi_cbs = { .detected_cb = wifi_lpm_detection_callback, .device_detected_cb = wifi_device_detection_callback, .device_added_cb = wifi_device_added_callback, - .active_scan_cb = wifi_active_scan_callback + .device_active_scan_cb = wifi_active_scan_callback, + .device_update_timestamp_cb = NULL }; diff --git a/ua-daemon/src/ua-manager-common.c b/ua-daemon/src/ua-manager-common.c index 6b0ec59..a6ba42f 100644 --- a/ua-daemon/src/ua-manager-common.c +++ b/ua-daemon/src/ua-manager-common.c @@ -16,6 +16,7 @@ */ #include +#include #include "ua-api.h" #include "ua-plugin.h" #include "ua-internal.h" @@ -224,3 +225,15 @@ size_t _uam_get_file_size(const char* filename) return size; } + +unsigned long long _uam_get_timestamp(void) +{ + int ret; + struct timespec t; + ret = clock_gettime(CLOCK_MONOTONIC, &t); + if (-1 == ret) { + UAM_ERR("Failed to call clock_gettime [%d]", ret); + return 0; + } + return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000; +} diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index d045bd0..396fefb 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -2446,11 +2446,12 @@ int _uam_core_handle_device_added(int status, int user_id, const uam_device_info_s *dev_info) { FUNC_ENTRY; + GSList *l; int ret = UAM_ERROR_NONE; - uam_db_user_info_t *user = NULL; GSList *svc_list = NULL; - long timestamp; + uam_db_user_info_t *user = NULL; + unsigned long long timestamp; UAM_INFO("[%d]", user_id); @@ -2529,13 +2530,9 @@ int _uam_core_handle_device_added(int status, svc_list = g_slist_append(svc_list, service); } - timestamp = time(NULL); - if (-1 == timestamp) { - UAM_ERR("time() return -1"); - timestamp = 0; - } + timestamp = _uam_get_timestamp(); __uam_core_add_dev_to_list(user, dev_info, - UAM_PRESENCE_STATE_PRESENT, (unsigned long long)timestamp, svc_list); + UAM_PRESENCE_STATE_PRESENT, timestamp, svc_list); retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED); /* Add device to database */ @@ -2721,8 +2718,8 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor, char *device_id) { FUNC_ENTRY; + GSList *l; - long timestamp; uam_db_user_info_t *user; uam_svc_dev_info_t *svc_dev = NULL; @@ -2761,12 +2758,7 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor, if (UAM_DETECT_PRESENCE != mon->mode) continue; - timestamp =time(NULL); - if (-1 == timestamp) { - UAM_ERR("time() return -1"); - user->timestamp = 0; - } else - user->timestamp = (unsigned long long)timestamp; + user->timestamp = _uam_get_timestamp(); UAM_INFO("sensor [%d]", sensor); _uam_manager_send_event(mon->name, @@ -2789,7 +2781,6 @@ int _uam_core_handle_presence_detected(unsigned int sensor, int user_id, void *info) { FUNC_ENTRY; - long timestamp; int ret = UAM_ERROR_NONE; GSList *l; @@ -2847,12 +2838,7 @@ int _uam_core_handle_presence_detected(unsigned int sensor, tech = l->data; tech->presence_state = UAM_PRESENCE_STATE_PRESENT; - - timestamp = time(NULL); - if (-1 == timestamp) - UAM_ERR("time() return -1"); - else - tech->timestamp = (unsigned long long)timestamp; + tech->timestamp = _uam_get_timestamp(); retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER); /* Check if IP address was updated then update in DB */ diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index 6a60f86..e0134dc 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -184,6 +184,16 @@ typedef void (*uas_device_added_callback)(int status, uas_device_info_t *device) typedef void (*uas_device_active_scan_callback)( uas_active_scan_event_e event, const uas_device_info_t *device); +/* + * Callback to be invoked in response to search_active_devices() API + * + * [Param] timestamp - Timestamp to update. + * [Param] device - Device information to update. + */ +typedef void (*uas_device_update_timestamp_callback)(unsigned long long timestamp, + uas_device_info_t *device); + + /* UA plug-in callback structure */ typedef struct { uas_state_changed_callback state_changed_cb; /**< 0:Not ready 1:Ready */ @@ -192,7 +202,8 @@ typedef struct { uas_detection_callback detected_cb; /**< For environmental sensors */ uas_device_detection_callback device_detected_cb; /**< For connectivity sensors */ uas_device_added_callback device_added_cb; /**< For connectivity sensors */ - uas_device_active_scan_callback active_scan_cb; /**< For connectivity sensors */ + uas_device_active_scan_callback device_active_scan_cb; /**< For connectivity sensors */ + uas_device_update_timestamp_callback device_update_timestamp_cb; /**< Update device timestamp */ } uas_callbacks_t; typedef struct { -- 2.7.4