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