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

Commit 28f9b466 authored by Myles Watson's avatar Myles Watson
Browse files

Disable advertising set on connection

Send LeAdvertisingSetTerminated event when advertising is
terminated due to an incoming connection.

Bug: 202018502
Test: run/cert
      CtsVerifier LE Secure Server/Client
Tag: #feature
Change-Id: Ia90de3648a2830ebc533139b5e69c32d09f6c7be
parent 8cb04188
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ void LeAdvertiser::SetScanResponse(const std::vector<uint8_t>& data) {
void LeAdvertiser::Enable() {
  enabled_ = true;
  last_le_advertisement_ = std::chrono::steady_clock::now() - interval_;
  num_events_ = 0;
  LOG_INFO("%s -> %s type = %hhx ad length %zu, scan length %zu",
           address_.ToString().c_str(), peer_address_.ToString().c_str(), type_,
           advertisement_.size(), scan_response_.size());
@@ -86,11 +87,11 @@ void LeAdvertiser::Enable() {

void LeAdvertiser::EnableExtended(
    std::chrono::steady_clock::duration duration) {
  last_le_advertisement_ = std::chrono::steady_clock::now();
  Enable();
  if (duration != std::chrono::milliseconds(0)) {
    ending_time_ = std::chrono::steady_clock::now() + duration;
  }
  enabled_ = true;
  extended_ = true;
  LOG_INFO("%s -> %s type = %hhx ad length %zu, scan length %zu",
           address_.ToString().c_str(), peer_address_.ToString().c_str(), type_,
           advertisement_.size(), scan_response_.size());
@@ -100,6 +101,10 @@ void LeAdvertiser::Disable() { enabled_ = false; }

bool LeAdvertiser::IsEnabled() const { return enabled_; }

bool LeAdvertiser::IsExtended() const { return extended_; }

uint8_t LeAdvertiser::GetNumAdvertisingEvents() const { return num_events_; }

std::unique_ptr<model::packets::LeAdvertisementBuilder>
LeAdvertiser::GetAdvertisement(std::chrono::steady_clock::time_point now) {
  if (!enabled_) {
@@ -116,6 +121,7 @@ LeAdvertiser::GetAdvertisement(std::chrono::steady_clock::time_point now) {
  }

  last_le_advertisement_ = now;
  num_events_ += (num_events_ < 255 ? 1 : 0);
  return model::packets::LeAdvertisementBuilder::Create(
      address_.GetAddress(), peer_address_.GetAddress(),
      static_cast<model::packets::AddressType>(address_.GetAddressType()),
+6 −0
Original line number Diff line number Diff line
@@ -69,6 +69,10 @@ class LeAdvertiser {

  bool IsEnabled() const;

  bool IsExtended() const;

  uint8_t GetNumAdvertisingEvents() const;

  bluetooth::hci::AddressWithType GetAddress() const;

 private:
@@ -81,6 +85,8 @@ class LeAdvertiser {
  std::vector<uint8_t> scan_response_;
  std::chrono::steady_clock::duration interval_{};
  std::chrono::steady_clock::time_point ending_time_{};
  uint8_t num_events_{0};
  bool extended_{false};
  bool enabled_{false};
  std::chrono::steady_clock::time_point last_le_advertisement_;
};
+28 −15
Original line number Diff line number Diff line
@@ -1270,7 +1270,7 @@ void LinkLayerController::IncomingLeAdvertisementPacket(
  }
}

void LinkLayerController::HandleLeConnection(AddressWithType address,
uint16_t LinkLayerController::HandleLeConnection(AddressWithType address,
                                                 AddressWithType own_address,
                                                 uint8_t role,
                                                 uint16_t connection_interval,
@@ -1281,16 +1281,17 @@ void LinkLayerController::HandleLeConnection(AddressWithType address,
  if (handle == kReservedHandle) {
    LOG_WARN("No pending connection for connection from %s",
             address.ToString().c_str());
    return;
    return kReservedHandle;
  }
  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
    auto packet = bluetooth::hci::LeConnectionCompleteBuilder::Create(
        ErrorCode::SUCCESS, handle, static_cast<bluetooth::hci::Role>(role),
        address.GetAddressType(), address.GetAddress(), connection_interval,
        connection_latency, supervision_timeout,
        static_cast<bluetooth::hci::ClockAccuracy>(0x00));
  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
    send_event_(std::move(packet));
  }
  return handle;
}

void LinkLayerController::IncomingLeConnectPacket(
@@ -1312,11 +1313,13 @@ void LinkLayerController::IncomingLeConnectPacket(
  }
  bluetooth::hci::AddressWithType my_address{};
  bool matched_advertiser = false;
  for (auto advertiser : advertisers_) {
    AddressWithType advertiser_address = advertiser.GetAddress();
  size_t set = 0;
  for (size_t i = 0; i < advertisers_.size(); i++) {
    AddressWithType advertiser_address = advertisers_[i].GetAddress();
    if (incoming.GetDestinationAddress() == advertiser_address.GetAddress()) {
      my_address = advertiser_address;
      matched_advertiser = true;
      set = i;
    }
  }

@@ -1326,7 +1329,7 @@ void LinkLayerController::IncomingLeConnectPacket(
    return;
  }

  HandleLeConnection(
  uint16_t handle = HandleLeConnection(
      AddressWithType(
          incoming.GetSourceAddress(),
          static_cast<bluetooth::hci::AddressType>(connect.GetAddressType())),
@@ -1340,6 +1343,16 @@ void LinkLayerController::IncomingLeConnectPacket(
      connect.GetLeConnectionSupervisionTimeout(),
      static_cast<uint8_t>(my_address.GetAddressType()));
  SendLeLinkLayerPacket(std::move(to_send));

  if (advertisers_[set].IsExtended()) {
    uint8_t num_advertisements = advertisers_[set].GetNumAdvertisingEvents();
    advertisers_[set].Disable();
    if (properties_.GetLeEventSupported(
            bluetooth::hci::SubeventCode::ADVERTISING_SET_TERMINATED)) {
      send_event_(bluetooth::hci::LeAdvertisingSetTerminatedBuilder::Create(
          ErrorCode::SUCCESS, set, handle, num_advertisements));
    }
  }
}

void LinkLayerController::IncomingLeConnectCompletePacket(
+4 −4
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ class LinkLayerController {
  ErrorCode LeConnectionUpdate(
      bluetooth::hci::LeConnectionUpdateView connection_update_view);

  void HandleLeConnection(AddressWithType addr, AddressWithType own_addr,
  uint16_t HandleLeConnection(AddressWithType addr, AddressWithType own_addr,
                              uint8_t role, uint16_t connection_interval,
                              uint16_t connection_latency,
                              uint16_t supervision_timeout);