Modify PRESENCE/ANSENCE event handling 38/208838/1
authorAtul Rai <a.rai@samsung.com>
Tue, 11 Jun 2019 09:17:27 +0000 (14:47 +0530)
committersaerome kim <saerome.kim@samsung.com>
Mon, 1 Jul 2019 01:48:55 +0000 (10:48 +0900)
This patch modifies user PRESENCE/ABSENCE event handling to
incorporate service based event sending.

Change-Id: I1e89373afdb99d1347b926262c76352d879dd1f6
Signed-off-by: Atul Rai <a.rai@samsung.com>
ua-daemon/include/ua-manager-core.h
ua-daemon/src/ua-manager-core.c

index a32d328..c57f13b 100644 (file)
@@ -44,6 +44,7 @@ typedef struct {
        unsigned int cycle;
        unsigned int remaining_time;
        GSList *monitors;
+       GSList *dev_techs;
 } uam_db_service_info_t;
 
 typedef struct {
index 9b02b61..d9f0ee0 100644 (file)
@@ -184,6 +184,34 @@ static void __send_device_event(int event, const uam_device_info_t *dev_info)
        FUNC_EXIT;
 }
 
+static void __add_service_to_dev_tech_mapping(
+               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+{
+       FUNC_ENTRY;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == service);
+
+       tech->services = g_slist_append(tech->services, service);
+       service->dev_techs = g_slist_append(service->dev_techs, tech);
+
+       FUNC_EXIT;
+}
+
+static void __remove_service_to_dev_tech_mapping(
+               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+{
+       FUNC_ENTRY;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == service);
+
+       tech->services = g_slist_remove(tech->services, service);
+       service->dev_techs = g_slist_remove(service->dev_techs, tech);
+
+       FUNC_EXIT;
+}
+
 static void __remove_user_device(gpointer data)
 {
        FUNC_ENTRY;
@@ -579,6 +607,16 @@ static void __uam_core_add_dev_to_list(
        tech->timestamp = timestamp;
        tech->device = device;
 
+       tech->services = svc_list;
+       for (l = svc_list; NULL != l; l = g_slist_next(l)) {
+               uam_db_service_info_t *service = l->data;
+
+               if (!service)
+                       continue;
+
+               service->dev_techs = g_slist_prepend(service->dev_techs, tech);
+       }
+
        /* Add tech info to tech list */
        device->tech_list = g_slist_append(device->tech_list, tech);
        device->supported_techs |= tech->tech_type;
@@ -1291,7 +1329,8 @@ int _uam_core_service_add_user(const char *svc_name, const char *account)
                        if (NULL != l2)
                                continue;
 
-                       tech->services = g_slist_append(tech->services, service);
+                       __add_service_to_dev_tech_mapping(tech, service);
+
                        /*
                         * TODO: insert device service info to db
                         * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device->device_id,
@@ -1343,7 +1382,9 @@ int _uam_core_service_remove_user(const char *svc_name, const char *account)
                                continue;
 
                        UAM_DBG("Service %s found, remove it from list", svc_name);
-                       tech->services = g_slist_remove(tech->services, l2->data);
+                       __remove_service_to_dev_tech_mapping(
+                                       tech, (uam_db_service_info_t *)l2->data);
+
                        /*
                         * TODO: Remove service-device from DB
                         * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info(
@@ -1427,7 +1468,8 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, in
        } else
                service = l->data;
 
-       tech_info->services = g_slist_append(tech_info->services, service);
+       __add_service_to_dev_tech_mapping(tech_info, service);
+
        /*
         * TODO: insert device service info to db
         * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device_id,
@@ -1460,7 +1502,8 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id,
        l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name);
        retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED);
 
-       tech_info->services = g_slist_remove(tech_info->services, l->data);
+       __remove_service_to_dev_tech_mapping(tech_info, (uam_db_service_info_t *)l->data);
+
        /*
         * TODO: Remove service-device from DB
         * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info(
@@ -2089,6 +2132,48 @@ void _uam_core_handle_device_added(int status,
        FUNC_EXIT;
 }
 
+void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor)
+{
+       FUNC_ENTRY;
+       uam_db_user_info_t *user;
+       GSList *l;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == tech->services);
+
+       user = tech->device->user;
+       for (l = tech->services; NULL != l; l = g_slist_next(l)) {
+               uam_db_service_info_t *svc = l->data;
+               GSList *l1;
+
+               if (!svc || !svc->monitors)
+                       continue;
+
+               for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
+                       uam_monitor_info_t *mon = l1->data;
+
+                       if (!mon)
+                               continue;
+
+                       if (!(mon->sensors & sensor))
+                               continue;
+
+                       if (UAM_DETECT_PRESENCE != mon->mode)
+                               continue;
+
+                       _uam_manager_send_event(mon->name,
+                                       UAM_EVENT_USER_PRESENCE_DETECTED,
+                                       g_variant_new("(uss)", sensor,
+                                               user->account, svc->name));
+                       UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED to %s"
+                                       " for 0x%8.8X, User: %s Service: %s",
+                                       mon->name, sensor, user->account, svc->name);
+               }
+       }
+
+       FUNC_EXIT;
+}
+
 void _uam_core_handle_presence_detected(unsigned int sensor,
                int user_id, uam_device_info_t *dev_info)
 {
@@ -2189,55 +2274,86 @@ void _uam_core_handle_presence_detected(unsigned int sensor,
                                dev_info->type, dev_info->mac, tech->presence_state))
                UAM_WARN("_ua_device_db_update_device_presence failed");
 
-       _uam_manager_send_event(NULL, UAM_EVENT_USER_PRESENCE_DETECTED,
-                       g_variant_new("(us)", sensor, user->account));
-       UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED for 0x%8.8X, User: %s",
-                       sensor, user->account);
+       __send_user_presence_event(tech, sensor);
 
        FUNC_EXIT;
 }
 
-static void __send_user_absence_event(uam_db_user_info_t *user,
-               uam_tech_type_e type, unsigned int sensor)
+static void __send_user_absence_event(uam_tech_type_e type, unsigned int sensor)
 {
        FUNC_ENTRY;
-       gboolean is_present = FALSE;
        GSList *l;
 
        /*
-        * If none of the user's devices which support the tech_type, is PRESENT,
-        * send user ABSENCE event (User absent for tech_type/sensor).
+        * For each service, find users absent on given sensor. Then for each
+        * monitor in serivce's monitor list, if it is monitoring ABSENCE on
+        * given sensor, send user ABSENCE event.
         */
-       for (l = user->devices; NULL != l; l = g_slist_next(l)) {
-               uam_db_device_info_t *device = l->data;
+       for (l = services; NULL != l; l = g_slist_next(l)) {
+               uam_db_service_info_t *svc = l->data;
+               GSList *absent_users = NULL;
+               GSList *present_users = NULL;
                GSList *l1;
+               GSList *l2;
 
-               if (!device)
-                       continue;
-
-               if (!(device->supported_techs & type))
+               if (!svc || !svc->monitors || !svc->dev_techs)
                        continue;
 
-               for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
-                       uam_db_tech_info_t *tech = l1->data;
+               for (l1 = svc->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
+                       uam_db_tech_info_t *tech = l->data;
 
-                       if (!tech || tech->tech_type != type)
+                       if (!tech || (tech->tech_type != type))
                                continue;
 
+                       l2 = g_slist_find_custom(present_users,
+                                       &(tech->device->user->user_id), __compare_user_id);
                        if (UAM_PRESENCE_STATE_PRESENT == tech->presence_state) {
-                               is_present = TRUE;
-                               break;
+                               /* Remove user from absent list */
+                               absent_users = g_slist_remove(absent_users, tech->device->user);
+
+                               /* If user not in present list, add user to the list */
+                               if (!l2)
+                                       present_users = g_slist_prepend(present_users, tech->device->user);
+                       } else {
+                               /* If user not in the present list then only add it to absent list */
+                               if (!l2)
+                                       absent_users = g_slist_prepend(absent_users, tech->device->user);
                        }
                }
 
-               if (is_present)
-                       break;
-       }
+               g_slist_free(present_users);
+               if (!absent_users)
+                       continue;
+
+               for (l2 = svc->monitors; NULL != l2; l2 = g_slist_next(l2)) {
+                       uam_monitor_info_t *mon = l2->data;
+
+                       if (!mon)
+                               continue;
+
+                       if (!(mon->sensors & sensor))
+                               continue;
+
+                       if (UAM_DETECT_ABSENCE != mon->mode)
+                               continue;
+
+                       for (l1 = absent_users; NULL != l1; l1 = g_slist_next(l1)) {
+                               uam_db_user_info_t *user = l1->data;
+
+                               if (!user)
+                                       continue;
 
-       if (!is_present) {
-               _uam_manager_send_event(NULL, UAM_EVENT_USER_ABSENCE_DETECTED,
-                               g_variant_new("(us)", sensor, user->account));
-               UAM_DBG("Sent UAM_EVENT_USER_ABSENCE_DETECTED for 0x%8.8X", sensor);
+                               _uam_manager_send_event(mon->name,
+                                               UAM_EVENT_USER_PRESENCE_DETECTED,
+                                               g_variant_new("(uss)", sensor,
+                                                       user->account, svc->name));
+                               UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED to %s"
+                                               " for 0x%8.8X, User: %s Service: %s",
+                                               mon->name, sensor, user->account, svc->name);
+                       }
+               }
+
+               g_slist_free(absent_users);
        }
 
        FUNC_EXIT;
@@ -2300,8 +2416,6 @@ void _uam_core_handle_absence_detected(unsigned int sensor,
                                dev_info->type, dev_info->mac, tech->presence_state))
                UAM_WARN("_ua_device_db_update_device_presence failed");
 
-       //__send_user_absence_event(user, dev_info->type, sensor);
-
        FUNC_EXIT;
 }
 
@@ -2400,17 +2514,8 @@ void _uam_core_handle_detection_stopped(unsigned int sensor)
        else if (UAM_SENSOR_BITMASK_WIFI == sensor)
                type = UAM_TECH_TYPE_WIFI;
 
-       if (UAM_TECH_TYPE_NONE != type) {
-               GSList *l;
-
-               for (l = users; NULL != l; l = g_slist_next(l)) {
-                       uam_db_user_info_t *user = l->data;
-                       if (!user)
-                               continue;
-
-                       __send_user_absence_event(user, type, sensor);
-               }
-       }
+       if (UAM_TECH_TYPE_NONE != type)
+               __send_user_absence_event(type, sensor);
 
        if (0 == detecting_sensors)
                /* Send detection stopped event */