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

Commit e3cc57a8 authored by Henri Chataing's avatar Henri Chataing Committed by Thomas Girardier
Browse files

RootCanal: Misc improvements

- Remove some declared but un-implemented functions
  in DualModeController
- Move suggested_max_tx_octets and suggested_max_tx_time
  to the link layer controller
- Change the type of extended inquiry response
  to std::array<uint8_t, 240>, which accurately
  reflects the length of the response as passed
  from the HCI command

Bug: 245578454
Test: m root-canal
Ignore-AOSP-First: Cherry-picked from AOSP
Merged-In: I8837333f9c080454d987a9b7877d8c55e6d0c324
Change-Id: I8837333f9c080454d987a9b7877d8c55e6d0c324
parent 88e501ba
Loading
Loading
Loading
Loading
+23 −42
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ using gd_hci::OpCode;
using std::vector;

namespace rootcanal {
constexpr uint16_t DualModeController::kSecurityManagerNumKeys;
constexpr uint16_t kNumCommandPackets = 0x01;
constexpr uint16_t kLeMaximumAdvertisingDataLength = 256;
constexpr uint16_t kLeMaximumDataLength = 64;
@@ -55,11 +54,11 @@ void DualModeController::Close() {
}

void DualModeController::SendCommandCompleteUnknownOpCodeEvent(
    uint16_t command_opcode) const {
    uint16_t op_code) const {
  std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
      std::make_unique<bluetooth::packet::RawBuilder>();
  raw_builder_ptr->AddOctets1(kNumCommandPackets);
  raw_builder_ptr->AddOctets2(command_opcode);
  raw_builder_ptr->AddOctets2(op_code);
  raw_builder_ptr->AddOctets1(
      static_cast<uint8_t>(ErrorCode::UNKNOWN_HCI_COMMAND));

@@ -88,10 +87,7 @@ DualModeController::DualModeController(const std::string& properties_filename,
        DualModeController::SendLinkLayerPacket(packet, phy_type);
      });

