16
16
#include " BLECharacteristic.h"
17
17
#include " BLEService.h"
18
18
#include " BLEUtils.h"
19
+ #include " BLE2902.h"
19
20
#include " GeneralUtils.h"
20
21
21
22
static char LOG_TAG[] = " BLECharacteristic" ;
@@ -118,6 +119,16 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
118
119
} // executeCreate
119
120
120
121
122
+ /* *
123
+ * @brief Return the BLE Descriptor for the given UUID if associated with this characteristic.
124
+ * @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
125
+ * @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned.
126
+ */
127
+ BLEDescriptor* BLECharacteristic::getDescriptorByUUID (BLEUUID descriptorUUID) {
128
+ return m_descriptorMap.getByUUID (descriptorUUID);
129
+ } // getDescriptorByUUID
130
+
131
+
121
132
/* *
122
133
* @brief Get the handle of the characteristic.
123
134
* @return The handle of the characteristic.
@@ -171,6 +182,11 @@ void BLECharacteristic::handleGATTServerEvent(
171
182
esp_gatt_if_t gatts_if,
172
183
esp_ble_gatts_cb_param_t * param) {
173
184
switch (event) {
185
+ // Events handled:
186
+ // ESP_GATTS_ADD_CHAR_EVT
187
+ // ESP_GATTS_WRITE_EVT
188
+ // ESP_GATTS_READ_EVT
189
+ //
174
190
// ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service.
175
191
// add_char:
176
192
// - esp_gatt_status_t status
@@ -185,6 +201,7 @@ void BLECharacteristic::handleGATTServerEvent(
185
201
break ;
186
202
} // ESP_GATTS_ADD_CHAR_EVT
187
203
204
+
188
205
// ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived.
189
206
//
190
207
// write:
@@ -312,6 +329,17 @@ void BLECharacteristic::indicate() {
312
329
313
330
assert (getService () != nullptr );
314
331
assert (getService ()->getServer () != nullptr );
332
+
333
+ // Test to see if we have a 0x2902 descriptor. If we do, then check to see if indications are enabled
334
+ // and, if not, prevent the indication.
335
+
336
+ BLE2902 *p2902 = (BLE2902*)getDescriptorByUUID ((uint16_t )0x2902 );
337
+ if (p2902 != nullptr && !p2902->getIndications ()) {
338
+ ESP_LOGD (LOG_TAG, " << indications disabled; ignoring" );
339
+ return ;
340
+ }
341
+
342
+
315
343
esp_err_t errRc = ::esp_ble_gatts_send_indicate (
316
344
getService ()->getServer ()->getGattsIf (),
317
345
getService ()->getServer ()->getConnId (),
@@ -337,6 +365,16 @@ void BLECharacteristic::notify() {
337
365
338
366
assert (getService () != nullptr );
339
367
assert (getService ()->getServer () != nullptr );
368
+
369
+ // Test to see if we have a 0x2902 descriptor. If we do, then check to see if notification is enabled
370
+ // and, if not, prevent the notification.
371
+
372
+ BLE2902 *p2902 = (BLE2902*)getDescriptorByUUID ((uint16_t )0x2902 );
373
+ if (p2902 != nullptr && !p2902->getNotifications ()) {
374
+ ESP_LOGD (LOG_TAG, " << notifications disabled; ignoring" );
375
+ return ;
376
+ }
377
+
340
378
esp_err_t errRc = ::esp_ble_gatts_send_indicate (
341
379
getService ()->getServer ()->getGattsIf (),
342
380
getService ()->getServer ()->getConnId (),
@@ -364,6 +402,7 @@ void BLECharacteristic::setBroadcastProperty(bool value) {
364
402
}
365
403
} // setBroadcastProperty
366
404
405
+
367
406
/* *
368
407
* @brief Set the callback handlers for this characteristic.
369
408
*/
@@ -372,13 +411,27 @@ void BLECharacteristic::setCallbacks(BLECharacteristicCallbacks* pCallbacks) {
372
411
} // setCallbacks
373
412
374
413
414
+ /* *
415
+ * @brief Set the BLE handle associated with this characteristic.
416
+ * A user program will request that a characteristic be created against a service. When the characteristic has been
417
+ * registered, the service will be given a "handle" that it knows the characteristic as. This handle is unique to the
418
+ * server/service but it is told to the service, not the characteristic associated with the service. This internally
419
+ * exposed function can be invoked by the service against this model of the characteristic to allow the characteristic
420
+ * to learn its own handle. Once the characteristic knows its own handle, it will be able to see incoming GATT events
421
+ * that will be propagated down to it which contain a handle value and now know that the event is destined for it.
422
+ * @param [in] handle The handle associated with this characteristic.
423
+ */
375
424
void BLECharacteristic::setHandle (uint16_t handle) {
376
425
ESP_LOGD (LOG_TAG, " >> setHandle: handle=0x%.2x, characteristic uuid=%s" , handle, getUUID ().toString ().c_str ());
377
426
m_handle = handle;
378
427
ESP_LOGD (LOG_TAG, " << setHandle" );
379
428
} // setHandle
380
429
381
430
431
+ /* *
432
+ * @brief Set the Indicate property value.
433
+ * @param [in] value Set to true if we are to allow indicate messages.
434
+ */
382
435
void BLECharacteristic::setIndicateProperty (bool value) {
383
436
// ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
384
437
if (value) {
@@ -389,6 +442,10 @@ void BLECharacteristic::setIndicateProperty(bool value) {
389
442
} // setIndicateProperty
390
443
391
444
445
+ /* *
446
+ * @brief Set the Notify property value.
447
+ * @param [in] value Set to true if we are to allow notification messages.
448
+ */
392
449
void BLECharacteristic::setNotifyProperty (bool value) {
393
450
// ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
394
451
if (value) {
@@ -399,6 +456,10 @@ void BLECharacteristic::setNotifyProperty(bool value) {
399
456
} // setNotifyProperty
400
457
401
458
459
+ /* *
460
+ * @brief Set the Read property value.
461
+ * @param [in] value Set to true if we are to allow reads.
462
+ */
402
463
void BLECharacteristic::setReadProperty (bool value) {
403
464
// ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
404
465
if (value) {
@@ -440,6 +501,10 @@ void BLECharacteristic::setValue(std::string value) {
440
501
} // setValue
441
502
442
503
504
+ /* *
505
+ * @brief Set the Write No Response property value.
506
+ * @param [in] value Set to true if we are to allow writes with no response.
507
+ */
443
508
void BLECharacteristic::setWriteNoResponseProperty (bool value) {
444
509
// ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
445
510
if (value) {
@@ -450,6 +515,10 @@ void BLECharacteristic::setWriteNoResponseProperty(bool value) {
450
515
} // setWriteNoResponseProperty
451
516
452
517
518
+ /* *
519
+ * @brief Set the Write property value.
520
+ * @param [in] value Set to true if we are to allow writes.
521
+ */
453
522
void BLECharacteristic::setWriteProperty (bool value) {
454
523
// ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
455
524
if (value) {
0 commit comments