Skip to content

Commit

Permalink
[BREAKING] Refactor attributes
Browse files Browse the repository at this point in the history
Refactor attributes to reduce code duplication and improve maintainability.

* Add attribute base classes to provide common code.
* Add const where possible to functions and parameters.
* `NimBLECharacteristic::notify` no longer takes a `bool is_notification` parameter, instead `indicate()` should be called to send indications.
* `NimBLECharacteristic::indicate` now takes the same parameters as `notify`.
* `NimBLECharacteristicCallbacks` and `NimBLEDescriptorCallbacks` methods now take `const NimBLEConnInfo&` instead of non-const.
* `NimBLECharacteristic::onNotify` callback removed as unnecessary, the library does not call notify without app input.
* Add NimBLEUUID constructor that takes a reference to `ble_uuid_any_t`.
* `NimBLERemoteService::getCharacteristics` now returns a `const std::vector<NimBLERemoteCharacteristic*>&` instead of non-const `std::vector<NimBLERemoteCharacteristic*>*`
* `NimBLERemoteService::getValue` now returns `NimBLEAttValue` instead of `std::string`
* `NimBLEService::getCharacteristics` now returns a `const std::vector<NimBLECharacteristic*>&` instead of a copy of std::vector<NimBLECharacteristic *>.
  • Loading branch information
h2zero committed Aug 10, 2024
1 parent b6a169f commit eb28997
Show file tree
Hide file tree
Showing 29 changed files with 1,664 additions and 2,460 deletions.
2 changes: 1 addition & 1 deletion examples/NimBLE_Client/NimBLE_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData,
std::string str = (isNotify == true) ? "Notification" : "Indication";
str += " from ";
/** NimBLEAddress and NimBLEUUID have std::string operators */
str += std::string(pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress());
str += std::string(pRemoteCharacteristic->getClient()->getPeerAddress());
str += ": Service = " + std::string(pRemoteCharacteristic->getRemoteService()->getUUID());
str += ", Characteristic = " + std::string(pRemoteCharacteristic->getUUID());
str += ", Value = " + std::string((char*)pData, length);
Expand Down
2 changes: 1 addition & 1 deletion examples/NimBLE_Server/NimBLE_Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void loop() {
if(pSvc) {
NimBLECharacteristic* pChr = pSvc->getCharacteristic("F00D");
if(pChr) {
pChr->notify(true);
pChr->notify();
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/NimBLEAttValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,15 @@ class NimBLEAttValue {
return setValue(reinterpret_cast<const uint8_t*>(s), len);
}

/**
* @brief Get a pointer to the value buffer with timestamp.
* @param[in] timestamp A pointer to a time_t variable to store the timestamp.
* @returns A pointer to the internal value buffer.
*/
const uint8_t* getValue(time_t* timestamp = nullptr) const {
const NimBLEAttValue& getValue(time_t* timestamp = nullptr) const {
if (timestamp != nullptr) {
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
*timestamp = m_timestamp;
# else
*timestamp = 0;
# endif
}
return m_attr_value;
return *this;
}

/**
Expand Down Expand Up @@ -271,7 +266,15 @@ class NimBLEAttValue {
if (!skipSizeCheck && size() < sizeof(T)) {
return T();
}
return *(reinterpret_cast<const T*>(getValue(timestamp)));
if (timestamp != nullptr) {
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
*timestamp = m_timestamp;
# else
*timestamp = 0;
# endif
}

return *(reinterpret_cast<const T*>(m_attr_value));
}

/*********************** Operators ************************/
Expand Down
50 changes: 50 additions & 0 deletions src/NimBLEAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* NimBLEAttribute.h
*
* Created: on July 28 2024
* Author H2zero
*/

#ifndef NIMBLE_CPP_ATTRIBUTE_H_
#define NIMBLE_CPP_ATTRIBUTE_H_

#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && (defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL))

# include "NimBLEUUID.h"

/**
* @brief A base class for BLE attributes.
*/
class NimBLEAttribute {
public:
/**
* @brief Get the UUID of the attribute.
* @return The UUID.
*/
const NimBLEUUID& getUUID() const { return m_uuid; }

/**
* @brief Get the handle of the attribute.
*/
uint16_t getHandle() const { return m_handle; };

protected:
/**
* @brief Construct a new NimBLEAttribute object.
* @param [in] handle The handle of the attribute.
* @param [in] uuid The UUID of the attribute.
*/
NimBLEAttribute(const NimBLEUUID& uuid, uint16_t handle) : m_uuid{uuid}, m_handle{handle} {}

/**
* @brief Destroy the NimBLEAttribute object.
*/
~NimBLEAttribute() = default;

const NimBLEUUID m_uuid{};
uint16_t m_handle{0};
};

#endif // CONFIG_BT_ENABLED && (CONFIG_BT_NIMBLE_ROLE_PERIPHERAL || CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // NIMBLE_CPP_ATTRIBUTE_H_
Loading

0 comments on commit eb28997

Please sign in to comment.