ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
ble_address.hpp
Go to the documentation of this file.
1
5#ifndef ELEC_C7222_BLE_ADDRESS_H_
6#define ELEC_C7222_BLE_ADDRESS_H_
7
8#include <array>
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <ostream>
13
14namespace c7222 {
15
44 public:
48 static constexpr size_t kLength = 6;
49
53 using RawAddress = std::array<uint8_t, kLength>;
54
58 enum class AddressType : uint8_t {
62 kLePublic = 0x00,
66 kLeRandom = 0x01,
70 kLePublicIdentity = 0x02,
74 kLeRandomIdentity = 0x03,
78 kSco = 0x04,
82 kAcl = 0x05,
86 kUnknown = 0xFF,
87 };
88
92 BleAddress() = default;
93
103 BleAddress(AddressType type, const RawAddress& address)
104 : type_(type), address_(address) {}
105
109 BleAddress(AddressType type, const uint8_t address[kLength])
110 : type_(type) {
111 std::copy(address, address + kLength, address_.begin());
112 }
113
118 return type_;
119 }
120
124 void SetType(AddressType type) {
125 type_ = type;
126 }
127
131 const uint8_t* GetBytes() const {
132 return address_.data();
133 }
134
138 const RawAddress& GetRaw() const {
139 return address_;
140 }
141
145 static const RawAddress& NullAddress() {
146 static const RawAddress addr = {};
147 return addr;
148 }
149
153 void CopyTo(RawAddress& out) const {
154 out = address_;
155 }
156
160 void CopyTo(uint8_t (&out)[kLength]) const {
161 std::copy(address_.begin(), address_.end(), out);
162 }
163
167 bool operator==(const BleAddress& other) const {
168 return type_ == other.type_ && address_ == other.address_;
169 }
170
174 bool operator!=(const BleAddress& other) const {
175 return !(*this == other);
176 }
177
181 friend std::ostream& operator<<(std::ostream& os, const BleAddress& addr);
182
183 private:
185 RawAddress address_{};
186};
187
188std::ostream& operator<<(std::ostream& os, const BleAddress& addr);
189
190} // namespace c7222
191
192#endif // ELEC_C7222_BLE_ADDRESS_H_
BLE address container with an associated address type.
Definition ble_address.hpp:43
bool operator!=(const BleAddress &other) const
Compares both address type and address bytes.
Definition ble_address.hpp:174
static constexpr size_t kLength
Number of bytes in a BLE device address.
Definition ble_address.hpp:48
void SetType(AddressType type)
Sets the address type.
Definition ble_address.hpp:124
static const RawAddress & NullAddress()
Returns a zero-filled address constant.
Definition ble_address.hpp:145
BleAddress()=default
Constructs an empty address with type Unknown and zero bytes.
const RawAddress & GetRaw() const
Returns a const reference to the raw address array.
Definition ble_address.hpp:138
bool operator==(const BleAddress &other) const
Compares both address type and address bytes.
Definition ble_address.hpp:167
void CopyTo(RawAddress &out) const
Copies the raw address into a caller-provided buffer.
Definition ble_address.hpp:153
AddressType
Bluetooth address types (BTstack-compatible values).
Definition ble_address.hpp:58
@ kUnknown
Unknown or not provided.
@ kAcl
BR/EDR (ACL) address type.
@ kLePublic
Public Device Address (Vol 3, Part C, 10.8.1).
@ kSco
BR/EDR (SCO) address type.
@ kLePublicIdentity
Public Identity Address (resolvable via IRK).
@ kLeRandomIdentity
Random (static or resolvable) Identity Address.
@ kLeRandom
Random Device Address (Vol 3, Part C, 10.8.2).
friend std::ostream & operator<<(std::ostream &os, const BleAddress &addr)
Stream output helper.
BleAddress(AddressType type, const uint8_t address[kLength])
Constructs from address type and a raw 6-byte buffer.
Definition ble_address.hpp:109
void CopyTo(uint8_t(&out)[kLength]) const
Copies the raw address into a caller-provided array.
Definition ble_address.hpp:160
AddressType GetType() const
Returns the stored address type.
Definition ble_address.hpp:117
std::array< uint8_t, kLength > RawAddress
Raw 6-byte address container.
Definition ble_address.hpp:53
BleAddress(AddressType type, const RawAddress &address)
Constructs from address type and raw Bluetooth address.
Definition ble_address.hpp:103
const uint8_t * GetBytes() const
Returns a pointer to the raw address bytes.
Definition ble_address.hpp:131
C7222 course abstractions namespace.
Definition ble.hpp:20
std::ostream & operator<<(std::ostream &os, const BleAddress &addr)