|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
Encapsulates the ATT attribute server for the Pico W BLE stack. More...
#include <attribute_server.hpp>


Classes | |
| struct | ReadResult |
Public Member Functions | |
Initialization and State | |
| BleError | Init (const void *context) |
| Initialize the ATT server from a platform context. | |
| bool | IsInitialized () const |
| Check whether the server was initialized. | |
| const void * | GetContext () const |
| Get the stored platform context pointer. | |
| bool | HasContext () const |
| Check whether a platform context has been stored. | |
Service and Characteristic Lookup | |
| size_t | GetServiceCount () const |
| Get the number of parsed services. | |
| Service & | GetService (size_t index) |
| Get a service by index. | |
| const std::list< Service > & | GetServices () const |
| Get the list of services. | |
| std::list< Service > & | GetServices () |
| Get the list of services. | |
| const Service & | GetService (size_t index) const |
| Get a service by index (const version). | |
| Service * | FindServiceByUuid (const Uuid &uuid) |
| Find a service by UUID. | |
| const Service * | FindServiceByUuid (const Uuid &uuid) const |
| Find a service by UUID (const version). | |
| bool | HasServicesRequiringAuthentication () const |
| Check whether any service contains characteristics requiring authentication. | |
| bool | HasServicesRequiringEncryption () const |
| Check whether any service contains characteristics requiring encryption. | |
| bool | HasServicesRequiringAuthorization () const |
| Check whether any service contains characteristics requiring authorization. | |
| std::list< Characteristic * > | FindCharacteristicByUuid (const Uuid &uuid) |
| Find characteristics by UUID. | |
| std::list< const Characteristic * > | FindCharacteristicByUuid (const Uuid &uuid) const |
| Find characteristics by UUID (const version). | |
| Characteristic * | FindCharacteristicByHandle (uint16_t handle) |
| Find a characteristic by attribute handle. | |
| const Characteristic * | FindCharacteristicByHandle (uint16_t handle) const |
| Find a characteristic by attribute handle (const version). | |
Iteration | |
| auto | begin () |
| Get iterator to first service. | |
| auto | end () |
| Get iterator to end of services. | |
| auto | begin () const |
| Get const iterator to first service. | |
| auto | end () const |
| Get const iterator to end of services. | |
| auto | cbegin () const |
| Get const iterator to first service. | |
| auto | cend () const |
| Get const iterator to end of services. | |
Connection and Event Routing | |
| void | SetConnectionHandle (uint16_t connection_handle) |
| Set the active connection handle for all characteristics. | |
| void | SetSecurityLevel (uint16_t connection_handle, uint8_t security_level) |
| Update cached security level for the active connection. | |
| uint8_t | GetSecurityLevel (uint16_t connection_handle) const |
| Get cached security level for the active connection. | |
| void | SetAuthorizationGranted (uint16_t connection_handle, bool granted) |
| Update cached authorization result for the active connection. | |
| bool | IsAuthorizationGranted (uint16_t connection_handle) const |
| Get cached authorization result for the active connection. | |
| uint16_t | GetConnectionHandle () const |
| Get the current connection handle. | |
| void | SetDisconnected () |
| bool | HasConnectionHandle () const |
| Check whether a connection handle is set. | |
| bool | IsConnected () const |
| Queries whether a client is connected to the server. | |
| BleError | DispatchBleHciPacket (uint8_t packet_type, const uint8_t *packet_data, uint16_t packet_data_size) |
| Dispatch HCI ATT events to all characteristics. | |
ATT Callbacks (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). | |
| BleError | WriteAttribute (uint16_t attribute_handle, uint16_t offset, const uint8_t *data, uint16_t size) |
| Handle an ATT write request (internal use). | |
Service Parsing Helpers | |
| void | InitServices (std::list< Attribute > &attributes) |
| Initialize services from a parsed attribute list. | |
Public Member Functions inherited from c7222::NonCopyable | |
| NonCopyable (const NonCopyable &)=delete | |
| NonCopyable & | operator= (const NonCopyable &)=delete |
| NonCopyable (NonCopyable &&)=default | |
| NonCopyable & | operator= (NonCopyable &&)=default |
Public Member Functions inherited from c7222::NonMovable | |
| NonMovable (const NonMovable &)=default | |
| NonMovable & | operator= (const NonMovable &)=default |
| NonMovable (NonMovable &&)=delete | |
| NonMovable & | operator= (NonMovable &&)=delete |
Static Public Member Functions | |
Construction and Lifetime | |
| static AttributeServer * | GetInstance () |
| Get the singleton instance. | |
Friends | |
Stream Output | |
| std::ostream & | operator<< (std::ostream &os, const AttributeServer &server) |
| Stream insertion operator for attribute server information. | |
Additional Inherited Members | |
Protected Member Functions inherited from c7222::NonCopyableNonMovable | |
| NonCopyableNonMovable ()=default | |
| ~NonCopyableNonMovable ()=default | |
Protected Member Functions inherited from c7222::NonCopyable | |
| NonCopyable ()=default | |
| ~NonCopyable ()=default | |
Protected Member Functions inherited from c7222::NonMovable | |
| NonMovable ()=default | |
| ~NonMovable ()=default | |
Encapsulates the ATT attribute server for the Pico W BLE stack.
This class parses the compiled ATT database into GATT Service/Characteristic objects and routes ATT read/write requests to the appropriate handlers. It also forwards HCI ATT events to characteristics for indications and flow-control callbacks.
Attribute objects, then InitServices() builds Service and Characteristic objects in discovery order.Characteristic or service-level Attribute handlers.The server now exposes aggregate security checks across all parsed services:
HasServicesRequiringAuthentication()HasServicesRequiringEncryption()HasServicesRequiringAuthorization()These helpers are intended to guide SecurityManager enablement and configuration before connections are established.
The server caches the active connection handle and the security state associated with that connection:
SetConnectionHandle() updates the handle propagated to characteristics.SetSecurityLevel() caches the GAP security level (used by CCCD/SCCD authorization checks).SetAuthorizationGranted() caches per-connection authorization decisions.Platform glue (e.g., BTstack packet handlers) should update these values when GAP/SM events are received so characteristic descriptor writes can be validated consistently.
BTstack exposes the ATT server through a C API:
att_server_init(att_db, read_cb, write_cb) registers the database and callbacks used when a client reads/writes an attribute.This class wraps that model as follows:
ReadAttribute() locates the target Attribute/Characteristic, checks permissions, then calls Characteristic::HandleAttributeRead() or Attribute::InvokeReadCallback(). ATT error codes are mapped through BleError and returned to BTstack via the platform glue.WriteAttribute() routes writes to Characteristic::HandleAttributeWrite() or Attribute::InvokeWriteCallback(), enforcing ATT write permissions. Errors are mapped back to BTstack.DispatchBleHciPacket() fans out HCI events to all characteristics so indication completion and ATT_EVENT_CAN_SEND_NOW are handled in one place.The class itself is platform-agnostic; the BTstack binding and ATT DB parsing live in the platform implementation (platform/rpi_pico/attribute_server.cpp).
On RP2040/Pico W, Init() treats the context pointer as the BTstack ATT DB blob (att_db.h/att_db.c) and:
att_server_init().Attribute objects and calls InitServices().The context pointer is cached the first time Init() is called. Subsequent calls reuse the stored context pointer so the ATT DB remains stable.
The ATT DB blob must remain valid for the lifetime of the server because parsed static attributes point directly into the DB memory.
AttributeServer instance lifetime.Init() runs.The following methods exist to integrate with the BLE stack and platform glue. They are not part of the public application API and should be considered reserved for internal use:
InitServices() (parses service/characteristic structure)ReadAttribute() / WriteAttribute() (ATT callbacks)DispatchBleHciPacket() (HCI event fan-out)
|
inline |
Get iterator to first service.
|
inline |
Get const iterator to first service.
|
inline |
Get const iterator to first service.
|
inline |
Get const iterator to end of services.
| BleError c7222::AttributeServer::DispatchBleHciPacket | ( | uint8_t | packet_type, |
| const uint8_t * | packet_data, | ||
| uint16_t | packet_data_size | ||
| ) |
Dispatch HCI ATT events to all characteristics.
This should be called from the BLE packet handler so that ATT indication completion and can-send-now flow control are handled by characteristics.
|
inline |
Get iterator to end of services.
|
inline |
Get const iterator to end of services.
| Characteristic * c7222::AttributeServer::FindCharacteristicByHandle | ( | uint16_t | handle | ) |
Find a characteristic by attribute handle.
| handle | ATT handle to search for (value, declaration, or descriptor). |

