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

Commit 9fbccebd authored by johnshamoon's avatar johnshamoon Committed by android-build-merger
Browse files

test_vendor: Use Custom Iterators in L2CAP Classes

am: 3297977f

Change-Id: Ie5fcdbe89d35ab391fd756f675b264d1dd2233ed
parents 6bd6781b 3297977f
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -62,6 +62,7 @@ class HciPacket : public std::enable_shared_from_this<HciPacket> {
  virtual size_t get_length() = 0;
  virtual size_t get_length() = 0;


  virtual uint8_t& get_at_index(size_t index) = 0;
  virtual uint8_t& get_at_index(size_t index) = 0;

};  // HciPacket
};  // HciPacket


};  // namespace test_vendor_lib
};  // namespace test_vendor_lib
+6 −17
Original line number Original line Diff line number Diff line
@@ -34,21 +34,16 @@ const int kSduHeaderLength = 4;
class L2capPacket : public HciPacket {
class L2capPacket : public HciPacket {
 public:
 public:
  // Returns an assembled L2cap object if successful, nullptr if failure.
  // Returns an assembled L2cap object if successful, nullptr if failure.
  static std::unique_ptr<L2capPacket> assemble(
  static std::shared_ptr<L2capPacket> assemble(
      const std::vector<std::unique_ptr<L2capSdu> >& sdu_packet);
      const std::vector<std::shared_ptr<L2capSdu> >& sdu_packet);

  // Construct a vector of just the L2CAP payload. This essentially
  // will remove the L2CAP header from the private member variable.
  // TODO: Remove this in favor of custom iterators.
  std::vector<uint8_t> get_l2cap_payload() const;

  uint16_t get_l2cap_cid() const;


  // Returns a fragmented vector of L2capSdu objects if successful
  // Returns a fragmented vector of L2capSdu objects if successful
  // Returns an empty vector of L2capSdu objects if unsuccessful
  // Returns an empty vector of L2capSdu objects if unsuccessful
  std::vector<std::unique_ptr<L2capSdu> > fragment(uint16_t maximum_sdu_size,
  std::vector<std::shared_ptr<L2capSdu> > fragment(uint16_t maximum_sdu_size,
                                                   uint8_t txseq,
                                                   uint8_t txseq,
                                                   uint8_t reqseq) const;
                                                   uint8_t reqseq);

  uint16_t get_l2cap_cid() const;


