summaryrefslogtreecommitdiff
path: root/system/gd/packet/endian_inserter.h
blob: e4eab5326968ef1d699925c018e7c68033c7d1a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <cstdint>
#include <forward_list>
#include <iterator>
#include <memory>
#include <vector>

#include "os/log.h"
#include "packet/bit_inserter.h"
#include "packet/custom_field_fixed_size_interface.h"

namespace bluetooth {
namespace packet {

// Abstract base class that is subclassed to provide insert() functions.
// The template parameter little_endian controls the generation of insert().
template <bool little_endian>
class EndianInserter {
 public:
  EndianInserter() = default;
  virtual ~EndianInserter() = default;

 protected:
  // Write sizeof(FixedWidthPODType) bytes using the iterator
  template <typename FixedWidthPODType, typename std::enable_if<std::is_pod<FixedWidthPODType>::value, int>::type = 0>
  void insert(FixedWidthPODType value, BitInserter& it) const {
    uint8_t* raw_bytes = (uint8_t*)&value;
    for (size_t i = 0; i < sizeof(FixedWidthPODType); i++) {
      if (little_endian == true) {
        it.insert_byte(raw_bytes[i]);
      } else {
        it.insert_byte(raw_bytes[sizeof(FixedWidthPODType) - i - 1]);
      }
    }
  }

  // Write sizeof(FixedWidthCustomType) bytes using the iterator
  template <
      typename T,
      typename std::enable_if<std::is_base_of<CustomFieldFixedSizeInterface<T>, T>::value, int>::type = 0>
  void insert(const T& value, BitInserter& it) const {
    auto* raw_bytes = value.data();
    for (size_t i = 0; i < CustomFieldFixedSizeInterface<T>::length(); i++) {
      if (little_endian == true) {
        it.insert_byte(raw_bytes[i]);
      } else {
        it.insert_byte(raw_bytes[CustomFieldFixedSizeInterface<T>::length() - i - 1]);
      }
    }
  }

  // Write num_bits bits using the iterator
  template <typename FixedWidthIntegerType,
            typename std::enable_if<std::is_pod<FixedWidthIntegerType>::value, int>::type = 0>
  void insert(FixedWidthIntegerType value, BitInserter& it, size_t num_bits) const {
    ASSERT(num_bits <= (sizeof(FixedWidthIntegerType) * 8));

    for (size_t i = 0; i < num_bits / 8; i++) {
      if (little_endian == true) {
        it.insert_byte(static_cast<uint8_t>(value >> (i * 8)));
      } else {
        it.insert_byte(static_cast<uint8_t>(value >> (((num_bits / 8) - i - 1) * 8)));
      }
    }
    if (num_bits % 8) {
      it.insert_bits(static_cast<uint8_t>(value >> ((num_bits / 8) * 8)), num_bits % 8);
    }
  }

  // Specialized insert that allows inserting enums without casting
  template <typename Enum, typename std::enable_if<std::is_enum_v<Enum>, int>::type = 0>
  inline void insert(Enum value, BitInserter& it) const {
    using enum_type = typename std::underlying_type_t<Enum>;
    static_assert(std::is_unsigned_v<enum_type>, "Enum type is signed. Did you forget to specify the enum size?");
    insert<enum_type>(static_cast<enum_type>(value), it);
  }

  // Write a vector of FixedWidthIntegerType using the iterator
  template <typename FixedWidthIntegerType>
  void insert_vector(const std::vector<FixedWidthIntegerType>& vec, BitInserter& it) const {
    static_assert(std::is_pod<FixedWidthIntegerType>::value,
                  "EndianInserter::insert requires a vector with elements of a fixed-size.");
    for (const auto& element : vec) {
      insert(element, it);
    }
  }
};

}  // namespace packet
}  // namespace bluetooth