Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ void BLECharacteristic::handleGATTServerEvent(
// - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL
//
case ESP_GATTS_EXEC_WRITE_EVT: {
esp_gatt_status_t retval = ESP_GATT_OK;
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
m_value.commit();
if (m_pCallbacks != nullptr) {
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
retval = m_pCallbacks->onWrite(this,param); // Invoke the onWrite callback handler.
}
} else {
m_value.cancel();
Expand All @@ -242,7 +243,7 @@ void BLECharacteristic::handleGATTServerEvent(
esp_err_t errRc = ::esp_ble_gatts_send_response(
gatts_if,
param->write.conn_id,
param->write.trans_id, ESP_GATT_OK, nullptr);
param->write.trans_id, retval, nullptr);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
Expand Down Expand Up @@ -284,6 +285,7 @@ void BLECharacteristic::handleGATTServerEvent(
// we save the new value. Next we look at the need_rsp flag which indicates whether or not we need
// to send a response. If we do, then we formulate a response and send it.
if (param->write.handle == m_handle) {
esp_gatt_status_t retval = ESP_GATT_OK;
if (param->write.is_prep) {
m_value.addPart(param->write.value, param->write.len);
} else {
Expand All @@ -297,6 +299,11 @@ void BLECharacteristic::handleGATTServerEvent(
ESP_LOGD(LOG_TAG, " - Data: length: %d, data: %s", param->write.len, pHexData);
free(pHexData);

if (m_pCallbacks != nullptr && param->write.is_prep != true) {
retval = m_pCallbacks->onWrite(this,param); // Invoke the onWrite callback handler.
}


if (param->write.need_rsp) {
esp_gatt_rsp_t rsp;

Expand All @@ -309,15 +316,11 @@ void BLECharacteristic::handleGATTServerEvent(
esp_err_t errRc = ::esp_ble_gatts_send_response(
gatts_if,
param->write.conn_id,
param->write.trans_id, ESP_GATT_OK, &rsp);
param->write.trans_id, retval, &rsp);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
} // Response needed

if (m_pCallbacks != nullptr && param->write.is_prep != true) {
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
}
} // Match on handles.
break;
} // ESP_GATTS_WRITE_EVT
Expand Down Expand Up @@ -780,7 +783,6 @@ void BLECharacteristicCallbacks::onRead(BLECharacteristic *pCharacteristic) {
ESP_LOGD("BLECharacteristicCallbacks", "<< onRead");
} // onRead


/**
* @brief Callback function to support a write request.
* @param [in] pCharacteristic The characteristic that is the source of the event.
Expand All @@ -790,4 +792,15 @@ void BLECharacteristicCallbacks::onWrite(BLECharacteristic *pCharacteristic) {
ESP_LOGD("BLECharacteristicCallbacks", "<< onWrite");
} // onWrite

/**
* @brief Callback function to support a write request with custom GATTS status code... can be used to implement object transfer protocol
* @param [out] esp_gatt_status_t GATTS_STATUS code in the response
* @param [in] pCharacteristic The characteristic that is the source of the event.
* @param [in] param the context of the write
*/
esp_gatt_status_t BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
this->onRead(pCharacteristic);
return ESP_GATT_OK;
} // onWrite

#endif /* CONFIG_BT_ENABLED */
2 changes: 2 additions & 0 deletions src/BLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class BLECharacteristicCallbacks {
virtual ~BLECharacteristicCallbacks();
virtual void onRead(BLECharacteristic* pCharacteristic);
virtual void onWrite(BLECharacteristic* pCharacteristic);
virtual esp_gatt_status_t onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
};

#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_ */