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 /src/user-awareness-monitors.c | |
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>
Diffstat (limited to 'src/user-awareness-monitors.c')
-rw-r--r-- | src/user-awareness-monitors.c | 229 |
1 files changed, 154 insertions, 75 deletions
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; } |