  // HciPacket Functions
  // HciPacket Functions
  size_t get_length();
  size_t get_length();
@@ -60,14 +55,8 @@ class L2capPacket : public HciPacket {
  // Entire L2CAP packet: length, CID, and payload in that order.
  // Entire L2CAP packet: length, CID, and payload in that order.
  std::vector<uint8_t> l2cap_packet_;
  std::vector<uint8_t> l2cap_packet_;


  // Returns an iterator to the beginning of the L2CAP payload on success.
  std::vector<uint8_t>::const_iterator get_l2cap_payload_begin() const;

  DISALLOW_COPY_AND_ASSIGN(L2capPacket);
  DISALLOW_COPY_AND_ASSIGN(L2capPacket);


  // Returns an iterator to the end of the L2CAP payload.
  std::vector<uint8_t>::const_iterator get_l2cap_payload_end() const;

  // Helper functions for fragmenting.
  // Helper functions for fragmenting.
  static void set_sdu_header_length(std::vector<uint8_t>& sdu, uint16_t length);
  static void set_sdu_header_length(std::vector<uint8_t>& sdu, uint16_t length);


+9 −18
Original line number Original line Diff line number Diff line
@@ -20,6 +20,8 @@
#include <iterator>
#include <iterator>
#include <vector>
#include <vector>


#include "hci_packet.h"

namespace test_vendor_lib {
namespace test_vendor_lib {


// Abstract representation of an SDU packet that contains an L2CAP
// Abstract representation of an SDU packet that contains an L2CAP
@@ -57,32 +59,17 @@ namespace test_vendor_lib {
// L2CAP packet will not include either of the control or FCS
// L2CAP packet will not include either of the control or FCS
// bytes.
// bytes.
//
//
class L2capSdu {
class L2capSdu : public HciPacket {
 public:
 public:
  // Returns a unique_ptr to an L2capSdu object that is constructed with the
  // Returns a unique_ptr to an L2capSdu object that is constructed with the
  // assumption that the SDU packet is complete and correct.
  // assumption that the SDU packet is complete and correct.
  static std::unique_ptr<L2capSdu> L2capSduConstructor(
  static std::shared_ptr<L2capSdu> L2capSduConstructor(
      std::vector<uint8_t> create_from);
      std::vector<uint8_t> create_from);


  // Adds an FCS to create_from and returns a unique_ptr to an L2capSdu object.
  // Adds an FCS to create_from and returns a unique_ptr to an L2capSdu object.
  static std::unique_ptr<L2capSdu> L2capSduBuilder(
  static std::shared_ptr<L2capSdu> L2capSduBuilder(
      std::vector<uint8_t> create_from);
      std::vector<uint8_t> create_from);


  // Get a vector iterator that points to the first byte of the
  // L2CAP payload within an SDU. The offset parameter will be the
  // number of bytes that are in the SDU header. This should always
  // be 6 bytes with the exception being the first SDU of a stream
  // of SDU packets where the first SDU packet will have an extra
  // two bytes and the offset should be 8 bytes.
  std::vector<uint8_t>::const_iterator get_payload_begin(
      const unsigned int offset) const;

  // Get a vector iterator that points to the last bytes of the
  // L2CAP payload within an SDU packet. There is no offset
  // parameter for this function because there will always be two
  // FCS bytes and nothing else at the end of each SDU.
  std::vector<uint8_t>::const_iterator get_payload_end() const;

  // Get the FCS bytes from the end of the L2CAP payload of an SDU
  // Get the FCS bytes from the end of the L2CAP payload of an SDU
  // packet.
  // packet.
  uint16_t get_fcs() const;
  uint16_t get_fcs() const;
@@ -114,6 +101,10 @@ class L2capSdu {
  // Reasembly is 10b, false otherwise.
  // Reasembly is 10b, false otherwise.
  static bool is_ending_sdu(const L2capSdu& sdu);
  static bool is_ending_sdu(const L2capSdu& sdu);


  // HciPacket functions
  size_t get_length();
  uint8_t& get_at_index(size_t index);

 private:
 private:
  // This is the SDU packet in bytes.
  // This is the SDU packet in bytes.
  std::vector<uint8_t> sdu_data_;
  std::vector<uint8_t> sdu_data_;
+19 −44
Original line number Original line Diff line number Diff line
@@ -25,17 +25,18 @@


namespace test_vendor_lib {
namespace test_vendor_lib {


const int kL2capHeaderLength = 4;
const size_t kL2capHeaderLength = 4;
const size_t kSduFirstHeaderLength = 8;
const size_t kSduStandardHeaderLength = 6;
const size_t kFcsBytes = 2;
const uint16_t kSduTxSeqBits = 0x007E;
const uint16_t kSduTxSeqBits = 0x007E;
const int kSduStandardHeaderLength = 6;
const int kSduFirstHeaderLength = 8;
const uint8_t kSduFirstReqseq = 0x40;
const uint8_t kSduFirstReqseq = 0x40;
const uint8_t kSduContinuationReqseq = 0xC0;
const uint8_t kSduContinuationReqseq = 0xC0;
const uint8_t kSduEndReqseq = 0x80;
const uint8_t kSduEndReqseq = 0x80;


std::unique_ptr<L2capPacket> L2capPacket::assemble(
std::shared_ptr<L2capPacket> L2capPacket::assemble(
    const std::vector<std::unique_ptr<L2capSdu> >& sdu_packets) {
    const std::vector<std::shared_ptr<L2capSdu> >& sdu_packets) {
  std::unique_ptr<L2capPacket> built_l2cap_packet(new L2capPacket());
  std::shared_ptr<L2capPacket> built_l2cap_packet(new L2capPacket());
  uint16_t l2cap_payload_length = 0;
  uint16_t l2cap_payload_length = 0;
  uint16_t first_packet_channel_id = 0;
  uint16_t first_packet_channel_id = 0;
  uint16_t total_expected_l2cap_length;
  uint16_t total_expected_l2cap_length;
@@ -93,7 +94,7 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble(
    // control bits set to 01b and the ending segment must have them set to 10b.
    // control bits set to 01b and the ending segment must have them set to 10b.
    // Meanwhile all segments in between the start and end must have the bits
    // Meanwhile all segments in between the start and end must have the bits
    // set to 11b.
    // set to 11b.
    uint16_t starting_index;
    size_t starting_index;
    uint8_t txseq = controls & kSduTxSeqBits;
    uint8_t txseq = controls & kSduTxSeqBits;
    if (sdu_packets.size() > 1 && i == 0 &&
    if (sdu_packets.size() > 1 && i == 0 &&
        !L2capSdu::is_starting_sdu(*sdu_packets[i])) {
        !L2capSdu::is_starting_sdu(*sdu_packets[i])) {
@@ -133,8 +134,8 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble(
      starting_index = kSduStandardHeaderLength;
      starting_index = kSduStandardHeaderLength;
    }
    }


    auto payload_begin = sdu_packets[i]->get_payload_begin(starting_index);
    Iterator payload_begin = sdu_packets[i]->get_begin() + starting_index;
    auto payload_end = sdu_packets[i]->get_payload_end();
    Iterator payload_end = sdu_packets[i]->get_end() - kFcsBytes;


    built_l2cap_packet->l2cap_packet_.insert(
    built_l2cap_packet->l2cap_packet_.insert(
        built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end);
        built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end);
@@ -156,40 +157,19 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble(
  return built_l2cap_packet;
  return built_l2cap_packet;
}  // Assemble
}  // Assemble


std::vector<uint8_t> L2capPacket::get_l2cap_payload() const {
  std::vector<uint8_t> payload_sub_vector;
  payload_sub_vector.clear();

  auto begin_payload_iter = get_l2cap_payload_begin();
  payload_sub_vector.insert(payload_sub_vector.end(), begin_payload_iter,
                            l2cap_packet_.end());

  return payload_sub_vector;
}

uint16_t L2capPacket::get_l2cap_cid() const {
uint16_t L2capPacket::get_l2cap_cid() const {
  return ((l2cap_packet_[3] << 8) | l2cap_packet_[2]);
  return ((l2cap_packet_[3] << 8) | l2cap_packet_[2]);
}
}


std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_begin()
std::vector<std::shared_ptr<L2capSdu> > L2capPacket::fragment(
    const {
    uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) {
  return std::next(l2cap_packet_.begin(), kSduHeaderLength);
  std::vector<std::shared_ptr<L2capSdu> > sdu;
}

std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_end()
    const {
  return l2cap_packet_.end();
}

std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment(
    uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) const {
  std::vector<std::unique_ptr<L2capSdu> > sdu;
  if (!check_l2cap_packet()) return sdu;
  if (!check_l2cap_packet()) return sdu;


  std::vector<uint8_t> current_sdu;
  std::vector<uint8_t> current_sdu;


  auto current_iter = get_l2cap_payload_begin();
  Iterator current_iter = get_begin() + kL2capHeaderLength;
  auto end_iter = get_l2cap_payload_end();
  Iterator end_iter = get_end();


  size_t number_of_packets = ceil((l2cap_packet_.size() - kL2capHeaderLength) /
  size_t number_of_packets = ceil((l2cap_packet_.size() - kL2capHeaderLength) /
                                  static_cast<float>(maximum_sdu_size));
                                  static_cast<float>(maximum_sdu_size));
@@ -230,8 +210,8 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment(
    return sdu;
    return sdu;
  }
  }


  auto next_iter =
  Iterator next_iter =
      std::next(current_iter, maximum_sdu_size - (kSduFirstHeaderLength + 2));
      current_iter + (maximum_sdu_size - (kSduFirstHeaderLength + 2));


  sdu.reserve(number_of_packets);
  sdu.reserve(number_of_packets);
  sdu.clear();
  sdu.clear();
@@ -269,15 +249,9 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment(
    if (txseq > 0x3F) txseq = 0x00;
    if (txseq > 0x3F) txseq = 0x00;


    current_sdu.insert(current_sdu.end(), current_iter, next_iter);
    current_sdu.insert(current_sdu.end(), current_iter, next_iter);

    current_iter = next_iter;
    current_iter = next_iter;


    next_iter =
    next_iter = current_iter + (maximum_sdu_size - kSduFirstHeaderLength);
        std::next(current_iter, maximum_sdu_size - kSduFirstHeaderLength);

    if (next_iter > end_iter) {
      next_iter = end_iter;
    }


    sdu.push_back(L2capSdu::L2capSduBuilder(std::move(current_sdu)));
    sdu.push_back(L2capSdu::L2capSduBuilder(std::move(current_sdu)));
  }
  }
@@ -323,4 +297,5 @@ size_t L2capPacket::get_length() { return l2cap_packet_.size(); }
uint8_t& L2capPacket::get_at_index(size_t index) {
uint8_t& L2capPacket::get_at_index(size_t index) {
  return l2cap_packet_[index];
  return l2cap_packet_[index];
}
}

}  // namespace test_vendor_lib
}  // namespace test_vendor_lib
+8 −13
Original line number Original line Diff line number Diff line
@@ -61,14 +61,14 @@ L2capSdu::L2capSdu(std::vector<uint8_t>&& create_from) {
  sdu_data_ = create_from;
  sdu_data_ = create_from;
}
}


std::unique_ptr<L2capSdu> L2capSdu::L2capSduConstructor(
std::shared_ptr<L2capSdu> L2capSdu::L2capSduConstructor(
    std::vector<uint8_t> create_from) {
    std::vector<uint8_t> create_from) {
  L2capSdu packet(std::move(create_from));
  L2capSdu packet(std::move(create_from));


  return std::make_unique<L2capSdu>(packet);
  return std::make_shared<L2capSdu>(packet);
}
}


std::unique_ptr<L2capSdu> L2capSdu::L2capSduBuilder(
std::shared_ptr<L2capSdu> L2capSdu::L2capSduBuilder(
    std::vector<uint8_t> create_from) {
    std::vector<uint8_t> create_from) {
  L2capSdu packet(std::move(create_from));
  L2capSdu packet(std::move(create_from));


@@ -79,16 +79,7 @@ std::unique_ptr<L2capSdu> L2capSdu::L2capSduBuilder(
  packet.sdu_data_[packet.sdu_data_.size() - 2] = fcs & 0xFF;
  packet.sdu_data_[packet.sdu_data_.size() - 2] = fcs & 0xFF;
  packet.sdu_data_[packet.sdu_data_.size() - 1] = (fcs & 0xFF00) >> 8;
  packet.sdu_data_[packet.sdu_data_.size() - 1] = (fcs & 0xFF00) >> 8;


  return std::make_unique<L2capSdu>(packet);
  return std::make_shared<L2capSdu>(packet);
}

std::vector<uint8_t>::const_iterator L2capSdu::get_payload_begin(
    const unsigned int offset) const {
  return std::next(sdu_data_.begin(), offset);
}

std::vector<uint8_t>::const_iterator L2capSdu::get_payload_end() const {
  return std::prev(sdu_data_.end(), 2);
}
}


uint16_t L2capSdu::convert_from_little_endian(
uint16_t L2capSdu::convert_from_little_endian(
@@ -152,4 +143,8 @@ bool L2capSdu::is_ending_sdu(const L2capSdu& sdu) {
  return (sar_bits == 0x8000);
  return (sar_bits == 0x8000);
}
}


// HciPacket functions.
size_t L2capSdu::get_length() { return sdu_data_.size(); }
uint8_t& L2capSdu::get_at_index(size_t index) { return sdu_data_[index]; }

}  // namespace test_vendor_lib
}  // namespace test_vendor_lib
Loading