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

Commit 922ea520 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Fix address passing when we accept incoming connection

Tag: #gd-refactor
Bug: 155399771
Test: gd/cert/run --host LeSecurityTest
Change-Id: I21ceb49fe1b69e447e3d2e282db5d02b7a0c1bfe
parent f6c7b89f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -38,6 +38,10 @@ class LeAclConnection : public AclConnection {
    return local_address_;
  }

  virtual void SetLocalAddress(AddressWithType local_address) {
    local_address_ = local_address;
  }

  virtual AddressWithType GetRemoteAddress() const {
    return remote_address_;
  }
+11 −2
Original line number Diff line number Diff line
@@ -174,7 +174,6 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
    auto peer_address_type = connection_complete.GetPeerAddressType();
    auto peer_resolvable_address = connection_complete.GetPeerResolvablePrivateAddress();
    AddressWithType remote_address(address, peer_address_type);
    AddressWithType local_address = le_address_manager_->GetCurrentAddress();
    if (!peer_resolvable_address.IsEmpty()) {
      remote_address = AddressWithType(peer_resolvable_address, AddressType::RANDOM_DEVICE_ADDRESS);
    }
@@ -194,6 +193,17 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
                                                common::Unretained(le_client_callbacks_), remote_address, status));
      return;
    }

    auto role = connection_complete.GetRole();
    AddressWithType local_address;
    if (role == hci::Role::MASTER) {
      local_address = le_address_manager_->GetCurrentAddress();
    } else {
      // when accepting connection, we must obtain the address from the advertiser.
      // When we receive "set terminated event", we associate connection handle with advertiser address
      local_address = AddressWithType{};
    }

    uint16_t conn_interval = connection_complete.GetConnInterval();
    uint16_t conn_latency = connection_complete.GetConnLatency();
    uint16_t supervision_timeout = connection_complete.GetSupervisionTimeout();