  std::array<uint8_t, 64> supported_commands;
  for (size_t i = 0; i < 64; i++) {
    supported_commands[i] = 0;
  }
  std::array<uint8_t, 64> supported_commands{0};

#define SET_HANDLER(name, method)                                  \
  active_hci_commands_[OpCode::name] = [this](CommandView param) { \
@@ -566,14 +562,8 @@ void DualModeController::ReadBdAddr(CommandView command) {
void DualModeController::ReadLocalSupportedCommands(CommandView command) {
  auto command_view = gd_hci::ReadLocalSupportedCommandsView::Create(command);
  ASSERT(command_view.IsValid());

  std::array<uint8_t, 64> supported_commands{0};
  size_t len = std::min(properties_.supported_commands.size(), (size_t)64);
  std::copy_n(properties_.supported_commands.begin(), len,
              supported_commands.begin());

  send_event_(bluetooth::hci::ReadLocalSupportedCommandsCompleteBuilder::Create(
      kNumCommandPackets, ErrorCode::SUCCESS, supported_commands));
      kNumCommandPackets, ErrorCode::SUCCESS, properties_.supported_commands));
}

void DualModeController::ReadLocalSupportedFeatures(CommandView command) {
@@ -1685,25 +1675,15 @@ void DualModeController::WriteLinkSupervisionTimeout(CommandView command) {
void DualModeController::ReadLocalName(CommandView command) {
  auto command_view = gd_hci::ReadLocalNameView::Create(command);
  ASSERT(command_view.IsValid());

  std::array<uint8_t, 248> local_name{0};
  size_t len = std::min(link_layer_controller_.GetName().size(), (size_t)247);
  std::copy_n(link_layer_controller_.GetName().begin(), len,
              local_name.begin());

  send_event_(bluetooth::hci::ReadLocalNameCompleteBuilder::Create(
      kNumCommandPackets, ErrorCode::SUCCESS, local_name));
      kNumCommandPackets, ErrorCode::SUCCESS,
      link_layer_controller_.GetLocalName()));
}

void DualModeController::WriteLocalName(CommandView command) {
  auto command_view = gd_hci::WriteLocalNameView::Create(command);
  ASSERT(command_view.IsValid());
  const auto local_name = command_view.GetLocalName();
  std::vector<uint8_t> name_vec(248);
  for (size_t i = 0; i < 248; i++) {
    name_vec[i] = local_name[i];
  }
  link_layer_controller_.SetName(name_vec);
  link_layer_controller_.SetLocalName(command_view.GetLocalName());
  send_event_(bluetooth::hci::WriteLocalNameCompleteBuilder::Create(
      kNumCommandPackets, ErrorCode::SUCCESS));
}
@@ -1711,7 +1691,7 @@ void DualModeController::WriteLocalName(CommandView command) {
void DualModeController::WriteExtendedInquiryResponse(CommandView command) {
  auto command_view = gd_hci::WriteExtendedInquiryResponseView::Create(command);
  ASSERT(command_view.IsValid());
  link_layer_controller_.SetExtendedInquiryData(std::vector<uint8_t>(
  link_layer_controller_.SetExtendedInquiryResponse(std::vector<uint8_t>(
      command_view.GetPayload().begin() + 1, command_view.GetPayload().end()));
  send_event_(
      bluetooth::hci::WriteExtendedInquiryResponseCompleteBuilder::Create(
@@ -2370,7 +2350,8 @@ void DualModeController::LeReadSuggestedDefaultDataLength(CommandView command) {
  send_event_(
      bluetooth::hci::LeReadSuggestedDefaultDataLengthCompleteBuilder::Create(
          kNumCommandPackets, ErrorCode::SUCCESS,
          le_suggested_default_data_bytes_, le_suggested_default_data_time_));
          link_layer_controller_.GetLeSuggestedMaxTxOctets(),
          link_layer_controller_.GetLeSuggestedMaxTxTime()));
}

void DualModeController::LeWriteSuggestedDefaultDataLength(
@@ -2379,20 +2360,21 @@ void DualModeController::LeWriteSuggestedDefaultDataLength(
      gd_hci::LeConnectionManagementCommandView::Create(
          gd_hci::AclCommandView::Create(command)));
  ASSERT(command_view.IsValid());
  uint16_t bytes = command_view.GetTxOctets();
  uint16_t time = command_view.GetTxTime();
  if (bytes > 0xFB || bytes < 0x1B || time < 0x148 || time > 0x4290) {
    send_event_(
        bluetooth::hci::LeWriteSuggestedDefaultDataLengthCompleteBuilder::
            Create(kNumCommandPackets,
                   ErrorCode::INVALID_HCI_COMMAND_PARAMETERS));
    return;

  uint16_t max_tx_octets = command_view.GetTxOctets();
  uint16_t max_tx_time = command_view.GetTxTime();
  ErrorCode status = ErrorCode::SUCCESS;
  if (max_tx_octets > 0xFB || max_tx_octets < 0x1B || max_tx_time < 0x148 ||
      max_tx_time > 0x4290) {
    status = ErrorCode::INVALID_HCI_COMMAND_PARAMETERS;
  } else {
    link_layer_controller_.SetLeSuggestedMaxTxOctets(max_tx_octets);
    link_layer_controller_.SetLeSuggestedMaxTxTime(max_tx_time);
  }
  le_suggested_default_data_bytes_ = bytes;
  le_suggested_default_data_time_ = time;

  send_event_(
      bluetooth::hci::LeWriteSuggestedDefaultDataLengthCompleteBuilder::Create(
          kNumCommandPackets, ErrorCode::SUCCESS));
          kNumCommandPackets, status));
}

void DualModeController::LeAddDeviceToResolvingList(CommandView command) {
@@ -2971,8 +2953,7 @@ void DualModeController::ReadLoopbackMode(CommandView command) {
  auto command_view = gd_hci::ReadLoopbackModeView::Create(command);
  ASSERT(command_view.IsValid());
  send_event_(bluetooth::hci::ReadLoopbackModeCompleteBuilder::Create(
      kNumCommandPackets, ErrorCode::SUCCESS,
      static_cast<LoopbackMode>(loopback_mode_)));
      kNumCommandPackets, ErrorCode::SUCCESS, loopback_mode_));
}

void DualModeController::WriteLoopbackMode(CommandView command) {
+11 −17
Original line number Diff line number Diff line
@@ -67,7 +67,6 @@ class DualModeController : public Device {
      model::packets::LinkLayerPacketView incoming) override;

  virtual void TimerTick() override;

  virtual void Close() override;

  // Route commands and data from the stack.
@@ -626,17 +625,9 @@ class DualModeController : public Device {
  LinkLayerController link_layer_controller_{address_, properties_};

 private:
  // Set a timer for a future action
  void AddControllerEvent(std::chrono::milliseconds,
                          const TaskCallback& callback);

  void AddConnectionAction(const TaskCallback& callback, uint16_t handle);

  void SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const;

  // Unused state to maintain consistency for the Host
  uint16_t le_suggested_default_data_bytes_{0x20};
  uint16_t le_suggested_default_data_time_{0x148};
  // Send a HCI_Command_Complete event for the specified op_code with
  // the error code UNKNOWN_OPCODE.
  void SendCommandCompleteUnknownOpCodeEvent(uint16_t op_code) const;

  // Callbacks to send packets back to the HCI.
  std::function<void(std::shared_ptr<bluetooth::hci::AclBuilder>)> send_acl_;
@@ -645,21 +636,24 @@ class DualModeController : public Device {
  std::function<void(std::shared_ptr<bluetooth::hci::ScoBuilder>)> send_sco_;
  std::function<void(std::shared_ptr<bluetooth::hci::IsoBuilder>)> send_iso_;

  // Maintains the commands to be registered and used in the HciHandler object.
  // Keys are command opcodes and values are the callbacks to handle each
  // command.
  // Map supported opcodes to the function implementing the handler
  // for the associated command. The map should be a subset of the
  // supported_command field in the properties_ object.
  std::unordered_map<bluetooth::hci::OpCode,
                     std::function<void(bluetooth::hci::CommandView)>>
      active_hci_commands_;

  // Loopback mode (Vol 4, Part E § 7.6.1).
  // The local loopback mode is used to pass the android Vendor Test Suite
  // with RootCanal.
  bluetooth::hci::LoopbackMode loopback_mode_;

#ifndef ROOTCANAL_LMP
  SecurityManager security_manager_;
#endif /* ROOTCANAL_LMP */

  DualModeController(const DualModeController& cmdPckt) = delete;
  DualModeController& operator=(const DualModeController& cmdPckt) = delete;
  DualModeController(const DualModeController& other) = delete;
  DualModeController& operator=(const DualModeController& other) = delete;
};

}  // namespace rootcanal
+21 −17
Original line number Diff line number Diff line
@@ -1314,11 +1314,23 @@ void LinkLayerController::SetSecureConnectionsSupport(bool enable) {
  }
}

void LinkLayerController::SetName(std::vector<uint8_t> const& name) {
  name_.fill(0);
  for (size_t i = 0; i < 248 && i < name.size(); i++) {
    name_[i] = name[i];
void LinkLayerController::SetLocalName(
    std::array<uint8_t, 248> const& local_name) {
  std::copy(local_name.begin(), local_name.end(), local_name_.begin());
}

void LinkLayerController::SetLocalName(std::vector<uint8_t> const& local_name) {
  ASSERT(local_name.size() <= local_name_.size());
  local_name_.fill(0);
  std::copy(local_name.begin(), local_name.end(), local_name_.begin());
}

void LinkLayerController::SetExtendedInquiryResponse(
    std::vector<uint8_t> const& extended_inquiry_response) {
  ASSERT(extended_inquiry_response.size() <= extended_inquiry_response_.size());
  extended_inquiry_response_.fill(0);
  std::copy(extended_inquiry_response.begin(), extended_inquiry_response.end(),
            extended_inquiry_response_.begin());
}

void LinkLayerController::SendLeLinkLayerPacketWithRssi(
@@ -1879,7 +1891,7 @@ void LinkLayerController::IncomingRemoteNameRequest(
  ASSERT(view.IsValid());

  SendLinkLayerPacket(model::packets::RemoteNameRequestResponseBuilder::Create(
      packet.GetDestinationAddress(), packet.GetSourceAddress(), name_));
      packet.GetDestinationAddress(), packet.GetSourceAddress(), local_name_));
}

void LinkLayerController::IncomingRemoteNameRequestResponse(
@@ -2138,7 +2150,7 @@ void LinkLayerController::IncomingInquiryPacket(
              GetAddress(), peer,
              static_cast<uint8_t>(GetPageScanRepetitionMode()),
              class_of_device_, GetClockOffset(), rssi,
              extended_inquiry_data_));
              extended_inquiry_response_));

    } break;
    default:
@@ -2213,7 +2225,8 @@ void LinkLayerController::IncomingInquiryResponsePacket(
              inquiry_response.GetPageScanRepetitionMode()),
          inquiry_response.GetClassOfDevice(),
          inquiry_response.GetClockOffset(), inquiry_response.GetRssi(),
          inquiry_response.GetExtendedData()));
          std::vector<uint8_t>(extended_inquiry_response_.begin(),
                               extended_inquiry_response_.end())));
    } break;
    default:
      LOG_WARN("Unhandled Incoming Inquiry Response of type %d",
@@ -3169,13 +3182,6 @@ void LinkLayerController::ScanIncomingLeExtendedAdvertisingPdu(
      break;
  }

  // When the Scanning_Filter_Policy is set to 0x02 or 0x03 (see Section 7.8.10)
  // and a directed advertisement was received where the advertiser used a
  // resolvable private address which the Controller is unable to resolve, an
  // HCI_LE_Directed_Advertising_Report event shall be generated instead of an
  // HCI_LE_Advertising_Report event.
  bool should_send_directed_advertising_report = false;

  if (directed_advertising) {
    switch (scanner_.scan_filter_policy) {
      // In both basic scanner filter policy modes, a directed advertising PDU
@@ -3213,8 +3219,6 @@ void LinkLayerController::ScanIncomingLeExtendedAdvertisingPdu(
              target_address.GetAddressType());
          return;
        }
        should_send_directed_advertising_report =
            target_address.IsRpa() && !resolved_target_address;
        break;
    }
  }
