Add database API to insert device payload
authorAbhay agarwal <ay.agarwal@samsung.com>
Fri, 29 Nov 2019 08:51:15 +0000 (14:21 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 30 Dec 2019 07:29:55 +0000 (16:29 +0900)
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/CMakeLists.txt
ua-daemon/include/ua-manager-database.h
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-db.c
ua-daemon/src/ua-manager-payload-db.c [new file with mode: 0644]

index 51878cc..9b3b6dd 100644 (file)
@@ -15,6 +15,7 @@ SET(SRCS
        src/ua-manager-user-db.c
        src/ua-manager-service-db.c
        src/ua-manager-device-service-db.c
+       src/ua-manager-payload-db.c
        src/ua-manager-adv-db.c
        src/pm/ua-plugin-manager.c
        src/pm/ua-pm-util.c
index 9571516..aa46e34 100644 (file)
@@ -244,6 +244,25 @@ GSList *_uam_db_get_all_advs(void);
 /* delete operations */
 int _uam_adv_db_clear(void);
 
+
+/* PAYLOAD QUERIES */
+typedef struct {
+       uam_ble_payload_s payload_info;
+} db_payload_info_t;
+
+/* db init/deinit */
+int _uam_payload_db_initialize(void);
+int _uam_payload_db_deinitialize(void);
+
+/* insert */
+int _uam_db_insert_payload_info(
+       const char *device_id, int tech_type,
+       const char *address, uam_ble_payload_s *payload);
+
+/* delete operations */
+int _uam_payload_db_clear(void);
+
+
 #ifdef __cplusplus
 }
 #endif
index 5d01e39..1cc2eae 100644 (file)
@@ -3882,7 +3882,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
        uam_db_tech_info_t *tech_info;
-//     char *mac;
+       char *mac;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
@@ -3891,10 +3891,10 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
 
        tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
        retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
-//     mac = __get_mac_addr(tech_info);
+       mac = __get_mac_addr(tech_info);
 
        /*** Add payload to database ***/
-//     ret = _uam_db_insert_payload(payload, device_id, tech_type, mac);
+       ret = _uam_db_insert_payload_info(device_id, tech_type, mac, payload);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_db_insert_adv_info failed");
                return ret;
index 35f91c1..f4abfcc 100644 (file)
@@ -30,7 +30,7 @@
 #define UAM_DB_SERVICES_TABLE "services" /**< Service DB table name */
 #define UAM_DB_DEVICE_SERVICES_TABLE "device_services" /**< Device services DB table name */
 #define UAM_DB_IBEACON_ADV_TABLE "ibeacon_adv" /**< iBeacon adv DB table name */
-#define UAM_DB_PAYLOAD_TABLE "payload" /**< payload DB table name */
+#define UAM_DB_PAYLOADS_TABLE "payloads" /**< payload DB table name */
 
 #define CREATE_USERDATA_TABLE "CREATE TABLE IF NOT EXISTS userdata ( " \
        "name TEXT, " \
@@ -54,7 +54,7 @@
        "UNIQUE (device_id, tech_type, address) " \
        "); "
 
-#define CREATE_PAYLOAD_TABLE "CREATE TABLE IF NOT EXISTS payloads ( " \
+#define CREATE_PAYLOADS_TABLE "CREATE TABLE IF NOT EXISTS payloads ( " \
        "payload_number INTEGER PRIMARY KEY AUTOINCREMENT, " \
        "primary_key TEXT, " \
        "secondary_key TEXT, " \
@@ -406,6 +406,9 @@ static int __uam_db_check_table_creation(void)
        if (!__uam_db_is_table_existing(UAM_DB_IBEACON_ADV_TABLE))
                retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_IBEACON_ADV_TABLE), error_code);
 
