diff --git a/btle/btle_discovery.cpp b/btle/btle_discovery.cpp index 07819e9..444c64c 100644 --- a/btle/btle_discovery.cpp +++ b/btle/btle_discovery.cpp @@ -65,6 +65,19 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt) DiscoveredCharacteristic::onDataReadCallback(&response); } break; + + case BLE_GATTC_EVT_WRITE_RSP: + if (DiscoveredCharacteristic::onDataWriteCallback != NULL) { + GattWriteCallbackParams response = { + .handle = p_ble_evt->evt.gattc_evt.params.write_rsp.handle, + .writeOp = (GattWriteCallbackParams::WriteOp_t)(p_ble_evt->evt.gattc_evt.params.write_rsp.write_op), + .offset = p_ble_evt->evt.gattc_evt.params.write_rsp.offset, + .len = p_ble_evt->evt.gattc_evt.params.write_rsp.len, + .data = p_ble_evt->evt.gattc_evt.params.write_rsp.data, + }; + DiscoveredCharacteristic::onDataWriteCallback(&response); + } + break; } sdSingleton.progressCharacteristicDiscovery(); diff --git a/nRF51GattClient.h b/nRF51GattClient.h index 4da276e..a1cc100 100644 --- a/nRF51GattClient.h +++ b/nRF51GattClient.h @@ -115,8 +115,31 @@ public: } } - virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, size_t length, const uint8_t *value) const { - return BLE_ERROR_NONE; + virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, size_t length, const uint8_t *value) const { + ble_gattc_write_params_t writeParams = { + .write_op = cmd, + // .flags = 0, + .handle = attributeHandle, + .offset = 0, + .len = length, + .p_value = const_cast(value), + }; + + uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams); + if (rc == NRF_SUCCESS) { + return BLE_ERROR_NONE; + } + switch (rc) { + case NRF_ERROR_BUSY: + return BLE_STACK_BUSY; + case BLE_ERROR_NO_TX_BUFFERS: + return BLE_ERROR_NO_MEM; + case BLE_ERROR_INVALID_CONN_HANDLE: + case NRF_ERROR_INVALID_STATE: + case NRF_ERROR_INVALID_ADDR: + default: + return BLE_ERROR_INVALID_STATE; + } } public: