Skip to content

Commit 89e7893

Browse files
authored
[2.0.0] Add BLE characteristic callbacks overloads (#4832)
Add BLE characteristic callbacks overloads with esp_ble_gatts_cb_param_t* param. Example: class BleCharactCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic *pCharacteristic, esp_ble_gatts_cb_param_t *param) { auto addr = param->read.bda; ESP_LOGV(TAG, "Device " ESP_BD_ADDR_STR " request data", ESP_BD_ADDR_HEX(addr)); } void onWrite(BLECharacteristic *pCharacteristic, esp_ble_gatts_cb_param_t *param) { auto addr = param->write.bda; ESP_LOGV(TAG, "Device " ESP_BD_ADDR_STR " transmit data", ESP_BD_ADDR_HEX(addr)); } };
1 parent 7a4e706 commit 89e7893

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

Diff for: libraries/BLE/src/BLECharacteristic.cpp

+20-29
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ void BLECharacteristic::handleGATTServerEvent(
223223
m_writeEvt = false;
224224
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
225225
m_value.commit();
226-
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
226+
// Invoke the onWrite callback handler.
227+
m_pCallbacks->onWrite(this, param);
227228
} else {
228229
m_value.cancel();
229230
}
@@ -311,7 +312,8 @@ void BLECharacteristic::handleGATTServerEvent(
311312
} // Response needed
312313

313314
if (param->write.is_prep != true) {
314-
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
315+
// Invoke the onWrite callback handler.
316+
m_pCallbacks->onWrite(this, param);
315317
}
316318
} // Match on handles.
317319
break;
@@ -383,7 +385,7 @@ void BLECharacteristic::handleGATTServerEvent(
383385

384386
// If is.long is false then this is the first (or only) request to read data, so invoke the callback
385387
// Invoke the read callback.
386-
m_pCallbacks->onRead(this);
388+
m_pCallbacks->onRead(this, param);
387389

388390
std::string value = m_value.getValue();
389391

@@ -757,46 +759,35 @@ std::string BLECharacteristic::toString() {
757759

758760
BLECharacteristicCallbacks::~BLECharacteristicCallbacks() {}
759761

762+
void BLECharacteristicCallbacks::onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
763+
onRead(pCharacteristic);
764+
} // onRead
760765

761-
/**
762-
* @brief Callback function to support a read request.
763-
* @param [in] pCharacteristic The characteristic that is the source of the event.
764-
*/
765766
void BLECharacteristicCallbacks::onRead(BLECharacteristic* pCharacteristic) {
766-
log_d("BLECharacteristicCallbacks", ">> onRead: default");
767-
log_d("BLECharacteristicCallbacks", "<< onRead");
767+
log_d(">> onRead: default");
768+
log_d("<< onRead");
768769
} // onRead
769770

770771

771-
/**
772-
* @brief Callback function to support a write request.
773-
* @param [in] pCharacteristic The characteristic that is the source of the event.
774-
*/
772+
void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
773+
onWrite(pCharacteristic);
774+
} // onWrite
775+
775776
void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic) {
776-
log_d("BLECharacteristicCallbacks", ">> onWrite: default");
777-
log_d("BLECharacteristicCallbacks", "<< onWrite");
777+
log_d(">> onWrite: default");
778+
log_d("<< onWrite");
778779
} // onWrite
779780

780781

781-
/**
782-
* @brief Callback function to support a Notify request.
783-
* @param [in] pCharacteristic The characteristic that is the source of the event.
784-
*/
785782
void BLECharacteristicCallbacks::onNotify(BLECharacteristic* pCharacteristic) {
786-
log_d("BLECharacteristicCallbacks", ">> onNotify: default");
787-
log_d("BLECharacteristicCallbacks", "<< onNotify");
783+
log_d(">> onNotify: default");
784+
log_d("<< onNotify");
788785
} // onNotify
789786

790787

791-
/**
792-
* @brief Callback function to support a Notify/Indicate Status report.
793-
* @param [in] pCharacteristic The characteristic that is the source of the event.
794-
* @param [in] s Status of the notification/indication
795-
* @param [in] code Additional code of underlying errors
796-
*/
797788
void BLECharacteristicCallbacks::onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) {
798-
log_d("BLECharacteristicCallbacks", ">> onStatus: default");
799-
log_d("BLECharacteristicCallbacks", "<< onStatus");
789+
log_d(">> onStatus: default");
790+
log_d("<< onStatus");
800791
} // onStatus
801792

802793

Diff for: libraries/BLE/src/BLECharacteristic.h

+34
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,43 @@ class BLECharacteristicCallbacks {
145145
}Status;
146146

147147
virtual ~BLECharacteristicCallbacks();
148+
149+
/**
150+
* @brief Callback function to support a read request.
151+
* @param [in] pCharacteristic The characteristic that is the source of the event.
152+
* @param [in] param The BLE GATTS param. Use param->read.
153+
*/
154+
virtual void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
155+
/**
156+
* @brief DEPRECATED! Callback function to support a read request. Called only if onRead(,) not overrided.
157+
* @param [in] pCharacteristic The characteristic that is the source of the event.
158+
*/
148159
virtual void onRead(BLECharacteristic* pCharacteristic);
160+
161+
/**
162+
* @brief Callback function to support a write request.
163+
* @param [in] pCharacteristic The characteristic that is the source of the event.
164+
* @param [in] param The BLE GATTS param. Use param->write.
165+
*/
166+
virtual void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
167+
/**
168+
* @brief DEPRECATED! Callback function to support a write request. Called only if onWrite(,) not overrided.
169+
* @param [in] pCharacteristic The characteristic that is the source of the event.
170+
*/
149171
virtual void onWrite(BLECharacteristic* pCharacteristic);
172+
173+
/**
174+
* @brief Callback function to support a Notify request.
175+
* @param [in] pCharacteristic The characteristic that is the source of the event.
176+
*/
150177
virtual void onNotify(BLECharacteristic* pCharacteristic);
178+
179+
/**
180+
* @brief Callback function to support a Notify/Indicate Status report.
181+
* @param [in] pCharacteristic The characteristic that is the source of the event.
182+
* @param [in] s Status of the notification/indication
183+
* @param [in] code Additional code of underlying errors
184+
*/
151185
virtual void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code);
152186
};
153187
#endif /* CONFIG_BLUEDROID_ENABLED */

0 commit comments

Comments
 (0)