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

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

Merge "Handle encryption change in SecurityManager rather than AclManager"

parents 1d859b5a 66ab8a70
Loading
Loading
Loading
Loading
+18 −5
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include "hci/acl_fragmenter.h"
#include "hci/controller.h"
#include "hci/hci_layer.h"
#include "security/security_module.h"

namespace bluetooth {
namespace hci {
@@ -154,7 +155,7 @@ struct AclManager::acl_connection {
  }
};

struct AclManager::impl {
struct AclManager::impl : public security::ISecurityManagerListener {
  impl(const AclManager& acl_manager) : acl_manager_(acl_manager) {}

  void Start() {
@@ -212,8 +213,6 @@ struct AclManager::impl {
    hci_layer_->RegisterEventHandler(EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE,
                                     Bind(&impl::on_read_remote_version_information_complete, common::Unretained(this)),
                                     handler_);
    hci_layer_->RegisterEventHandler(EventCode::ENCRYPTION_CHANGE,
                                     Bind(&impl::on_encryption_change, common::Unretained(this)), handler_);
    hci_layer_->RegisterEventHandler(EventCode::LINK_SUPERVISION_TIMEOUT_CHANGED,
                                     Bind(&impl::on_link_supervision_timeout_changed, common::Unretained(this)),
                                     handler_);
@@ -237,6 +236,7 @@ struct AclManager::impl {
    hci_queue_end_ = nullptr;
    handler_ = nullptr;
    hci_layer_ = nullptr;
    security_manager_.reset();
  }

  void incoming_acl_credits(uint16_t handle, uint16_t credits) {
@@ -588,8 +588,11 @@ struct AclManager::impl {
    }
  }

  void on_encryption_change(EventPacketView packet) {
    EncryptionChangeView encryption_change_view = EncryptionChangeView::Create(packet);
  void OnDeviceBonded(bluetooth::hci::AddressWithType device) override {}
  void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) override {}
  void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) override {}

  void OnEncryptionStateChanged(EncryptionChangeView encryption_change_view) override {
    if (!encryption_change_view.IsValid()) {
      LOG_ERROR("Received on_encryption_change with invalid packet");
      return;
@@ -1190,6 +1193,11 @@ struct AclManager::impl {
        handler_);
  }

  void set_security_module(security::SecurityModule* security_module) {
    security_manager_ = security_module->GetSecurityManager();
    security_manager_->RegisterCallbackListener(this, handler_);
  }

  void accept_connection(Address address) {
    auto role = AcceptConnectionRequestRole::BECOME_MASTER;  // We prefer to be master
    hci_layer_->EnqueueCommand(AcceptConnectionRequestBuilder::Create(address, role),
@@ -1915,6 +1923,7 @@ struct AclManager::impl {
  std::map<uint16_t, acl_connection>::iterator current_connection_pair_;

  HciLayer* hci_layer_ = nullptr;
  std::unique_ptr<security::SecurityManager> security_manager_;
  os::Handler* handler_ = nullptr;
  ConnectionCallbacks* client_callbacks_ = nullptr;
  os::Handler* client_handler_ = nullptr;
@@ -2143,6 +2152,10 @@ void AclManager::WriteDefaultLinkPolicySettings(uint16_t default_link_policy_set
                              default_link_policy_settings));
}

void AclManager::SetSecurityModule(security::SecurityModule* security_module) {
  GetHandler()->Post(BindOnce(&impl::set_security_module, common::Unretained(pimpl_.get()), security_module));
}

void AclManager::ListDependencies(ModuleList* list) {
  list->add<HciLayer>();
  list->add<Controller>();
+8 −0
Original line number Diff line number Diff line
@@ -28,6 +28,11 @@
#include "os/handler.h"

namespace bluetooth {

namespace security {
class SecurityModule;
}

namespace hci {

class AclManager;
@@ -239,6 +244,9 @@ class AclManager : public Module {
  virtual void ReadDefaultLinkPolicySettings();
  virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);

  // In order to avoid circular dependency use setter rather than module dependency.
  virtual void SetSecurityModule(security::SecurityModule* security_module);

  static const ModuleFactory Factory;

 protected:
+1 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ class SecurityInterface {
                              common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) = 0;

  static constexpr hci::EventCode SecurityEvents[] = {
      hci::EventCode::ENCRYPTION_CHANGE,
      hci::EventCode::CHANGE_CONNECTION_LINK_KEY_COMPLETE,
      hci::EventCode::MASTER_LINK_KEY_COMPLETE,
      hci::EventCode::RETURN_LINK_KEYS,
+7 −0
Original line number Diff line number Diff line
@@ -545,6 +545,13 @@ TEST_F(SecurityManagerChannelTest, send_delete_stored_link_key) {
  ASSERT_EQ(OpCode::DELETE_STORED_LINK_KEY, packet_view.GetOpCode());
}

TEST_F(SecurityManagerChannelTest, recv_encryption_change) {
  uint16_t connection_handle = 0x0;
  hci_layer_->IncomingEvent(
      hci::EncryptionChangeBuilder::Create(hci::ErrorCode::SUCCESS, connection_handle, hci::EncryptionEnabled::ON));
  ASSERT_TRUE(callback_->receivedEncryptionChange);
}

TEST_F(SecurityManagerChannelTest, recv_encryption_key_refresh) {
  uint16_t connection_handle = 0x0;
  hci_layer_->IncomingEvent(
+2 −0
Original line number Diff line number Diff line
@@ -164,6 +164,8 @@ class SecurityModuleFacadeService : public SecurityModuleFacade::Service, public
    bond_events_.OnIncomingEvent(bonded);
  }

  void OnEncryptionStateChanged(hci::EncryptionChangeView encryption_change_view) override {}

  void OnDeviceUnbonded(hci::AddressWithType peer) override {
    LOG_INFO("%s", peer.ToString().c_str());
    BondMsg unbonded;
Loading