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

Commit ea1561c7 authored by sravan voleti's avatar sravan voleti Committed by Qasim Javed
Browse files

Bluetooth advance audio code

Bluetooth advance audio code
Snapshot as of 326217e71ec6099715f76338356bda6e6e605132.
Submitted on behalf of a third-party:
The Linux Foundation, Broadcom Corporation, The Android Open Source Project, Google, Inc
License rights, if any, to the submission are granted solely by the
copyright owner of such submission under its applicable intellectual
property.
Copyright (c) 2014-2015, 2020-2021 The Linux Foundation. All rights reserved
Copyright (C) 2003-2012 Broadcom Corporation
Copyright (C) 2008, 2011-2012, 2016-2018 The Android Open Source Project
Copyright (C) 2014 Google, Inc
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Third Party code includes additions/modifications from Qualcomm Innovation Center, Inc.

Change-Id: I4bbebd14323c4b4fa292ecb617c16918386d867c
parent 54135c6c
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
cc_binary {
    name: "bap_uclient_test",
    system_ext_specific: true,
    enabled: false,
    srcs: ["bap_uclient_test.cpp"],

    include_dirs: [
        ".",
        "packages/modules/Bluetooth/system/include",
        "packages/modules/Bluetooth/system/types",
        "vendor/qcom/opensource/commonsys/packages/modules/Bluetooth/system/stack/l2cap",
        "vendor/qcom/opensource/commonsys/packages/modules/Bluetooth/system/utils/include",
        "vendor/qcom/opensource/commonsys/packages/modules/Bluetooth/system",
        "vendor/qcom/opensource/commonsys/bluetooth_lea/vhal/include",
        "external/libchrome",
    ],

    cflags: ["-DHAS_NO_BDROID_BUILDCFG"],

    shared_libs: [
        "libcutils",
        "libchrome",
        "libutils",
    ],

    static_libs: ["libbluetooth-types-qti"],

}
+1904 −0

File added.

Preview size limit exceeded, changes collapsed.

+17 −0
Original line number Diff line number Diff line

cc_library_static {
    name: "libbluetooth-types-qti",
    vendor_available: true,
    enabled: false,
    defaults: ["fluoride_types_defaults"],
    cflags: [
        /* we export all classes, so change default visibility, instead of having EXPORT_SYMBOL on each class*/
        "-fvisibility=default",
    ],
    host_supported: true,
    srcs: [
        "class_of_device.cc",
        "raw_address.cc",
        "bluetooth/uuid.cc",
    ],
}
+177 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2017 The Android Open Source Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "uuid.h"

#include <base/rand_util.h>
#include <base/strings/stringprintf.h>
#include <string.h>
#include <algorithm>

namespace bluetooth {

static_assert(sizeof(Uuid) == 16, "Uuid must be 16 bytes long!");

using UUID128Bit = Uuid::UUID128Bit;

const Uuid Uuid::kEmpty = Uuid::From128BitBE(UUID128Bit{{0x00}});

namespace {
constexpr Uuid kBase = Uuid::From128BitBE(
    UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
                0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}});
}  // namespace

size_t Uuid::GetShortestRepresentationSize() const {
  if (memcmp(uu.data() + kNumBytes32, kBase.uu.data() + kNumBytes32,
             kNumBytes128 - kNumBytes32) != 0) {
    return kNumBytes128;
  }

  if (uu[0] == 0 && uu[1] == 0) return kNumBytes16;

  return kNumBytes32;
}

bool Uuid::Is16Bit() const {
  return GetShortestRepresentationSize() == kNumBytes16;
}

uint16_t Uuid::As16Bit() const { return (((uint16_t)uu[2]) << 8) + uu[3]; }

uint32_t Uuid::As32Bit() const {
  return (((uint32_t)uu[0]) << 24) + (((uint32_t)uu[1]) << 16) +
         (((uint32_t)uu[2]) << 8) + uu[3];
}

Uuid Uuid::FromString(const std::string& uuid, bool* is_valid) {
  if (is_valid) *is_valid = false;
  Uuid ret = kBase;

  if (uuid.empty()) return ret;

  uint8_t* p = ret.uu.data();
  if (uuid.size() == kString128BitLen) {
    if (uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' ||
        uuid[23] != '-') {
      return ret;
    }

    int c;
    int rc =
        sscanf(uuid.c_str(),
               "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
               "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%n",
               &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8],
               &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15], &c);
    if (rc != 16) return ret;
    if (c != kString128BitLen) return ret;

    if (is_valid) *is_valid = true;
  } else if (uuid.size() == 8) {
    int c;
    int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%n", &p[0], &p[1],
                    &p[2], &p[3], &c);
    if (rc != 4) return ret;
    if (c != 8) return ret;

    if (is_valid) *is_valid = true;
  } else if (uuid.size() == 4) {
    int c;
    int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%n", &p[2], &p[3], &c);
    if (rc != 2) return ret;
    if (c != 4) return ret;

    if (is_valid) *is_valid = true;
  }

  return ret;
}

Uuid Uuid::From16Bit(uint16_t uuid16) {
  Uuid u = kBase;

  u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8);
  u.uu[3] = (uint8_t)(0x00FF & uuid16);
  return u;
}

Uuid Uuid::From32Bit(uint32_t uuid32) {
  Uuid u = kBase;

  u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24);
  u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16);
  u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8);
  u.uu[3] = (uint8_t)(0x000000FF & uuid32);
  return u;
}

