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

Commit bea491b4 authored by Joseph Hwang's avatar Joseph Hwang Committed by Automerger Merge Worker
Browse files

Merge changes from topic "msft_cmds" am: c293a324

parents 0c51854f c293a324
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -214,6 +214,10 @@ cc_defaults {
        "libbt_shim_bridge",
        "libbt_shim_ffi",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/system/include",
        "packages/modules/Bluetooth/system/types",
    ],
    export_static_lib_headers: [ "libbluetooth_rust_interop" ],
}

+10 −0
Original line number Diff line number Diff line
@@ -6375,6 +6375,14 @@ test MsftLeMonitorAdvConditionUuid16 {
  "\x70\xfd\x17\x03\x10\x11\x12\x13\x02\x03\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f",
}

packet MsftLeCancelMonitorAdv: MsftCommand (subcommand_opcode = MSFT_LE_CANCEL_MONITOR_ADV) {
  monitor_handle: 8,
}

test MsftLeCancelMonitorAdv {
  "\x1e\xfc\x02\x04\x01", // cancel handle 0x01
}

packet MsftLeSetAdvFilterEnable : MsftCommand (subcommand_opcode = MSFT_LE_SET_ADV_FILTER_ENABLE) {
  enable: 8,
}
@@ -6410,6 +6418,8 @@ test MsftLeMonitorAdvCommandComplete {
  "\x0e\x06\x01\x70\xfd\x01\x03\x06", // failed
}

packet MsftLeCancelMonitorAdvCommandComplete : MsftCommandComplete (subcommand_opcode = MSFT_LE_CANCEL_MONITOR_ADV) {}

packet MsftLeSetAdvFilterEnableCommandComplete : MsftCommandComplete (subcommand_opcode = MSFT_LE_SET_ADV_FILTER_ENABLE) {}

enum MsftEventCode : 8 {
+154 −12
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
#include "hci/msft.h"

#include <hardware/bt_common_types.h>

#include "hal/hci_hal.h"
#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
@@ -76,22 +78,41 @@ struct MsftExtensionManager::impl {
    LOG_INFO("MsftExtensionManager stop()");
  }

  void handle_msft_events(VendorSpecificEventView event) {
    // TODO(b/246398494): MSFT events are vendor specific events with an event prefix.
  void handle_rssi_event(MsftRssiEventPayloadView view) {
    LOG_WARN("The Microsoft MSFT_RSSI_EVENT is not supported yet.");
  }

  void handle_le_monitor_device_event(MsftLeMonitorDeviceEventPayloadView view) {
    ASSERT(view.IsValid());
    LOG_WARN("The Microsoft MSFT_LE_MONITOR_DEVICE_EVENT is not supported yet.");
    // TODO: to call a callback here.
  }

    /* Myles suggested that the structure look like
    auto payload = event.GetPayload();
    for (size_t i = 0; i < msft_.prefix.size(); i++) {
      if (msft_.prefix[i] != payload[i]) {
        // Print a warning and return
  void handle_msft_events(VendorSpecificEventView view) {
    auto payload = view.GetPayload();
    for (size_t i = 0; i < msft_.prefix.size() - 1; i++) {
      if (msft_.prefix[i + 1] != payload[i]) {
        LOG_WARN("The Microsoft vendor event prefix does not match.");
        return;
      }
    }

    MsftEventPayloadView::Create(payload.GetLittleEndianSubview(msft_.prefix.size(), payload.size()));
    // Assert that it's valid
    // Check the type of event
    // Cast it, and handle it.
    */
    auto msft_view = MsftEventPayloadView::Create(
        payload.GetLittleEndianSubview(msft_.prefix.size() - 1, payload.size()));
    ASSERT(msft_view.IsValid());

    MsftEventCode ev_code = msft_view.GetMsftEventCode();
    switch (ev_code) {
      case MsftEventCode::MSFT_RSSI_EVENT:
        handle_rssi_event(MsftRssiEventPayloadView::Create(msft_view));
        break;
      case MsftEventCode::MSFT_LE_MONITOR_DEVICE_EVENT:
        handle_le_monitor_device_event(MsftLeMonitorDeviceEventPayloadView::Create(msft_view));
        break;
      default:
        LOG_WARN("Unknown MSFT event code %hhu", ev_code);
        break;
    }
  }

