Skip to content

Adds a feature for BLEDescriptors so that they optionally reset their value on client disconnect #3930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libraries/BLE/src/BLE2902.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
BLE2902::BLE2902() : BLEDescriptor(BLEUUID((uint16_t) 0x2902)) {
uint8_t data[2] = { 0, 0 };
setValue(data, 2);
setResetsOnDisconnect(true);
} // BLE2902


Expand Down
44 changes: 38 additions & 6 deletions libraries/BLE/src/BLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions libraries/BLE/src/BLEDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down