Diff between dcfc3f5602350581676921b377fdc63a0ec1e5c4 and 3c9d4bdd940ea137ec3499990aa8fa250d62cf5c

Changed Files

File Additions Deletions Status
src/shared/gatt-db.c +45 -0 modified
src/shared/gatt-db.h +3 -0 modified

Full Patch

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index 0c24904..2515e5a 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -305,6 +305,51 @@ bool gatt_db_remove_service(struct gatt_db *db,
 	return true;
 }
 
+bool gatt_db_clear(struct gatt_db *db)
+{
+	if (!db)
+		return false;
+
+	queue_remove_all(db->services, NULL, NULL, gatt_db_service_destroy);
+
+	db->next_handle = 0;
+
+	return true;
+}
+
+struct clear_range {
+	uint16_t start, end;
+};
+
+static bool match_range(const void *a, const void *b)
+{
+	const struct gatt_db_service *service = a;
+	const struct clear_range *range = b;
+	uint16_t svc_start, svc_end;
+
+	svc_start = service->attributes[0]->handle;
+	svc_end = service->attributes[0]->handle + service->num_handles - 1;
+
+	return svc_start <= range->end && svc_end >= range->start;
+}
+
+bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
+							uint16_t end_handle)
+{
+	struct clear_range range;
+
+	if (!db || start_handle > end_handle)
+		return false;
+
+	range.start = start_handle;
+	range.end = end_handle;
+
+	queue_remove_all(db->services, match_range, &range,
+						gatt_db_service_destroy);
+
+	return true;
+}
+
 struct insert_loc_data {
 	struct gatt_db_service *cur;
 	uint16_t start, end;
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index c9e3b59..ea9f2dc 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -34,6 +34,9 @@ struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
 
 bool gatt_db_remove_service(struct gatt_db *db,
 					struct gatt_db_attribute *attrib);
+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);
 
 struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
 							uint16_t handle,