1 /*
2  * Copyright 2019 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 <cstdint>
20 #include <forward_list>
21 
22 #include "packet/iterator.h"
23 #include "packet/view.h"
24 
25 namespace bluetooth {
26 namespace packet {
27 
28 static const bool kLittleEndian = true;
29 
30 // Abstract base class that is subclassed to provide type-specifc accessors.
31 // Holds a shared pointer to the underlying data.
32 // The template parameter little_endian controls the generation of extract().
33 template <bool little_endian>
34 class PacketView {
35  public:
36   explicit PacketView(std::forward_list<View> fragments);
37   PacketView(const PacketView& PacketView) = default;
38   explicit PacketView(std::shared_ptr<std::vector<uint8_t>> packet);
39   PacketView<little_endian>() = delete;
40   virtual ~PacketView() = default;
41 
42   virtual Iterator<little_endian> begin() const;
43 
44   virtual Iterator<little_endian> end() const;
45 
46   uint8_t operator[](size_t i) const;
47 
48   uint8_t at(size_t index) const;
49 
50   size_t size() const;
51 
52   PacketView<true> GetLittleEndianSubview(size_t begin, size_t end) const;
53 
54   PacketView<false> GetBigEndianSubview(size_t begin, size_t end) const;
55 
56  protected:
57   void Append(PacketView to_add);
58 
59  private:
60   std::forward_list<View> fragments_;
61   size_t length_;
62   std::forward_list<View> GetSubviewList(size_t begin, size_t end) const;
63 };
64 
65 }  // namespace packet
66 }  // namespace bluetooth
67