OSDN Git Service

Use std::list in GATT related code
authorJakub Pawlowski <jpawlowski@google.com>
Sat, 9 Sep 2017 19:50:38 +0000 (12:50 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Wed, 4 Oct 2017 18:21:49 +0000 (18:21 +0000)
Bug: 67057055
Test: sl4a Gatt* tests
Change-Id: I8201ebdad5ba4c3d5d0a2fd3d0fe9dc900b51d60

bta/gatt/bta_gattc_act.cc
bta/gatt/bta_gattc_api.cc
bta/gatt/bta_gattc_cache.cc
bta/gatt/bta_gattc_int.h
bta/gatt/bta_gattc_utils.cc
bta/hh/bta_hh_le.cc
bta/include/bta_gatt_api.h

index 7b09533..30bd3dc 100644 (file)
@@ -123,7 +123,7 @@ static void bta_gattc_enable() {
 
   if (bta_gattc_cb.state == BTA_GATTC_STATE_DISABLED) {
     /* initialize control block */
-    memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
+    bta_gattc_cb = tBTA_GATTC_CB();
     bta_gattc_cb.state = BTA_GATTC_STATE_ENABLED;
   } else {
     APPL_TRACE_DEBUG("GATTC is already enabled");
@@ -167,7 +167,7 @@ void bta_gattc_disable() {
 
   /* no registered apps, indicate disable completed */
   if (bta_gattc_cb.state != BTA_GATTC_STATE_DISABLING) {
-    memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
+    bta_gattc_cb = tBTA_GATTC_CB();
     bta_gattc_cb.state = BTA_GATTC_STATE_DISABLED;
   }
 }
@@ -587,7 +587,7 @@ void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data) {
   if (p_clcb->p_srcb->mtu == 0) p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
 
   /* start database cache if needed */
-  if (p_clcb->p_srcb->p_srvc_cache == NULL ||
+  if (p_clcb->p_srcb->srvc_cache.empty() ||
       p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE) {
     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) {
       p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
@@ -877,10 +877,7 @@ void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
 
   if (p_clcb->status != GATT_SUCCESS) {
     /* clean up cache */
-    if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) {
-      list_free(p_clcb->p_srcb->p_srvc_cache);
-      p_clcb->p_srcb->p_srvc_cache = NULL;
-    }
+    if (p_clcb->p_srcb) p_clcb->p_srcb->srvc_cache.clear();
 
     /* used to reset cache in application */
     bta_gattc_cache_reset(p_clcb->p_srcb->server_bda);
@@ -1252,7 +1249,7 @@ void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data) {
   tGATT_STATUS status = GATT_INTERNAL_ERROR;
   tBTA_GATTC cb_data;
   APPL_TRACE_DEBUG("%s: conn_id=%d", __func__, p_clcb->bta_conn_id);
-  if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) {
+  if (p_clcb->p_srcb && !p_clcb->p_srcb->srvc_cache.empty()) {
     status = GATT_SUCCESS;
     /* search the local cache of a server device */
     bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
@@ -1423,10 +1420,7 @@ void bta_gattc_process_api_refresh(const RawAddress& remote_bda) {
       }
     }
     /* in all other cases, mark it and delete the cache */
-    if (p_srvc_cb->p_srvc_cache != NULL) {
-      list_free(p_srvc_cb->p_srvc_cache);
-      p_srvc_cb->p_srvc_cache = NULL;
-    }
+    p_srvc_cb->srvc_cache.clear();
   }
   /* used to reset cache in application */
   bta_gattc_cache_reset(remote_bda);
index 656b5b7..8470a85 100644 (file)
@@ -266,10 +266,10 @@ void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id,
  *
  * Parameters       conn_id: connection ID which identify the server.
  *
- * Returns          returns list_t of tBTA_GATTC_SERVICE or NULL.
+ * Returns          returns list of tBTA_GATTC_SERVICE or NULL.
  *
  ******************************************************************************/
-const list_t* BTA_GATTC_GetServices(uint16_t conn_id) {
+const std::list<tBTA_GATTC_SERVICE>* BTA_GATTC_GetServices(uint16_t conn_id) {
   return bta_gattc_get_services(conn_id);
 }
 
index c0e05bb..fae164a 100644 (file)
@@ -52,10 +52,10 @@ static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id,
                                            tBTA_GATTC_SERV* p_srvc_cb);
 static tGATT_STATUS bta_gattc_sdp_service_disc(uint16_t conn_id,
                                                tBTA_GATTC_SERV* p_server_cb);
-tBTA_GATTC_SERVICE* bta_gattc_find_matching_service(const list_t* services,
-                                                    uint16_t handle);
-tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(tBTA_GATTC_SERV* p_srcb,
-                                                     uint16_t handle);
+tBTA_GATTC_SERVICE* bta_gattc_find_matching_service(
+    std::list<tBTA_GATTC_SERVICE>& services, uint16_t handle);
+const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(
+    tBTA_GATTC_SERV* p_srcb, uint16_t handle);
 tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb(
     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
 
@@ -88,54 +88,37 @@ static const char* bta_gattc_attr_type[] = {
 };
 /* utility functions */
 
-bool display_descriptor(void* data, void* context) {
-  tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)data;
-  APPL_TRACE_ERROR("\t Descriptor handle[%d] uuid[%s]", p_desc->handle,
-                   p_desc->uuid.ToString().c_str());
-  return true;
-}
+/* debug function to display the server cache */
+static void bta_gattc_display_cache_server(
+    const std::list<tBTA_GATTC_SERVICE>& cache) {
+  APPL_TRACE_ERROR("<================Start Server Cache =============>");
 
-bool display_cache_attribute(void* data, void* context) {
-  tBTA_GATTC_CHARACTERISTIC* p_attr = (tBTA_GATTC_CHARACTERISTIC*)data;
-  APPL_TRACE_ERROR("\t Characteristic handle[%d] uuid[%s]  prop[0x%1x]",
-                   p_attr->handle, p_attr->uuid.ToString().c_str(),
-                   p_attr->properties);
-  if (!p_attr->descriptors) {
-    APPL_TRACE_ERROR("No descriptors");
-    return true;
-  }
+  for (const tBTA_GATTC_SERVICE& service : cache) {
+    APPL_TRACE_ERROR("Service: handle[%d ~ %d] %s inst[%d]", service.s_handle,
+                     service.e_handle, service.uuid.ToString().c_str(),
+                     service.handle);
 
-  list_foreach(p_attr->descriptors, display_descriptor, NULL);
-  return true;
-}
+    if (service.characteristics.empty()) {
+      APPL_TRACE_ERROR("\t No characteristics");
+      continue;
+    }
 
-bool display_cache_service(void* data, void* context) {
-  tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)data;
-  APPL_TRACE_ERROR("Service: handle[%d ~ %d] %s inst[%d]", p_cur_srvc->s_handle,
-                   p_cur_srvc->e_handle, p_cur_srvc->uuid.ToString().c_str(),
-                   p_cur_srvc->handle);
+    for (const tBTA_GATTC_CHARACTERISTIC& c : service.characteristics) {
+      APPL_TRACE_ERROR("\t Characteristic handle[%d] uuid[%s]  prop[0x%1x]",
+                       c.handle, c.uuid.ToString().c_str(), c.properties);
 
-  if (!p_cur_srvc->characteristics) {
-    APPL_TRACE_ERROR("No characteristics");
-    return true;
-  }
+      if (c.descriptors.empty()) {
+        APPL_TRACE_ERROR("\t\t No descriptors");
+        continue;
+      }
 
-  list_foreach(p_cur_srvc->characteristics, display_cache_attribute, NULL);
-  return true;
-}
+      for (const tBTA_GATTC_DESCRIPTOR& d : c.descriptors) {
+        APPL_TRACE_ERROR("\t\t Descriptor handle[%d] uuid[%s]", d.handle,
+                         d.uuid.ToString().c_str());
+      }
+    }
+  }
 
-/*******************************************************************************
- *
- * Function         bta_gattc_display_cache_server
- *
- * Description      debug function to display the server cache.
- *
- * Returns          none.
- *
- ******************************************************************************/
-static void bta_gattc_display_cache_server(list_t* p_cache) {
-  APPL_TRACE_ERROR("<================Start Server Cache =============>");
-  list_foreach(p_cache, display_cache_service, NULL);
   APPL_TRACE_ERROR("<================End Server Cache =============>");
   APPL_TRACE_ERROR(" ");
 }
@@ -177,10 +160,7 @@ static void bta_gattc_display_explore_record(tBTA_GATTC_ATTR_REC* p_rec,
  *
  ******************************************************************************/
 tGATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb) {
-  if (p_srvc_cb->p_srvc_cache != NULL) {
-    list_free(p_srvc_cb->p_srvc_cache);
-    p_srvc_cb->p_srvc_cache = NULL;
-  }
+  p_srvc_cb->srvc_cache.clear();
 
   osi_free(p_srvc_cb->p_srvc_list);
   p_srvc_cb->p_srvc_list =
@@ -193,62 +173,27 @@ tGATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb) {
   return GATT_SUCCESS;
 }
 
-static void characteristic_free(void* ptr) {
-  tBTA_GATTC_CHARACTERISTIC* p_char = (tBTA_GATTC_CHARACTERISTIC*)ptr;
-  list_free(p_char->descriptors);
-  osi_free(p_char);
-}
-
-static void service_free(void* ptr) {
-  tBTA_GATTC_SERVICE* srvc = (tBTA_GATTC_SERVICE*)ptr;
-  list_free(srvc->characteristics);
-  list_free(srvc->included_svc);
-  osi_free(srvc);
-}
-
-/*******************************************************************************
- *
- * Function         bta_gattc_add_srvc_to_cache
- *
- * Description      Add a service into database cache.
- *
- * Returns          status
- *
- ******************************************************************************/
-static tGATT_STATUS bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
-                                                uint16_t s_handle,
-                                                uint16_t e_handle,
-                                                const Uuid& uuid,
-                                                bool is_primary) {
+/** Add a service into database cache */
+static void bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
+                                        uint16_t s_handle, uint16_t e_handle,
+                                        const Uuid& uuid, bool is_primary) {
 #if (BTA_GATT_DEBUG == TRUE)
   APPL_TRACE_DEBUG("Add a service into Service");
 #endif
 
-  tBTA_GATTC_SERVICE* p_new_srvc =
-      (tBTA_GATTC_SERVICE*)osi_malloc(sizeof(tBTA_GATTC_SERVICE));
-
-  /* update service information */
-  p_new_srvc->s_handle = s_handle;
-  p_new_srvc->e_handle = e_handle;
-  p_new_srvc->is_primary = is_primary;
-  p_new_srvc->uuid = uuid;
-  p_new_srvc->handle = s_handle;
-  p_new_srvc->characteristics = list_new(characteristic_free);
-  p_new_srvc->included_svc = list_new(osi_free);
-
-  if (p_srvc_cb->p_srvc_cache == NULL) {
-    p_srvc_cb->p_srvc_cache = list_new(service_free);
-  }
-
-  list_append(p_srvc_cb->p_srvc_cache, p_new_srvc);
-  return GATT_SUCCESS;
+  p_srvc_cb->srvc_cache.emplace_back(tBTA_GATTC_SERVICE{
+      .s_handle = s_handle,
+      .e_handle = e_handle,
+      .is_primary = is_primary,
+      .uuid = uuid,
+      .handle = s_handle,
+  });
 }
 
-static tGATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
-                                                uint16_t attr_handle,
-                                                uint16_t value_handle,
-                                                const Uuid& uuid,
-                                                uint8_t property) {
+static void bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
+                                        uint16_t attr_handle,
+                                        uint16_t value_handle, const Uuid& uuid,
+                                        uint8_t property) {
 #if (BTA_GATT_DEBUG == TRUE)
   APPL_TRACE_DEBUG("%s: Add a characteristic into Service", __func__);
   APPL_TRACE_DEBUG("handle=%d uuid16=%s property=0x%x", value_handle,
@@ -256,11 +201,11 @@ static tGATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
 #endif
 
   tBTA_GATTC_SERVICE* service =
-      bta_gattc_find_matching_service(p_srvc_cb->p_srvc_cache, attr_handle);
+      bta_gattc_find_matching_service(p_srvc_cb->srvc_cache, attr_handle);
   if (!service) {
     APPL_TRACE_ERROR(
         "Illegal action to add char/descr/incl srvc for non-existing service!");
-    return GATT_WRONG_STATE;
+    return;
   }
 
   /* TODO(jpawlowski): We should use attribute handle, not value handle to refer
@@ -269,32 +214,20 @@ static tGATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
   */
   if (service->e_handle < value_handle) service->e_handle = value_handle;
 
-  tBTA_GATTC_CHARACTERISTIC* characteristic =
-      (tBTA_GATTC_CHARACTERISTIC*)osi_malloc(sizeof(tBTA_GATTC_CHARACTERISTIC));
-
-  characteristic->handle = value_handle;
-  characteristic->properties = property;
-  characteristic->descriptors = list_new(osi_free);
-  characteristic->uuid = uuid;
-
-  characteristic->service = service;
-  list_append(service->characteristics, characteristic);
-
-  return GATT_SUCCESS;
+  service->characteristics.emplace_back(
+      tBTA_GATTC_CHARACTERISTIC{.handle = value_handle,
+                                .properties = property,
+                                .uuid = uuid,
+                                .service = service});
+  return;
 }
 
-/*******************************************************************************
- *
- * Function         bta_gattc_add_attr_to_cache
- *
- * Description      Add an attribute into database cache buffer.
- *
- * Returns          status
- *
- ******************************************************************************/
-static tGATT_STATUS bta_gattc_add_attr_to_cache(
-    tBTA_GATTC_SERV* p_srvc_cb, uint16_t handle, const Uuid& uuid,
-    uint8_t property, uint16_t incl_srvc_s_handle, tBTA_GATTC_ATTR_TYPE type) {
+/* Add an attribute into database cache buffer */
+static void bta_gattc_add_attr_to_cache(tBTA_GATTC_SERV* p_srvc_cb,
+                                        uint16_t handle, const Uuid& uuid,
+                                        uint8_t property,
+                                        uint16_t incl_srvc_s_handle,
+                                        tBTA_GATTC_ATTR_TYPE type) {
 #if (BTA_GATT_DEBUG == TRUE)
   APPL_TRACE_DEBUG("%s: Add a [%s] into Service", __func__,
                    bta_gattc_attr_type[type]);
@@ -303,55 +236,42 @@ static tGATT_STATUS bta_gattc_add_attr_to_cache(
 #endif
 
   tBTA_GATTC_SERVICE* service =
-      bta_gattc_find_matching_service(p_srvc_cb->p_srvc_cache, handle);
+      bta_gattc_find_matching_service(p_srvc_cb->srvc_cache, handle);
   if (!service) {
     APPL_TRACE_ERROR(
         "Illegal action to add char/descr/incl srvc for non-existing service!");
-    return GATT_WRONG_STATE;
+    return;
   }
 
   if (type == BTA_GATTC_ATTR_TYPE_INCL_SRVC) {
-    tBTA_GATTC_INCLUDED_SVC* isvc =
-        (tBTA_GATTC_INCLUDED_SVC*)osi_malloc(sizeof(tBTA_GATTC_INCLUDED_SVC));
-
-    isvc->handle = handle;
-    isvc->uuid = uuid;
-
-    isvc->owning_service = service;
-    isvc->included_service = bta_gattc_find_matching_service(
-        p_srvc_cb->p_srvc_cache, incl_srvc_s_handle);
-    if (!isvc->included_service) {
+    tBTA_GATTC_SERVICE* included_service = bta_gattc_find_matching_service(
+        p_srvc_cb->srvc_cache, incl_srvc_s_handle);
+    if (!included_service) {
       APPL_TRACE_ERROR(
           "%s: Illegal action to add non-existing included service!", __func__);
-      osi_free(isvc);
-      return GATT_WRONG_STATE;
+      return;
     }
 
-    list_append(service->included_svc, isvc);
+    service->included_svc.emplace_back(tBTA_GATTC_INCLUDED_SVC{
+        .handle = handle,
+        .uuid = uuid,
+        .owning_service = service,
+        .included_service = included_service,
+    });
   } else if (type == BTA_GATTC_ATTR_TYPE_CHAR_DESCR) {
-    tBTA_GATTC_DESCRIPTOR* descriptor =
-        (tBTA_GATTC_DESCRIPTOR*)osi_malloc(sizeof(tBTA_GATTC_DESCRIPTOR));
-
-    descriptor->handle = handle;
-    descriptor->uuid = uuid;
-
-    if (service->characteristics == NULL ||
-        list_is_empty(service->characteristics)) {
+    if (service->characteristics.empty()) {
       APPL_TRACE_ERROR(
           "%s: Illegal action to add descriptor before adding a "
           "characteristic!",
           __func__);
-      osi_free(descriptor);
-      return GATT_WRONG_STATE;
+      return;
     }
 
-    tBTA_GATTC_CHARACTERISTIC* char_node =
-        (tBTA_GATTC_CHARACTERISTIC*)list_back(service->characteristics);
-
-    descriptor->characteristic = char_node;
-    list_append(char_node->descriptors, descriptor);
+    tBTA_GATTC_CHARACTERISTIC& char_node = service->characteristics.back();
+    char_node.descriptors.emplace_back(tBTA_GATTC_DESCRIPTOR{
+        .handle = handle, .uuid = uuid, .characteristic = &char_node,
+    });
   }
-  return GATT_SUCCESS;
 }
 
 /*******************************************************************************
@@ -509,18 +429,18 @@ static void bta_gattc_explore_srvc(uint16_t conn_id,
   /* start expore a service if there is service not been explored */
   if (p_srvc_cb->cur_srvc_idx < p_srvc_cb->total_srvc) {
     /* add the first service into cache */
-    if (bta_gattc_add_srvc_to_cache(p_srvc_cb, p_rec->s_handle, p_rec->e_handle,
-                                    p_rec->uuid, p_rec->is_primary) == 0) {
-      /* start discovering included services */
-      bta_gattc_start_disc_include_srvc(conn_id, p_srvc_cb);
-      return;
-    }
+    bta_gattc_add_srvc_to_cache(p_srvc_cb, p_rec->s_handle, p_rec->e_handle,
+                                p_rec->uuid, p_rec->is_primary);
+
+    /* start discovering included services */
+    bta_gattc_start_disc_include_srvc(conn_id, p_srvc_cb);
+    return;
   }
   /* no service found at all, the end of server discovery*/
   LOG_WARN(LOG_TAG, "%s no more services found", __func__);
 
 #if (BTA_GATT_DEBUG == TRUE)
-  bta_gattc_display_cache_server(p_srvc_cb->p_srvc_cache);
+  bta_gattc_display_cache_server(p_srvc_cb->srvc_cache);
 #endif
   /* save cache to NV */
   p_clcb->p_srcb->state = BTA_GATTC_SERV_SAVE;
@@ -790,7 +710,6 @@ static tGATT_STATUS bta_gattc_sdp_service_disc(uint16_t conn_id,
   uint16_t num_attrs = 2;
   uint16_t attr_list[2];
 
-
   /*
    * On success, cb_data will be freed inside bta_gattc_sdp_callback,
    * otherwise it will be freed within this function.
@@ -940,40 +859,34 @@ void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
  *
  ******************************************************************************/
 void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, Uuid* p_uuid) {
-  auto cache = p_clcb->p_srcb->p_srvc_cache;
-  if (!cache || list_is_empty(cache)) return;
-
-  for (list_node_t* sn = list_begin(cache); sn != list_end(cache);
-       sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* p_cache = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (p_uuid && *p_uuid != p_cache->uuid) continue;
+  for (const tBTA_GATTC_SERVICE& service : p_clcb->p_srcb->srvc_cache) {
+    if (p_uuid && *p_uuid != service.uuid) continue;
 
 #if (BTA_GATT_DEBUG == TRUE)
     APPL_TRACE_DEBUG("found service %s, inst[%d] handle [%d]",
-                     p_cache->uuid.ToString().c_str(), p_cache->handle,
-                     p_cache->s_handle);
+                     service.uuid.ToString().c_str(), service.handle,
+                     service.s_handle);
 #endif
     if (!p_clcb->p_rcb->p_cback) continue;
 
     tBTA_GATTC cb_data;
     memset(&cb_data, 0, sizeof(tBTA_GATTC));
     cb_data.srvc_res.conn_id = p_clcb->bta_conn_id;
-    cb_data.srvc_res.service_uuid.inst_id = p_cache->handle;
-    cb_data.srvc_res.service_uuid.uuid = p_cache->uuid;
+    cb_data.srvc_res.service_uuid.inst_id = service.handle;
+    cb_data.srvc_res.service_uuid.uuid = service.uuid;
 
     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_RES_EVT, &cb_data);
   }
 }
 
-list_t* bta_gattc_get_services_srcb(tBTA_GATTC_SERV* p_srcb) {
-  if (!p_srcb || !p_srcb->p_srvc_cache || list_is_empty(p_srcb->p_srvc_cache))
-    return NULL;
+std::list<tBTA_GATTC_SERVICE>* bta_gattc_get_services_srcb(
+    tBTA_GATTC_SERV* p_srcb) {
+  if (!p_srcb || p_srcb->srvc_cache.empty()) return NULL;
 
-  return p_srcb->p_srvc_cache;
+  return &p_srcb->srvc_cache;
 }
 
-const list_t* bta_gattc_get_services(uint16_t conn_id) {
+std::list<tBTA_GATTC_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;
@@ -983,47 +896,40 @@ const list_t* bta_gattc_get_services(uint16_t conn_id) {
   return bta_gattc_get_services_srcb(p_srcb);
 }
 
-tBTA_GATTC_SERVICE* bta_gattc_find_matching_service(const list_t* services,
-                                                    uint16_t handle) {
-  if (!services || list_is_empty(services)) return NULL;
-
-  for (list_node_t* sn = list_begin(services); sn != list_end(services);
-       sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* service = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (handle >= service->s_handle && handle <= service->e_handle)
-      return service;
+tBTA_GATTC_SERVICE* bta_gattc_find_matching_service(
+    std::list<tBTA_GATTC_SERVICE>& services, uint16_t handle) {
+  for (tBTA_GATTC_SERVICE& service : services) {
+    if (handle >= service.s_handle && handle <= service.e_handle)
+      return &service;
   }
 
   return NULL;
 }
 
-const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle_srcb(
+static tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle_srcb(
     tBTA_GATTC_SERV* p_srcb, uint16_t handle) {
-  const list_t* services = bta_gattc_get_services_srcb(p_srcb);
-
-  return bta_gattc_find_matching_service(services, handle);
+  std::list<tBTA_GATTC_SERVICE>* services = bta_gattc_get_services_srcb(p_srcb);
+  if (services == NULL) return NULL;
+  return bta_gattc_find_matching_service(*services, handle);
 }
 
 const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle(uint16_t conn_id,
                                                            uint16_t handle) {
-  const list_t* services = bta_gattc_get_services(conn_id);
+  std::list<tBTA_GATTC_SERVICE>* services = bta_gattc_get_services(conn_id);
+  if (services == NULL) return NULL;
 
-  return bta_gattc_find_matching_service(services, handle);
+  return bta_gattc_find_matching_service(*services, handle);
 }
 
 tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb(
     tBTA_GATTC_SERV* p_srcb, uint16_t handle) {
-  const tBTA_GATTC_SERVICE* service =
+  tBTA_GATTC_SERVICE* service =
       bta_gattc_get_service_for_handle_srcb(p_srcb, handle);
 
   if (!service) return NULL;
 
-  for (list_node_t* cn = list_begin(service->characteristics);
-       cn != list_end(service->characteristics); cn = list_next(cn)) {
-    tBTA_GATTC_CHARACTERISTIC* p_char =
-        (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-    if (handle == p_char->handle) return p_char;
+  for (tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) {
+    if (handle == charac.handle) return &charac;
   }
 
   return NULL;
@@ -1039,8 +945,8 @@ tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(uint16_t conn_id,
   return bta_gattc_get_characteristic_srcb(p_srcb, handle);
 }
 
-tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(tBTA_GATTC_SERV* p_srcb,
-                                                     uint16_t handle) {
+const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(
+    tBTA_GATTC_SERV* p_srcb, uint16_t handle) {
   const tBTA_GATTC_SERVICE* service =
       bta_gattc_get_service_for_handle_srcb(p_srcb, handle);
 
@@ -1048,22 +954,17 @@ tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(tBTA_GATTC_SERV* p_srcb,
     return NULL;
   }
 
-  for (list_node_t* cn = list_begin(service->characteristics);
-       cn != list_end(service->characteristics); cn = list_next(cn)) {
-    tBTA_GATTC_CHARACTERISTIC* p_char =
-        (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-    for (list_node_t* dn = list_begin(p_char->descriptors);
-         dn != list_end(p_char->descriptors); dn = list_next(dn)) {
-      tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)list_node(dn);
-      if (handle == p_desc->handle) return p_desc;
+  for (const tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) {
+    for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) {
+      if (handle == desc.handle) return &desc;
     }
   }
 
   return NULL;
 }
 
-tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id,
-                                                uint16_t handle) {
+const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id,
+                                                      uint16_t handle) {
   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
 
   if (p_clcb == NULL) return NULL;
@@ -1102,37 +1003,27 @@ 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(list_t* services, uint16_t start_handle,
-                                    uint16_t end_handle) {
-  if (!services || list_is_empty(services)) return 0;
+static size_t bta_gattc_get_db_size(
+    const std::list<tBTA_GATTC_SERVICE>& services, uint16_t start_handle,
+    uint16_t end_handle) {
+  if (services.empty()) return 0;
 
   size_t db_size = 0;
 
-  for (list_node_t* sn = list_begin(services); sn != list_end(services);
-       sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (p_cur_srvc->s_handle < start_handle) continue;
+  for (const tBTA_GATTC_SERVICE& service : services) {
+    if (service.s_handle < start_handle) continue;
 
-    if (p_cur_srvc->e_handle > end_handle) break;
+    if (service.e_handle > end_handle) break;
 
     db_size++;
-    if (!p_cur_srvc->characteristics ||
-        list_is_empty(p_cur_srvc->characteristics))
-      continue;
 
-    for (list_node_t* cn = list_begin(p_cur_srvc->characteristics);
-         cn != list_end(p_cur_srvc->characteristics); cn = list_next(cn)) {
-      tBTA_GATTC_CHARACTERISTIC* p_char =
-          (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
+    for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) {
       db_size++;
 
-      if (p_char->descriptors) db_size += list_length(p_char->descriptors);
+      db_size += charac.descriptors.size();
     }
 
-    if (p_cur_srvc->included_svc) {
-      db_size += list_length(p_cur_srvc->included_svc);
-    }
+    db_size += service.included_svc.size();
   }
 
   return db_size;
@@ -1160,74 +1051,51 @@ static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV* p_srvc_cb,
   APPL_TRACE_DEBUG("%s: start_handle 0x%04x, end_handle 0x%04x", __func__,
                    start_handle, end_handle);
 
-  if (!p_srvc_cb->p_srvc_cache || list_is_empty(p_srvc_cb->p_srvc_cache)) {
+  if (p_srvc_cb->srvc_cache.empty()) {
     *count = 0;
     *db = NULL;
     return;
   }
 
   size_t db_size =
-      bta_gattc_get_db_size(p_srvc_cb->p_srvc_cache, start_handle, end_handle);
+      bta_gattc_get_db_size(p_srvc_cb->srvc_cache, start_handle, end_handle);
 
   void* buffer = osi_malloc(db_size * sizeof(btgatt_db_element_t));
   btgatt_db_element_t* curr_db_attr = (btgatt_db_element_t*)buffer;
 
-  for (list_node_t* sn = list_begin(p_srvc_cb->p_srvc_cache);
-       sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (p_cur_srvc->s_handle < start_handle) continue;
+  for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) {
+    if (service.s_handle < start_handle) continue;
 
-    if (p_cur_srvc->e_handle > end_handle) break;
+    if (service.e_handle > end_handle) break;
 
-    bta_gattc_fill_gatt_db_el(
-        curr_db_attr, p_cur_srvc->is_primary ? BTGATT_DB_PRIMARY_SERVICE
-                                             : BTGATT_DB_SECONDARY_SERVICE,
-        0 /* att_handle */, p_cur_srvc->s_handle, p_cur_srvc->e_handle,
-        p_cur_srvc->s_handle, p_cur_srvc->uuid, 0 /* prop */);
+    bta_gattc_fill_gatt_db_el(curr_db_attr,
+                              service.is_primary ? BTGATT_DB_PRIMARY_SERVICE
+                                                 : BTGATT_DB_SECONDARY_SERVICE,
+                              0 /* att_handle */, service.s_handle,
+                              service.e_handle, service.s_handle, service.uuid,
+                              0 /* prop */);
     curr_db_attr++;
 
-    if (!p_cur_srvc->characteristics ||
-        list_is_empty(p_cur_srvc->characteristics))
-      continue;
-
-    for (list_node_t* cn = list_begin(p_cur_srvc->characteristics);
-         cn != list_end(p_cur_srvc->characteristics); cn = list_next(cn)) {
-      tBTA_GATTC_CHARACTERISTIC* p_char =
-          (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-
+    for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) {
       bta_gattc_fill_gatt_db_el(curr_db_attr, BTGATT_DB_CHARACTERISTIC,
-                                p_char->handle, 0 /* s_handle */,
-                                0 /* e_handle */, p_char->handle, p_char->uuid,
-                                p_char->properties);
+                                charac.handle, 0 /* s_handle */,
+                                0 /* e_handle */, charac.handle, charac.uuid,
+                                charac.properties);
       curr_db_attr++;
 
-      if (!p_char->descriptors || list_is_empty(p_char->descriptors)) continue;
-
-      for (list_node_t* dn = list_begin(p_char->descriptors);
-           dn != list_end(p_char->descriptors); dn = list_next(dn)) {
-        tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)list_node(dn);
-
-        bta_gattc_fill_gatt_db_el(curr_db_attr, BTGATT_DB_DESCRIPTOR,
-                                  p_desc->handle, 0 /* s_handle */,
-                                  0 /* e_handle */, p_desc->handle,
-                                  p_desc->uuid, 0 /* property */);
+      for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) {
+        bta_gattc_fill_gatt_db_el(
+            curr_db_attr, BTGATT_DB_DESCRIPTOR, desc.handle, 0 /* s_handle */,
+            0 /* e_handle */, desc.handle, desc.uuid, 0 /* property */);
         curr_db_attr++;
       }
     }
 
-    if (!p_cur_srvc->included_svc || list_is_empty(p_cur_srvc->included_svc))
-      continue;
-
-    for (list_node_t* isn = list_begin(p_cur_srvc->included_svc);
-         isn != list_end(p_cur_srvc->included_svc); isn = list_next(isn)) {
-      tBTA_GATTC_INCLUDED_SVC* p_isvc =
-          (tBTA_GATTC_INCLUDED_SVC*)list_node(isn);
-
+    for (const tBTA_GATTC_INCLUDED_SVC& p_isvc : service.included_svc) {
       bta_gattc_fill_gatt_db_el(
-          curr_db_attr, BTGATT_DB_INCLUDED_SERVICE, p_isvc->handle,
-          p_isvc->included_service ? p_isvc->included_service->s_handle : 0,
-          0 /* e_handle */, p_isvc->handle, p_isvc->uuid, 0 /* property */);
+          curr_db_attr, BTGATT_DB_INCLUDED_SERVICE, p_isvc.handle,
+          p_isvc.included_service ? p_isvc.included_service->s_handle : 0,
+          0 /* e_handle */, p_isvc.handle, p_isvc.uuid, 0 /* property */);
       curr_db_attr++;
     }
   }
@@ -1269,7 +1137,7 @@ void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
 
   if (!p_clcb->p_srcb ||
       p_clcb->p_srcb->p_srvc_list || /* no active discovery */
-      !p_clcb->p_srcb->p_srvc_cache) {
+      p_clcb->p_srcb->srvc_cache.empty()) {
     APPL_TRACE_ERROR("No server cache available");
     return;
   }
@@ -1294,8 +1162,7 @@ void bta_gattc_rebuild_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t num_attr,
   /* first attribute loading, initialize buffer */
   APPL_TRACE_ERROR("%s: bta_gattc_rebuild_cache", __func__);
 
-  list_free(p_srvc_cb->p_srvc_cache);
-  p_srvc_cb->p_srvc_cache = NULL;
+  p_srvc_cb->srvc_cache.clear();
 
   while (num_attr > 0 && p_attr != NULL) {
     switch (p_attr->attr_type) {
@@ -1357,65 +1224,37 @@ void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR* p_attr, uint8_t type,
  *
  ******************************************************************************/
 void bta_gattc_cache_save(tBTA_GATTC_SERV* p_srvc_cb, uint16_t conn_id) {
-  if (!p_srvc_cb->p_srvc_cache || list_is_empty(p_srvc_cb->p_srvc_cache))
-    return;
+  if (p_srvc_cb->srvc_cache.empty()) return;
 
   int i = 0;
-  size_t db_size =
-      bta_gattc_get_db_size(p_srvc_cb->p_srvc_cache, 0x0000, 0xFFFF);
+  size_t db_size = bta_gattc_get_db_size(p_srvc_cb->srvc_cache, 0x0000, 0xFFFF);
   tBTA_GATTC_NV_ATTR* nv_attr =
       (tBTA_GATTC_NV_ATTR*)osi_malloc(db_size * sizeof(tBTA_GATTC_NV_ATTR));
 
-  for (list_node_t* sn = list_begin(p_srvc_cb->p_srvc_cache);
-       sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)list_node(sn);
-
+  for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) {
     bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_SRVC,
-                           p_cur_srvc->s_handle, p_cur_srvc->e_handle,
-                           p_cur_srvc->uuid, 0 /* properties */,
-                           0 /* incl_srvc_handle */, p_cur_srvc->is_primary);
+                           service.s_handle, service.e_handle, service.uuid,
+                           0 /* properties */, 0 /* incl_srvc_handle */,
+                           service.is_primary);
   }
 
-  for (list_node_t* sn = list_begin(p_srvc_cb->p_srvc_cache);
-       sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (!p_cur_srvc->characteristics ||
-        list_is_empty(p_cur_srvc->characteristics))
-      continue;
-
-    for (list_node_t* cn = list_begin(p_cur_srvc->characteristics);
-         cn != list_end(p_cur_srvc->characteristics); cn = list_next(cn)) {
-      tBTA_GATTC_CHARACTERISTIC* p_char =
-          (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-
-      bta_gattc_fill_nv_attr(
-          &nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR, p_char->handle, 0,
-          p_char->uuid, p_char->properties, 0 /* incl_srvc_handle */, false);
+  for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) {
+    for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) {
+      bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR,
+                             charac.handle, 0, charac.uuid, charac.properties,
+                             0 /* incl_srvc_handle */, false);
 
-      if (!p_char->descriptors || list_is_empty(p_char->descriptors)) continue;
-
-      for (list_node_t* dn = list_begin(p_char->descriptors);
-           dn != list_end(p_char->descriptors); dn = list_next(dn)) {
-        tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)list_node(dn);
-
-        bta_gattc_fill_nv_attr(
-            &nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR_DESCR, p_desc->handle, 0,
-            p_desc->uuid, 0 /* properties */, 0 /* incl_srvc_handle */, false);
+      for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) {
+        bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR_DESCR,
+                               desc.handle, 0, desc.uuid, 0 /* properties */,
+                               0 /* incl_srvc_handle */, false);
       }
     }
 
-    if (!p_cur_srvc->included_svc || list_is_empty(p_cur_srvc->included_svc))
-      continue;
-
-    for (list_node_t* an = list_begin(p_cur_srvc->included_svc);
-         an != list_end(p_cur_srvc->included_svc); an = list_next(an)) {
-      tBTA_GATTC_INCLUDED_SVC* p_isvc = (tBTA_GATTC_INCLUDED_SVC*)list_node(an);
-
+    for (const tBTA_GATTC_INCLUDED_SVC& p_isvc : service.included_svc) {
       bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_INCL_SRVC,
-                             p_isvc->handle, 0, p_isvc->uuid,
-                             0 /* properties */,
-                             p_isvc->included_service->s_handle, false);
+                             p_isvc.handle, 0, p_isvc.uuid, 0 /* properties */,
+                             p_isvc.included_service->s_handle, false);
     }
   }
 
@@ -1469,7 +1308,8 @@ bool bta_gattc_cache_load(tBTA_GATTC_CLCB* p_clcb) {
   }
 
   if (num_attr > 0xFFFF) {
-    APPL_TRACE_ERROR("%s: more than 0xFFFF GATT attributes: %s", __func__, fname);
+    APPL_TRACE_ERROR("%s: more than 0xFFFF GATT attributes: %s", __func__,
+                     fname);
     goto done;
   }
 
index 08fc2e3..4b4521c 100644 (file)
@@ -223,7 +223,7 @@ typedef struct {
 
   uint8_t state;
 
-  list_t* p_srvc_cache; /* list of tBTA_GATTC_SERVICE */
+  std::list<tBTA_GATTC_SERVICE> srvc_cache;
   uint8_t update_count; /* indication received */
   uint8_t num_clcb;     /* number of associated CLCB */
 
@@ -450,15 +450,15 @@ 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 list_t* bta_gattc_get_services(uint16_t conn_id);
+extern std::list<tBTA_GATTC_SERVICE>* bta_gattc_get_services(uint16_t conn_id);
 extern const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle(
     uint16_t conn_id, uint16_t handle);
 tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb(
     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
 extern tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(uint16_t conn_id,
                                                                uint16_t handle);
-extern tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id,
-                                                       uint16_t handle);
+extern const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id,
+                                                             uint16_t handle);
 extern void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
                                   uint16_t end_handle, btgatt_db_element_t** db,
                                   int* count);
index d9dabe1..249e480 100644 (file)
@@ -203,10 +203,7 @@ void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb) {
       p_srcb->mtu = 0;
 
       /* clean up cache */
-      if (p_srcb->p_srvc_cache) {
-        list_free(p_srcb->p_srvc_cache);
-        p_srcb->p_srvc_cache = NULL;
-      }
+      p_srcb->srvc_cache.clear();
     }
 
     osi_free_and_reset((void**)&p_clcb->p_q_cmd);
@@ -300,10 +297,10 @@ tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda) {
     p_tcb = p_recycle;
 
   if (p_tcb != NULL) {
-    if (p_tcb->p_srvc_cache != NULL) list_free(p_tcb->p_srvc_cache);
+    p_tcb->srvc_cache.clear();
 
     osi_free_and_reset((void**)&p_tcb->p_srvc_list);
-    memset(p_tcb, 0, sizeof(tBTA_GATTC_SERV));
+    *p_tcb = tBTA_GATTC_SERV();
 
     p_tcb->in_use = true;
     p_tcb->server_bda = bda;
index 82b5bac..7cca56d 100644 (file)
@@ -580,7 +580,7 @@ tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb,
   return NULL;
 }
 
-static tBTA_GATTC_DESCRIPTOR* find_descriptor_by_short_uuid(
+static const tBTA_GATTC_DESCRIPTOR* find_descriptor_by_short_uuid(
     uint16_t conn_id, uint16_t char_handle, uint16_t short_uuid) {
   const tBTA_GATTC_CHARACTERISTIC* p_char =
       BTA_GATTC_GetCharacteristic(conn_id, char_handle);
@@ -590,13 +590,8 @@ static tBTA_GATTC_DESCRIPTOR* find_descriptor_by_short_uuid(
     return NULL;
   }
 
-  if (!p_char->descriptors || list_is_empty(p_char->descriptors)) return NULL;
-
-  for (list_node_t* dn = list_begin(p_char->descriptors);
-       dn != list_end(p_char->descriptors); dn = list_next(dn)) {
-    tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)list_node(dn);
-
-    if (p_desc->uuid == Uuid::From16Bit(short_uuid)) return p_desc;
+  for (const tBTA_GATTC_DESCRIPTOR& desc : p_char->descriptors) {
+    if (desc.uuid == Uuid::From16Bit(short_uuid)) return &desc;
   }
 
   return NULL;
@@ -800,7 +795,7 @@ void bta_hh_le_open_cmpl(tBTA_HH_DEV_CB* p_cb) {
 bool bta_hh_le_write_ccc(tBTA_HH_DEV_CB* p_cb, uint8_t char_handle,
                          uint16_t clt_cfg_value, GATT_WRITE_OP_CB cb,
                          void* cb_data) {
-  tBTA_GATTC_DESCRIPTOR* p_desc = find_descriptor_by_short_uuid(
+  const tBTA_GATTC_DESCRIPTOR* p_desc = find_descriptor_by_short_uuid(
       p_cb->conn_id, char_handle, GATT_UUID_CHAR_CLIENT_CONFIG);
   if (!p_desc) return false;
 
@@ -1501,35 +1496,31 @@ void read_pref_conn_params_cb(uint16_t conn_id, tGATT_STATUS status,
  *
  ******************************************************************************/
 static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
-                                       tBTA_GATTC_SERVICE* service) {
+                                       const tBTA_GATTC_SERVICE* service) {
   tBTA_HH_LE_RPT* p_rpt;
 
-  for (list_node_t* cn = list_begin(service->characteristics);
-       cn != list_end(service->characteristics); cn = list_next(cn)) {
-    tBTA_GATTC_CHARACTERISTIC* p_char =
-        (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-
-    if (!p_char->uuid.Is16Bit()) continue;
+  for (const tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) {
+    if (!charac.uuid.Is16Bit()) continue;
 
-    uint16_t uuid16 = p_char->uuid.As16Bit();
+    uint16_t uuid16 = charac.uuid.As16Bit();
     LOG_DEBUG(LOG_TAG, "%s: %s %s", __func__, bta_hh_uuid_to_str(uuid16),
-              p_char->uuid.ToString().c_str());
+              charac.uuid.ToString().c_str());
 
     switch (uuid16) {
       case GATT_UUID_HID_CONTROL_POINT:
-        p_dev_cb->hid_srvc.control_point_handle = p_char->handle;
+        p_dev_cb->hid_srvc.control_point_handle = charac.handle;
         break;
       case GATT_UUID_HID_INFORMATION:
         /* only one instance per HID service */
-        gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, p_char->handle,
+        gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, charac.handle,
                            read_hid_info_cb, p_dev_cb);
         break;
       case GATT_UUID_HID_REPORT_MAP:
         /* only one instance per HID service */
-        gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, p_char->handle,
+        gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, charac.handle,
                            read_hid_report_map_cb, p_dev_cb);
         /* descriptor is optional */
-        bta_hh_le_read_char_descriptor(p_dev_cb, p_char->handle,
+        bta_hh_le_read_char_descriptor(p_dev_cb, charac.handle,
                                        GATT_UUID_EXT_RPT_REF_DESCR,
                                        read_ext_rpt_ref_desc_cb, p_dev_cb);
         break;
@@ -1537,7 +1528,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
       case GATT_UUID_HID_REPORT:
         p_rpt = bta_hh_le_find_alloc_report_entry(
             p_dev_cb, p_dev_cb->hid_srvc.srvc_inst_id, GATT_UUID_HID_REPORT,
-            p_char->handle);
+            charac.handle);
         if (p_rpt == NULL) {
           APPL_TRACE_ERROR("%s: Add report entry failed !!!", __func__);
           break;
@@ -1545,7 +1536,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
 
         if (p_rpt->rpt_type != BTA_HH_RPTT_INPUT) break;
 
-        bta_hh_le_read_char_descriptor(p_dev_cb, p_char->handle,
+        bta_hh_le_read_char_descriptor(p_dev_cb, charac.handle,
                                        GATT_UUID_RPT_REF_DESCR,
                                        read_report_ref_desc_cb, p_dev_cb);
         break;
@@ -1555,7 +1546,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
       case GATT_UUID_HID_BT_MOUSE_INPUT:
       case GATT_UUID_HID_BT_KB_INPUT:
         if (bta_hh_le_find_alloc_report_entry(p_dev_cb, service->handle, uuid16,
-                                              p_char->handle) == NULL)
+                                              charac.handle) == NULL)
           APPL_TRACE_ERROR("%s: Add report entry failed !!!", __func__);
 
         break;
@@ -1567,13 +1558,9 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
   }
 
   /* Make sure PROTO_MODE is processed as last */
-  for (list_node_t* cn = list_begin(service->characteristics);
-       cn != list_end(service->characteristics); cn = list_next(cn)) {
-    tBTA_GATTC_CHARACTERISTIC* p_char =
-        (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-
-    if (p_char->uuid == Uuid::From16Bit(GATT_UUID_HID_PROTO_MODE)) {
-      p_dev_cb->hid_srvc.proto_mode_handle = p_char->handle;
+  for (const tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) {
+    if (charac.uuid == Uuid::From16Bit(GATT_UUID_HID_PROTO_MODE)) {
+      p_dev_cb->hid_srvc.proto_mode_handle = charac.handle;
       bta_hh_le_set_protocol_mode(p_dev_cb, p_dev_cb->mode);
       break;
     }
@@ -1602,38 +1589,33 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) {
     return;
   }
 
-  const list_t* services = BTA_GATTC_GetServices(p_data->conn_id);
+  const std::list<tBTA_GATTC_SERVICE>* services =
+      BTA_GATTC_GetServices(p_data->conn_id);
 
   bool have_hid = false;
-  for (list_node_t* sn = list_begin(services); sn != list_end(services);
-       sn = list_next(sn)) {
-    tBTA_GATTC_SERVICE* service = (tBTA_GATTC_SERVICE*)list_node(sn);
-
-    if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_LE_HID) &&
-        service->is_primary && !have_hid) {
+  for (const tBTA_GATTC_SERVICE& service : *services) {
+    if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_LE_HID) &&
+        service.is_primary && !have_hid) {
       have_hid = true;
 
       /* found HID primamry service */
       p_dev_cb->hid_srvc.in_use = true;
-      p_dev_cb->hid_srvc.srvc_inst_id = service->handle;
+      p_dev_cb->hid_srvc.srvc_inst_id = service.handle;
       p_dev_cb->hid_srvc.proto_mode_handle = 0;
       p_dev_cb->hid_srvc.control_point_handle = 0;
 
-      bta_hh_le_search_hid_chars(p_dev_cb, service);
+      bta_hh_le_search_hid_chars(p_dev_cb, &service);
 
       APPL_TRACE_DEBUG("%s: have HID service inst_id= %d", __func__,
                        p_dev_cb->hid_srvc.srvc_inst_id);
-    } else if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_SCAN_PARAM)) {
+    } else if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_SCAN_PARAM)) {
       p_dev_cb->scan_refresh_char_handle = 0;
 
-      for (list_node_t* cn = list_begin(service->characteristics);
-           cn != list_end(service->characteristics); cn = list_next(cn)) {
-        tBTA_GATTC_CHARACTERISTIC* p_char =
-            (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-        if (p_char->uuid == Uuid::From16Bit(GATT_UUID_SCAN_REFRESH)) {
-          p_dev_cb->scan_refresh_char_handle = p_char->handle;
+      for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) {
+        if (charac.uuid == Uuid::From16Bit(GATT_UUID_SCAN_REFRESH)) {
+          p_dev_cb->scan_refresh_char_handle = charac.handle;
 
-          if (p_char->properties & GATT_CHAR_PROP_BIT_NOTIFY)
+          if (charac.properties & GATT_CHAR_PROP_BIT_NOTIFY)
             p_dev_cb->scps_notify |= BTA_HH_LE_SCPS_NOTIFY_SPT;
           else
             p_dev_cb->scps_notify = BTA_HH_LE_SCPS_NOTIFY_NONE;
@@ -1641,16 +1623,13 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) {
           break;
         }
       }
-    } else if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) {
+    } else if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) {
       // TODO(jpawlowski): this should be done by GAP profile, remove when GAP
       // is fixed.
-      for (list_node_t* cn = list_begin(service->characteristics);
-           cn != list_end(service->characteristics); cn = list_next(cn)) {
-        tBTA_GATTC_CHARACTERISTIC* p_char =
-            (tBTA_GATTC_CHARACTERISTIC*)list_node(cn);
-        if (p_char->uuid == Uuid::From16Bit(GATT_UUID_GAP_PREF_CONN_PARAM)) {
+      for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) {
+        if (charac.uuid == Uuid::From16Bit(GATT_UUID_GAP_PREF_CONN_PARAM)) {
           /* read the char value */
-          gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, p_char->handle,
+          gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, charac.handle,
                              read_pref_conn_params_cb, p_dev_cb);
 
           break;
index b19a7cb..feddc54 100644 (file)
@@ -30,6 +30,7 @@
 #include "osi/include/list.h"
 
 #include <base/callback_forward.h>
+#include <list>
 #include <vector>
 
 using std::vector;
@@ -47,7 +48,7 @@ using std::vector;
 /* GATT ID */
 typedef struct {
   bluetooth::Uuid uuid; /* uuid of the attribute */
-  uint8_t inst_id; /* instance ID */
+  uint8_t inst_id;      /* instance ID */
 } __attribute__((packed)) tBTA_GATT_ID;
 
 /* Client callback function events */
@@ -78,7 +79,7 @@ typedef struct {
 } tBTA_GATT_CHAR_PRES;
 
 /* Characteristic Aggregate Format attribute value
-*/
+ */
 #define BTA_GATT_AGGR_HANDLE_NUM_MAX 10
 typedef struct {
   uint8_t num_handle;
@@ -282,7 +283,7 @@ typedef uint8_t tBTA_GATTS_EVT;
 #endif
 
 /***********************  NV callback Data Definitions   **********************
-*/
+ */
 typedef struct {
   bluetooth::Uuid app_uuid128;
   bluetooth::Uuid svc_uuid;
@@ -363,9 +364,9 @@ typedef union {
   tBTA_GATTS_SRVC_OPER srvc_oper;
   tGATT_STATUS status; /* BTA_GATTS_LISTEN_EVT */
   tBTA_GATTS_REQ req_data;
-  tBTA_GATTS_CONN conn;       /* BTA_GATTS_CONN_EVT */
-  tBTA_GATTS_CONGEST congest; /* BTA_GATTS_CONGEST_EVT callback data */
-  tBTA_GATTS_CONF confirm;    /* BTA_GATTS_CONF_EVT callback data */
+  tBTA_GATTS_CONN conn;             /* BTA_GATTS_CONN_EVT */
+  tBTA_GATTS_CONGEST congest;       /* BTA_GATTS_CONGEST_EVT callback data */
+  tBTA_GATTS_CONF confirm;          /* BTA_GATTS_CONF_EVT callback data */
   tBTA_GATTS_PHY_UPDATE phy_update; /* BTA_GATTS_PHY_UPDATE_EVT callback data */
   tBTA_GATTS_CONN_UPDATE
       conn_update; /* BTA_GATTS_CONN_UPDATE_EVT callback data */
@@ -377,37 +378,40 @@ typedef void(tBTA_GATTS_ENB_CBACK)(tGATT_STATUS status);
 /* Server callback function */
 typedef void(tBTA_GATTS_CBACK)(tBTA_GATTS_EVT event, tBTA_GATTS* p_data);
 
+struct tBTA_GATTC_CHARACTERISTIC;
+struct tBTA_GATTC_DESCRIPTOR;
+struct tBTA_GATTC_INCLUDED_SVC;
+
 typedef struct {
   bluetooth::Uuid uuid;
   bool is_primary;
   uint16_t handle;
   uint16_t s_handle;
   uint16_t e_handle;
-  list_t* characteristics; /* list of tBTA_GATTC_CHARACTERISTIC */
-  list_t* included_svc;    /* list of tBTA_GATTC_INCLUDED_SVC */
+  std::list<tBTA_GATTC_CHARACTERISTIC> characteristics;
+  std::list<tBTA_GATTC_INCLUDED_SVC> included_svc;
 } __attribute__((packed, aligned(alignof(bluetooth::Uuid)))) tBTA_GATTC_SERVICE;
 
-typedef struct {
+struct tBTA_GATTC_CHARACTERISTIC {
   bluetooth::Uuid uuid;
   uint16_t handle;
   tGATT_CHAR_PROP properties;
   tBTA_GATTC_SERVICE* service; /* owning service*/
-  list_t* descriptors;         /* list of tBTA_GATTC_DESCRIPTOR */
-} __attribute__((packed, aligned(alignof(bluetooth::Uuid))))
-tBTA_GATTC_CHARACTERISTIC;
+  std::list<tBTA_GATTC_DESCRIPTOR> descriptors;
+} __attribute__((packed, aligned(alignof(bluetooth::Uuid))));
 
-typedef struct {
+struct tBTA_GATTC_DESCRIPTOR {
   bluetooth::Uuid uuid;
   uint16_t handle;
   tBTA_GATTC_CHARACTERISTIC* characteristic; /* owning characteristic */
-} __attribute__((packed)) tBTA_GATTC_DESCRIPTOR;
+} __attribute__((packed));
 
-typedef struct {
+struct tBTA_GATTC_INCLUDED_SVC {
   bluetooth::Uuid uuid;
   uint16_t handle;
   tBTA_GATTC_SERVICE* owning_service; /* owning service*/
   tBTA_GATTC_SERVICE* included_service;
-} __attribute__((packed)) tBTA_GATTC_INCLUDED_SVC;
+} __attribute__((packed));
 
 /*****************************************************************************
  *  External Function Declarations
@@ -540,10 +544,11 @@ extern void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id,
  *
  * Parameters       conn_id: connection ID which identify the server.
  *
- * Returns          returns list_t of tBTA_GATTC_SERVICE or NULL.
+ * Returns          returns list of tBTA_GATTC_SERVICE or NULL.
  *
  ******************************************************************************/
-extern const list_t* BTA_GATTC_GetServices(uint16_t conn_id);
+extern const std::list<tBTA_GATTC_SERVICE>* BTA_GATTC_GetServices(
+    uint16_t conn_id);
 
 /*******************************************************************************
  *