OSDN Git Service

Refactor adding ext prop descriptor
authorHsingYuan Lo <hylo@google.com>
Thu, 17 Sep 2020 08:54:04 +0000 (16:54 +0800)
committerHsingYuan Lo <hylo@google.com>
Thu, 17 Sep 2020 10:02:53 +0000 (18:02 +0800)
In oder to make db hash easier for unit testing
1. Creare a new API to add ext prop descriptor
2. Move the extended properties check to gatt_api.cc

Test: compile
Tag: #refactor
Bug: 110864501
Change-Id: I7afd748e3cd6f6588a30c0f255da4f2ab4b9376c

stack/gatt/gatt_api.cc
stack/gatt/gatt_db.cc
stack/gatt/gatt_int.h

index 2d93e61..d55e197 100644 (file)
@@ -239,9 +239,14 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service,
         return GATT_INTERNAL_ERROR;
       }
 
-      el->attribute_handle =
-          gatts_add_characteristic(list.svc_db, el->permissions, el->properties,
-                                   el->extended_properties, uuid);
+      el->attribute_handle = gatts_add_characteristic(
+          list.svc_db, el->permissions, el->properties, uuid);
+
+      // add characteristic extended properties descriptor if needed
+      if (el->properties & GATT_CHAR_PROP_BIT_EXT_PROP) {
+        gatts_add_char_ext_prop_descr(list.svc_db, el->extended_properties);
+      }
+
     } else if (el->type == BTGATT_DB_DESCRIPTOR) {
       if (is_gatt_attr_type(uuid)) {
         LOG(ERROR) << __func__
index d4adbe3..f56e93d 100644 (file)
@@ -376,7 +376,6 @@ uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
  ******************************************************************************/
 uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
                                   tGATT_CHAR_PROP property,
-                                  uint16_t extended_properties,
                                   const Uuid& char_uuid) {
   Uuid uuid = Uuid::From16Bit(GATT_UUID_CHAR_DECLARE);
 
@@ -391,20 +390,38 @@ uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
   char_decl.p_value->char_decl.char_val_handle = char_val.handle;
   char_val.gatt_type = BTGATT_DB_CHARACTERISTIC;
 
-  if (property & GATT_CHAR_PROP_BIT_EXT_PROP) {
-    Uuid char_ext_prop_uuid = Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP);
-    tGATT_ATTR& char_ext_prop =
-        allocate_attr_in_db(db, char_ext_prop_uuid, GATT_PERM_READ);
-    char_ext_prop.p_value.reset(new tGATT_ATTR_VALUE);
-    char_ext_prop.p_value->char_ext_prop = extended_properties;
-    char_ext_prop.gatt_type = BTGATT_DB_DESCRIPTOR;
-  }
-
   return char_val.handle;
 }
 
 /*******************************************************************************
  *
+ * Function         gatts_add_char_ext_prop_descr
+ *
+ * Description      add a characteristics extended properties descriptor.
+ *
+ * Parameter        db: database pointer.
+ *                  extended_properties: characteristic descriptors values.
+ *
+ * Returns          Status of the operation.
+ *
+ ******************************************************************************/
+uint16_t gatts_add_char_ext_prop_descr(
+    tGATT_SVC_DB& db, uint16_t extended_properties) {
+  Uuid descr_uuid = Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP);
+
+  VLOG(1) << StringPrintf("gatts_add_char_ext_prop_descr uuid=%s",
+                          descr_uuid.ToString().c_str());
+
+  tGATT_ATTR& char_dscptr = allocate_attr_in_db(db, descr_uuid, GATT_PERM_READ);
+  char_dscptr.gatt_type = BTGATT_DB_DESCRIPTOR;
+  char_dscptr.p_value.reset(new tGATT_ATTR_VALUE);
+  char_dscptr.p_value->char_ext_prop = extended_properties;
+
+  return char_dscptr.handle;
+}
+
+/*******************************************************************************
+ *
  * Function         gatts_add_char_descr
  *
  * Description      This function add a characteristics descriptor.
index 5728b85..258c251 100644 (file)
@@ -596,8 +596,9 @@ extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
                                            const bluetooth::Uuid& service);
 extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
                                          tGATT_CHAR_PROP property,
-                                         uint16_t extended_properties,
                                          const bluetooth::Uuid& char_uuid);
+extern uint16_t gatts_add_char_ext_prop_descr(tGATT_SVC_DB& db,
+                                              uint16_t extended_properties);
 extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
                                      const bluetooth::Uuid& dscp_uuid);
 extern tGATT_STATUS gatts_db_read_attr_value_by_type(