@@ -208,7 +218,6 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
                                std::forward_as_tuple(remote_address, queue->GetDownEnd(), handler_));
    auto& connection_proxy = check_and_get_le_connection(handle);
    round_robin_scheduler_->Register(RoundRobinScheduler::ConnectionType::LE, handle, queue);
    auto role = connection_complete.GetRole();
    auto do_disconnect =
        common::BindOnce(&DisconnectorForLe::handle_disconnect, common::Unretained(disconnector_), handle);
    std::unique_ptr<LeAclConnection> connection(new LeAclConnection(std::move(queue), le_acl_connection_interface_,
+24 −7
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ enum class AdvertisingApiType {

struct Advertiser {
  os::Handler* handler;
  AddressWithType current_address;
  common::Callback<void(Address, AddressType)> scan_callback;
  common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback;
};
@@ -108,6 +109,11 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
    return num_instances_;
  }

  void register_set_terminated_callback(
      common::ContextualCallback<void(ErrorCode, uint16_t, hci::AddressWithType)> set_terminated_callback) {
    set_terminated_callback_ = std::move(set_terminated_callback);
  }

  void handle_event(LeMetaEventView event) {
    switch (event.GetSubeventCode()) {
      case hci::SubeventCode::SCAN_REQUEST_RECEIVED:
@@ -135,9 +141,11 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
      LOG_INFO("Dropping invalid advertising event");
      return;
    }
    registered_handler_->Post(common::BindOnce(set_terminated_callback_, event_view.GetStatus(),
                                               event_view.GetAdvertisingHandle(),
                                               event_view.GetNumCompletedExtendedAdvertisingEvents()));

    AddressWithType advertiser_address = advertising_sets_[event_view.GetAdvertisingHandle()].current_address;

    set_terminated_callback_.InvokeIfNotEmpty(
        event_view.GetStatus(), event_view.GetConnectionHandle(), advertiser_address);
  }

  AdvertiserId allocate_advertiser() {
@@ -175,6 +183,7 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
    advertising_sets_[id].scan_callback = scan_callback;
    advertising_sets_[id].set_terminated_callback = set_terminated_callback;
    advertising_sets_[id].handler = handler;
    advertising_sets_[id].current_address = AddressWithType{};

    if (!address_manager_registered) {
      le_address_manager_->Register(this);
@@ -233,8 +242,10 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
              hci::LeMultiAdvtSetScanRespBuilder::Create(config.scan_response, id),
              module_handler_->BindOnce(impl::check_status<LeMultiAdvtCompleteView>));
        }

        advertising_sets_[id].current_address = le_address_manager_->GetAnotherAddress();
        le_advertising_interface_->EnqueueCommand(
            hci::LeMultiAdvtSetRandomAddrBuilder::Create(le_address_manager_->GetAnotherAddress().GetAddress(), id),
            hci::LeMultiAdvtSetRandomAddrBuilder::Create(advertising_sets_[id].current_address.GetAddress(), id),
            module_handler_->BindOnce(impl::check_status<LeMultiAdvtCompleteView>));
        if (!paused) {
          le_advertising_interface_->EnqueueCommand(
@@ -313,9 +324,10 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
          module_handler_->BindOnce(impl::check_status<LeSetExtendedAdvertisingParametersCompleteView>));
    }

    advertising_sets_[id].current_address = le_address_manager_->GetAnotherAddress();
    le_advertising_interface_->EnqueueCommand(
        hci::LeSetExtendedAdvertisingRandomAddressBuilder::Create(
            id, le_address_manager_->GetAnotherAddress().GetAddress()),
            id, advertising_sets_[id].current_address.GetAddress()),
        module_handler_->BindOnce(impl::check_status<LeSetExtendedAdvertisingRandomAddressCompleteView>));
    if (!config.scan_response.empty()) {
      le_advertising_interface_->EnqueueCommand(
@@ -449,7 +461,7 @@ struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallb
  }

  common::Callback<void(Address, AddressType)> scan_callback_;
  common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback_;
  common::ContextualCallback<void(ErrorCode, uint16_t, hci::AddressWithType)> set_terminated_callback_;
  os::Handler* registered_handler_{nullptr};
  Module* module_;
  os::Handler* module_handler_;
@@ -566,7 +578,12 @@ AdvertiserId LeAdvertisingManager::ExtendedCreateAdvertiser(
}

void LeAdvertisingManager::RemoveAdvertiser(AdvertiserId id) {
  GetHandler()->Post(common::BindOnce(&impl::remove_advertiser, common::Unretained(pimpl_.get()), id));
  GetHandler()->CallOn(pimpl_.get(), &impl::remove_advertiser, id);
}

void LeAdvertisingManager::RegisterSetTerminatedCallback(
    common::ContextualCallback<void(ErrorCode, uint16_t, hci::AddressWithType)> set_terminated_callback) {
  GetHandler()->CallOn(pimpl_.get(), &impl::register_set_terminated_callback, set_terminated_callback);
}

}  // namespace hci
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

#include <memory>

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

@@ -79,6 +80,9 @@ class LeAdvertisingManager : public bluetooth::Module {

  void RemoveAdvertiser(AdvertiserId id);

  virtual void RegisterSetTerminatedCallback(
      common::ContextualCallback<void(ErrorCode, uint16_t, hci::AddressWithType)> set_terminated_callback);

  static const ModuleFactory Factory;

 protected:
+60 −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 "hci/le_advertising_manager.h"

#include <gmock/gmock.h>

// Unit test interfaces
namespace bluetooth {
namespace hci {

struct LeAdvertisingManager::impl : public bluetooth::hci::LeAddressManagerCallback {};

namespace testing {

using hci::AdvertiserId;
using hci::LeAdvertisingManager;

class MockLeAdvertisingManager : public LeAdvertisingManager {
 public:
  MOCK_METHOD(size_t, GetNumberOfAdvertisingInstances, (), (const));
  MOCK_METHOD(
      AdvertiserId,
      CreateAdvertiser,
      (const AdvertisingConfig&,
       const common::Callback<void(Address, AddressType)>&,
       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>&,
       os::Handler*));
  MOCK_METHOD(
      AdvertiserId,
      ExtendedCreateAdvertiser,
      (const ExtendedAdvertisingConfig&,
       const common::Callback<void(Address, AddressType)>&,
       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>&,
       os::Handler*));
  MOCK_METHOD(void, RemoveAdvertiser, (AdvertiserId));
  MOCK_METHOD(
      void,
      RegisterSetTerminatedCallback,
      (common::ContextualCallback<void(ErrorCode, uint16_t, hci::AddressWithType)>), (override));

};

}  // namespace testing
}  // namespace hci
}  // namespace bluetooth
Loading