Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d66740e8 authored by Myles Watson's avatar Myles Watson
Browse files

GATT: Use a list for services in a GATT database

Iterators to list elements are not invalidated by the addition and
removal of other elements.

Bug: 128938477
Test: net_test_bta
Change-Id: I45d74a63fc6b55ece3a4af02e0cc3e1de757cc4d
parent e5c95df7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -269,7 +269,7 @@ void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id, const Uuid& srvc_uuid) {
 * Returns          returns list of gatt::Service or NULL.
 *
 ******************************************************************************/
const std::vector<gatt::Service>* BTA_GATTC_GetServices(uint16_t conn_id) {
const std::list<gatt::Service>* BTA_GATTC_GetServices(uint16_t conn_id) {
  return bta_gattc_get_services(conn_id);
}

+6 −7
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ void bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb) {
}

const Service* bta_gattc_find_matching_service(
    const std::vector<Service>& services, uint16_t handle) {
    const std::list<Service>& services, uint16_t handle) {
  for (const Service& service : services) {
    if (handle >= service.handle && handle <= service.end_handle)
      return &service;
@@ -419,14 +419,13 @@ void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, Uuid* p_uuid) {
  }
}

const std::vector<Service>* bta_gattc_get_services_srcb(
    tBTA_GATTC_SERV* p_srcb) {
const std::list<Service>* bta_gattc_get_services_srcb(tBTA_GATTC_SERV* p_srcb) {
  if (!p_srcb || p_srcb->gatt_database.IsEmpty()) return NULL;

  return &p_srcb->gatt_database.Services();
}

const std::vector<Service>* bta_gattc_get_services(uint16_t conn_id) {
const std::list<Service>* bta_gattc_get_services(uint16_t conn_id) {
  tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);

  if (p_clcb == NULL) return NULL;
@@ -438,14 +437,14 @@ const std::vector<Service>* bta_gattc_get_services(uint16_t conn_id) {

const Service* bta_gattc_get_service_for_handle_srcb(tBTA_GATTC_SERV* p_srcb,
                                                     uint16_t handle) {
  const std::vector<Service>* services = bta_gattc_get_services_srcb(p_srcb);
  const std::list<Service>* services = bta_gattc_get_services_srcb(p_srcb);
  if (services == NULL) return NULL;
  return bta_gattc_find_matching_service(*services, handle);
}

const Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
                                                uint16_t handle) {
  const std::vector<Service>* services = bta_gattc_get_services(conn_id);
  const std::list<Service>* services = bta_gattc_get_services(conn_id);
  if (services == NULL) return NULL;

  return bta_gattc_find_matching_service(*services, handle);
@@ -556,7 +555,7 @@ void bta_gattc_fill_gatt_db_el(btgatt_db_element_t* p_attr,
/*******************************************************************************
 * Returns          number of elements inside db from start_handle to end_handle
 ******************************************************************************/
static size_t bta_gattc_get_db_size(const std::vector<Service>& services,
static size_t bta_gattc_get_db_size(const std::list<Service>& services,
                                    uint16_t start_handle,
                                    uint16_t end_handle) {
  if (services.empty()) return 0;
+1 −2
Original line number Diff line number Diff line
@@ -426,8 +426,7 @@ extern tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id,
                                                   uint8_t disc_type);
extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb,
                                     bluetooth::Uuid* p_uuid);
extern const std::vector<gatt::Service>* bta_gattc_get_services(
    uint16_t conn_id);
extern const std::list<gatt::Service>* bta_gattc_get_services(uint16_t conn_id);
extern const gatt::Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
                                                             uint16_t handle);
const gatt::Characteristic* bta_gattc_get_characteristic_srcb(
+3 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "stack/include/gattdefs.h"

#include <base/logging.h>
#include <list>
#include <memory>
#include <sstream>

@@ -39,7 +40,7 @@ bool HandleInRange(const Service& svc, uint16_t handle) {
}
}  // namespace

Service* FindService(std::vector<Service>& services, uint16_t handle) {
Service* FindService(std::list<Service>& services, uint16_t handle) {
  for (Service& service : services) {
    if (handle >= service.handle && handle <= service.end_handle)
      return &service;
+5 −4
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#pragma once

#include <list>
#include <set>
#include <string>
#include <utility>
@@ -101,10 +102,10 @@ class Database {

  /* Clear the GATT database. This method forces relocation to ensure no extra
   * space is used unnecesarly */
  void Clear() { std::vector<Service>().swap(services); }
  void Clear() { std::list<Service>().swap(services); }

  /* Return list of services available in this database */
  const std::vector<Service>& Services() const { return services; }
  const std::list<Service>& Services() const { return services; }

  std::string ToString() const;

@@ -116,11 +117,11 @@ class Database {
  friend class DatabaseBuilder;

 private:
  std::vector<Service> services;
  std::list<Service> services;
};

/* Find a service that should contain handle. Helper method for internal use
 * inside gatt namespace.*/
Service* FindService(std::vector<Service>& services, uint16_t handle);
Service* FindService(std::list<Service>& services, uint16_t handle);

}  // namespace gatt
Loading