Fix device service list in ua-manager for NULL payloads
authorAbhay agarwal <ay.agarwal@samsung.com>
Thu, 26 Dec 2019 15:53:33 +0000 (21:23 +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/include/ua-manager-database.h
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-payload-db.c

index 0db5058..de7c0b4 100644 (file)
@@ -198,6 +198,7 @@ GSList *_uam_db_get_device_services(
 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
        const char *address, int *count);
 GSList *_uam_db_get_service_devices_info();
+GSList *_uam_db_get_service_devices_payloads_info();
 
 /* delete operations */
 int _uam_device_service_db_clear(void);
index b3edda2..8b11fea 100644 (file)
@@ -103,23 +103,29 @@ static gint __compare_svc_name(gconstpointer data, gconstpointer user_data)
 void __print_payload(const uam_ble_payload_s *payload)
 {
        ret_if(NULL == payload);
+       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
        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]);
+       for (int i = 0; i < user_data_len; i++)
+               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
 }
 
 void __print_db_payload(const uam_db_payload_info_t *payload)
 {
        ret_if(NULL == payload);
+       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
        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]);
+       for (int i = 0; i < user_data_len; i++)
+               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
 }
 
 static gint __compare_payload(gconstpointer data, gconstpointer user_data)
@@ -2520,6 +2526,7 @@ int _uam_core_init(void)
        }
 
        /* Fetch svc dev list */
+
        db_svc_dev_list = _uam_db_get_service_devices_info();
        if (!db_svc_dev_list) {
                UAM_INFO_C("No service devices in database");
@@ -2532,6 +2539,20 @@ int _uam_core_init(void)
                                        &(db_svc->payload_info));
                }
        }
+       g_slist_free_full(db_svc_dev_list, g_free);
+
+       db_svc_dev_list = _uam_db_get_service_devices_payloads_info();
+       if (!db_svc_dev_list) {
+               UAM_INFO_C("No service devices in database");
+       } else {
+               for (l = db_svc_dev_list; NULL != l; l = g_slist_next(l)) {
+                       db_svc_dev_info_t *db_svc = l->data;
+
+                       _uam_core_update_svc_dev_info(db_svc->device_id, db_svc->type,
+                                       db_svc->svc, db_svc->discriminant, db_svc->last_seen,
+                                       &(db_svc->payload_info));
+               }
+       }
 
        /* Fetch device list */
        db_devices = _uam_device_db_get_all_devices();
@@ -4225,6 +4246,7 @@ int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list)
                if (!db_info)
                        continue;
                __uam_copy_db_payload_info(&((*payload_list)[*count]), db_info);
+               __print_db_payload(db_info);
                *count += 1;
        }
 
index 7b7872e..1b07fc2 100644 (file)
 #define SELECT_DEVICE_SERVICES_COUNT "SELECT count(*) " \
        "FROM device_services where device_number = ?"
 
-#define SELECT_DEVICE_SERVICES \
+#define SELECT_DEVICE_SERVICES_PAYLOADS \
        "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, " \
        "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)"
 
+#define SELECT_DEVICE_SERVICES \
+       "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen " \
+       "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
+       "USING(device_number)"
+
 #define INSERT_DEVICE_SERVICE "insert into device_services " \
        "(device_number, service_number, discriminant, last_seen) values (?, ?, ?, ?)"
 
@@ -70,6 +75,7 @@ static sqlite3_stmt *delete_device;
 static sqlite3_stmt *select_service;
 static sqlite3_stmt *select_device_services_count;
 static sqlite3_stmt *select_device_services;
+static sqlite3_stmt *select_device_services_payloads;
 
 /* INSERT statements */
 static sqlite3_stmt *insert_device_service;
@@ -100,6 +106,7 @@ static void __uam_device_service_finalize_select(void)
        FINALIZE(select_service);
        FINALIZE(select_device_services_count);
        FINALIZE(select_device_services);
+       FINALIZE(select_device_services_payloads);
 
        FUNC_EXIT;
 }
