Store payload device user data in database
authorAbhay agarwal <ay.agarwal@samsung.com>
Thu, 26 Dec 2019 13:48:33 +0000 (19:18 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 30 Dec 2019 07:29:56 +0000 (16:29 +0900)
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/data/ua_db.sql
ua-daemon/include/ua-manager-database.h
ua-daemon/src/pm/ua-pm-util.c
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-db.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-payload-db.c

index ab1fe7b..c3984dc 100644 (file)
@@ -47,6 +47,7 @@ CREATE TABLE IF NOT EXISTS payloads (
   device_uid_len INTEGER,
   device_uid TEXT,
   device_icon TEXT,
+  user_data TEXT,
   device_number INTEGER,
   FOREIGN KEY(device_number) REFERENCES devices(device_number),
   UNIQUE (primary_key, secondary_key, device_uid)
index 07cf3fc..0db5058 100644 (file)
@@ -274,6 +274,11 @@ int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
 int _uam_db_get_payload_number(uam_ble_payload_s *payload,
                int *payload_number);
 
+/* update */
+int _uam_db_update_payload_info(
+       const char *device_id, int tech_type,
+       const char *address, uam_ble_payload_s *payload);
+
 #ifdef __cplusplus
 }
 #endif
index be1e3f5..fa8a74c 100644 (file)
@@ -150,15 +150,20 @@ void _pm_util_uas_payload_to_uam_payload(
                uam_ble_payload_s *dst_payload, uas_payload_info_t *src_payload)
 {
        if (src_payload) {
+               int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
                dst_payload->primary_key = src_payload->primary_key;
                dst_payload->device_icon = src_payload->device_icon;
                dst_payload->secondary_key = src_payload->secondary_key;
                dst_payload->device_uid_len = src_payload->device_uid_len;
                memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
                memset(dst_payload->bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
+               memset(dst_payload->user_data, 0, user_data_len);
                if (src_payload->device_uid)
                        memcpy(dst_payload->device_uid,
                        src_payload->device_uid, src_payload->device_uid_len);
+               if (src_payload->user_data)
+                       memcpy(dst_payload->user_data,
+                       src_payload->user_data, user_data_len);
                if (src_payload->bt_mac)
                        memcpy(dst_payload->bt_mac,
                        src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
index c01a6a9..b3edda2 100644 (file)
@@ -100,17 +100,46 @@ static gint __compare_svc_name(gconstpointer data, gconstpointer user_data)
        return g_strcmp0(service->name, svc_name);
 }
 
+void __print_payload(const uam_ble_payload_s *payload)
+{
+       ret_if(NULL == payload);
+       UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d] " \
+               "device uid len: [%d]",
+               payload->primary_key, payload->secondary_key, payload->device_icon,
+               payload->device_uid_len);
+       for (int i = 0; i < payload->device_uid_len; i++)
+               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
+}
+
+void __print_db_payload(const uam_db_payload_info_t *payload)
+{
+       ret_if(NULL == payload);
+       UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d] " \
+               "device uid len: [%d]",
+               payload->primary_key, payload->secondary_key, payload->device_icon,
+               payload->device_uid_len);
+       for (int i = 0; i < payload->device_uid_len; i++)
+               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
+}
+
 static gint __compare_payload(gconstpointer data, gconstpointer user_data)
 {
        const uam_db_payload_info_t *db_payload = data;
        const uam_ble_payload_s *payload = user_data;
 
+       __print_payload(payload);
+       __print_db_payload(db_payload);
+
        retv_if(NULL == db_payload, -1);
        retv_if(NULL == payload, -1);
 
        if ((db_payload->primary_key == payload->primary_key) &&
-               (0 == g_strcmp0(db_payload->device_uid, payload->device_uid)))
+               (db_payload->secondary_key == payload->secondary_key) &&
+               (db_payload->device_uid_len == payload->device_uid_len) &&
+               (0 == memcmp(db_payload->device_uid, payload->device_uid,
+                       payload->device_uid_len)))
                return 0;
+
        return -1;
 }
 
@@ -237,17 +266,6 @@ static void __print_svc_dev(gpointer data, gpointer user_data)
        FUNC_EXIT;
 }
 
