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

Commit 26e54009 authored by johnshamoon's avatar johnshamoon
Browse files

test_vendor: Add HciPacket and Iterator classes

HciPacket is the abstract base class for all packet types.

Iterator is the custom iterator class that all packet types will use
to iterate through. The iterators in this class are bounded from
[start_of_packet, length_of_packet]. This prevents incrementing the iterator
passed the end sentinel.

Test: Passes all of the tests in test/iterator_test.cc
Change-Id: If9fec9ae32d9a23575ad3e9219b8249d164bc0d5
parent 9ec0799b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -17,7 +17,10 @@ cc_library_static {
        "src/device_factory.cc",
        "src/dual_mode_controller.cc",
        "src/event_packet.cc",
        "src/hci_packet.cc",
        "src/keyboard.cc",
        "src/l2cap_packet.cc",
        "src/l2cap_sdu.cc",
        "src/packet.cc",
        "src/packet_stream.cc",
        "src/sco_packet.cc",
@@ -58,6 +61,7 @@ cc_test_host {
    srcs: [
        "src/async_manager.cc",
        "src/bt_address.cc",
        "src/hci_packet.cc",
        "src/command_packet.cc",
        "src/event_packet.cc",
        "src/packet.cc",
+67 −0
Original line number Diff line number Diff line
#pragma once
#include <iterator>
#include <memory>

namespace test_vendor_lib {

// Iterator is a custom iterator class for HciPackets.
class Iterator
    : public std::iterator<std::random_access_iterator_tag, uint8_t> {
 public:
  Iterator(std::shared_ptr<class HciPacket> packet, size_t i);
  Iterator(const Iterator& itr);

  ~Iterator() {}

  operator bool() const;

  // All addition and subtraction operators are bounded from 0 to the length of
  // the packet.
  Iterator operator+(size_t offset);
  Iterator& operator+=(size_t offset);
  Iterator operator++(int);
  Iterator& operator++();

  Iterator operator-(size_t offset);
  int operator-(Iterator& itr);
  Iterator& operator-=(size_t offset);
  Iterator operator--(int);
  Iterator& operator--();

  Iterator& operator=(const Iterator& itr);

  bool operator!=(Iterator& itr);
  bool operator==(Iterator& itr);

  bool operator<(Iterator& itr);
  bool operator>(Iterator& itr);

  bool operator<=(Iterator& itr);
  bool operator>=(Iterator& itr);

  uint8_t& operator*() const;
  uint8_t* operator->() const;

 private:
  std::shared_ptr<class HciPacket> hci_packet_;
  size_t index_;

};  // Iterator

// HciPacket is an abstract class that will serve as the base class for all
// packet types.
class HciPacket : public std::enable_shared_from_this<HciPacket> {
 public:
  virtual ~HciPacket() = default;

  Iterator get_begin();
  Iterator get_end();

  uint8_t operator[](size_t i);

  virtual size_t get_length() = 0;

  virtual uint8_t& get_at_index(size_t index) = 0;
};  // HciPacket

};  // namespace test_vendor_lib
+6 −1
Original line number Diff line number Diff line
@@ -24,13 +24,14 @@
#include <vector>

#include "base/macros.h"
#include "hci_packet.h"
#include "l2cap_sdu.h"

namespace test_vendor_lib {

const int kSduHeaderLength = 4;

class L2capPacket {
class L2capPacket : public HciPacket {
 public:
  // Returns an assembled L2cap object if successful, nullptr if failure.
  static std::unique_ptr<L2capPacket> assemble(
@@ -49,6 +50,10 @@ class L2capPacket {
                                                   uint8_t txseq,
                                                   uint8_t reqseq) const;

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

 private:
  L2capPacket() = default;

+123 −0
Original line number Diff line number Diff line
#include "hci_packet.h"

namespace test_vendor_lib {

// Iterator Functions
Iterator::Iterator(std::shared_ptr<HciPacket> packet, size_t i) {
  hci_packet_ = packet;
  index_ = i;
}

Iterator::Iterator(const Iterator& itr) { *this = itr; }

Iterator::operator bool() const { return hci_packet_ != nullptr; }

Iterator Iterator::operator+(size_t offset) {
  auto itr(*this);

  return itr += offset;
}

Iterator& Iterator::operator+=(size_t offset) {
  size_t new_offset = index_ + offset;
  index_ = new_offset >= hci_packet_->get_length() ? hci_packet_->get_length()
                                                   : new_offset;
  return *this;
}

Iterator Iterator::operator++(int) {
  auto itr(*this);
  index_++;

  if (index_ >= hci_packet_->get_length()) index_ = hci_packet_->get_length();

  return itr;
}

Iterator& Iterator::operator++() {
  index_++;

  if (index_ >= hci_packet_->get_length()) index_ = hci_packet_->get_length();

  return *this;
}

Iterator Iterator::operator-(size_t offset) {
  auto itr(*this);

  return itr -= offset;
}

int Iterator::operator-(Iterator& itr) { return index_ - itr.index_; }

Iterator& Iterator::operator-=(size_t offset) {
  index_ = offset > index_ ? 0 : index_ - offset;

  return *this;
}

Iterator Iterator::operator--(int) {
  auto itr(*this);
  if (index_ != 0) index_--;

  return itr;
}

Iterator& Iterator::operator--() {
  if (index_ != 0) index_--;

  return *this;
}

Iterator& Iterator::operator=(const Iterator& itr) {
  hci_packet_ = itr.hci_packet_;
  index_ = itr.index_;

  return *this;
}

bool Iterator::operator==(Iterator& itr) {
  return ((hci_packet_ == itr.hci_packet_) && (index_ == itr.index_));
}

bool Iterator::operator!=(Iterator& itr) { return !(*this == itr); }

bool Iterator::operator<(Iterator& itr) {
  return ((hci_packet_ == itr.hci_packet_) && (index_ < itr.index_));
}

bool Iterator::operator>(Iterator& itr) {
  return ((hci_packet_ == itr.hci_packet_) && (index_ > itr.index_));
}

bool Iterator::operator<=(Iterator& itr) {
  return ((hci_packet_ == itr.hci_packet_) && (index_ <= itr.index_));
}

bool Iterator::operator>=(Iterator& itr) {
  return ((hci_packet_ == itr.hci_packet_) && (index_ >= itr.index_));
}

uint8_t& Iterator::operator*() const {
  return hci_packet_->get_at_index(index_);
}

uint8_t* Iterator::operator->() const {
  return &hci_packet_->get_at_index(index_);
}

// BtPacket Functions
Iterator HciPacket::get_begin() {
  Iterator itr(shared_from_this(), 0);

  return itr;
}

Iterator HciPacket::get_end() {
  Iterator itr(shared_from_this(), get_length());

  return itr;
}

uint8_t HciPacket::operator[](size_t i) { return get_at_index(i); }
};  // namespace test_vendor_lib
+6 −0
Original line number Diff line number Diff line
@@ -317,4 +317,10 @@ bool L2capPacket::check_l2cap_packet() const {
  return true;
}

// HciPacket Functions
size_t L2capPacket::get_length() { return l2cap_packet_.size(); }

uint8_t& L2capPacket::get_at_index(size_t index) {
  return l2cap_packet_[index];
}
}  // namespace test_vendor_lib