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

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

Explicitly serialize GATT DB entries

Bug: 304830775
Test: atest net_test_bta_gatt
Change-Id: Ib2a40d3ea853ea3977738850f72429a9441fa285
parent 582ff0ac
Loading
Loading
Loading
Loading
+50 −3
Original line number Diff line number Diff line
@@ -233,9 +233,6 @@ cc_test {
        "test/bta_dm_cust_uuid_test.cc",
        "test/bta_hf_client_add_record_test.cc",
        "test/bta_hf_client_test.cc",
        "test/gatt/database_builder_sample_device_test.cc",
        "test/gatt/database_builder_test.cc",
        "test/gatt/database_test.cc",
    ],
    generated_headers: [
        "BluetoothGeneratedDumpsysDataSchema_h",
@@ -272,6 +269,56 @@ cc_test {
    cflags: ["-Wno-unused-parameter"],
}

// bta GATT unit tests
cc_test {
    name: "net_test_bta_gatt",
    defaults: [
        "bluetooth_gtest_x86_asan_workaround",
        "fluoride_bta_defaults",
        "mts_defaults",
    ],
    test_suites: ["general-tests"],
    srcs: [
        ":LegacyStackSdp",
        ":TestCommonMockFunctions",
        ":TestFakeOsi",
        ":TestMockBtif",
        ":TestMockDevice",
        ":TestMockMainShim",
        ":TestMockStackBtm",
        ":TestMockStackL2cap",
        ":TestMockStackMetrics",
        "test/gatt/database_builder_sample_device_test.cc",
        "test/gatt/database_builder_test.cc",
        "test/gatt/database_test.cc",
    ],
    generated_headers: [
        "BluetoothGeneratedDumpsysDataSchema_h",
    ],
    shared_libs: [
        "libcrypto",
        "liblog",
    ],
    static_libs: [
        "crypto_toolbox_for_tests",
        "libbluetooth-types",
        "libbluetooth_gd",
        "libbluetooth_hci_pdl",
        "libbt-audio-hal-interface",
        "libbt-bta",
        "libbt-bta-core",
        "libbt-common",
        "libbt-platform-protos-lite",
        "libbt_shim_bridge",
        "libbt_shim_ffi",
        "libbtcore",
        "libchrome",
        "libcom.android.sysprop.bluetooth",
        "libgmock",
    ],
    cflags: ["-Wno-unused-parameter"],
}

// bta unit tests for target
cc_test {
    name: "net_test_bta_security",
+67 −2
Original line number Diff line number Diff line
@@ -27,7 +27,10 @@
#include <vector>

#include "bta/gatt/bta_gattc_int.h"
#include "osi/include/log.h"
#include "gatt/database.h"
#include "os/log.h"
#include "stack/include/gattdefs.h"
#include "types/bluetooth/uuid.h"

using gatt::StoredAttribute;
using std::string;
@@ -165,6 +168,61 @@ gatt::Database bta_gattc_hash_load(const Octet16& hash) {
  return bta_gattc_load_db(fname);
}

void StoredAttribute::SerializeStoredAttribute(const StoredAttribute& attr,
                                               std::vector<uint8_t>& bytes) {
  size_t original_size = bytes.size();
  // handle
  bytes.push_back(attr.handle & 0xff);
  bytes.push_back(attr.handle >> 8);
  auto uuid = attr.type.To128BitBE();
  bytes.insert(bytes.cend(), uuid.cbegin(), uuid.cend());

  if (attr.type.Is16Bit()) {
    switch (attr.type.As16Bit()) {
      /* primary or secondary service definition */
      case GATT_UUID_PRI_SERVICE:
      case GATT_UUID_SEC_SERVICE:
        uuid = attr.value.service.uuid.To128BitBE();
        bytes.insert(bytes.cend(), uuid.cbegin(), uuid.cend());
        bytes.push_back(attr.value.service.end_handle & 0xff);
        bytes.push_back(attr.value.service.end_handle >> 8);
        break;
      case GATT_UUID_INCLUDE_SERVICE:
        /* included service definition */
        bytes.push_back(attr.value.included_service.handle & 0xff);
        bytes.push_back(attr.value.included_service.handle >> 8);
        bytes.push_back(attr.value.included_service.end_handle & 0xff);
        bytes.push_back(attr.value.included_service.end_handle >> 8);
        uuid = attr.value.included_service.uuid.To128BitBE();
        bytes.insert(bytes.cend(), uuid.cbegin(), uuid.cend());
        break;
      case GATT_UUID_CHAR_DECLARE:
        /* characteristic definition */
        bytes.push_back(attr.value.characteristic.properties);
        bytes.push_back(0);  // Padding byte
        bytes.push_back(attr.value.characteristic.value_handle & 0xff);
        bytes.push_back(attr.value.characteristic.value_handle >> 8);
        uuid = attr.value.characteristic.uuid.To128BitBE();
        bytes.insert(bytes.cend(), uuid.cbegin(), uuid.cend());
        break;
      case GATT_UUID_CHAR_EXT_PROP:
        /* for descriptor we store value only for
         * «Characteristic Extended Properties» */
        bytes.push_back(attr.value.characteristic_extended_properties & 0xff);
        bytes.push_back(attr.value.characteristic_extended_properties >> 8);
        break;
      default:
        // LOG_VERBOSE("Unhandled type UUID 0x%04x", attr.type.As16Bit());
        break;
    }
  }
  // padding
  for (size_t i = bytes.size() - original_size;
       i < StoredAttribute::kSizeOnDisk; i++) {
    bytes.push_back(0);
  }
}

/*******************************************************************************
 *
 * Function         bta_gattc_store_db
@@ -201,7 +259,14 @@ static bool bta_gattc_store_db(const char* fname,
    return false;
  }

  if (fwrite(attr.data(), sizeof(StoredAttribute), num_attr, fd) != num_attr) {
  std::vector<uint8_t> db_bytes;
  db_bytes.reserve(num_attr * StoredAttribute::kSizeOnDisk);
  for (const auto attribute : attr) {
    StoredAttribute::SerializeStoredAttribute(attribute, db_bytes);
  }

  if (fwrite(db_bytes.data(), sizeof(uint8_t), db_bytes.size(), fd) !=
      db_bytes.size()) {
    LOG(ERROR) << __func__ << ": can't write GATT cache attributes: " << fname;
    fclose(fd);
    return false;
+0 −1
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@

#include <algorithm>
#include <list>
#include <memory>
#include <sstream>

#include "bt_trace.h"
+4 −2
Original line number Diff line number Diff line
@@ -19,9 +19,7 @@
#pragma once

#include <list>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "stack/include/bt_types.h" /* for Octet16 */
@@ -33,6 +31,8 @@ constexpr uint16_t HANDLE_MAX = 0xffff;

/* Representation of GATT attribute for storage */
struct StoredAttribute {
  // kSizeOnDisk includes two padding bytes for backward compatibility.
  static constexpr size_t kSizeOnDisk = 38;
  uint16_t handle;
  bluetooth::Uuid type;

@@ -61,6 +61,8 @@ struct StoredAttribute {
     * «Characteristic Extended Properties» */
    uint16_t characteristic_extended_properties;
  } value;
  static void SerializeStoredAttribute(const StoredAttribute& attr,
                                       std::vector<uint8_t>& bytes);
};

struct IncludedService;
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#pragma once

#include <set>
#include <utility>

#include "bta/gatt/database.h"
Loading