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 "common/address.h" 26 #include "common/class_of_device.h" 27 #include "os/log.h" 28 #include "packet/base_packet_builder.h" 29 #include "packet/bit_inserter.h" 30 31 namespace bluetooth { 32 namespace packet { 33 34 // Abstract base class that is subclassed to build specifc packets. 35 // The template parameter little_endian controls the generation of insert(). 36 template <bool little_endian> 37 class PacketBuilder : public BasePacketBuilder { 38 public: 39 PacketBuilder() = default; 40 virtual ~PacketBuilder() = default; 41 42 // Classes which need fragmentation should define a function like this: 43 // std::forward_list<DerivedBuilder>& Fragment(size_t max_size); 44 45 protected: 46 // Write sizeof(FixedWidthIntegerType) bytes using the iterator 47 template <typename FixedWidthIntegerType, 48 typename std::enable_if<std::is_integral<FixedWidthIntegerType>::value, int>::type = 0> insert(FixedWidthIntegerType value,BitInserter & it)49 void insert(FixedWidthIntegerType value, BitInserter& it) const { 50 for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) { 51 if (little_endian == true) { 52 it.insert_byte(static_cast<uint8_t>(value >> (i * 8))); 53 } else { 54 it.insert_byte(static_cast<uint8_t>(value >> ((sizeof(FixedWidthIntegerType) - i - 1) * 8))); 55 } 56 } 57 } 58 59 // Write num_bits bits using the iterator 60 template <typename FixedWidthIntegerType, 61 typename std::enable_if<std::is_integral<FixedWidthIntegerType>::value, int>::type = 0> insert(FixedWidthIntegerType value,BitInserter & it,size_t num_bits)62 void insert(FixedWidthIntegerType value, BitInserter& it, size_t num_bits) const { 63 ASSERT(num_bits <= (sizeof(FixedWidthIntegerType) * 8)); 64 65 for (size_t i = 0; i < num_bits / 8; i++) { 66 if (little_endian == true) { 67 it.insert_byte(static_cast<uint8_t>(value >> (i * 8))); 68 } else { 69 it.insert_byte(static_cast<uint8_t>(value >> (((num_bits / 8) - i - 1) * 8))); 70 } 71 } 72 if (num_bits % 8) { 73 it.insert_bits(static_cast<uint8_t>(value >> ((num_bits / 8) * 8)), num_bits % 8); 74 } 75 } 76 77 // Specialized insert that allows inserting enums without casting 78 template <typename Enum, typename std::enable_if<std::is_enum_v<Enum>, int>::type = 0> insert(Enum value,BitInserter & it)79 inline void insert(Enum value, BitInserter& it) const { 80 using enum_type = typename std::underlying_type_t<Enum>; 81 static_assert(std::is_unsigned_v<enum_type>, "Enum type is signed. Did you forget to specify the enum size?"); 82 insert<enum_type>(static_cast<enum_type>(value), it); 83 } 84 85 // Write a vector of FixedWidthIntegerType using the iterator 86 template <typename FixedWidthIntegerType> insert_vector(const std::vector<FixedWidthIntegerType> & vec,BitInserter & it)87 void insert_vector(const std::vector<FixedWidthIntegerType>& vec, BitInserter& it) const { 88 static_assert(std::is_integral<FixedWidthIntegerType>::value, 89 "PacketBuilder::insert requires an integral type vector."); 90 for (const auto& element : vec) { 91 insert(element, it); 92 } 93 } 94 insert_address(const common::Address & addr,BitInserter & it)95 void insert_address(const common::Address& addr, BitInserter& it) const { 96 for (const auto& element : addr.address) { 97 insert(element, it); 98 } 99 } 100 insert_class_of_device(const common::ClassOfDevice & cod,BitInserter & it)101 void insert_class_of_device(const common::ClassOfDevice& cod, BitInserter& it) const { 102 for (const auto& element : cod.cod) { 103 insert(element, it); 104 } 105 } 106 }; 107 108 } // namespace packet 109 } // namespace bluetooth 110