Skip to content

Commit 35f97f4

Browse files
authored
Merge pull request #13729 from AGlass0fMilk/implicit-cccd
BLE: Enable getting an implicitly-created CCCD through `GattCharacteristic::getDescriptor`
2 parents 6fc886a + f9af08c commit 35f97f4

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
#include "ble/gatt/GattAttribute.h"
2525
#include "ble/gatt/GattCallbackParamTypes.h"
2626

27+
// Forward declare ble::impl::GattServer
28+
namespace ble {
29+
30+
#if !defined(DOXYGEN_ONLY)
31+
namespace impl {
32+
class GattServer;
33+
}
34+
#endif // !defined(DOXYGEN_ONLY)
35+
}
36+
2737
/**
2838
* @addtogroup ble
2939
* @{
@@ -1429,6 +1439,11 @@ class GattCharacteristic {
14291439
GattCharacteristic(const GattCharacteristic &) = delete;
14301440
GattCharacteristic& operator=(const GattCharacteristic &) = delete;
14311441

1442+
~GattCharacteristic() {
1443+
delete _implicit_cccd;
1444+
_implicit_cccd = nullptr;
1445+
}
1446+
14321447
public:
14331448

14341449
/**
@@ -1714,7 +1729,7 @@ class GattCharacteristic {
17141729
*/
17151730
uint8_t getDescriptorCount() const
17161731
{
1717-
return _descriptorCount;
1732+
return (_implicit_cccd == nullptr? _descriptorCount : _descriptorCount+1);
17181733
}
17191734

17201735
/**
@@ -1750,16 +1765,45 @@ class GattCharacteristic {
17501765
*
17511766
* @return A pointer the requested descriptor if @p index is within the
17521767
* range of the descriptor array or nullptr otherwise.
1768+
*
1769+
* @note if this characteristic has an implicitly-created CCCD this
1770+
* descriptor will be available at the highest index
1771+
* (ie: return of getDescriptorCount() - 1)
17531772
*/
17541773
GattAttribute *getDescriptor(uint8_t index)
17551774
{
1756-
if (index >= _descriptorCount) {
1775+
1776+
if(index == _descriptorCount) {
1777+
// If _implicit_cccd is nullptr, we want to return that anyway
1778+
return _implicit_cccd;
1779+
}
1780+
else if (index > _descriptorCount) {
17571781
return nullptr;
17581782
}
17591783

17601784
return _descriptors[index];
17611785
}
17621786

1787+
1788+
private:
1789+
1790+
friend ble::impl::GattServer;
1791+
1792+
#if !defined(DOXYGEN_ONLY)
1793+
/**
1794+
* Sets this GattCharacteristic's implicitly-created CCCD, if
1795+
* applicable.
1796+
*
1797+
* @note once this is called, the pointed-to GattAttribute
1798+
* is owned by this GattCharacteristic and will be deleted
1799+
* during this object's destructor
1800+
*/
1801+
void setImplicitCCCD(GattAttribute *implicit_cccd) {
1802+
_implicit_cccd = implicit_cccd;
1803+
}
1804+
#endif // !defined(DOXYGEN_ONLY)
1805+
1806+
17631807
private:
17641808

17651809
/**
@@ -1783,6 +1827,16 @@ class GattCharacteristic {
17831827
*/
17841828
uint8_t _descriptorCount;
17851829

1830+
/**
1831+
* Pointer to characteristic's implicit CCCD, if applicable
1832+
*
1833+
* @note this is only populated if the stack creates an implicit CCCD
1834+
* for this GattCharacteristic. If the descriptors array passed into
1835+
* the constructor includes a CCCD this field is left as nullptr to
1836+
* indicate the CCCD was explicitly created.
1837+
*/
1838+
GattAttribute* _implicit_cccd = nullptr;
1839+
17861840
/**
17871841
* The registered callback handler for read authorization reply.
17881842
*/

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "wsf_types.h"
2424
#include "att_api.h"
2525

26+
#include <new>
27+
2628
namespace ble {
2729
namespace impl {
2830

@@ -588,6 +590,28 @@ ble_error_t GattServer::insert_cccd(
588590
cccds[cccd_cnt].secLevel = characteristic->getUpdateSecurityRequirement().value();
589591
cccd_handles[cccd_cnt] = characteristic->getValueAttribute().getHandle();
590592

593+
/**
594+
* Set the characteristic's implicitly-created CCCD
595+
*
596+
* Ownership is passed to the GattCharacteristic
597+
*/
598+
GattAttribute* implicit_cccd = new (std::nothrow) GattAttribute(
599+
CCCD_UUID,
600+
attribute_it->pValue,
601+
*attribute_it->pLen,
602+
attribute_it->maxLen,
603+
false);
604+
605+
if(implicit_cccd == nullptr) {
606+
currentHandle--;
607+
return BLE_ERROR_NO_MEM;
608+
}
609+
610+
implicit_cccd->setHandle(cccds[cccd_cnt].handle);
611+
implicit_cccd->allowRead(true);
612+
implicit_cccd->allowWrite(true);
613+
characteristic->setImplicitCCCD(implicit_cccd);
614+
591615
cccd_cnt++;
592616
attribute_it++;
593617

0 commit comments

Comments
 (0)