8000 sync 2017-07-17 15:32CT · adiorio/esp32-snippets@6112e18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6112e18

Browse files
committed
sync 2017-07-17 15:32CT
1 parent 5cc98cf commit 6112e18

File tree

10 files changed

+197
-80
lines changed

10 files changed

+197
-80
lines changed

cpp_utils/BLE2902.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ BLE2902::BLE2902() : BLEDescriptor(BLEUUID((uint16_t) 0x2902)) {
1919
setValue(data, 2);
2020
}
2121

22+
2223
/**
23-
* @brief Set the notifications flag.
24-
* @param [in] flag The notifications flag.
24+
* @brief Get the notifications value.
25+
* @return The notifications value.
2526
*/
26-
void BLE2902::setNotifications(bool flag) {
27-
uint8_t *pValue = getValue();
28-
if (flag) {
29-
pValue[0] |= 1<<0;
30-
} else {
31-
pValue[0] &= ~(1<<0);
32-
}
33-
} // setNotifications
27+
bool BLE2902::getNotifications() {
28+
return (getValue()[0] & (1 << 0)) != 0;
29+
} // getNotifications
30+
31+
32+
/**
33+
* @brief Get the indications value.
34+
* @return The indications value.
35+
*/
36+
bool BLE2902::getIndications() {
37+
return (getValue()[0] & (1 << 1)) != 0;
38+
} // getIndications
3439

3540

3641
/**
@@ -45,4 +50,20 @@ void BLE2902::setIndications(bool flag) {
4550
pValue[0] &= ~(1<<1);
4651
}
4752
} // setIndications
53+
54+
55+
/**
56+
* @brief Set the notifications flag.
57+
* @param [in] flag The notifications flag.
58+
*/
59+
void BLE2902::setNotifications(bool flag) {
60+
uint8_t *pValue = getValue();
61+
if (flag) {
62+
pValue[0] |= 1<<0;
63+
} else {
64+
pValue[0] &= ~(1<<0);
65+
}
66+
} // setNotifications
67+
68+
4869
#endif

cpp_utils/BLE2902.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
class BLE2902: public BLEDescriptor {
2525
public:
2626
BLE2902();
27+
bool getNotifications();
28+
bool getIndications();
2729
void setNotifications(bool flag);
2830
void setIndications(bool flag);
31+
2932
}; // BLE2902
3033

3134
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLECharacteristic.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "BLECharacteristic.h"
1717
#include "BLEService.h"
1818
#include "BLEUtils.h"
19+
#include "BLE2902.h"
1920
#include "GeneralUtils.h"
2021

2122
static char LOG_TAG[] = "BLECharacteristic";
@@ -118,6 +119,16 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
118119
} // executeCreate
119120

120121

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+
121132
/**
122133
* @brief Get the handle of the characteristic.
123134
* @return The handle of the characteristic.
@@ -171,6 +182,11 @@ void BLECharacteristic::handleGATTServerEvent(
171182
esp_gatt_if_t gatts_if,
172183
esp_ble_gatts_cb_param_t* param) {
173184
switch(event) {
185+
// Events handled:
186+
// ESP_GATTS_ADD_CHAR_EVT
187+
// ESP_GATTS_WRITE_EVT
188+
// ESP_GATTS_READ_EVT
189+
//
174190
// ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service.
175191
// add_char:
176192
// - esp_gatt_status_t status
@@ -185,6 +201,7 @@ void BLECharacteristic::handleGATTServerEvent(
185201
break;
186202
} // ESP_GATTS_ADD_CHAR_EVT
187203

204+
188205
// ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived.
189206
//
190207
// write:
@@ -312,6 +329,17 @@ void BLECharacteristic::indicate() {
312329

313330
assert(getService() != nullptr);
314331
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+
315343
esp_err_t errRc = ::esp_ble_gatts_send_indicate(
316344
getService()->getServer()->getGattsIf(),
317345
getService()->getServer()->getConnId(),
@@ -337,6 +365,16 @@ void BLECharacteristic::notify() {
337365

338366
assert(getService() != nullptr);
339367
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+
340378
esp_err_t errRc = ::esp_ble_gatts_send_indicate(
341379
getService()->getServer()->getGattsIf(),
342380
getService()->getServer()->getConnId(),
@@ -364,6 +402,7 @@ void BLECharacteristic::setBroadcastProperty(bool value) {
364402
}
365403
} // setBroadcastProperty
366404

405+
367406
/**
368407
* @brief Set the callback handlers for this characteristic.
369408
*/
@@ -372,13 +411,27 @@ void BLECharacteristic::setCallbacks(BLECharacteristicCallbacks* pCallbacks) {
372411
} // setCallbacks
373412

374413

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+
*/
375424
void BLECharacteristic::setHandle(uint16_t handle) {
376425
ESP_LOGD(LOG_TAG, ">> setHandle: handle=0x%.2x, characteristic uuid=%s", handle, getUUID().toString().c_str());
377426
m_handle = handle;
378427
ESP_LOGD(LOG_TAG, "<< setHandle");
379428
} // setHandle
380429

381430

