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

Commit 04b0094d authored by Arman Uguray's avatar Arman Uguray
Browse files

service: Add UUID::GetRandom

Added a static class method to UUID that returns a random 128-bit UUID.
Also converted the enum constants to pure integer constants to make
their meaning more explicit.

Bug: 23395353
Change-Id: If8b97d40c020fdfc8b460b537b2da7c62b0c24ad
parent 849a7758
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ using namespace bluetooth;

namespace {

const std::array<uint8_t, UUID::kUUID128Octets> kBtSigBaseUUID = {
const std::array<uint8_t, UUID::kNumBytes128> kBtSigBaseUUID = {
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
      0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb, }
};
+13 −4
Original line number Diff line number Diff line
@@ -21,8 +21,17 @@
#include <stack>
#include <string>

#include <base/rand_util.h>

namespace bluetooth {

// static
UUID UUID::GetRandom() {
  UUID128Bit bytes;
  base::RandBytes(bytes.data(), bytes.size());
  return UUID(bytes);
}

void UUID::InitializeDefault() {
  // Initialize to base bluetooth UUID.
  id_ = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
@@ -47,17 +56,17 @@ UUID::UUID(const bt_uuid_t& uuid) {
  std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin());
}

UUID::UUID(const UUID::UUID16Bit& uuid) {
UUID::UUID(const UUID16Bit& uuid) {
  InitializeDefault();
  std::copy(uuid.begin(), uuid.end(), id_.begin() + kUUID16Octets);
  std::copy(uuid.begin(), uuid.end(), id_.begin() + kNumBytes16);
}

UUID::UUID(const UUID::UUID32Bit& uuid) {
UUID::UUID(const UUID32Bit& uuid) {
  InitializeDefault();
  std::copy(uuid.begin(), uuid.end(), id_.begin());
}

UUID::UUID(const UUID::UUID128Bit& uuid) : id_(uuid) {}
UUID::UUID(const UUID128Bit& uuid) : id_(uuid) {}

const UUID::UUID128Bit UUID::GetFullBigEndian() const {
  return id_;
+12 −11
Original line number Diff line number Diff line
@@ -25,15 +25,16 @@ namespace bluetooth {

class UUID {
 public:
  enum Type {
    kUUID128Octets = 16,
    kUUID32Octets = 4,
    kUUID16Octets = 2,
  };
  static constexpr int kNumBytes128 = 16;
  static constexpr int kNumBytes32 = 4;
  static constexpr int kNumBytes16 = 2;

  typedef std::array<uint8_t, kNumBytes16> UUID16Bit;
  typedef std::array<uint8_t, kNumBytes32> UUID32Bit;
  typedef std::array<uint8_t, kNumBytes128> UUID128Bit;

  typedef std::array<uint8_t, UUID::kUUID16Octets> UUID16Bit;
  typedef std::array<uint8_t, UUID::kUUID32Octets> UUID32Bit;
  typedef std::array<uint8_t, UUID::kUUID128Octets> UUID128Bit;
  // Creates and returns a random 128-bit UUID.
  static UUID GetRandom();

  // Construct a Bluetooth 'base' UUID.
  UUID();
@@ -45,9 +46,9 @@ class UUID {
  explicit UUID(const std::string& uuid);

  // std::array variants constructors.
  explicit UUID(const UUID::UUID16Bit& uuid);
  explicit UUID(const UUID::UUID32Bit& uuid);
  explicit UUID(const UUID::UUID128Bit& uuid);
  explicit UUID(const UUID16Bit& uuid);
  explicit UUID(const UUID32Bit& uuid);
  explicit UUID(const UUID128Bit& uuid);

  // Provide the full network-byte-ordered blob.
  const UUID128Bit GetFullBigEndian() const;