  bool supports_msft_extensions() {
@@ -105,6 +126,51 @@ struct MsftExtensionManager::impl {
    return true;
  }

  void msft_adv_monitor_add(const MsftAdvMonitor& monitor, MsftAdvMonitorAddCallback cb) {
    std::vector<MsftLeMonitorAdvConditionPattern> patterns;
    MsftLeMonitorAdvConditionPattern pattern;
    // The Microsoft Extension specifies 1 octet for the number of patterns.
    // However, the max number of patters should not exceed 61.
    // (255 - 1 (packet type) - 2 (OGF/OCF) - 1 (length) - 7 (MSFT command parameters)) /
    // 4 (min size of a pattern) = 61
    if (monitor.patterns.size() > 61) {
      LOG_ERROR("Number of MSFT patterns %zu is too large", monitor.patterns.size());
      return;
    }
    for (auto& p : monitor.patterns) {
      pattern.ad_type_ = p.ad_type;
      pattern.start_of_pattern_ = p.start_byte;
      pattern.pattern_ = p.pattern;
      patterns.push_back(pattern);
    }

    msft_adv_monitor_add_cb_ = cb;
    hci_layer_->EnqueueCommand(
        MsftLeMonitorAdvConditionPatternsBuilder::Create(
            static_cast<OpCode>(msft_.opcode.value()),
            monitor.rssi_threshold_high,
            monitor.rssi_threshold_low,
            monitor.rssi_threshold_low_time_interval,
            monitor.rssi_sampling_period,
            patterns),
        module_handler_->BindOnceOn(this, &impl::on_msft_adv_monitor_add_complete));
  }

  void msft_adv_monitor_remove(uint8_t monitor_handle, MsftAdvMonitorRemoveCallback cb) {
    msft_adv_monitor_remove_cb_ = cb;
    hci_layer_->EnqueueCommand(
        MsftLeCancelMonitorAdvBuilder::Create(
            static_cast<OpCode>(msft_.opcode.value()), monitor_handle),
        module_handler_->BindOnceOn(this, &impl::on_msft_adv_monitor_remove_complete));
  }

  void msft_adv_monitor_enable(bool enable, MsftAdvMonitorEnableCallback cb) {
    msft_adv_monitor_enable_cb_ = cb;
    hci_layer_->EnqueueCommand(
        MsftLeSetAdvFilterEnableBuilder::Create(static_cast<OpCode>(msft_.opcode.value()), enable),
        module_handler_->BindOnceOn(this, &impl::on_msft_adv_monitor_enable_complete));
  }

  /*
   * Get the event prefix from the packet for configuring MSFT's
   * Vendor Specific events. Also get the MSFT supported features.
@@ -136,6 +202,61 @@ struct MsftExtensionManager::impl {

    LOG_INFO(
        "MSFT features 0x%16.16llx prefix length %u", (unsigned long long)msft_.features, (unsigned int)prefix.size());

    // We are here because Microsoft Extension is supported. Hence, register the
    // first octet of the vendor prefix so that the vendor specific event manager
    // can dispatch the event correctly.
    // Note: registration of the first octet of the vendor prefix is sufficient
    //       because each vendor controller should ensure that the first octet
    //       is unique within the vendor's events.
    vendor_specific_event_manager_->RegisterEventHandler(
        static_cast<VseSubeventCode>(msft_.prefix[0]),
        module_handler_->BindOn(this, &impl::handle_msft_events));
  }

  void on_msft_adv_monitor_add_complete(CommandCompleteView view) {
    ASSERT(view.IsValid());
    auto status_view =
        MsftLeMonitorAdvCommandCompleteView::Create(MsftCommandCompleteView::Create(view));
    ASSERT(status_view.IsValid());

    MsftSubcommandOpcode sub_opcode = status_view.GetSubcommandOpcode();
    if (sub_opcode != MsftSubcommandOpcode::MSFT_LE_MONITOR_ADV) {
      LOG_WARN("Wrong MSFT subcommand opcode %hhu returned", sub_opcode);
      return;
    }

    msft_adv_monitor_add_cb_.Run(status_view.GetMonitorHandle(), status_view.GetStatus());
  }

  void on_msft_adv_monitor_remove_complete(CommandCompleteView view) {
    ASSERT(view.IsValid());
    auto status_view =
        MsftLeCancelMonitorAdvCommandCompleteView::Create(MsftCommandCompleteView::Create(view));
    ASSERT(status_view.IsValid());

    MsftSubcommandOpcode sub_opcode = status_view.GetSubcommandOpcode();
    if (sub_opcode != MsftSubcommandOpcode::MSFT_LE_CANCEL_MONITOR_ADV) {
      LOG_WARN("Wrong MSFT subcommand opcode %hhu returned", sub_opcode);
      return;
    }

    msft_adv_monitor_remove_cb_.Run(status_view.GetStatus());
  }

  void on_msft_adv_monitor_enable_complete(CommandCompleteView view) {
    ASSERT(view.IsValid());
    auto status_view =
        MsftLeSetAdvFilterEnableCommandCompleteView::Create(MsftCommandCompleteView::Create(view));
    ASSERT(status_view.IsValid());

    MsftSubcommandOpcode sub_opcode = status_view.GetSubcommandOpcode();
    if (sub_opcode != MsftSubcommandOpcode::MSFT_LE_SET_ADV_FILTER_ENABLE) {
      LOG_WARN("Wrong MSFT subcommand opcode %hhu returned", sub_opcode);
      return;
    }

    msft_adv_monitor_enable_cb_.Run(status_view.GetStatus());
  }

  Module* module_;
@@ -144,6 +265,9 @@ struct MsftExtensionManager::impl {
  hci::HciLayer* hci_layer_;
  hci::VendorSpecificEventManager* vendor_specific_event_manager_;
  Msft msft_;
  MsftAdvMonitorAddCallback msft_adv_monitor_add_cb_;
  MsftAdvMonitorRemoveCallback msft_adv_monitor_remove_cb_;
  MsftAdvMonitorEnableCallback msft_adv_monitor_enable_cb_;
};

MsftExtensionManager::MsftExtensionManager() {
@@ -173,5 +297,23 @@ std::string MsftExtensionManager::ToString() const {
  return "Microsoft Extension Manager";
}

bool MsftExtensionManager::SupportsMsftExtensions() {
  return pimpl_->supports_msft_extensions();
}

void MsftExtensionManager::MsftAdvMonitorAdd(
    const MsftAdvMonitor& monitor, MsftAdvMonitorAddCallback cb) {
  CallOn(pimpl_.get(), &impl::msft_adv_monitor_add, monitor, cb);
}

void MsftExtensionManager::MsftAdvMonitorRemove(
    uint8_t monitor_handle, MsftAdvMonitorRemoveCallback cb) {
  CallOn(pimpl_.get(), &impl::msft_adv_monitor_remove, monitor_handle, cb);
}

void MsftExtensionManager::MsftAdvMonitorEnable(bool enable, MsftAdvMonitorEnableCallback cb) {
  CallOn(pimpl_.get(), &impl::msft_adv_monitor_enable, enable, cb);
}

}  // namespace hci
}  // namespace bluetooth
+12 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#include "hci/hci_packets.h"
#include "module.h"

struct MsftAdvMonitor;

namespace bluetooth {
namespace hci {

@@ -28,6 +30,16 @@ class MsftExtensionManager : public bluetooth::Module {
  MsftExtensionManager(const MsftExtensionManager&) = delete;
  MsftExtensionManager& operator=(const MsftExtensionManager&) = delete;

  using MsftAdvMonitorAddCallback =
      base::Callback<void(uint8_t /* monitor_handle */, ErrorCode /* status */)>;
  using MsftAdvMonitorRemoveCallback = base::Callback<void(ErrorCode /* status */)>;
  using MsftAdvMonitorEnableCallback = base::Callback<void(ErrorCode /* status */)>;

