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

Commit 67193845 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "gd: Handle LE advertisement tracking sub-event"

parents 3864cf4a dee71e9d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ class LeScanningManagerFacadeService : public LeScanningManagerFacade::Service,
    le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
    pending_events_.OnIncomingEvent(std::move(le_report_msg));
  };
  void OnTrackAdvFoundLost(){};
  void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info){};
  void OnBatchScanReports(int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data){};
  void OnBatchScanThresholdCrossed(int client_if){};
  void OnTimeout(){};
+7 −2
Original line number Diff line number Diff line
@@ -5631,10 +5631,15 @@ packet VendorSpecificEvent : Event (event_code = VENDOR_SPECIFIC) {
packet StorageThresholdBreachEvent : VendorSpecificEvent (subevent_code = BLE_THRESHOLD) {
}

enum AdvtInfoPresent : 8 {
  ADVT_INFO_PRESENT = 0x00,
  NO_ADVT_INFO_PRESENT = 0x01,
}

packet LEAdvertisementTrackingEvent : VendorSpecificEvent (subevent_code = BLE_TRACKING) {
  apcf_filter_index : 8,
  advertiser_state : 8,
  advt_info_present : 8,
  advt_info_present : AdvtInfoPresent,
  advertiser_address : Address,
  advertiser_address_type : 8,
  _body_,
@@ -5643,7 +5648,7 @@ packet LEAdvertisementTrackingEvent : VendorSpecificEvent (subevent_code = BLE_T
packet LEAdvertisementTrackingWithInfoEvent : LEAdvertisementTrackingEvent {
  tx_power : 8,
  rssi : 8,
  timestamp : 8,
  timestamp : 16,
  _size_(adv_packet) : 8,
  adv_packet : 8[],
  _size_(scan_response) : 8,
+87 −4
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ class NullScanningCallback : public ScanningCallback {
      std::vector<uint8_t> advertising_data) {
    LOG_INFO("OnScanResult in NullScanningCallback");
  }
  void OnTrackAdvFoundLost() {
  void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info) {
    LOG_INFO("OnTrackAdvFoundLost in NullScanningCallback");
  }
  void OnBatchScanReports(int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) {
@@ -192,6 +192,7 @@ struct BatchScanConfig {
  uint32_t scan_interval;
  uint32_t scan_window;
  BatchScanDiscardRule discard_rule;
  ScannerId ref_value;
};

struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback {
@@ -222,12 +223,21 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
    }
    is_filter_support_ = controller_->IsSupported(OpCode::LE_ADV_FILTER);
    is_batch_scan_support_ = controller->IsSupported(OpCode::LE_BATCH_SCAN);
    if (is_batch_scan_support_) {
      // TODO implement vse module
      // hci_layer_->RegisterVesEventHandler(
      //     VseSubeventCode::BLE_THRESHOLD, handler->BindOn(this,
      //     &LeScanningManager::impl::on_storage_threshold_breach));
      // hci_layer_->RegisterVesEventHandler(
      //     VseSubeventCode::BLE_TRACKING, handler->BindOn(this, &LeScanningManager::impl::on_advertisement_tracking));
    }
    scanners_ = std::vector<Scanner>(kMaxAppNum + 1);
    for (size_t i = 0; i < scanners_.size(); i++) {
      scanners_[i].app_uuid = Uuid::kEmpty;
      scanners_[i].in_use = false;
    }
    batch_scan_config_.current_state = BatchScanState::DISABLED_STATE;
    batch_scan_config_.ref_value = kInvalidScannerId;
    configure_scan();
  }

@@ -235,6 +245,13 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
    for (auto subevent_code : LeScanningEvents) {
      hci_layer_->UnregisterLeEventHandler(subevent_code);
    }
    if (is_batch_scan_support_) {
      // TODO implete vse module
      // hci_layer_->UnregisterVesEventHandler(VseSubeventCode::BLE_THRESHOLD);
      // hci_layer_->UnregisterVesEventHandler(VseSubeventCode::BLE_TRACKING);
    }
    batch_scan_config_.current_state = BatchScanState::DISABLED_STATE;
    batch_scan_config_.ref_value = kInvalidScannerId;
    scanning_callbacks_ = &null_scanning_callback_;
  }

@@ -830,11 +847,16 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
  }

  void batch_scan_set_storage_parameter(
      uint8_t batch_scan_full_max, uint8_t batch_scan_truncated_max, uint8_t batch_scan_notify_threshold) {
      uint8_t batch_scan_full_max,
      uint8_t batch_scan_truncated_max,
      uint8_t batch_scan_notify_threshold,
      ScannerId scanner_id) {
    if (!is_batch_scan_support_) {
      LOG_WARN("Batch scan is not supported");
      return;
    }
    // scanner id for OnBatchScanThresholdCrossed
    batch_scan_config_.ref_value = scanner_id;

    if (batch_scan_config_.current_state == BatchScanState::ERROR_STATE ||
        batch_scan_config_.current_state == BatchScanState::DISABLED_STATE ||
@@ -963,6 +985,18 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
        module_handler_->BindOnceOn(this, &impl::on_batch_scan_read_result_complete, scanner_id, total_num_of_records));
  }

  void track_advertiser(ScannerId scanner_id) {
    if (!is_batch_scan_support_) {
      LOG_WARN("Batch scan is not supported");
      AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info = {};
      on_found_on_lost_info.scanner_id = scanner_id;
      on_found_on_lost_info.advertiser_info_present = AdvtInfoPresent::NO_ADVT_INFO_PRESENT;
      scanning_callbacks_->OnTrackAdvFoundLost(on_found_on_lost_info);
      return;
    }
    tracker_id = scanner_id;
  }

  void register_scanning_callback(ScanningCallback* scanning_callbacks) {
    scanning_callbacks_ = scanning_callbacks;
  }
@@ -1112,6 +1146,46 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
    }
  }

  void on_storage_threshold_breach(VendorSpecificEventView event) {
    if (batch_scan_config_.ref_value == kInvalidScannerId) {
      LOG_WARN("storage threshold was not set !!");
      return;
    }
    scanning_callbacks_->OnBatchScanThresholdCrossed(static_cast<int>(batch_scan_config_.ref_value));
  }

  void on_advertisement_tracking(VendorSpecificEventView event) {
    if (tracker_id == kInvalidScannerId) {
      LOG_WARN("Advertisement track is not register");
      return;
    }
    auto view = LEAdvertisementTrackingEventView::Create(event);
    ASSERT(view.IsValid());
    AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info = {};
    on_found_on_lost_info.scanner_id = tracker_id;
    on_found_on_lost_info.filter_index = view.GetApcfFilterIndex();
    on_found_on_lost_info.advertiser_state = view.GetAdvertiserState();
    on_found_on_lost_info.advertiser_address = view.GetAdvertiserAddress();
    on_found_on_lost_info.advertiser_address_type = view.GetAdvertiserAddressType();
    on_found_on_lost_info.advertiser_info_present = view.GetAdvtInfoPresent();
    /* Extract the adv info details */
    if (on_found_on_lost_info.advertiser_info_present == AdvtInfoPresent::ADVT_INFO_PRESENT) {
      auto info_view = LEAdvertisementTrackingWithInfoEventView::Create(view);
      ASSERT(info_view.IsValid());
      on_found_on_lost_info.tx_power = info_view.GetTxPower();
      on_found_on_lost_info.rssi = info_view.GetRssi();
      on_found_on_lost_info.time_stamp = info_view.GetTimestamp();
      auto adv_data = info_view.GetAdvPacket();
      on_found_on_lost_info.adv_packet.reserve(adv_data.size());
      on_found_on_lost_info.adv_packet.insert(on_found_on_lost_info.adv_packet.end(), adv_data.begin(), adv_data.end());
      auto scan_rsp_data = info_view.GetScanResponse();
      on_found_on_lost_info.scan_response.reserve(scan_rsp_data.size());
      on_found_on_lost_info.scan_response.insert(
          on_found_on_lost_info.scan_response.end(), scan_rsp_data.begin(), scan_rsp_data.end());
    }
    scanning_callbacks_->OnTrackAdvFoundLost(on_found_on_lost_info);
  }

  void OnPause() override {
    paused_ = true;
    scan_on_resume_ = is_scanning_;
@@ -1157,6 +1231,7 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
  LeScanningFilterPolicy filter_policy_{LeScanningFilterPolicy::ACCEPT_ALL};
  BatchScanConfig batch_scan_config_;
  std::map<ScannerId, std::vector<uint8_t>> batch_scan_result_cache_;
  ScannerId tracker_id = kInvalidScannerId;

  static void check_status(CommandCompleteView view) {
    switch (view.GetCommandOpCode()) {
@@ -1246,13 +1321,17 @@ void LeScanningManager::ScanFilterAdd(
}

void LeScanningManager::BatchScanConifgStorage(
    uint8_t batch_scan_full_max, uint8_t batch_scan_truncated_max, uint8_t batch_scan_notify_threshold) {
    uint8_t batch_scan_full_max,
    uint8_t batch_scan_truncated_max,
    uint8_t batch_scan_notify_threshold,
    ScannerId scanner_id) {
  CallOn(
      pimpl_.get(),
      &impl::batch_scan_set_storage_parameter,
      batch_scan_full_max,
      batch_scan_truncated_max,
      batch_scan_notify_threshold);
      batch_scan_notify_threshold,
      scanner_id);
}

void LeScanningManager::BatchScanEnable(
@@ -1277,6 +1356,10 @@ void LeScanningManager::BatchScanReadReport(ScannerId scanner_id, BatchScanMode
  CallOn(pimpl_.get(), &impl::batch_scan_read_results, scanner_id, 0, scan_mode);
}

void LeScanningManager::TrackAdvertiser(ScannerId scanner_id) {
  CallOn(pimpl_.get(), &impl::track_advertiser, scanner_id);
}

void LeScanningManager::RegisterScanningCallback(ScanningCallback* scanning_callback) {
  CallOn(pimpl_.get(), &impl::register_scanning_callback, scanning_callback);
}
+23 −2
Original line number Diff line number Diff line
@@ -28,6 +28,21 @@ namespace hci {

using ScannerId = uint8_t;

class AdvertisingFilterOnFoundOnLostInfo {
 public:
  uint8_t scanner_id;
  uint8_t filter_index;
  uint8_t advertiser_state;
  AdvtInfoPresent advertiser_info_present;
  Address advertiser_address;
  uint8_t advertiser_address_type;
  uint8_t tx_power;
  int8_t rssi;
  uint16_t time_stamp;
  std::vector<uint8_t> adv_packet;
  std::vector<uint8_t> scan_response;
};

class ScanningCallback {
 public:
  enum ScanningStatus {
@@ -50,7 +65,7 @@ class ScanningCallback {
      int8_t rssi,
      uint16_t periodic_advertising_interval,
      std::vector<uint8_t> advertising_data) = 0;
  virtual void OnTrackAdvFoundLost() = 0;
  virtual void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info) = 0;
  virtual void OnBatchScanReports(
      int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) = 0;
  virtual void OnBatchScanThresholdCrossed(int client_if) = 0;
@@ -102,6 +117,7 @@ class LeScanningManager : public bluetooth::Module {
  static constexpr uint8_t kAdvertisingDataInfoNotPresent = 0xff;
  static constexpr uint8_t kTxPowerInformationNotPresent = 0x7f;
  static constexpr uint8_t kNotPeriodicAdvertisement = 0x00;
  static constexpr ScannerId kInvalidScannerId = 0xFF;
  LeScanningManager();

  void RegisterScanner(const Uuid app_uuid);
@@ -122,7 +138,10 @@ class LeScanningManager : public bluetooth::Module {

  /*Batch Scan*/
  void BatchScanConifgStorage(
      uint8_t batch_scan_full_max, uint8_t batch_scan_truncated_max, uint8_t batch_scan_notify_threshold);
      uint8_t batch_scan_full_max,
      uint8_t batch_scan_truncated_max,
      uint8_t batch_scan_notify_threshold,
      ScannerId scanner_id);
  void BatchScanEnable(
      BatchScanMode scan_mode,
      uint32_t duty_cycle_scan_window_slots,
@@ -131,6 +150,8 @@ class LeScanningManager : public bluetooth::Module {
  void BatchScanDisable();
  void BatchScanReadReport(ScannerId scanner_id, BatchScanMode scan_mode);

  void TrackAdvertiser(ScannerId scanner_id);

  void RegisterScanningCallback(ScanningCallback* scanning_callback);

  static const ModuleFactory Factory;
+6 −2
Original line number Diff line number Diff line
@@ -314,7 +314,11 @@ class LeScanningManagerTest : public ::testing::Test {
         uint16_t periodic_advertising_interval,
         std::vector<uint8_t> advertising_data),
        (override));
    MOCK_METHOD(void, OnTrackAdvFoundLost, (), (override));
    MOCK_METHOD(
        void,
        OnTrackAdvFoundLost,
        (bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info),
        (override));
    MOCK_METHOD(
        void,
        OnBatchScanReports,
@@ -467,7 +471,7 @@ TEST_F(LeAndroidHciScanningManagerTest, scan_filter_add_test) {
TEST_F(LeAndroidHciScanningManagerTest, read_batch_scan_result) {
  // Enable batch scan feature
  auto next_command_future = test_hci_layer_->GetCommandFuture();
  le_scanning_manager->BatchScanConifgStorage(100, 0, 95);
  le_scanning_manager->BatchScanConifgStorage(100, 0, 95, 0x00);
  auto result = next_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
  ASSERT_EQ(std::future_status::ready, result);
  test_hci_layer_->IncomingEvent(LeBatchScanEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
Loading