431+
/**
432+
* @brief Set the Indicate property value.
433+
* @param [in] value Set to true if we are to allow indicate messages.
434+
*/
382435
void BLECharacteristic::setIndicateProperty(bool value) {
383436
//ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
384437
if (value) {
@@ -389,6 +442,10 @@ void BLECharacteristic::setIndicateProperty(bool value) {
389442
} // setIndicateProperty
390443

391444

445+
/**
446+
* @brief Set the Notify property value.
447+
* @param [in] value Set to true if we are to allow notification messages.
448+
*/
392449
void BLECharacteristic::setNotifyProperty(bool value) {
393450
//ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
394451
if (value) {
@@ -399,6 +456,10 @@ void BLECharacteristic::setNotifyProperty(bool value) {
399456
} // setNotifyProperty
400457

401458

459+
/**
460+
* @brief Set the Read property value.
461+
* @param [in] value Set to true if we are to allow reads.
462+
*/
402463
void BLECharacteristic::setReadProperty(bool value) {
403464
//ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
404465
if (value) {
@@ -440,6 +501,10 @@ void BLECharacteristic::setValue(std::string value) {
440501
} // setValue
441502

442503

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+
*/
443508
void BLECharacteristic::setWriteNoResponseProperty(bool value) {
444509
//ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
445510
if (value) {
@@ -450,6 +515,10 @@ void BLECharacteristic::setWriteNoResponseProperty(bool value) {
450515
} // setWriteNoResponseProperty
451516

452517

518+
/**
519+
* @brief Set the Write property value.
520+
* @param [in] value Set to true if we are to allow writes.
521+
*/
453522
void BLECharacteristic::setWriteProperty(bool value) {
454523
//ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
455524
if (value) {

cpp_utils/BLECharacteristic.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ class BLECharacteristic {
2626
BLECharacteristic(BLEUUID uuid, uint32_t properties = 0);
2727
virtual ~BLECharacteristic();
2828

29-
void addDescriptor(BLEDescriptor *pDescriptor);
30-
size_t getLength();
31-
BLEUUID getUUID();
32-
uint8_t *getValue();
29+
void addDescriptor(BLEDescriptor *pDescriptor);
30+
BLEDescriptor* getDescriptorByUUID(BLEUUID descriptorUUID);
31+
size_t getLength();
32+
BLEUUID getUUID();
33+
uint8_t* getValue();
3334

3435
void indicate();
3536
void notify();

cpp_utils/BLEDescriptorMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* @brief Return the descriptor by UUID.
1717
* @param [in] UUID The UUID to look up the descriptor.
18-
* @return The descriptor.
18+
* @return The descriptor. If not present, then nullptr is returned.
1919
*/
2020
BLEDescriptor* BLEDescriptorMap::getByUUID(BLEUUID uuid) {
2121
for (auto &myPair : m_uuidMap) {

cpp_utils/BLEUUID.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ esp_bt_uuid_t *BLEUUID::getNative() {
199199
* A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method
200200
* will convert 16 or 32 bit representations to the full 128bit.
201201
*/
202-
void BLEUUID::to128() {
202+
BLEUUID BLEUUID::to128() {
203203
//ESP_LOGD(LOG_TAG, ">> toFull() - %s", toString().c_str());
204204

205205
// If we either don't have a value or are already a 128 bit UUID, nothing further to do.
206206
if (m_valueSet == false || m_uuid.len == ESP_UUID_LEN_128) {
207-
return;
207+
return *this;
208208
}
209209

210210
// If we are 16 bit or 32 bit, then set the 4 bytes of the variable part of the UUID.
@@ -243,6 +243,7 @@ void BLEUUID::to128() {
243243

244244
m_uuid.len = ESP_UUID_LEN_128;
245245
//ESP_LOGD(TAG, "<< toFull <- %s", toString().c_str());
246+
return *this;
246247
} // to128
247248

248249

cpp_utils/BLEUUID.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BLEUUID {
2323
BLEUUID();
2424
bool equals(BLEUUID uuid);
2525
esp_bt_uuid_t *getNative();
26-
void to128();
26+
BLEUUID to128();
2727
std::string toString();
2828

2929
private:

cpp_utils/BLEUtils.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,15 +963,42 @@ void BLEUtils::dumpGattServerEvent(
963963
break;
964964
} // ESP_GATTS_DISCONNECT_EVT
965965

966+
967+
// ESP_GATTS_EXEC_WRITE_EVT
968+
// exec_write:
969+
// - uint16_t conn_id
970+
// - uint32_t trans_id
971+
// - esp_bd_addr_t bda
972+
// - uint8_t exec_write_flag
973+
//
966974
case ESP_GATTS_EXEC_WRITE_EVT: {
967-
ESP_LOGD(LOG_TAG, "[conn_id: %d, trans_id: %d, bda: %s, exec_write_flag: 0x%.2x]",
975+
char* pWriteFlagText;
976+
switch(evtParam->exec_write.exec_write_flag) {
977+
case ESP_GATT_PREP_WRITE_EXEC: {
978+
pWriteFlagText = (char*)"WRITE";
979+
break;
980+
}
981+
982+
case ESP_GATT_PREP_WRITE_CANCEL: {
983+
pWriteFlagText = (char*)"CANCEL";
984+
break;
985+
}
986+
987+
default:
988+
pWriteFlagText = (char*)"<Unknown>";
989+
break;
990+
}
991+
992+
ESP_LOGD(LOG_TAG, "[conn_id: %d, trans_id: %d, bda: %s, exec_write_flag: 0x%.2x=%s]",
968993
evtParam->exec_write.conn_id,
969994
evtParam->exec_write.trans_id,
970995
BLEAddress(evtParam->exec_write.bda).toString().c_str(),
971-
evtParam->exec_write.exec_write_flag);
996+
evtParam->exec_write.exec_write_flag,
997+
pWriteFlagText);
972998
break;
973999
} // ESP_GATTS_DISCONNECT_EVT
9741000

1001+
9751002
case ESP_GATTS_MTU_EVT: {
9761003
ESP_LOGD(LOG_TAG, "[conn_id: %d, mtu: %d]",
9771004
evtParam->mtu.conn_id,

0 commit comments

Comments
 (0)
0