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

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

Merge "RootCanal: Handle partial sizes in link layer"

parents f2d5e850 b8b2091b
Loading
Loading
Loading
Loading
+22 −20
Original line number Diff line number Diff line
@@ -27,38 +27,40 @@ using std::vector;
namespace test_vendor_lib {

LinkLayerSocketDevice::LinkLayerSocketDevice(int socket_fd, Phy::Type phy_type)
    : socket_(socket_fd), phy_type_(phy_type) {}
    : socket_(socket_fd),
      phy_type_(phy_type),
      size_bytes_(std::make_shared<std::vector<uint8_t>>(kSizeBytes)) {}

void LinkLayerSocketDevice::TimerTick() {
  if (bytes_left_ == 0) {
    auto packet_size = std::make_shared<std::vector<uint8_t>>(kSizeBytes);

    size_t bytes_received = socket_.TryReceive(kSizeBytes, packet_size->data());
    if (bytes_received == 0) {
  if (receiving_size_) {
    size_t bytes_received =
        socket_.TryReceive(kSizeBytes, size_bytes_->data() + offset_);
    if (bytes_received < bytes_left_) {
      bytes_left_ -= bytes_received;
      offset_ += bytes_received;
      return;
    }
    ASSERT_LOG(bytes_received == kSizeBytes, "bytes_received == %d", static_cast<int>(bytes_received));
    bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> size(
        {bluetooth::packet::View(packet_size, 0, kSizeBytes)});
        {bluetooth::packet::View(size_bytes_, 0, kSizeBytes)});
    bytes_left_ = size.begin().extract<uint32_t>();
    received_ = std::make_shared<std::vector<uint8_t>>(bytes_left_);
    offset_ = 0;
    receiving_size_ = false;
  }
  size_t bytes_received = socket_.TryReceive(bytes_left_, received_->data() + offset_);
  if (bytes_received == 0) {
    return;
  }
  if (bytes_received < bytes_left_) {
    bytes_left_ -= bytes_received;
    offset_ += bytes_received;
  if (bytes_left_ == 0) {
    bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet_view(
        received_);
    auto packet = model::packets::LinkLayerPacketView::Create(packet_view);
    return;
  }
  bytes_left_ = kSizeBytes;
  offset_ = 0;
  receiving_size_ = true;
  auto packet = model::packets::LinkLayerPacketView::Create(
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian>(
          received_));
  ASSERT(packet.IsValid());
  SendLinkLayerPacket(packet, phy_type_);
    offset_ = 0;
    received_.reset();
  }
}

void LinkLayerSocketDevice::IncomingPacket(
+4 −2
Original line number Diff line number Diff line
@@ -52,8 +52,10 @@ class LinkLayerSocketDevice : public Device {
 private:
  net::PolledSocket socket_;
  Phy::Type phy_type_;
  size_t bytes_left_{0};
  size_t offset_{};
  bool receiving_size_{true};
  size_t bytes_left_{kSizeBytes};
  size_t offset_{0};
  std::shared_ptr<std::vector<uint8_t>> size_bytes_;
  std::shared_ptr<std::vector<uint8_t>> received_;
  std::vector<model::packets::LinkLayerPacketView> packet_queue_;
};