Integrate UA database (persistent) into ua-manager
authorAtul Rai <a.rai@samsung.com>
Thu, 3 Jan 2019 06:54:59 +0000 (12:24 +0530)
committer김새롬/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <saerome.kim@samsung.com>
Thu, 3 Jan 2019 22:54:59 +0000 (07:54 +0900)
Signed-off-by: Atul Rai <a.rai@samsung.com>
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-request-handler.c

index c3b54b0..6568e64 100644 (file)
@@ -480,6 +480,111 @@ static unsigned int __uam_get_active_sensors(uam_pm_detection_mode_e mode)
        return sensors;
 }
 
+static void __uam_core_add_dev_to_list(
+               uam_db_user_info_t *user, const uam_device_info_t *dev_info,
+               int presence_state, long long timestamp)
+{
+       FUNC_ENTRY;
+       uam_db_tech_info_t *tech;
+       uam_db_device_info_t *device;
+       GSList *l;
+
+       ret_if(NULL == dev_info);
+       ret_if(NULL == user);
+
+       l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
+       if (NULL != l) {
+               device = l->data;
+               ret_if(user != device->user);
+               ret_if(device->supported_techs & dev_info->type);
+
+               if (device->os != dev_info->operating_system) {
+                       UAM_INFO("device->os: %d, dev_info->operating_system: %d",
+                                       device->os, dev_info->operating_system);
+                       /* Update device OS type */
+                       if (UAM_OS_TYPE_INVALID == device->os)
+                               device->os = dev_info->operating_system;
+                       else
+                               UAM_WARN("Strange - OS types did not match, need to check");
+               }
+       } else {
+               device = g_new0(uam_db_device_info_t, 1);
+               device->device_id = g_strdup(dev_info->device_id);
+               device->os = dev_info->operating_system;
+               device->user = user;
+
+               /* Add device to global device list */
+               devices = g_slist_append(devices, device);
+
+               /* Add same device to user's device list */
+               user->devices = g_slist_append(user->devices, device);
+       }
+
+       tech = g_new0(uam_db_tech_info_t, 1);
+       tech->tech_type = dev_info->type;
+       tech->presence_state = presence_state;
+       tech->timestamp = timestamp;
+       tech->device = device;
+
+       /* Add tech info to tech list */
+       device->tech_list = g_slist_append(device->tech_list, tech);
+       device->supported_techs |= tech->tech_type;
+       UAM_INFO("device->supported_techs: %8.8X", device->supported_techs);
+
+       if (0 < strlen(dev_info->mac)) {
+               uam_db_address_info_t *addr;
+
+               addr = g_new0(uam_db_address_info_t, 1);
+               addr->address = g_strdup(dev_info->mac);
+
+               switch (dev_info->type) {
+               case UAM_TECH_TYPE_BLE:
+                       addr->addr_type = UAM_ADDR_TYPE_BLE;
+                       break;
+               case UAM_TECH_TYPE_BT:
+                       addr->addr_type = UAM_ADDR_TYPE_BT;
+                       break;
+               case UAM_TECH_TYPE_P2P:
+                       addr->addr_type = UAM_ADDR_TYPE_P2P;
+                       break;
+               case UAM_TECH_TYPE_WIFI:
+                       addr->addr_type = UAM_ADDR_TYPE_WIFI;
+                       break;
+               default:
+                       UAM_ERR("Unknown tech type: %d", dev_info->type);
+                       g_free(addr->address);
+                       g_free(addr);
+                       addr = NULL;
+               }
+
+               if (addr) {
+                       UAM_DBG("MAC address %s added for tech type: %d",
+                                       dev_info->mac, dev_info->type);
+                       tech->addresses = g_slist_append(tech->addresses, addr);
+               }
+       }
+
+       if (0 < strlen(dev_info->ipv4_addr)) {
+               uam_db_address_info_t *addr;
+
+               if (UAM_TECH_TYPE_BLE == dev_info->type ||
+                               UAM_TECH_TYPE_BT == dev_info->type)
+                       UAM_WARN("IPv4 address %s added for tech type: %d",
+                                       dev_info->ipv4_addr, dev_info->type);
+               else
+                       UAM_DBG("IPv4 address %s added for tech type: %d",
+                                       dev_info->ipv4_addr, dev_info->type);
+
+               addr = g_new0(uam_db_address_info_t, 1);
+               addr->addr_type = UAM_ADDR_TYPE_IPv4;
+               addr->address = g_strdup(dev_info->ipv4_addr);
+
+               tech->addresses = g_slist_append(tech->addresses, addr);
+       }
+
+       FUNC_EXIT;
+}
+
 int _uam_core_add_user(const char *name, const char *account)
 {
        FUNC_ENTRY;
@@ -497,14 +602,19 @@ int _uam_core_add_user(const char *name, const char *account)
                return UAM_ERROR_INTERNAL;
        }
 
