From e868f37135b5a0d251c5756fd6c7c6a1bd8aa4e9 Mon Sep 17 00:00:00 2001 From: gluhovsky Date: Mon, 12 Sep 2022 05:18:55 +0300 Subject: [PATCH] Add conn_handle to NimBLECharacteristic::notify to send notification/indication to a specific client (#97) Adds the ability to send notifications/indications to specific clients individually. --- src/NimBLECharacteristic.cpp | 18 +++++++++++++----- src/NimBLECharacteristic.h | 6 +++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 86ece5f..fc905f1 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -411,9 +411,10 @@ void NimBLECharacteristic::indicate(const std::vector& value) { /** * @brief Send a notification or indication. * @param[in] is_notification if true sends a notification, false sends an indication. + * @param[in] conn_handle Connection handle to send individual notification, or BLE_HCI_LE_CONN_HANDLE_MAX + 1 to send notification to all subscribed clients. */ -void NimBLECharacteristic::notify(bool is_notification) { - notify(m_value.data(), m_value.length(), is_notification); +void NimBLECharacteristic::notify(bool is_notification, uint16_t conn_handle) { + notify(m_value.data(), m_value.length(), is_notification, conn_handle); } // notify @@ -421,9 +422,10 @@ void NimBLECharacteristic::notify(bool is_notification) { * @brief Send a notification or indication. * @param[in] value A std::vector containing the value to send as the notification value. * @param[in] is_notification if true sends a notification, false sends an indication. + * @param[in] conn_handle Connection handle to send individual notification, or BLE_HCI_LE_CONN_HANDLE_MAX + 1 to send notification to all subscribed clients. */ -void NimBLECharacteristic::notify(const std::vector& value, bool is_notification) { - notify(value.data(), value.size(), is_notification); +void NimBLECharacteristic::notify(const std::vector& value, bool is_notification, uint16_t conn_handle) { + notify(value.data(), value.size(), is_notification, conn_handle); } // notify @@ -432,8 +434,9 @@ void NimBLECharacteristic::notify(const std::vector& value, bool is_not * @param[in] value A pointer to the data to send. * @param[in] length The length of the data to send. * @param[in] is_notification if true sends a notification, false sends an indication. + * @param[in] conn_handle Connection handle to send individual notification, or BLE_HCI_LE_CONN_HANDLE_MAX + 1 to send notification to all subscribed clients. */ -void NimBLECharacteristic::notify(const uint8_t* value, size_t length, bool is_notification) { +void NimBLECharacteristic::notify(const uint8_t* value, size_t length, bool is_notification, uint16_t conn_handle) { NIMBLE_LOGD(LOG_TAG, ">> notify: length: %d", length); if(!(m_properties & NIMBLE_PROPERTY::NOTIFY) && @@ -457,6 +460,11 @@ void NimBLECharacteristic::notify(const uint8_t* value, size_t length, bool is_n int rc = 0; for (auto &it : m_subscribedVec) { + // check if need a specific client + if ((conn_handle <= BLE_HCI_LE_CONN_HANDLE_MAX) && (it.first != conn_handle)) { + continue; + } + uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first) - 3; // check if connected and subscribed diff --git a/src/NimBLECharacteristic.h b/src/NimBLECharacteristic.h index 93d0902..494242c 100644 --- a/src/NimBLECharacteristic.h +++ b/src/NimBLECharacteristic.h @@ -84,9 +84,9 @@ public: void indicate(); void indicate(const uint8_t* value, size_t length); void indicate(const std::vector& value); - void notify(bool is_notification = true); - void notify(const uint8_t* value, size_t length, bool is_notification = true); - void notify(const std::vector& value, bool is_notification = true); + void notify(bool is_notification = true, uint16_t conn_handle = BLE_HCI_LE_CONN_HANDLE_MAX + 1); + void notify(const uint8_t* value, size_t length, bool is_notification = true, uint16_t conn_handle = BLE_HCI_LE_CONN_HANDLE_MAX + 1); + void notify(const std::vector& value, bool is_notification = true, uint16_t conn_handle = BLE_HCI_LE_CONN_HANDLE_MAX + 1); size_t getSubscribedCount(); void addDescriptor(NimBLEDescriptor *pDescriptor); NimBLEDescriptor* getDescriptorByUUID(const char* uuid);