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 <cassert>
20 #include <cstdint>
21 #include <forward_list>
22 #include <iterator>
23 #include <memory>
24 #include <vector>
25 
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(T) bytes using the iterator
42   template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
insert(T value,BitInserter & it)43   void insert(T value, BitInserter& it) const {
44     uint8_t* raw_bytes = (uint8_t*)&value;
45     for (size_t i = 0; i < sizeof(T); i++) {
46       if (little_endian == true) {
47         it.insert_byte(raw_bytes[i]);
48       } else {
49         it.insert_byte(raw_bytes[sizeof(T) - 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 T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
insert(T value,BitInserter & it,size_t num_bits)71   void insert(T value, BitInserter& it, size_t num_bits) const {
72     assert(num_bits <= (sizeof(T) * 8));
73 
74     for (size_t i = 0; i < num_bits / 8; i++) {
75       if (little_endian == true) {
76         it.insert_byte(static_cast<uint8_t>(static_cast<uint64_t>(value) >> (i * 8)));
77       } else {
78         it.insert_byte(static_cast<uint8_t>(static_cast<uint64_t>(value) >> (((num_bits / 8) - i - 1) * 8)));
79       }
80     }
81     if (num_bits % 8) {
82       it.insert_bits(static_cast<uint8_t>(static_cast<uint64_t>(value) >> ((num_bits / 8) * 8)), num_bits % 8);
83     }
84   }
85 
86   // Specialized insert that allows inserting enums without casting
87   template <typename Enum, typename std::enable_if<std::is_enum_v<Enum>, int>::type = 0>
insert(Enum value,BitInserter & it)88   inline void insert(Enum value, BitInserter& it) const {
89     using enum_type = typename std::underlying_type_t<Enum>;
90     static_assert(std::is_unsigned_v<enum_type>, "Enum type is signed. Did you forget to specify the enum size?");
91     insert<enum_type>(static_cast<enum_type>(value), it);
92   }
93 
94   // Write a vector of T using the iterator
95   template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
insert_vector(const std::vector<T> & vec,BitInserter & it)96   void insert_vector(const std::vector<T>& vec, BitInserter& it) const {
97     static_assert(
98         std::is_trivial<T>::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