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

Commit 8da1217f authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I87ce02f0,I2c81de7a

* changes:
  L2CAP: Use AddressWithType in L2CAP LE APIs
  HCI: Create AddressWithType and its unit tests
parents b56c65cb 10ed2a3e
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -17,10 +17,11 @@ filegroup {
    srcs: [
        "acl_builder_test.cc",
        "acl_manager_test.cc",
        "classic_security_manager_test.cc",
        "controller_test.cc",
        "address_unittest.cc",
        "address_with_type_test.cc",
        "class_of_device_unittest.cc",
        "classic_security_manager_test.cc",
        "controller_test.cc",
        "device_test.cc",
        "device_database_test.cc",
        "dual_device_test.cc",
+96 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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 <sstream>
#include <string>
#include <utility>

#include "hci/address.h"
#include "hci/hci_packets.h"

namespace bluetooth {
namespace hci {

class AddressWithType final {
 public:
  AddressWithType(Address address, AddressType address_type) : address_(address), address_type_(address_type) {}

  explicit AddressWithType() : address_(Address::kEmpty), address_type_(AddressType::PUBLIC_DEVICE_ADDRESS) {}

  inline Address GetAddress() const {
    return address_;
  }

  inline AddressType GetAddressType() const {
    return address_type_;
  }

  bool operator<(const AddressWithType& rhs) const {
    return address_ < rhs.address_ && address_type_ < rhs.address_type_;
  }
  bool operator==(const AddressWithType& rhs) const {
    return address_ == rhs.address_ && address_type_ == rhs.address_type_;
  }
  bool operator>(const AddressWithType& rhs) const {
    return (rhs < *this);
  }
  bool operator<=(const AddressWithType& rhs) const {
    return !(*this > rhs);
  }
  bool operator>=(const AddressWithType& rhs) const {
    return !(*this < rhs);
  }
  bool operator!=(const AddressWithType& rhs) const {
    return !(*this == rhs);
  }

  std::string ToString() const {
    std::stringstream ss;
    ss << address_ << "[" << AddressTypeText(address_type_) << "]";
    return ss.str();
  }

 private:
  Address address_;
  AddressType address_type_;
};

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

}  // namespace hci
}  // namespace bluetooth

namespace std {
template <>
struct hash<bluetooth::hci::AddressWithType> {
  std::size_t operator()(const bluetooth::hci::AddressWithType& val) const {
    static_assert(sizeof(uint64_t) >= (sizeof(bluetooth::hci::Address) + sizeof(bluetooth::hci::AddressType)));
    uint64_t int_addr = 0;
    memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.GetAddress().address, sizeof(bluetooth::hci::Address));
    bluetooth::hci::AddressType address_type = val.GetAddressType();
    memcpy(reinterpret_cast<uint8_t*>(&int_addr) + sizeof(bluetooth::hci::Address), &address_type,
           sizeof(address_type));
    return std::hash<uint64_t>{}(int_addr);
  }
};
}  // namespace std
 No newline at end of file
+68 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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 <unordered_map>

#include <gtest/gtest.h>

#include "hci/address.h"
#include "hci/address_with_type.h"
#include "hci/hci_packets.h"

namespace bluetooth {
namespace hci {

TEST(AddressWithTypeTest, AddressWithTypeSameValueSameOrder) {
  Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  AddressType type1 = AddressType::PUBLIC_DEVICE_ADDRESS;
  AddressWithType address_with_type_1(addr1, type1);
  Address addr2{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  AddressType type2 = AddressType::PUBLIC_DEVICE_ADDRESS;
  AddressWithType address_with_type_2(addr2, type2);
  // Test if two address with type with same byte value have the same hash
  struct std::hash<bluetooth::hci::AddressWithType> hasher;
  EXPECT_EQ(hasher(address_with_type_1), hasher(address_with_type_2));
  // Test if two address with type with the same hash and the same value, they will
  // still map to the same value
  std::unordered_map<AddressWithType, int> data = {};
  data[address_with_type_1] = 5;
  data[address_with_type_2] = 8;
  EXPECT_EQ(data[address_with_type_1], data[address_with_type_2]);
}

TEST(AddressWithTypeTest, HashDifferentDiffAddrSameType) {
  Address addr{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  AddressType type = AddressType::PUBLIC_IDENTITY_ADDRESS;
  AddressWithType address_with_type(addr, type);
  struct std::hash<AddressWithType> hasher;
  EXPECT_NE(hasher(address_with_type), hasher(AddressWithType(Address::kEmpty, AddressType::PUBLIC_IDENTITY_ADDRESS)));
}

TEST(AddressWithTypeTest, HashDifferentSameAddressDiffType) {
  Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  AddressType type1 = AddressType::PUBLIC_DEVICE_ADDRESS;
  AddressWithType address_with_type_1(addr1, type1);
  Address addr2{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  AddressType type2 = AddressType::PUBLIC_IDENTITY_ADDRESS;
  AddressWithType address_with_type_2(addr2, type2);
  struct std::hash<bluetooth::hci::AddressWithType> hasher;
  EXPECT_NE(hasher(address_with_type_1), hasher(address_with_type_2));
}

}  // namespace hci
}  // namespace bluetooth
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -23,14 +23,14 @@ namespace bluetooth {
namespace l2cap {
namespace le {

bool FixedChannelManager::ConnectServices(hci::Address device, hci::AddressType address_type,
bool FixedChannelManager::ConnectServices(hci::AddressWithType address_with_type,
                                          OnConnectionFailureCallback on_fail_callback, os::Handler* handler) {
  internal::LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
      .handler_ = handler,
      .on_fail_callback_ = std::move(on_fail_callback),
  };
  l2cap_layer_handler_->Post(common::BindOnce(&internal::LinkManager::ConnectFixedChannelServices,
                                              common::Unretained(link_manager_), device, address_type,
                                              common::Unretained(link_manager_), address_with_type,
                                              std::move(pending_fixed_channel_connection)));
  return true;
}
+3 −3
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
#include <string>

#include "hci/acl_manager.h"
#include "hci/address.h"
#include "hci/address_with_type.h"
#include "l2cap/cid.h"
#include "l2cap/le/fixed_channel.h"
#include "l2cap/le/fixed_channel_service.h"
@@ -96,14 +96,14 @@ class FixedChannelManager {
   * A module calling ConnectServices() must have called RegisterService() before.
   * The callback will come back from on_open_callback in the service that is registered
   *
   * @param device: Remote device to make this connection.
   * @param address_with_type: Remote device with type to make this connection.
   * @param address_type: Address type of remote device
   * @param on_fail_callback: A callback to indicate connection failure along with a status code.
   * @param handler: The handler context in which to execute the @callback parameters.
   *
   * Returns: true if connection was able to be initiated, false otherwise.
   */
  bool ConnectServices(hci::Address device, hci::AddressType address_type, OnConnectionFailureCallback on_fail_callback,
  bool ConnectServices(hci::AddressWithType address_with_type, OnConnectionFailureCallback on_fail_callback,
                       os::Handler* handler);

  /**
Loading