Loading system/vendor_libs/test_vendor_lib/include/l2cap_packet.h +4 −3 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ class L2capPacket { public: // Returns an assembled L2cap object if successful, nullptr if failure. static std::unique_ptr<L2capPacket> assemble( const std::vector<L2capSdu>& sdu_packet); 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. Loading @@ -45,7 +45,8 @@ class L2capPacket { // Returns a fragmented vector of L2capSdu objects if successful // Returns an empty vector of L2capSdu objects if unsuccessful std::vector<L2capSdu> fragment(uint16_t maximum_sdu_size, uint8_t txseq, std::vector<std::unique_ptr<L2capSdu> > fragment(uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) const; private: Loading system/vendor_libs/test_vendor_lib/include/l2cap_sdu.h +10 −12 Original line number Diff line number Diff line Loading @@ -59,19 +59,14 @@ namespace test_vendor_lib { // class L2capSdu { public: // Returns a completed L2capSdu object. L2capSdu(std::vector<uint8_t> create_from); static L2capSdu L2capSduBuilder(std::vector<uint8_t> create_from); // TODO: Remove this when the move to L2capSdu* is done L2capSdu& operator=(L2capSdu obj1) { sdu_data_.clear(); // 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( std::vector<uint8_t> create_from); sdu_data_ = obj1.sdu_data_; return *this; } // Adds an FCS to create_from and returns a unique_ptr to an L2capSdu object. static std::unique_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 Loading Loading @@ -123,6 +118,9 @@ class L2capSdu { // This is the SDU packet in bytes. std::vector<uint8_t> sdu_data_; // Returns a completed L2capSdu object. L2capSdu(std::vector<uint8_t>&& create_from); // Table for precalculated lfsr values. static const uint16_t lfsr_table_[256]; Loading system/vendor_libs/test_vendor_lib/src/l2cap_packet.cc +20 −20 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ const uint8_t kSduContinuationReqseq = 0xC0; const uint8_t kSduEndReqseq = 0x80; std::unique_ptr<L2capPacket> L2capPacket::assemble( const std::vector<L2capSdu>& sdu_packets) { const std::vector<std::unique_ptr<L2capSdu> >& sdu_packets) { std::unique_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); uint16_t l2cap_payload_length = 0; uint16_t first_packet_channel_id = 0; Loading @@ -45,17 +45,18 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( LOG_DEBUG(LOG_TAG, "%s: No SDU received.", __func__); return nullptr; } if (sdu_packets.size() == 1 && !L2capSdu::is_complete_l2cap(sdu_packets[0])) { if (sdu_packets.size() == 1 && !L2capSdu::is_complete_l2cap(*sdu_packets[0])) { LOG_DEBUG(LOG_TAG, "%s: Unsegmented SDU has incorrect SAR bits.", __func__); return nullptr; } first_packet_channel_id = sdu_packets[0].get_channel_id(); first_packet_channel_id = sdu_packets[0]->get_channel_id(); built_l2cap_packet->l2cap_packet_.resize(kL2capHeaderLength); for (size_t i = 0; i < sdu_packets.size(); i++) { uint16_t payload_length = sdu_packets[i].get_payload_length(); uint16_t payload_length = sdu_packets[i]->get_payload_length(); // TODO(jruthe): Remove these checks when ACL packets have been // implemented. Once those are done, that will be the only way to create Loading @@ -64,21 +65,21 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( // Check the integrity of the packet length, if it is zero, it is invalid. // The maximum size of a single, segmented L2CAP payload is 1016 bytes. if ((payload_length <= 0) || (payload_length != sdu_packets[i].get_vector_size() - 4)) { (payload_length != sdu_packets[i]->get_vector_size() - 4)) { LOG_DEBUG(LOG_TAG, "%s: SDU payload length incorrect.", __func__); return nullptr; } uint16_t fcs_check = sdu_packets[i].get_fcs(); uint16_t fcs_check = sdu_packets[i]->get_fcs(); if (sdu_packets[i].calculate_fcs() != fcs_check) { if (sdu_packets[i]->calculate_fcs() != fcs_check) { LOG_DEBUG(LOG_TAG, "%s: SDU fcs is incorrect.", __func__); return nullptr; } uint16_t controls = sdu_packets[i].get_controls(); uint16_t controls = sdu_packets[i]->get_controls(); if (sdu_packets[i].get_channel_id() != first_packet_channel_id) { if (sdu_packets[i]->get_channel_id() != first_packet_channel_id) { LOG_DEBUG(LOG_TAG, "%s: SDU CID does not match expected.", __func__); return nullptr; } Loading @@ -95,12 +96,12 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( uint16_t starting_index; uint8_t txseq = controls & kSduTxSeqBits; if (sdu_packets.size() > 1 && i == 0 && !L2capSdu::is_starting_sdu(sdu_packets[i])) { !L2capSdu::is_starting_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: First segmented SDU has incorrect SAR bits.", __func__); return nullptr; } if (i != 0 && L2capSdu::is_starting_sdu(sdu_packets[i])) { if (i != 0 && L2capSdu::is_starting_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: SAR bits set to first segmented SDU on " "non-starting SDU.", Loading @@ -112,7 +113,7 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( return nullptr; } if (sdu_packets.size() > 1 && i == sdu_packets.size() - 1 && !L2capSdu::is_ending_sdu(sdu_packets[i])) { !L2capSdu::is_ending_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: Final segmented SDU has incorrect SAR bits.", __func__); return nullptr; Loading @@ -121,9 +122,9 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( // Subtract the control and fcs from every SDU payload length. l2cap_payload_length += (payload_length - 4); if (L2capSdu::is_starting_sdu(sdu_packets[i])) { if (L2capSdu::is_starting_sdu(*sdu_packets[i])) { starting_index = kSduFirstHeaderLength; total_expected_l2cap_length = sdu_packets[i].get_total_l2cap_length(); total_expected_l2cap_length = sdu_packets[i]->get_total_l2cap_length(); // Subtract the additional two bytes from the first packet of a segmented // SDU. Loading @@ -132,8 +133,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(); auto payload_begin = sdu_packets[i]->get_payload_begin(starting_index); auto payload_end = sdu_packets[i]->get_payload_end(); built_l2cap_packet->l2cap_packet_.insert( built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end); Loading Loading @@ -180,10 +181,9 @@ std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_end() return l2cap_packet_.end(); } std::vector<L2capSdu> L2capPacket::fragment(uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) const { std::vector<L2capSdu> sdu; 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; std::vector<uint8_t> current_sdu; Loading system/vendor_libs/test_vendor_lib/src/l2cap_sdu.cc +12 −4 Original line number Diff line number Diff line Loading @@ -57,11 +57,19 @@ const uint16_t L2capSdu::lfsr_table_[256] = { 0x4100, 0x81c1, 0x8081, 0x4040, }; // lfsr_table L2capSdu::L2capSdu(std::vector<uint8_t> create_from) { sdu_data_ = std::move(create_from); L2capSdu::L2capSdu(std::vector<uint8_t>&& create_from) { sdu_data_ = create_from; } L2capSdu L2capSdu::L2capSduBuilder(std::vector<uint8_t> create_from) { std::unique_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::vector<uint8_t> create_from) { L2capSdu packet(std::move(create_from)); return std::make_unique<L2capSdu>(packet); } std::unique_ptr<L2capSdu> L2capSdu::L2capSduBuilder( std::vector<uint8_t> create_from) { L2capSdu packet(std::move(create_from)); packet.sdu_data_.resize(packet.sdu_data_.size() + 2, 0x00); Loading @@ -71,7 +79,7 @@ L2capSdu L2capSdu::L2capSduBuilder(std::vector<uint8_t> create_from) { packet.sdu_data_[packet.sdu_data_.size() - 2] = fcs & 0xFF; packet.sdu_data_[packet.sdu_data_.size() - 1] = (fcs & 0xFF00) >> 8; return packet; return std::make_unique<L2capSdu>(packet); } std::vector<uint8_t>::const_iterator L2capSdu::get_payload_begin( Loading system/vendor_libs/test_vendor_lib/test/l2cap_sdu_test.cc +77 −64 Original line number Diff line number Diff line Loading @@ -16,21 +16,34 @@ * ******************************************************************************/ #include "l2cap_sdu.h" #include <gtest/gtest.h> #include <memory> #include "l2cap_test_packets.h" using std::vector; namespace test_vendor_lib { L2capSdu packet_1(l2cap_test_packet_1); L2capSdu packet_2(l2cap_test_packet_2); L2capSdu packet_3(l2cap_test_packet_3); L2capSdu packet_4(l2cap_test_packet_4); L2capSdu packet_5(l2cap_test_packet_5); L2capSdu packet_6(l2cap_test_packet_6); L2capSdu packet_7(l2cap_test_packet_7); L2capSdu packet_8(l2cap_test_packet_8); L2capSdu packet_9(l2cap_test_packet_9); std::unique_ptr<L2capSdu> packet_1 = L2capSdu::L2capSduConstructor(l2cap_test_packet_1); std::unique_ptr<L2capSdu> packet_2 = L2capSdu::L2capSduConstructor(l2cap_test_packet_2); std::unique_ptr<L2capSdu> packet_3 = L2capSdu::L2capSduConstructor(l2cap_test_packet_3); std::unique_ptr<L2capSdu> packet_4 = L2capSdu::L2capSduConstructor(l2cap_test_packet_4); std::unique_ptr<L2capSdu> packet_5 = L2capSdu::L2capSduConstructor(l2cap_test_packet_5); std::unique_ptr<L2capSdu> packet_6 = L2capSdu::L2capSduConstructor(l2cap_test_packet_6); std::unique_ptr<L2capSdu> packet_7 = L2capSdu::L2capSduConstructor(l2cap_test_packet_7); std::unique_ptr<L2capSdu> packet_8 = L2capSdu::L2capSduConstructor(l2cap_test_packet_8); std::unique_ptr<L2capSdu> packet_9 = L2capSdu::L2capSduConstructor(l2cap_test_packet_9); class L2capSduTest : public ::testing::Test { public: Loading @@ -41,79 +54,79 @@ class L2capSduTest : public ::testing::Test { }; // L2capSduTest TEST_F(L2capSduTest, getFcsTest) { EXPECT_EQ(0x72aa, packet_1.get_fcs()); EXPECT_EQ(0x5b57, packet_2.get_fcs()); EXPECT_EQ(0xe644, packet_3.get_fcs()); EXPECT_EQ(0x21b0, packet_4.get_fcs()); EXPECT_EQ(0xae96, packet_5.get_fcs()); EXPECT_EQ(0x9254, packet_6.get_fcs()); EXPECT_EQ(0xf6fa, packet_7.get_fcs()); EXPECT_EQ(0x1da4, packet_8.get_fcs()); EXPECT_EQ(0x781a, packet_9.get_fcs()); EXPECT_EQ(0x72aa, packet_1->get_fcs()); EXPECT_EQ(0x5b57, packet_2->get_fcs()); EXPECT_EQ(0xe644, packet_3->get_fcs()); EXPECT_EQ(0x21b0, packet_4->get_fcs()); EXPECT_EQ(0xae96, packet_5->get_fcs()); EXPECT_EQ(0x9254, packet_6->get_fcs()); EXPECT_EQ(0xf6fa, packet_7->get_fcs()); EXPECT_EQ(0x1da4, packet_8->get_fcs()); EXPECT_EQ(0x781a, packet_9->get_fcs()); } TEST_F(L2capSduTest, getPayloadLengthTest) { EXPECT_EQ(l2cap_test_packet_1.size() - 4, packet_1.get_payload_length()); EXPECT_EQ(l2cap_test_packet_2.size() - 4, packet_2.get_payload_length()); EXPECT_EQ(l2cap_test_packet_3.size() - 4, packet_3.get_payload_length()); EXPECT_EQ(l2cap_test_packet_4.size() - 4, packet_4.get_payload_length()); EXPECT_EQ(l2cap_test_packet_5.size() - 4, packet_5.get_payload_length()); EXPECT_EQ(l2cap_test_packet_6.size() - 4, packet_6.get_payload_length()); EXPECT_EQ(l2cap_test_packet_7.size() - 4, packet_7.get_payload_length()); EXPECT_EQ(l2cap_test_packet_8.size() - 4, packet_8.get_payload_length()); EXPECT_EQ(l2cap_test_packet_9.size() - 4, packet_9.get_payload_length()); EXPECT_EQ(l2cap_test_packet_1.size() - 4, packet_1->get_payload_length()); EXPECT_EQ(l2cap_test_packet_2.size() - 4, packet_2->get_payload_length()); EXPECT_EQ(l2cap_test_packet_3.size() - 4, packet_3->get_payload_length()); EXPECT_EQ(l2cap_test_packet_4.size() - 4, packet_4->get_payload_length()); EXPECT_EQ(l2cap_test_packet_5.size() - 4, packet_5->get_payload_length()); EXPECT_EQ(l2cap_test_packet_6.size() - 4, packet_6->get_payload_length()); EXPECT_EQ(l2cap_test_packet_7.size() - 4, packet_7->get_payload_length()); EXPECT_EQ(l2cap_test_packet_8.size() - 4, packet_8->get_payload_length()); EXPECT_EQ(l2cap_test_packet_9.size() - 4, packet_9->get_payload_length()); } TEST_F(L2capSduTest, calculateFcsTest) { EXPECT_EQ(0x72aa, packet_1.calculate_fcs()); EXPECT_EQ(0x5b57, packet_2.calculate_fcs()); EXPECT_EQ(0xe644, packet_3.calculate_fcs()); EXPECT_EQ(0x21b0, packet_4.calculate_fcs()); EXPECT_EQ(0xae96, packet_5.calculate_fcs()); EXPECT_EQ(0x9254, packet_6.calculate_fcs()); EXPECT_EQ(0xf6fa, packet_7.calculate_fcs()); EXPECT_EQ(0x1da4, packet_8.calculate_fcs()); EXPECT_EQ(0x781a, packet_9.calculate_fcs()); EXPECT_EQ(0x72aa, packet_1->calculate_fcs()); EXPECT_EQ(0x5b57, packet_2->calculate_fcs()); EXPECT_EQ(0xe644, packet_3->calculate_fcs()); EXPECT_EQ(0x21b0, packet_4->calculate_fcs()); EXPECT_EQ(0xae96, packet_5->calculate_fcs()); EXPECT_EQ(0x9254, packet_6->calculate_fcs()); EXPECT_EQ(0xf6fa, packet_7->calculate_fcs()); EXPECT_EQ(0x1da4, packet_8->calculate_fcs()); EXPECT_EQ(0x781a, packet_9->calculate_fcs()); } TEST_F(L2capSduTest, getControlsTest) { EXPECT_EQ(0x4102, packet_1.get_controls()); EXPECT_EQ(0xc104, packet_2.get_controls()); EXPECT_EQ(0xc106, packet_3.get_controls()); EXPECT_EQ(0xc108, packet_4.get_controls()); EXPECT_EQ(0xc10a, packet_5.get_controls()); EXPECT_EQ(0xc10c, packet_6.get_controls()); EXPECT_EQ(0xc10e, packet_7.get_controls()); EXPECT_EQ(0xc110, packet_8.get_controls()); EXPECT_EQ(0x8112, packet_9.get_controls()); EXPECT_EQ(0x4102, packet_1->get_controls()); EXPECT_EQ(0xc104, packet_2->get_controls()); EXPECT_EQ(0xc106, packet_3->get_controls()); EXPECT_EQ(0xc108, packet_4->get_controls()); EXPECT_EQ(0xc10a, packet_5->get_controls()); EXPECT_EQ(0xc10c, packet_6->get_controls()); EXPECT_EQ(0xc10e, packet_7->get_controls()); EXPECT_EQ(0xc110, packet_8->get_controls()); EXPECT_EQ(0x8112, packet_9->get_controls()); } TEST_F(L2capSduTest, getTotalLengthTest) { EXPECT_EQ(0x1f95, packet_1.get_total_l2cap_length()); EXPECT_EQ(0x1f95, packet_1->get_total_l2cap_length()); } TEST_F(L2capSduTest, getVectorSizeTest) { EXPECT_EQ(l2cap_test_packet_1.size(), packet_1.get_vector_size()); EXPECT_EQ(l2cap_test_packet_2.size(), packet_2.get_vector_size()); EXPECT_EQ(l2cap_test_packet_3.size(), packet_3.get_vector_size()); EXPECT_EQ(l2cap_test_packet_4.size(), packet_4.get_vector_size()); EXPECT_EQ(l2cap_test_packet_5.size(), packet_5.get_vector_size()); EXPECT_EQ(l2cap_test_packet_6.size(), packet_6.get_vector_size()); EXPECT_EQ(l2cap_test_packet_7.size(), packet_7.get_vector_size()); EXPECT_EQ(l2cap_test_packet_8.size(), packet_8.get_vector_size()); EXPECT_EQ(l2cap_test_packet_9.size(), packet_9.get_vector_size()); EXPECT_EQ(l2cap_test_packet_1.size(), packet_1->get_vector_size()); EXPECT_EQ(l2cap_test_packet_2.size(), packet_2->get_vector_size()); EXPECT_EQ(l2cap_test_packet_3.size(), packet_3->get_vector_size()); EXPECT_EQ(l2cap_test_packet_4.size(), packet_4->get_vector_size()); EXPECT_EQ(l2cap_test_packet_5.size(), packet_5->get_vector_size()); EXPECT_EQ(l2cap_test_packet_6.size(), packet_6->get_vector_size()); EXPECT_EQ(l2cap_test_packet_7.size(), packet_7->get_vector_size()); EXPECT_EQ(l2cap_test_packet_8.size(), packet_8->get_vector_size()); EXPECT_EQ(l2cap_test_packet_9.size(), packet_9->get_vector_size()); } TEST_F(L2capSduTest, getCidTest) { EXPECT_EQ(0x0047, packet_1.get_channel_id()); EXPECT_EQ(0x0047, packet_2.get_channel_id()); EXPECT_EQ(0x0047, packet_3.get_channel_id()); EXPECT_EQ(0x0047, packet_4.get_channel_id()); EXPECT_EQ(0x0047, packet_5.get_channel_id()); EXPECT_EQ(0x0047, packet_6.get_channel_id()); EXPECT_EQ(0x0047, packet_7.get_channel_id()); EXPECT_EQ(0x0047, packet_8.get_channel_id()); EXPECT_EQ(0x0047, packet_9.get_channel_id()); EXPECT_EQ(0x0047, packet_1->get_channel_id()); EXPECT_EQ(0x0047, packet_2->get_channel_id()); EXPECT_EQ(0x0047, packet_3->get_channel_id()); EXPECT_EQ(0x0047, packet_4->get_channel_id()); EXPECT_EQ(0x0047, packet_5->get_channel_id()); EXPECT_EQ(0x0047, packet_6->get_channel_id()); EXPECT_EQ(0x0047, packet_7->get_channel_id()); EXPECT_EQ(0x0047, packet_8->get_channel_id()); EXPECT_EQ(0x0047, packet_9->get_channel_id()); } } // namespace test_vendor_lib Loading
system/vendor_libs/test_vendor_lib/include/l2cap_packet.h +4 −3 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ class L2capPacket { public: // Returns an assembled L2cap object if successful, nullptr if failure. static std::unique_ptr<L2capPacket> assemble( const std::vector<L2capSdu>& sdu_packet); 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. Loading @@ -45,7 +45,8 @@ class L2capPacket { // Returns a fragmented vector of L2capSdu objects if successful // Returns an empty vector of L2capSdu objects if unsuccessful std::vector<L2capSdu> fragment(uint16_t maximum_sdu_size, uint8_t txseq, std::vector<std::unique_ptr<L2capSdu> > fragment(uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) const; private: Loading
system/vendor_libs/test_vendor_lib/include/l2cap_sdu.h +10 −12 Original line number Diff line number Diff line Loading @@ -59,19 +59,14 @@ namespace test_vendor_lib { // class L2capSdu { public: // Returns a completed L2capSdu object. L2capSdu(std::vector<uint8_t> create_from); static L2capSdu L2capSduBuilder(std::vector<uint8_t> create_from); // TODO: Remove this when the move to L2capSdu* is done L2capSdu& operator=(L2capSdu obj1) { sdu_data_.clear(); // 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( std::vector<uint8_t> create_from); sdu_data_ = obj1.sdu_data_; return *this; } // Adds an FCS to create_from and returns a unique_ptr to an L2capSdu object. static std::unique_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 Loading Loading @@ -123,6 +118,9 @@ class L2capSdu { // This is the SDU packet in bytes. std::vector<uint8_t> sdu_data_; // Returns a completed L2capSdu object. L2capSdu(std::vector<uint8_t>&& create_from); // Table for precalculated lfsr values. static const uint16_t lfsr_table_[256]; Loading
system/vendor_libs/test_vendor_lib/src/l2cap_packet.cc +20 −20 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ const uint8_t kSduContinuationReqseq = 0xC0; const uint8_t kSduEndReqseq = 0x80; std::unique_ptr<L2capPacket> L2capPacket::assemble( const std::vector<L2capSdu>& sdu_packets) { const std::vector<std::unique_ptr<L2capSdu> >& sdu_packets) { std::unique_ptr<L2capPacket> built_l2cap_packet(new L2capPacket()); uint16_t l2cap_payload_length = 0; uint16_t first_packet_channel_id = 0; Loading @@ -45,17 +45,18 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( LOG_DEBUG(LOG_TAG, "%s: No SDU received.", __func__); return nullptr; } if (sdu_packets.size() == 1 && !L2capSdu::is_complete_l2cap(sdu_packets[0])) { if (sdu_packets.size() == 1 && !L2capSdu::is_complete_l2cap(*sdu_packets[0])) { LOG_DEBUG(LOG_TAG, "%s: Unsegmented SDU has incorrect SAR bits.", __func__); return nullptr; } first_packet_channel_id = sdu_packets[0].get_channel_id(); first_packet_channel_id = sdu_packets[0]->get_channel_id(); built_l2cap_packet->l2cap_packet_.resize(kL2capHeaderLength); for (size_t i = 0; i < sdu_packets.size(); i++) { uint16_t payload_length = sdu_packets[i].get_payload_length(); uint16_t payload_length = sdu_packets[i]->get_payload_length(); // TODO(jruthe): Remove these checks when ACL packets have been // implemented. Once those are done, that will be the only way to create Loading @@ -64,21 +65,21 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( // Check the integrity of the packet length, if it is zero, it is invalid. // The maximum size of a single, segmented L2CAP payload is 1016 bytes. if ((payload_length <= 0) || (payload_length != sdu_packets[i].get_vector_size() - 4)) { (payload_length != sdu_packets[i]->get_vector_size() - 4)) { LOG_DEBUG(LOG_TAG, "%s: SDU payload length incorrect.", __func__); return nullptr; } uint16_t fcs_check = sdu_packets[i].get_fcs(); uint16_t fcs_check = sdu_packets[i]->get_fcs(); if (sdu_packets[i].calculate_fcs() != fcs_check) { if (sdu_packets[i]->calculate_fcs() != fcs_check) { LOG_DEBUG(LOG_TAG, "%s: SDU fcs is incorrect.", __func__); return nullptr; } uint16_t controls = sdu_packets[i].get_controls(); uint16_t controls = sdu_packets[i]->get_controls(); if (sdu_packets[i].get_channel_id() != first_packet_channel_id) { if (sdu_packets[i]->get_channel_id() != first_packet_channel_id) { LOG_DEBUG(LOG_TAG, "%s: SDU CID does not match expected.", __func__); return nullptr; } Loading @@ -95,12 +96,12 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( uint16_t starting_index; uint8_t txseq = controls & kSduTxSeqBits; if (sdu_packets.size() > 1 && i == 0 && !L2capSdu::is_starting_sdu(sdu_packets[i])) { !L2capSdu::is_starting_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: First segmented SDU has incorrect SAR bits.", __func__); return nullptr; } if (i != 0 && L2capSdu::is_starting_sdu(sdu_packets[i])) { if (i != 0 && L2capSdu::is_starting_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: SAR bits set to first segmented SDU on " "non-starting SDU.", Loading @@ -112,7 +113,7 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( return nullptr; } if (sdu_packets.size() > 1 && i == sdu_packets.size() - 1 && !L2capSdu::is_ending_sdu(sdu_packets[i])) { !L2capSdu::is_ending_sdu(*sdu_packets[i])) { LOG_DEBUG(LOG_TAG, "%s: Final segmented SDU has incorrect SAR bits.", __func__); return nullptr; Loading @@ -121,9 +122,9 @@ std::unique_ptr<L2capPacket> L2capPacket::assemble( // Subtract the control and fcs from every SDU payload length. l2cap_payload_length += (payload_length - 4); if (L2capSdu::is_starting_sdu(sdu_packets[i])) { if (L2capSdu::is_starting_sdu(*sdu_packets[i])) { starting_index = kSduFirstHeaderLength; total_expected_l2cap_length = sdu_packets[i].get_total_l2cap_length(); total_expected_l2cap_length = sdu_packets[i]->get_total_l2cap_length(); // Subtract the additional two bytes from the first packet of a segmented // SDU. Loading @@ -132,8 +133,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(); auto payload_begin = sdu_packets[i]->get_payload_begin(starting_index); auto payload_end = sdu_packets[i]->get_payload_end(); built_l2cap_packet->l2cap_packet_.insert( built_l2cap_packet->l2cap_packet_.end(), payload_begin, payload_end); Loading Loading @@ -180,10 +181,9 @@ std::vector<uint8_t>::const_iterator L2capPacket::get_l2cap_payload_end() return l2cap_packet_.end(); } std::vector<L2capSdu> L2capPacket::fragment(uint16_t maximum_sdu_size, uint8_t txseq, uint8_t reqseq) const { std::vector<L2capSdu> sdu; 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; std::vector<uint8_t> current_sdu; Loading
system/vendor_libs/test_vendor_lib/src/l2cap_sdu.cc +12 −4 Original line number Diff line number Diff line Loading @@ -57,11 +57,19 @@ const uint16_t L2capSdu::lfsr_table_[256] = { 0x4100, 0x81c1, 0x8081, 0x4040, }; // lfsr_table L2capSdu::L2capSdu(std::vector<uint8_t> create_from) { sdu_data_ = std::move(create_from); L2capSdu::L2capSdu(std::vector<uint8_t>&& create_from) { sdu_data_ = create_from; } L2capSdu L2capSdu::L2capSduBuilder(std::vector<uint8_t> create_from) { std::unique_ptr<L2capSdu> L2capSdu::L2capSduConstructor( std::vector<uint8_t> create_from) { L2capSdu packet(std::move(create_from)); return std::make_unique<L2capSdu>(packet); } std::unique_ptr<L2capSdu> L2capSdu::L2capSduBuilder( std::vector<uint8_t> create_from) { L2capSdu packet(std::move(create_from)); packet.sdu_data_.resize(packet.sdu_data_.size() + 2, 0x00); Loading @@ -71,7 +79,7 @@ L2capSdu L2capSdu::L2capSduBuilder(std::vector<uint8_t> create_from) { packet.sdu_data_[packet.sdu_data_.size() - 2] = fcs & 0xFF; packet.sdu_data_[packet.sdu_data_.size() - 1] = (fcs & 0xFF00) >> 8; return packet; return std::make_unique<L2capSdu>(packet); } std::vector<uint8_t>::const_iterator L2capSdu::get_payload_begin( Loading
system/vendor_libs/test_vendor_lib/test/l2cap_sdu_test.cc +77 −64 Original line number Diff line number Diff line Loading @@ -16,21 +16,34 @@ * ******************************************************************************/ #include "l2cap_sdu.h" #include <gtest/gtest.h> #include <memory> #include "l2cap_test_packets.h" using std::vector; namespace test_vendor_lib { L2capSdu packet_1(l2cap_test_packet_1); L2capSdu packet_2(l2cap_test_packet_2); L2capSdu packet_3(l2cap_test_packet_3); L2capSdu packet_4(l2cap_test_packet_4); L2capSdu packet_5(l2cap_test_packet_5); L2capSdu packet_6(l2cap_test_packet_6); L2capSdu packet_7(l2cap_test_packet_7); L2capSdu packet_8(l2cap_test_packet_8); L2capSdu packet_9(l2cap_test_packet_9); std::unique_ptr<L2capSdu> packet_1 = L2capSdu::L2capSduConstructor(l2cap_test_packet_1); std::unique_ptr<L2capSdu> packet_2 = L2capSdu::L2capSduConstructor(l2cap_test_packet_2); std::unique_ptr<L2capSdu> packet_3 = L2capSdu::L2capSduConstructor(l2cap_test_packet_3); std::unique_ptr<L2capSdu> packet_4 = L2capSdu::L2capSduConstructor(l2cap_test_packet_4); std::unique_ptr<L2capSdu> packet_5 = L2capSdu::L2capSduConstructor(l2cap_test_packet_5); std::unique_ptr<L2capSdu> packet_6 = L2capSdu::L2capSduConstructor(l2cap_test_packet_6); std::unique_ptr<L2capSdu> packet_7 = L2capSdu::L2capSduConstructor(l2cap_test_packet_7); std::unique_ptr<L2capSdu> packet_8 = L2capSdu::L2capSduConstructor(l2cap_test_packet_8); std::unique_ptr<L2capSdu> packet_9 = L2capSdu::L2capSduConstructor(l2cap_test_packet_9); class L2capSduTest : public ::testing::Test { public: Loading @@ -41,79 +54,79 @@ class L2capSduTest : public ::testing::Test { }; // L2capSduTest TEST_F(L2capSduTest, getFcsTest) { EXPECT_EQ(0x72aa, packet_1.get_fcs()); EXPECT_EQ(0x5b57, packet_2.get_fcs()); EXPECT_EQ(0xe644, packet_3.get_fcs()); EXPECT_EQ(0x21b0, packet_4.get_fcs()); EXPECT_EQ(0xae96, packet_5.get_fcs()); EXPECT_EQ(0x9254, packet_6.get_fcs()); EXPECT_EQ(0xf6fa, packet_7.get_fcs()); EXPECT_EQ(0x1da4, packet_8.get_fcs()); EXPECT_EQ(0x781a, packet_9.get_fcs()); EXPECT_EQ(0x72aa, packet_1->get_fcs()); EXPECT_EQ(0x5b57, packet_2->get_fcs()); EXPECT_EQ(0xe644, packet_3->get_fcs()); EXPECT_EQ(0x21b0, packet_4->get_fcs()); EXPECT_EQ(0xae96, packet_5->get_fcs()); EXPECT_EQ(0x9254, packet_6->get_fcs()); EXPECT_EQ(0xf6fa, packet_7->get_fcs()); EXPECT_EQ(0x1da4, packet_8->get_fcs()); EXPECT_EQ(0x781a, packet_9->get_fcs()); } TEST_F(L2capSduTest, getPayloadLengthTest) { EXPECT_EQ(l2cap_test_packet_1.size() - 4, packet_1.get_payload_length()); EXPECT_EQ(l2cap_test_packet_2.size() - 4, packet_2.get_payload_length()); EXPECT_EQ(l2cap_test_packet_3.size() - 4, packet_3.get_payload_length()); EXPECT_EQ(l2cap_test_packet_4.size() - 4, packet_4.get_payload_length()); EXPECT_EQ(l2cap_test_packet_5.size() - 4, packet_5.get_payload_length()); EXPECT_EQ(l2cap_test_packet_6.size() - 4, packet_6.get_payload_length()); EXPECT_EQ(l2cap_test_packet_7.size() - 4, packet_7.get_payload_length()); EXPECT_EQ(l2cap_test_packet_8.size() - 4, packet_8.get_payload_length()); EXPECT_EQ(l2cap_test_packet_9.size() - 4, packet_9.get_payload_length()); EXPECT_EQ(l2cap_test_packet_1.size() - 4, packet_1->get_payload_length()); EXPECT_EQ(l2cap_test_packet_2.size() - 4, packet_2->get_payload_length()); EXPECT_EQ(l2cap_test_packet_3.size() - 4, packet_3->get_payload_length()); EXPECT_EQ(l2cap_test_packet_4.size() - 4, packet_4->get_payload_length()); EXPECT_EQ(l2cap_test_packet_5.size() - 4, packet_5->get_payload_length()); EXPECT_EQ(l2cap_test_packet_6.size() - 4, packet_6->get_payload_length()); EXPECT_EQ(l2cap_test_packet_7.size() - 4, packet_7->get_payload_length()); EXPECT_EQ(l2cap_test_packet_8.size() - 4, packet_8->get_payload_length()); EXPECT_EQ(l2cap_test_packet_9.size() - 4, packet_9->get_payload_length()); } TEST_F(L2capSduTest, calculateFcsTest) { EXPECT_EQ(0x72aa, packet_1.calculate_fcs()); EXPECT_EQ(0x5b57, packet_2.calculate_fcs()); EXPECT_EQ(0xe644, packet_3.calculate_fcs()); EXPECT_EQ(0x21b0, packet_4.calculate_fcs()); EXPECT_EQ(0xae96, packet_5.calculate_fcs()); EXPECT_EQ(0x9254, packet_6.calculate_fcs()); EXPECT_EQ(0xf6fa, packet_7.calculate_fcs()); EXPECT_EQ(0x1da4, packet_8.calculate_fcs()); EXPECT_EQ(0x781a, packet_9.calculate_fcs()); EXPECT_EQ(0x72aa, packet_1->calculate_fcs()); EXPECT_EQ(0x5b57, packet_2->calculate_fcs()); EXPECT_EQ(0xe644, packet_3->calculate_fcs()); EXPECT_EQ(0x21b0, packet_4->calculate_fcs()); EXPECT_EQ(0xae96, packet_5->calculate_fcs()); EXPECT_EQ(0x9254, packet_6->calculate_fcs()); EXPECT_EQ(0xf6fa, packet_7->calculate_fcs()); EXPECT_EQ(0x1da4, packet_8->calculate_fcs()); EXPECT_EQ(0x781a, packet_9->calculate_fcs()); } TEST_F(L2capSduTest, getControlsTest) { EXPECT_EQ(0x4102, packet_1.get_controls()); EXPECT_EQ(0xc104, packet_2.get_controls()); EXPECT_EQ(0xc106, packet_3.get_controls()); EXPECT_EQ(0xc108, packet_4.get_controls()); EXPECT_EQ(0xc10a, packet_5.get_controls()); EXPECT_EQ(0xc10c, packet_6.get_controls()); EXPECT_EQ(0xc10e, packet_7.get_controls()); EXPECT_EQ(0xc110, packet_8.get_controls()); EXPECT_EQ(0x8112, packet_9.get_controls()); EXPECT_EQ(0x4102, packet_1->get_controls()); EXPECT_EQ(0xc104, packet_2->get_controls()); EXPECT_EQ(0xc106, packet_3->get_controls()); EXPECT_EQ(0xc108, packet_4->get_controls()); EXPECT_EQ(0xc10a, packet_5->get_controls()); EXPECT_EQ(0xc10c, packet_6->get_controls()); EXPECT_EQ(0xc10e, packet_7->get_controls()); EXPECT_EQ(0xc110, packet_8->get_controls()); EXPECT_EQ(0x8112, packet_9->get_controls()); } TEST_F(L2capSduTest, getTotalLengthTest) { EXPECT_EQ(0x1f95, packet_1.get_total_l2cap_length()); EXPECT_EQ(0x1f95, packet_1->get_total_l2cap_length()); } TEST_F(L2capSduTest, getVectorSizeTest) { EXPECT_EQ(l2cap_test_packet_1.size(), packet_1.get_vector_size()); EXPECT_EQ(l2cap_test_packet_2.size(), packet_2.get_vector_size()); EXPECT_EQ(l2cap_test_packet_3.size(), packet_3.get_vector_size()); EXPECT_EQ(l2cap_test_packet_4.size(), packet_4.get_vector_size()); EXPECT_EQ(l2cap_test_packet_5.size(), packet_5.get_vector_size()); EXPECT_EQ(l2cap_test_packet_6.size(), packet_6.get_vector_size()); EXPECT_EQ(l2cap_test_packet_7.size(), packet_7.get_vector_size()); EXPECT_EQ(l2cap_test_packet_8.size(), packet_8.get_vector_size()); EXPECT_EQ(l2cap_test_packet_9.size(), packet_9.get_vector_size()); EXPECT_EQ(l2cap_test_packet_1.size(), packet_1->get_vector_size()); EXPECT_EQ(l2cap_test_packet_2.size(), packet_2->get_vector_size()); EXPECT_EQ(l2cap_test_packet_3.size(), packet_3->get_vector_size()); EXPECT_EQ(l2cap_test_packet_4.size(), packet_4->get_vector_size()); EXPECT_EQ(l2cap_test_packet_5.size(), packet_5->get_vector_size()); EXPECT_EQ(l2cap_test_packet_6.size(), packet_6->get_vector_size()); EXPECT_EQ(l2cap_test_packet_7.size(), packet_7->get_vector_size()); EXPECT_EQ(l2cap_test_packet_8.size(), packet_8->get_vector_size()); EXPECT_EQ(l2cap_test_packet_9.size(), packet_9->get_vector_size()); } TEST_F(L2capSduTest, getCidTest) { EXPECT_EQ(0x0047, packet_1.get_channel_id()); EXPECT_EQ(0x0047, packet_2.get_channel_id()); EXPECT_EQ(0x0047, packet_3.get_channel_id()); EXPECT_EQ(0x0047, packet_4.get_channel_id()); EXPECT_EQ(0x0047, packet_5.get_channel_id()); EXPECT_EQ(0x0047, packet_6.get_channel_id()); EXPECT_EQ(0x0047, packet_7.get_channel_id()); EXPECT_EQ(0x0047, packet_8.get_channel_id()); EXPECT_EQ(0x0047, packet_9.get_channel_id()); EXPECT_EQ(0x0047, packet_1->get_channel_id()); EXPECT_EQ(0x0047, packet_2->get_channel_id()); EXPECT_EQ(0x0047, packet_3->get_channel_id()); EXPECT_EQ(0x0047, packet_4->get_channel_id()); EXPECT_EQ(0x0047, packet_5->get_channel_id()); EXPECT_EQ(0x0047, packet_6->get_channel_id()); EXPECT_EQ(0x0047, packet_7->get_channel_id()); EXPECT_EQ(0x0047, packet_8->get_channel_id()); EXPECT_EQ(0x0047, packet_9->get_channel_id()); } } // namespace test_vendor_lib