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

Commit 9688ed13 authored by Chris Manton's avatar Chris Manton
Browse files

gd: Use flushable attribute to packets

Bug: 178752129
Test: gd/cert/run
Tag: #refactor
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: Ie83541faf25a7d8d22877021ff8cc1ff32c110f2
parent 05390f6a
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -55,10 +55,9 @@ void bluetooth::shim::ACL_IgnoreLeConnectionFrom(
}

void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) {
  bool is_flushable = false;
  std::unique_ptr<bluetooth::packet::RawBuilder> packet =
      MakeUniquePacket(p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
                       p_buf->len - HCI_DATA_PREAMBLE_SIZE, is_flushable);
  std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(
      p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
      p_buf->len - HCI_DATA_PREAMBLE_SIZE, IsPacketFlushable(p_buf));
  Stack::GetInstance()->GetAcl()->WriteData(handle, std::move(packet));
  osi_free(p_buf);
}
+5 −0
Original line number Diff line number Diff line
@@ -244,6 +244,11 @@ inline hci::DisconnectReason ToDisconnectReasonFromLegacy(
  return static_cast<hci::DisconnectReason>(reason);
}

inline bool IsPacketFlushable(const BT_HDR* p_buf) {
  ASSERT(p_buf != nullptr);
  return ToPacketData<const HciDataPreamble>(p_buf)->IsFlushable();
}

namespace debug {

inline void DumpBtHdr(const BT_HDR* p_buf, const char* token) {
+5 −4
Original line number Diff line number Diff line
@@ -778,7 +778,7 @@ uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data) {
  auto* data = p_data->data + p_data->offset;
  uint8_t sent_length =
      classic_dynamic_channel_helper_map_[psm]->send(
          cid, MakeUniquePacket(data, len, false /* flushable */)) *
          cid, MakeUniquePacket(data, len, IsPacketFlushable(p_data))) *
      len;
  osi_free(p_data);
  return sent_length;
@@ -1014,8 +1014,9 @@ uint16_t L2CA_SendFixedChnlData(uint16_t cid, const RawAddress& rem_bda,
  auto* helper = &le_fixed_channel_helper_.find(cid)->second;
  auto len = p_buf->len;
  auto* data = p_buf->data + p_buf->offset;
  bool sent = helper->send(ToGdAddress(rem_bda),
                           MakeUniquePacket(data, len, false /* flushable */));
  bool sent =
      helper->send(ToGdAddress(rem_bda),
                   MakeUniquePacket(data, len, IsPacketFlushable(p_buf)));
  osi_free(p_buf);
  return sent ? L2CAP_DW_SUCCESS : L2CAP_DW_FAILED;
}
@@ -1567,7 +1568,7 @@ uint8_t L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data) {
  auto* data = p_data->data + p_data->offset;
  uint8_t sent_length =
      le_dynamic_channel_helper_map_[psm]->send(
          cid, MakeUniquePacket(data, len, false /* flushable */)) *
          cid, MakeUniquePacket(data, len, IsPacketFlushable(p_data))) *
      len;
  osi_free(p_data);
  return sent_length;
+45 −0
Original line number Diff line number Diff line
@@ -371,3 +371,48 @@ TEST_F(MainShimTest, connect_and_disconnect) {
                 std::move(done));
  future.wait();
}

TEST_F(MainShimTest, is_flushable) {
  {
    BT_HDR* bt_hdr =
        (BT_HDR*)calloc(1, sizeof(BT_HDR) + sizeof(HciDataPreamble));

    ASSERT_TRUE(!IsPacketFlushable(bt_hdr));
    HciDataPreamble* hci = ToPacketData<HciDataPreamble>(bt_hdr);
    hci->SetFlushable();
    ASSERT_TRUE(IsPacketFlushable(bt_hdr));

    free(bt_hdr);
  }

  {
    size_t offset = 1024;
    BT_HDR* bt_hdr =
        (BT_HDR*)calloc(1, sizeof(BT_HDR) + sizeof(HciDataPreamble) + offset);
    bt_hdr->offset = offset;

    ASSERT_TRUE(!IsPacketFlushable(bt_hdr));
    HciDataPreamble* hci = ToPacketData<HciDataPreamble>(bt_hdr);
    hci->SetFlushable();
    ASSERT_TRUE(IsPacketFlushable(bt_hdr));

    free(bt_hdr);
  }

  {
    size_t offset = 1024;
    BT_HDR* bt_hdr =
        (BT_HDR*)calloc(1, sizeof(BT_HDR) + sizeof(HciDataPreamble) + offset);

    uint8_t* p = ToPacketData<uint8_t>(bt_hdr, L2CAP_SEND_CMD_OFFSET);
    UINT16_TO_STREAM(
        p, 0x123 | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT));
    ASSERT_TRUE(!IsPacketFlushable(bt_hdr));

    p = ToPacketData<uint8_t>(bt_hdr, L2CAP_SEND_CMD_OFFSET);
    UINT16_TO_STREAM(p, 0x123 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
    ASSERT_TRUE(IsPacketFlushable(bt_hdr));

    free(bt_hdr);
  }
}
+11 −0
Original line number Diff line number Diff line
@@ -101,6 +101,17 @@ typedef struct {
  // and will not trigger various flexible member compilation issues.
} BT_HDR_RIGID;

#ifdef __cplusplus
template <typename T>
T* ToPacketData(BT_HDR* bt_hdr, size_t offset = 0) {
  return reinterpret_cast<T*>(bt_hdr->data + bt_hdr->offset + offset);
}
template <typename T>
const T* ToPacketData(const BT_HDR* bt_hdr, size_t offset = 0) {
  return reinterpret_cast<const T*>(bt_hdr->data + bt_hdr->offset + offset);
}
#endif  // __cplusplus

#define BT_HDR_SIZE (sizeof(BT_HDR))

enum {