Skip to content

Commit 5c82533

Browse files
committed
Enabled getting an implicitly-created CCCD through GattCharacteristic::getDescriptor
1 parent ad40b1b commit 5c82533

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

connectivity/FEATURE_BLE/include/ble/gatt/GattCharacteristic.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,12 @@ class GattCharacteristic {
14291429
GattCharacteristic(const GattCharacteristic &) = delete;
14301430
GattCharacteristic& operator=(const GattCharacteristic &) = delete;
14311431

1432+
~GattCharacteristic() {
1433+
if(_implicit_cccd != nullptr) {
1434+
delete _implicit_cccd;
1435+
}
1436+
}
1437+
14321438
public:
14331439

14341440
/**
@@ -1714,7 +1720,7 @@ class GattCharacteristic {
17141720
*/
17151721
uint8_t getDescriptorCount() const
17161722
{
1717-
return _descriptorCount;
1723+
return (_implicit_cccd == nullptr? _descriptorCount : _descriptorCount+1);
17181724
}
17191725

17201726
/**
@@ -1750,16 +1756,40 @@ class GattCharacteristic {
17501756
*
17511757
* @return A pointer the requested descriptor if @p index is within the
17521758
* range of the descriptor array or nullptr otherwise.
1759+
*
1760+
* @note if this characteristic has an implicitly-created CCCD this
1761+
* descriptor will be available at the highest index
1762+
* (ie: return of getDescriptorCount() - 1)
17531763
*/
17541764
GattAttribute *getDescriptor(uint8_t index)
17551765
{
1756-
if (index >= _descriptorCount) {
1766+
1767+
if(index == _descriptorCount) {
1768+
// If _implicit_cccd is nullptr, we want to return that anyway
1769+
return _implicit_cccd;
1770+
}
1771+
else if (index > _descriptorCount) {
17571772
return nullptr;
17581773
}
17591774

17601775
return _descriptors[index];
17611776
}
17621777

1778+
/**
1779+
* Sets this GattCharacteristic's implicitly-created CCCD, if
1780+
* applicable.
1781+
*
1782+
* @note once this is called, the pointed-to GattAttribute
1783+
* is owned by this GattCharacteristic and will be deleted
1784+
* during this object's destructor
1785+
*
1786+
* @note this is an internal function and should not be called
1787+
* by the application
1788+
*/
1789+
void _setImplicitCCCD(GattAttribute *implicit_cccd) {
1790+
_implicit_cccd = implicit_cccd;
1791+
}
1792+
17631793
private:
17641794

17651795
/**
@@ -1783,6 +1813,16 @@ class GattCharacteristic {
17831813
*/
17841814
uint8_t _descriptorCount;
17851815

1816+
/**
1817+
* Pointer to characteristic's implicit CCCD, if applicable
1818+
*
1819+
* @note this is only populated if the stack creates an implicit CCCD
1820+
* for this GattCharacteristic. If the descriptors array passed into
1821+
* the constructor includes a CCCD this field is left as nullptr to
1822+
* indicate the CCCD was explicitly created.
1823+
*/
1824+
GattAttribute* _implicit_cccd;
1825+
17861826
/**
17871827
* The registered callback handler for read authorization reply.
17881828
*/

connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,23 @@ ble_error_t GattServer::insert_cccd(
588588
cccds[cccd_cnt].secLevel = characteristic->getUpdateSecurityRequirement().value();
589589
cccd_handles[cccd_cnt] = characteristic->getValueAttribute().getHandle();
590590

591+
/**
592+
* Set the characteristic's implicitly-created CCCD
593+
*
594+
* Ownership is passed to the GattCharacteristic
595+
*/
596+
GattAttribute* implicit_cccd = new GattAttribute(
597+
CCCD_UUID,
598+
attribute_it->pValue,
599+
*attribute_it->pLen,
600+
attribute_it->maxLen,
601+
false);
602+
603+
implicit_cccd->setHandle(cccds[cccd_cnt].handle);
604+
implicit_cccd->allowRead(true);
605+
implicit_cccd->allowWrite(true);
606+
characteristic->_setImplicitCCCD(implicit_cccd);
607+
591608
cccd_cnt++;
592609
attribute_it++;
593610

0 commit comments

Comments
 (0)