Loading system/vendor_libs/test_vendor_lib/include/hci_packet.h +1 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ class HciPacket : public std::enable_shared_from_this<HciPacket> { virtual size_t get_length() = 0; virtual uint8_t& get_at_index(size_t index) = 0; }; // HciPacket }; // namespace test_vendor_lib system/vendor_libs/test_vendor_lib/include/l2cap_packet.h +6 −17 Original line number Diff line number Diff line Loading @@ -34,21 +34,16 @@ const int kSduHeaderLength = 4; class L2capPacket : public HciPacket { public: // Returns an assembled L2cap object if successful, nullptr if failure. static std::unique_ptr<L2capPacket> assemble( const std::vector<std::unique_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; static std::shared_ptr<L2capPacket> assemble( const std::vector<std::shared_ptr<L2capSdu> >& sdu_packet); // Returns a fragmented vector of L2capSdu objects if successful // 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 reqseq) const; uint8_t reqseq); uint16_t get_l2cap_cid() const; // HciPacket Functions size_t get_length(); Loading @@ -60,14 +55,8 @@ class L2capPacket : public HciPacket { // Entire L2CAP packet: length, CID, and payload in that order. 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); // 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. static void set_sdu_header_length(std::vector<uint8_t>& sdu, uint16_t length); Loading system/vendor_libs/test_vendor_lib/include/l2cap_sdu.h +9 −18 Original line number Diff line number Diff line Loading @@ -20,6 +20,8 @@ #include <iterator> #include <vector> #include "hci_packet.h" namespace test_vendor_lib { // Abstract representation of an SDU packet that contains an L2CAP Loading Loading @@ -57,32 +59,17 @@ namespace test_vendor_lib { // L2CAP packet will not include either of the control or FCS // bytes. // class L2capSdu { class L2capSdu : public HciPacket { public: // Returns a unique_ptr to an L2capSdu object that is constructed with the // 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); // 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); // 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 // packet. uint16_t get_fcs() const; Loading Loading @@ -114,6 +101,10 @@ class L2capSdu { // Reasembly is 10b, false otherwise. static bool is_ending_sdu(const L2capSdu& sdu); // HciPacket functions size_t get_length(); uint8_t& get_at_index(size_t index); private: // This is the SDU packet in bytes. std::vector<uint8_t> sdu_data_; Loading system/vendor_libs/test_vendor_lib/src/l2cap_packet.cc +19 −44 Original line number Diff line number Diff line Loading @@ -25,17 +25,18 @@ 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 int kSduStandardHeaderLength = 6; const int kSduFirstHeaderLength = 8; const uint8_t kSduFirstReqseq = 0x40; const uint8_t kSduContinuationReqseq = 0xC0; const uint8_t kSduEndReqseq = 0x80; std::unique_ptr<L2capPacket> L2capPacket::assemble( const std::vector<std::unique_ptr<L2capSdu> >& sdu_packets) { std::unique_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); std::shared_ptr<L2capPacket> L2capPacket::assemble( const std::vector<std::shared_ptr<L2capSdu> >& sdu_packets) { std::shared_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); uint16_t l2cap_payload_length = 0; uint16_t first_packet_channel_id = 0; uint16_t total_expected_l2cap_length; Loading Loading @@ -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. // Meanwhile all segments in between the start and end must have the bits // set to 11b. uint16_t starting_index; size_t starting_index; uint8_t txseq = controls & kSduTxSeqBits; if (sdu_packets.size() > 1 && i == 0 && !L2capSdu::is_starting_sdu(*sdu_packets[i])) { Loading Loading @@ -133,8 +134,8 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( starting_index = kSduStandardHeaderLength; } auto payload_begin = sdu_packets[i]->get_payload_begin(starting_index); auto payload_end = sdu_packets[i]->get_payload_end(); Iterator payload_begin = sdu_packets[i]->get_begin() + starting_index; Iterator payload_end = sdu_packets[i]->get_end() - kFcsBytes; built_l2cap_packet->l2cap_packet_.insert( built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end); Loading @@ -156,40 +157,19 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( return built_l2cap_packet; } // 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 { return ((l2cap_packet_[3] << 8) | l2cap_packet_[2]); } std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_begin() const { return std::next(l2cap_packet_.begin(), kSduHeaderLength); } 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; std::vector<std::shared_ptr<L2capSdu> > L2capPacket::fragment( uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) { std::vector<std::shared_ptr<L2capSdu> > sdu; if (!check_l2cap_packet()) return sdu; std::vector<uint8_t> current_sdu; auto current_iter = get_l2cap_payload_begin(); auto end_iter = get_l2cap_payload_end(); Iterator current_iter = get_begin() + kL2capHeaderLength; Iterator end_iter = get_end(); size_t number_of_packets = ceil((l2cap_packet_.size() - kL2capHeaderLength) / static_cast<float>(maximum_sdu_size)); Loading Loading @@ -230,8 +210,8 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment( return sdu; } auto next_iter = std::next(current_iter, maximum_sdu_size - (kSduFirstHeaderLength + 2)); Iterator next_iter = current_iter + (maximum_sdu_size - (kSduFirstHeaderLength + 2)); sdu.reserve(number_of_packets); sdu.clear(); Loading Loading @@ -269,15 +249,9 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment( if (txseq > 0x3F) txseq = 0x00; current_sdu.insert(current_sdu.end(), current_iter, next_iter); current_iter = next_iter; next_iter = std::next(current_iter, maximum_sdu_size - kSduFirstHeaderLength); if (next_iter > end_iter) { next_iter = end_iter; } next_iter = current_iter + (maximum_sdu_size - kSduFirstHeaderLength); sdu.push_back(L2capSdu::L2capSduBuilder(std::move(current_sdu))); } Loading Loading @@ -323,4 +297,5 @@ 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 system/vendor_libs/test_vendor_lib/src/l2cap_sdu.cc +8 −13 Original line number Diff line number Diff line Loading @@ -61,14 +61,14 @@ L2capSdu::L2capSdu(std::vector<uint8_t>&& create_from) { sdu_data_ = create_from; } std::unique_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::shared_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::vector<uint8_t> 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) { L2capSdu packet(std::move(create_from)); Loading @@ -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() - 1] = (fcs & 0xFF00) >> 8; return std::make_unique<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); return std::make_shared<L2capSdu>(packet); } uint16_t L2capSdu::convert_from_little_endian( Loading Loading @@ -152,4 +143,8 @@ bool L2capSdu::is_ending_sdu(const L2capSdu& sdu) { 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 Loading
system/vendor_libs/test_vendor_lib/include/hci_packet.h +1 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ class HciPacket : public std::enable_shared_from_this<HciPacket> { virtual size_t get_length() = 0; virtual uint8_t& get_at_index(size_t index) = 0; }; // HciPacket }; // namespace test_vendor_lib
system/vendor_libs/test_vendor_lib/include/l2cap_packet.h +6 −17 Original line number Diff line number Diff line Loading @@ -34,21 +34,16 @@ const int kSduHeaderLength = 4; class L2capPacket : public HciPacket { public: // Returns an assembled L2cap object if successful, nullptr if failure. static std::unique_ptr<L2capPacket> assemble( const std::vector<std::unique_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; static std::shared_ptr<L2capPacket> assemble( const std::vector<std::shared_ptr<L2capSdu> >& sdu_packet); // Returns a fragmented vector of L2capSdu objects if successful // 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 reqseq) const; uint8_t reqseq); uint16_t get_l2cap_cid() const; // HciPacket Functions size_t get_length(); Loading @@ -60,14 +55,8 @@ class L2capPacket : public HciPacket { // Entire L2CAP packet: length, CID, and payload in that order. 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); // 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. static void set_sdu_header_length(std::vector<uint8_t>& sdu, uint16_t length); Loading
system/vendor_libs/test_vendor_lib/include/l2cap_sdu.h +9 −18 Original line number Diff line number Diff line Loading @@ -20,6 +20,8 @@ #include <iterator> #include <vector> #include "hci_packet.h" namespace test_vendor_lib { // Abstract representation of an SDU packet that contains an L2CAP Loading Loading @@ -57,32 +59,17 @@ namespace test_vendor_lib { // L2CAP packet will not include either of the control or FCS // bytes. // class L2capSdu { class L2capSdu : public HciPacket { public: // Returns a unique_ptr to an L2capSdu object that is constructed with the // 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); // 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); // 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 // packet. uint16_t get_fcs() const; Loading Loading @@ -114,6 +101,10 @@ class L2capSdu { // Reasembly is 10b, false otherwise. static bool is_ending_sdu(const L2capSdu& sdu); // HciPacket functions size_t get_length(); uint8_t& get_at_index(size_t index); private: // This is the SDU packet in bytes. std::vector<uint8_t> sdu_data_; Loading
system/vendor_libs/test_vendor_lib/src/l2cap_packet.cc +19 −44 Original line number Diff line number Diff line Loading @@ -25,17 +25,18 @@ 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 int kSduStandardHeaderLength = 6; const int kSduFirstHeaderLength = 8; const uint8_t kSduFirstReqseq = 0x40; const uint8_t kSduContinuationReqseq = 0xC0; const uint8_t kSduEndReqseq = 0x80; std::unique_ptr<L2capPacket> L2capPacket::assemble( const std::vector<std::unique_ptr<L2capSdu> >& sdu_packets) { std::unique_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); std::shared_ptr<L2capPacket> L2capPacket::assemble( const std::vector<std::shared_ptr<L2capSdu> >& sdu_packets) { std::shared_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); uint16_t l2cap_payload_length = 0; uint16_t first_packet_channel_id = 0; uint16_t total_expected_l2cap_length; Loading Loading @@ -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. // Meanwhile all segments in between the start and end must have the bits // set to 11b. uint16_t starting_index; size_t starting_index; uint8_t txseq = controls & kSduTxSeqBits; if (sdu_packets.size() > 1 && i == 0 && !L2capSdu::is_starting_sdu(*sdu_packets[i])) { Loading Loading @@ -133,8 +134,8 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( starting_index = kSduStandardHeaderLength; } auto payload_begin = sdu_packets[i]->get_payload_begin(starting_index); auto payload_end = sdu_packets[i]->get_payload_end(); Iterator payload_begin = sdu_packets[i]->get_begin() + starting_index; Iterator payload_end = sdu_packets[i]->get_end() - kFcsBytes; built_l2cap_packet->l2cap_packet_.insert( built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end); Loading @@ -156,40 +157,19 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( return built_l2cap_packet; } // 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 { return ((l2cap_packet_[3] << 8) | l2cap_packet_[2]); } std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_begin() const { return std::next(l2cap_packet_.begin(), kSduHeaderLength); } 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; std::vector<std::shared_ptr<L2capSdu> > L2capPacket::fragment( uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) { std::vector<std::shared_ptr<L2capSdu> > sdu; if (!check_l2cap_packet()) return sdu; std::vector<uint8_t> current_sdu; auto current_iter = get_l2cap_payload_begin(); auto end_iter = get_l2cap_payload_end(); Iterator current_iter = get_begin() + kL2capHeaderLength; Iterator end_iter = get_end(); size_t number_of_packets = ceil((l2cap_packet_.size() - kL2capHeaderLength) / static_cast<float>(maximum_sdu_size)); Loading Loading @@ -230,8 +210,8 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment( return sdu; } auto next_iter = std::next(current_iter, maximum_sdu_size - (kSduFirstHeaderLength + 2)); Iterator next_iter = current_iter + (maximum_sdu_size - (kSduFirstHeaderLength + 2)); sdu.reserve(number_of_packets); sdu.clear(); Loading Loading @@ -269,15 +249,9 @@ std::vector<std::unique_ptr<L2capSdu> > L2capPacket::fragment( if (txseq > 0x3F) txseq = 0x00; current_sdu.insert(current_sdu.end(), current_iter, next_iter); current_iter = next_iter; next_iter = std::next(current_iter, maximum_sdu_size - kSduFirstHeaderLength); if (next_iter > end_iter) { next_iter = end_iter; } next_iter = current_iter + (maximum_sdu_size - kSduFirstHeaderLength); sdu.push_back(L2capSdu::L2capSduBuilder(std::move(current_sdu))); } Loading Loading @@ -323,4 +297,5 @@ 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
system/vendor_libs/test_vendor_lib/src/l2cap_sdu.cc +8 −13 Original line number Diff line number Diff line Loading @@ -61,14 +61,14 @@ L2capSdu::L2capSdu(std::vector<uint8_t>&& create_from) { sdu_data_ = create_from; } std::unique_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::shared_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::vector<uint8_t> 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) { L2capSdu packet(std::move(create_from)); Loading @@ -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() - 1] = (fcs & 0xFF00) >> 8; return std::make_unique<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); return std::make_shared<L2capSdu>(packet); } uint16_t L2capSdu::convert_from_little_endian( Loading Loading @@ -152,4 +143,8 @@ bool L2capSdu::is_ending_sdu(const L2capSdu& sdu) { 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