1#ifndef ELEC_C7222_BLE_GATT_CHARACTERISTIC_HPP_
2#define ELEC_C7222_BLE_GATT_CHARACTERISTIC_HPP_
463 virtual void OnWrite(
const std::vector<uint8_t>& data) {
506 uint16_t declaration_handle,
507 uint16_t value_handle);
522 std::list<Attribute>&& descriptor_attrs);
577 static std::optional<Characteristic>
664 return (
static_cast<uint8_t
>(properties_) &
static_cast<uint8_t
>(
Properties::kRead)) != 0;
672 return (
static_cast<uint8_t
>(properties_) &
681 return (
static_cast<uint8_t
>(properties_) &
static_cast<uint8_t
>(
Properties::kWrite)) != 0;
689 return (
static_cast<uint8_t
>(properties_) &
static_cast<uint8_t
>(
Properties::kNotify)) != 0;
706 return (
static_cast<uint8_t
>(properties_) &
715 return (
static_cast<uint8_t
>(properties_) &
1052 template <
typename T>
1054 static_assert(std::is_trivial<T>::value,
"T must be a trivial type for binary conversion");
1055 return SetValue(
reinterpret_cast<const uint8_t*
>(&value),
sizeof(T));
1076 return cccd_ !=
nullptr;
1129 return sccd_ !=
nullptr;
1176 return extended_properties_ !=
nullptr;
1184 return extended_properties_.get();
1192 return extended_properties_.get();
1229 return user_description_ !=
nullptr;
1237 return user_description_.get();
1248 static const std::string empty_string;
1249 return user_description_ ? user_description_text_ : empty_string;
1256 return user_description_.get();
1270 const std::vector<uint8_t>& value,
1271 uint16_t handle = 0);
1278 return descriptors_.size();
1306 return declaration_attr_;
1314 return declaration_attr_;
1378 return event_handlers_;
1395 connection_handle_ = connection_handle;
1403 return connection_handle_;
1424 const uint8_t* packet_data,
1425 uint16_t packet_data_size);
1433 uint16_t buffer_size);
1440 const uint8_t* data,
1458 const uint8_t* event_data,
1459 uint16_t event_data_size);
1491 uint16_t connection_handle_;
1492 bool notification_pending_;
1499 std::unique_ptr<Attribute> cccd_;
1500 std::unique_ptr<Attribute> sccd_;
1501 std::unique_ptr<Attribute> extended_properties_;
1502 std::unique_ptr<Attribute> user_description_;
1503 std::string user_description_text_;
1504 std::list<Attribute> descriptors_;
1514 BleError HandleCccdWrite(uint16_t offset,
const uint8_t* data, uint16_t size)
const;
1519 BleError HandleSccdWrite(uint16_t offset,
const uint8_t* data, uint16_t size)
const;
1524 uint16_t HandleUserDescriptionRead(uint16_t offset, uint8_t* buffer, uint16_t buffer_size)
const;
1529 BleError HandleUserDescriptionWrite(uint16_t offset,
const uint8_t* data, uint16_t size)
const;
1536 uint16_t HandleValueRead(uint16_t offset, uint8_t* buffer, uint16_t buffer_size)
const;
1541 BleError HandleValueWrite(uint16_t offset,
const uint8_t* data, uint16_t size);
1545 std::list<EventHandler*> event_handlers_;
1551 void ConfigureUserDescriptionDescriptor();
1556 void SyncUserDescriptionTextFromAttribute();
1563 void RebindInternalCallbacks();
1583 static_cast<uint8_t
>(rhs));
1595 static_cast<uint8_t
>(rhs));
1607 static_cast<uint8_t
>(rhs));
1628 static_cast<uint8_t
>(rhs));
1641 static_cast<uint8_t
>(rhs));
1654 static_cast<uint8_t
>(rhs));
1682 static_cast<uint16_t
>(rhs));
1688 static_cast<uint16_t
>(rhs));
1696 static_cast<uint16_t
>(rhs));
1702 static_cast<uint16_t
>(rhs));
1710 static_cast<uint16_t
>(rhs));
1716 static_cast<uint16_t
>(rhs));
GATT attribute wrapper for BTstack ATT fields.
ATT attribute wrapper with BTstack-compatible fields.
Definition attribute.hpp:119
uint16_t GetProperties() const
Get the properties bitmask of the attribute.
Definition attribute.hpp:474
Properties
Attribute flags with direct BTstack ATT_PROPERTY_* mapping.
Definition attribute.hpp:130
@ kUuid128
Entry uses a 128-bit UUID in the ATT DB.
@ kReadPermissionBit0
Read permission bit 0 (LSB of a 2-bit read security level). Combined with kReadPermissionBit1 by the ...
@ kDynamic
Value is dynamic (handled by callbacks, not fixed DB storage).
@ kReadPermissionSc
Read requires Secure Connections (SC) per BTstack flags.
@ kWritePermissionSc
Write requires Secure Connections (SC) per BTstack flags.
@ kWritePermissionBit1
Write permission bit 1 (MSB of a 2-bit write security level). Combined with kWritePermissionBit0 by t...
@ kWritePermissionBit0
Write permission bit 0 (LSB of a 2-bit write security level). Combined with kWritePermissionBit1 by t...
@ kReadPermissionBit1
Read permission bit 1 (MSB of a 2-bit read security level). Combined with kReadPermissionBit0 by the ...
@ kEncryptionKeySizeMask
Mask for encryption key size requirement (bits 12-15).
uint16_t GetHandle() const
Get the ATT handle.
Definition attribute.hpp:403
Represents a GATT Characteristic with its declaration, value, and descriptors.
Definition characteristic.hpp:229
bool HasHandle(uint16_t handle) const
Check if this characteristic owns the given handle.
void SetReadRequiresSecureConnections(bool required)
Set Secure Connections requirement for reads.
bool RequiresAuthorization() const
Check whether any access requires authorization.
bool IsNotificationsEnabled() const
Check if notifications are enabled via CCCD.
Attribute * GetDescriptor(size_t index)
Get a custom descriptor by index.
bool CanWrite() const
Check if characteristic supports write operations (with response).
Definition characteristic.hpp:680
Attribute * GetExtendedProperties()
Get the Extended Properties descriptor.
Definition characteristic.hpp:1183
void SetWriteRequiresSecureConnections(bool required)
Set Secure Connections requirement for writes.
bool ReadRequiresAuthorization() const
Check whether reads require application-level authorization.
ExtendedProperties
Characteristic Extended Properties bit values.
Definition characteristic.hpp:300
@ kWritableAuxiliaries
Writable Auxiliaries enabled.
@ kReliableWrite
Reliable Write enabled.
uint16_t HandleAttributeRead(uint16_t attribute_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size)
Attribute read handler for BLE stack callbacks.
SecurityLevel
Security level requirements for read/write operations.
Definition characteristic.hpp:321
@ kAuthorizationRequired
Authenticated pairing with encryption AND authorization required. Highest security level: authenticat...
@ kAuthenticationRequired
Authenticated pairing with encryption required. Client must pair using authenticated method with MITM...
@ kEncryptionRequired
Unauthenticated pairing with encryption required. Client must pair and establish encrypted link (Just...
bool WriteRequiresEncryption() const
Check whether writes require at least encrypted link security.
const Attribute * GetDescriptor(size_t index) const
Get a custom descriptor by index (const version).
void SetWriteSecurityLevel(SecurityLevel level)
Set write security level requirement for this characteristic.
SecurityLevel GetWriteSecurityLevel() const
Get current write security level.
bool RemoveEventHandler(const EventHandler &handler)
Unregister an event handler from this characteristic.
bool HasCCCD() const
Check if CCCD descriptor is present.
Definition characteristic.hpp:1075
bool CanRead() const
Check if characteristic supports read operations.
Definition characteristic.hpp:663
const Attribute * GetCCCD() const
Get the CCCD descriptor (const version).
Definition characteristic.hpp:1103
bool HasBroadcast() const
Check if characteristic supports broadcasts.
Definition characteristic.hpp:654
const Attribute * GetSCCD() const
Get the SCCD descriptor (const version).
Definition characteristic.hpp:1150
void ClearEventHandlers()
Clear all registered event handlers for this characteristic.
Attribute & EnableExtendedProperties()
Enable Characteristic Extended Properties Descriptor. Defines additional characteristic properties be...
bool ReadRequiresSC() const
Check if characteristic read requires Secure Connections (SC).
Definition characteristic.hpp:749
CCCDProperties
Client Characteristic Configuration Descriptor (CCCD) bit values.
Definition characteristic.hpp:270
@ kNotifications
Notifications enabled.
@ kIndications
Indications enabled.
bool ReadRequiresAuthentication() const
Check whether reads require authenticated pairing (MITM).
uint16_t GetEncryptionKeySize() const
Get the required encryption key size for this characteristic. Returns the encryption key size require...
Definition characteristic.hpp:768
bool IsThisCharacteristic(const Uuid &uuid) const
Check if this characteristic matches the given UUID.
BleError DispatchEvent(EventId event_id, const uint8_t *event_data, uint16_t event_data_size)
Dispatch event to registered event handlers.
const uint8_t * GetValueData() const
Get the current value data pointer. For dynamic values, this may change after SetValue() calls.
Attribute & EnableSCCD()
Enable Server Characteristic Configuration Descriptor (SCCD). Required for characteristics with Broad...
size_t GetDescriptorCount() const
Get count of custom descriptors (excluding CCCD and User Description).
Definition characteristic.hpp:1277
Attribute & SetUserDescription(const std::string &description)
Set the Characteristic User Description. Creates or updates the user-readable description of this cha...
bool WriteRequiresAuthorization() const
Check whether writes require application-level authorization.
bool HasUserDescription() const
Check if User Description descriptor is present.
Definition characteristic.hpp:1228
bool RequiresEncryption() const
Check whether any access requires at least encrypted link security.
bool SetValue(const std::vector< uint8_t > &data)
Set the characteristic value from an lvalue vector (copy semantics). Only allowed for dynamic charact...
size_t GetValueSize() const
Get the size of the current value.
bool WriteHasSecurityRequirement() const
Check if characteristic has write permission bits set (requires authorization). Write permission bits...
Definition characteristic.hpp:739
std::vector< uint8_t > GetValueAsVector() const
Get the complete value as a vector.
bool HasSCCD() const
Check if SCCD descriptor is present.
Definition characteristic.hpp:1128
bool HasIndications() const
Check if characteristic supports indications.
Definition characteristic.hpp:696
Attribute & GetValueAttribute()
Get the Value Attribute. The value attribute holds the actual characteristic data.
Definition characteristic.hpp:1322
Attribute * GetCCCD()
Get the CCCD descriptor.
Definition characteristic.hpp:1095
bool SetValue(std::vector< uint8_t > &&data)
Set the characteristic value from an rvalue vector (move semantics). Only allowed for dynamic charact...
bool IsDynamic() const
Check if characteristic value is dynamic (can change).
Definition characteristic.hpp:982
bool Uses128BitUuid() const
Check if characteristic has 128-bit UUID.
Definition characteristic.hpp:973
uint16_t GetConnectionHandle() const
Get the current connection handle.
Definition characteristic.hpp:1402
bool WriteRequiresAuthentication() const
Check whether writes require authenticated pairing (MITM).
void SetConnectionHandle(uint16_t connection_handle)
Set the connection handle for this characteristic.
Definition characteristic.hpp:1394
bool CanSignedWrite() const
Check if characteristic supports authenticated signed writes.
Definition characteristic.hpp:705
EventId
Event IDs for Characteristic-related ATT events.
Definition characteristic.hpp:358
@ kHandleValueIndicationComplete
ATT Handle Value Indication Complete (confirmation received or timeout).
@ kAttEventEnd
Generic ATT event (extensible for future events).
const Attribute & GetDeclarationAttribute() const
Get the Declaration Attribute (const version).
Definition characteristic.hpp:1313
bool IsBroadcastEnabled() const
Check if broadcasts are enabled via SCCD.
Attribute * GetSCCD()
Get the SCCD descriptor.
Definition characteristic.hpp:1142
virtual BleError DispatchBleHciPacket(uint8_t packet_type, const uint8_t *packet_data, uint16_t packet_data_size)
Dispatch BLE HCI packet to the appropriate event handler.
~Characteristic() override=default
Destructor. Cleans up all managed attributes and descriptors.
bool ReadRequiresEncryption() const
Check whether reads require at least encrypted link security.
bool CanWriteWithoutResponse() const
Check if characteristic supports write without response.
Definition characteristic.hpp:671
SecurityLevel GetReadSecurityLevel() const
Get current read security level.
const std::string & GetUserDescriptionText() const
Get the user description text.
Definition characteristic.hpp:1247
bool IsValid() const
Check if this characteristic is valid.
Characteristic & operator=(Characteristic &&other) noexcept
Move assignment operator. Transfers ownership of all internal attributes and descriptors.
uint16_t GetValueHandle() const
Get the handle of the Value attribute.
Definition characteristic.hpp:605
Attribute & GetDeclarationAttribute()
Get the Declaration Attribute. The declaration attribute contains properties, value handle,...
Definition characteristic.hpp:1305
std::list< EventHandler * > GetEventHandlers() const
Get the list of registered event handlers.
Definition characteristic.hpp:1377
Properties
GATT Characteristic Properties as defined in Bluetooth Core Spec.
Definition characteristic.hpp:242
@ kNotify
Characteristic supports notifications.
@ kNone
No properties set.
@ kRead
Characteristic value can be read.
@ kBroadcast
Characteristic supports broadcast.
@ kWrite
Characteristic value can be written with response.
@ kIndicate
Characteristic supports indications.
@ kWriteWithoutResponse
Characteristic value can be written without response.
@ kExtendedProperties
Characteristic has extended properties descriptor.
@ kAuthenticatedSignedWrites
Characteristic supports authenticated signed writes.
Characteristic()=delete
Construct a new Characteristic object (deleted). Prevents default construction without parameters.
friend std::ostream & operator<<(std::ostream &os, const Characteristic &characteristic)
Stream insertion operator for Characteristic. Outputs the characteristic UUID, properties,...
Attribute & EnableCCCD()
Enable Client Characteristic Configuration Descriptor (CCCD). Required for characteristics with Notif...
bool SetValue(const T &value)
Set the characteristic value from a typed value (generic template). Converts any trivial type to byte...
Definition characteristic.hpp:1053
void SetEncryptionKeySize(uint8_t key_size)
Set minimum encryption key size requirement.
BleError HandleAttributeWrite(uint16_t attribute_handle, uint16_t offset, const uint8_t *data, uint16_t size)
Attribute write handler for BLE stack callbacks.
const Uuid & GetUuid() const
Get the UUID of this characteristic.
Definition characteristic.hpp:589
const std::string * SetUserDescriptionText(const std::string &description)
Update the user description text if a User Description descriptor exists.
virtual BleError UpdateValue()
Update the characteristic value and send notification/indication if enabled.
bool RequiresAuthentication() const
Check whether any access requires authentication.
bool ReadHasSecurityRequirement() const
Check if characteristic has read permission bits set (requires authorization). Read permission bits 0...
Definition characteristic.hpp:728
Characteristic(const Uuid &uuid, uint8_t properties, uint16_t declaration_handle, uint16_t value_handle)
Construct a new Characteristic with declaration data. Creates the characteristic with proper declarat...
bool IsReadPermitted(bool authorized, bool authenticated) const
Check if read is permitted given the connection security state.
uint16_t GetDeclarationHandle() const
Get the handle of the Declaration attribute.
Definition characteristic.hpp:613
Characteristic(Characteristic &&other) noexcept
Move constructor. Transfers ownership of all internal attributes and descriptors.
void SetReadSecurityLevel(SecurityLevel level)
Set read security level requirement for this characteristic.
SCCDProperties
Server Characteristic Configuration Descriptor (SCCD) bit values.
Definition characteristic.hpp:286
@ kBroadcasts
Broadcasts enabled.
bool IsIndicationsEnabled() const
Check if indications are enabled via CCCD.
bool HasExtendedPropertiesCapability() const
Check if characteristic properties flag has extended properties capability.
Definition characteristic.hpp:714
const Attribute & GetValueAttribute() const
Get the Value Attribute (const version).
Definition characteristic.hpp:1330
Attribute * GetUserDescription()
Get the User Description descriptor.
Definition characteristic.hpp:1236
void AddEventHandler(EventHandler &handler)
Register an event handler for this characteristic.
bool HasNotifications() const
Check if characteristic supports notifications.
Definition characteristic.hpp:688
const Attribute * GetExtendedProperties() const
Get the Extended Properties descriptor (const version).
Definition characteristic.hpp:1191
bool IsThisCharacteristic(const Uuid &uuid, uint16_t handle) const
Check if this characteristic matches the given UUID and handle.
Properties GetProperties() const
Get the properties of this characteristic.
Definition characteristic.hpp:597
const Attribute * GetUserDescription() const
Get the User Description descriptor (const version).
Definition characteristic.hpp:1255
bool HasExtendedProperties() const
Check if Extended Properties descriptor is present.
Definition characteristic.hpp:1175
bool SetValue(const uint8_t *data, size_t size)
Set the characteristic value from raw bytes (pointer + size). Only allowed for dynamic characteristic...
Attribute & SetCCCDValue(CCCDProperties config)
Set CCCD configuration value. Enables or configures notifications and/or indications....
Attribute & AddDescriptor(const Uuid &uuid, Attribute::Properties properties, const std::vector< uint8_t > &value, uint16_t handle=0)
Add a custom descriptor to this characteristic. Allows extension with application-specific descriptor...
static std::optional< Characteristic > ParseFromAttributes(std::list< Attribute > &attributes)
Parse the first characteristic from a list of attributes.
bool WriteRequiresSC() const
Check if characteristic write requires Secure Connections (SC).
Definition characteristic.hpp:758
Characteristic(Attribute &&decl_attribute, Attribute &&value_attr, std::list< Attribute > &&descriptor_attrs)
Construct a Characteristic by moving parsed attributes.
Attribute & SetSCCDValue(SCCDProperties config)
Set SCCD configuration value. Enables or disables broadcasts. Automatically creates SCCD descriptor i...
bool IsWritePermitted(bool authorized, bool authenticated) const
Check if write is permitted given the connection security state.
Attribute & SetExtendedPropertiesValue(ExtendedProperties config)
Set Extended Properties configuration value. Configures reliable write and writable auxiliaries....
Convenience base that allows move but forbids copy.
Definition non_copyable.hpp:91
UUID storage for 16-bit and 128-bit UUIDs.
Definition uuid.hpp:53
C7222 course abstractions namespace.
Definition ble.hpp:20
constexpr uint16_t operator|=(uint16_t &lhs, Attribute::Properties rhs)
Bitwise OR assignment: uint16_t |= Properties.
Definition attribute.hpp:853
constexpr uint16_t operator|(Attribute::Properties lhs, Attribute::Properties rhs)
Bitwise OR for two Properties values.
Definition attribute.hpp:821
BleError
BLE error codes used across HCI/L2CAP/ATT/GATT and BTstack helpers.
Definition ble_error.hpp:19
constexpr uint16_t operator&=(uint16_t &lhs, Attribute::Properties rhs)
Bitwise AND assignment: uint16_t &= Properties.
Definition attribute.hpp:862
std::ostream & operator<<(std::ostream &os, const BleAddress &addr)
constexpr uint16_t operator^=(uint16_t &lhs, Attribute::Properties rhs)
Bitwise XOR assignment: uint16_t ^= Properties.
Definition attribute.hpp:871
constexpr uint16_t operator^(Attribute::Properties lhs, Attribute::Properties rhs)
Bitwise XOR for two Properties values.
Definition attribute.hpp:837
constexpr uint16_t operator&(Attribute::Properties lhs, Attribute::Properties rhs)
Bitwise AND for two Properties values.
Definition attribute.hpp:829
constexpr uint16_t operator~(Attribute::Properties value)
Bitwise NOT for a Properties value.
Definition attribute.hpp:845
Characteristic event handler structure.
Definition characteristic.hpp:378
virtual void OnUpdatesDisabled()
Called when notifications or indications are disabled by a client.
Definition characteristic.hpp:402
virtual void OnIndicationComplete(uint8_t status)
Called when an indication transaction completes.
Definition characteristic.hpp:418
virtual void OnWrite(const std::vector< uint8_t > &data)
Called when a write operation is performed on this characteristic.
Definition characteristic.hpp:463
virtual void OnRead()
Called when a read operation is performed on this characteristic.
Definition characteristic.hpp:450
virtual void OnUpdatesEnabled(bool is_indication)
Called when notifications or indications are enabled by a client.
Definition characteristic.hpp:391
virtual ~EventHandler()=default
Virtual destructor for the EventHandlers interface.
virtual void OnBroadcastEnabled()
Called when broadcasts are enabled by a client.
Definition characteristic.hpp:429
virtual void OnBroadcastDisabled()
Called when broadcasts are disabled by a client.
Definition characteristic.hpp:439
virtual void OnConfirmationReceived(bool status)
Called when a confirmation for an indication is received.
Definition characteristic.hpp:477