| const Characteristic * c7222::AttributeServer::FindCharacteristicByHandle | ( | uint16_t | handle | ) | const |
Find a characteristic by attribute handle (const version).
| handle | ATT handle to search for (value, declaration, or descriptor). |
| std::list< Characteristic * > c7222::AttributeServer::FindCharacteristicByUuid | ( | const Uuid & | uuid | ) |
Find characteristics by UUID.
Multiple characteristics can share a UUID (e.g., replicated instances), so this returns all matches in discovery order.
| uuid | Characteristic UUID to search for. |
| std::list< const Characteristic * > c7222::AttributeServer::FindCharacteristicByUuid | ( | const Uuid & | uuid | ) | const |
Find characteristics by UUID (const version).
Multiple characteristics can share a UUID (e.g., replicated instances), so this returns all matches in discovery order.
| uuid | Characteristic UUID to search for. |
Find a service by UUID.
| uuid | Service UUID to search for. |

Find a service by UUID (const version).
| uuid | Service UUID to search for. |
|
inline |
Get the current connection handle.
Returns 0 when disconnected or not set.
|
inline |
Get the stored platform context pointer.
On Pico W this is the ATT DB blob pointer; other platforms may store different context data.
|
inlinestatic |
Get the singleton instance.
The server is a process-wide singleton because BTstack exposes a single global ATT server for the device.
| uint8_t c7222::AttributeServer::GetSecurityLevel | ( | uint16_t | connection_handle | ) | const |
Get cached security level for the active connection.
| Service & c7222::AttributeServer::GetService | ( | size_t | index | ) |
Get a service by index.
| index | Service index in discovery order. |
| const Service & c7222::AttributeServer::GetService | ( | size_t | index | ) | const |
Get a service by index (const version).
| index | Service index in discovery order. |
|
inline |
Get the number of parsed services.
Returns the count of services parsed from the ATT DB in discovery order.
|
inline |
Get the list of services.
|
inline |
Get the list of services.
|
inline |
Check whether a connection handle is set.
|
inline |
Check whether a platform context has been stored.
| bool c7222::AttributeServer::HasServicesRequiringAuthentication | ( | ) | const |
Check whether any service contains characteristics requiring authentication.
This aggregates the per-service checks and is intended to guide SecurityManager configuration before connections are established.
| bool c7222::AttributeServer::HasServicesRequiringAuthorization | ( | ) | const |
Check whether any service contains characteristics requiring authorization.
Authorization implies authentication and typically requires application-level approval.
| bool c7222::AttributeServer::HasServicesRequiringEncryption | ( | ) | const |
Check whether any service contains characteristics requiring encryption.
| BleError c7222::AttributeServer::Init | ( | const void * | context | ) |
Initialize the ATT server from a platform context.
On the Pico W BLE stack, the context is the BTstack ATT database blob (att_db.h). Other platforms may treat the context differently.
When called the first time, the context pointer is cached in the instance. The Pico W implementation parses the ATT DB and registers BTstack read/write callbacks. Subsequent calls reuse the cached context.
RPi Pico (BTstack) steps:
Attribute objects.InitServices() to build services/characteristics.att_server_init().| context | Platform-specific context pointer. |
| void c7222::AttributeServer::InitServices | ( | std::list< Attribute > & | attributes | ) |
Initialize services from a parsed attribute list.
This method consumes the ordered attribute list to construct services and characteristics in discovery order. The list is modified in place and may be emptied by the parsing process.
| attributes | Parsed attributes in DB order. |
| bool c7222::AttributeServer::IsAuthorizationGranted | ( | uint16_t | connection_handle | ) | const |
Get cached authorization result for the active connection.
|
inline |
Queries whether a client is connected to the server.
An attribute server is connected if it has a non-zero connection handle.

