libGulliBLE
uuid.hpp
Go to the documentation of this file.
1//
2// Created by spak on 9/26/21.
3//
4
5#ifndef LIBGULLIBLE_UUID_HPP
6#define LIBGULLIBLE_UUID_HPP
7
8#include <array>
10#include <mlab/any_of.hpp>
11
12namespace ble {
13
14 constexpr std::array<uint8_t, 16> base_uuid = {
15 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
16 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
17
18 enum struct uuid_type : std::uint8_t {
19 _16 = BLE_UUID_TYPE_16,
20 _32 = BLE_UUID_TYPE_32,
21 _128 = BLE_UUID_TYPE_128
22 };
23
24 template <uuid_type>
25 struct uuid_data {
26 };
27
28 template <std::size_t Size>
30 std::array<std::uint8_t, Size> d{};
31
33
34 inline explicit uuid_data_with_length(std::array<std::uint8_t, Size> d_);
35 };
36
37 template <>
40
41 inline explicit uuid_data(std::uint16_t d_);
42
43 [[nodiscard]] inline std::uint16_t to_nimble() const;
44
45 [[nodiscard]] inline std::array<std::uint8_t, 16> expand() const;
46 };
47
48 template <>
51
52 inline explicit uuid_data(std::uint32_t d_);
53
54 [[nodiscard]] inline std::uint32_t to_nimble() const;
55
56 [[nodiscard]] inline std::array<std::uint8_t, 16> expand() const;
57 };
58
59 template <>
62 };
63
64 template <uuid_type>
65 struct uuid_cast_t {
66 };
67
68 static constexpr auto cast_uuid16 = uuid_cast_t<uuid_type::_16>{};
69 static constexpr auto cast_uuid32 = uuid_cast_t<uuid_type::_32>{};
70 static constexpr auto cast_uuid128 = uuid_cast_t<uuid_type::_128>{};
71
72 class uuid : public mlab::any_of<uuid_type, uuid_data> {
73 public:
74 uuid();
75
76 uuid(uuid const &other);
77
78 uuid &operator=(uuid const &other);
79
80 uuid(std::initializer_list<std::uint8_t> data);
81
83
85
86 explicit uuid(std::array<std::uint8_t, 2> data);
87
88 explicit uuid(std::array<std::uint8_t, 4> data);
89
90 explicit uuid(std::array<std::uint8_t, 16> data);
91
92 uuid(uuid_cast_t<uuid_type::_16>, std::initializer_list<std::uint8_t> data);
93
94 uuid(uuid_cast_t<uuid_type::_32>, std::initializer_list<std::uint8_t> data);
95
96 uuid(uuid_cast_t<uuid_type::_128>, std::initializer_list<std::uint8_t> data);
97
98 uuid(uuid &&) noexcept = default;
99
100 uuid &operator=(uuid &&) noexcept = default;
101
102 [[nodiscard]] static uuid from_nimble(ble_uuid_t const &ble_uuid);
103
104 [[nodiscard]] static uuid from_nimble(ble_uuid16_t const &ble_uuid);
105
106 [[nodiscard]] static uuid from_nimble(ble_uuid32_t const &ble_uuid);
107
108 [[nodiscard]] static uuid from_nimble(ble_uuid128_t const &ble_uuid);
109
110 [[nodiscard]] static uuid from_nimble(ble_uuid_any_t const &ble_uuid);
111
112 [[nodiscard]] ble_uuid_any_t to_nimble() const;
113
114 [[nodiscard]] bool operator==(uuid const &other) const;
115
116 [[nodiscard]] bool operator!=(uuid const &other) const;
117
118 [[nodiscard]] uuid as_uuid128() const;
119
120 [[nodiscard]] std::string to_string() const;
121 };
122
123}// namespace ble
124
125namespace ble {
126
127 template <std::size_t Size>
128 uuid_data_with_length<Size>::uuid_data_with_length(std::array<std::uint8_t, Size> d_) : d{d_} {}
129
131 : uuid_data{{std::uint8_t(d_ & 0xff), std::uint8_t((d_ >> 8) & 0xff)}} {}
132
134 : uuid_data{{std::uint8_t(d_ & 0xff), std::uint8_t((d_ >> 8) & 0xff),
135 std::uint8_t((d_ >> 16) & 0xff), std::uint8_t((d_ >> 24) & 0xff)}} {}
136
138 return std::uint16_t(d[0]) | (std::uint16_t(d[1]) << 8);
139 }
140
142 return std::uint16_t(d[0]) | (std::uint16_t(d[1]) << 8) | (std::uint16_t(d[2]) << 16) |
143 (std::uint16_t(d[3]) << 24);
144 }
145
146 std::array<std::uint8_t, 16> uuid_data<uuid_type::_16>::expand() const {
147 auto new_d = base_uuid;
148 new_d[12] = d[0];
149 new_d[13] = d[1];
150 return new_d;
151 }
152
153 std::array<std::uint8_t, 16> uuid_data<uuid_type::_32>::expand() const {
154 auto new_d = base_uuid;
155 new_d[12] = d[0];
156 new_d[13] = d[1];
157 new_d[14] = d[2];
158 new_d[15] = d[3];
159 return new_d;
160 }
161}// namespace ble
162#endif//LIBGULLIBLE_UUID_HPP
mlab::bin_data & data
Definition: characteristic.cpp:49
Definition: uuid.hpp:72
std::string to_string() const
Definition: uuid.cpp:86
uuid & operator=(uuid const &other)
Definition: uuid.cpp:56
uuid as_uuid128() const
Definition: uuid.cpp:73
ble_uuid_any_t to_nimble() const
Definition: uuid.cpp:181
uuid(uuid &&) noexcept=default
uuid()
Definition: uuid.cpp:10
static uuid from_nimble(ble_uuid_t const &ble_uuid)
Definition: uuid.cpp:123
Definition: characteristic.hpp:14
std::uint16_t to_nimble(access_type a)
Definition: characteristic.hpp:212
static constexpr auto cast_uuid16
Definition: uuid.hpp:68
constexpr std::array< uint8_t, 16 > base_uuid
Definition: uuid.hpp:14
static constexpr auto cast_uuid128
Definition: uuid.hpp:70
static constexpr auto cast_uuid32
Definition: uuid.hpp:69
uuid_type
Definition: uuid.hpp:18
Definition: uuid.hpp:65
Definition: uuid.hpp:29
std::array< std::uint8_t, Size > d
Definition: uuid.hpp:30
uuid_data_with_length(std::array< std::uint8_t, Size > d_)
Definition: uuid.hpp:128
Definition: uuid.hpp:25