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 "packet.h"
20 #include "packet_test_helper.h"
21 
22 // We have our own definition of loghex to avoid dependencies
23 namespace {
24 template <typename T>
loghex(T x)25 std::string loghex(T x) {
26   std::stringstream tmp;
27   tmp << "0x" << std::internal << std::hex << std::setfill('0')
28       << std::setw(sizeof(T) * 2) << (unsigned int)x;
29   return tmp.str();
30 }
31 }  // namespace
32 
33 namespace bluetooth {
34 
35 class PacketImpl : public Packet {
36  public:
37   using Packet::Packet;  // Inherit constructors
38 
IsValid()39   virtual bool IsValid() const { return true; }
40 
ToString()41   virtual std::string ToString() const {
42     std::stringstream ss;
43     ss << "TestPacket: Start = " << packet_start_index_
44        << " : End = " << packet_end_index_ << std::endl;
45     ss << "  └ Payload =";
46     for (auto it = begin(); it != end(); it++) {
47       ss << " " << loghex(*it);
48     }
49     ss << std::endl;
50 
51     return ss.str();
52   };
53 
GetPayloadIndecies()54   virtual std::pair<size_t, size_t> GetPayloadIndecies() const {
55     return std::pair<size_t, size_t>(packet_start_index_, packet_end_index_);
56   }
57 };
58 
59 using TestPacket = TestPacketType<PacketImpl>;
60 
61 // A helper class that has public accessors to protected methods
62 class TestPacketBuilder : public PacketBuilder {
63  public:
MakeBuilder(std::vector<uint8_t> data)64   static std::unique_ptr<TestPacketBuilder> MakeBuilder(
65       std::vector<uint8_t> data) {
66     std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
67     return builder;
68   };
69 
70   // Make all the utility functions public
71   using PacketBuilder::ReserveSpace;
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 
size()79   size_t size() const override { return data_.size(); };
80 
Serialize(const std::shared_ptr<Packet> & pkt)81   bool Serialize(const std::shared_ptr<Packet>& pkt) override {
82     ReserveSpace(pkt, size());
83 
84     for (uint8_t byte : data_) {
85       AddPayloadOctets1(pkt, byte);
86     }
87 
88     return true;
89   }
90 
TestPacketBuilder(std::vector<uint8_t> data)91   TestPacketBuilder(std::vector<uint8_t> data) : data_(data){};
92 
93   std::vector<uint8_t> data_;
94 };
95 
96 }  // namespace bluetooth