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

Commit 0a9b2017 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Add partial BleScanner topshim

Add a partially implemented BleScanner topshim implementation. This
implements the callbacks but not the method calls into BleScanner since
lack of callbacks is causing a crash in Floss.

Bug: 216384386
Tag: #floss
Test: Discovery works without segfault on ChromeOS
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: If843996a41d44e21ab436c1ceaf825971f6ce1a2
parent ea183db4
Loading
Loading
Loading
Loading
+7 −1
Original line number Original line Diff line number Diff line
@@ -6,7 +6,8 @@ use bt_topshim::bindings::root::bluetooth::Uuid;
use bt_topshim::btif::{BluetoothInterface, RawAddress, Uuid128Bit};
use bt_topshim::btif::{BluetoothInterface, RawAddress, Uuid128Bit};
use bt_topshim::profiles::gatt::{
use bt_topshim::profiles::gatt::{
    BtGattDbElement, BtGattNotifyParams, BtGattReadParams, Gatt, GattClientCallbacks,
    BtGattDbElement, BtGattNotifyParams, BtGattReadParams, Gatt, GattClientCallbacks,
    GattClientCallbacksDispatcher, GattServerCallbacksDispatcher, GattStatus,
    GattClientCallbacksDispatcher, GattScannerCallbacksDispatcher, GattServerCallbacksDispatcher,
    GattStatus,
};
};
use bt_topshim::topstack;
use bt_topshim::topstack;


@@ -514,6 +515,11 @@ impl BluetoothGatt {
                    debug!("received Gatt server callback: {:?}", cb);
                    debug!("received Gatt server callback: {:?}", cb);
                }),
                }),
            },
            },
            GattScannerCallbacksDispatcher {
                dispatch: Box::new(move |cb| {
                    debug!("received Gatt scanner callback: {:?}", cb);
                }),
            },
        );
        );
    }
    }
}
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -50,6 +50,7 @@ cc_library_static {
        "btav_sink/btav_sink_shim.cc",
        "btav_sink/btav_sink_shim.cc",
        "btif/btif_shim.cc",
        "btif/btif_shim.cc",
        "gatt/gatt_shim.cc",
        "gatt/gatt_shim.cc",
        "gatt/gatt_ble_scanner_shim.cc",
        "hfp/hfp_shim.cc",
        "hfp/hfp_shim.cc",
        "controller/controller_shim.cc",
        "controller/controller_shim.cc",
        "common/utils.cc",
        "common/utils.cc",
+1 −0
Original line number Original line Diff line number Diff line
@@ -55,6 +55,7 @@ source_set("btif_cxx_bridge_code") {
    "btav_sink/btav_sink_shim.cc",
    "btav_sink/btav_sink_shim.cc",
    "hfp/hfp_shim.cc",
    "hfp/hfp_shim.cc",
    "gatt/gatt_shim.cc",
    "gatt/gatt_shim.cc",
    "gatt/gatt_ble_scanner_shim.cc",
    "controller/controller_shim.cc",
    "controller/controller_shim.cc",
    "common/utils.cc",
    "common/utils.cc",
  ]
  ]
+113 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */

#include "gd/rust/topshim/gatt/gatt_ble_scanner_shim.h"

#include <algorithm>
#include <iterator>
#include <vector>

#include "gd/rust/topshim/common/utils.h"
#include "rust/cxx.h"
#include "src/profiles/gatt.rs.h"
#include "types/raw_address.h"