@@ -166,6 +173,8 @@ static int __uam_device_service_prepare_select(sqlite3 *db)
                SELECT_DEVICE_SERVICES_COUNT, __uam_device_service_finalize_select);
        PREPARE_QUERY(rc, db, select_device_services,
                SELECT_DEVICE_SERVICES, __uam_device_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_device_services_payloads,
+               SELECT_DEVICE_SERVICES_PAYLOADS, __uam_device_service_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -769,10 +778,10 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_service_devices_info()
+GSList *_uam_db_get_service_devices_payloads_info()
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_device_services;
+       sqlite3_stmt *stmt = select_device_services_payloads;
        GSList *svc_dev_list = NULL;
        db_svc_dev_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
@@ -830,6 +839,53 @@ GSList *_uam_db_get_service_devices_info()
        return svc_dev_list;
 }
 
+
+GSList *_uam_db_get_service_devices_info()
+{
+       FUNC_ENTRY;
+       sqlite3_stmt *stmt = select_device_services;
+       GSList *svc_dev_list = NULL;
+       db_svc_dev_info_t *info = NULL;
+       int sql_ret = SQLITE_OK;
+
+       do {
+               sql_ret = sqlite3_step(stmt);
+
+               switch (sql_ret) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       UAM_DBG("Device services info found");
+                       info = g_new0(db_svc_dev_info_t, 1);
+                       g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 0),
+                               UAM_DEVICE_ID_MAX_STRING_LEN);
+                       info->type = sqlite3_column_int(stmt, 1);
+                       g_strlcpy(info->svc, (char *)sqlite3_column_text(stmt, 2),
+                               UAM_SERVICE_MAX_STRING_LEN);
+                       info->discriminant = sqlite3_column_int(stmt, 3);
+                       info->last_seen = sqlite3_column_int64(stmt, 4);
+
+                       UAM_INFO("device id:%s-%d-%s-%d-%llu ",
+                               info->device_id,
+                               info->type,
+                               info->svc,
+                               info->discriminant,
+                               info->last_seen);
+
+                       svc_dev_list = g_slist_append(svc_dev_list, info);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info [%d:%s]",
+                               sql_ret, sqlite3_errmsg(database_handle));
+               }
+       } while (sql_ret == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return svc_dev_list;
+}
+
 static int __uam_db_update_dev_svc_payload(int device_number,
        int service_number, int payload_number)
 {
index 31c6d21..b13a214 100644 (file)
@@ -43,7 +43,7 @@
 
 #define UPDATE_PAYLOAD "UPDATE payloads " \
        "SET primary_key = ?, secondary_key = ?, device_uid_len = ?, " \
-       "device_uid = ?, device_icon = ? user_data = ? WHERE device_number = ?"
+       "device_uid = ?, device_icon = ?, user_data = ? WHERE device_number = ?"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_payloads;
@@ -306,7 +306,7 @@ int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
                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_text(stmt, 3, payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1, SQLITE_TRANSIENT),
+       DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
 
        do {
@@ -347,7 +347,7 @@ int _uam_db_get_payload_number(uam_ble_payload_s *payload,
                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_text(stmt, 3, payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1, SQLITE_TRANSIENT),
+       DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
 
        do {
@@ -434,7 +434,7 @@ GSList *_uam_db_get_all_payloads(void)
 
                        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);
+                       buf = (char *)sqlite3_column_text(stmt, 7);
                        if (buf)
                                memcpy(info->payload_info.user_data, buf,
                                        user_data_len);
@@ -442,6 +442,10 @@ GSList *_uam_db_get_all_payloads(void)
                        UAM_INFO("primary key: %d secondary key: %d",
                                info->payload_info.primary_key,
                                info->payload_info.secondary_key);
+                       for (int i = 0; i < info->payload_info.device_uid_len; i++)
+                               UAM_DBG("payload device uid [0x%2.2X]", info->payload_info.device_uid[i]);
+                       for (int i = 0; i < user_data_len; i++)
+                               UAM_DBG("payload user data [0x%2.2X]", info->payload_info.user_data[i]);
 
                        payload_list = g_slist_append(payload_list, info);
                        break;
@@ -501,10 +505,12 @@ int _uam_db_update_payload_info(
                goto handle_error;
        }
 
-       UAM_DBG("Device-payload info inserted [%d] primary_key: [%d] " \
+       UAM_DBG("Device-payload info updated [%d] primary_key: [%d] " \
                "secondary_key: [%d] device_icon: [%d]", device_number,
                payload->primary_key, payload->secondary_key,
                payload->device_icon);
+       for (int i = 0; i < user_data_len; i++)
+               UAM_DBG("payload user data [0x%2.2X]", payload->user_data[i]);
 
 handle_error:
        sqlite3_reset(stmt);