libGulliBLE
Toggle main menu visibility
Loading...
Searching...
No Matches
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>
9
#include <
ble/nimble_host_guard.hpp
>
10
#include <mlab/any_of.hpp>
11
12
namespace
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
<uu
id
_type>
25
struct
uuid_data
{
26
};
27
28
template
<std::
size_t
Size>
29
struct
uuid_data_with_length
{
30
std::array<std::uint8_t, Size>
d
{};
31
32
uuid_data_with_length
() =
default
;
33
34
inline
explicit
uuid_data_with_length
(std::array<std::uint8_t, Size> d_);
35
};
36
37
template
<>
38
struct
uuid_data
<
uuid_type
::
_16
> :
public
uuid_data_with_length
<2> {
39
using
uuid_data_with_length
<2>
::uuid_data_with_length
;
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
<>
49
struct
uuid_data
<
uuid_type
::
_32
> :
public
uuid_data_with_length
<4> {
50
using
uuid_data_with_length
<4>
::uuid_data_with_length
;
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
<>
60
struct
uuid_data
<
uuid_type
::
_128
> :
public
uuid_data_with_length
<16> {
61
using
uuid_data_with_length
<16>
::uuid_data_with_length
;
62
};
63
64
template
<uu
id
_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
82
uuid
(
uuid_cast_t<uuid_type::_16>
, std::uint16_t data);
83
84
uuid
(
uuid_cast_t<uuid_type::_32>
, std::uint32_t data);
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
125
namespace
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
130
uuid_data<uuid_type::_16>::uuid_data
(std::uint16_t d_)
131
:
uuid_data
{{std::uint8_t(d_ & 0xff), std::uint8_t((d_ >> 8) & 0xff)}} {}
132
133
uuid_data<uuid_type::_32>::uuid_data
(std::uint32_t d_)
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
137
std::uint16_t
uuid_data<uuid_type::_16>::to_nimble
()
const
{
138
return
std::uint16_t(
d
[0]) | (std::uint16_t(
d
[1]) << 8);
139
}
140
141
std::uint32_t
uuid_data<uuid_type::_32>::to_nimble
()
const
{
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
ble::uuid::to_string
std::string to_string() const
Definition
uuid.cpp:86
ble::uuid::operator=
uuid & operator=(uuid const &other)
Definition
uuid.cpp:56
ble::uuid::as_uuid128
uuid as_uuid128() const
Definition
uuid.cpp:73
ble::uuid::to_nimble
ble_uuid_any_t to_nimble() const
Definition
uuid.cpp:181
ble::uuid::uuid
uuid(uuid &&) noexcept=default
ble::uuid::uuid
uuid()
Definition
uuid.cpp:10
ble::uuid::from_nimble
static uuid from_nimble(ble_uuid_t const &ble_uuid)
Definition
uuid.cpp:123
ble
Definition
characteristic.cpp:11
ble::to_nimble
std::uint16_t to_nimble(access_type a)
Definition
characteristic.hpp:212
ble::cast_uuid16
static constexpr auto cast_uuid16
Definition
uuid.hpp:68
ble::base_uuid
constexpr std::array< uint8_t, 16 > base_uuid
Definition
uuid.hpp:14
ble::cast_uuid128
static constexpr auto cast_uuid128
Definition
uuid.hpp:70
ble::cast_uuid32
static constexpr auto cast_uuid32
Definition
uuid.hpp:69
ble::uuid_type
uuid_type
Definition
uuid.hpp:18
ble::uuid_type::_32
@ _32
Definition
uuid.hpp:20
ble::uuid_type::_128
@ _128
Definition
uuid.hpp:21
ble::uuid_type::_16
@ _16
Definition
uuid.hpp:19
nimble_host_guard.hpp
ble::uuid_cast_t
Definition
uuid.hpp:65
ble::uuid_data< uuid_type::_16 >::expand
std::array< std::uint8_t, 16 > expand() const
Definition
uuid.hpp:146
ble::uuid_data< uuid_type::_16 >::uuid_data
uuid_data(std::uint16_t d_)
Definition
uuid.hpp:130
ble::uuid_data< uuid_type::_32 >::uuid_data
uuid_data(std::uint32_t d_)
Definition
uuid.hpp:133
ble::uuid_data< uuid_type::_32 >::expand
std::array< std::uint8_t, 16 > expand() const
Definition
uuid.hpp:153
ble::uuid_data_with_length::d
std::array< std::uint8_t, Size > d
Definition
uuid.hpp:30
ble::uuid_data_with_length::uuid_data_with_length
uuid_data_with_length()=default
ble::uuid_data
Definition
uuid.hpp:25
libgullible
include
ble
uuid.hpp
Generated by
1.17.0