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

Commit dac1c8ca authored by Hansong Zhang's avatar Hansong Zhang
Browse files

GD L2cap: buffer packets if that CID cannot be handled

We need to buffer a few packets before data controller is attached,
instead of dropping them directly.

Bug: 150170271
Test: cert/run
Tag: #stability
Change-Id: I2bc7631c85478ac6564a290ff16ac4055ec316ef
parent 03cb9cac
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
@@ -25,9 +25,12 @@
namespace bluetooth {
namespace l2cap {
namespace internal {
Receiver::Receiver(LowerQueueUpEnd* link_queue_up_end, os::Handler* handler,
                   DataPipelineManager* data_pipeline_manager_)
    : link_queue_up_end_(link_queue_up_end), handler_(handler), data_pipeline_manager_(data_pipeline_manager_) {
Receiver::Receiver(
    LowerQueueUpEnd* link_queue_up_end, os::Handler* handler, DataPipelineManager* data_pipeline_manager_)
    : link_queue_up_end_(link_queue_up_end),
      handler_(handler),
      buffer_timer_(handler),
      data_pipeline_manager_(data_pipeline_manager_) {
  ASSERT(link_queue_up_end_ != nullptr && handler_ != nullptr);
  link_queue_up_end_->RegisterDequeue(handler_,
                                      common::Bind(&Receiver::link_queue_dequeue_callback, common::Unretained(this)));
@@ -50,12 +53,36 @@ void Receiver::link_queue_dequeue_callback() {
  auto* data_controller = data_pipeline_manager_->GetDataController(cid);
  if (data_controller == nullptr) {
    // TODO(b/150170271): Buffer a few packets before data controller is attached
    LOG_WARN("Received a packet with invalid cid: %d", cid);
    LOG_WARN("Received a packet without data controller. cid: %d", cid);
    buffered_packets_.emplace(*packet);
    LOG_WARN("Enqueued the unexpected packet. Current queue size: %zu", buffered_packets_.size());
    buffer_timer_.Schedule(
        common::BindOnce(&Receiver::check_buffered_packets, common::Unretained(this)), std::chrono::milliseconds(500));

    return;
  }
  data_controller->OnPdu(*packet);
}

void Receiver::check_buffered_packets() {
  while (!buffered_packets_.empty()) {
    auto packet = buffered_packets_.front();
    buffered_packets_.pop();
    auto basic_frame_view = BasicFrameView::Create(packet);
    if (!basic_frame_view.IsValid()) {
      LOG_WARN("Received an invalid basic frame");
      return;
    }
    Cid cid = static_cast<Cid>(basic_frame_view.GetChannelId());
    auto* data_controller = data_pipeline_manager_->GetDataController(cid);
    if (data_controller == nullptr) {
      LOG_ERROR("Dropping a packet with invalid cid: %d", cid);
    } else {
      data_controller->OnPdu(packet);
    }
  }
}

}  // namespace internal
}  // namespace l2cap
}  // namespace bluetooth
+5 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#pragma once

#include <memory>
#include <queue>
#include <unordered_map>
#include <utility>

@@ -26,6 +27,7 @@
#include "l2cap/internal/scheduler.h"
#include "l2cap/l2cap_packets.h"
#include "l2cap/mtu.h"
#include "os/alarm.h"
#include "os/queue.h"
#include "packet/base_packet_builder.h"
#include "packet/packet_view.h"
@@ -59,9 +61,12 @@ class Receiver {
 private:
  LowerQueueUpEnd* link_queue_up_end_;
  os::Handler* handler_;
  os::Alarm buffer_timer_;
  std::queue<LowerDequeue> buffered_packets_;
  DataPipelineManager* data_pipeline_manager_;

  void link_queue_dequeue_callback();
  void check_buffered_packets();
};

}  // namespace internal