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

View File

@ -145,9 +145,43 @@ public:
}Status; }Status;
virtual ~BLECharacteristicCallbacks(); 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); 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); 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); 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); virtual void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code);
}; };
#endif /* CONFIG_BLUEDROID_ENABLED */ #endif /* CONFIG_BLUEDROID_ENABLED */