Fix the issue that MAC cannot be updated when adding a device to broadcast MAC
authorsaerome.kim <saerome.kim@samsung.com>
Thu, 5 Dec 2019 05:38:06 +0000 (14:38 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Thu, 5 Dec 2019 10:30:06 +0000 (19:30 +0900)
- Problem: The actual MAC is not applied even if the ARP response is received
  from the device added with broadcast MAC.
- Cause: MAC is managed as a unique key.
- Solution: Modified so that MAC can be changed.

Change-Id: I00483fc70a938b8b63fe0905a3b452f8991a03a6
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
packaging/ua-manager.spec
ua-daemon/data/ua_db.sql
ua-daemon/include/ua-manager-database.h
ua-daemon/src/pm/ua-cloud-plugin-handler.c
ua-daemon/src/pm/ua-wifi-plugin-handler.c
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-device-db.c

index 81f2fe6..59bc4f6 100644 (file)
@@ -1,6 +1,6 @@
 Name:       ua-manager
 Summary:    User awareness manager
-Version:    0.13.11
+Version:    0.13.12
 Release:    1
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
index d5bf3ba..22cefee 100644 (file)
@@ -41,7 +41,7 @@ CREATE TABLE IF NOT EXISTS devices (
   duid TEXT,
   device_icon TEXT,
   FOREIGN KEY(user_id) REFERENCES userdata(user_id),
-  UNIQUE (device_id, tech_type, address)
+  UNIQUE (device_id, tech_type)
 );
 
 CREATE TABLE IF NOT EXISTS services (
index 9f1d6ac..7c08057 100644 (file)
@@ -139,7 +139,9 @@ int _uam_device_db_update_device_last_seen(
 int _uam_device_db_update_device_presence(
        char *device_id, int tech_type, char *address, int presence_state);
 int _uam_device_db_update_device_ip_address(
-       char *device_id, int tech_type, char *address, char *ip_address);
+       char *device_id, int tech_type, char *ip_address);
+int _uam_device_db_update_device_mac_address(
+       char *device_id, int tech_type, char *address);
 int _uam_device_db_update_device_device(char *device_id, int tech_type,
        char *address, char *ip, char os_type, char discriminant, uam_ble_payload_s payload);
 
index f1de902..d4bf9a8 100644 (file)
@@ -282,7 +282,7 @@ static int __load_cloud_plugin(void)
 
        dir = g_dir_open(CLOUD_PLUGIN_DIR, 0, NULL);
        if (!dir) {
-               UAM_ERR("Unable to open directory %s", CLOUD_PLUGIN_DIR);
+               UAM_WARN("Unable to open directory %s", CLOUD_PLUGIN_DIR);
                return UAM_ERROR_INTERNAL;
        }
 
index b81fa40..297075d 100644 (file)
@@ -78,6 +78,8 @@ static void wifi_device_detection_callback(uas_detection_type_e type, uas_device
        dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
        ret_if(NULL == dev_info);
 
+       UAM_INFO("dev_info->mac [%s]", dev_info->mac);
+
        if (UAS_PRESENCE == type) {
                UAM_DBG("Presence detected for %s", dev_info->device_id);
                ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI, device->user_id, dev_info);
index 6259d70..9a78865 100644 (file)
@@ -2917,44 +2917,56 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
        tech->last_seen = dev_info->last_seen;
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER);
-       /* Check if IP address was updated then update in DB */
+
+       /* Check if IP/MAC 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))
+                       if (NULL == addr)
                                continue;
 
-                       if (!strcasecmp(addr->address, dev_info->ipv4_addr))
-                               is_updated = FALSE;
-                       else
-                               UAM_DBG("Old IPv4: %s, New IPv4: %s",
+                       if (UAM_ADDR_TYPE_IPv4 == addr->addr_type) {
+
+                               if (strcasecmp(addr->address, dev_info->ipv4_addr)) {
+                                       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);
-                               tech->addresses = g_slist_append(tech->addresses, addr_info);
-                       } else {
-                               g_free(addr_info->address);
+                                       g_free(addr->address);
+                                       addr->addr_type = UAM_ADDR_TYPE_IPv4;
+                                       addr->address = g_strdup(dev_info->ipv4_addr);
+
+                                       /* Update IP address in DB */
+                                       ret = _uam_device_db_update_device_ip_address(dev_info->device_id,
+                                                       dev_info->type, dev_info->ipv4_addr);
+                                       if (UAM_ERROR_NONE != ret) {
+                                               UAM_WARN("_uam_device_db_update_device_ip_address failed");
+                                               __uam_db_end_transaction(0);
+                                               return ret;
+                                       }
+                               }
                        }
 
