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