+       /* Add user to database */
+       if (UAM_ERROR_NONE != _ua_db_insert_user_info(user->user_id, name, account)) {
+               UAM_ERR("_ua_db_insert_user_info failed");
+               g_free(user);
+               return UAM_ERROR_DB_FAILED;
+       }
+
        user->name = g_strdup(name);
        user->account = g_strdup(account);
        user->devices = NULL;
 
        users = g_slist_append(users, user);
 
-       /* TODO : Add user to database */
-
        /* Send user added event to application */
        if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
                                UAM_EVENT_USER_ADDED, g_variant_new("(iss)",
@@ -525,9 +635,13 @@ int _uam_core_remove_user(const char *account)
        retv_if((NULL == l), UAM_ERROR_NOT_REGISTERED);
        user = l->data;
 
-       users = g_slist_remove(users, user);
+       /* Remove user from database */
+       if (UAM_ERROR_NONE != _ua_db_delete_by_user_id(user->user_id)) {
+               UAM_ERR("_ua_db_delete_by_user_id failed");
+               return UAM_ERROR_DB_FAILED;
+       }
 
-       /* TODO : Remove user from database */
+       users = g_slist_remove(users, user);
 
        /* Send user removed event to application */
        if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
@@ -710,7 +824,10 @@ int _uam_core_remove_device(const char *account, const uam_device_info_t *dev_in
        /* Send device removed event to application */
        __send_device_event(UAM_EVENT_DEVICE_REMOVED, dev_info);
 
-       /* TODO : remove from database */
+       /* Remove device from database */
+       if (UAM_ERROR_NONE != _ua_device_db_delete_device_info(
+                               dev_info->device_id, dev_info->type, dev_info->mac))
+               UAM_ERR("_ua_device_db_delete_device_info failed");
 
        /* Remove tech info from device's tech list */
        device->tech_list = g_slist_remove(device->tech_list, tech);
@@ -775,7 +892,10 @@ int _uam_core_remove_device_by_device_id(const char *device_id, int tech_type)
        /* Send device removed event to application */
        __send_device_event(UAM_EVENT_DEVICE_REMOVED, &dev_info);
 
-       /* TODO : remove from database */
+       /* remove from database */
+       if (UAM_ERROR_NONE != _ua_device_db_delete_device_info(
+                               dev_info.device_id, dev_info.type, dev_info.mac))
+               UAM_ERR("_ua_device_db_delete_device_info failed");
 
        /* Remove tech info from device's tech list */
        device->tech_list = g_slist_remove(device->tech_list, tech);
@@ -831,7 +951,10 @@ int _uam_core_remove_device_by_mac(const char *mac)
        /* Send device removed event to application */
        __send_device_event(UAM_EVENT_DEVICE_REMOVED, &dev_info);
 
-       /* TODO : remove from database */
+       /* Remove device from database */
+       if (UAM_ERROR_NONE != _ua_device_db_delete_device_info(
+                               dev_info.device_id, dev_info.type, dev_info.mac))
+               UAM_ERR("_ua_device_db_delete_device_info failed");
 
        /* Remove tech info from device's tech list */
        device->tech_list = g_slist_remove(device->tech_list, tech);
@@ -1302,123 +1425,99 @@ int _uam_core_set_detection_parameters(unsigned int sensor,
 int _uam_core_init(void)
 {
        FUNC_ENTRY;
+       GSList *db_users;
+       GSList *db_devices;
+       GSList *l;
 
-       /* TODO: Init database and fetch user/device list */
+       /* Init database */
+       _ua_db_initialize();
 
-       FUNC_EXIT;
-       return UAM_ERROR_NONE;
-}
+       /* Fetch user list */
+       db_users = _ua_db_get_all_users();
+       if (!db_users) {
+               UAM_INFO_C("No users in database");
+               return UAM_ERROR_NONE;
+       }
 
-void _uam_core_deinit(void)
-{
-       FUNC_ENTRY;
+       for (l = db_users; NULL != l; l = g_slist_next(l)) {
+               db_user_info_t *info = l->data;
+               uam_db_user_info_t *user;
 
-       /* TODO: de-init database */
+               if (!info)
+                       continue;
 
-       FUNC_EXIT;
-}
+               user = g_new0(uam_db_user_info_t, 1);
+               user->user_id = info->user_id;
+               user_id_list[user->user_id] = TRUE;
+               user->name = g_strdup(info->name);
+               user->account = g_strdup(info->account);
+               user->devices = NULL;
 
-static void __uam_core_add_dev_to_db(uam_db_user_info_t *user, const uam_device_info_t *dev_info)
-{
-       FUNC_ENTRY;
-       uam_db_tech_info_t *tech;
-       uam_db_device_info_t *device;
-       GSList *l;
+               users = g_slist_prepend(users, user);
+       }
 
-       ret_if(NULL == dev_info);
-       ret_if(NULL == user);
+       /* Fetch device list */
+       db_devices = _ua_device_db_get_all_devices();
+       if (!db_devices) {
+               UAM_INFO_C("No Devices registered in database");
+               return UAM_ERROR_NONE;
+       }
 
-       l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
-       if (NULL != l) {
-               device = l->data;
-               ret_if(user != device->user);
-               ret_if(device->supported_techs & dev_info->type);
+       for (l = db_devices; NULL != l; l = g_slist_next(l)) {
+               db_device_info_t *db_info = l->data;
+               uam_db_user_info_t *user;
+               GSList *l1;
 
-               if (device->os != dev_info->operating_system) {
-                       UAM_INFO("device->os: %d, dev_info->operating_system: %d",
-                                       device->os, dev_info->operating_system);
-                       /* Update device OS type */
-                       if (UAM_OS_TYPE_INVALID == device->os)
-                               device->os = dev_info->operating_system;
-                       else
-                               UAM_WARN("Strange - OS types did not match, need to check");
-               }
-       } else {
-               device = g_new0(uam_db_device_info_t, 1);
-               device->device_id = g_strdup(dev_info->device_id);
-               device->os = dev_info->operating_system;
-               device->user = user;
+               if (!db_info)
+                       continue;
 
-               /* Add device to global device list */
-               devices = g_slist_append(devices, device);
+               l1 = g_slist_find_custom(users,
+                               &(db_info->user_id), __compare_user_id);
+               if (!l1) {
+                       UAM_ERR("Invalid user Id: %d", db_info->user_id);
+                       continue;
+               }
+               user = l1->data;
 
-               /* Add same device to user's device list */
-               user->devices = g_slist_append(user->devices, device);
+               __uam_core_add_dev_to_list(user, &(db_info->dev_info),
+                               db_info->presence_state, db_info->timestamp);
        }
 
-       tech = g_new0(uam_db_tech_info_t, 1);
-       tech->tech_type = dev_info->type;
-       tech->presence_state = UAM_PRESENCE_STATE_INVALID;
-       tech->timestamp = 0;
-       tech->device = device;
+       g_slist_free_full(db_devices, g_free);
+       g_slist_free_full(db_users, g_free);
 
-       /* Add tech info to tech list */
-       device->tech_list = g_slist_append(device->tech_list, tech);
-       device->supported_techs |= tech->tech_type;
-       UAM_INFO("device->supported_techs: %8.8X", device->supported_techs);
-
-       if (0 < strlen(dev_info->mac)) {
-               uam_db_address_info_t *addr;
-
-               addr = g_new0(uam_db_address_info_t, 1);
-               addr->address = g_strdup(dev_info->mac);
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
 
-               switch (dev_info->type) {
-               case UAM_TECH_TYPE_BLE:
-                       addr->addr_type = UAM_ADDR_TYPE_BLE;
-                       break;
-               case UAM_TECH_TYPE_BT:
-                       addr->addr_type = UAM_ADDR_TYPE_BT;
-                       break;
-               case UAM_TECH_TYPE_P2P:
-                       addr->addr_type = UAM_ADDR_TYPE_P2P;
-                       break;
-               case UAM_TECH_TYPE_WIFI:
-                       addr->addr_type = UAM_ADDR_TYPE_WIFI;
-                       break;
-               default:
-                       UAM_ERR("Unknown tech type: %d", dev_info->type);
-                       g_free(addr->address);
-                       g_free(addr);
-                       addr = NULL;
-               }
+void _uam_core_deinit(void)
+{
+       FUNC_ENTRY;
+       GSList *l;
 
-               if (addr) {
-                       UAM_DBG("MAC address %s added for tech type: %d",
-                                       dev_info->mac, dev_info->type);
-                       tech->addresses = g_slist_append(tech->addresses, addr);
-               }
-       }
+       /* de-init database */
+       _ua_db_deinitialize();
 
-       if (0 < strlen(dev_info->ipv4_addr)) {
-               uam_db_address_info_t *addr;
+       /* Release allocated memory for devices */
+       g_slist_free_full(devices, __free_user_device);
+       devices = NULL;
 
-               if (UAM_TECH_TYPE_BLE == dev_info->type ||
-                               UAM_TECH_TYPE_BT == dev_info->type)
-                       UAM_WARN("IPv4 address %s added for tech type: %d",
-                                       dev_info->ipv4_addr, dev_info->type);
-               else
-                       UAM_DBG("IPv4 address %s added for tech type: %d",
-                                       dev_info->ipv4_addr, dev_info->type);
+       /* Release allocated memory for users */
+       for (l = users; NULL != l; l = g_slist_next(l)) {
+               uam_db_user_info_t *user = l->data;
 
-               addr = g_new0(uam_db_address_info_t, 1);
-               addr->addr_type = UAM_ADDR_TYPE_IPv4;
-               addr->address = g_strdup(dev_info->ipv4_addr);
+               if (!user)
+                       continue;
 
-               tech->addresses = g_slist_append(tech->addresses, addr);
+               __uam_core_release_user_id(user->user_id);
+               g_free(user->name);
+               g_free(user->account);
+               g_free(user);
        }
 
-       /* TODO : Add to database */
+       g_slist_free(users);
+       users = NULL;
+
        FUNC_EXIT;
 }
 
@@ -1479,6 +1578,7 @@ void _uam_core_handle_device_added(int status,
 
        if (UAM_ERROR_NONE == status) {
                uam_db_user_info_t *user;
+               long long timestamp;
 
                if (0 > user_id) {
                        int ret;
@@ -1505,7 +1605,14 @@ void _uam_core_handle_device_added(int status,
                        user = l->data;
                }
 
-               __uam_core_add_dev_to_db(user, dev_info);
+               timestamp = time(NULL);
+               __uam_core_add_dev_to_list(user, dev_info,
+                               UAM_PRESENCE_STATE_PRESENT, timestamp);
+
+               /* Add device to database */
+               if (UAM_ERROR_NONE != _ua_device_db_insert_device_info(user->user_id,
+                                       dev_info, UAM_PRESENCE_STATE_PRESENT, timestamp))
+                       UAM_WARN("Device addition to persistent DB failed");
 
                /* Send device added event to application */
                __send_device_event(UAM_EVENT_DEVICE_ADDED, dev_info);
@@ -1571,7 +1678,48 @@ void _uam_core_handle_presence_detected(unsigned int sensor,
        tech->presence_state = UAM_PRESENCE_STATE_PRESENT;
        tech->timestamp = time(NULL);
 
-       /* TODO: Update database (presence state & timestamp) */
+       /* Check if IP address was updated then update in DB */
+       if (UAM_TECH_TYPE_WIFI == dev_info->type) {
+               uam_db_address_info_t *addr_info = NULL;
+               gboolean is_updated = TRUE;
+
+               for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
+                       uam_db_address_info_t *addr = l->data;
+
+                       if (!addr || (UAM_ADDR_TYPE_IPv4 != addr->addr_type))
+                               continue;
+
+                       if (!strncasecmp(addr->address, dev_info->ipv4_addr,
+                                               UAM_MAC_ADDRESS_STRING_SIZE))
+                               is_updated = FALSE;
+                       else
+                               UAM_DBG("Old IPv4: %s, New IPv4: %s",
+                                               addr->address, dev_info->ipv4_addr);
+                       addr_info = addr;
+                       break;
+               }
+
+               if (is_updated) {
+                       if (!addr_info)
+                               addr_info = g_new0(uam_db_address_info_t, 1);
+                       else
+                               g_free(addr_info->address);
+
+                       addr_info->addr_type = UAM_ADDR_TYPE_IPv4;
+                       addr_info->address = g_strdup(dev_info->ipv4_addr);
+
+                       /* TODO: Update address in DB */
+               }
+       }
+
+       /* Update database (presence state & timestamp) */
+       if (UAM_ERROR_NONE != _ua_device_db_update_device_timestamp(dev_info->device_id,
+                               dev_info->type, dev_info->mac, tech->timestamp))
+               UAM_WARN("_ua_device_db_update_device_timestamp failed");
+
+       if (UAM_ERROR_NONE != _ua_device_db_update_device_presence(dev_info->device_id,
+                               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));
@@ -1678,7 +1826,10 @@ void _uam_core_handle_absence_detected(unsigned int sensor,
 
        tech->presence_state = UAM_PRESENCE_STATE_ABSENT;
 
-       /* TODO: Update database (presence state) */
+       /* Update database (presence state) */
+       if (UAM_ERROR_NONE != _ua_device_db_update_device_presence(dev_info->device_id,
+                               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);
 
index 9c2148d..83d6377 100644 (file)
@@ -110,7 +110,7 @@ static int __uam_manager_sync_request_handler(
 
                account = g_variant_get_string(in_param1, NULL);
                name = g_variant_get_string(in_param2, NULL);
-               UAM_DBG("Email: [%s], Name: [%s]", account, name);
+               UAM_DBG("Account: [%s], Name: [%s]", account, name);
 
                result = _uam_core_add_user(account, name);
                break;
@@ -119,7 +119,7 @@ static int __uam_manager_sync_request_handler(
                const char *account;
 
                account = g_variant_get_string(in_param1, NULL);
-               UAM_DBG("Email: [%s]", account);
+               UAM_DBG("Account: [%s]", account);
 
                result = _uam_core_remove_user(account);
                break;
@@ -129,7 +129,7 @@ static int __uam_manager_sync_request_handler(
                uam_user_info_t user;
 
                account = g_variant_get_string(in_param1, NULL);
-               UAM_DBG("Email: [%s]", account);
+               UAM_DBG("Account: [%s]", account);
 
                result = _uam_core_get_user_by_account(account, &user);
                if (UAM_ERROR_NONE == result)
@@ -200,7 +200,7 @@ static int __uam_manager_sync_request_handler(
                account = g_variant_get_string(in_param1, NULL);
                __uam_manager_copy_params(in_param2,
                                &dev_info, sizeof(uam_device_info_t));
-               UAM_DBG("Email: [%s], Type: %d, MAC: [%s]",
+               UAM_DBG("Account: [%s], Type: %d, MAC: [%s]",
                                account, dev_info.type, dev_info.mac);
 
                result = _uam_core_remove_device(account, &dev_info);
@@ -276,7 +276,7 @@ static int __uam_manager_sync_request_handler(
                uam_device_info_t *devices = NULL;
 
                account = g_variant_get_string(in_param1, NULL);
-               UAM_DBG("Email: [%s]", account);
+               UAM_DBG("Account: [%s]", account);
 
                result = _uam_core_get_user_devices(account, &count, &devices);
                if (UAM_ERROR_NONE == result) {
@@ -431,7 +431,7 @@ static int __uam_manager_async_request_handler(
                account = g_variant_get_string(in_param1, NULL);
                __uam_manager_copy_params(in_param2,
                                &dev_info, sizeof(uam_device_info_t));
-               UAM_DBG("Email: [%s], Type: %d, MAC: [%s]",
+               UAM_DBG("Account: [%s], Type: %d, MAC: [%s]",
                                account, dev_info.type, dev_info.mac);
 
                result = _uam_core_remove_device(account, &dev_info);