From 9507aa383965d5ab05375169b25c119d9258858f Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 19 Oct 2021 06:12:02 +0200 Subject: BTDevice/BTGattHandler: Add send[Notification|Indication](..) commands --- src/direct_bt/BTGattHandler.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/direct_bt/BTGattHandler.cpp') diff --git a/src/direct_bt/BTGattHandler.cpp b/src/direct_bt/BTGattHandler.cpp index d0f7ad69..5836fa2c 100644 --- a/src/direct_bt/BTGattHandler.cpp +++ b/src/direct_bt/BTGattHandler.cpp @@ -1230,6 +1230,43 @@ uint16_t BTGattHandler::exchangeMTUImpl(const uint16_t clientMaxMTU, const int32 return mtu; } +bool BTGattHandler::sendNotification(const uint16_t handle, const jau::TROOctets & value) { + if( GATTRole::Server != role ) { + ERR_PRINT("BTDevice::sendNotification: GATTRole not server"); + return false; + } + if( !hasHandle(handle) ) { + ERR_PRINT("BTDevice::sendNotification: invalid handle %s", jau::to_hexstring(handle).c_str()); + return false; + } + AttHandleValueRcv data(true /* isNotify */, handle, value); + COND_PRINT(env.DEBUG_DATA, "GATT SEND NTF: %s to %s", data.toString().c_str(), toString().c_str()); + send(data); + return true; +} + +bool BTGattHandler::sendIndication(const uint16_t handle, const jau::TROOctets & value) { + if( GATTRole::Server != role ) { + ERR_PRINT("BTDevice::sendIndication: GATTRole not server"); + return false; + } + if( !hasHandle(handle) ) { + ERR_PRINT("BTDevice::sendIndication: invalid handle %s", jau::to_hexstring(handle).c_str()); + return false; + } + AttHandleValueRcv req(false /* isNotify */, handle, value); + std::unique_ptr pdu = sendWithReply(req, env.GATT_WRITE_COMMAND_REPLY_TIMEOUT); // valid reply or exception + if( pdu->getOpcode() == AttPDUMsg::Opcode::HANDLE_VALUE_CFM ) { + COND_PRINT(env.DEBUG_DATA, "GATT SENT IND: %s -> %s to/from %s", + req.toString().c_str(), pdu->toString().c_str(), toString().c_str()); + return true; + } else { + WARN_PRINT("GATT SENT IND: Failed, no CFM reply: %s -> %s to/from %s", + req.toString().c_str(), pdu->toString().c_str(), toString().c_str()); + return false; + } +} + BTGattCharRef BTGattHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle) noexcept { return findCharacterisicsByValueHandle(charValueHandle, services); } -- cgit v1.2.3