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 PacketView(const std::forward_list<class View> fragments); 37 PacketView(const PacketView& PacketView) = default; 38 PacketView(std::shared_ptr<std::vector<uint8_t>> packet); 39 virtual ~PacketView() = default; 40 41 virtual Iterator<little_endian> begin() const; 42 43 virtual Iterator<little_endian> end() const; 44 45 uint8_t operator[](size_t i) const; 46 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 53 PacketView<false> GetBigEndianSubview(size_t begin, size_t end) const; 54 55 private: 56 std::forward_list<View> fragments_; 57 size_t length_; 58 PacketView<little_endian>() = delete; 59 std::forward_list<View> GetSubviewList(size_t begin, size_t end) const; 60 }; 61 62 } // namespace packet 63 } // namespace bluetooth 64