1 #include "hci_packet.h" 2 3 namespace test_vendor_lib { 4 5 // Iterator Functions 6 Iterator::Iterator(std::shared_ptr<HciPacket> packet, size_t i) { 7 hci_packet_ = packet; 8 index_ = i; 9 } 10 11 Iterator::Iterator(const Iterator& itr) { *this = itr; } 12 13 Iterator::operator bool() const { return hci_packet_ != nullptr; } 14 15 Iterator Iterator::operator+(size_t offset) { 16 auto itr(*this); 17 18 return itr += offset; 19 } 20 21 Iterator& Iterator::operator+=(size_t offset) { 22 size_t new_offset = index_ + offset; 23 index_ = new_offset >= hci_packet_->get_length() ? hci_packet_->get_length() 24 : new_offset; 25 return *this; 26 } 27 28 Iterator Iterator::operator++(int) { 29 auto itr(*this); 30 index_++; 31 32 if (index_ >= hci_packet_->get_length()) index_ = hci_packet_->get_length(); 33 34 return itr; 35 } 36 37 Iterator& Iterator::operator++() { 38 index_++; 39 40 if (index_ >= hci_packet_->get_length()) index_ = hci_packet_->get_length(); 41 42 return *this; 43 } 44 45 Iterator Iterator::operator-(size_t offset) { 46 auto itr(*this); 47 48 return itr -= offset; 49 } 50 51 int Iterator::operator-(Iterator& itr) { return index_ - itr.index_; } 52 53 Iterator& Iterator::operator-=(size_t offset) { 54 index_ = offset > index_ ? 0 : index_ - offset; 55 56 return *this; 57 } 58 59 Iterator Iterator::operator--(int) { 60 auto itr(*this); 61 if (index_ != 0) index_--; 62 63 return itr; 64 } 65 66 Iterator& Iterator::operator--() { 67 if (index_ != 0) index_--; 68 69 return *this; 70 } 71 72 Iterator& Iterator::operator=(const Iterator& itr) { 73 hci_packet_ = itr.hci_packet_; 74 index_ = itr.index_; 75 76 return *this; 77 } 78 79 bool Iterator::operator==(Iterator& itr) { 80 return ((hci_packet_ == itr.hci_packet_) && (index_ == itr.index_)); 81 } 82 83 bool Iterator::operator!=(Iterator& itr) { return !(*this == itr); } 84 85 bool Iterator::operator<(Iterator& itr) { 86 return ((hci_packet_ == itr.hci_packet_) && (index_ < itr.index_)); 87 } 88 89 bool Iterator::operator>(Iterator& itr) { 90 return ((hci_packet_ == itr.hci_packet_) && (index_ > itr.index_)); 91 } 92 93 bool Iterator::operator<=(Iterator& itr) { 94 return ((hci_packet_ == itr.hci_packet_) && (index_ <= itr.index_)); 95 } 96 97 bool Iterator::operator>=(Iterator& itr) { 98 return ((hci_packet_ == itr.hci_packet_) && (index_ >= itr.index_)); 99 } 100 101 uint8_t& Iterator::operator*() const { 102 CHECK(index_ != hci_packet_->get_length()); 103 104 return hci_packet_->get_at_index(index_); 105 } 106 107 uint8_t* Iterator::operator->() const { 108 return &hci_packet_->get_at_index(index_); 109 } 110 111 // BtPacket Functions 112 Iterator HciPacket::get_begin() { 113 Iterator itr(shared_from_this(), 0); 114 115 return itr; 116 } 117 118 Iterator HciPacket::get_end() { 119 Iterator itr(shared_from_this(), get_length()); 120 121 return itr; 122 } 123 124 uint8_t HciPacket::operator[](size_t i) { return get_at_index(i); } 125 }; // namespace test_vendor_lib 126