1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <iomanip> 20 #include <vector> 21 22 #include "packet_test_helper.h" 23 24 // We have our own definition of loghex to avoid dependencies 25 namespace { 26 template <typename T> loghex(T x)27std::string loghex(T x) { 28 std::stringstream tmp; 29 tmp << "0x" << std::internal << std::hex << std::setfill('0') << std::setw(sizeof(T) * 2) 30 << (unsigned int)x; 31 return tmp.str(); 32 } 33 } // namespace 34 35 namespace bluetooth { 36 37 class PacketImpl : public Packet { 38 public: 39 using Packet::Packet; // Inherit constructors 40 IsValid()41 virtual bool IsValid() const { return true; } 42 ToString()43 virtual std::string ToString() const { 44 std::stringstream ss; 45 ss << "TestPacket: Start = " << packet_start_index_ << " : End = " << packet_end_index_ 46 << std::endl; 47 ss << " └ Payload ="; 48 for (auto it = begin(); it != end(); it++) { 49 ss << " " << loghex(*it); 50 } 51 ss << std::endl; 52 53 return ss.str(); 54 } 55 GetPayloadIndecies()56 virtual std::pair<size_t, size_t> GetPayloadIndecies() const { 57 return std::pair<size_t, size_t>(packet_start_index_, packet_end_index_); 58 } 59 }; 60 61 using TestPacket = TestPacketType<PacketImpl>; 62 63 // A helper class that has public accessors to protected methods 64 class TestPacketBuilder : public PacketBuilder { 65 public: MakeBuilder(std::vector<uint8_t> data)66 static std::unique_ptr<TestPacketBuilder> MakeBuilder(std::vector<uint8_t> data) { 67 std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data)); 68 return builder; 69 } 70 71 // Make all the utility functions public 72 using PacketBuilder::AddPayloadOctets1; 73 using PacketBuilder::AddPayloadOctets2; 74 using PacketBuilder::AddPayloadOctets3; 75 using PacketBuilder::AddPayloadOctets4; 76 using PacketBuilder::AddPayloadOctets6; 77 using PacketBuilder::AddPayloadOctets8; 78 using PacketBuilder::ReserveSpace; 79 size()80 size_t size() const override { return data_.size(); } 81 Serialize(const std::shared_ptr<Packet> & pkt)82 bool Serialize(const std::shared_ptr<Packet>& pkt) override { 83 ReserveSpace(pkt, size()); 84 85 for (uint8_t byte : data_) { 86 AddPayloadOctets1(pkt, byte); 87 } 88 89 return true; 90 } 91 TestPacketBuilder(std::vector<uint8_t> data)92 TestPacketBuilder(std::vector<uint8_t> data) : data_(data) {} 93 94 std::vector<uint8_t> data_; 95 }; 96 97 } // namespace bluetooth 98