ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
uuid.hpp
Go to the documentation of this file.
1
5#ifndef ELEC_C7222_BLE_GATT_UUID_H_
6#define ELEC_C7222_BLE_GATT_UUID_H_
7
8#include <algorithm>
9#include <array>
10#include <cassert>
11#include <cstddef>
12#include <cstdint>
13#include <ostream>
14
15#include <bluetooth_gatt.h> // For BTstack defined UUIDs
16namespace c7222 {
17
53class Uuid {
54 public:
58 enum class Type : uint8_t {
60 Invalid = 0,
62 k16Bit = 1,
64 k128Bit = 2
65 };
66
96 Uuid() = default;
97
99 Uuid(const Uuid&) = default;
100
102 Uuid(Uuid&&) noexcept = default;
103
105 Uuid& operator=(const Uuid&) = default;
106
108 Uuid& operator=(Uuid&&) noexcept = default;
109
114 explicit Uuid(uint16_t uuid16) : type_(Type::k16Bit) {
115 auto* ptr = reinterpret_cast<uint8_t*>(&uuid16);
116 std::fill(uuid_.begin(), uuid_.end(), 0);
117 std::copy(ptr, ptr + sizeof(uuid16), uuid_.begin());
118 }
119
128 Uuid(const uint8_t* uuid, size_t size);
129
134 explicit Uuid(const std::array<uint8_t, 16>& uuid) : type_(Type::k128Bit), uuid_(uuid) {}
135
140 explicit Uuid(const std::array<uint8_t, 2>& uuid) : type_(Type::k16Bit) {
141 std::copy(uuid.begin(), uuid.end(), uuid_.begin());
142 }
143
147 [[nodiscard]] Type type() const {
148 return type_;
149 }
150
154 [[nodiscard]] bool Is16Bit() const {
155 return type_ == Type::k16Bit;
156 }
157
161 [[nodiscard]] bool Is128Bit() const;
162
166 [[nodiscard]] bool IsValid() const {
167 return type_ != Type::Invalid;
168 }
169
174 [[nodiscard]] uint16_t Get16Bit() const {
175 assert(type_ == Type::k16Bit && "UUID is not 16-bit");
176 uint16_t ret;
177 auto* ptr = reinterpret_cast<uint8_t*>(&ret);
178 std::copy(uuid_.begin(), uuid_.begin() + 2, ptr);
179 return ret;
180 }
181
186 [[nodiscard]] const std::array<uint8_t, 16>& Get128Bit() const {
187 assert(type_ == Type::k128Bit && "UUID is not 128-bit");
188 return uuid_;
189 }
190
197 [[nodiscard]] const uint8_t* data() const {
198 return uuid_.data();
199 }
200
206 static Uuid Convert16To128(const Uuid& uuid16);
207
208 // ========== GATT Attribute Type Helpers ==========
209 // These helpers return false for non-16-bit/custom UUIDs.
210
215
220
225
230
235
240
245
250
255
260
264 static bool IsPrimaryServiceDeclaration(const Uuid& uuid);
265
269 static bool IsSecondaryServiceDeclaration(const Uuid& uuid);
270
274 static bool IsIncludedServiceDeclaration(const Uuid& uuid);
275
279 static bool IsCharacteristicDeclaration(const Uuid& uuid);
280
284 static bool IsServiceDeclaration(const Uuid& uuid);
285
290
295
299 static bool IsCharacteristicUserDescription(const Uuid& uuid);
300
304 static bool IsCharacteristicExtendedProperties(const Uuid& uuid);
305
309 static bool IsDescriptor(const Uuid& uuid);
310
316 bool operator==(const Uuid& other) const;
317
323 bool operator!=(const Uuid& other) const {
324 return !(*this == other);
325 }
326
333 friend std::ostream& operator<<(std::ostream& os, const Uuid& uuid);
334
335 private:
337 Type type_ = Type::Invalid;
339 std::array<uint8_t, 16> uuid_{};
340};
341
347std::ostream& operator<<(std::ostream& os, const Uuid& uuid);
348
349} // namespace c7222
350
351#endif // ELEC_C7222_BLE_GATT_UUID_H_
UUID storage for 16-bit and 128-bit UUIDs.
Definition uuid.hpp:53
AttributeType
Standard GATT Attribute Type UUIDs (16-bit).
Definition uuid.hpp:73
@ kIncludedServiceDeclaration
Included Service Declaration attribute (0x2802).
@ kPrimaryServiceDeclaration
Primary Service Declaration attribute (0x2800).
@ kCharacteristicUserDescription
Characteristic User Description Descriptor (0x2901).
@ kClientCharacteristicConfiguration
Client Characteristic Configuration Descriptor (0x2902).
@ kCharacteristicPresentationFormat
Characteristic Presentation Format Descriptor (0x2904).
@ kCharacteristicAggregateFormat
Characteristic Aggregate Format Descriptor (0x2905).
@ kCharacteristicDeclaration
Characteristic Declaration attribute (0x2803).
@ kServerCharacteristicConfiguration
Server Characteristic Configuration Descriptor (0x2903).
@ kCharacteristicExtendedProperties
Characteristic Extended Properties Descriptor (0x2900).
@ kSecondaryServiceDeclaration
Secondary Service Declaration attribute (0x2801).
static bool IsSecondaryServiceDeclaration(const Uuid &uuid)
Check if UUID is Secondary Service Declaration (0x2801).
bool Is128Bit() const
Returns true if this UUID is 128-bit.
Uuid(const std::array< uint8_t, 2 > &uuid)
Constructs a 16-bit UUID from an array.
Definition uuid.hpp:140
Uuid(Uuid &&) noexcept=default
Move-constructs a UUID.
bool operator==(const Uuid &other) const
Equality comparison based on UUID type and value.
Type
UUID representation type.
Definition uuid.hpp:58
@ Invalid
Uninitialized or invalid UUID.
@ k16Bit
16-bit UUID stored in the first two bytes.
@ k128Bit
128-bit UUID stored in the full array.
uint16_t Get16Bit() const
Returns the 16-bit UUID value.
Definition uuid.hpp:174
const std::array< uint8_t, 16 > & Get128Bit() const
Returns the 128-bit UUID bytes.
Definition uuid.hpp:186
static bool IsServerCharacteristicConfiguration(const Uuid &uuid)
Check if UUID is Server Characteristic Configuration (0x2903).
static Uuid PrimaryServiceDeclaration()
Create a Primary Service Declaration UUID (0x2800).
Uuid(const Uuid &)=default
Copy-constructs a UUID.
static Uuid CharacteristicExtendedProperties()
Create a Characteristic Extended Properties UUID (0x2900).
static bool IsDescriptor(const Uuid &uuid)
Check if UUID matches a known descriptor type.
Uuid()=default
Constructs an invalid/empty UUID.
Uuid(const std::array< uint8_t, 16 > &uuid)
Constructs a 128-bit UUID from an array.
Definition uuid.hpp:134
bool operator!=(const Uuid &other) const
Inequality comparison based on UUID type and value.
Definition uuid.hpp:323
static Uuid ClientCharacteristicConfiguration()
Create a Client Characteristic Configuration UUID (0x2902).
static Uuid CharacteristicUserDescription()
Create a Characteristic User Description UUID (0x2901).
static bool IsCharacteristicUserDescription(const Uuid &uuid)
Check if UUID is Characteristic User Description (0x2901).
static Uuid CharacteristicAggregateFormat()
Create a Characteristic Aggregate Format UUID (0x2905).
static bool IsIncludedServiceDeclaration(const Uuid &uuid)
Check if UUID is Included Service Declaration (0x2802).
static Uuid CharacteristicDeclaration()
Create a Characteristic Declaration UUID (0x2803).
static bool IsCharacteristicExtendedProperties(const Uuid &uuid)
Check if UUID is Characteristic Extended Properties (0x2900).
static bool IsPrimaryServiceDeclaration(const Uuid &uuid)
Check if UUID is Primary Service Declaration (0x2800).
static Uuid Convert16To128(const Uuid &uuid16)
Converts a 16-bit UUID to a 128-bit Bluetooth base UUID.
static Uuid ServerCharacteristicConfiguration()
Create a Server Characteristic Configuration UUID (0x2903).
Type type() const
Returns the UUID type.
Definition uuid.hpp:147
static Uuid IncludedServiceDeclaration()
Create an Included Service Declaration UUID (0x2802).
static bool IsCharacteristicDeclaration(const Uuid &uuid)
Check if UUID is Characteristic Declaration (0x2803).
const uint8_t * data() const
Returns the raw UUID storage pointer.
Definition uuid.hpp:197
bool IsValid() const
Returns true if the UUID has been initialized to 16- or 128-bit.
Definition uuid.hpp:166
static bool IsClientCharacteristicConfiguration(const Uuid &uuid)
Check if UUID is Client Characteristic Configuration (0x2902).
static bool IsServiceDeclaration(const Uuid &uuid)
Check if UUID is any Service Declaration (Primary or Secondary).
static Uuid CharacteristicPresentationFormat()
Create a Characteristic Presentation Format UUID (0x2904).
Uuid(const uint8_t *uuid, size_t size)
Constructs a UUID from a raw byte buffer.
bool Is16Bit() const
Returns true if this UUID is 16-bit.
Definition uuid.hpp:154
static Uuid SecondaryServiceDeclaration()
Create a Secondary Service Declaration UUID (0x2801).
friend std::ostream & operator<<(std::ostream &os, const Uuid &uuid)
Prints the UUID to a stream.
C7222 course abstractions namespace.
Definition ble.hpp:20
std::ostream & operator<<(std::ostream &os, const BleAddress &addr)