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

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

Merge "Rootcanal: Use packets for LinkLayerSocket size"

parents 02979f32 bb4853ba
Loading
Loading
Loading
Loading
+20 −14
Original line number Diff line number Diff line
@@ -18,8 +18,9 @@

#include <unistd.h>

#include "packets/packet_view.h"
#include "packets/view.h"
#include "packet/packet_view.h"
#include "packet/raw_builder.h"
#include "packet/view.h"

using std::vector;

@@ -30,18 +31,18 @@ LinkLayerSocketDevice::LinkLayerSocketDevice(int socket_fd, Phy::Type phy_type)

void LinkLayerSocketDevice::TimerTick() {
  if (bytes_left_ == 0) {
    size_t size_bytes = sizeof(uint32_t);
    received_ = std::make_shared<std::vector<uint8_t>>(size_bytes);
    size_t bytes_received = socket_.TryReceive(size_bytes, received_->data());
    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) {
      return;
    }
    ASSERT_LOG(bytes_received == size_bytes, "bytes_received == %d",
               static_cast<int>(bytes_received));
    packets::PacketView<true> size({packets::View(received_, 0, size_bytes)});
    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)});
    bytes_left_ = size.begin().extract<uint32_t>();
    received_->resize(size_bytes + bytes_left_);
    offset_ = size_bytes;
    received_ = std::make_shared<std::vector<uint8_t>>(bytes_left_);
    offset_ = 0;
  }
  size_t bytes_received = socket_.TryReceive(bytes_left_, received_->data() + offset_);
  if (bytes_received == 0) {
@@ -62,11 +63,16 @@ void LinkLayerSocketDevice::TimerTick() {

void LinkLayerSocketDevice::IncomingPacket(
    model::packets::LinkLayerPacketView packet) {
  std::shared_ptr<std::vector<uint8_t>> payload_bytes =
      std::make_shared<std::vector<uint8_t>>(packet.begin(), packet.end());
  packets::PacketView<true> packet_view(payload_bytes);
  auto size_packet = bluetooth::packet::RawBuilder();
  size_packet.AddOctets4(packet.size());
  std::vector<uint8_t> size_bytes;
  bluetooth::packet::BitInserter bit_inserter(size_bytes);
  size_packet.Serialize(bit_inserter);

  socket_.TrySend(packet_view);
  if (socket_.TrySend(size_bytes) == kSizeBytes) {
    std::vector<uint8_t> payload_bytes{packet.begin(), packet.end()};
    socket_.TrySend(payload_bytes);
  }
}

}  // namespace test_vendor_lib
+2 −0
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ class LinkLayerSocketDevice : public Device {

  virtual void TimerTick() override;

  static constexpr size_t kSizeBytes = sizeof(uint32_t);

 private:
  net::PolledSocket socket_;
  Phy::Type phy_type_;
+2 −8
Original line number Diff line number Diff line
@@ -45,17 +45,11 @@ void PolledSocket::CleanUp() {
  file_descriptor_ = -1;
}

size_t PolledSocket::TrySend(packets::PacketView<true> packet) {
size_t PolledSocket::TrySend(const std::vector<uint8_t>& packet) {
  if (file_descriptor_ == -1) {
    return 0;
  }
  // Could skip this copy if the packet is guaranteed to be contiguous.
  std::vector<uint8_t> copy;
  copy.reserve(packet.size());
  for (const auto&& c : packet) {
    copy.push_back(c);
  }
  int ret = write(file_descriptor_, copy.data(), copy.size());
  int ret = write(file_descriptor_, packet.data(), packet.size());
  if (ret == -1) {
    LOG_WARN("%s error %s", __func__, strerror(errno));
    return 0;
+1 −3
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@
#include <sys/socket.h>
#include <unistd.h>

#include "packets/packet_view.h"

namespace test_vendor_lib {
namespace net {

@@ -41,7 +39,7 @@ class PolledSocket {
  PolledSocket(PolledSocket&& p);
  virtual ~PolledSocket();

  size_t TrySend(packets::PacketView<true> packet);
  size_t TrySend(const std::vector<uint8_t>& packet);
  // size_t TrySendVector(const std::vector<const std::vector<uint8_t>&>& data);
  size_t TryReceive(size_t num_bytes, uint8_t* data);