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

Commit b698e4e1 authored by Chienyuan Huang's avatar Chienyuan Huang Committed by Gerrit Code Review
Browse files

Merge "CS: Read local capability when module start" into main

parents e5582bb7 fb957d5e
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 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/command_interface.h"
#include "hci/hci_packets.h"

namespace bluetooth {
namespace hci {

constexpr hci::SubeventCode DistanceMeasurementEvents[] = {
    hci::SubeventCode::LE_CS_TEST_END_COMPLETE,
    hci::SubeventCode::LE_CS_SUBEVENT_RESULT_CONTINUE,
    hci::SubeventCode::LE_CS_SUBEVENT_RESULT,
    hci::SubeventCode::LE_CS_PROCEDURE_ENABLE_COMPLETE,
    hci::SubeventCode::LE_CS_CONFIG_COMPLETE,
    hci::SubeventCode::LE_CS_SECURITY_ENABLE_COMPLETE,
    hci::SubeventCode::LE_CS_READ_REMOTE_FAE_TABLE_COMPLETE,
    hci::SubeventCode::LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES_COMPLETE,
};

typedef CommandInterface<DistanceMeasurementCommandBuilder> DistanceMeasurementInterface;

}  // namespace hci
}  // namespace bluetooth
+44 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <unordered_map>

#include "hci/acl_manager.h"
#include "hci/distance_measurement_interface.h"
#include "hci/hci_layer.h"
#include "module.h"
#include "os/handler.h"
@@ -43,6 +44,11 @@ struct DistanceMeasurementManager::impl {
    hci_layer_->RegisterLeEventHandler(
        hci::SubeventCode::TRANSMIT_POWER_REPORTING,
        handler_->BindOn(this, &impl::on_transmit_power_reporting));
    distance_measurement_interface_ = hci_layer_->GetDistanceMeasurementInterface(
        handler_->BindOn(this, &DistanceMeasurementManager::impl::handle_event));
    distance_measurement_interface_->EnqueueCommand(
        LeCsReadLocalSupportedCapabilitiesBuilder::Create(),
        handler_->BindOnceOn(this, &impl::on_cs_read_local_supported_capabilities));
  }

  void stop() {
@@ -135,6 +141,41 @@ struct DistanceMeasurementManager::impl {
        std::chrono::milliseconds(rssi_trackers[address].frequency));
  }

  void handle_event(LeMetaEventView event) {
    switch (event.GetSubeventCode()) {
      case hci::SubeventCode::LE_CS_TEST_END_COMPLETE:
      case hci::SubeventCode::LE_CS_SUBEVENT_RESULT_CONTINUE:
      case hci::SubeventCode::LE_CS_SUBEVENT_RESULT:
      case hci::SubeventCode::LE_CS_PROCEDURE_ENABLE_COMPLETE:
      case hci::SubeventCode::LE_CS_CONFIG_COMPLETE:
      case hci::SubeventCode::LE_CS_SECURITY_ENABLE_COMPLETE:
      case hci::SubeventCode::LE_CS_READ_REMOTE_FAE_TABLE_COMPLETE:
      case hci::SubeventCode::LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES_COMPLETE: {
        LOG_WARN("Unhandled subevent %s", hci::SubeventCodeText(event.GetSubeventCode()).c_str());
      } break;
      default:
        LOG_INFO("Unknown subevent %s", hci::SubeventCodeText(event.GetSubeventCode()).c_str());
    }
  }

  void on_cs_read_local_supported_capabilities(CommandCompleteView view) {
    auto complete_view = LeCsReadLocalSupportedCapabilitiesCompleteView::Create(view);
    if (!complete_view.IsValid()) {
      LOG_WARN("Invalid LeCsReadLocalSupportedCapabilitiesComplete event");
      is_channel_sounding_supported_ = false;
      return;
    } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(complete_view.GetStatus());
      LOG_WARN(
          "Received LeCsReadLocalSupportedCapabilitiesComplete with error code %s",
          error_code.c_str());
      is_channel_sounding_supported_ = false;
      return;
    }
    is_channel_sounding_supported_ = true;
    cs_subfeature_supported_ = complete_view.GetOptionalSubfeaturesSupported();
  }

  void on_read_remote_transmit_power_level_status(Address address, CommandStatusView view) {
    auto status_view = LeReadRemoteTransmitPowerLevelStatusView::Create(view);
    if (!status_view.IsValid()) {
@@ -282,8 +323,11 @@ struct DistanceMeasurementManager::impl {
  os::Handler* handler_;
  hci::HciLayer* hci_layer_;
  hci::AclManager* acl_manager_;
  bool is_channel_sounding_supported_ = false;
  hci::DistanceMeasurementInterface* distance_measurement_interface_;
  std::unordered_map<Address, RSSITracker> rssi_trackers;
  DistanceMeasurementCallbacks* distance_measurement_callbacks_;
  CsOptionalSubfeaturesSupported cs_subfeature_supported_;
};

DistanceMeasurementManager::DistanceMeasurementManager() {
+5 −0
Original line number Diff line number Diff line
@@ -66,6 +66,11 @@ hci::LeIsoInterface* FuzzHciLayer::GetLeIsoInterface(ContextualCallback<void(hci
  return &le_iso_interface_;
}

hci::DistanceMeasurementInterface* FuzzHciLayer::GetDistanceMeasurementInterface(
    ContextualCallback<void(hci::LeMetaEventView)> event_handler) {
  return &distance_measurement_interface_;
}

void FuzzHciLayer::Start() {
  acl_dev_null_ = new os::fuzz::DevNullQueue<AclBuilder>(acl_queue_.GetDownEnd(), GetHandler());
  acl_dev_null_->Start();
+4 −0
Original line number Diff line number Diff line
@@ -129,6 +129,9 @@ class FuzzHciLayer : public HciLayer {

  hci::LeIsoInterface* GetLeIsoInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler) override;

  hci::DistanceMeasurementInterface* GetDistanceMeasurementInterface(
      common::ContextualCallback<void(hci::LeMetaEventView)> event_handler) override;

  void injectArbitrary(FuzzedDataProvider& fdp);

  std::string ToString() const override {
@@ -179,6 +182,7 @@ class FuzzHciLayer : public HciLayer {
  FuzzCommandInterface<LeAdvertisingCommandBuilder> le_advertising_interface_{};
  FuzzCommandInterface<LeScanningCommandBuilder> le_scanning_interface_{};
  FuzzCommandInterface<LeIsoCommandBuilder> le_iso_interface_{};
  FuzzCommandInterface<DistanceMeasurementCommandBuilder> distance_measurement_interface_{};

  common::ContextualOnceCallback<void(hci::CommandCompleteView)> on_command_complete_;
  common::ContextualOnceCallback<void(hci::CommandStatusView)> on_command_status_;
+2 −0
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ void HciLayerFuzzClient::Start() {
      GetHandler()->Bind([](hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t) {}));
  le_advertising_interface_ = hci_->GetLeAdvertisingInterface(GetHandler()->Bind([](LeMetaEventView) {}));
  le_scanning_interface_ = hci_->GetLeScanningInterface(GetHandler()->Bind([](LeMetaEventView) {}));
  distance_measurement_interface_ =
      hci_->GetDistanceMeasurementInterface(GetHandler()->Bind([](LeMetaEventView) {}));
}

void HciLayerFuzzClient::Stop() {
Loading