jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
uuid.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef JAU_UUID_HPP_
27#define JAU_UUID_HPP_
28
29#include <cstring>
30#include <string>
31#include <memory>
32#include <cstdint>
33#include <vector>
34
35#include <jau/basic_types.hpp>
36#include <jau/secmem.hpp>
37
38namespace jau {
39
40/** \addtogroup NetUtils
41 *
42 * @{
43 */
44
45class uuid128_t; // forward
46
47/**
48 * Bluetooth UUID <https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/>
49 * <p>
50 * Bluetooth is LSB or Little-Endian!
51 * </p>
52 * <p>
53 * BASE_UUID '00000000-0000-1000-8000-00805F9B34FB'
54 * </p>
55 */
57
58class uuid_t {
59public:
60 /** Underlying integer value present octet count */
61 enum class TypeSize : jau::nsize_t {
63 };
64 static constexpr jau::nsize_t number(const TypeSize rhs) noexcept {
65 return static_cast<jau::nsize_t>(rhs);
66 }
67 static std::string getTypeSizeString(const TypeSize v) noexcept;
68
69private:
71
72protected:
73 uuid_t(TypeSize const type_) : type(type_) {}
74
75public:
76 static TypeSize toTypeSize(const jau::nsize_t size);
77 static std::unique_ptr<uuid_t> create(TypeSize const t, uint8_t const * const buffer, lb_endian_t const le_or_be);
78 static std::unique_ptr<uuid_t> create(const std::string& str);
79
80 virtual ~uuid_t() noexcept = default;
81
82 uuid_t(const uuid_t &o) noexcept = default;
83 uuid_t(uuid_t &&o) noexcept = default;
84 uuid_t& operator=(const uuid_t &o) noexcept = default;
85 uuid_t& operator=(uuid_t &&o) noexcept = default;
86
87 std::unique_ptr<uuid_t> clone() const noexcept;
88
89 /**
90 * Strict equality operator.
91 *
92 * Only returns true if type and value are equal.
93 *
94 * @param o other comparison argument
95 * @return true if equal, otherwise false.
96 */
97 bool operator==(uuid_t const &o) const noexcept;
98
99 /**
100 * Strict not-equal operator.
101 *
102 * Returns true if type and/or value are not equal.
103 *
104 * @param o other comparison argument
105 * @return true if equal, otherwise false.
106 */
107 bool operator!=(uuid_t const &o) const noexcept
108 { return !(*this == o); }
109
110 /**
111 * Relaxed equality operator.
112 *
113 * Returns true if both uuid values are equivalent.
114 *
115 * If their uuid_t type differs, i.e. their TypeSize,
116 * both values will be transformed to uuid128_t before comparison.
117 *
118 * Potential uuid128_t conversion is performed using toUUDI128(),
119 * placing the sub-uuid at index 12 on BT_BASE_UUID (default).
120 *
121 * @param o other comparison argument
122 * @return true if equal, otherwise false.
123 */
124 bool equivalent(uuid_t const &o) const noexcept;
125
126 TypeSize getTypeSize() const noexcept { return type; }
127 jau::nsize_t getTypeSizeInt() const noexcept { return uuid_t::number(type); }
128 std::string getTypeSizeString() const noexcept { return getTypeSizeString(type); }
129
130 uuid128_t toUUID128(uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const uuid32_le_octet_index=12) const noexcept;
131
132 /** returns the pointer to the uuid data of size getTypeSize() */
133 virtual const uint8_t * data() const noexcept = 0;
134
135 /**
136 * Returns the string representation in BE network order, i.e. `00000000-0000-1000-8000-00805F9B34FB`.
137 */
138 virtual std::string toString() const noexcept = 0;
139
140 /**
141 * Returns the uuid128_t string representation in BE network order, i.e. `00000000-0000-1000-8000-00805F9B34FB`.
142 */
143 virtual std::string toUUID128String(uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept = 0;
144
145 virtual jau::nsize_t put(uint8_t * const buffer, lb_endian_t const le_or_be) const noexcept = 0;
146};
147
148inline std::string to_string(const uuid_t::TypeSize v) noexcept {
150}
151
152class uuid16_t : public uuid_t {
153public:
154 uint16_t value;
155
156 uuid16_t(uint16_t const v) noexcept
158
159 uuid16_t(const std::string& str);
160
161 uuid16_t(uint8_t const * const buffer, lb_endian_t const le_or_be) noexcept
162 : uuid_t(TypeSize::UUID16_SZ), value(jau::get_uint16(buffer, le_or_be)) { }
163
164 uuid16_t(const uuid16_t &o) noexcept = default;
165 uuid16_t(uuid16_t &&o) noexcept = default;
166 uuid16_t& operator=(const uuid16_t &o) noexcept = default;
167 uuid16_t& operator=(uuid16_t &&o) noexcept = default;
168
169 const uint8_t * data() const noexcept override {
170 return static_cast<uint8_t*>(static_cast<void*>(const_cast<uint16_t*>(&value))); // NOLINT(bugprone-casting-through-void): Alignment OK - same as reinterpret_cast<T*>( p )
171 }
172 std::string toString() const noexcept override;
173 std::string toUUID128String(uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept override;
174
175 jau::nsize_t put(uint8_t * const buffer, lb_endian_t const le_or_be) const noexcept override {
176 jau::put_uint16(buffer, value, le_or_be);
177 return 2;
178 }
179};
180
181class uuid32_t : public uuid_t {
182public:
183 uint32_t value;
184
185 uuid32_t(uint32_t const v) noexcept
187
188 uuid32_t(const std::string& str);
189
190 uuid32_t(uint8_t const * const buffer, lb_endian_t const le_or_be) noexcept
191 : uuid_t(TypeSize::UUID32_SZ), value(jau::get_uint32(buffer, le_or_be)) { }
192
193 uuid32_t(const uuid32_t &o) noexcept = default;
194 uuid32_t(uuid32_t &&o) noexcept = default;
195 uuid32_t& operator=(const uuid32_t &o) noexcept = default;
196 uuid32_t& operator=(uuid32_t &&o) noexcept = default;
197
198 const uint8_t * data() const noexcept override {
199 return static_cast<uint8_t*>(static_cast<void*>(const_cast<uint32_t*>(&value))); // NOLINT(bugprone-casting-through-void): Alignment OK - same as reinterpret_cast<T*>( p )
200 }
201 std::string toString() const noexcept override;
202 std::string toUUID128String(uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept override;
203
204 jau::nsize_t put(uint8_t * const buffer, lb_endian_t const le_or_be) const noexcept override {
205 jau::put_uint32(buffer, value, le_or_be);
206 return 4;
207 }
208};
209
210class uuid128_t : public uuid_t {
211public:
213
214 uuid128_t() noexcept : uuid_t(TypeSize::UUID128_SZ) { zero_bytes_sec(value.data, sizeof(value)); }
215
218
219 uuid128_t(const std::string& str);
220
221 uuid128_t(uint8_t const * const buffer, lb_endian_t const le_or_be) noexcept
222 : uuid_t(TypeSize::UUID128_SZ), value(jau::get_uint128(buffer, le_or_be)) { }
223
224 uuid128_t(uuid16_t const & uuid16, uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const uuid16_le_octet_index=12) noexcept;
225
226 uuid128_t(uuid32_t const & uuid32, uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const uuid32_le_octet_index=12) noexcept;
227
228 uuid128_t(const uuid128_t &o) noexcept = default;
229 uuid128_t(uuid128_t &&o) noexcept = default;
230 uuid128_t& operator=(const uuid128_t &o) noexcept = default;
231 uuid128_t& operator=(uuid128_t &&o) noexcept = default;
232
233 const uint8_t * data() const noexcept override { return value.data; }
234 std::string toString() const noexcept override;
235 std::string toUUID128String(uuid128_t const & base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept override {
236 (void)base_uuid;
237 (void)le_octet_index;
238 return toString();
239 }
240
241 jau::nsize_t put(uint8_t * const buffer, lb_endian_t const le_or_be) const noexcept override {
242 jau::put_uint128(buffer, value, le_or_be);
243 return 16;
244 }
245};
246
247inline uuid16_t get_uuid16(uint8_t const * buffer) noexcept
248{
249 return uuid16_t(jau::get_uint16(buffer));
250}
251inline uuid16_t get_uuid16(uint8_t const * buffer, lb_endian_t const le_or_be) noexcept
252{
253 return uuid16_t(jau::get_uint16(buffer, le_or_be));
254}
255inline uuid32_t get_uuid32(uint8_t const * buffer) noexcept
256{
257 return uuid32_t(jau::get_uint32(buffer));
258}
259inline uuid32_t get_uuid32(uint8_t const * buffer, lb_endian_t const le_or_be) noexcept
260{
261 return uuid32_t(jau::get_uint32(buffer, le_or_be));
262}
263inline uuid128_t get_uuid128(uint8_t const * buffer) noexcept
264{
265 return uuid128_t(jau::get_uint128(buffer));
266}
267inline uuid128_t get_uuid128(uint8_t const * buffer, lb_endian_t const le_or_be) noexcept
268{
269 return uuid128_t(jau::get_uint128(buffer, le_or_be));
270}
271
272/**@}*/
273
274} /* namespace jau */
275
276#endif /* JAU_UUID_HPP_ */
std::string toString() const noexcept override
Returns the string representation in BE network order, i.e.
Definition uuid.cpp:177
uuid128_t() noexcept
Definition uuid.hpp:214
uuid128_t(jau::uint128dp_t const v) noexcept
Definition uuid.hpp:216
jau::nsize_t put(uint8_t *const buffer, lb_endian_t const le_or_be) const noexcept override
Definition uuid.hpp:241
std::string toUUID128String(uuid128_t const &base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept override
Returns the uuid128_t string representation in BE network order, i.e.
Definition uuid.hpp:235
const uint8_t * data() const noexcept override
returns the pointer to the uuid data of size getTypeSize()
Definition uuid.hpp:233
jau::uint128dp_t value
Definition uuid.hpp:212
uuid128_t(uint8_t const *const buffer, lb_endian_t const le_or_be) noexcept
Definition uuid.hpp:221
uuid16_t & operator=(uuid16_t &&o) noexcept=default
uuid16_t(uint8_t const *const buffer, lb_endian_t const le_or_be) noexcept
Definition uuid.hpp:161
uint16_t value
Definition uuid.hpp:154
const uint8_t * data() const noexcept override
returns the pointer to the uuid data of size getTypeSize()
Definition uuid.hpp:169
jau::nsize_t put(uint8_t *const buffer, lb_endian_t const le_or_be) const noexcept override
Definition uuid.hpp:175
uuid16_t(uuid16_t &&o) noexcept=default
uuid16_t & operator=(const uuid16_t &o) noexcept=default
uuid16_t(uint16_t const v) noexcept
Definition uuid.hpp:156
uuid16_t(const uuid16_t &o) noexcept=default
uuid32_t & operator=(uuid32_t &&o) noexcept=default
uuid32_t(const uuid32_t &o) noexcept=default
uuid32_t(uint8_t const *const buffer, lb_endian_t const le_or_be) noexcept
Definition uuid.hpp:190
uuid32_t(uuid32_t &&o) noexcept=default
uint32_t value
Definition uuid.hpp:183
jau::nsize_t put(uint8_t *const buffer, lb_endian_t const le_or_be) const noexcept override
Definition uuid.hpp:204
uuid32_t & operator=(const uuid32_t &o) noexcept=default
uuid32_t(uint32_t const v) noexcept
Definition uuid.hpp:185
const uint8_t * data() const noexcept override
returns the pointer to the uuid data of size getTypeSize()
Definition uuid.hpp:198
static TypeSize toTypeSize(const jau::nsize_t size)
Definition uuid.cpp:47
static constexpr jau::nsize_t number(const TypeSize rhs) noexcept
Definition uuid.hpp:64
virtual jau::nsize_t put(uint8_t *const buffer, lb_endian_t const le_or_be) const noexcept=0
uuid128_t toUUID128(uuid128_t const &base_uuid=BT_BASE_UUID, jau::nsize_t const uuid32_le_octet_index=12) const noexcept
Definition uuid.cpp:119
TypeSize getTypeSize() const noexcept
Definition uuid.hpp:126
virtual const uint8_t * data() const noexcept=0
returns the pointer to the uuid data of size getTypeSize()
TypeSize
Underlying integer value present octet count.
Definition uuid.hpp:61
uuid_t(TypeSize const type_)
Definition uuid.hpp:73
std::string getTypeSizeString() const noexcept
Definition uuid.hpp:128
virtual ~uuid_t() noexcept=default
virtual std::string toString() const noexcept=0
Returns the string representation in BE network order, i.e.
jau::nsize_t getTypeSizeInt() const noexcept
Definition uuid.hpp:127
bool equivalent(uuid_t const &o) const noexcept
Relaxed equality operator.
Definition uuid.cpp:109
static std::unique_ptr< uuid_t > create(TypeSize const t, uint8_t const *const buffer, lb_endian_t const le_or_be)
Definition uuid.cpp:56
std::unique_ptr< uuid_t > clone() const noexcept
Definition uuid.cpp:84
virtual std::string toUUID128String(uuid128_t const &base_uuid=BT_BASE_UUID, jau::nsize_t const le_octet_index=12) const noexcept=0
Returns the uuid128_t string representation in BE network order, i.e.
constexpr uint128dp_t get_uint128(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr uint16_t get_uint16(uint8_t const *buffer) noexcept
Returns a uint16_t value from the given byte address using packed_t to resolve a potential memory ali...
constexpr uint32_t get_uint32(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr void put_uint32(uint8_t *buffer, const uint32_t v) noexcept
See put_uint16() for reference.
lb_endian_t
Simplified reduced endian type only covering little- and big-endian.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr void put_uint16(uint8_t *buffer, const uint16_t v) noexcept
Put the given uint16_t value into the given byte address using packed_t to resolve a potential memory...
constexpr void put_uint128(uint8_t *buffer, const uint128dp_t &v) noexcept
See put_uint16() for reference.
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition int_types.hpp:55
uuid32_t get_uuid32(uint8_t const *buffer) noexcept
Definition uuid.hpp:255
uuid128_t BT_BASE_UUID
Bluetooth UUID https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
uuid16_t get_uuid16(uint8_t const *buffer) noexcept
Definition uuid.hpp:247
uuid128_t get_uuid128(uint8_t const *buffer) noexcept
Definition uuid.hpp:263
void zero_bytes_sec(void *s, size_t n) noexcept __attrdecl_no_optimize__
Wrapper to ::explicit_bzero(), ::bzero() or ::memset(), whichever is available in that order.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
STL namespace.
A 128-bit packed uint8_t data array.