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)
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
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);
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;
}
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)
{
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)
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");
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);
"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) " \
#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)"
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 " \
#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;
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;
{
FUNC_ENTRY;
-// FINALIZE(update_payload_info);
+ FINALIZE(update_payload_info);
FUNC_EXIT;
}
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;
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);
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);
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);
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);
return error_code;
}
+/*
int _uam_db_delete_payload_info(const char *payload_name)
{
FUNC_ENTRY;