From 6ba2da98222a203f50df310309ece870e393d232 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 17 Dec 2019 17:57:35 +0530 Subject: [PATCH 01/16] Update core payload list from database during init Signed-off-by: Abhay agarwal --- include/ua-api.h | 19 ++++++ ua-daemon/include/ua-manager-database.h | 1 + ua-daemon/src/ua-manager-core.c | 102 ++++++++++++++++++++++---------- ua-daemon/src/ua-manager-payload-db.c | 5 +- 4 files changed, 93 insertions(+), 34 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index 7d68602..f02da1c 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -1433,6 +1433,25 @@ int _uam_request_add_payload(uam_ble_payload_s *payload, int _uam_service_add_payload(const char *service, uam_ble_payload_s *payload); +/** + * @brief Gets registered payloads list. + * @since_tizen 5.5 + * + * @remarks The @a payload_list should be destroyed by using #g_free() and #g_ptr_array_free(). + * + * @param[out] payload_list Payloads list array + * + * @return 0 on success, otherwise a negative error value + * @retval #UAM_ERROR_NONE Successful + * @retval #UAM_ERROR_INVALID_PARAMETER Invalid parameters + * @retval #UAM_ERROR_INTERNAL Internal error + * + * @exception + * @pre + * @post + */ +int _uam_get_added_payloads(GPtrArray **payload_list); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index d514af7..9d5375e 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -265,6 +265,7 @@ int _uam_db_insert_payload_info( int _uam_payload_db_clear(void); /* select operations */ +GSList *_uam_db_get_all_payloads(void); int _uam_db_get_payload_device_number(uam_ble_payload_s *payload, int *device_number); int _uam_db_get_payload_number(uam_ble_payload_s *payload, diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index f6405d5..37e7453 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -628,6 +628,37 @@ static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list) return 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; + if (src_payload->device_uid) + memcpy(dst_payload->device_uid, + src_payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + if (src_payload->bt_mac) + memcpy(dst_payload->bt_mac, + src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN); + } +} + +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 = g_new0(uam_db_payload_info_t, 1); + 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 = g_memdup(&(src_payload->device_uid), + UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac), + UAM_BT_MAC_ADDRESS_STRING_LEN); + } +} + static void __uam_core_add_dev_to_list( uam_db_user_info_t *user, const uam_device_info_s *dev_info, int presence_state, unsigned long long last_seen, GSList *svc_list) @@ -2242,6 +2273,7 @@ int _uam_core_init(void) GSList *db_svc_list; GSList *db_adv_list; GSList *db_svc_dev_list; + GSList *db_payload_list; GSList *l; /* Init database */ @@ -2361,11 +2393,35 @@ int _uam_core_init(void) } } + /* Fetch payload list */ + db_payload_list = _uam_db_get_all_payloads(); + if (!db_payload_list) { + UAM_INFO_C("No device payload in database"); + } else { + for (l = db_payload_list; NULL != l; l = g_slist_next(l)) { + db_payload_info_t *db_payload = l->data; + uam_db_payload_info_t *payload; + GSList *l1; + + if (!db_payload) + continue; + +// l1 = g_slist_find_custom(payloads, +// db_payload->payload_name, __compare_payload_name); + if (!l1) { + payload = g_new0(uam_db_payload_info_t, 1); + __uam_copy_uam_payload_info(payload, &(db_payload->payload_info)); + payloads = g_slist_append(payloads, payload); + } + } + } + g_slist_free_full(db_devices, g_free); g_slist_free_full(db_users, g_free); g_slist_free_full(db_svc_list, g_free); g_slist_free_full(db_adv_list, g_free); g_slist_free_full(db_svc_dev_list, g_free); + g_slist_free_full(db_payload_list, g_free); /* Set/update registered device list to plugins */ if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices)) @@ -2435,6 +2491,19 @@ void _uam_core_deinit(void) g_slist_free(svc_devs); svc_devs = NULL; + /* Release allocated memory for payloads */ + for (l = payloads; NULL != l; l = g_slist_next(l)) { + uam_db_payload_info_t *payload = l->data; + + if (!payload) + continue; + g_free(payload->device_uid); + g_free(payload->bt_mac); + g_free(payload); + } + g_slist_free(payloads); + payloads = NULL; + FUNC_EXIT; } @@ -3858,37 +3927,6 @@ void _uam_core_handle_status_changed(unsigned int sensor, void *info) FUNC_EXIT; } -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; - if (src_payload->device_uid) - memcpy(dst_payload->device_uid, - src_payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); - if (src_payload->bt_mac) - memcpy(dst_payload->bt_mac, - src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN); - } -} - -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 = g_new0(uam_db_payload_info_t, 1); - 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 = g_memdup(&(src_payload->device_uid), - UAM_BLE_PAYLOAD_DEVICE_UID_LEN); - dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac), - UAM_BT_MAC_ADDRESS_STRING_LEN); - } -} - int _uam_core_add_payload(uam_ble_payload_s *payload, const char *device_id, int tech_type) { @@ -3968,7 +4006,7 @@ int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list) for (l = payloads; l; l = g_slist_next(l)) { uam_db_payload_info_t *db_info = l->data; - if (!db_info || !db_info->device_uid) + if (!db_info) continue; __uam_copy_db_payload_info(&(*payload_list)[*count], db_info); diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index bd21216..1837dcb 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -410,8 +410,9 @@ GSList *_uam_db_get_all_payloads(void) info->payload_info.primary_key = buf ? *buf : 13; buf = (char *)sqlite3_column_text(stmt, 1); info->payload_info.secondary_key = buf ? *buf : 1; - if ((char*)sqlite3_column_text(stmt, 2)) - memcpy(info->payload_info.device_uid, (char *)sqlite3_column_text(stmt, 11), + buf = (char *)sqlite3_column_text(stmt, 2); + if (buf) + memcpy(info->payload_info.device_uid, buf, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); buf = (char *)sqlite3_column_text(stmt, 3); info->payload_info.device_icon = buf ? *buf : 1; -- 2.7.4 From 87935e714c348fec6f1c4b0e30a64af36b3f0094 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 17 Dec 2019 19:54:08 +0530 Subject: [PATCH 02/16] Do not return from uam core initialization if no data in database - All the database tables must be checked before returning from uam_core_init function - Some/All of tables of uam database may be empty and can be updated by application Signed-off-by: Abhay agarwal --- ua-daemon/src/ua-manager-core.c | 110 ++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 37e7453..115ac3a 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -2288,49 +2288,49 @@ int _uam_core_init(void) db_users = _uam_db_get_all_users(); if (!db_users) { UAM_INFO_C("No users in database"); - return UAM_ERROR_NONE; } + else { + for (l = db_users; NULL != l; l = g_slist_next(l)) { + db_user_info_t *info = l->data; + uam_db_user_info_t *user; - for (l = db_users; NULL != l; l = g_slist_next(l)) { - db_user_info_t *info = l->data; - uam_db_user_info_t *user; - - if (!info) - continue; + if (!info) + continue; - user = g_new0(uam_db_user_info_t, 1); - user->user_id = info->user_id; - user->name = g_strdup(info->name); - user->account = g_strdup(info->account); - user->devices = NULL; + user = g_new0(uam_db_user_info_t, 1); + user->user_id = info->user_id; + user->name = g_strdup(info->name); + user->account = g_strdup(info->account); + user->devices = NULL; - users = g_slist_prepend(users, user); + users = g_slist_prepend(users, user); + } } /* Fetch service list */ db_svc_list = _uam_service_db_get_all_services(); if (!db_svc_list) { UAM_INFO_C("No services in database"); - return UAM_ERROR_NONE; } + else { + for (l = db_svc_list; NULL != l; l = g_slist_next(l)) { + db_service_info_t *db_svc = l->data; + uam_db_service_info_t *service; + GSList *l1; - for (l = db_svc_list; NULL != l; l = g_slist_next(l)) { - db_service_info_t *db_svc = l->data; - uam_db_service_info_t *service; - GSList *l1; - - if (!db_svc) - continue; + if (!db_svc) + continue; - l1 = g_slist_find_custom(services, - db_svc->service_name, __compare_svc_name); - if (!l1) { - service = g_new0(uam_db_service_info_t, 1); - service->name = g_strdup(db_svc->service_name); - service->cycle = db_svc->cycle; - service->presence_threshold = db_svc->presence_threshold; - service->absence_threshold = db_svc->absence_threshold; - services = g_slist_append(services, service); + l1 = g_slist_find_custom(services, + db_svc->service_name, __compare_svc_name); + if (!l1) { + service = g_new0(uam_db_service_info_t, 1); + service->name = g_strdup(db_svc->service_name); + service->cycle = db_svc->cycle; + service->presence_threshold = db_svc->presence_threshold; + service->absence_threshold = db_svc->absence_threshold; + services = g_slist_append(services, service); + } } } @@ -2338,35 +2338,35 @@ int _uam_core_init(void) db_devices = _uam_device_db_get_all_devices(); if (!db_devices) { UAM_INFO_C("No Devices registered in database"); - return UAM_ERROR_NONE; } + else { + for (l = db_devices; NULL != l; l = g_slist_next(l)) { + db_device_info_t *db_info = l->data; + uam_db_user_info_t *user; + GSList *svc_list = NULL; + GSList *l1; - for (l = db_devices; NULL != l; l = g_slist_next(l)) { - db_device_info_t *db_info = l->data; - uam_db_user_info_t *user; - GSList *svc_list = NULL; - GSList *l1; - - if (!db_info) - continue; + if (!db_info) + continue; - l1 = g_slist_find_custom(users, + l1 = g_slist_find_custom(users, &(db_info->user_id), __compare_user_id); - if (!l1) { - UAM_ERR("Invalid user Id: %d", db_info->user_id); - continue; - } - user = l1->data; + if (!l1) { + UAM_ERR("Invalid user Id: %d", db_info->user_id); + continue; + } + user = l1->data; - /* Fetch device services from DB */ - l1 = _uam_db_get_device_services( + /* Fetch device services from DB */ + l1 = _uam_db_get_device_services( db_info->dev_info.device_id, db_info->dev_info.type, db_info->dev_info.mac); - svc_list = __convert_db_svc_list_to_uam_svc_list(l1); - __uam_core_add_dev_to_list(user, &(db_info->dev_info), + svc_list = __convert_db_svc_list_to_uam_svc_list(l1); + __uam_core_add_dev_to_list(user, &(db_info->dev_info), db_info->presence_state, db_info->last_seen, svc_list); + } } /* Fetch iBeacon adv list */ @@ -2423,12 +2423,14 @@ int _uam_core_init(void) g_slist_free_full(db_svc_dev_list, g_free); g_slist_free_full(db_payload_list, g_free); - /* Set/update registered device list to plugins */ - if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices)) - UAM_ERR("_uam_pm_set_registered_devices failed"); + if (devices) { + /* Set/update registered device list to plugins */ + if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices)) + UAM_ERR("_uam_pm_set_registered_devices failed"); - /* Set/update registered device list to cloud plugin */ - _uam_cloud_update_registered_devices(); + /* Set/update registered device list to cloud plugin */ + _uam_cloud_update_registered_devices(); + } FUNC_EXIT; return UAM_ERROR_NONE; -- 2.7.4 From 1dfa5fe6fbb98a9d63a3eaa1d231d28e6cc34a7e Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Wed, 18 Dec 2019 14:59:29 +0530 Subject: [PATCH 03/16] Fix copy function of payload Signed-off-by: Abhay agarwal --- ua-daemon/src/ua-manager-core.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 115ac3a..928340c 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -648,7 +648,6 @@ 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 = g_new0(uam_db_payload_info_t, 1); dst_payload->primary_key = src_payload->primary_key; dst_payload->device_icon = src_payload->device_icon; dst_payload->secondary_key = src_payload->secondary_key; @@ -2400,8 +2399,8 @@ int _uam_core_init(void) } else { for (l = db_payload_list; NULL != l; l = g_slist_next(l)) { db_payload_info_t *db_payload = l->data; - uam_db_payload_info_t *payload; - GSList *l1; + uam_db_payload_info_t *payload = g_new0(uam_db_payload_info_t, 1); + GSList *l1 = NULL; if (!db_payload) continue; @@ -3935,7 +3934,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; - uam_db_payload_info_t *db_payload; + uam_db_payload_info_t *db_payload = g_new0(uam_db_payload_info_t, 1); char *mac; retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER); @@ -4010,8 +4009,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); + __uam_copy_db_payload_info(&((*payload_list)[*count]), db_info); *count += 1; } -- 2.7.4 From 588d99d5807281c408bb60450b263203230abb30 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Thu, 19 Dec 2019 17:32:50 +0530 Subject: [PATCH 04/16] Add service-device-payload list in device tech Signed-off-by: Abhay agarwal --- ua-daemon/include/ua-manager-core.h | 18 +-- ua-daemon/include/ua-manager-database.h | 1 + ua-daemon/src/ua-manager-core.c | 174 ++++++++++++++++++--------- ua-daemon/src/ua-manager-device-service-db.c | 27 ++++- 4 files changed, 152 insertions(+), 68 deletions(-) diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 295876b..fcf2c32 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -55,11 +55,20 @@ typedef struct { } uam_db_service_info_t; typedef struct { + char primary_key; /** Primary key */ + char device_icon; /** Device icon */ + char secondary_key; /** Purpose */ + char *device_uid; /** DEVICE_UID */ + char *bt_mac; /** BT MAC Address */ +} uam_db_payload_info_t; + +typedef struct { char *device_id; /**< Device ID */ uam_tech_type_e tech_type; /**< Connectivity type */ char *service; /**< Connected service name */ int discriminant; /**< Service dependent discriminant */ unsigned long long last_seen; /**< Service dependent last seen time */ + uam_db_payload_info_t *payload; /**device service specific payload info */ } uam_svc_dev_info_t; typedef struct { @@ -68,14 +77,6 @@ typedef struct { } uam_db_address_info_t; typedef struct { - char primary_key; /** Primary key */ - char device_icon; /** Device icon */ - char secondary_key; /** Purpose */ - char *device_uid; /** DEVICE_UID */ - char *bt_mac; /** BT MAC Address */ -} uam_db_payload_info_t; - -typedef struct { uam_tech_type_e tech_type; /**< Connectivity type */ GSList *addresses; /**< Address list */ int presence_state; /**< PRESENCE or ABSENCE */ @@ -83,6 +84,7 @@ typedef struct { gboolean discriminant; /**< Decide whether to detect device or not */ struct uam_db_device_info *device; /**< Device info which stored in the DB */ GSList *svc_list; /**< Service list */ + GSList *svc_dev_list; /**< Service device list */ } uam_db_tech_info_t; typedef struct uam_db_device_info { diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 9d5375e..66760ff 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -184,6 +184,7 @@ typedef struct { char svc[UAM_SERVICE_MAX_STRING_LEN]; /**< Service name */ int discriminant; /**< Discriminant for each service */ unsigned long long last_seen; /**< Last seen time for each device of each service */ + uam_ble_payload_s payload_info; /** Payload info for this device service */ } db_svc_dev_info_t; /* db init/deinit */ diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 928340c..5da6d1a 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -193,6 +193,20 @@ static void __print_service(gpointer data, gpointer user_data) FUNC_EXIT; } +static void __print_svc_dev(gpointer data, gpointer user_data) +{ + FUNC_ENTRY; + uam_svc_dev_info_t *svc_dev = data; + + ret_if(NULL == svc_dev); + + UAM_DBG("DevId: %s, Svc: %s, payload primary key: %d", + svc_dev->device_id, svc_dev->service, + svc_dev->payload->primary_key); + + FUNC_EXIT; +} + static void __add_service_to_dev_tech_mapping( uam_db_tech_info_t *tech, uam_db_service_info_t *service) { @@ -658,9 +672,39 @@ static void __uam_copy_uam_payload_info( } } +static GSList *_uam_core_find_svc_dev_list(uam_device_info_s *dev_info) +{ + FUNC_ENTRY; + uam_svc_dev_info_t *svc_dev = NULL; + GSList *l; + GSList *svc_dev_list = NULL; + + retv_if(NULL == dev_info, NULL); + + /* + * Iterate over the svc_devs and find each service device information + */ + for (l = svc_devs; NULL != l; l = g_slist_next(l)) { + svc_dev = l->data; + + if (!svc_dev || !svc_dev->device_id || !svc_dev->service) + continue; + + if ((0 == g_strcmp0(svc_dev->device_id, dev_info->device_id)) && + (svc_dev->tech_type == dev_info->type)) { + svc_dev_list = g_slist_append(svc_dev_list, svc_dev); + UAM_DBG("Service found for device in list"); + } + } + + FUNC_EXIT; + return svc_dev_list; +} + static void __uam_core_add_dev_to_list( uam_db_user_info_t *user, const uam_device_info_s *dev_info, - int presence_state, unsigned long long last_seen, GSList *svc_list) + int presence_state, unsigned long long last_seen, GSList *svc_list, + GSList *svc_dev_list) { FUNC_ENTRY; uam_db_tech_info_t *tech; @@ -716,6 +760,8 @@ static void __uam_core_add_dev_to_list( service->dev_techs = g_slist_prepend(service->dev_techs, tech); } + tech->svc_dev_list = svc_dev_list; + g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL); /* Add tech info to tech list */ device->tech_list = g_slist_append(device->tech_list, tech); @@ -1485,7 +1531,8 @@ static uam_svc_dev_info_t *_uam_core_find_svc_dev_info(const char *device_id, } static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e tech_type, - const char *svc_name, gboolean discriminant, unsigned long long last_seen) + const char *svc_name, gboolean discriminant, unsigned long long last_seen, + uam_ble_payload_s *payload_info) { FUNC_ENTRY; uam_svc_dev_info_t *svc = NULL; @@ -1501,14 +1548,19 @@ static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e svc->device_id = g_strdup(device_id); svc->tech_type = tech_type; svc->service = g_strdup(svc_name); + svc->payload = g_new0(uam_db_payload_info_t, 1); + __uam_copy_uam_payload_info(svc->payload, payload_info); svc_devs = g_slist_append(svc_devs, svc); } svc->discriminant = discriminant; svc->last_seen = last_seen; + __uam_copy_uam_payload_info(svc->payload, payload_info); - UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] last_seen[%llu]", - svc->service, svc->device_id, svc->tech_type, svc->discriminant, svc->last_seen); + UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] " \ + "last_seen [%llu] payload primary key [%d]", + svc->service, svc->device_id, svc->tech_type, svc->discriminant, + svc->last_seen, svc->payload->primary_key); FUNC_EXIT; return UAM_ERROR_NONE; @@ -1597,8 +1649,8 @@ int _uam_core_service_add_user(const char *svc_name, const char *account) __uam_db_end_transaction(0); return ret; } - ret = _uam_core_update_svc_dev_info(device->device_id, - tech->tech_type, service->name, device->discriminant, 0); + ret = _uam_core_update_svc_dev_info(device->device_id, tech->tech_type, + service->name, device->discriminant, 0, NULL); if (UAM_ERROR_NONE != ret) { UAM_WARN("Device service addition to service device mapping failed"); __uam_db_end_transaction(0); @@ -1752,7 +1804,7 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, return ret; } ret = _uam_core_update_svc_dev_info(device_id, tech_type, service->name, - tech_info->discriminant, 0); + tech_info->discriminant, 0, NULL); if (UAM_ERROR_NONE != ret) { UAM_WARN("Device service addition to svc dev mapping failed"); return ret; @@ -2333,6 +2385,43 @@ int _uam_core_init(void) } } + /* Fetch payload list */ + db_payload_list = _uam_db_get_all_payloads(); + if (!db_payload_list) { + UAM_INFO_C("No device payload in database"); + } else { + for (l = db_payload_list; NULL != l; l = g_slist_next(l)) { + db_payload_info_t *db_payload = l->data; + uam_db_payload_info_t *payload = g_new0(uam_db_payload_info_t, 1); + GSList *l1 = NULL; + + if (!db_payload) + continue; + +// l1 = g_slist_find_custom(payloads, +// db_payload->payload_name, __compare_payload_name); + if (!l1) { + payload = g_new0(uam_db_payload_info_t, 1); + __uam_copy_uam_payload_info(payload, &(db_payload->payload_info)); + payloads = g_slist_append(payloads, payload); + } + } + } + + /* 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"); + } 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(); if (!db_devices) { @@ -2343,6 +2432,7 @@ int _uam_core_init(void) db_device_info_t *db_info = l->data; uam_db_user_info_t *user; GSList *svc_list = NULL; + GSList *svc_dev_list = NULL; GSList *l1; if (!db_info) @@ -2363,8 +2453,11 @@ int _uam_core_init(void) db_info->dev_info.mac); svc_list = __convert_db_svc_list_to_uam_svc_list(l1); + + svc_dev_list = _uam_core_find_svc_dev_list(&(db_info->dev_info)); __uam_core_add_dev_to_list(user, &(db_info->dev_info), - db_info->presence_state, db_info->last_seen, svc_list); + db_info->presence_state, db_info->last_seen, + svc_list, svc_dev_list); } } @@ -2379,42 +2472,6 @@ 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"); - } 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); - } - } - - /* Fetch payload list */ - db_payload_list = _uam_db_get_all_payloads(); - if (!db_payload_list) { - UAM_INFO_C("No device payload in database"); - } else { - for (l = db_payload_list; NULL != l; l = g_slist_next(l)) { - db_payload_info_t *db_payload = l->data; - uam_db_payload_info_t *payload = g_new0(uam_db_payload_info_t, 1); - GSList *l1 = NULL; - - if (!db_payload) - continue; - -// l1 = g_slist_find_custom(payloads, -// db_payload->payload_name, __compare_payload_name); - if (!l1) { - payload = g_new0(uam_db_payload_info_t, 1); - __uam_copy_uam_payload_info(payload, &(db_payload->payload_info)); - payloads = g_slist_append(payloads, payload); - } - } - } - g_slist_free_full(db_devices, g_free); g_slist_free_full(db_users, g_free); g_slist_free_full(db_svc_list, g_free); @@ -2487,6 +2544,7 @@ void _uam_core_deinit(void) g_free(svc_dev->device_id); g_free(svc_dev->service); + g_free(svc_dev->payload); g_free(svc_dev); } g_slist_free(svc_devs); @@ -2531,7 +2589,9 @@ int _uam_core_handle_device_added(int status, GSList *l; int ret = UAM_ERROR_NONE; GSList *svc_list = NULL; + GSList *svc_dev_list = NULL; uam_db_user_info_t *user = NULL; + uam_svc_dev_info_t *svc_dev = NULL; UAM_INFO("[%d]", user_id); @@ -2611,9 +2671,22 @@ int _uam_core_handle_device_added(int status, svc_list = g_slist_append(svc_list, service); } - __uam_core_add_dev_to_list(user, dev_info, - UAM_PRESENCE_STATE_PRESENT, dev_info->last_seen, svc_list); + /** updates for svc dev*/ + ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type, + UAM_SERVICE_DEFAULT, dev_info->discriminant, 0, NULL); + if (UAM_ERROR_NONE != ret) { + UAM_WARN("Device service mappiing update failed"); + __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info); + return ret; + } + + svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id, + dev_info->type, UAM_SERVICE_DEFAULT); + svc_dev_list = g_slist_append(svc_dev_list, svc_dev); + __uam_core_add_dev_to_list(user, dev_info, UAM_PRESENCE_STATE_PRESENT, + dev_info->last_seen, svc_list, svc_dev_list); + /** Start database transaction */ retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED); /* Add device to database */ @@ -2637,14 +2710,7 @@ int _uam_core_handle_device_added(int status, return ret; } __uam_db_end_transaction(1); - - ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type, - UAM_SERVICE_DEFAULT, dev_info->discriminant, 0); - if (UAM_ERROR_NONE != ret) { - UAM_WARN("Device service mappiing update failed"); - __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info); - return ret; - } + /** End database transaction */ /* Send device added event to application */ __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info); diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index 024e50c..2ba41c1 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -32,9 +32,10 @@ "FROM device_services where device_number = ?" #define SELECT_DEVICE_SERVICES \ - "SELECT D.device_id, D.tech_type, S.service_name, P.discriminant, P.last_seen " \ - "FROM device_services as P JOIN services as S USING(service_number) JOIN devices as D " \ - "USING(device_number)" + "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" \ + "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 INSERT_DEVICE_SERVICE "insert into device_services " \ "(device_number, service_number, discriminant, last_seen) values (?, ?, ?, ?)" @@ -775,6 +776,7 @@ GSList *_uam_db_get_service_devices_info() GSList *svc_dev_list = NULL; db_svc_dev_info_t *info = NULL; int sql_ret = SQLITE_OK; + char *buf = NULL; do { sql_ret = sqlite3_step(stmt); @@ -792,13 +794,26 @@ GSList *_uam_db_get_service_devices_info() UAM_SERVICE_MAX_STRING_LEN); info->discriminant = sqlite3_column_int(stmt, 3); info->last_seen = sqlite3_column_int64(stmt, 4); - - UAM_INFO("%s-%d-%s-%d-%llu", + buf = (char *)sqlite3_column_text(stmt, 5); + 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); + if (buf) + memcpy(info->payload_info.device_uid, buf, + UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + buf = (char *)sqlite3_column_text(stmt, 8); + info->payload_info.device_icon = buf ? *buf : 1; + + UAM_INFO("device id:%s-%d-%s-%d-%llu " \ + "primary key: %d secondary key: %d", info->device_id, info->type, info->svc, info->discriminant, - info->last_seen); + info->last_seen, + info->payload_info.primary_key, + info->payload_info.secondary_key); svc_dev_list = g_slist_append(svc_dev_list, info); break; -- 2.7.4 From 5952d419acf22251437a80b5009c8957e62edbce Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Fri, 20 Dec 2019 18:19:30 +0530 Subject: [PATCH 05/16] Update svc_dev info while adding payload to service Signed-off-by: Abhay agarwal --- ua-daemon/include/ua-manager-core.h | 2 + ua-daemon/include/ua-manager-database.h | 2 + ua-daemon/src/pm/ua-pm-util.c | 2 + ua-daemon/src/ua-manager-core.c | 72 +++++++++++++++++++++++++++------ ua-daemon/src/ua-manager-payload-db.c | 8 +++- ua-plugins/include/ua-plugin.h | 6 +-- 6 files changed, 75 insertions(+), 17 deletions(-) diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index fcf2c32..a26762f 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -60,6 +60,8 @@ typedef struct { char secondary_key; /** Purpose */ char *device_uid; /** DEVICE_UID */ char *bt_mac; /** BT MAC Address */ + char *device_id; /**< Device ID */ + uam_tech_type_e tech_type; /**< Connectivity type */ } uam_db_payload_info_t; typedef struct { diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 66760ff..07cf3fc 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -251,6 +251,8 @@ int _uam_adv_db_clear(void); /* PAYLOAD QUERIES */ typedef struct { uam_ble_payload_s payload_info; + char device_id[UAM_DEVICE_ID_MAX_STRING_LEN]; + uam_tech_type_e type; } db_payload_info_t; /* db init/deinit */ diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 6fbe543..6bab1d5 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -257,6 +257,8 @@ uas_device_info_t *_pm_util_uam_dev_info_to_uas_dev_info(const uam_device_info_s device->addr_list[i++].address = ipv4_addr; } + device->payload = NULL; + FUNC_EXIT; return device; } diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 5da6d1a..a29582f 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -100,6 +100,34 @@ static gint __compare_svc_name(gconstpointer data, gconstpointer user_data) return g_strcmp0(service->name, svc_name); } +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; + + 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))) + return 0; + return -1; +} + +static gint __compare_db_payload(gconstpointer data, gconstpointer user_data) +{ + const uam_db_payload_info_t *db_payload = data; + const uam_ble_payload_s *payload = user_data; + + 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))) + return 0; + return -1; +} + static void __free_address_info(gpointer data) { FUNC_ENTRY; @@ -1553,9 +1581,12 @@ static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e svc_devs = g_slist_append(svc_devs, svc); } - svc->discriminant = discriminant; - svc->last_seen = last_seen; - __uam_copy_uam_payload_info(svc->payload, payload_info); + if (discriminant >= 0) + svc->discriminant = discriminant; + if (last_seen >= 0) + svc->last_seen = last_seen; + if (payload_info) + __uam_copy_uam_payload_info(svc->payload, payload_info); UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] " \ "last_seen [%llu] payload primary key [%d]", @@ -2398,11 +2429,12 @@ int _uam_core_init(void) if (!db_payload) continue; -// l1 = g_slist_find_custom(payloads, -// db_payload->payload_name, __compare_payload_name); + l1 = g_slist_find_custom(payloads, db_payload, __compare_db_payload); if (!l1) { payload = g_new0(uam_db_payload_info_t, 1); __uam_copy_uam_payload_info(payload, &(db_payload->payload_info)); + payload->device_id = g_strdup(db_payload->device_id); + payload->tech_type = db_payload->type; payloads = g_slist_append(payloads, payload); } } @@ -4014,6 +4046,11 @@ int _uam_core_add_payload(uam_ble_payload_s *payload, retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER); mac = __get_mac_addr(tech_info); + __uam_copy_uam_payload_info(db_payload, payload); + db_payload->device_id = g_strdup(device_id); + db_payload->tech_type = tech_type; + payloads = g_slist_append(payloads, db_payload); + /*** Add payload to database ***/ ret = _uam_db_insert_payload_info(device_id, tech_type, mac, payload); if (UAM_ERROR_NONE != ret) { @@ -4021,9 +4058,6 @@ int _uam_core_add_payload(uam_ble_payload_s *payload, return ret; } - __uam_copy_uam_payload_info(db_payload, payload); - payloads = g_slist_append(payloads, db_payload); - FUNC_EXIT; return UAM_ERROR_NONE; } @@ -4034,20 +4068,34 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload, FUNC_ENTRY; int ret = UAM_ERROR_NONE; uam_db_service_info_t *service; + uam_db_payload_info_t *db_payload; + uam_svc_dev_info_t *svc_dev; GSList *l; retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER); retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER); - // check if payload exist - // check if service exist - // check if device-service exist - /* Retrieve service from list */ l = g_slist_find_custom(services, svc_name, __compare_svc_name); retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER); service = l->data; + /* Retrieve payload from list */ + l = g_slist_find_custom(payloads, payload, __compare_payload); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER); + db_payload = l->data; + + // check if this device-service pair exist + + /**Update svc_dev*/ + _uam_core_update_svc_dev_info(db_payload->device_id, db_payload->tech_type, + service->name, -1, -1, payload); + svc_dev = _uam_core_find_svc_dev_info(db_payload->device_id, + db_payload->tech_type, service->name); + retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER); + /** Update svc_dev list for device*/ + /** Update registered devices for ble*/ + /*** Add payload service mapping to database ***/ ret = _uam_db_update_device_service_payload_info(payload, service->name); if (UAM_ERROR_NONE != ret) { diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index 1837dcb..8c60583 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -24,8 +24,9 @@ #include "ua-manager-common.h" #include "ua-manager-database.h" -#define SELECT_ALL_PAYLOADS "SELECT primary_key, secondary_key, " \ - "device_uid, device_icon FROM payloads" +#define SELECT_ALL_PAYLOADS "SELECT P.primary_key, P.secondary_key, " \ + "P.device_uid, P.device_icon, D.device_id, D.tech_type " \ + "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 = ?" @@ -416,6 +417,9 @@ GSList *_uam_db_get_all_payloads(void) UAM_BLE_PAYLOAD_DEVICE_UID_LEN); buf = (char *)sqlite3_column_text(stmt, 3); info->payload_info.device_icon = buf ? *buf : 1; + g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 4), + UAM_DEVICE_ID_MAX_STRING_LEN); + info->type = sqlite3_column_int(stmt, 5); UAM_INFO("primary key: %d secondary key: %d", info->payload_info.primary_key, diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index ccdcc7d..3cc420e 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -103,10 +103,10 @@ typedef struct { typedef struct { char primary_key; /** Primary Key */ char device_icon; /** Device icon */ - char secondary_key; /** Purpose */ + char secondary_key; /** Secondary key */ char *device_uid; /** DEVICE_UID */ char *bt_mac; /** BT MAC Address */ -} uas_ble_payload_t; +} uas_payload_info_t; /* Device information structure */ typedef struct { @@ -118,7 +118,7 @@ typedef struct { uas_address_info_t *addr_list; /**< Address list (IP, Subnet Mask, Gateway etc) */ int discriminant; /**< Determines whether to judge PRESENCE/ABSENCE */ unsigned long long last_seen; /**< Last seen time */ - uas_ble_payload_t *payload; /**< BLE payload for BLE device filtering */ + uas_payload_info_t *payload; /**< Payload for device filtering */ } uas_device_info_t; /* Sensor information structure */ -- 2.7.4 From ac181e06beb04bc074c8e62ea58dfce0bdec71e9 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Mon, 23 Dec 2019 17:46:10 +0530 Subject: [PATCH 06/16] Update registered device list to plugin with payload information Signed-off-by: Abhay agarwal --- ua-daemon/src/pm/ua-pm-util.c | 30 ++++++++++++++++++++++++++++ ua-daemon/src/ua-manager-core.c | 26 +++++++++++++++++++++--- ua-daemon/src/ua-manager-device-service-db.c | 2 +- ua-plugins/include/ua-plugin.h | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 6bab1d5..b698b27 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -131,6 +131,21 @@ uas_address_type_e _pm_util_uam_addr_type_to_uas_addr_type(uam_addr_type_e type) } } +void _pm_util_uam_db_payload_to_uas_payload( + uas_payload_info_t *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 = g_memdup(&(src_payload->device_uid), + UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac), + UAM_BT_MAC_ADDRESS_STRING_LEN); + } + UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); +} + void _pm_util_uas_device_info_free(uas_device_info_t *device) { FUNC_ENTRY; @@ -153,6 +168,7 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type, FUNC_ENTRY; int i = 0; GSList *l; + int len = 0; ret_if(NULL == dev); ret_if(NULL == dev->user); @@ -190,6 +206,20 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type, (*device)->addr_list[i].type = _pm_util_uam_addr_type_to_uas_addr_type(addr->addr_type); (*device)->addr_list[i++].address = g_strdup(addr->address); } + + /** update payload list */ + len = g_slist_length(tech->svc_dev_list); + (*device)->num_payload = len; + (*device)->payload = g_new0(uas_payload_info_t, len); + for (l1 = tech->svc_dev_list; NULL != l1; l1 = g_slist_next(l1)) { + uam_svc_dev_info_t *svc_dev = l1->data; + uam_db_payload_info_t *payload = svc_dev->payload; + + if (!payload) + continue; + + _pm_util_uam_db_payload_to_uas_payload(&(*device)->payload[i], payload); + } } FUNC_EXIT; diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index a29582f..05fb7b5 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -328,7 +328,7 @@ static int __copy_tech_info_to_device_info(uam_db_tech_info_t *tech, uam_device_ device->operating_system = tech->device->os; g_strlcpy(device->device_id, tech->device->device_id, - UAM_DEVICE_ID_MAX_STRING_LEN); + UAM_DEVICE_ID_MAX_STRING_LEN); device->type = tech->tech_type; device->discriminant = tech->discriminant; device->last_seen = tech->last_seen; @@ -4070,6 +4070,7 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload, uam_db_service_info_t *service; uam_db_payload_info_t *db_payload; uam_svc_dev_info_t *svc_dev; + uam_db_device_info_t *device; GSList *l; retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER); @@ -4093,8 +4094,27 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload, svc_dev = _uam_core_find_svc_dev_info(db_payload->device_id, db_payload->tech_type, service->name); retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER); - /** Update svc_dev list for device*/ - /** Update registered devices for ble*/ + + /**Update svc_dev list for device*/ + l = g_slist_find_custom(devices, db_payload->device_id, __compare_device_id); + device = l->data; + + for (l = device->tech_list; NULL != l; l = g_slist_next(l)) { + uam_db_tech_info_t *tech = l->data; + + if (!tech) + continue; + + if (db_payload->tech_type != tech->tech_type) + continue; + + tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev); + } + + /* Set/update registered device list to plugins */ + if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices)) + UAM_ERR("_uam_pm_set_registered_devices failed"); + /*** Add payload service mapping to database ***/ ret = _uam_db_update_device_service_payload_info(payload, service->name); diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index 2ba41c1..2696b8d 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -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, 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)" diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index 3cc420e..0059618 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -118,6 +118,7 @@ typedef struct { uas_address_info_t *addr_list; /**< Address list (IP, Subnet Mask, Gateway etc) */ int discriminant; /**< Determines whether to judge PRESENCE/ABSENCE */ unsigned long long last_seen; /**< Last seen time */ + int num_payload; /**< The number of payloads */ uas_payload_info_t *payload; /**< Payload for device filtering */ } uas_device_info_t; -- 2.7.4 From b67e7bdefd11ce2eb26fb8a921954f6d5c805fba Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Mon, 23 Dec 2019 19:15:09 +0530 Subject: [PATCH 07/16] update svc_dev list while registering device to service Signed-off-by: Abhay agarwal --- ua-daemon/src/pm/ua-pm-util.c | 2 ++ ua-daemon/src/ua-manager-core.c | 47 +++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index b698b27..964b247 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -211,6 +211,7 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type, len = g_slist_length(tech->svc_dev_list); (*device)->num_payload = len; (*device)->payload = g_new0(uas_payload_info_t, len); + i = 0; for (l1 = tech->svc_dev_list; NULL != l1; l1 = g_slist_next(l1)) { uam_svc_dev_info_t *svc_dev = l1->data; uam_db_payload_info_t *payload = svc_dev->payload; @@ -219,6 +220,7 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type, continue; _pm_util_uam_db_payload_to_uas_payload(&(*device)->payload[i], payload); + i++; } } diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 05fb7b5..e96df7b 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -250,6 +250,20 @@ static void __add_service_to_dev_tech_mapping( FUNC_EXIT; } +static void __add_svc_dev_to_dev_tech_mapping( + uam_db_tech_info_t *tech, uam_svc_dev_info_t *svc_dev) +{ + FUNC_ENTRY; + + ret_if(NULL == tech); + ret_if(NULL == svc_dev); + + tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev); + g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL); + + FUNC_EXIT; +} + static void __remove_service_to_dev_tech_mapping( uam_db_tech_info_t *tech, uam_db_service_info_t *service) { @@ -1807,6 +1821,7 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, int ret = UAM_ERROR_NONE; uam_db_tech_info_t *tech_info; uam_db_service_info_t *service; + uam_svc_dev_info_t *svc_dev = NULL; retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER); retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER); @@ -1826,6 +1841,11 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, __add_service_to_dev_tech_mapping(tech_info, service); + _uam_core_update_svc_dev_info(device_id, tech_type, + service->name, -1, -1, NULL); + svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, service->name); + __add_svc_dev_to_dev_tech_mapping(tech_info, svc_dev); + mac = __get_mac_addr(tech_info); /* Insert device service info to db */ ret = _uam_db_insert_device_service_info(device_id, tech_type, mac, @@ -4070,7 +4090,6 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload, uam_db_service_info_t *service; uam_db_payload_info_t *db_payload; uam_svc_dev_info_t *svc_dev; - uam_db_device_info_t *device; GSList *l; retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER); @@ -4086,36 +4105,22 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload, retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER); db_payload = l->data; - // check if this device-service pair exist + /* Retrieve device from list */ + l = g_slist_find_custom(devices, db_payload->device_id, __compare_device_id); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER); - /**Update svc_dev*/ - _uam_core_update_svc_dev_info(db_payload->device_id, db_payload->tech_type, - service->name, -1, -1, payload); svc_dev = _uam_core_find_svc_dev_info(db_payload->device_id, db_payload->tech_type, service->name); retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER); - /**Update svc_dev list for device*/ - l = g_slist_find_custom(devices, db_payload->device_id, __compare_device_id); - device = l->data; - - for (l = device->tech_list; NULL != l; l = g_slist_next(l)) { - uam_db_tech_info_t *tech = l->data; - - if (!tech) - continue; - - if (db_payload->tech_type != tech->tech_type) - continue; - - tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev); - } + /**Update svc_dev*/ + _uam_core_update_svc_dev_info(db_payload->device_id, db_payload->tech_type, + service->name, -1, -1, payload); /* Set/update registered device list to plugins */ if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices)) UAM_ERR("_uam_pm_set_registered_devices failed"); - /*** Add payload service mapping to database ***/ ret = _uam_db_update_device_service_payload_info(payload, service->name); if (UAM_ERROR_NONE != ret) { -- 2.7.4 From 8a86881a3a80af09de5d56a0b696cc5eaa603703 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 24 Dec 2019 15:09:00 +0530 Subject: [PATCH 08/16] update handle device detected for payload handling Signed-off-by: Abhay agarwal --- ua-daemon/include/ua-manager-core.h | 2 +- ua-daemon/src/pm/ua-ble-plugin-handler.c | 14 +++++++++++--- ua-daemon/src/pm/ua-light-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-motion-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-pm-util.c | 17 +++++++++++++++++ ua-daemon/src/pm/ua-pm-util.h | 8 ++++++++ ua-daemon/src/pm/ua-wifi-plugin-handler.c | 6 ++++-- ua-daemon/src/ua-manager-core.c | 23 +++++++++++++++++------ 8 files changed, 62 insertions(+), 14 deletions(-) diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index a26762f..7e881f5 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -187,7 +187,7 @@ int _uam_core_handle_device_added(int status, int user_id, const uam_device_info_s *dev_info); int _uam_core_handle_presence_detected(unsigned int sensor, - int user_id, void *sensor_info); + int user_id, void *sensor_info, uam_ble_payload_s *payload); void _uam_core_handle_absence_detected(unsigned int sensor, int user_id, void *sensor_info); diff --git a/ua-daemon/src/pm/ua-ble-plugin-handler.c b/ua-daemon/src/pm/ua-ble-plugin-handler.c index da39bbe..c4d62e2 100644 --- a/ua-daemon/src/pm/ua-ble-plugin-handler.c +++ b/ua-daemon/src/pm/ua-ble-plugin-handler.c @@ -46,7 +46,8 @@ static void ble_lpm_detection_callback(uas_detection_type_e type, void *sensor_i /* Currently, in lpm mode only Presence is detected */ if (UAS_PRESENCE == type) { UAM_DBG("Presence detected"); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE, -1, NULL); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE, + -1, NULL, NULL); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { @@ -72,6 +73,7 @@ static void ble_device_detection_callback(uas_detection_type_e type, uas_device_ { FUNC_ENTRY; uam_device_info_s *dev_info; + uam_ble_payload_s *payload = NULL; int ret = UAM_ERROR_NONE; ret_if(NULL == device); @@ -79,9 +81,15 @@ static void ble_device_detection_callback(uas_detection_type_e type, uas_device_ dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device); ret_if(NULL == dev_info); + if (device->payload) { + payload = g_new0(uam_ble_payload_s, 1); + _pm_util_uas_payload_to_uam_payload(payload, device->payload); + } + if (UAS_PRESENCE == type) { - UAM_DBG("Presence detected for %s", dev_info->device_id); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE, device->user_id, dev_info); + UAM_DBG("BLE Presence detected for %s", dev_info->device_id); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE, + device->user_id, dev_info, payload); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c index ee5032d..0dcdb9a 100644 --- a/ua-daemon/src/pm/ua-light-plugin-handler.c +++ b/ua-daemon/src/pm/ua-light-plugin-handler.c @@ -57,7 +57,8 @@ void light_detection_callback(uas_detection_type_e type, void *sensor_info) if (UAS_PRESENCE == type) { UAM_DBG("Presence detected by light sensor"); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_LIGHT, 0, light_info); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_LIGHT, + 0, light_info, NULL); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c index 8dac1cf..195d5e1 100644 --- a/ua-daemon/src/pm/ua-motion-plugin-handler.c +++ b/ua-daemon/src/pm/ua-motion-plugin-handler.c @@ -80,7 +80,8 @@ void motion_detection_callback(uas_detection_type_e type, void *sensor_info) if (UAS_PRESENCE == type) { UAM_DBG("Presence detected by motion sensor"); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_MOTION, 0, motion_info); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_MOTION, + 0, motion_info, NULL); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 964b247..82c749a 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -146,6 +146,23 @@ void _pm_util_uam_db_payload_to_uas_payload( UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); } +void _pm_util_uas_payload_to_uam_payload( + uam_ble_payload_s *dst_payload, uas_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; + if (src_payload->device_uid) + memcpy(dst_payload->device_uid, + src_payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + if (src_payload->bt_mac) + memcpy(dst_payload->bt_mac, + src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN); + } + UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); +} + void _pm_util_uas_device_info_free(uas_device_info_t *device) { FUNC_ENTRY; diff --git a/ua-daemon/src/pm/ua-pm-util.h b/ua-daemon/src/pm/ua-pm-util.h index 3be4a94..e45d73f 100644 --- a/ua-daemon/src/pm/ua-pm-util.h +++ b/ua-daemon/src/pm/ua-pm-util.h @@ -53,6 +53,14 @@ uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_sca uam_sensor_info_s *_pm_util_uas_sensor_info_to_uam_sensor_info(const uas_sensor_info_t *info); +void _pm_util_uam_db_payload_to_uas_payload( + uas_payload_info_t *dst_payload, + uam_db_payload_info_t *src_payload); + +void _pm_util_uas_payload_to_uam_payload( + uam_ble_payload_s *dst_payload, + uas_payload_info_t *src_payload); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index 297075d..53f5df2 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -45,7 +45,8 @@ static void wifi_lpm_detection_callback(uas_detection_type_e type, void *sensor_ /* Currently, in lpm mode only Presence is detected */ if (UAS_PRESENCE == type) { UAM_DBG("Presence detected"); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI, -1, NULL); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI, + -1, NULL, NULL); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { @@ -82,7 +83,8 @@ static void wifi_device_detection_callback(uas_detection_type_e type, uas_device if (UAS_PRESENCE == type) { UAM_DBG("Presence detected for %s", dev_info->device_id); - ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI, device->user_id, dev_info); + ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI, + device->user_id, dev_info, NULL); if (UAM_ERROR_NONE != ret) UAM_WARN("_uam_core_handle_presence_detected failed"); } else { diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index e96df7b..5f8c69d 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -114,10 +114,11 @@ static gint __compare_payload(gconstpointer data, gconstpointer user_data) return -1; } +#if 0 static gint __compare_db_payload(gconstpointer data, gconstpointer user_data) { const uam_db_payload_info_t *db_payload = data; - const uam_ble_payload_s *payload = user_data; + const uam_db_payload_info_t *payload = user_data; retv_if(NULL == db_payload, -1); retv_if(NULL == payload, -1); @@ -127,6 +128,7 @@ static gint __compare_db_payload(gconstpointer data, gconstpointer user_data) return 0; return -1; } +#endif static void __free_address_info(gpointer data) { @@ -2449,7 +2451,8 @@ int _uam_core_init(void) if (!db_payload) continue; - l1 = g_slist_find_custom(payloads, db_payload, __compare_db_payload); + l1 = g_slist_find_custom(payloads, &(db_payload->payload_info), + __compare_payload); if (!l1) { payload = g_new0(uam_db_payload_info_t, 1); __uam_copy_uam_payload_info(payload, &(db_payload->payload_info)); @@ -2917,7 +2920,7 @@ static void __send_sensor_absence_event(uam_sensor_info_s *sensor_info, } void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor, - uam_device_info_s *dev_info) + uam_device_info_s *dev_info, uam_ble_payload_s *payload) { FUNC_ENTRY; @@ -2955,6 +2958,14 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor, if (!svc_dev || !svc_dev->discriminant) continue; + /** Compare device payload*/ + /** If payload is NULL, send event to all services */ + 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)) + continue; + UAM_DBG("Send event"); for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) { uam_monitor_info_t *mon = l1->data; @@ -3003,7 +3014,7 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor, } int _uam_core_handle_presence_detected(unsigned int sensor, - int user_id, void *info) + int user_id, void *info, uam_ble_payload_s *payload) { FUNC_ENTRY; int ret = UAM_ERROR_NONE; @@ -3136,9 +3147,9 @@ int _uam_core_handle_presence_detected(unsigned int sensor, return ret; } - /* Send user presence event and update service_device timestamp */ - __send_user_presence_event(tech, sensor, dev_info); __uam_db_end_transaction(1); + /* Send user presence event and update service_device timestamp */ + __send_user_presence_event(tech, sensor, dev_info, payload); FUNC_EXIT; return ret; -- 2.7.4 From 305be8a6d91378c8c137a5deeb52569d6c9d29ca Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 24 Dec 2019 19:04:28 +0530 Subject: [PATCH 09/16] Fix payload copy functions Signed-off-by: Abhay agarwal --- ua-daemon/src/pm/ua-pm-util.c | 6 +++--- ua-daemon/src/ua-manager-core.c | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 82c749a..b935548 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -138,9 +138,9 @@ void _pm_util_uam_db_payload_to_uas_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 = g_memdup(&(src_payload->device_uid), + dst_payload->device_uid = g_memdup((src_payload->device_uid), UAM_BLE_PAYLOAD_DEVICE_UID_LEN); - dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac), + dst_payload->bt_mac = g_memdup((src_payload->bt_mac), UAM_BT_MAC_ADDRESS_STRING_LEN); } UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); @@ -236,7 +236,7 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type, if (!payload) continue; - _pm_util_uam_db_payload_to_uas_payload(&(*device)->payload[i], payload); + _pm_util_uam_db_payload_to_uas_payload(&((*device)->payload[i]), payload); i++; } } diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 5f8c69d..7726818 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -237,6 +237,15 @@ 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]", + payload->primary_key, payload->secondary_key, payload->device_icon); + for (int i = 0; i < UAM_BLE_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) { @@ -693,6 +702,8 @@ static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_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; + memset(dst_payload->device_uid, 0, UAM_BLE_PAYLOAD_DEVICE_UID_LEN + 1); + 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, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); @@ -4072,6 +4083,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload, retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER); // check if payload already exist + __print_payload(payload); tech_info = __uam_core_get_dev_tech_info(device_id, tech_type); retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER); -- 2.7.4 From 7baa2961de13fdaa41531ec3f5092f41e0401fd5 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Thu, 26 Dec 2019 09:54:44 +0530 Subject: [PATCH 10/16] Set length for payload device uid Signed-off-by: Abhay agarwal --- include/ua-api.h | 12 +++++++++-- ua-daemon/data/ua_db.sql | 1 + ua-daemon/include/ua-manager-core.h | 2 ++ ua-daemon/src/pm/ua-pm-util.c | 10 +++++---- ua-daemon/src/ua-manager-core.c | 25 +++++++++++----------- ua-daemon/src/ua-manager-db.c | 1 + ua-daemon/src/ua-manager-device-service-db.c | 2 +- ua-daemon/src/ua-manager-payload-db.c | 31 +++++++++++++++------------- ua-plugins/include/ua-plugin.h | 2 ++ 9 files changed, 53 insertions(+), 33 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index f02da1c..26ab5c2 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -59,7 +59,13 @@ typedef enum { * @brief Device's BLE payload DEVICE_UID length. * @since_tizen 5.5 */ -#define UAM_BLE_PAYLOAD_DEVICE_UID_LEN 17 +#define UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN 17 + +/** + * @brief Device's BLE payload user_data max length. + * @since_tizen 6.0 + */ +#define UAM_PAYLOAD_USER_DATA_MAX_LEN 17 /** * @brief Max. BT MAC address length. @@ -487,8 +493,10 @@ typedef struct { char primary_key; /** Primary Key */ char device_icon; /** Device icon */ char secondary_key; /** Purpose */ - char device_uid[UAM_BLE_PAYLOAD_DEVICE_UID_LEN + 1]; /** DEVICE_UID */ + int device_uid_len; /** Device uid length */ + char device_uid[UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1]; /** DEVICE_UID */ char bt_mac[UAM_BT_MAC_ADDRESS_STRING_LEN]; /** BT MAC Address */ + char user_data[UAM_PAYLOAD_USER_DATA_MAX_LEN + 1]; /** User Data */ } uam_ble_payload_s; /** diff --git a/ua-daemon/data/ua_db.sql b/ua-daemon/data/ua_db.sql index c83282d..ab1fe7b 100644 --- a/ua-daemon/data/ua_db.sql +++ b/ua-daemon/data/ua_db.sql @@ -44,6 +44,7 @@ CREATE TABLE IF NOT EXISTS payloads ( payload_number INTEGER PRIMARY KEY AUTOINCREMENT, primary_key TEXT, secondary_key TEXT, + device_uid_len INTEGER, device_uid TEXT, device_icon TEXT, device_number INTEGER, diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 7e881f5..077f7a0 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -58,7 +58,9 @@ typedef struct { char primary_key; /** Primary key */ char device_icon; /** Device icon */ char secondary_key; /** Purpose */ + int device_uid_len; /** Device uid length */ char *device_uid; /** DEVICE_UID */ + char *user_data; /** User data */ char *bt_mac; /** BT MAC Address */ char *device_id; /**< Device ID */ uam_tech_type_e tech_type; /**< Connectivity type */ diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index b935548..be1e3f5 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -138,12 +138,12 @@ void _pm_util_uam_db_payload_to_uas_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), - UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + src_payload->device_uid_len); dst_payload->bt_mac = g_memdup((src_payload->bt_mac), UAM_BT_MAC_ADDRESS_STRING_LEN); } - UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); } void _pm_util_uas_payload_to_uam_payload( @@ -153,14 +153,16 @@ void _pm_util_uas_payload_to_uam_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, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + 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); } - UAM_DBG("payload primary key: [%d]", dst_payload->primary_key); } void _pm_util_uas_device_info_free(uas_device_info_t *device) diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 7726818..c01a6a9 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -240,9 +240,11 @@ static void __print_svc_dev(gpointer data, gpointer user_data) void __print_payload(uam_ble_payload_s *payload) { ret_if(NULL == payload); - UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d]", - payload->primary_key, payload->secondary_key, payload->device_icon); - for (int i = 0; i < UAM_BLE_PAYLOAD_DEVICE_UID_LEN; i++) + 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]); } @@ -702,11 +704,12 @@ static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_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; - memset(dst_payload->device_uid, 0, UAM_BLE_PAYLOAD_DEVICE_UID_LEN + 1); + 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, UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + 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); @@ -720,8 +723,9 @@ static void __uam_copy_uam_payload_info( 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), - UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + src_payload->device_uid_len); dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac), UAM_BT_MAC_ADDRESS_STRING_LEN); } @@ -2403,8 +2407,7 @@ int _uam_core_init(void) db_users = _uam_db_get_all_users(); if (!db_users) { UAM_INFO_C("No users in database"); - } - else { + } else { for (l = db_users; NULL != l; l = g_slist_next(l)) { db_user_info_t *info = l->data; uam_db_user_info_t *user; @@ -2426,8 +2429,7 @@ int _uam_core_init(void) db_svc_list = _uam_service_db_get_all_services(); if (!db_svc_list) { UAM_INFO_C("No services in database"); - } - else { + } else { for (l = db_svc_list; NULL != l; l = g_slist_next(l)) { db_service_info_t *db_svc = l->data; uam_db_service_info_t *service; @@ -2492,8 +2494,7 @@ int _uam_core_init(void) db_devices = _uam_device_db_get_all_devices(); if (!db_devices) { UAM_INFO_C("No Devices registered in database"); - } - else { + } else { for (l = db_devices; NULL != l; l = g_slist_next(l)) { db_device_info_t *db_info = l->data; uam_db_user_info_t *user; diff --git a/ua-daemon/src/ua-manager-db.c b/ua-daemon/src/ua-manager-db.c index de1a01d..a250c0f 100644 --- a/ua-daemon/src/ua-manager-db.c +++ b/ua-daemon/src/ua-manager-db.c @@ -58,6 +58,7 @@ "payload_number INTEGER PRIMARY KEY AUTOINCREMENT, " \ "primary_key TEXT, " \ "secondary_key TEXT, " \ + "device_uid_len INTEGER, " \ "device_uid TEXT, " \ "device_icon TEXT, " \ "device_number INTEGER, " \ diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index 2696b8d..f0e9a22 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -801,7 +801,7 @@ GSList *_uam_db_get_service_devices_info() buf = (char *)sqlite3_column_text(stmt, 7); if (buf) memcpy(info->payload_info.device_uid, buf, - UAM_BLE_PAYLOAD_DEVICE_UID_LEN); + UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN); buf = (char *)sqlite3_column_text(stmt, 8); info->payload_info.device_icon = buf ? *buf : 1; diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index 8c60583..553d470 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -25,8 +25,8 @@ #include "ua-manager-database.h" #define SELECT_ALL_PAYLOADS "SELECT P.primary_key, P.secondary_key, " \ - "P.device_uid, P.device_icon, D.device_id, D.tech_type " \ - "FROM payloads as P JOIN devices as D USING(device_number)" + "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)" #define SELECT_PAYLOAD_DEVICE "SELECT device_number FROM payloads " \ "where primary_key = ? AND secondary_key = ? AND device_uid = ?" @@ -34,7 +34,7 @@ "where primary_key = ? AND secondary_key = ? AND device_uid = ?" #define INSERT_PAYLOAD "insert into payloads (primary_key, " \ - "secondary_key, device_uid, device_icon, device_number)" \ + "secondary_key, device_uid_len, device_uid, device_icon, device_number)" \ "values (?, ?, ?, ?, ?)" #define DELETE_ALL_PAYLOADS "delete from payloads" @@ -258,11 +258,13 @@ int _uam_db_insert_payload_info( 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), + DB_ACTION(sqlite3_bind_int(stmt, 3, payload->device_uid_len), error_code, handle_error); - DB_ACTION(sqlite3_bind_text(stmt, 4, &(payload->device_icon), 1, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_text(stmt, 4, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT), error_code, handle_error); - DB_ACTION(sqlite3_bind_int(stmt, 5, device_number), + DB_ACTION(sqlite3_bind_text(stmt, 5, &(payload->device_icon), 1, SQLITE_TRANSIENT), + error_code, handle_error); + DB_ACTION(sqlite3_bind_int(stmt, 6, device_number), error_code, handle_error); sql_ret = sqlite3_step(stmt); @@ -298,7 +300,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_LEN + 1, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1, SQLITE_TRANSIENT), error_code, handle_error); do { @@ -339,7 +341,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_LEN + 1, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1, SQLITE_TRANSIENT), error_code, handle_error); do { @@ -405,21 +407,22 @@ 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_LEN + 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; - buf = (char *)sqlite3_column_text(stmt, 2); + info->payload_info.device_uid_len = sqlite3_column_int(stmt, 2); + buf = (char *)sqlite3_column_text(stmt, 3); if (buf) memcpy(info->payload_info.device_uid, buf, - UAM_BLE_PAYLOAD_DEVICE_UID_LEN); - buf = (char *)sqlite3_column_text(stmt, 3); + UAM_BLE_PAYLOAD_DEVICE_UID_MAX_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, 4), + g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 5), UAM_DEVICE_ID_MAX_STRING_LEN); - info->type = sqlite3_column_int(stmt, 5); + info->type = sqlite3_column_int(stmt, 6); UAM_INFO("primary key: %d secondary key: %d", info->payload_info.primary_key, diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index 0059618..20fdbfe 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -104,8 +104,10 @@ typedef struct { char primary_key; /** Primary Key */ char device_icon; /** Device icon */ char secondary_key; /** Secondary key */ + int device_uid_len; /** Device uid len */ char *device_uid; /** DEVICE_UID */ char *bt_mac; /** BT MAC Address */ + char *user_data; /** User data */ } uas_payload_info_t; /* Device information structure */ -- 2.7.4 From d9de50e7e9a70c69fec00e6cf57cf2840d36355b Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Thu, 26 Dec 2019 19:18:33 +0530 Subject: [PATCH 11/16] Store payload device user data in database Signed-off-by: Abhay agarwal --- ua-daemon/data/ua_db.sql | 1 + ua-daemon/include/ua-manager-database.h | 5 ++ ua-daemon/src/pm/ua-pm-util.c | 5 ++ ua-daemon/src/ua-manager-core.c | 126 +++++++++++++++++++-------- ua-daemon/src/ua-manager-db.c | 1 + ua-daemon/src/ua-manager-device-service-db.c | 9 +- ua-daemon/src/ua-manager-payload-db.c | 71 +++++++++++---- 7 files changed, 159 insertions(+), 59 deletions(-) diff --git a/ua-daemon/data/ua_db.sql b/ua-daemon/data/ua_db.sql index ab1fe7b..c3984dc 100644 --- a/ua-daemon/data/ua_db.sql +++ b/ua-daemon/data/ua_db.sql @@ -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) diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 07cf3fc..0db5058 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -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 diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index be1e3f5..fa8a74c 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -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); diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index c01a6a9..b3edda2 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -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); diff --git a/ua-daemon/src/ua-manager-db.c b/ua-daemon/src/ua-manager-db.c index a250c0f..7404e8e 100644 --- a/ua-daemon/src/ua-manager-db.c +++ b/ua-daemon/src/ua-manager-db.c @@ -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) " \ diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index f0e9a22..7b7872e 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -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 " \ diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index 553d470..31c6d21 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -26,19 +26,25 @@ #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; -- 2.7.4 From f210523a933a2e50f34e8776b169078a18dc5d75 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Thu, 26 Dec 2019 21:23:33 +0530 Subject: [PATCH 12/16] Fix device service list in ua-manager for NULL payloads Signed-off-by: Abhay agarwal --- ua-daemon/include/ua-manager-database.h | 1 + ua-daemon/src/ua-manager-core.c | 22 ++++++++++ ua-daemon/src/ua-manager-device-service-db.c | 62 ++++++++++++++++++++++++++-- ua-daemon/src/ua-manager-payload-db.c | 16 ++++--- 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 0db5058..de7c0b4 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -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); diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index b3edda2..8b11fea 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -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; } diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index 7b7872e..1b07fc2 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -31,12 +31,17 @@ #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) { diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index 31c6d21..b13a214 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -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); -- 2.7.4 From c40696c6d0ed27cd7410d138447753e95fae4a08 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Fri, 27 Dec 2019 08:54:19 +0530 Subject: [PATCH 13/16] Fix device payload update query Signed-off-by: Abhay agarwal --- ua-daemon/src/ua-manager-core.c | 22 ++++++---------------- ua-daemon/src/ua-manager-payload-db.c | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 8b11fea..96421a4 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -149,22 +149,6 @@ static gint __compare_payload(gconstpointer data, gconstpointer user_data) return -1; } -#if 0 -static gint __compare_db_payload(gconstpointer data, gconstpointer user_data) -{ - const uam_db_payload_info_t *db_payload = data; - const uam_db_payload_info_t *payload = user_data; - - 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))) - return 0; - return -1; -} -#endif - static void __free_address_info(gpointer data) { FUNC_ENTRY; @@ -3151,6 +3135,12 @@ int _uam_core_handle_presence_detected(unsigned int sensor, tech->presence_state = UAM_PRESENCE_STATE_PRESENT; tech->last_seen = dev_info->last_seen; + /** Update payload info in list with user data */ + l = g_slist_find_custom(payloads, payload, __compare_payload); + if (l) { + __uam_copy_uam_payload_info(l->data, payload); + } + retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER); /* Check if IP/MAC address was updated then update in DB */ diff --git a/ua-daemon/src/ua-manager-payload-db.c b/ua-daemon/src/ua-manager-payload-db.c index b13a214..466c7fa 100644 --- a/ua-daemon/src/ua-manager-payload-db.c +++ b/ua-daemon/src/ua-manager-payload-db.c @@ -42,8 +42,9 @@ #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 = ?" + "SET device_icon = ?, user_data = ? WHERE primary_key = ? AND " \ + "secondary_key = ? AND device_uid_len = ? AND " \ + "device_uid = ? AND device_number = ?" /* DELETE statements */ static sqlite3_stmt *delete_all_payloads; @@ -480,19 +481,19 @@ int _uam_db_update_payload_info( if (UAM_ERROR_NONE != error_code) goto handle_error; - DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->primary_key), 1, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->device_icon), 1, SQLITE_TRANSIENT), error_code, handle_error); - DB_ACTION(sqlite3_bind_text(stmt, 2, &(payload->secondary_key), 1, SQLITE_TRANSIENT), + user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len; + DB_ACTION(sqlite3_bind_text(stmt, 2, payload->user_data, user_data_len, SQLITE_TRANSIENT), error_code, handle_error); - DB_ACTION(sqlite3_bind_int(stmt, 3, payload->device_uid_len), + + DB_ACTION(sqlite3_bind_text(stmt, 3, &(payload->primary_key), 1, SQLITE_TRANSIENT), error_code, handle_error); - DB_ACTION(sqlite3_bind_text(stmt, 4, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_text(stmt, 4, &(payload->secondary_key), 1, SQLITE_TRANSIENT), error_code, handle_error); - DB_ACTION(sqlite3_bind_text(stmt, 5, &(payload->device_icon), 1, SQLITE_TRANSIENT), + DB_ACTION(sqlite3_bind_int(stmt, 5, payload->device_uid_len), 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), + DB_ACTION(sqlite3_bind_text(stmt, 6, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT), error_code, handle_error); DB_ACTION(sqlite3_bind_int(stmt, 7, device_number), error_code, handle_error); -- 2.7.4 From e5b1a06fbbe62bd000a118c0814f29c09bf4945b Mon Sep 17 00:00:00 2001 From: Abhay Agarwal Date: Tue, 14 Jan 2020 17:08:58 +0530 Subject: [PATCH 14/16] Fix check for db file size Database file should not be 0 after db creation Change-Id: I93803f853ffd6b3afed3b18010cc556da7eb3417 Signed-off-by: Abhay Agarwal --- packaging/ua-manager.spec | 2 +- ua-daemon/src/ua-manager-db.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index 21bc4aa..afb09ad 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -1,6 +1,6 @@ Name: ua-manager Summary: User awareness manager -Version: 0.13.16 +Version: 0.13.17 Release: 1 License: Apache-2.0 Source0: %{name}-%{version}.tar.gz diff --git a/ua-daemon/src/ua-manager-db.c b/ua-daemon/src/ua-manager-db.c index 7404e8e..71e10fb 100644 --- a/ua-daemon/src/ua-manager-db.c +++ b/ua-daemon/src/ua-manager-db.c @@ -425,7 +425,7 @@ static int __uam_db_verify() /* check table existance*/ retv_if(UAM_ERROR_NONE != __uam_db_check_table_creation(), UAM_ERROR_DB_FAILED); /* check db size */ - _uam_get_file_size(DATABASE_FULL_PATH); + retv_if(0 >= _uam_get_file_size(DATABASE_FULL_PATH), UAM_ERROR_DB_FAILED); /* check db integrity */ retv_if(UAM_ERROR_NONE != __uam_db_check_integrity(), UAM_ERROR_DB_FAILED); /* set locking mode */ -- 2.7.4 From dc28058f75f3c71cdd58a45566a2eacb4f824741 Mon Sep 17 00:00:00 2001 From: Abhay Agarwal Date: Tue, 14 Jan 2020 15:05:31 +0530 Subject: [PATCH 15/16] Add support to report light sensor status change This patch add support of light sensor in existing functionality to report sensor status change to application Change-Id: Ie237eeecc1fbf15c3ccb08b35b91ec5e597e60d3 Signed-off-by: Abhay Agarwal --- packaging/ua-manager.spec | 2 +- ua-daemon/src/pm/ua-light-plugin-handler.c | 23 ++++++++++++++++++++++- ua-daemon/src/pm/ua-motion-plugin-handler.c | 1 - ua-daemon/src/ua-manager-core.c | 5 ++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index afb09ad..e43a414 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -1,6 +1,6 @@ Name: ua-manager Summary: User awareness manager -Version: 0.13.17 +Version: 0.13.19 Release: 1 License: Apache-2.0 Source0: %{name}-%{version}.tar.gz diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c index 0dcdb9a..5bb2ed9 100644 --- a/ua-daemon/src/pm/ua-light-plugin-handler.c +++ b/ua-daemon/src/pm/ua-light-plugin-handler.c @@ -43,6 +43,27 @@ static void light_detection_state_changed_cb(int state) FUNC_EXIT; } +static void light_detection_status_changed_cb(uas_detection_type_e type, + void *sensor_info) +{ + FUNC_ENTRY; + + uas_sensor_info_t *info = NULL; + uam_sensor_info_s *light_info = NULL; + + UAM_DBG("Light: %s", type == UAS_PRESENCE ? "PRESENCE" : "ABSENCE"); + + if (sensor_info) + info = sensor_info; + light_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info); + + _uam_core_handle_status_changed(UAM_SENSOR_BITMASK_LIGHT, light_info); + + g_free(light_info); + + FUNC_EXIT; +} + void light_detection_callback(uas_detection_type_e type, void *sensor_info) { FUNC_ENTRY; @@ -74,7 +95,7 @@ void light_detection_callback(uas_detection_type_e type, void *sensor_info) uas_callbacks_t light_cbs = { .state_changed_cb = light_state_changed_callback, .detection_state_cb = light_detection_state_changed_cb, - .detection_status_cb = NULL, + .detection_status_cb = light_detection_status_changed_cb, .detected_cb = light_detection_callback, .device_detected_cb = NULL, .device_added_cb = NULL, diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c index 195d5e1..0bf1848 100644 --- a/ua-daemon/src/pm/ua-motion-plugin-handler.c +++ b/ua-daemon/src/pm/ua-motion-plugin-handler.c @@ -43,7 +43,6 @@ static void motion_detection_state_changed_cb(int state) FUNC_EXIT; } - static void motion_detection_status_changed_cb(uas_detection_type_e type, void *sensor_info) { diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 96421a4..f6965f1 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -4118,11 +4118,10 @@ void _uam_core_handle_status_changed(unsigned int sensor, void *info) ret_if(NULL == info); - UAM_DBG("%d %d %llu %d %d", sensor, sensor_info->status, sensor_info->timestamp, - sensor_info->accuracy, sensor_info->count); - UAM_INFO("sensor: 0x%8.8X %s", sensor, sensor_info->status == UAS_ABSENCE ? "UAM_EVENT_ABSENCE_DETECTED" : "UAM_EVENT_PRESENCE_DETECTED"); + UAM_DBG("%llu %d %d", sensor_info->timestamp, + sensor_info->accuracy, sensor_info->count); _uam_manager_send_event(NULL, UAM_EVENT_SENSOR_STATUS_CHANGED, g_variant_new("(uutiidddd)", sensor, sensor_info->status, sensor_info->timestamp, -- 2.7.4 From 738ef724a7cdb2baef04bc691428ca55b371e4d6 Mon Sep 17 00:00:00 2001 From: Abhay Agarwal Date: Tue, 21 Jan 2020 11:10:44 +0530 Subject: [PATCH 16/16] Simultaneously start presence and absence detection Issue: when presence detection is follwed by absence detection, absence callback is missed in first cycle. Solution: start both presence and absence detection Change-Id: I353a2ecd985c86afefb322cad47283695853896c Signed-off-by: Abhay Agarwal --- packaging/ua-manager.spec | 2 +- ua-daemon/src/ua-manager-core.c | 72 +++++++++++++---------------------------- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index e43a414..a06bec7 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -1,6 +1,6 @@ Name: ua-manager Summary: User awareness manager -Version: 0.13.19 +Version: 0.13.20 Release: 1 License: Apache-2.0 Source0: %{name}-%{version}.tar.gz diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index f6965f1..da0b1f3 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -2108,25 +2108,16 @@ static gboolean __start_detection(gpointer data) unsigned int sensors; gboolean start_detection = FALSE; GSList *l; - unsigned int presence_env_sensors; - unsigned int absence_env_sensors; + unsigned int env_sensors; - presence_env_sensors = _uam_core_get_active_env_sensors(UAM_DETECT_PRESENCE); - UAM_DBG("Presence Envionmental sensors: 0x%8.8X", presence_env_sensors); + env_sensors = _uam_core_get_active_env_sensors(UAM_DETECT_PRESENCE) | + _uam_core_get_active_env_sensors(UAM_DETECT_ABSENCE); + UAM_DBG("Active envionmental sensors: 0x%8.8X", env_sensors); - absence_env_sensors = _uam_core_get_active_env_sensors(UAM_DETECT_ABSENCE); - UAM_DBG("Absence Envionmental sensors: 0x%8.8X", absence_env_sensors); - - if (0 != presence_env_sensors) { - /* Stop PRESENCE detection on active envionmental sensors*/ - ret = _uam_pm_stop_detection(UAM_DETECT_PRESENCE, presence_env_sensors); - if (UAM_ERROR_NONE != ret) - UAM_ERR("Failed with error: %s (0x%4.4X)", - _uam_manager_error_to_str(ret), ret); - } - if (0 != absence_env_sensors) { - /* Stop ABSENCE detection on envionmental sensors*/ - ret = _uam_pm_stop_detection(UAM_DETECT_ABSENCE, absence_env_sensors); + if (0 != env_sensors) { + /* stop detection on active envionmental sensors*/ + ret = _uam_pm_stop_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE, + env_sensors); if (UAM_ERROR_NONE != ret) UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); @@ -2149,49 +2140,30 @@ static gboolean __start_detection(gpointer data) if (!start_detection) goto done; - /* Get sensors on which PRESENCE detection to be started */ - sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE); - UAM_DBG("PRESENCE sensors: 0x%8.8X", sensors); - - /* Remove env sensors from active sensors */ - sensors &= ~presence_env_sensors; - UAM_DBG("Presence Connectivity sensors: 0x%8.8X", sensors); - - if (0 != sensors) { - /* Start PRESENCE detection */ - ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE, sensors); - if (UAM_ERROR_NONE != ret) - UAM_ERR("Failed with error: %s (0x%4.4X)", - _uam_manager_error_to_str(ret), ret); - } - - /* Get sensors on which ABSENCE detection to be started */ - sensors = _uam_core_get_active_sensors(UAM_DETECT_ABSENCE); - UAM_DBG("ABSENCE sensors: 0x%8.8X", sensors); + /* Get sensors on which detection to be started */ + sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE) | + _uam_core_get_active_sensors(UAM_DETECT_ABSENCE); + UAM_DBG("Active sensors: 0x%8.8X", sensors); /* Remove env sensors from active sensors */ - sensors &= ~absence_env_sensors; - UAM_DBG("ABSENCE Connectivity sensors: 0x%8.8X", sensors); + sensors &= ~env_sensors; + UAM_DBG("Active connectivity sensors: 0x%8.8X", sensors); if (0 != sensors) { - /* Start ABSENCE detection */ - ret = _uam_pm_start_detection(UAM_DETECT_ABSENCE, sensors); + /* Start detection */ + ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE, + sensors); if (UAM_ERROR_NONE != ret) UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); } done: - if (0 != presence_env_sensors) { - /* Always Start PRESENCE detection on active envionmental sensors*/ - ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE, presence_env_sensors); - if (UAM_ERROR_NONE != ret) - UAM_ERR("Failed with error: %s (0x%4.4X)", - _uam_manager_error_to_str(ret), ret); - } - if (0 != absence_env_sensors) { - /* Always Start ABSENCE detection on active envionmental sensors*/ - ret = _uam_pm_start_detection(UAM_DETECT_ABSENCE, absence_env_sensors); + if (0 != env_sensors) { + /* continuous monitoring + * always start detection on active envionmental sensors */ + ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE, + env_sensors); if (UAM_ERROR_NONE != ret) UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); -- 2.7.4