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

Commit ba272e54 authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

gdx: bt property special case names am: a1a41fed

parents 2912d6ca a1a41fed
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -346,9 +346,8 @@ std::shared_ptr<RemoteASHATruncatedHiSyncId> RemoteASHATruncatedHiSyncId::Create
    const uint32_t& id) {
  return std::make_shared<RemoteASHATruncatedHiSyncId>(RemoteASHATruncatedHiSyncId(id));
}
std::shared_ptr<RemoteModelNum> RemoteModelNum::Create(const uint8_t* model, size_t len) {
  ASSERT(model != nullptr);
  return std::make_shared<RemoteModelNum>(RemoteModelNum(model, len));
std::shared_ptr<RemoteModelNum> RemoteModelNum::Create(const bt_bdname_t& name) {
  return std::make_shared<RemoteModelNum>(RemoteModelNum(name));
}
std::shared_ptr<RemoteAddrType> RemoteAddrType::Create(const uint8_t& addr) {
  return std::make_shared<RemoteAddrType>(RemoteAddrType(addr));
+27 −11
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class BtProperty {
  virtual ~BtProperty() = default;

 private:
  bt_property_type_t type_;
  const bt_property_type_t type_;
};

// Provide pointer/size access to properties for legacy jni API
@@ -114,14 +114,25 @@ class BtPropertyVector : public BtProperty {
  BtPropertyVector<T>(bt_property_type_t type, const T* val, size_t size)
      : BtProperty(type), val_(std::make_shared<std::vector<T>>(val, val + size)) {}

 private:
 protected:
  std::shared_ptr<std::vector<T>> val_;
};

class BdName : public BtPropertyVector<uint8_t> {
template <typename T>
class BtPropertyVectorWithPad : public BtPropertyVector<T> {
 protected:
  // Create a vector property from a raw pointer and size with pad element
  BtPropertyVectorWithPad<T>(bt_property_type_t type, const T* val, size_t size, T pad)
      : BtPropertyVector<T>(type, val, size) {
    BtPropertyVector<T>::val_->push_back(pad);
  }
};

class BdName : public BtPropertyVectorWithPad<uint8_t> {
 public:
  BdName(const BD_NAME bd_name)
      : BtPropertyVector<uint8_t>(BT_PROPERTY_BDNAME, bd_name, kBdNameLength) {}
      : BtPropertyVectorWithPad<uint8_t>(BT_PROPERTY_BDNAME, bd_name, kBdNameLength, kBdNameDelim) {
  }

  static std::shared_ptr<BdName> Create(const BD_NAME bd_name);
};
@@ -185,10 +196,11 @@ class AdapterDiscoverableTimeout : public BtPropertySimple<uint32_t> {
  static std::shared_ptr<AdapterDiscoverableTimeout> Create(const uint32_t& timeout);
};

class RemoteFriendlyName : public BtPropertyVector<uint8_t> {
class RemoteFriendlyName : public BtPropertyVectorWithPad<uint8_t> {
 public:
  RemoteFriendlyName(const uint8_t bd_name[], size_t len)
      : BtPropertyVector<uint8_t>(BT_PROPERTY_REMOTE_FRIENDLY_NAME, bd_name, len) {}
      : BtPropertyVectorWithPad<uint8_t>(
            BT_PROPERTY_REMOTE_FRIENDLY_NAME, bd_name, len, kBdNameDelim) {}

  static std::shared_ptr<RemoteFriendlyName> Create(const uint8_t bd_name[], size_t len);
};
@@ -264,12 +276,16 @@ class RemoteASHATruncatedHiSyncId : public BtPropertySimple<uint32_t> {
  static std::shared_ptr<RemoteASHATruncatedHiSyncId> Create(const uint32_t& id);
};

class RemoteModelNum : public BtPropertyVector<uint8_t> {
class RemoteModelNum : public BtPropertyVectorWithPad<uint8_t> {
 public:
  RemoteModelNum(const uint8_t* model, size_t len)
      : BtPropertyVector<uint8_t>(BT_PROPERTY_REMOTE_MODEL_NUM, model, len) {}

  static std::shared_ptr<RemoteModelNum> Create(const uint8_t* model, size_t len);
  RemoteModelNum(const bt_bdname_t& name)
      : BtPropertyVectorWithPad<uint8_t>(
            BT_PROPERTY_REMOTE_MODEL_NUM,
            name.name,
            sizeof(bt_bdname_t) - sizeof(kBdNameDelim),
            kBdNameDelim) {}

  static std::shared_ptr<RemoteModelNum> Create(const bt_bdname_t& name);
};

class RemoteAddrType : public BtPropertySimple<uint8_t> {
+31 −5
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@

#include "discovery/device/bt_property.h"

#include <cstdint>
#include <future>

#include "gtest/gtest.h"
#include "hardware/bluetooth.h"
#include "os/log.h"
#include "stack/include/bt_name.h"

using namespace bluetooth::property;
@@ -28,6 +30,13 @@ namespace {

constexpr size_t kNumberTestedProperties = 22;

constexpr size_t kBdPropNameLength = kBdNameLength + sizeof(kBdNameDelim);

constexpr uint8_t kReallyLongName[kBdPropNameLength] =
    "aaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaa"
    "aaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaa"
    "aAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaa";

// BT_PROPERTY_BDNAME
constexpr BD_NAME kBdName{'k', 'B', 'd', 'N', 'a', 'm', 'e', '\0'};

@@ -158,7 +167,7 @@ void fill_property(
  switch (type) {
    case BT_PROPERTY_BDNAME: {
      properties.push_back(BdName::Create(kBdName));
      ASSERT_EQ(kBdNameLength, properties.back()->Size());
      ASSERT_EQ(kBdPropNameLength, properties.back()->Size());
    } break;

    case BT_PROPERTY_BDADDR:
@@ -204,7 +213,7 @@ void fill_property(
    case BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
      properties.push_back(
          RemoteFriendlyName::Create(kRemoteFriendlyName, sizeof(kRemoteFriendlyName)));
      ASSERT_EQ(sizeof(kRemoteFriendlyName), properties.back()->Size());
      ASSERT_EQ(sizeof(kRemoteFriendlyName) + sizeof(kBdNameDelim), properties.back()->Size());
    } break;

    case BT_PROPERTY_REMOTE_RSSI:
@@ -260,7 +269,7 @@ void fill_property(
      break;

    case BT_PROPERTY_REMOTE_MODEL_NUM: {
      properties.push_back(RemoteModelNum::Create(kRemoteModelNum.name, sizeof(kRemoteModelNum)));
      properties.push_back(RemoteModelNum::Create(kRemoteModelNum));
      ASSERT_EQ(sizeof(kRemoteModelNum), properties.back()->Size());
    } break;

@@ -285,7 +294,7 @@ void verify_property(const bt_property_type_t& type, const bt_property_t& proper
  ASSERT_EQ(type, property.type);
  switch (property.type) {
    case BT_PROPERTY_BDNAME:
      ASSERT_EQ((int)kBdNameLength, property.len);
      ASSERT_EQ((int)kBdPropNameLength, property.len);
      ASSERT_STREQ((const char*)kBdName, (const char*)property.val);
      break;

@@ -338,7 +347,7 @@ void verify_property(const bt_property_type_t& type, const bt_property_t& proper
      break;

    case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
      ASSERT_EQ((int)sizeof(kRemoteFriendlyName), property.len);
      ASSERT_EQ((int)(sizeof(kRemoteFriendlyName) + sizeof(kBdNameDelim)), property.len);
      ASSERT_STREQ((const char*)kRemoteFriendlyName, (const char*)property.val);
      break;

@@ -799,6 +808,23 @@ TEST_F(BtPropertyTest, serialize_and_verify) {
  }
}

TEST_F(BtPropertyTest, name_too_long) {
  std::vector<std::shared_ptr<BtProperty>> properties;
  BD_NAME bd_name;
  for (size_t i = 0; i < kBdPropNameLength; i++) {
    bd_name[i] = ((i + 1) % 10) ? 'a' : 'A';
  }

  properties.push_back(BdName::Create(bd_name));
  BtPropertyLegacy legacy(properties);
  ASSERT_EQ(1U, legacy.NumProperties());

  bt_property_t bt_properties[1];
  legacy.Export(bt_properties, 1U);

  ASSERT_STREQ((const char*)kReallyLongName, (const char*)bt_properties[0].val);
}

class BtPropertyArrayTest : public testing::Test {
 protected:
  void SetUp() override {
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ typedef uint8_t tBTM_LOC_BD_NAME[BTM_MAX_LOC_BD_NAME_LEN + 1];
#include "osi/include/compat.h"  // strlcpy
inline constexpr tBTM_BD_NAME kBtmBdNameEmpty = {};
constexpr size_t kBdNameLength = static_cast<size_t>(BD_NAME_LEN);
constexpr uint8_t kBdNameDelim = (uint8_t)NULL;

inline size_t bd_name_copy(BD_NAME bd_name_dest, const char* src) {
  return strlcpy(reinterpret_cast<char*>(bd_name_dest), const_cast<char*>(src),