+28 −11
Original line number Diff line number Diff line
@@ -661,10 +661,12 @@ class LinkLayerController {
  uint8_t GetEncryptionKeySize() const { return min_encryption_key_size_; }

  bool GetScoFlowControlEnable() const { return sco_flow_control_enable_; }

  AuthenticationEnable GetAuthenticationEnable() {
    return authentication_enable_;
  }
  std::array<uint8_t, 248> const& GetName() { return name_; }

  std::array<uint8_t, 248> const& GetLocalName() { return local_name_; }

  uint64_t GetLeSupportedFeatures() const {
    return properties_.le_features | le_host_supported_features_;
@@ -686,6 +688,11 @@ class LinkLayerController {
                            : properties_.lmp_features[page_number];
  }

  void SetLocalName(std::vector<uint8_t> const& local_name);
  void SetLocalName(std::array<uint8_t, 248> const& local_name);
  void SetExtendedInquiryResponse(
      std::vector<uint8_t> const& extended_inquiry_response);

  void SetClassOfDevice(ClassOfDevice class_of_device) {
    class_of_device_ = class_of_device;
  }
@@ -696,11 +703,6 @@ class LinkLayerController {
    class_of_device_.cod[2] = (class_of_device >> 16) & 0xff;
  }

  void SetExtendedInquiryData(
      std::vector<uint8_t> const& extended_inquiry_data) {
    extended_inquiry_data_ = extended_inquiry_data;
  }

  void SetAuthenticationEnable(AuthenticationEnable enable) {
    authentication_enable_ = enable;
  }
@@ -716,8 +718,6 @@ class LinkLayerController {
    le_event_mask_ = le_event_mask;
  }

  void SetName(std::vector<uint8_t> const& name);

  void SetLeHostSupport(bool enable);
  void SetSecureSimplePairingSupport(bool enable);
  void SetSecureConnectionsSupport(bool enable);
@@ -747,6 +747,18 @@ class LinkLayerController {
    }
  }

  uint16_t GetLeSuggestedMaxTxOctets() const {
    return le_suggested_max_tx_octets_;
  }
  uint16_t GetLeSuggestedMaxTxTime() const { return le_suggested_max_tx_time_; }

  void SetLeSuggestedMaxTxOctets(uint16_t max_tx_octets) {
    le_suggested_max_tx_octets_ = max_tx_octets;
  }
  void SetLeSuggestedMaxTxTime(uint16_t max_tx_time) {
    le_suggested_max_tx_time_ = max_tx_time;
  }

 private:
  const Address& address_;
  const ControllerProperties& properties_;
@@ -805,7 +817,10 @@ class LinkLayerController {
  bool sco_flow_control_enable_{false};

  // Local Name (Vol 4, Part E § 6.23).
  std::array<uint8_t, 248> name_;
  std::array<uint8_t, 248> local_name_{};

  // Extended Inquiry Response (Vol 4, Part E § 6.24).
  std::array<uint8_t, 240> extended_inquiry_response_{};

  // Class of Device (Vol 4, Part E § 6.26).
  ClassOfDevice class_of_device_{{0, 0, 0}};
@@ -823,12 +838,14 @@ class LinkLayerController {
  uint64_t event_mask_{0x00001fffffffffff};
  uint64_t le_event_mask_{0x01f};

  // Suggested Default Data Length (Vol 4, Part E § 7.8.34).
  uint16_t le_suggested_max_tx_octets_{0x001b};
  uint16_t le_suggested_max_tx_time_{0x0148};

  // Page Scan Repetition Mode (Vol 2 Part B § 8.3.1 Page Scan substate).
  // The Page Scan Repetition Mode depends on the selected Page Scan Interval.
  PageScanRepetitionMode page_scan_repetition_mode_{PageScanRepetitionMode::R0};

  std::vector<uint8_t> extended_inquiry_data_;

  AclConnectionHandler connections_;

  // Callbacks to schedule tasks.
+12 −14
Original line number Diff line number Diff line
@@ -23,10 +23,7 @@ namespace rootcanal {
HciDevice::HciDevice(std::shared_ptr<HciTransport> transport,
                     const std::string& properties_filename)
    : DualModeController(properties_filename), transport_(transport) {
  link_layer_controller_.SetClassOfDevice(0x600420);
  link_layer_controller_.SetExtendedInquiryData({
      12,  // length
      9,   // Type: Device Name
  link_layer_controller_.SetLocalName(std::vector<uint8_t>({
      'g',
      'D',
      'e',
@@ -35,12 +32,13 @@ HciDevice::HciDevice(std::shared_ptr<HciTransport> transport,
      'c',
      'e',
      '-',
      'h',
      'c',
      'i',

  });
  link_layer_controller_.SetName({
      'H',
      'C',
      'I',
  }));
  link_layer_controller_.SetExtendedInquiryResponse(std::vector<uint8_t>({
      12,  // Length
      9,   // Type: Device Name
      'g',
      'D',
      'e',
@@ -49,10 +47,10 @@ HciDevice::HciDevice(std::shared_ptr<HciTransport> transport,
      'c',
      'e',
      '-',
      'H',
      'C',
      'I',
  });
      'h',
      'c',
      'i',
  }));

  RegisterEventChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
    transport_->SendEvent(*packet);
Loading