-void __print_payload(uam_ble_payload_s *payload)
-{
-       ret_if(NULL == payload);
-       UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d] " \
-               "device uid len: [%d]",
-               payload->primary_key, payload->secondary_key, payload->device_icon,
-               payload->device_uid_len);
-       for (int i = 0; i < payload->device_uid_len; i++)
-               UAM_DBG("Device uid [0x%2.2x]", payload->device_uid[i]);
-}
-
 static void __add_service_to_dev_tech_mapping(
                uam_db_tech_info_t *tech, uam_db_service_info_t *service)
 {
@@ -700,35 +718,60 @@ static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list)
 static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_payload,
        uam_db_payload_info_t *src_payload)
 {
-       if (src_payload) {
-               dst_payload->primary_key = src_payload->primary_key;
-               dst_payload->device_icon = src_payload->device_icon;
-               dst_payload->secondary_key = src_payload->secondary_key;
-               dst_payload->device_uid_len = src_payload->device_uid_len;
-               memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
-               memset(dst_payload->bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
-               if (src_payload->device_uid)
-                       memcpy(dst_payload->device_uid,
-                       src_payload->device_uid, src_payload->device_uid_len);
-               if (src_payload->bt_mac)
-                       memcpy(dst_payload->bt_mac,
-                       src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
-       }
+       FUNC_ENTRY;
+
+       int user_data_len = 0;
+       ret_if (NULL == src_payload);
+
+       dst_payload->primary_key = src_payload->primary_key;
+       dst_payload->device_icon = src_payload->device_icon;
+       dst_payload->secondary_key = src_payload->secondary_key;
+       dst_payload->device_uid_len = src_payload->device_uid_len;
+
+       memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
+       memset(dst_payload->bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
+       if (src_payload->device_uid)
+               memcpy(dst_payload->device_uid,
+               src_payload->device_uid, src_payload->device_uid_len);
+       if (src_payload->bt_mac)
+               memcpy(dst_payload->bt_mac,
+               src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
+
+       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
+       ret_if(0 == user_data_len);
+       memset(dst_payload->user_data, 0, user_data_len);
+       if (src_payload->user_data)
+               memcpy(dst_payload->user_data,
+               src_payload->user_data, user_data_len);
+
+       FUNC_EXIT;
+       return;
 }
 
 static void __uam_copy_uam_payload_info(
                uam_db_payload_info_t *dst_payload, uam_ble_payload_s *src_payload)
 {
-       if (src_payload) {
-               dst_payload->primary_key = src_payload->primary_key;
-               dst_payload->device_icon = src_payload->device_icon;
-               dst_payload->secondary_key = src_payload->secondary_key;
-               dst_payload->device_uid_len = src_payload->device_uid_len;
-               dst_payload->device_uid = g_memdup(&(src_payload->device_uid),
-                                       src_payload->device_uid_len);
-               dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac),
-                                       UAM_BT_MAC_ADDRESS_STRING_LEN);
-       }
+       FUNC_ENTRY;
+
+       int user_data_len = 0;
+       ret_if (NULL == src_payload);
+
+       dst_payload->primary_key = src_payload->primary_key;
+       dst_payload->device_icon = src_payload->device_icon;
+       dst_payload->secondary_key = src_payload->secondary_key;
+       dst_payload->device_uid_len = src_payload->device_uid_len;
+       dst_payload->device_uid = g_memdup(&(src_payload->device_uid),
+                               src_payload->device_uid_len);
+       dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac),
+                               UAM_BT_MAC_ADDRESS_STRING_LEN);
+
+       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
+       ret_if(0 == user_data_len);
+       dst_payload->user_data = g_memdup(&(src_payload->user_data),
+                               user_data_len);
+
+       FUNC_EXIT;
+       return;
 }
 
 static GSList *_uam_core_find_svc_dev_list(uam_device_info_s *dev_info)
@@ -2975,7 +3018,7 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
                UAM_DBG("Compare payload - primary_key detected: [%d], svc_dev: [%d] ",
                        payload->primary_key, svc_dev->payload->primary_key);
 
-               if (payload && !__compare_payload(svc_dev->payload, payload))
+               if (payload && (0 != __compare_payload(svc_dev->payload, payload)))
                        continue;
 
                UAM_DBG("Send event");
@@ -3159,6 +3202,15 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
                return ret;
        }
 
+       /* Update database (payload user data) */
+       ret = _uam_db_update_payload_info(dev_info->device_id,
+                               dev_info->type, dev_info->mac, payload);
+       if (UAM_ERROR_NONE != ret) {
+               UAM_WARN("_uam_db_update_payload_user_data failed");
+               __uam_db_end_transaction(0);
+               return ret;
+       }
+
        __uam_db_end_transaction(1);
        /* Send user presence event and update service_device timestamp */
        __send_user_presence_event(tech, sensor, dev_info, payload);
