ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
characteristic.hpp
Go to the documentation of this file.
1#ifndef ELEC_C7222_BLE_GATT_CHARACTERISTIC_HPP_
2#define ELEC_C7222_BLE_GATT_CHARACTERISTIC_HPP_
3
4#include <iosfwd>
5#include <list>
6#include <memory>
7#include <optional>
8#include <string>
9#include <vector>
10
11#include "attribute.hpp"
12#include "ble_error.hpp"
13#include "uuid.hpp"
14
15namespace c7222 {
16
229class Characteristic final : public MovableOnly {
230 public:
234
242 enum class Properties : uint8_t {
244 kNone = 0x00,
246 kBroadcast = 0x01,
248 kRead = 0x02,
252 kWrite = 0x08,
254 kNotify = 0x10,
256 kIndicate = 0x20,
261 };
262
270 enum class CCCDProperties : uint16_t {
272 kNone = 0x0000,
274 kNotifications = 0x0001,
276 kIndications = 0x0002
277 };
278
286 enum class SCCDProperties : uint16_t {
288 kNone = 0x0000,
290 kBroadcasts = 0x0001
291 };
292
300 enum class ExtendedProperties : uint16_t {
302 kNone = 0x0000,
304 kReliableWrite = 0x0001,
306 kWritableAuxiliaries = 0x0002
307 };
308
321 enum class SecurityLevel : uint8_t {
327 kNone = 0,
328
336
344
353 };
354
358 enum class EventId : uint8_t {
363 };
364
391 virtual void OnUpdatesEnabled(bool is_indication) {
392 (void)is_indication;
393 }
402 virtual void OnUpdatesDisabled() {
403 }
404
418 virtual void OnIndicationComplete(uint8_t status) {
419 (void)status;
420 }
421
429 virtual void OnBroadcastEnabled() {
430 }
431
439 virtual void OnBroadcastDisabled() {
440 }
441
450 virtual void OnRead() {
451 }
452
463 virtual void OnWrite(const std::vector<uint8_t>& data) {
464 (void)data;
465 }
477 virtual void OnConfirmationReceived(bool status) {
478 (void)status;
479 }
480 protected:
488 virtual ~EventHandler() = default;
489 };
491
495
504 explicit Characteristic(const Uuid& uuid,
505 uint8_t properties,
506 uint16_t declaration_handle,
507 uint16_t value_handle);
508
520 explicit Characteristic(Attribute&& decl_attribute,
521 Attribute&& value_attr,
522 std::list<Attribute>&& descriptor_attrs);
523
529
539 Characteristic() = delete;
544 ~Characteristic() override = default;
545
577 static std::optional<Characteristic>
578 ParseFromAttributes(std::list<Attribute>& attributes);
580
584
589 [[nodiscard]] const Uuid& GetUuid() const {
590 return uuid_;
591 }
592
597 [[nodiscard]] Properties GetProperties() const {
598 return properties_;
599 }
600
605 [[nodiscard]] uint16_t GetValueHandle() const {
606 return value_attr_.GetHandle();
607 }
608
613 [[nodiscard]] uint16_t GetDeclarationHandle() const {
614 return declaration_attr_.GetHandle();
615 }
616
621 [[nodiscard]] bool IsValid() const;
622
629 [[nodiscard]] bool IsThisCharacteristic(const Uuid& uuid, uint16_t handle) const;
630
636 [[nodiscard]] bool IsThisCharacteristic(const Uuid& uuid) const;
637
643 [[nodiscard]] bool HasHandle(uint16_t handle) const;
645
649
654 [[nodiscard]] bool HasBroadcast() const {
655 return (static_cast<uint8_t>(properties_) & static_cast<uint8_t>(Properties::kBroadcast)) !=
656 0;
657 }
658
663 [[nodiscard]] bool CanRead() const {
664 return (static_cast<uint8_t>(properties_) & static_cast<uint8_t>(Properties::kRead)) != 0;
665 }
666
671 [[nodiscard]] bool CanWriteWithoutResponse() const {
672 return (static_cast<uint8_t>(properties_) &
673 static_cast<uint8_t>(Properties::kWriteWithoutResponse)) != 0;
674 }
675
680 [[nodiscard]] bool CanWrite() const {
681 return (static_cast<uint8_t>(properties_) & static_cast<uint8_t>(Properties::kWrite)) != 0;
682 }
683
688 [[nodiscard]] bool HasNotifications() const {
689 return (static_cast<uint8_t>(properties_) & static_cast<uint8_t>(Properties::kNotify)) != 0;
690 }
691
696 [[nodiscard]] bool HasIndications() const {
697 return (static_cast<uint8_t>(properties_) & static_cast<uint8_t>(Properties::kIndicate)) !=
698 0;
699 }
700
705 [[nodiscard]] bool CanSignedWrite() const {
706 return (static_cast<uint8_t>(properties_) &
707 static_cast<uint8_t>(Properties::kAuthenticatedSignedWrites)) != 0;
708 }
709
714 [[nodiscard]] bool HasExtendedPropertiesCapability() const {
715 return (static_cast<uint8_t>(properties_) &
716 static_cast<uint8_t>(Properties::kExtendedProperties)) != 0;
717 }
719
723
728 [[nodiscard]] bool ReadHasSecurityRequirement() const {
729 uint16_t props = value_attr_.GetProperties();
730 return (props & static_cast<uint16_t>(Attribute::Properties::kReadPermissionBit0)) != 0 ||
731 (props & static_cast<uint16_t>(Attribute::Properties::kReadPermissionBit1)) != 0;
732 }
733
739 [[nodiscard]] bool WriteHasSecurityRequirement() const {
740 uint16_t props = value_attr_.GetProperties();
741 return (props & static_cast<uint16_t>(Attribute::Properties::kWritePermissionBit0)) != 0 ||
742 (props & static_cast<uint16_t>(Attribute::Properties::kWritePermissionBit1)) != 0;
743 }
744
749 [[nodiscard]] bool ReadRequiresSC() const {
750 return (value_attr_.GetProperties() &
751 static_cast<uint16_t>(Attribute::Properties::kReadPermissionSc)) != 0;
752 }
753
758 [[nodiscard]] bool WriteRequiresSC() const {
759 return (value_attr_.GetProperties() &
760 static_cast<uint16_t>(Attribute::Properties::kWritePermissionSc)) != 0;
761 }
762
768 [[nodiscard]] uint16_t GetEncryptionKeySize() const {
769 return (value_attr_.GetProperties() &
770 static_cast<uint16_t>(Attribute::Properties::kEncryptionKeySizeMask)) >>
771 12;
772 }
773
780 [[nodiscard]] bool ReadRequiresAuthentication() const;
781
788 [[nodiscard]] bool WriteRequiresAuthentication() const;
789
795 [[nodiscard]] bool ReadRequiresAuthorization() const;
796
802 [[nodiscard]] bool WriteRequiresAuthorization() const;
803
809 [[nodiscard]] bool ReadRequiresEncryption() const;
810
816 [[nodiscard]] bool WriteRequiresEncryption() const;
817
823 [[nodiscard]] bool RequiresAuthentication() const;
824
830 [[nodiscard]] bool RequiresAuthorization() const;
831
835 [[nodiscard]] bool RequiresEncryption() const;
837
841
857
873
884
895
906 void SetEncryptionKeySize(uint8_t key_size);
907
912 [[nodiscard]] SecurityLevel GetReadSecurityLevel() const;
913
920
924
945 [[nodiscard]] bool IsReadPermitted(bool authorized, bool authenticated) const;
946
967 [[nodiscard]] bool IsWritePermitted(bool authorized, bool authenticated) const;
968
973 [[nodiscard]] bool Uses128BitUuid() const {
974 return (value_attr_.GetProperties() &
975 static_cast<uint16_t>(Attribute::Properties::kUuid128)) != 0;
976 }
977
982 [[nodiscard]] bool IsDynamic() const {
983 return (value_attr_.GetProperties() &
984 static_cast<uint16_t>(Attribute::Properties::kDynamic)) != 0;
985 }
987
991
997 [[nodiscard]] const uint8_t* GetValueData() const;
998
1003 [[nodiscard]] size_t GetValueSize() const;
1004
1009 [[nodiscard]] std::vector<uint8_t> GetValueAsVector() const;
1011
1015
1023 bool SetValue(const uint8_t* data, size_t size);
1024
1031 bool SetValue(std::vector<uint8_t>&& data);
1032
1039 bool SetValue(const std::vector<uint8_t>& data);
1040
1052 template <typename T>
1053 bool SetValue(const T& value) {
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));
1056 }
1058
1062
1070
1075 [[nodiscard]] bool HasCCCD() const {
1076 return cccd_ != nullptr;
1077 }
1078
1083 [[nodiscard]] bool IsNotificationsEnabled() const;
1084
1089 [[nodiscard]] bool IsIndicationsEnabled() const;
1090
1096 return cccd_.get();
1097 }
1098
1103 [[nodiscard]] const Attribute* GetCCCD() const {
1104 return cccd_.get();
1105 }
1106
1115
1123
1128 [[nodiscard]] bool HasSCCD() const {
1129 return sccd_ != nullptr;
1130 }
1131
1136 [[nodiscard]] bool IsBroadcastEnabled() const;
1137
1143 return sccd_.get();
1144 }
1145
1150 [[nodiscard]] const Attribute* GetSCCD() const {
1151 return sccd_.get();
1152 }
1153
1162
1170
1175 [[nodiscard]] bool HasExtendedProperties() const {
1176 return extended_properties_ != nullptr;
1177 }
1178
1184 return extended_properties_.get();
1185 }
1186
1191 [[nodiscard]] const Attribute* GetExtendedProperties() const {
1192 return extended_properties_.get();
1193 }
1194
1203
1210 Attribute& SetUserDescription(const std::string& description);
1211
1222 const std::string* SetUserDescriptionText(const std::string& description);
1223
1228 [[nodiscard]] bool HasUserDescription() const {
1229 return user_description_ != nullptr;
1230 }
1231
1237 return user_description_.get();
1238 }
1239
1247 const std::string& GetUserDescriptionText() const {
1248 static const std::string empty_string;
1249 return user_description_ ? user_description_text_ : empty_string;
1250 }
1255 [[nodiscard]] const Attribute* GetUserDescription() const {
1256 return user_description_.get();
1257 }
1258
1269 Attribute::Properties properties,
1270 const std::vector<uint8_t>& value,
1271 uint16_t handle = 0);
1272
1277 [[nodiscard]] size_t GetDescriptorCount() const {
1278 return descriptors_.size();
1279 }
1280
1286 Attribute* GetDescriptor(size_t index);
1287
1293 [[nodiscard]] const Attribute* GetDescriptor(size_t index) const;
1295
1299
1306 return declaration_attr_;
1307 }
1308
1313 [[nodiscard]] const Attribute& GetDeclarationAttribute() const {
1314 return declaration_attr_;
1315 }
1316
1323 return value_attr_;
1324 }
1325
1330 [[nodiscard]] const Attribute& GetValueAttribute() const {
1331 return value_attr_;
1332 }
1334
1338
1348
1363 bool RemoveEventHandler(const EventHandler& handler);
1364
1372
1377 [[nodiscard]] std::list<EventHandler*> GetEventHandlers() const {
1378 return event_handlers_;
1379 }
1381
1385
1394 void SetConnectionHandle(uint16_t connection_handle) {
1395 connection_handle_ = connection_handle;
1396 }
1397
1402 [[nodiscard]] uint16_t GetConnectionHandle() const {
1403 return connection_handle_;
1404 }
1406
1410
1423 virtual BleError DispatchBleHciPacket(uint8_t packet_type,
1424 const uint8_t* packet_data,
1425 uint16_t packet_data_size);
1430 uint16_t HandleAttributeRead(uint16_t attribute_handle,
1431 uint16_t offset,
1432 uint8_t* buffer,
1433 uint16_t buffer_size);
1438 BleError HandleAttributeWrite(uint16_t attribute_handle,
1439 uint16_t offset,
1440 const uint8_t* data,
1441 uint16_t size);
1443
1444 protected:
1448
1458 const uint8_t* event_data,
1459 uint16_t event_data_size);
1461
1465
1486
1487 private:
1488 // Core characteristic data
1489 Uuid uuid_;
1490 Properties properties_;
1491 uint16_t connection_handle_;
1492 bool notification_pending_;
1493
1494 // Required attributes
1495 Attribute declaration_attr_;
1496 Attribute value_attr_;
1497
1498 // Optional descriptors
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_;
1505
1509 // Internal helpers for descriptor write handling
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;
1530
1531 // Internal helpers for value attribute read/write handling
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);
1543
1544 // Event handlers
1545 std::list<EventHandler*> event_handlers_;
1546
1551 void ConfigureUserDescriptionDescriptor();
1556 void SyncUserDescriptionTextFromAttribute();
1563 void RebindInternalCallbacks();
1564
1569 friend std::ostream& operator<<(std::ostream& os, const Characteristic& characteristic);
1570};
1571
1572// ========== Bitwise operators for Characteristic::Properties ==========
1573
1581 Characteristic::Properties rhs) noexcept {
1582 return static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) |
1583 static_cast<uint8_t>(rhs));
1584}
1585
1593 Characteristic::Properties rhs) noexcept {
1594 return static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) &
1595 static_cast<uint8_t>(rhs));
1596}
1597
1605 Characteristic::Properties rhs) noexcept {
1606 return static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) ^
1607 static_cast<uint8_t>(rhs));
1608}
1609
1616 return static_cast<Characteristic::Properties>(~static_cast<uint8_t>(prop));
1617}
1618
1626 Characteristic::Properties rhs) noexcept {
1627 lhs = static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) |
1628 static_cast<uint8_t>(rhs));
1629 return lhs;
1630}
1631
1639 Characteristic::Properties rhs) noexcept {
1640 lhs = static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) &
1641 static_cast<uint8_t>(rhs));
1642 return lhs;
1643}
1644
1652 Characteristic::Properties rhs) noexcept {
1653 lhs = static_cast<Characteristic::Properties>(static_cast<uint8_t>(lhs) ^
1654 static_cast<uint8_t>(rhs));
1655 return lhs;
1656}
1657
1665std::ostream& operator<<(std::ostream& os, Characteristic::Properties props);
1666
1675std::ostream& operator<<(std::ostream& os, const Characteristic& characteristic);
1676
1677// ========== Bitwise operators for Characteristic::CCCDProperties ==========
1678
1680 Characteristic::CCCDProperties rhs) noexcept {
1681 return static_cast<Characteristic::CCCDProperties>(static_cast<uint16_t>(lhs) |
1682 static_cast<uint16_t>(rhs));
1683}
1684
1686 Characteristic::CCCDProperties rhs) noexcept {
1687 return static_cast<Characteristic::CCCDProperties>(static_cast<uint16_t>(lhs) &
1688 static_cast<uint16_t>(rhs));
1689}
1690
1691// ========== Bitwise operators for Characteristic::SCCDProperties ==========
1692
1694 Characteristic::SCCDProperties rhs) noexcept {
1695 return static_cast<Characteristic::SCCDProperties>(static_cast<uint16_t>(lhs) |
1696 static_cast<uint16_t>(rhs));
1697}
1698
1700 Characteristic::SCCDProperties rhs) noexcept {
1701 return static_cast<Characteristic::SCCDProperties>(static_cast<uint16_t>(lhs) &
1702 static_cast<uint16_t>(rhs));
1703}
1704
1705// ========== Bitwise operators for Characteristic::ExtendedProperties ==========
1706
1709 return static_cast<Characteristic::ExtendedProperties>(static_cast<uint16_t>(lhs) |
1710 static_cast<uint16_t>(rhs));
1711}
1712
1715 return static_cast<Characteristic::ExtendedProperties>(static_cast<uint16_t>(lhs) &
1716 static_cast<uint16_t>(rhs));
1717}
1718
1719} // namespace c7222
1720
1721#endif // ELEC_C7222_BLE_GATT_CHARACTERISTIC_HPP_
GATT attribute wrapper for BTstack ATT fields.
BLE error codes.
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.
@ 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
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
GATT UUID wrapper.