1#ifndef ELEC_C7222_BLE_GATT_ATTRIBUTE_SERVER_HPP_
2#define ELEC_C7222_BLE_GATT_ATTRIBUTE_SERVER_HPP_
161 if(instance_ ==
nullptr) {
163 assert(instance_ !=
nullptr &&
164 "Failed to allocate AttributeServer singleton instance");
215 return context_ !=
nullptr;
227 return services_.size();
348 return services_.begin();
356 return services_.end();
364 return services_.begin();
371 [[nodiscard]]
auto end()
const {
372 return services_.end();
380 return services_.cbegin();
387 [[nodiscard]]
auto cend()
const {
388 return services_.cend();
428 return connection_handle_;
436 return connection_handle_ != 0;
445 return connection_handle_ != 0;
455 const uint8_t* packet_data,
456 uint16_t packet_data_size);
469 uint16_t buffer_size);
477 WriteAttribute(uint16_t attribute_handle, uint16_t offset,
const uint8_t* data, uint16_t size);
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;
540 static bool IsAttErrorCode(uint16_t value,
BleError& out_error);
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;
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