ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
attribute_server.hpp
Go to the documentation of this file.
1#ifndef ELEC_C7222_BLE_GATT_ATTRIBUTE_SERVER_HPP_
2#define ELEC_C7222_BLE_GATT_ATTRIBUTE_SERVER_HPP_
3
4#include <cstdint>
5#include <list>
6
7#include "ble_error.hpp"
8#include "non_copyable.hpp"
9#include "service.hpp"
10#include "uuid.hpp"
11
12namespace c7222 {
13
139 public:
142 struct ReadResult {
144 uint16_t bytes = 0;
148 bool ok = true;
149 };
151
154
161 if(instance_ == nullptr) {
162 instance_ = new AttributeServer();
163 assert(instance_ != nullptr &&
164 "Failed to allocate AttributeServer singleton instance");
165 }
166 return instance_;
167 }
169
172
192 BleError Init(const void* context);
193
197 [[nodiscard]] bool IsInitialized() const {
198 return initialized_;
199 }
200
207 [[nodiscard]] const void* GetContext() const {
208 return context_;
209 }
210
214 [[nodiscard]] bool HasContext() const {
215 return context_ != nullptr;
216 }
218
221
226 [[nodiscard]] size_t GetServiceCount() const {
227 return services_.size();
228 }
229
230
237 Service& GetService(size_t index);
238
244 [[nodiscard]] const std::list<Service>& GetServices() const {
245 return services_;
246 }
247
253 std::list<Service>& GetServices() {
254 return services_;
255 }
256
263 [[nodiscard]] const Service& GetService(size_t index) const;
264
272
279 [[nodiscard]] const Service* FindServiceByUuid(const Uuid& uuid) const;
280
287 [[nodiscard]] bool HasServicesRequiringAuthentication() const;
288
292 [[nodiscard]] bool HasServicesRequiringEncryption() const;
293
300 [[nodiscard]] bool HasServicesRequiringAuthorization() const;
301
311 std::list<Characteristic*> FindCharacteristicByUuid(const Uuid& uuid);
312
322 [[nodiscard]] std::list<const Characteristic*> FindCharacteristicByUuid(const Uuid& uuid) const;
323
331
338 [[nodiscard]] const Characteristic* FindCharacteristicByHandle(uint16_t handle) const;
340
343
347 auto begin() {
348 return services_.begin();
349 }
350
355 auto end() {
356 return services_.end();
357 }
358
363 [[nodiscard]] auto begin() const {
364 return services_.begin();
365 }
366
371 [[nodiscard]] auto end() const {
372 return services_.end();
373 }
374
379 [[nodiscard]] auto cbegin() const {
380 return services_.cbegin();
381 }
382
387 [[nodiscard]] auto cend() const {
388 return services_.cend();
389 }
391
394
400 void SetConnectionHandle(uint16_t connection_handle);
401
405 void SetSecurityLevel(uint16_t connection_handle, uint8_t security_level);
406
410 [[nodiscard]] uint8_t GetSecurityLevel(uint16_t connection_handle) const;
411
415 void SetAuthorizationGranted(uint16_t connection_handle, bool granted);
416
420 [[nodiscard]] bool IsAuthorizationGranted(uint16_t connection_handle) const;
421
427 [[nodiscard]] uint16_t GetConnectionHandle() const {
428 return connection_handle_;
429 }
430
435 [[nodiscard]] bool HasConnectionHandle() const {
436 return connection_handle_ != 0;
437 }
438
444 [[nodiscard]] bool IsConnected() const {
445 return connection_handle_ != 0;
446 }
447
454 BleError DispatchBleHciPacket(uint8_t packet_type,
455 const uint8_t* packet_data,
456 uint16_t packet_data_size);
458
461
466 ReadResult ReadAttribute(uint16_t attribute_handle,
467 uint16_t offset,
468 uint8_t* buffer,
469 uint16_t buffer_size);
470
477 WriteAttribute(uint16_t attribute_handle, uint16_t offset, const uint8_t* data, uint16_t size);
479
482
490 friend std::ostream& operator<<(std::ostream& os, const AttributeServer& server);
492
495
504 void InitServices(std::list<Attribute>& attributes);
506
507 private:
510 AttributeServer() = default;
511 ~AttributeServer() = default;
513
516
521 Attribute* FindServiceAttributeByHandle(uint16_t handle);
525 [[nodiscard]] const Attribute* FindServiceAttributeByHandle(uint16_t handle) const;
529 Attribute* FindAttributeByHandle(uint16_t handle);
533 [[nodiscard]] const Attribute* FindAttributeByHandle(uint16_t handle) const;
534
540 static bool IsAttErrorCode(uint16_t value, BleError& out_error);
542
546 std::list<Service> services_;
548 const void* context_ = nullptr;
550 uint16_t connection_handle_ = 0;
552 uint8_t security_level_ = 0;
554 bool authorization_granted_ = false;
556 bool initialized_ = false;
558
562 static AttributeServer* instance_;
564};
565
566} // namespace c7222
567
568#endif // ELEC_C7222_BLE_GATT_ATTRIBUTE_SERVER_HPP_
BLE error codes.
Encapsulates the ATT attribute server for the Pico W BLE stack.
Definition attribute_server.hpp:138
void SetConnectionHandle(uint16_t connection_handle)
Set the active connection handle for all characteristics.
friend std::ostream & operator<<(std::ostream &os, const AttributeServer &server)
Stream insertion operator for attribute server information.
uint16_t GetConnectionHandle() const
Get the current connection handle.
Definition attribute_server.hpp:427
bool IsAuthorizationGranted(uint16_t connection_handle) const
Get cached authorization result for the active connection.
BleError Init(const void *context)
Initialize the ATT server from a platform context.
std::list< Characteristic * > FindCharacteristicByUuid(const Uuid &uuid)
Find characteristics by UUID.
const Service * FindServiceByUuid(const Uuid &uuid) const
Find a service by UUID (const version).
const Service & GetService(size_t index) const
Get a service by index (const version).
BleError WriteAttribute(uint16_t attribute_handle, uint16_t offset, const uint8_t *data, uint16_t size)
Handle an ATT write request (internal use).
ReadResult ReadAttribute(uint16_t attribute_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size)
Handle an ATT read request (internal use).
const std::list< Service > & GetServices() const
Get the list of services.
Definition attribute_server.hpp:244
bool HasContext() const
Check whether a platform context has been stored.
Definition attribute_server.hpp:214
auto begin() const
Get const iterator to first service.
Definition attribute_server.hpp:363
bool IsInitialized() const
Check whether the server was initialized.
Definition attribute_server.hpp:197
bool HasConnectionHandle() const
Check whether a connection handle is set.
Definition attribute_server.hpp:435
uint8_t GetSecurityLevel(uint16_t connection_handle) const
Get cached security level for the active connection.
static AttributeServer * GetInstance()
Get the singleton instance.
Definition attribute_server.hpp:160
size_t GetServiceCount() const
Get the number of parsed services.
Definition attribute_server.hpp:226
std::list< Service > & GetServices()
Get the list of services.
Definition attribute_server.hpp:253
BleError DispatchBleHciPacket(uint8_t packet_type, const uint8_t *packet_data, uint16_t packet_data_size)
Dispatch HCI ATT events to all characteristics.
bool HasServicesRequiringAuthorization() const
Check whether any service contains characteristics requiring authorization.
const Characteristic * FindCharacteristicByHandle(uint16_t handle) const
Find a characteristic by attribute handle (const version).
auto begin()
Get iterator to first service.
Definition attribute_server.hpp:347
Service * FindServiceByUuid(const Uuid &uuid)
Find a service by UUID.
auto cbegin() const
Get const iterator to first service.
Definition attribute_server.hpp:379
const void * GetContext() const
Get the stored platform context pointer.
Definition attribute_server.hpp:207
std::list< const Characteristic * > FindCharacteristicByUuid(const Uuid &uuid) const
Find characteristics by UUID (const version).
auto cend() const
Get const iterator to end of services.
Definition attribute_server.hpp:387
void SetAuthorizationGranted(uint16_t connection_handle, bool granted)
Update cached authorization result for the active connection.
bool IsConnected() const
Queries whether a client is connected to the server.
Definition attribute_server.hpp:444
Service & GetService(size_t index)
Get a service by index.
auto end()
Get iterator to end of services.
Definition attribute_server.hpp:355
void InitServices(std::list< Attribute > &attributes)
Initialize services from a parsed attribute list.
bool HasServicesRequiringAuthentication() const
Check whether any service contains characteristics requiring authentication.
void SetSecurityLevel(uint16_t connection_handle, uint8_t security_level)
Update cached security level for the active connection.
auto end() const
Get const iterator to end of services.
Definition attribute_server.hpp:371
Characteristic * FindCharacteristicByHandle(uint16_t handle)
Find a characteristic by attribute handle.
bool HasServicesRequiringEncryption() const
Check whether any service contains characteristics requiring encryption.
ATT attribute wrapper with BTstack-compatible fields.
Definition attribute.hpp:119
Represents a GATT Characteristic with its declaration, value, and descriptors.
Definition characteristic.hpp:229
Disable both copy and move operations.
Definition non_copyable.hpp:75
Represents a GATT Service with its characteristics and included services.
Definition service.hpp:119
UUID storage for 16-bit and 128-bit UUIDs.
Definition uuid.hpp:53
C7222 course abstractions namespace.
Definition ble.hpp:20
BleError
BLE error codes used across HCI/L2CAP/ATT/GATT and BTstack helpers.
Definition ble_error.hpp:19
@ kSuccess
Generic HCI status and controller errors.
Base classes to control copy/move semantics.
Definition attribute_server.hpp:142
uint16_t bytes
Number of bytes read on success.
Definition attribute_server.hpp:144
BleError error
ATT error to return when ok == false.
Definition attribute_server.hpp:146
bool ok
True when read succeeded and bytes is valid.
Definition attribute_server.hpp:148
GATT UUID wrapper.