--- /dev/null
+/*
+ * 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;
+}