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 <iterator> 22 #include <memory> 23 #include <vector> 24 25 #include "os/log.h" 26 #include "packet/bit_inserter.h" 27 28 namespace bluetooth { 29 namespace packet { 30 31 // Abstract base class that is subclassed to provide insert() functions. 32 // The template parameter little_endian controls the generation of insert(). 33 template <bool little_endian> 34 class EndianInserter { 35 public: 36 EndianInserter() = default; 37 virtual ~EndianInserter() = default; 38 39 protected: 40 // Write sizeof(FixedWidthIntegerType) bytes using the iterator 41 template <typename FixedWidthPODType, typename std::enable_if<std::is_pod<FixedWidthPODType>::value, int>::type = 0> insert(FixedWidthPODType value,BitInserter & it)42 void insert(FixedWidthPODType value, BitInserter& it) const { 43 uint8_t* raw_bytes = (uint8_t*)&value; 44 for (size_t i = 0; i < sizeof(FixedWidthPODType); i++) { 45 if (little_endian == true) { 46 it.insert_byte(raw_bytes[i]); 47 } else { 48 it.insert_byte(raw_bytes[sizeof(FixedWidthPODType) - i - 1]); 49 } 50 } 51 } 52 53 // Write num_bits bits using the iterator 54 template <typename FixedWidthIntegerType, 55 typename std::enable_if<std::is_pod<FixedWidthIntegerType>::value, int>::type = 0> insert(FixedWidthIntegerType value,BitInserter & it,size_t num_bits)56 void insert(FixedWidthIntegerType value, BitInserter& it, size_t num_bits) const { 57 ASSERT(num_bits <= (sizeof(FixedWidthIntegerType) * 8)); 58 59 for (size_t i = 0; i < num_bits / 8; i++) { 60 if (little_endian == true) { 61 it.insert_byte(static_cast<uint8_t>(value >> (i * 8))); 62 } else { 63 it.insert_byte(static_cast<uint8_t>(value >> (((num_bits / 8) - i - 1) * 8))); 64 } 65 } 66 if (num_bits % 8) { 67 it.insert_bits(static_cast<uint8_t>(value >> ((num_bits / 8) * 8)), num_bits % 8); 68 } 69 } 70 71 // Specialized insert that allows inserting enums without casting 72 template <typename Enum, typename std::enable_if<std::is_enum_v<Enum>, int>::type = 0> insert(Enum value,BitInserter & it)73 inline void insert(Enum value, BitInserter& it) const { 74 using enum_type = typename std::underlying_type_t<Enum>; 75 static_assert(std::is_unsigned_v<enum_type>, "Enum type is signed. Did you forget to specify the enum size?"); 76 insert<enum_type>(static_cast<enum_type>(value), it); 77 } 78 79 // Write a vector of FixedWidthIntegerType using the iterator 80 template <typename FixedWidthIntegerType> insert_vector(const std::vector<FixedWidthIntegerType> & vec,BitInserter & it)81 void insert_vector(const std::vector<FixedWidthIntegerType>& vec, BitInserter& it) const { 82 static_assert(std::is_pod<FixedWidthIntegerType>::value, 83 "EndianInserter::insert requires a vector with elements of a fixed-size."); 84 for (const auto& element : vec) { 85 insert(element, it); 86 } 87 } 88 }; 89 90 } // namespace packet 91 } // namespace bluetooth 92