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

Commit d4228011 authored by Myles Watson's avatar Myles Watson Committed by Gerrit Code Review
Browse files

Merge "Explicitly serialize GATT DB entries" into main

parents 1ffac7f8 e0d5f031
Loading
Loading
Loading
Loading
+50 −3
Original line number Original line Diff line number Diff line
@@ -233,9 +233,6 @@ cc_test {
        "test/bta_dm_cust_uuid_test.cc",
        "test/bta_dm_cust_uuid_test.cc",
        "test/bta_hf_client_add_record_test.cc",
        "test/bta_hf_client_add_record_test.cc",
        "test/bta_hf_client_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: [
    generated_headers: [
        "BluetoothGeneratedDumpsysDataSchema_h",
        "BluetoothGeneratedDumpsysDataSchema_h",
@@ -272,6 +269,56 @@ cc_test {
    cflags: ["-Wno-unused-parameter"],
    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
// bta unit tests for target
cc_test {
cc_test {
    name: "net_test_bta_security",
    name: "net_test_bta_security",
+67 −2
Original line number Original line Diff line number Diff line
@@ -27,7 +27,10 @@
#include <vector>
#include <vector>


#include "bta/gatt/bta_gattc_int.h"
#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 gatt::StoredAttribute;
using std::string;
using std::string;
@@ -165,6 +168,61 @@ gatt::Database bta_gattc_hash_load(const Octet16& hash) {
  return bta_gattc_load_db(fname);
  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
 * Function         bta_gattc_store_db
@@ -201,7 +259,14 @@ static bool bta_gattc_store_db(const char* fname,
    return false;
    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;
    LOG(ERROR) << __func__ << ": can't write GATT cache attributes: " << fname;
    fclose(fd);
    fclose(fd);
    return false;
    return false;
+0 −1
Original line number Original line Diff line number Diff line
@@ -22,7 +22,6 @@


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


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


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


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


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


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


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


#pragma once
#pragma once


#include <set>
#include <utility>
#include <utility>


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