From 6df7b1331a547cbcceb00bbd50604cc9f614f8c0 Mon Sep 17 00:00:00 2001 From: h2zero Date: Wed, 8 Jul 2020 21:10:11 -0600 Subject: [PATCH] Refactor NimBLERemoteCharacteristic::subscribe * Rearrange subscribe parameters to remove requirement of specifying write response to register a callback. --- src/NimBLERemoteCharacteristic.cpp | 16 ++++++++-------- src/NimBLERemoteCharacteristic.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index b044bd4..154206c 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -503,12 +503,12 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, /** * @brief Subscribe or unsubscribe for notifications or indications. * @param [in] val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications. - * @param [in] response If write response required set this to true. * @param [in] notifyCallback A callback to be invoked for a notification. + * @param [in] response If write response required set this to true. * If NULL is provided then no callback is performed. * @return true if successful. */ -bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_callback notifyCallback) { +bool NimBLERemoteCharacteristic::setNotify(uint16_t val, notify_callback notifyCallback, bool response) { NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val); NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902)); @@ -528,16 +528,16 @@ bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_c /** * @brief Subscribe for notifications or indications. * @param [in] notifications If true, subscribe for notifications, false subscribe for indications. - * @param [in] response If true, require a write response from the descriptor write operation. * @param [in] notifyCallback A callback to be invoked for a notification. + * @param [in] response If true, require a write response from the descriptor write operation. * If NULL is provided then no callback is performed. * @return true if successful. */ -bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, notify_callback notifyCallback) { +bool NimBLERemoteCharacteristic::subscribe(bool notifications, notify_callback notifyCallback, bool response) { if(notifications) { - return setNotify(0x01, response, notifyCallback); + return setNotify(0x01, notifyCallback, response); } else { - return setNotify(0x02, response, notifyCallback); + return setNotify(0x02, notifyCallback, response); } } // subscribe @@ -548,7 +548,7 @@ bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, no * @return true if successful. */ bool NimBLERemoteCharacteristic::unsubscribe(bool response) { - return setNotify(0x00, response); + return setNotify(0x00, nullptr, response); } // unsubscribe @@ -564,7 +564,7 @@ bool NimBLERemoteCharacteristic::unsubscribe(bool response) { bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) { bool success; if(notifyCallback != nullptr) { - success = subscribe(notifications, response, notifyCallback); + success = subscribe(notifications, notifyCallback, response); } else { success = unsubscribe(response); } diff --git a/src/NimBLERemoteCharacteristic.h b/src/NimBLERemoteCharacteristic.h index df2d6de..38e3b6c 100644 --- a/src/NimBLERemoteCharacteristic.h +++ b/src/NimBLERemoteCharacteristic.h @@ -104,9 +104,9 @@ public: } bool subscribe(bool notifications = true, - bool response = true, - notify_callback notifyCallback = nullptr); - bool unsubscribe(bool response = true); + notify_callback notifyCallback = nullptr, + bool response = false); + bool unsubscribe(bool response = false); bool registerForNotify(notify_callback notifyCallback, bool notifications = true, bool response = true) @@ -138,7 +138,7 @@ private: friend class NimBLERemoteDescriptor; // Private member functions - bool setNotify(uint16_t val, bool response = true, notify_callback notifyCallback = nullptr); + bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg);