[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));
    }
};
This commit is contained in:
mixa3607 2021-04-15 18:08:22 +05:00 committed by GitHub
parent 7a4e7066f9
commit 89e7893b1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 29 deletions

View File

@ -223,7 +223,8 @@ void BLECharacteristic::handleGATTServerEvent(
m_writeEvt = false;
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
m_value.commit();
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
// Invoke the onWrite callback handler.
m_pCallbacks->onWrite(this, param);
} else {
m_value.cancel();
}
@ -311,7 +312,8 @@ void BLECharacteristic::handleGATTServerEvent(
} // Response needed
if (param->write.is_prep != true) {
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
// Invoke the onWrite callback handler.
m_pCallbacks->onWrite(this, param);
}
} // Match on handles.
break;
@ -383,7 +385,7 @@ void BLECharacteristic::handleGATTServerEvent(
// If is.long is false then this is the first (or only) request to read data, so invoke the callback
// Invoke the read callback.
m_pCallbacks->onRead(this);
m_pCallbacks->onRead(this, param);
std::string value = m_value.getValue();
@ -757,46 +759,35 @@ std::string BLECharacteristic::toString() {
BLECharacteristicCallbacks::~BLECharacteristicCallbacks() {}
void BLECharacteristicCallbacks::onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
onRead(pCharacteristic);
} // onRead
/**
* @brief Callback function to support a read request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
void BLECharacteristicCallbacks::onRead(BLECharacteristic* pCharacteristic) {
log_d("BLECharacteristicCallbacks", ">> onRead: default");
log_d("BLECharacteristicCallbacks", "<< onRead");
log_d(">> onRead: default");
log_d("<< onRead");
} // onRead
/**
* @brief Callback function to support a write request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
onWrite(pCharacteristic);
} // onWrite
void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic) {
log_d("BLECharacteristicCallbacks", ">> onWrite: default");
log_d("BLECharacteristicCallbacks", "<< onWrite");
log_d(">> onWrite: default");
log_d("<< onWrite");
} // onWrite
/**
* @brief Callback function to support a Notify request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
void BLECharacteristicCallbacks::onNotify(BLECharacteristic* pCharacteristic) {
log_d("BLECharacteristicCallbacks", ">> onNotify: default");
log_d("BLECharacteristicCallbacks", "<< onNotify");
log_d(">> onNotify: default");
log_d("<< onNotify");
} // onNotify
/**
* @brief Callback function to support a Notify/Indicate Status report.
* @param [in] pCharacteristic The characteristic that is the source of the event.
* @param [in] s Status of the notification/indication
* @param [in] code Additional code of underlying errors
*/
void BLECharacteristicCallbacks::onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) {
log_d("BLECharacteristicCallbacks", ">> onStatus: default");
log_d("BLECharacteristicCallbacks", "<< onStatus");
log_d(">> onStatus: default");
log_d("<< onStatus");
} // onStatus

View File

@ -145,9 +145,43 @@ public:
}Status;
virtual ~BLECharacteristicCallbacks();
/**
* @brief Callback function to support a read request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
* @param [in] param The BLE GATTS param. Use param->read.
*/
virtual void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
/**
* @brief DEPRECATED! Callback function to support a read request. Called only if onRead(,) not overrided.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
virtual void onRead(BLECharacteristic* pCharacteristic);
/**
* @brief Callback function to support a write request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
* @param [in] param The BLE GATTS param. Use param->write.
*/
virtual void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
/**
* @brief DEPRECATED! Callback function to support a write request. Called only if onWrite(,) not overrided.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
virtual void onWrite(BLECharacteristic* pCharacteristic);
/**
* @brief Callback function to support a Notify request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
*/
virtual void onNotify(BLECharacteristic* pCharacteristic);
/**
* @brief Callback function to support a Notify/Indicate Status report.
* @param [in] pCharacteristic The characteristic that is the source of the event.
* @param [in] s Status of the notification/indication
* @param [in] code Additional code of underlying errors
*/
virtual void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code);
};
#endif /* CONFIG_BLUEDROID_ENABLED */