namespace bluetooth {
namespace topshim {
namespace rust {

namespace rusty = ::bluetooth::topshim::rust;

void BleScannerIntf::RegisterCallbacks() {
  // Register self as a callback handler. We will dispatch to Rust callbacks.
  scanner_intf_->RegisterCallbacks(this);
}

// ScanningCallbacks overrides
void BleScannerIntf::OnScannerRegistered(const bluetooth::Uuid app_uuid, uint8_t scannerId, uint8_t status) {
  rusty::gdscan_on_scanner_registered(reinterpret_cast<const signed char*>(&app_uuid), scannerId, status);
}

void BleScannerIntf::OnSetScannerParameterComplete(uint8_t scannerId, uint8_t status) {
  rusty::gdscan_on_set_scanner_parameter_complete(scannerId, status);
}

void BleScannerIntf::OnScanResult(
    uint16_t event_type,
    uint8_t addr_type,
    RawAddress bda,
    uint8_t primary_phy,
    uint8_t secondary_phy,
    uint8_t advertising_sid,
    int8_t tx_power,
    int8_t rssi,
    uint16_t periodic_adv_int,
    std::vector<uint8_t> adv_data) {
  RustRawAddress raw_address = rusty::CopyToRustAddress(bda);
  rusty::gdscan_on_scan_result(
      event_type,
      addr_type,
      reinterpret_cast<const signed char*>(&raw_address),
      primary_phy,
      secondary_phy,
      advertising_sid,
      tx_power,
      rssi,
      periodic_adv_int,
      adv_data.data(),
      adv_data.size());
}

void BleScannerIntf::OnTrackAdvFoundLost(AdvertisingTrackInfo ati) {
  rusty::RustRawAddress addr = rusty::CopyToRustAddress(ati.advertiser_address);
  rusty::RustAdvertisingTrackInfo rust_info = {
      .scanner_id = ati.scanner_id,
      .filter_index = ati.filter_index,
      .advertiser_state = ati.advertiser_state,
      .advertiser_info_present = ati.advertiser_info_present,
      .advertiser_address = addr,
      .advertiser_address_type = ati.advertiser_address_type,
      .tx_power = ati.tx_power,
      .rssi = ati.rssi,
      .timestamp = ati.time_stamp,
      .adv_packet_len = ati.adv_packet_len,
      // .adv_packet is copied below
      .scan_response_len = ati.scan_response_len,
      // .scan_response is copied below
  };

  std::copy(ati.adv_packet.begin(), ati.adv_packet.end(), std::back_inserter(rust_info.adv_packet));
  std::copy(ati.scan_response.begin(), ati.scan_response.end(), std::back_inserter(rust_info.scan_response));

  rusty::gdscan_on_track_adv_found_lost(rust_info);
}

void BleScannerIntf::OnBatchScanReports(
    int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) {
  rusty::gdscan_on_batch_scan_reports(client_if, status, report_format, num_records, data.data(), data.size());
}

void BleScannerIntf::OnBatchScanThresholdCrossed(int client_if) {
  rusty::gdscan_on_batch_scan_threshold_crossed(client_if);
}

std::unique_ptr<BleScannerIntf> GetBleScannerIntf(const unsigned char* gatt_intf) {
  return std::make_unique<BleScannerIntf>(reinterpret_cast<const btgatt_interface_t*>(gatt_intf)->scanner);
}

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
+70 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */
#ifndef GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H
#define GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H

#include <memory>

#include "include/hardware/ble_scanner.h"
#include "include/hardware/bt_gatt.h"
#include "rust/cxx.h"

namespace bluetooth {
namespace topshim {
namespace rust {

class BleScannerIntf : public ScanningCallbacks {
 public:
  BleScannerIntf(BleScannerInterface* scanner_intf) : scanner_intf_(scanner_intf){};
  ~BleScannerIntf() = default;

  void RegisterCallbacks();

  // ScanningCallbacks overrides
  void OnScannerRegistered(const bluetooth::Uuid app_uuid, uint8_t scannerId, uint8_t status) override;

  void OnSetScannerParameterComplete(uint8_t scannerId, uint8_t status) override;

  void OnScanResult(
      uint16_t event_type,
      uint8_t addr_type,
      RawAddress bda,
      uint8_t primary_phy,
      uint8_t secondary_phy,
      uint8_t advertising_sid,
      int8_t tx_power,
      int8_t rssi,
      uint16_t periodic_adv_int,
      std::vector<uint8_t> adv_data) override;

  void OnTrackAdvFoundLost(AdvertisingTrackInfo advertising_track_info) override;

  void OnBatchScanReports(
      int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) override;

  void OnBatchScanThresholdCrossed(int client_if) override;

 private:
  BleScannerInterface* scanner_intf_;
};

std::unique_ptr<BleScannerIntf> GetBleScannerIntf(const unsigned char* gatt_intf);

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth

#endif  // GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H
Loading