diff --git a/libraries/BLE/src/BLE2902.cpp b/libraries/BLE/src/BLE2902.cpp index 23d9c77c093..4e1b2da648d 100644 --- a/libraries/BLE/src/BLE2902.cpp +++ b/libraries/BLE/src/BLE2902.cpp @@ -17,6 +17,7 @@ BLE2902::BLE2902() : BLEDescriptor(BLEUUID((uint16_t) 0x2902)) { uint8_t data[2] = { 0, 0 }; setValue(data, 2); + setResetsOnDisconnect(true); } // BLE2902 diff --git a/libraries/BLE/src/BLEDescriptor.cpp b/libraries/BLE/src/BLEDescriptor.cpp index 96b2de87c95..a24991f8684 100644 --- a/libraries/BLE/src/BLEDescriptor.cpp +++ b/libraries/BLE/src/BLEDescriptor.cpp @@ -30,12 +30,13 @@ BLEDescriptor::BLEDescriptor(const char* uuid, uint16_t len) : BLEDescriptor(BLE * @brief BLEDescriptor constructor. */ BLEDescriptor::BLEDescriptor(BLEUUID uuid, uint16_t max_len) { - m_bleUUID = uuid; - m_value.attr_len = 0; // Initial length is 0. - m_value.attr_max_len = max_len; // Maximum length of the data. - m_handle = NULL_HANDLE; // Handle is initially unknown. - m_pCharacteristic = nullptr; // No initial characteristic. - m_pCallback = nullptr; // No initial callback. + m_resetValuesOnDisconnect = false; + m_bleUUID = uuid; + m_value.attr_len = 0; // Initial length is 0. + m_value.attr_max_len = max_len; // Maximum length of the data. + m_handle = NULL_HANDLE; // Handle is initially unknown. + m_pCharacteristic = nullptr; // No initial characteristic. + m_pCallback = nullptr; // No initial callback. m_value.attr_value = (uint8_t*) malloc(max_len); // Allocate storage for the value. } // BLEDescriptor @@ -117,6 +118,13 @@ uint8_t* BLEDescriptor::getValue() { return m_value.attr_value; } // getValue +/** + * @brief Gets the value of the resetOnDisconnect flag + * @return flag + */ +bool BLEDescriptor::resetsOnDisconnect(){ + return m_resetValuesOnDisconnect; +} /** * @brief Handle GATT server events for the descripttor. @@ -193,6 +201,14 @@ void BLEDescriptor::handleGATTServerEvent( break; } // ESP_GATTS_READ_EVT + // Resets the values to its default + case ESP_GATTS_DISCONNECT_EVT: { + if(this->m_resetValuesOnDisconnect){ + this->resetValue(); + } + break; + } + default: break; } // switch event @@ -246,6 +262,22 @@ void BLEDescriptor::setValue(std::string value) { setValue((uint8_t*) value.data(), value.length()); } // setValue +/** + * @brief Sets the reset on discconect flag + * @param [in] flag The value of the flag + */ +void BLEDescriptor::setResetsOnDisconnect(bool flag){ + m_resetValuesOnDisconnect = flag; +} + +/** + * @brief Resets the value to its default content + */ +void BLEDescriptor::resetValue(){ + memset(m_value.attr_value,0,m_value.attr_max_len); + m_value.attr_len = 0; +} + void BLEDescriptor::setAccessPermissions(esp_gatt_perm_t perm) { m_permissions = perm; } diff --git a/libraries/BLE/src/BLEDescriptor.h b/libraries/BLE/src/BLEDescriptor.h index 03cc5791727..af7f9652882 100644 --- a/libraries/BLE/src/BLEDescriptor.h +++ b/libraries/BLE/src/BLEDescriptor.h @@ -28,10 +28,13 @@ class BLEDescriptor { BLEDescriptor(BLEUUID uuid, uint16_t max_len = 100); virtual ~BLEDescriptor(); + + uint16_t getHandle(); // Get the handle of the descriptor. size_t getLength(); // Get the length of the value of the descriptor. BLEUUID getUUID(); // Get the UUID of the descriptor. uint8_t* getValue(); // Get a pointer to the value of the descriptor. + bool resetsOnDisconnect(); void handleGATTServerEvent( esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, @@ -41,12 +44,17 @@ class BLEDescriptor { void setCallbacks(BLEDescriptorCallbacks* pCallbacks); // Set callbacks to be invoked for the descriptor. void setValue(uint8_t* data, size_t size); // Set the value of the descriptor as a pointer to data. void setValue(std::string value); // Set the value of the descriptor as a data buffer. + void setResetsOnDisconnect(bool flag); std::string toString(); // Convert the descriptor to a string representation. +protected: + void resetValue(); // Resets the values to its default + private: friend class BLEDescriptorMap; friend class BLECharacteristic; + bool m_resetValuesOnDisconnect; BLEUUID m_bleUUID; uint16_t m_handle; BLEDescriptorCallbacks* m_pCallback;