|
inline |
Check whether the server was initialized.
| ReadResult c7222::AttributeServer::ReadAttribute | ( | uint16_t | attribute_handle, |
| uint16_t | offset, | ||
| uint8_t * | buffer, | ||
| uint16_t | buffer_size | ||
| ) |
Handle an ATT read request (internal use).
Used by the platform binding to service BTstack read callbacks.
| void c7222::AttributeServer::SetAuthorizationGranted | ( | uint16_t | connection_handle, |
| bool | granted | ||
| ) |
Update cached authorization result for the active connection.
| void c7222::AttributeServer::SetConnectionHandle | ( | uint16_t | connection_handle | ) |
Set the active connection handle for all characteristics.
This value is propagated into each service/characteristic so that notifications/indications can be sent from Characteristic::UpdateValue().

| void c7222::AttributeServer::SetDisconnected | ( | ) |
| void c7222::AttributeServer::SetSecurityLevel | ( | uint16_t | connection_handle, |
| uint8_t | security_level | ||
| ) |
Update cached security level for the active connection.
| BleError c7222::AttributeServer::WriteAttribute | ( | uint16_t | attribute_handle, |
| uint16_t | offset, | ||
| const uint8_t * | data, | ||
| uint16_t | size | ||
| ) |
Handle an ATT write request (internal use).
Used by the platform binding to service BTstack write callbacks.
|
friend |
Stream insertion operator for attribute server information.
Outputs summary details including: