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)27 std::string loghex(T x) {
28   std::stringstream tmp;
29   tmp << "0x" << std::internal << std::hex << std::setfill('0')
30       << std::setw(sizeof(T) * 2) << (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_
46        << " : End = " << packet_end_index_ << 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(
67       std::vector<uint8_t> data) {
68     std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
69     return builder;
70   };
71 
72   // Make all the utility functions public
73   using PacketBuilder::ReserveSpace;
74   using PacketBuilder::AddPayloadOctets1;
75   using PacketBuilder::AddPayloadOctets2;
76   using PacketBuilder::AddPayloadOctets3;
77   using PacketBuilder::AddPayloadOctets4;
78   using PacketBuilder::AddPayloadOctets6;
79   using PacketBuilder::AddPayloadOctets8;
80 
size()81   size_t size() const override { return data_.size(); };
82 
Serialize(const std::shared_ptr<Packet> & pkt)83   bool Serialize(const std::shared_ptr<Packet>& pkt) override {
84     ReserveSpace(pkt, size());
85 
86     for (uint8_t byte : data_) {
87       AddPayloadOctets1(pkt, byte);
88     }
89 
90     return true;
91   }
92 
TestPacketBuilder(std::vector<uint8_t> data)93   TestPacketBuilder(std::vector<uint8_t> data) : data_(data){};
94 
95   std::vector<uint8_t> data_;
96 };
97 
98 }  // namespace bluetooth
99