@@ -37,7 +37,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
3737/* *
3838 * @brief Constructor.
3939 * @param [in] svc A pointer to the service this characteristic belongs to.
40- * @param [in] ble_gatt_chr struct defined as:
40+ * @param [in] chr struct defined as:
4141 * struct ble_gatt_chr {
4242 * uint16_t def_handle;
4343 * uint16_t val_handle;
@@ -63,44 +63,41 @@ NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() {
6363 * @brief Callback used by the API when a descriptor is discovered or search complete.
6464 */
6565int NimBLERemoteCharacteristic::descriptorDiscCB (
66- uint16_t conn_handle , const ble_gatt_error* error, uint16_t chr_val_handle , const ble_gatt_dsc* dsc, void * arg) {
66+ uint16_t connHandle , const ble_gatt_error* error, uint16_t chrHandle , const ble_gatt_dsc* dsc, void * arg) {
6767 int rc = error->status ;
6868 NIMBLE_LOGD (LOG_TAG, " Descriptor Discovery >> status: %d handle: %d" , rc, (rc == 0 ) ? dsc->handle : -1 );
6969
70- auto filter = (desc_filter_t *)arg;
71- auto pTaskData = (NimBLETaskData*)filter->task_data ;
72- const auto pChr = (NimBLERemoteCharacteristic*)pTaskData->m_pInstance ;
73- const NimBLEUUID* uuidFilter = filter->uuid ;
70+ auto filter = (desc_filter_t *)arg;
71+ auto pTaskData = (NimBLETaskData*)filter->task_data ;
72+ const auto pChr = (NimBLERemoteCharacteristic*)pTaskData->m_pInstance ;
73+ const auto uuid = filter->uuid ; // UUID to filter for
7474
75- if (pChr->getHandle () != chr_val_handle) {
76- return 0 ; // Descriptor not for this characteristic
75+ // Error/done, wrong characteristic, or no-match descriptor
76+ if (rc != 0 || pChr->getHandle () != chrHandle
77+ || (uuid && ble_uuid_cmp (uuid->getBase (), &dsc->uuid .u ))) {
78+ goto Done;
7779 }
80+ // Results added until rc != 0
81+ pChr->m_vDescriptors .push_back (new NimBLERemoteDescriptor (pChr, dsc));
82+ // Return BLE_HS_EDONE if the descriptor was found, stop the search
83+ rc = !!uuid * BLE_HS_EDONE;
7884
79- if (rc == 0 ) {
80- if (uuidFilter != nullptr ) {
81- if (ble_uuid_cmp (uuidFilter->getBase (), &dsc->uuid .u ) == 0 ) {
82- rc = BLE_HS_EDONE; // Found the descriptor, stop the search
83- } else {
84- return 0 ; // Not the descriptor we are looking for
85- }
86- }
87-
88- pChr->m_vDescriptors .push_back (new NimBLERemoteDescriptor (pChr, dsc));
89- return 0 ;
85+ Done:
86+ if (rc != 0 ) {
87+ NimBLEUtils::taskRelease (*pTaskData, rc);
9088 }
91-
92- NimBLEUtils::taskRelease (*pTaskData, rc);
9389 NIMBLE_LOGD (LOG_TAG, " << Descriptor Discovery" );
9490 return rc;
9591}
9692
9793/* *
9894 * @brief Populate the descriptors (if any) for this characteristic.
99- * @param [in] the end handle of the characteristic, or the service, whichever comes first.
95+ * @param [in] uuidFilter The end handle of the characteristic, or the service, whichever comes first.
96+ * @param [out] out Pointer to get the descriptor if found.
97+ * @return True if successfully retrieved, success = 0 or BLE_HS_EDONE.
10098 */
101- bool NimBLERemoteCharacteristic::retrieveDescriptors (const NimBLEUUID* uuidFilter) const {
99+ bool NimBLERemoteCharacteristic::retrieveDescriptors (const NimBLEUUID* uuidFilter, NimBLERemoteDescriptor* out ) const {
102100 NIMBLE_LOGD (LOG_TAG, " >> retrieveDescriptors() for characteristic: %s" , getUUID ().toString ().c_str ());
103-
104101 NimBLETaskData taskData (const_cast <NimBLERemoteCharacteristic*>(this ));
105102 desc_filter_t filter = {uuidFilter, &taskData};
106103
@@ -118,6 +115,9 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
118115 rc = taskData.m_flags ;
119116 if (rc == 0 || rc == BLE_HS_EDONE) {
120117 NIMBLE_LOGD (LOG_TAG, " << retrieveDescriptors(): found %d descriptors." , m_vDescriptors.size ());
118+ if (out && rc) {
119+ out = m_vDescriptors.back ();
120+ }
121121 return true ;
122122 }
123123
@@ -132,8 +132,8 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
132132 */
133133NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor (const NimBLEUUID& uuid) const {
134134 NIMBLE_LOGD (LOG_TAG, " >> getDescriptor: uuid: %s" , uuid.toString ().c_str ());
135- NimBLERemoteDescriptor* pDsc = nullptr ;
136- size_t prev_size = m_vDescriptors. size () ;
135+ NimBLERemoteDescriptor* pDsc = nullptr ;
136+ NimBLEUUID uuidTmp ;
137137
138138 for (const auto & it : m_vDescriptors) {
139139 if (it->getUUID () == uuid) {
@@ -142,36 +142,20 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
142142 }
143143 }
144144
145- if (retrieveDescriptors (&uuid)) {
146- if (m_vDescriptors.size () > prev_size) {
147- pDsc = m_vDescriptors.back ();
148- goto Done;
149- }
150-
151- // If the request was successful but 16/32 bit uuid not found
152- // try again with the 128 bit uuid.
153- if (uuid.bitSize () == BLE_UUID_TYPE_16 || uuid.bitSize () == BLE_UUID_TYPE_32) {
154- NimBLEUUID uuid128 (uuid);
155- uuid128.to128 ();
156- if (retrieveDescriptors (&uuid128)) {
157- if (m_vDescriptors.size () > prev_size) {
158- pDsc = m_vDescriptors.back ();
159- }
160- }
161- } else {
162- // If the request was successful but the 128 bit uuid not found
163- // try again with the 16 bit uuid.
164- NimBLEUUID uuid16 (uuid);
165- uuid16.to16 ();
166- // if the uuid was 128 bit but not of the BLE base type this check will fail
167- if (uuid16.bitSize () == BLE_UUID_TYPE_16) {
168- if (retrieveDescriptors (&uuid16)) {
169- if (m_vDescriptors.size () > prev_size) {
170- pDsc = m_vDescriptors.back ();
171- }
172- }
173- }
174- }
145+ if (!retrieveDescriptors (&uuid, pDsc) || pDsc) {
146+ goto Done;
147+ }
148+ // Try again with 128 bit uuid if request succeeded with no uuid found.
149+ if (uuid.bitSize () == BLE_UUID_TYPE_16 || uuid.bitSize () == BLE_UUID_TYPE_32) {
150+ uuidTmp = NimBLEUUID (uuid).to128 ();
151+ retrieveDescriptors (&uuidTmp, pDsc);
152+ goto Done;
153+ }
154+ // Try again with 16 bit uuid if request succeeded with no uuid found.
155+ // If the uuid was 128 bit but not of the BLE base type this check will fail.
156+ uuidTmp = NimBLEUUID (uuid).to16 ();
157+ if (uuidTmp.bitSize () == BLE_UUID_TYPE_16) {
158+ retrieveDescriptors (&uuidTmp, pDsc);
175159 }
176160
177161Done:
@@ -305,7 +289,7 @@ size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID& uuid) cons
305289 * @return True if supported.
306290 */
307291bool NimBLERemoteCharacteristic::canBroadcast () const {
308- return (m_properties & BLE_GATT_CHR_PROP_BROADCAST) != 0 ;
292+ return (m_properties & BLE_GATT_CHR_PROP_BROADCAST);
309293};
310294
311295/* *
0 commit comments