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

Commit d0a1671f authored by Chienyuan's avatar Chienyuan Committed by Myles Watson
Browse files

GD HCI: register request event for security interface

* register request event for security interface
* waiting for command status for REFRESH_ENCRYPTION_KEY command

Bug: 137138879
Test: run run_device_cert.sh
Change-Id: Ie22181a3259caafc5f28eaaccba3facfa15fa6b0
parent de10c9c2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "common/blocking_queue.h"
#include "grpc/grpc_event_stream.h"
#include "hci/cert/api.grpc.pb.h"
#include "hci/classic_security_manager.h"
#include "hci/controller.h"
#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
@@ -321,6 +322,7 @@ void AclManagerCertModule::ListDependencies(ModuleList* list) {
  ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
  list->add<Controller>();
  list->add<HciLayer>();
  list->add<ClassicSecurityManager>();
}

void AclManagerCertModule::Start() {
+19 −8
Original line number Diff line number Diff line
@@ -39,10 +39,15 @@ struct ClassicSecurityManager::impl {
    hci_layer_ = classic_security_manager_.GetDependency<HciLayer>();
    handler_ = classic_security_manager_.GetHandler();
    hci_layer_->RegisterEventHandler(EventCode::IO_CAPABILITY_REQUEST,
                                     Bind(&impl::on_io_capbility_request, common::Unretained(this)), handler_);

                                     Bind(&impl::on_request_event, common::Unretained(this)), handler_);
    hci_layer_->RegisterEventHandler(EventCode::AUTHENTICATION_COMPLETE,
                                     Bind(&impl::on_authentication_complete, common::Unretained(this)), handler_);
    hci_layer_->RegisterEventHandler(EventCode::LINK_KEY_REQUEST,
                                     Bind(&impl::on_request_event, common::Unretained(this)), handler_);
    hci_layer_->RegisterEventHandler(EventCode::PIN_CODE_REQUEST,
                                     Bind(&impl::on_request_event, common::Unretained(this)), handler_);
    hci_layer_->RegisterEventHandler(EventCode::ENCRYPTION_KEY_REFRESH_COMPLETE,
                                     Bind(&impl::on_complete_event, common::Unretained(this)), handler_);
  }

  void Stop() {
@@ -174,9 +179,8 @@ struct ClassicSecurityManager::impl {

  void refresh_encryption_key(uint16_t connection_handle) {
    std::unique_ptr<RefreshEncryptionKeyBuilder> packet = RefreshEncryptionKeyBuilder::Create(connection_handle);
    // TODO wait for CommandStatusView
    hci_layer_->EnqueueCommand(std::move(packet),
                               common::BindOnce(&impl::on_command_complete, common::Unretained(this)), handler_);
    hci_layer_->EnqueueCommand(std::move(packet), common::BindOnce([](CommandStatusView status) { /* TODO: check? */ }),
                               handler_);
  }

  void read_simple_pairing_mode() {
@@ -225,13 +229,20 @@ struct ClassicSecurityManager::impl {
  }

  // TODO remove
  void on_io_capbility_request(EventPacketView packet) {
    LOG_DEBUG("CYDBG on_io_capbility_request");
  void on_request_event(EventPacketView packet) {
    EventCode event_code = packet.GetEventCode();
    LOG_DEBUG("receive request %d", (uint8_t)event_code);
  }

  // TODO remove
  void on_complete_event(EventPacketView packet) {
    EventCode event_code = packet.GetEventCode();
    LOG_DEBUG("receive complete event %d", (uint8_t)event_code);
  }

  // TODO remove
  void on_authentication_complete(EventPacketView packet) {
    LOG_DEBUG("CYDBG on_authentication_complete");
    LOG_DEBUG("on_authentication_complete");
  }

  void on_command_complete(CommandCompleteView status) {
+19 −1
Original line number Diff line number Diff line
@@ -122,6 +122,9 @@ class ClassicSecurityManagerTest : public ::testing::Test, public ::bluetooth::h
    test_hci_layer_->RegisterEventHandler(
        EventCode::COMMAND_COMPLETE, base::Bind(&ClassicSecurityManagerTest::ExpectCommand, common::Unretained(this)),
        nullptr);
    test_hci_layer_->RegisterEventHandler(
        EventCode::COMMAND_STATUS,
        base::Bind(&ClassicSecurityManagerTest::ExpectCommandStatus, common::Unretained(this)), nullptr);

    Address::FromString("A1:A2:A3:A4:A5:A6", remote);
  }
@@ -155,6 +158,21 @@ class ClassicSecurityManagerTest : public ::testing::Test, public ::bluetooth::h
    command_complete_ = true;
  }

  void ExpectCommandStatus(EventPacketView packet) {
    CommandStatusView command_status_view = CommandStatusView::Create(std::move(packet));
    auto last_command_queue_entry = test_hci_layer_->GetLastCommand();
    auto last_command = std::move(last_command_queue_entry->command);
    auto command_packet = GetPacketView(std::move(last_command));
    CommandPacketView command_packet_view = CommandPacketView::Create(command_packet);

    // verify command complete event match last command opcode
    EXPECT_TRUE(command_packet_view.IsValid());
    EXPECT_TRUE(command_status_view.IsValid());
    EXPECT_EQ(command_packet_view.GetOpCode(), command_status_view.GetCommandOpCode());

    command_complete_ = true;
  }

  void OnCommandComplete(CommandCompleteView status) override {
    callback_done.notify_one();
  }
@@ -332,7 +350,7 @@ TEST_F(ClassicSecurityManagerTest, send_refresh_encryption_key) {

  auto payload = std::make_unique<RawBuilder>();
  test_hci_layer_->IncomingEvent(
      CommandCompleteBuilder::Create(0x01, OpCode::REFRESH_ENCRYPTION_KEY, std::move(payload)));
      CommandStatusBuilder::Create(ErrorCode::SUCCESS, 0x01, OpCode::REFRESH_ENCRYPTION_KEY, std::move(payload)));
  EXPECT_TRUE(command_complete_);
}