+       if (!__uam_db_is_table_existing(UAM_DB_PAYLOADS_TABLE))
+               retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_PAYLOADS_TABLE), error_code);
+
        UAM_DBG("Successfully verified table creation");
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -492,6 +495,7 @@ int _uam_db_initialize(void)
        EXEC(UAM_ERROR_NONE, _uam_service_db_initialize(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_device_service_db_initialize(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_adv_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_payload_db_initialize(), handle_error);
 
        transaction_cnt = 0;
        FUNC_EXIT;
@@ -514,6 +518,7 @@ int _uam_db_deinitialize(void)
        _uam_service_db_deinitialize();
        _uam_device_service_db_deinitialize();
        _uam_adv_db_deinitialize();
+       _uam_payload_db_deinitialize();
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -529,6 +534,7 @@ int _uam_db_clear(void)
        EXEC(UAM_ERROR_NONE, _uam_service_db_clear(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_device_service_db_clear(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_adv_db_clear(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_payload_db_clear(), handle_error);
        UAM_DBG("Table data deleted ");
 
        error_code = UAM_ERROR_NONE;
diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c
new file mode 100644 (file)
index 0000000..1dbd1d0
--- /dev/null
@@ -0,0 +1,492 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <sqlite3.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#include "ua-manager-common.h"
+#include "ua-manager-database.h"
+
+/*
+#define SELECT_ALL_PAYLOADS "SELECT payload_number, payload_name, " \
+       "cycle, presence_threshold, absence_threshold FROM payloads"
+
+#define SELECT_PAYLOAD "SELECT payload_number, payload_name, " \
+       "cycle, presence_threshold, absence_threshold FROM payloads " \
+       "where payload_name = ?"
+*/
+
+#define INSERT_PAYLOAD "insert into payloads (primary_key, " \
+       "secondary_key, device_uid, device_icon, device_number)" \
+       "values (?, ?, ?, ?, ?)"
+
+/*
+#define UPDATE_PAYLOAD "UPDATE payloads SET (presence_threshold, absence_threshold) " \
+       "= (?, ?) WHERE payload_name = ?"
+*/
+
+#define DELETE_ALL_PAYLOADS "delete from payloads"
+
+/*
+#define DELETE_PAYLOAD_INFO "delete from payloads WHERE payload_name = ?"
+*/
+
+/* DELETE statements */
+static sqlite3_stmt *delete_all_payloads;
+//static sqlite3_stmt *delete_payload_info;
+
+/* SELECT statements */
+//static sqlite3_stmt *select_all_payloads;
+//static sqlite3_stmt *select_payload;
+
+/* UPDATE statements */
+//static sqlite3_stmt *update_cycle;
+//static sqlite3_stmt *update_payload_info;
+
+/* INSERT statements */
+static sqlite3_stmt *insert_payload_info;
+
+extern sqlite3 *database_handle;
+
+static void __uam_payload_finalize_delete(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(delete_all_payloads);
+//     FINALIZE(delete_payload_info);
+
+       FUNC_EXIT;
+}
+
+static void __uam_payload_finalize_select(void)
+{
+       FUNC_ENTRY;
+
+//     FINALIZE(select_all_payloads);
+//     FINALIZE(select_payload);
+
+       FUNC_EXIT;
+}
+
+static void __uam_payload_finalize_insert(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(insert_payload_info);
+
+       FUNC_EXIT;
+}
+
+static void __uam_payload_finalize_update(void)
+{
+       FUNC_ENTRY;
+
+//     FINALIZE(update_payload_info);
+
+       FUNC_EXIT;
+}
+
+static int __uam_payload_prepare_delete(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, delete_all_payloads,
+               DELETE_ALL_PAYLOADS, __uam_payload_finalize_delete);
+//     PREPARE_QUERY(rc, db, delete_payload_info,
+//             DELETE_PAYLOAD_INFO, __uam_payload_finalize_delete);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_payload_prepare_select(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+//     PREPARE_QUERY(rc, db, select_all_payloads,
+//             SELECT_ALL_PAYLOADS, __uam_payload_finalize_select);
+//     PREPARE_QUERY(rc, db, select_payload,
+//             SELECT_PAYLOAD, __uam_payload_finalize_select);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_payload_prepare_update(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+//     PREPARE_QUERY(rc, db, update_payload_info,
+//             UPDATE_PAYLOAD, __uam_payload_finalize_update);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_payload_prepare_insert(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, insert_payload_info,
+               INSERT_PAYLOAD, __uam_payload_finalize_insert);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_payload_table_info_prepare(sqlite3 *db)
+{
+       FUNC_ENTRY;
+
+       int error_code = UAM_ERROR_NONE;
+
+       if (db == NULL) {
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+
+       DB_ACTION(__uam_payload_prepare_delete(db), error_code, handle_error);
+       DB_ACTION(__uam_payload_prepare_select(db), error_code, handle_error);
+       DB_ACTION(__uam_payload_prepare_update(db), error_code, handle_error);
+       DB_ACTION(__uam_payload_prepare_insert(db), error_code, handle_error);
+
+handle_error:
+
+       FUNC_EXIT;
+       return error_code;
+}
+
+static void __uam_payload_table_info_finalize(void)
+{
+       FUNC_ENTRY;
+       __uam_payload_finalize_delete();
+       __uam_payload_finalize_select();
+       __uam_payload_finalize_insert();
+       __uam_payload_finalize_update();
+       FUNC_EXIT;
+}
+
+int _uam_payload_db_deinitialize(void)
+{
+       FUNC_ENTRY;
+
+       retv_if(NULL == database_handle, UAM_ERROR_NONE);
+
+       __uam_payload_table_info_finalize();
+       sqlite3_close(database_handle);
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
+int _uam_payload_db_initialize(void)
+{
+       FUNC_ENTRY;
+
+       EXEC(UAM_ERROR_NONE, __uam_payload_table_info_prepare(database_handle), handle_error);
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+
+handle_error:
+       _uam_payload_db_deinitialize();
+       FUNC_EXIT;
+       return UAM_ERROR_DB_FAILED;
+}
+
+int _uam_db_insert_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 = insert_payload_info;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("%s-%d-%s", device_id, tech_type, address);
+
+       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_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_text(stmt, 3, payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_LEN + 1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_text(stmt, 4, &(payload->device_icon), 1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 5, device_number),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to insert payload info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("Device-payload info inserted [%d] ", device_number);
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+/*
+int _uam_db_update_payload_info(uam_db_payload_info_t *svc)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = update_payload_info;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("%s-%d-%d-%d", svc->name, svc->cycle, svc->presence_threshold,
+                               svc->absence_threshold);
+
+       DB_ACTION(sqlite3_bind_int(stmt, 1, svc->presence_threshold),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 2, svc->absence_threshold),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_text(stmt, 3, svc->name, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to update payload info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("Payload info updated");
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+int _uam_db_delete_payload_info(const char *payload_name)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = delete_payload_info;
+       int sql_ret = SQLITE_OK;
+
+       error_code = _uam_db_delete_device_payload(payload_name);
+       if (UAM_ERROR_NONE != error_code) {
+               UAM_ERR("_uam_db_delete_device_payload failed");
+               return error_code;
+       }
+
+       DB_ACTION(sqlite3_bind_text(stmt, 1, payload_name, -1, SQLITE_STATIC),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to delete payload info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+       } else
+               UAM_DBG("Payload info deleted");
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+int _uam_db_update_payload_cycle(const char *payload_name, int cycle)
+{
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = update_cycle;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == payload_name, UAM_ERROR_INVALID_PARAMETER);
+
+       DB_ACTION(sqlite3_bind_int64(stmt, 1, cycle),
+               error_code, handle_error);
+       DB_ACTION(sqlite3_bind_text(stmt, 2, payload_name, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to update payload cycle [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("Payload cycle updated [%d]", cycle);
+
+handle_error:
+       sqlite3_reset(stmt);
+       return error_code;
+}
+
+int _uam_db_get_payload_info(const char *payload_name, db_payload_info_t *info)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NOT_FOUND;
+       sqlite3_stmt *stmt = select_payload;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(!payload_name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!info, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("Payload Name: %s", payload_name);
+
+       DB_ACTION(sqlite3_bind_text(stmt, 1, payload_name, -1, SQLITE_STATIC),
+               error_code, handle_error);
+
+       do {
+               sql_ret = sqlite3_step(stmt);
+               switch (sql_ret) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       UAM_DBG("Payload info found");
+                       info->payload_number = sqlite3_column_int(stmt, 0);
+                       g_strlcpy(info->payload_name, (char *)sqlite3_column_text(stmt, 1),
+                               UAM_PAYLOAD_MAX_STRING_LEN);
+                       info->cycle = sqlite3_column_int(stmt, 2);
+                       info->presence_threshold = sqlite3_column_int(stmt, 3);
+                       info->absence_threshold = sqlite3_column_int(stmt, 4);
+
+                       UAM_INFO("%d-%s-%d", info->payload_number, info->payload_name, info->cycle);
+                       error_code = UAM_ERROR_NONE;
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info [%d:%s]",
+                               sql_ret, sqlite3_errmsg(database_handle));
+                       error_code = UAM_ERROR_DB_FAILED;
+               }
+       } while (sql_ret == SQLITE_ROW);
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+GSList *_uam_payload_db_get_all_payloads(void)
+{
+       FUNC_ENTRY;
+       sqlite3_stmt *stmt = select_all_payloads;
+       GSList *payload_list = NULL;
+       db_payload_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:
+                       info = g_new0(db_payload_info_t, 1);
+                       info->payload_number = sqlite3_column_int(stmt, 0);
+                       g_strlcpy(info->payload_name, (char *)sqlite3_column_text(stmt, 1),
+                               UAM_PAYLOAD_MAX_STRING_LEN);
+                       info->cycle = sqlite3_column_int(stmt, 2);
+                       info->presence_threshold = sqlite3_column_int(stmt, 3);
+                       info->absence_threshold = sqlite3_column_int(stmt, 4);
+
+                       UAM_INFO("%d-%s-%d-%d-%d", info->payload_number, info->payload_name,
+                                info->cycle, info->presence_threshold, info->absence_threshold);
+
+                       payload_list = g_slist_append(payload_list, info);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate payload info [%d:%s]",
+                               sql_ret, sqlite3_errmsg(database_handle));
+               }
+       } while (sql_ret == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return payload_list;
+}
+*/
+
+int _uam_payload_db_clear(void)
+{
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = delete_all_payloads;
+       int sql_ret = SQLITE_OK;
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to delete payload data [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("Payload data deleted ");
+
+handle_error:
+
+       sqlite3_reset(stmt);
+       return error_code;
+}