diff options
author | Mayank Haarit <mayank.h@samsung.com> | 2019-06-12 10:40:58 +0530 |
---|---|---|
committer | saerome kim <saerome.kim@samsung.com> | 2019-07-01 11:08:29 +0900 |
commit | ddfb70515b5f0c0f975915c7133b37b021ba18e7 (patch) | |
tree | 31e1c5a62d4f4ad67d570258ffa3034acab52d75 | |
parent | 05dd252b1efaa2aa7cb59955dfbecd31ffde9c92 (diff) | |
download | user-awareness-ddfb70515b5f0c0f975915c7133b37b021ba18e7.tar.gz user-awareness-ddfb70515b5f0c0f975915c7133b37b021ba18e7.tar.bz2 user-awareness-ddfb70515b5f0c0f975915c7133b37b021ba18e7.zip |
Handles service base presence/absence events.
This patch includes :
1) Send presence/absence event based on service
2) Remove sensor name from user specific detection callback
3) Modifies start/stop monitoring logic to handle service base
monitoring
Change-Id: I0bb50d879624c26be74150f17d0d405267d87509
Signed-off-by: Mayank Haarit <mayank.h@samsung.com>
-rw-r--r-- | include/user-awareness-private.h | 11 | ||||
-rw-r--r-- | include/user-awareness.h | 4 | ||||
-rw-r--r-- | src/user-awareness-event-handler.c | 36 | ||||
-rw-r--r-- | src/user-awareness-monitors.c | 229 | ||||
-rw-r--r-- | test/uat-init.c | 6 |
5 files changed, 175 insertions, 111 deletions
diff --git a/include/user-awareness-private.h b/include/user-awareness-private.h index e2f2ecf..61ebee9 100644 --- a/include/user-awareness-private.h +++ b/include/user-awareness-private.h @@ -19,6 +19,7 @@ #define __TIZEN_CAPI_NETWORK_USER_AWARENESS_PRIVATE_H__ #include <user-awareness-log.h> +#include <glib.h> #ifdef __cplusplus extern "C" { @@ -113,10 +114,16 @@ extern "C" { ua_presence_detected_cb presence_cb; /**< User presence detection callback */ ua_absence_detected_cb absence_cb; /**< Absence detection callback */ char *service; /**< Service name */ + GSList *user_state; /**< User current state i.e presence or absence */ void *user_data; /**< User data */ } ua_monitor_s; typedef struct { + char *account; + unsigned int sensor_bitmask; + } ua_user_state_info; + + typedef struct { ua_monitor_s *monitor; /**< Monitor handle */ ua_sensor_e type; /**< Bitmask to express the supported sensors */ unsigned short absence_period; /**< Absence period */ @@ -161,14 +168,14 @@ extern "C" { void _ua_monitor_handle_scan_complete(int result); void _ua_monitor_handle_user_presence_detected( - unsigned int bitmask, ua_user_info_s *user_info); + unsigned int bitmask, char *service, char *account); void _ua_monitor_handle_detection_stopped(); void _ua_monitor_handle_sensor_state(unsigned int bitmask, gboolean ready); void _ua_monitor_handle_user_absence_detected( - unsigned int bitmask, ua_user_info_s *user_info); + unsigned int bitmask, char *service, char *account); void _ua_free_ua_user_info_t(ua_user_info_s *user); diff --git a/include/user-awareness.h b/include/user-awareness.h index 25670b6..0a48d91 100644 --- a/include/user-awareness.h +++ b/include/user-awareness.h @@ -335,7 +335,6 @@ typedef void (*ua_absence_detected_cb)( * @param[in] result The result of the requested operation. * @param[in] handle The monitor handle with which absence detection start was invoked. * @param[in] user_handle The user handle to detected user's information. - * @param[in] sensor Sensor type by which absence was detected. \n * In case monitor has more than one sensor and detection mode is not * #UA_DETECT_MODE_ANY_SENSOR, sensor will be set to #UA_SENSOR_MAX before invoking * callback. @@ -346,7 +345,6 @@ typedef void (*ua_absence_detected_cb)( typedef void (*ua_absence_user_detected_cb)( int result, ua_monitor_h handle, - ua_sensor_e sensor, ua_user_h user_handle, void *user_data); @@ -363,7 +361,6 @@ typedef void (*ua_absence_user_detected_cb)( * @param[in] result The result of the requested operation. * @param[in] handle The monitor handle with which presence detection start was invoked. * @param[in] user_handle The user handle to detected user's information. - * @param[in] sensor Sensor type by which presence was detected. \n * In case monitor has more than one sensor and detection mode is not * #UA_DETECT_MODE_ANY_SENSOR, sensor will be set to #UA_SENSOR_MAX before invoking * callback. @@ -374,7 +371,6 @@ typedef void (*ua_absence_user_detected_cb)( typedef void (*ua_presence_user_detected_cb)( int result, ua_monitor_h handle, - ua_sensor_e sensor, ua_user_h user_handle, void *user_data); diff --git a/src/user-awareness-event-handler.c b/src/user-awareness-event-handler.c index b968b47..2ee821c 100644 --- a/src/user-awareness-event-handler.c +++ b/src/user-awareness-event-handler.c @@ -35,43 +35,27 @@ static void __ua_event_handler(int event, uam_event_data_t *event_param, void *u switch (event) { case UAM_EVENT_USER_PRESENCE_DETECTED: { uam_detection_event_data_t *event_data = NULL; - ua_user_info_s* user_info; - ua_user_h user_handle = NULL; event_data = event_param->data; ret_if(NULL == event_data); + ret_if(NULL == event_data->service); + ret_if(NULL == event_data->account); - user_handle = _ua_get_user_handle_by_account(event_data->account); - - if (user_handle) { - user_info = (ua_user_info_s*) user_handle; - _ua_mark_user_info_state_db(user_info->account, - UA_PRESENCE_STATE_ACTIVATE, event_data->sensor_bitmask); - - _ua_monitor_handle_user_presence_detected( - event_data->sensor_bitmask, user_info); - } + _ua_monitor_handle_user_presence_detected( + event_data->sensor_bitmask, event_data->service, event_data->account); break; } case UAM_EVENT_USER_ABSENCE_DETECTED: { uam_detection_event_data_t *event_data = NULL; - ua_user_info_s* user_info; - ua_user_h user_handle = NULL; event_data = event_param->data; ret_if(NULL == event_data); + ret_if(NULL == event_data->service); + ret_if(NULL == event_data->account); - user_handle = _ua_get_user_handle_by_account(event_data->account); - - if (user_handle) { - user_info = (ua_user_info_s*) user_handle; - _ua_mark_user_info_state_db(user_info->account, - UA_PRESENCE_STATE_INACTIVATE, event_data->sensor_bitmask); - - _ua_monitor_handle_user_absence_detected( - event_data->sensor_bitmask, user_info); - } + _ua_monitor_handle_user_absence_detected( + event_data->sensor_bitmask, event_data->service, event_data->account); break; } @@ -80,7 +64,7 @@ static void __ua_event_handler(int event, uam_event_data_t *event_param, void *u ret_if(NULL == sensor_bitmask); - _ua_monitor_handle_user_presence_detected(*sensor_bitmask, NULL); + _ua_monitor_handle_user_presence_detected(*sensor_bitmask, NULL, NULL); break; } case UAM_EVENT_ABSENCE_DETECTED: { @@ -88,7 +72,7 @@ static void __ua_event_handler(int event, uam_event_data_t *event_param, void *u ret_if(NULL == sensor_bitmask); - _ua_monitor_handle_user_absence_detected(*sensor_bitmask, NULL); + _ua_monitor_handle_user_absence_detected(*sensor_bitmask, NULL, NULL); break; } case UAM_EVENT_DETECTION_STARTED: { diff --git a/src/user-awareness-monitors.c b/src/user-awareness-monitors.c index 662850d..4d53bd2 100644 --- a/src/user-awareness-monitors.c +++ b/src/user-awareness-monitors.c @@ -672,15 +672,57 @@ int ua_monitor_stop_absence_detection(ua_monitor_h handle) return UA_ERROR_NONE; } -static void __ua_send_presence_detection() +static void _ua_free_user_state_info_t(gpointer data) +{ + FUNC_ENTRY; + ua_user_state_info *user_state = data; + ret_if(NULL == user_state); + + if (user_state->account) + g_free(user_state->account); + + g_free(user_state); + FUNC_EXIT; +} + +static void __ua_user_state_clean() { FUNC_ENTRY; GSList *l; - GSList *l1; - GSList *user_list; - unsigned int bitmask; - user_list = _ua_user_get_users(); + for (l = ua_monitor_list; NULL != l; l = g_slist_next(l)) { + ua_monitor_s *monitor = l->data; + + if (!monitor) + continue; + + g_slist_free_full(monitor->user_state, _ua_free_user_state_info_t); + } + FUNC_EXIT; +} + +static bool _ua_check_all_users_absence_state(GSList *user_state, unsigned int bitmask) +{ + FUNC_ENTRY; + GSList *l; + ua_user_state_info *u; + + for (l = user_state; l; l = g_slist_next(l)) { + u = (ua_user_state_info *)l->data; + + if (u->sensor_bitmask & bitmask) { + UA_INFO("User found [%s]", u->account); + return false; + } + } + + return true; +} + +static void __ua_send_presence_detection() +{ + FUNC_ENTRY; + GSList *l; for (l = ua_monitor_list; NULL != l; l = g_slist_next(l)) { ua_monitor_s *monitor = l->data; @@ -702,44 +744,73 @@ static void __ua_send_presence_detection() } monitor->presence_detected_bitmask = 0; + } - if (!monitor->presence_user_cb.callback) - continue; - - /* - * As MOTION and LIGHT sensors do not support user info, If only MOTION and LIGHT - * sensors are registered, do not invoke user absence callback. - */ - if (monitor->sensor_bitmask == (UA_SENSOR_MOTION | UA_SENSOR_LIGHT)) - continue; + FUNC_EXIT; +} - bitmask = monitor->sensor_bitmask; - bitmask &= ~(UA_SENSOR_MOTION | UA_SENSOR_LIGHT); +static ua_user_state_info* __ua_monitor_user_state_create(char *account) +{ + ua_user_state_info *user_state; - for (l1 = user_list; NULL != l1; l1 = g_slist_next(l1)) { - ua_user_info_s *user_info = l1->data; + user_state = g_malloc0(sizeof(ua_user_state_info)); - UA_INFO("Scanning user list..."); - if (!user_info) - continue; + if (!user_state) { + UA_ERR("g_malloc0 failed"); + return NULL; + } + user_state->account = g_strdup(account); - if ((bitmask & ~(user_info->sensor_bitmask)) == 0) - ((ua_presence_user_detected_cb)monitor->presence_user_cb.callback)( - UA_ERROR_NONE, monitor, UA_SENSOR_MAX, user_info->user_handle, - monitor->presence_user_cb.user_data); - } + if (user_state->account == NULL) { + UA_ERR("g_malloc0 failed"); + _ua_free_user_state_info_t((gpointer)user_state); + return NULL; } - FUNC_EXIT; + return user_state; } -static void __ua_sensor_presence_detected(ua_monitor_s *monitor, unsigned int bitmask, ua_user_info_s *user_info) +static void __ua_sensor_presence_detected(ua_monitor_s *monitor, unsigned int bitmask, char *account) { FUNC_ENTRY; ua_sensor_e sensor_type; sensor_type = __ua_sensor_bitmask_to_type(bitmask); + GSList *l; + int found = 0; + ua_user_state_info *user_state; ret_if(NULL == monitor); + if (account) { + for (l = monitor->user_state; l; l = g_slist_next(l)) { + user_state = (ua_user_state_info *)l->data; + + if (!g_strcmp0(account, user_state->account)) { + found = 1; + break; + } + } + + if (!found) { + user_state = __ua_monitor_user_state_create(account); + + if (!user_state) + return; + + monitor->user_state = g_slist_append(monitor->user_state, user_state); + } + + user_state->sensor_bitmask |= bitmask; + + if (monitor->presence_user_cb.callback) { + ua_user_h user_handle = _ua_get_user_handle_by_account(user_state->account); + + if (user_handle) + ((ua_presence_user_detected_cb)monitor->presence_user_cb.callback)( + UA_ERROR_NONE, monitor, user_handle, + monitor->presence_user_cb.user_data); + } + } + switch (monitor->presence_mode) { case UA_DETECT_MODE_ALL_SENSOR: /* @@ -757,10 +828,6 @@ static void __ua_sensor_presence_detected(ua_monitor_s *monitor, unsigned int bi break; case UA_DETECT_MODE_ANY_SENSOR: - if (user_info && monitor->presence_user_cb.callback) - ((ua_presence_user_detected_cb)monitor->presence_user_cb.callback)( - UA_ERROR_NONE, monitor, sensor_type, user_info->user_handle, - monitor->presence_user_cb.user_data); if ((monitor->presence_detected_bitmask & bitmask) == 0) monitor->presence_cb(UA_ERROR_NONE, monitor, @@ -783,9 +850,6 @@ static void __ua_send_absence_detection() FUNC_ENTRY; GSList *l; GSList *l1; - GSList *user_list; - - user_list = _ua_user_get_users(); for (l = ua_monitor_list; NULL != l; l = g_slist_next(l)) { ua_monitor_s *monitor = l->data; @@ -793,14 +857,10 @@ static void __ua_send_absence_detection() if (!monitor) continue; - if (monitor->absence_mode == UA_DETECT_MODE_ANY_SENSOR) { - monitor->absence_detected_bitmask = 0; - continue; - } - UA_INFO("monitor->sensor_bitmask: 0x%8.8X, monitor->absence_detected_bitmask: 0x%8.8X", monitor->sensor_bitmask, monitor->absence_detected_bitmask); - if (monitor->sensor_bitmask == monitor->absence_detected_bitmask) { + if ((monitor->absence_mode != UA_DETECT_MODE_ANY_SENSOR) && + (monitor->sensor_bitmask == monitor->absence_detected_bitmask)) { if (monitor->absence_cb) monitor->absence_cb(UA_ERROR_NONE, monitor, UA_SENSOR_MAX, monitor->user_data); @@ -811,53 +871,71 @@ static void __ua_send_absence_detection() if (!monitor->absence_user_cb.callback) continue; - /* - * As MOTION and LIGHT sensors do not support user info, If only MOTION and LIGHT - * sensors are registered, do not invoke user absence callback. - */ - if (monitor->sensor_bitmask == (UA_SENSOR_MOTION | UA_SENSOR_LIGHT)) - continue; - - for (l1 = user_list; NULL != l1; l1 = g_slist_next(l1)) { - ua_user_info_s *user_info = l1->data; + for (l1 = monitor->user_state; NULL != l1; l1 = g_slist_next(l1)) { + ua_user_state_info *user_state = l1->data; UA_INFO("Scanning user list..."); - if (!user_info || (monitor->sensor_bitmask & user_info->sensor_bitmask)) + if (!user_state) continue; - ((ua_absence_user_detected_cb)monitor->absence_user_cb.callback)( - UA_ERROR_NONE, monitor, UA_SENSOR_MAX, user_info->user_handle, - monitor->absence_user_cb.user_data); + if (!user_state->sensor_bitmask) { + ua_user_h user_handle = _ua_get_user_handle_by_account(user_state->account); + + if (user_handle) + ((ua_absence_user_detected_cb)monitor->absence_user_cb.callback)( + UA_ERROR_NONE, monitor, user_handle, + monitor->absence_user_cb.user_data); + } } } FUNC_EXIT; } -static void __ua_sensor_absence_detected(ua_monitor_s *monitor, unsigned int bitmask, ua_user_info_s *user_info) +static void __ua_sensor_absence_detected(ua_monitor_s *monitor, unsigned int bitmask, char *account) { FUNC_ENTRY; ua_sensor_e sensor_type; bool all_absence; + GSList *l; + int found = 0; + ua_user_state_info *user_state = NULL; sensor_type = __ua_sensor_bitmask_to_type(bitmask); ret_if(NULL == monitor); + if (account) { + for (l = monitor->user_state; l; l = g_slist_next(l)) { + user_state = (ua_user_state_info *)l->data; + + if (!g_strcmp0(account, user_state->account)) { + found = 1; + break; + } + } + + if (!found) { + user_state = __ua_monitor_user_state_create(account); + + if (!user_state) + return; + + monitor->user_state = g_slist_append(monitor->user_state, user_state); + } + + user_state->sensor_bitmask &= ~bitmask; + } + switch (monitor->absence_mode) { case UA_DETECT_MODE_ALL_SENSOR: - all_absence = _ua_check_all_users_absence_any(bitmask); + all_absence = _ua_check_all_users_absence_state(monitor->user_state, bitmask); if (all_absence) monitor->absence_detected_bitmask |= bitmask; break; case UA_DETECT_MODE_ANY_SENSOR: /* First add user info to local users list and then invoke callback */ - if (user_info) { - if (monitor->absence_user_cb.callback) - ((ua_absence_user_detected_cb)monitor->absence_user_cb.callback)( - UA_ERROR_NONE, monitor, sensor_type, user_info->user_handle, - monitor->absence_user_cb.user_data); - - all_absence = _ua_check_all_users_absence_any(bitmask); + if (account) { + all_absence = _ua_check_all_users_absence_state(monitor->user_state, bitmask); if (all_absence) { if (monitor->absence_cb) @@ -899,11 +977,11 @@ void _ua_monitor_handle_scanned_device(int result, uam_device_info_t *uam_info) if (monitor && monitor->scan_device_cb) { ((ua_scan_completed_cb)monitor->scan_device_cb)( - UA_ACTIVE_SCAN_TYPE_DEVICE_FOUND, monitor, dev, monitor->user_data); + UA_ACTIVE_SCAN_TYPE_DEVICE_FOUND, monitor, dev, monitor->user_data); UA_PRINT_DEVICE_HANDLE(dev); UA_INFO("Invoked scan_device_cb callback"); } else { - UA_WARN("Unexpected, scan started but callbacks are NULL"); + UA_WARN("Unexpected, scan started but callbacks are NULL"); } _ua_free_ua_device_info_t((gpointer)dev); @@ -921,7 +999,7 @@ void _ua_monitor_handle_scan_complete(int result) if (monitor && monitor->scan_device_cb) { ((ua_scan_completed_cb)monitor->scan_device_cb)( - UA_ACTIVE_SCAN_TYPE_COMPLETED, monitor, NULL, monitor->user_data); + UA_ACTIVE_SCAN_TYPE_COMPLETED, monitor, NULL, monitor->user_data); UA_INFO("Invoked scan_device_cb callback"); /* Reset callback when scan complete*/ @@ -930,13 +1008,13 @@ void _ua_monitor_handle_scan_complete(int result) monitor->scan_device_started = FALSE; scanning_monitor = NULL; } else { - UA_WARN("Unexpected, scan running but callbacks are NULL"); + UA_WARN("Unexpected, scan running but callbacks are NULL"); } FUNC_EXIT; } -void _ua_monitor_handle_user_presence_detected(unsigned int bitmask, ua_user_info_s *user_info) +void _ua_monitor_handle_user_presence_detected(unsigned int bitmask, char *service, char *account) { FUNC_ENTRY; GSList *l; @@ -950,18 +1028,16 @@ void _ua_monitor_handle_user_presence_detected(unsigned int bitmask, ua_user_inf if (0 == (bitmask & monitor->sensor_bitmask)) continue; - if (monitor->presence_cb) { + if (!service || (service && !g_strcmp0(service, monitor->service))) { /* Presence detection ongoing */ - __ua_sensor_presence_detected(monitor, bitmask, user_info); - } else { - UA_WARN("Unexpected, detection started but callbacks are NULL"); + __ua_sensor_presence_detected(monitor, bitmask, account); } } FUNC_EXIT; } -void _ua_monitor_handle_user_absence_detected(unsigned int bitmask, ua_user_info_s *user_info) +void _ua_monitor_handle_user_absence_detected(unsigned int bitmask, char *service, char *account) { FUNC_ENTRY; GSList *l; @@ -975,8 +1051,10 @@ void _ua_monitor_handle_user_absence_detected(unsigned int bitmask, ua_user_info if (0 == (bitmask & monitor->sensor_bitmask)) continue; - /* Absence detection ongoing */ - __ua_sensor_absence_detected(monitor, bitmask, user_info); + if (!service || (service && !g_strcmp0(service, monitor->service))) { + /* Absence detection ongoing */ + __ua_sensor_absence_detected(monitor, bitmask, account); + } } FUNC_EXIT; @@ -988,6 +1066,7 @@ void _ua_monitor_handle_detection_stopped() __ua_send_presence_detection(); __ua_send_absence_detection(); + __ua_user_state_clean(); FUNC_EXIT; } diff --git a/test/uat-init.c b/test/uat-init.c index 98d3488..0b112f4 100644 --- a/test/uat-init.c +++ b/test/uat-init.c @@ -35,13 +35,12 @@ static char app_id[MENU_DATA_SIZE + 1] = {0,}; static char uid[MENU_DATA_SIZE + 1] = {0,}; static void __absence_detected_all_cb(int result, ua_monitor_h monitor, - ua_sensor_e sensor, ua_user_h user_handle, void *user_data) + ua_user_h user_handle, void *user_data) { int ret = UA_ERROR_NONE; char *account = NULL; msgp("result : %s", uat_get_error_str(result)); - msgp("sensor %s ABSENCE detected", uat_get_sensor_bitmask_str(sensor)); if (user_handle) { ret = ua_user_get_account(user_handle, &account); @@ -53,13 +52,12 @@ static void __absence_detected_all_cb(int result, ua_monitor_h monitor, } static void __presence_detected_all_cb(int result, ua_monitor_h monitor, - ua_sensor_e sensor, ua_user_h user_handle, void *user_data) + ua_user_h user_handle, void *user_data) { int ret = UA_ERROR_NONE; char *account = NULL; msgp("result : %s", uat_get_error_str(result)); - msgp("sensor %s PRESENCE detected", uat_get_sensor_bitmask_str(sensor)); if (user_handle) { ret = ua_user_get_account(user_handle, &account); |