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 explicit PacketView(std::shared_ptr<const std::vector<uint8_t>> packet); 38 PacketView(const PacketView& PacketView) = default; 39 PacketView<little_endian>() = delete; 40 virtual ~PacketView() = default; 41 42 Iterator<little_endian> begin() const; 43 Iterator<little_endian> end() const; 44 45 uint8_t operator[](size_t i) const; 46 uint8_t at(size_t index) const; 47 48 size_t size() const; 49 50 PacketView<true> GetLittleEndianSubview(size_t begin, size_t end) const; 51 PacketView<false> GetBigEndianSubview(size_t begin, size_t end) const; 52 53 protected: 54 void Append(PacketView to_add); 55 56 private: 57 std::forward_list<View> fragments_; 58 size_t length_; 59 60 std::forward_list<View> GetSubviewList(size_t begin, size_t end) const; 61 }; 62 63 } // namespace packet 64 } // namespace bluetooth 65