10000 Merge pull request #601 from chegewara/master · Donderda/esp32-snippets@cf45586 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf45586

Browse files
authored
Merge pull request nkolban#601 from chegewara/master
Update remote characteristic to auto-register for notify
2 parents faf2839 + 1012efc commit cf45586

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ void BLERemoteCharacteristic::gattClientEventHandler(
204204
// and unlock the semaphore to ensure that the requestor of the data can continue.
205205
if (evtParam->read.status == ESP_GATT_OK) {
206206
m_value = std::string((char*)evtParam->read.value, evtParam->read.value_len);
207+
if(m_rawData != nullptr)
208+
free(m_rawData);
209+
210+
m_rawData = calloc(evtParam->read.value_len, sizeof(uint8_t));
211+
memcpy(m_rawData, evtParam->read.value, evtParam->read.value_len);
207212
} else {
208213
m_value = "";
209214
}
@@ -475,7 +480,7 @@ void BLERemoteCharacteristic::registerForNotify(
475480
BLERemoteCharacteristic* pBLERemoteCharacteristic,
476481
uint8_t* pData,
477482
size_t length,
478-
bool isNotify)) {
483+
bool isNotify), bool notifications) {
479484
ESP_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str());
480485

481486
m_notifyCallback = notifyCallback; // Save the notification callback.
@@ -492,6 +497,12 @@ void BLERemoteCharacteristic::registerForNotify(
492497
if (errRc != ESP_OK) {
493498
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
494499
}
500+
501+
uint8_t val[] = {0x01, 0x00};
502+
if(!notifications)
503+
val[0] = 0x02;
504+
BLERemoteDescriptor *desc = getDescriptorByUUID("0x2902");
505+
desc->writeValue(val, 2);
495506
} // End Register
496507
else { // If we weren't passed a callback function, then this is an unregistration.
497508
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
@@ -503,6 +514,10 @@ void BLERemoteCharacteristic::registerForNotify(
503514
if (errRc != ESP_OK) {
504515
ESP_LOGE(LOG_TAG, "esp_ble_gattc_unregister_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
505516
}
517+
518+
uint8_t val[] = {0x00, 0x00};
519+
BLERemoteDescriptor *desc = getDescriptorByUUID("0x2902");
520+
desc->writeValue(val, 2);
506521
} // End Unregister
507522

508523
m_semaphoreRegForNotifyEvt.wait("registerForNotify");
@@ -603,4 +618,12 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
603618
writeValue(std::string((char *)data, length), response);
604619
} // writeValue
605620

621+
/**
622+
* @brief Read raw data from remote characteristic as hex bytes
623+
* @return return pointer data read
624+
*/
625+
uint8_t* BLERemoteCharacteristic::readRawData() {
626+
return m_rawData;
627+
}
628+
606629
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLERemoteCharacteristic.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class BLERemoteCharacteristic {
4444
uint8_t readUInt8(void);
4545
uint16_t readUInt16(void);
4646
uint32_t readUInt32(void);
47-
void registerForNotify(void (*notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify));
47+
void registerForNotify(void (*notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify), bool notifications = true);
4848
void writeValue(uint8_t* data, size_t length, bool response = false);
4949
void writeValue(std::string newValue, bool response = false);
5050
void writeValue(uint8_t newValue, bool response = false);
5151
std::string toString(void);
52+
uint8_t* readRawData();
5253

5354
private:
5455
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
@@ -76,6 +77,7 @@ class BLERemoteCharacteristic {
7677
FreeRTOS::Semaphore m_semaphoreRegForNotifyEvt = FreeRTOS::Semaphore("RegForNotifyEvt");
7778
FreeRTOS::Semaphore m_semaphoreWriteCharEvt = FreeRTOS::Semaphore("WriteCharEvt");
7879
std::string m_value;
80+
uint8_t *m_rawData;
7981
void (*m_notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
8082

8183
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.

cpp_utils/BLEServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void BLEServer::handleGATTServerEvent(
202202
m_connId = param->connect.conn_id; // Save the connection id.
203203
if (m_pServerCallbacks != nullptr) {
204204
m_pServerCallbacks->onConnect(this);
205+
m_pServerCallbacks->onConnect(this, param);
205206
}
206207
m_connectedCount++; // Increment the number of connected devices count.
207208
break;

cpp_utils/BLEServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class BLEServerCallbacks {
104104
* @param [in] pServer A reference to the %BLE server that received the client connection.
105105
*/
106106
virtual void onConnect(BLEServer* pServer);
107-
107+
virtual void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param);
108108
/**
109109
* @brief Handle an existing client disconnection.
110110
*

0 commit comments

Comments
 (0)
0