  virtual bool SupportsMsftExtensions();
  void MsftAdvMonitorAdd(const MsftAdvMonitor& monitor, MsftAdvMonitorAddCallback cb);
  void MsftAdvMonitorRemove(uint8_t monitor_handle, MsftAdvMonitorRemoveCallback cb);
  void MsftAdvMonitorEnable(bool enable, MsftAdvMonitorEnableCallback cb);

  static const ModuleFactory Factory;

 protected:
+12 −4
Original line number Diff line number Diff line
@@ -1229,8 +1229,7 @@ impl Into<MsftAdvMonitor> for &ScanFilter {

impl IBluetoothGatt for BluetoothGatt {
    fn is_msft_supported(&self) -> bool {
        // TODO(b/244505567): Wire the real capability from lower layer.
        false
        self.gatt.as_ref().unwrap().lock().unwrap().scanner.is_msft_supported()
    }

    fn register_scanner_callback(&mut self, callback: Box<dyn IScannerCallback + Send>) -> u32 {
@@ -1279,7 +1278,6 @@ impl IBluetoothGatt for BluetoothGatt {
        // Multiplexing scanners happens at this layer. The implementations of start_scan
        // and stop_scan maintains the state of all registered scanners and based on the states
        // update the scanning and/or filter states of libbluetooth.
        // TODO(b/217274432): Honor settings and filters.
        {
            let mut scanners_lock = self.scanners.lock().unwrap();

@@ -1293,6 +1291,8 @@ impl IBluetoothGatt for BluetoothGatt {
        }

        let gatt_async = self.gatt_async.clone();
        let scanners = self.scanners.clone();
        let is_msft_supported = self.is_msft_supported();
        tokio::spawn(async move {
            // The three operations below (monitor add, monitor enable, update scan) happen one
            // after another, and cannot be interleaved with other GATT async operations.
@@ -1302,7 +1302,8 @@ impl IBluetoothGatt for BluetoothGatt {
            // handling callbacks.
            let mut gatt_async = gatt_async.lock().await;

            if let Some(filter) = filter {
            // Add and enable the monitor filter only when the MSFT extension is supported.
            if let (true, Some(filter)) = (is_msft_supported, filter) {
                let monitor_handle = match gatt_async.msft_adv_monitor_add((&filter).into()).await {
                    Ok((handle, 0)) => handle,
                    _ => {
@@ -1311,6 +1312,13 @@ impl IBluetoothGatt for BluetoothGatt {
                    }
                };

                if let Some(scanner) =
                    Self::find_scanner_by_id(&mut scanners.lock().unwrap(), scanner_id)
                {
                    // The monitor handle is needed in stop_scan().
                    scanner.monitor_handle = Some(monitor_handle);
                }

                log::debug!("Added adv monitor handle = {}", monitor_handle);

                if !gatt_async
Loading