shared/gatt-db: Introduce Database Hash
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Thu, 1 Mar 2018 15:41:05 +0000 (17:41 +0200)
committerhimanshu <h.himanshu@samsung.com>
Tue, 11 Feb 2020 08:57:47 +0000 (14:27 +0530)
This introduces a database hash which is generate whenever the database
changes.

Change-Id: I901800b64b7811527b7b978498ac8f0ceebe7403
Signed-off-by: himanshu <h.himanshu@samsung.com>
src/shared/gatt-db.c
src/shared/gatt-db.h

index b2bf0bc..1cb41e9 100644 (file)
@@ -43,6 +43,7 @@
 #define MAX_CHAR_DECL_VALUE_LEN 19
 #define MAX_INCLUDED_VALUE_LEN 6
 #define ATTRIBUTE_TIMEOUT 5000
+#define HASH_UPDATE_TIMEOUT 100
 
 static const bt_uuid_t primary_service_uuid = { .type = BT_UUID16,
                                        .value.u16 = GATT_PRIM_SVC_UUID };
@@ -57,6 +58,8 @@ static const bt_uuid_t ext_desc_uuid = { .type = BT_UUID16,
 
 struct gatt_db {
        int ref_count;
+       uint8_t hash[16];
+       unsigned int hash_id;
        uint16_t next_handle;
        struct queue *services;
 
@@ -267,6 +270,15 @@ static void handle_notify(void *data, void *user_data)
                notify->service_removed(notify_data->attr, notify->user_data);
 }
 
+static bool db_hash_update(void *user_data)
+{
+       struct gatt_db *db = user_data;
+
+       db->hash_id = 0;
+
+       return false;
+}
+
 static void notify_service_changed(struct gatt_db *db,
                                                struct gatt_db_service *service,
                                                bool added)
@@ -283,6 +295,11 @@ static void notify_service_changed(struct gatt_db *db,
 
        queue_foreach(db->notify_list, handle_notify, &data);
 
+       /* Tigger hash update */
+       if (!db->hash_id)
+               db->hash_id = timeout_add(HASH_UPDATE_TIMEOUT, db_hash_update,
+                                                               db, NULL);
+
        gatt_db_unref(db);
 }
 
@@ -313,6 +330,9 @@ static void gatt_db_destroy(struct gatt_db *db)
        queue_destroy(db->notify_list, notify_destroy);
        db->notify_list = NULL;
 
+       if (db->hash_id)
+               timeout_remove(db->hash_id);
+
        queue_destroy(db->services, gatt_db_service_destroy);
        free(db);
 }
@@ -486,6 +506,22 @@ done:
        return true;
 }
 
+uint8_t *gatt_db_get_hash(struct gatt_db *db)
+{
+       uint8_t hash[16] = {};
+
+       if (!db)
+               return NULL;
+
+       /* Generate hash if if has not been generated yet */
+       if (db->hash_id || !memcmp(db->hash, hash, 16)) {
+               timeout_remove(db->hash_id);
+               db_hash_update(db);
+       }
+
+       return db->hash;
+}
+
 static struct gatt_db_service *find_insert_loc(struct gatt_db *db,
                                                uint16_t start, uint16_t end,
                                                struct gatt_db_service **after)
index 92a23e8..8393915 100644 (file)
@@ -41,6 +41,7 @@ bool gatt_db_remove_service(struct gatt_db *db,
 bool gatt_db_clear(struct gatt_db *db);
 bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
                                                        uint16_t end_handle);
+uint8_t *gatt_db_get_hash(struct gatt_db *db);
 
 struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
                                                        uint16_t handle,