pabigot  0.1.1
C++ support classes
ble.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2017-2019 Peter A. Bigot */
3 
7 #ifndef PABIGOT_BLE_HPP
8 #define PABIGOT_BLE_HPP
9 #pragma once
10 
11 #include <algorithm>
12 #include <array>
13 #include <chrono>
14 #include <cstring>
15 #include <stdexcept>
16 #include <string>
17 
18 #include <pabigot/byteorder.hpp>
19 
20 namespace pabigot {
21 
27 namespace ble {
28 
29 namespace details {
30 
33 template <unsigned int clock_bit>
34 using clk_type = std::chrono::duration<uint32_t, std::ratio<(1U << clock_bit), 3200>>;
35 
42 template <std::size_t nb>
43 class uuid_type : public std::array<uint8_t, nb>
44 {
45 public:
46  using super_type = std::array<uint8_t, nb>;
47 
49  static constexpr std::size_t byte_length = nb;
50 
52  static constexpr std::size_t bit_length = 8 * byte_length;
53 
54  // We don't inherit the base class constructors because gcc through 7.3
55  // doesn't handle them correctly. Something about the default constructor
56  // not working.
57  //
58  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83921
59 
60  constexpr uuid_type () :
61  super_type{}
62  { }
63 
64  constexpr uuid_type (const super_type& arr) :
65  super_type{arr}
66  { }
67 
68  constexpr uuid_type (super_type& arr) :
69  super_type{arr}
70  { }
71 
72  constexpr uuid_type (const uint8_t (&arr)[byte_length]) :
73  super_type{}
74  {
75  std::copy(arr, byte_length + arr, this->begin());
76  }
77 
78  constexpr uuid_type (uint8_t (&arr)[byte_length]) :
79  super_type{}
80  {
81  std::copy(arr, byte_length + arr, this->begin());
82  }
83 };
84 
89 template <typename Int>
90 constexpr Int
91 to_integer (const uuid_type<sizeof(Int)>& uuid) noexcept
92 {
93  using integer_type = Int;
94  union { // @todo c++20 use std::byte
95  integer_type integer;
96  uint8_t u8;
97  } u{};
98  std::copy(uuid.begin(), uuid.end(), &u.u8);
99  return byteorder::host_x_le(u.integer);
100 }
101 } // namespace details
102 
112 
121 
130 
133 {
135 
136 public:
138  using integer_type = uint16_t;
139 
140  // Expose base class constructors
141  using super_type::super_type;
142 
144  explicit constexpr uuid16_type (integer_type src) :
145  super_type{}
146  {
147  union { // @todo c++20 use std::byte
148  integer_type integer;
149  uint8_t u8;
150  } u{byteorder::host_x_le(src)};
151  std::copy(&u.u8, super_type::byte_length + &u.u8, begin());
152  }
153 
155  integer_type as_integer () const noexcept;
156 
161  std::string as_string () const noexcept;
162 };
163 
165 class uuid32_type : public details::uuid_type<4>
166 {
168 
169 public:
171  using integer_type = uint32_t;
172 
173  // Expose base class constructors
174  using super_type::super_type;
175 
177  explicit constexpr uuid32_type (integer_type src) :
178  super_type{}
179  {
180  union { // @todo c++20 use std::byte
181  integer_type integer;
182  uint8_t u8;
183  } u{byteorder::host_x_le(src)};
184  std::copy(&u.u8, super_type::byte_length + &u.u8, begin());
185  }
186 
188  integer_type as_integer () const noexcept
189  {
190  return details::to_integer<integer_type>(*this);
191  }
192 
197  std::string as_string () const noexcept;
198 };
199 
217 class uuid128_type : public details::uuid_type<16>
218 {
220 
221 public:
226 
227  // Expose base class constructors
228  using super_type::super_type;
229 
235  std::string as_string () const noexcept;
236 
241  bool base_match (const uuid128_type& other) const noexcept;
242 
247  uuid128_type from_uuid16 (const uuid16_type& uuid16) const noexcept;
248 
253  uuid128_type from_uuid16 (uint16_t uuid16) const noexcept
254  {
255  return from_uuid16(uuid16_type{uuid16});
256  }
257 
259  uuid16_type::integer_type uuid16 () const noexcept;
260 
267  uuid128_type swap_endian () const noexcept;
268 };
269 
270 } // namespace ble
271 } // namespace pabigot
272 
273 #if (PABIGOT_OPTION_FULLCPP - 0)
274 #include <pabigot/ble/fullcpp.hpp>
275 #endif /* PABIGOT_OPTION_FULLCPP */
276 
277 #endif /* PABIGOT_BLE_HPP */
pabigot::ble::clk1_type
details::clk_type< 1 > clk1_type
A duration type that measures in 625 us ticks.
Definition: ble.hpp:111
pabigot::ble::uuid16_type::uuid16_type
constexpr uuid16_type(integer_type src)
Construct an instance from a native integral UUID.
Definition: ble.hpp:144
pabigot::ble::uuid32_type::as_integer
integer_type as_integer() const noexcept
Convert the UUID to its host integral representation.
Definition: ble.hpp:188
pabigot::ble::uuid128_type::BLUETOOTH_BASE
static const uuid128_type BLUETOOTH_BASE
The Bluetooth Base UUID.
Definition: ble.hpp:225
pabigot::ble::uuid32_type::uuid32_type
constexpr uuid32_type(integer_type src)
Construct an instance from a native integral UUID.
Definition: ble.hpp:177
pabigot::ble::details::uuid_type::bit_length
static constexpr std::size_t bit_length
The length of the UUID in bits.
Definition: ble.hpp:52
pabigot::ble::details::uuid_type
Base class that captures 2, 4, or 16-byte UUIDs.
Definition: ble.hpp:43
byteorder.hpp
Support for byte order manipulation and packed storage.
pabigot::ble::uuid32_type::integer_type
uint32_t integer_type
Native integer type for 32-bit UUID.
Definition: ble.hpp:171
pabigot::ble::clk5_type
details::clk_type< 5 > clk5_type
A duration type that measures in 10 ms ticks.
Definition: ble.hpp:129
pabigot::ble::uuid16_type::integer_type
uint16_t integer_type
Native integer type for 16-bit UUID.
Definition: ble.hpp:138
pabigot::ble::details::clk_type
std::chrono::duration< uint32_t, std::ratio<(1U<< clock_bit), 3200 > > clk_type
Template for durations corresponding to bits in the 3.2 kHz Bluetooth native clock.
Definition: ble.hpp:34
pabigot
Root for all pabigot namespaces.
Definition: gap.hpp:15
pabigot::ble::details::to_integer
constexpr Int to_integer(const uuid_type< sizeof(Int)> &uuid) noexcept
Convert a small UUID to its native integer type.
Definition: ble.hpp:91
pabigot::byteorder::host_x_le
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type host_x_le(const T &v)
Convert between host and little-endian byte order.
Definition: byteorder.hpp:233
pabigot::ble::uuid16_type::as_integer
integer_type as_integer() const noexcept
Convert the UUID to its host integral representation.
pabigot::ble::uuid16_type::as_string
std::string as_string() const noexcept
Convert the UUID to its standard text representation.
pabigot::ble::uuid32_type
32-bit UUID type stored as a little-endian byte sequence.
Definition: ble.hpp:165
pabigot::ble::clk2_type
details::clk_type< 2 > clk2_type
A duration type that measures in 1.25 ms ticks.
Definition: ble.hpp:120
pabigot::ble::details::uuid_type::byte_length
static constexpr std::size_t byte_length
The length of the UUID in bytes.
Definition: ble.hpp:49
pabigot::ble::uuid128_type
Basic holder for 128-bit UUIDs stored as a little-endian byte sequence.
Definition: ble.hpp:217
pabigot::ble::uuid16_type
16-bit UUID type stored as a little-endian byte sequence.
Definition: ble.hpp:132