-                       addr_info->addr_type = UAM_ADDR_TYPE_IPv4;
-                       addr_info->address = g_strdup(dev_info->ipv4_addr);
+                       if (UAM_ADDR_TYPE_WIFI == addr->addr_type) {
 
-                       /* Update address in DB */
-                       ret = _uam_device_db_update_device_ip_address(dev_info->device_id,
-                                       dev_info->type, dev_info->mac, dev_info->ipv4_addr);
-                       if (UAM_ERROR_NONE != ret) {
-                               UAM_WARN("_uam_device_db_update_device_ip_address failed");
-                               __uam_db_end_transaction(0);
-                               return ret;
+                               if (strcasecmp(addr->address, dev_info->mac)) {
+                                       UAM_DBG("Old MAC: %s, New MAC: %s",
+                                               addr->address, dev_info->mac);
+
+                                       g_free(addr->address);
+                                       addr->addr_type = UAM_ADDR_TYPE_WIFI;
+                                       addr->address = g_strdup(dev_info->mac);
+
+                                       /* Update address in DB */
+                                       ret = _uam_device_db_update_device_mac_address(dev_info->device_id,
+                                                       dev_info->type, dev_info->mac);
+                                       if (UAM_ERROR_NONE != ret) {
+                                               UAM_WARN("_uam_device_db_update_device_mac_address failed");
+                                               __uam_db_end_transaction(0);
+                                               return ret;
+                                       }
+                               }
                        }
                }
        }
index 83d81f0..69d51c3 100644 (file)
        "SET presence_state = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
 
 #define UPDATE_IP_ADDRESS "UPDATE devices " \
-       "SET ip_address = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "SET ip_address = ? WHERE device_id = ? AND tech_type = ?"
+
+#define UPDATE_MAC_ADDRESS "UPDATE devices " \
+       "SET address = ? WHERE device_id = ? AND tech_type = ?"
 
 #define UPDATE_DEVICE "UPDATE devices " \
        "SET (os_type, discriminant, ip_address, service_id, " \
@@ -76,6 +79,7 @@ static sqlite3_stmt *select_device_number;
 static sqlite3_stmt *update_last_seen;
 static sqlite3_stmt *update_presence;
 static sqlite3_stmt *update_ip_address;
+static sqlite3_stmt *update_mac_address;
 static sqlite3_stmt *update_device;
 
 /* INSERT statements */
@@ -120,6 +124,7 @@ static void __uam_device_finalize_update(void)
        FINALIZE(update_last_seen);
        FINALIZE(update_presence);
        FINALIZE(update_ip_address);
+       FINALIZE(update_mac_address);
        FINALIZE(update_device);
 
        FUNC_EXIT;
@@ -186,6 +191,8 @@ static int __uam_device_prepare_update(sqlite3 *db)
                UPDATE_PRESENCE, __uam_device_finalize_update);
        PREPARE_QUERY(rc, db, update_ip_address,
                UPDATE_IP_ADDRESS, __uam_device_finalize_update);
+               PREPARE_QUERY(rc, db, update_mac_address,
+               UPDATE_MAC_ADDRESS, __uam_device_finalize_update);
        PREPARE_QUERY(rc, db, update_device,
                UPDATE_DEVICE, __uam_device_finalize_update);
 
@@ -239,6 +246,7 @@ static void __uam_device_table_devicesinfo_finalize(void)
        FUNC_ENTRY;
        __uam_device_finalize_delete();
        __uam_device_finalize_select();
+       __uam_device_finalize_update();
        __uam_device_finalize_insert();
        FUNC_EXIT;
 }
@@ -272,14 +280,13 @@ handle_error:
 }
 
 int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
-               char *address, char *ip_address)
+       char *ip_address)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_ip_address;
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
-       retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == ip_address, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, ip_address, -1, SQLITE_TRANSIENT),
@@ -288,19 +295,50 @@ int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
                error_code, handle_error);
-       DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to update IP address [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("[%d] IP address updated [%s]", tech_type, ip_address);
+
+handle_error:
+       sqlite3_reset(stmt);
+       return error_code;
+}
+
+int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
+       char *address)
+{
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = update_mac_address;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+
+       DB_ACTION(sqlite3_bind_text(stmt, 1, address, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_text(stmt, 2, device_id, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
                error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
-               UAM_ERR("Failed to update ip address [%d:%s]",
+               UAM_ERR("Failed to update MAC address [%d:%s]",
                        sql_ret, sqlite3_errmsg(database_handle));
 
                error_code = UAM_ERROR_DB_FAILED;
                goto handle_error;
        }
 
-       UAM_DBG("ip address updated [%s]", ip_address);
+       UAM_DBG("[%d] MAC address updated [%s]", tech_type, address);
 
 handle_error:
        sqlite3_reset(stmt);