Add register for notify without callback

* Add subscribe() and unsubscribe() methods to replace registerForNotify() in NimBLERemoteCharacteristic.

* registerForNotify() remains as a (depreciated) method.
This commit is contained in:
h2zero 2020-06-19 12:29:30 -06:00
parent 7983c0e50e
commit f624deacb5
2 changed files with 65 additions and 25 deletions

View file

@ -494,7 +494,58 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
/** /**
* @brief Register for notifications. * @brief Subscribe or unsubscribe for notifications or indications.
* @param [in] uint16_t val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications.
* @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback
* is performed for notifications.
* @return true if successful.
*/
bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_callback notifyCallback) {
NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val);
NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902));
if(desc == nullptr) {
NIMBLE_LOGE(LOG_TAG, "<< setNotify(): Could not get descriptor");
return false;
}
m_notifyCallback = notifyCallback; // Save the notification callback.
NIMBLE_LOGD(LOG_TAG, "<< setNotify()");
return desc->writeValue((uint8_t *)&val, 2, response);
} // setNotify
/**
* @brief Subscribe for notifications or indications.
* @param [in] bool if true, subscribe for notifications, false subscribe for indications.
* @param [in] bool if true, require a write response from the descriptor write operation.
* @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback
* is performed for notifications.
* @return true if successful.
*/
bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, notify_callback notifyCallback) {
if(notifications) {
return setNotify(0x01, response, notifyCallback);
} else {
return setNotify(0x02, response, notifyCallback);
}
} // subscribe
/**
* @brief Unsubscribe for notifications or indications.
* @param [in] bool if true, require a write response from the descriptor write operation.
* @return true if successful.
*/
bool NimBLERemoteCharacteristic::unsubscribe(bool response) {
return setNotify(0x00, response);
} // unsubscribe
/**
* @brief backward-compatibility method for subscribe/unsubscribe notifications/indications
* @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are
* unregistering for notifications. * unregistering for notifications.
* @param [in] bool if true, register for notifications, false register for indications. * @param [in] bool if true, register for notifications, false register for indications.
@ -502,29 +553,13 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
* @return true if successful. * @return true if successful.
*/ */
bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) { bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) {
NIMBLE_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str()); bool success;
m_notifyCallback = notifyCallback; // Save the notification callback.
uint8_t val[] = {0x01, 0x00};
NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902));
if(desc == nullptr)
return false;
if(notifyCallback != nullptr) { if(notifyCallback != nullptr) {
if(!notifications){ success = subscribe(notifications, response, notifyCallback);
val[0] = 0x02; } else {
success = unsubscribe(response);
} }
} return success;
else if (notifyCallback == nullptr){
val[0] = 0x00;
}
NIMBLE_LOGD(LOG_TAG, "<< registerForNotify()");
return desc->writeValue(val, 2, response);
} // registerForNotify } // registerForNotify

View file

@ -78,9 +78,13 @@ public:
return *((T *)pData); return *((T *)pData);
} }
bool registerForNotify(notify_callback _callback, bool subscribe(bool notifications = true,
bool response = true,
notify_callback notifyCallback = nullptr);
bool unsubscribe(bool response = true);
bool registerForNotify(notify_callback notifyCallback,
bool notifications = true, bool notifications = true,
bool response = true); bool response = true) __attribute__ ((deprecated));
bool writeValue(const uint8_t* data, bool writeValue(const uint8_t* data,
size_t length, size_t length,
bool response = false); bool response = false);
@ -100,6 +104,7 @@ private:
friend class NimBLERemoteDescriptor; friend class NimBLERemoteDescriptor;
// Private member functions // Private member functions
bool setNotify(uint16_t val, bool response = true, notify_callback notifyCallback = nullptr);
bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
struct ble_gatt_attr *attr, void *arg); struct ble_gatt_attr *attr, void *arg);