index a250c0f..7404e8e 100644 (file)
@@ -61,6 +61,7 @@
        "device_uid_len INTEGER, " \
        "device_uid TEXT, " \
        "device_icon TEXT, " \
+       "user_data TEXT, " \
        "device_number INTEGER, " \
        "FOREIGN KEY(device_number) REFERENCES devices(device_number), " \
        "UNIQUE (primary_key, secondary_key, device_uid) " \
index f0e9a22..7b7872e 100644 (file)
@@ -33,7 +33,7 @@
 
 #define SELECT_DEVICE_SERVICES \
        "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, " \
-       "P.primary_key, P.secondary_key, P.device_uid, P.device_icon " \
+       "P.primary_key, P.secondary_key, P.device_uid_len, P.device_uid, P.device_icon " \
        "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
        "USING(device_number) JOIN payloads as P USING(payload_number)"
 
@@ -798,11 +798,12 @@ GSList *_uam_db_get_service_devices_info()
                        info->payload_info.primary_key = buf ? *buf : 13;
                        buf = (char *)sqlite3_column_text(stmt, 6);
                        info->payload_info.secondary_key = buf ? *buf : 1;
-                       buf = (char *)sqlite3_column_text(stmt, 7);
+                       info->payload_info.device_uid_len = sqlite3_column_int(stmt, 7);
+                       buf = (char *)sqlite3_column_text(stmt, 8);
                        if (buf)
                                memcpy(info->payload_info.device_uid, buf,
-                                       UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN);
-                       buf = (char *)sqlite3_column_text(stmt, 8);
+                                       info->payload_info.device_uid_len);
+                       buf = (char *)sqlite3_column_text(stmt, 9);
                        info->payload_info.device_icon = buf ? *buf : 1;
 
                        UAM_INFO("device id:%s-%d-%s-%d-%llu " \
index 553d470..31c6d21 100644 (file)
 
 #define SELECT_ALL_PAYLOADS "SELECT P.primary_key, P.secondary_key, " \
        "P.device_uid_len, P.device_uid, P.device_icon, D.device_id, " \
-       "D.tech_type FROM payloads as P JOIN devices as D USING(device_number)"
+       "D.tech_type, P.user_data " \
+       "FROM payloads as P JOIN devices as D USING(device_number)"
 
 #define SELECT_PAYLOAD_DEVICE "SELECT device_number FROM payloads " \
        "where primary_key = ? AND secondary_key = ? AND device_uid = ?"
+
 #define SELECT_PAYLOAD_NUMBER "SELECT payload_number FROM payloads " \
        "where primary_key = ? AND secondary_key = ? AND device_uid = ?"
 
 #define INSERT_PAYLOAD "insert into payloads (primary_key, " \
-       "secondary_key, device_uid_len, device_uid, device_icon, device_number)" \
-       "values (?, ?, ?, ?, ?)"
+       "secondary_key, device_uid_len, device_uid, device_icon, device_number) " \
+       "values (?, ?, ?, ?, ?, ?)"
 
 #define DELETE_ALL_PAYLOADS "delete from payloads"
 
+#define UPDATE_PAYLOAD "UPDATE payloads " \
+       "SET primary_key = ?, secondary_key = ?, device_uid_len = ?, " \
+       "device_uid = ?, device_icon = ? user_data = ? WHERE device_number = ?"
+
 /* DELETE statements */
 static sqlite3_stmt *delete_all_payloads;
 
@@ -48,7 +54,7 @@ static sqlite3_stmt *select_payload_device;
 static sqlite3_stmt *select_payload_number;
 
 /* UPDATE statements */
-//static sqlite3_stmt *update_payload_info;
+static sqlite3_stmt *update_payload_info;
 
 /* INSERT statements */
 static sqlite3_stmt *insert_payload_info;
@@ -89,7 +95,7 @@ static void __uam_payload_finalize_update(void)
 {
        FUNC_ENTRY;
 
-//     FINALIZE(update_payload_info);
+       FINALIZE(update_payload_info);
 
        FUNC_EXIT;
 }
@@ -149,8 +155,8 @@ static int __uam_payload_prepare_update(sqlite3 *db)
                return SQLITE_OK;
        }
 
-//     PREPARE_QUERY(rc, db, update_payload_info,
-//             UPDATE_PAYLOAD, __uam_payload_finalize_update);
+       PREPARE_QUERY(rc, db, update_payload_info,
+               UPDATE_PAYLOAD, __uam_payload_finalize_update);
 
        initialized = 1;
        FUNC_EXIT;
@@ -399,6 +405,7 @@ GSList *_uam_db_get_all_payloads(void)
        db_payload_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
        char *buf = NULL;
+       int user_data_len = 0;
 
        do {
                sql_ret = sqlite3_step(stmt);
@@ -407,23 +414,31 @@ GSList *_uam_db_get_all_payloads(void)
                        break;
                case SQLITE_ROW:
                        info = g_new0(db_payload_info_t, 1);
-                       memset(info->payload_info.device_uid, 0, UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1);
 
                        buf = (char *)sqlite3_column_text(stmt, 0);
                        info->payload_info.primary_key = buf ? *buf : 13;
                        buf = (char *)sqlite3_column_text(stmt, 1);
                        info->payload_info.secondary_key = buf ? *buf : 1;
                        info->payload_info.device_uid_len = sqlite3_column_int(stmt, 2);
+                       memset(info->payload_info.device_uid, 0,
+                               info->payload_info.device_uid_len);
                        buf = (char *)sqlite3_column_text(stmt, 3);
                        if (buf)
                                memcpy(info->payload_info.device_uid, buf,
-                                       UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN);
+                                       info->payload_info.device_uid_len);
                        buf = (char *)sqlite3_column_text(stmt, 4);
                        info->payload_info.device_icon = buf ? *buf : 1;
                        g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 5),
                                UAM_DEVICE_ID_MAX_STRING_LEN);
                        info->type = sqlite3_column_int(stmt, 6);
 
+                       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - info->payload_info.device_uid_len;
+                       memset(info->payload_info.user_data, 0, user_data_len);
+                       buf = (char *)sqlite3_column_text(stmt, 3);
+                       if (buf)
+                               memcpy(info->payload_info.user_data, buf,
+                                       user_data_len);
+
                        UAM_INFO("primary key: %d secondary key: %d",
                                info->payload_info.primary_key,
                                info->payload_info.secondary_key);
@@ -442,24 +457,40 @@ GSList *_uam_db_get_all_payloads(void)
        return payload_list;
 }
 
-/*
-int _uam_db_update_payload_info(uam_db_payload_info_t *svc)
+int _uam_db_update_payload_info(
+       const char *device_id, int tech_type,
+       const char *address, uam_ble_payload_s *payload)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_payload_info;
        int sql_ret = SQLITE_OK;
+       int user_data_len = 0;
 
-       retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("%s-%d-%s", device_id, tech_type, address);
 
-       UAM_INFO("%s-%d-%d-%d", svc->name, svc->cycle, svc->presence_threshold,
-                               svc->absence_threshold);
+       int device_number = 0;
+       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       if (UAM_ERROR_NONE != error_code)
+               goto handle_error;
 
-       DB_ACTION(sqlite3_bind_int(stmt, 1, svc->presence_threshold),
+       DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->primary_key), 1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_text(stmt, 2, &(payload->secondary_key), 1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 3, payload->device_uid_len),
                error_code, handle_error);
-       DB_ACTION(sqlite3_bind_int(stmt, 2, svc->absence_threshold),
+       DB_ACTION(sqlite3_bind_text(stmt, 4, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
-       DB_ACTION(sqlite3_bind_text(stmt, 3, svc->name, -1, SQLITE_TRANSIENT),
+       DB_ACTION(sqlite3_bind_text(stmt, 5, &(payload->device_icon), 1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+
+       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
+       DB_ACTION(sqlite3_bind_text(stmt, 6, payload->user_data, user_data_len, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 7, device_number),
                error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
@@ -470,7 +501,10 @@ int _uam_db_update_payload_info(uam_db_payload_info_t *svc)
                goto handle_error;
        }
 
-       UAM_DBG("Payload info updated");
+       UAM_DBG("Device-payload info inserted [%d] primary_key: [%d] " \
+               "secondary_key: [%d] device_icon: [%d]", device_number,
+               payload->primary_key, payload->secondary_key,
+               payload->device_icon);
 
 handle_error:
        sqlite3_reset(stmt);
@@ -478,6 +512,7 @@ handle_error:
        return error_code;
 }
 
+/*
 int _uam_db_delete_payload_info(const char *payload_name)
 {
        FUNC_ENTRY;