Uuid Uuid::From128BitBE(const uint8_t* uuid) {
  UUID128Bit tmp;
  memcpy(tmp.data(), uuid, kNumBytes128);
  return From128BitBE(tmp);
}

Uuid Uuid::From128BitLE(const UUID128Bit& uuid) {
  Uuid u;
  std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin());
  return u;
}

Uuid Uuid::From128BitLE(const uint8_t* uuid) {
  UUID128Bit tmp;
  memcpy(tmp.data(), uuid, kNumBytes128);
  return From128BitLE(tmp);
}

const UUID128Bit Uuid::To128BitLE() const {
  UUID128Bit le;
  std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin());
  return le;
}

const UUID128Bit& Uuid::To128BitBE() const { return uu; }

Uuid Uuid::GetRandom() {
  Uuid uuid;
  base::RandBytes(uuid.uu.data(), uuid.uu.size());
  return uuid;
}

bool Uuid::IsEmpty() const { return *this == kEmpty; }

void Uuid::UpdateUuid(const Uuid& uuid) {
  uu = uuid.uu;
}

bool Uuid::operator<(const Uuid& rhs) const {
  return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(),
                                      rhs.uu.end());
}

bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; }

bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; }

std::string Uuid::ToString() const {
  return base::StringPrintf(
      "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
      uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9],
      uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
}
}  // namespace bluetooth
+143 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2017 The Android Open Source Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

#include <stdint.h>
#include <array>
#include <string>

namespace bluetooth {

// This class is representing Bluetooth UUIDs across whole stack.
// Here are some general endianness rules:
// 1. UUID is internally kept as as Big Endian.
// 2. Bytes representing UUID coming from upper layers, Java or Binder, are Big
//    Endian.
// 3. Bytes representing UUID coming from lower layer, HCI packets, are Little
//    Endian.
// 4. UUID in storage is always string.
class Uuid final {
 public:
  static constexpr size_t kNumBytes128 = 16;
  static constexpr size_t kNumBytes32 = 4;
  static constexpr size_t kNumBytes16 = 2;

  static constexpr size_t kString128BitLen = 36;

  static const Uuid kEmpty;  // 00000000-0000-0000-0000-000000000000

  using UUID128Bit = std::array<uint8_t, kNumBytes128>;

  Uuid() = default;

  // Creates and returns a random 128-bit UUID.
  static Uuid GetRandom();

  // Returns the shortest possible representation of this UUID in bytes. Either
  // kNumBytes16, kNumBytes32, or kNumBytes128
  size_t GetShortestRepresentationSize() const;

  // Returns true if this UUID can be represented as 16 bit.
  bool Is16Bit() const;

  // Returns 16 bit Little Endian representation of this UUID. Use
  // GetShortestRepresentationSize() or Is16Bit() before using this method.
  uint16_t As16Bit() const;

  // Returns 32 bit Little Endian representation of this UUID. Use
  // GetShortestRepresentationSize() before using this method.
  uint32_t As32Bit() const;

  // Converts string representing 128, 32, or 16 bit UUID in
  // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx, or xxxx format to UUID. If
  // set, optional is_valid parameter will be set to true if conversion is
  // successfull, false otherwise.
  static Uuid FromString(const std::string& uuid, bool* is_valid = nullptr);

  // Converts 16bit Little Endian representation of UUID to UUID
  static Uuid From16Bit(uint16_t uuid16bit);

  // Converts 32bit Little Endian representation of UUID to UUID
  static Uuid From32Bit(uint32_t uuid32bit);

  // Converts 128 bit Big Endian array representing UUID to UUID.
  static constexpr Uuid From128BitBE(const UUID128Bit& uuid) {
    Uuid u(uuid);
    return u;
  }

  // Converts 128 bit Big Endian array representing UUID to UUID. |uuid| points
  // to beginning of array.
  static Uuid From128BitBE(const uint8_t* uuid);

  // Converts 128 bit Little Endian array representing UUID to UUID.
  static Uuid From128BitLE(const UUID128Bit& uuid);

  // Converts 128 bit Little Endian array representing UUID to UUID. |uuid|
  // points to beginning of array.
  static Uuid From128BitLE(const uint8_t* uuid);

  // Returns 128 bit Little Endian representation of this UUID
  const UUID128Bit To128BitLE() const;

  // Returns 128 bit Big Endian representation of this UUID
  const UUID128Bit& To128BitBE() const;

  // Returns string representing this UUID in
  // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format, lowercase.
  std::string ToString() const;

  // Returns true if this UUID is equal to kEmpty
  bool IsEmpty() const;

  // Update UUID with new value
  void UpdateUuid(const Uuid& uuid);

  bool operator<(const Uuid& rhs) const;
  bool operator==(const Uuid& rhs) const;
  bool operator!=(const Uuid& rhs) const;

 private:
  constexpr Uuid(const UUID128Bit& val) : uu{val} {};

  // Network-byte-ordered ID (Big Endian).
  UUID128Bit uu;
};
}  // namespace bluetooth

inline std::ostream& operator<<(std::ostream& os, const bluetooth::Uuid& a) {
  os << a.ToString();
  return os;
}

// Custom std::hash specialization so that bluetooth::UUID can be used as a key
// in std::unordered_map.
namespace std {

template <>
struct hash<bluetooth::Uuid> {
  std::size_t operator()(const bluetooth::Uuid& key) const {
    const auto& uuid_bytes = key.To128BitBE();
    std::hash<std::string> hash_fn;
    return hash_fn(std::string(reinterpret_cast<const char*>(uuid_bytes.data()),
                               uuid_bytes.size()));
  }
};

}  // namespace std
Loading