forked from h2zero/esp-nimble-cpp
[BREAKING] Refactor attributes
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. * `NimBLERemoteCharacteristic::getRemoteService` now returns a `const NimBLERemoteService*` instead of non-const. * 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 *>. * Remove const requirement for NimBLEConnInfo parameter in callbacks. Const is unnecessary as the data can't be changed by application code. * Change NimBLERemoteCharacteristic::getRemoteService to return const pointer.
This commit is contained in:
@@ -12,93 +12,34 @@
|
||||
* Author: kolban
|
||||
*/
|
||||
|
||||
#ifndef COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
|
||||
#define COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
|
||||
#ifndef NIMBLE_CPP_REMOTE_DESCRIPTOR_H_
|
||||
#define NIMBLE_CPP_REMOTE_DESCRIPTOR_H_
|
||||
|
||||
#include "nimconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
||||
|
||||
#include "NimBLERemoteCharacteristic.h"
|
||||
# include "NimBLERemoteValueAttribute.h"
|
||||
|
||||
class NimBLERemoteCharacteristic;
|
||||
class NimBLEClient;
|
||||
|
||||
/**
|
||||
* @brief A model of remote %BLE descriptor.
|
||||
* @brief A model of remote BLE descriptor.
|
||||
*/
|
||||
class NimBLERemoteDescriptor {
|
||||
public:
|
||||
uint16_t getHandle();
|
||||
NimBLERemoteCharacteristic* getRemoteCharacteristic();
|
||||
NimBLEUUID getUUID();
|
||||
NimBLEAttValue readValue();
|
||||
std::string toString(void);
|
||||
bool writeValue(const uint8_t* data, size_t length, bool response = false);
|
||||
bool writeValue(const std::vector<uint8_t>& v, bool response = false);
|
||||
bool writeValue(const char* s, bool response = false);
|
||||
class NimBLERemoteDescriptor : public NimBLERemoteValueAttribute {
|
||||
public:
|
||||
NimBLERemoteCharacteristic* getRemoteCharacteristic() const;
|
||||
std::string toString(void) const;
|
||||
NimBLEClient* getClient() const override;
|
||||
|
||||
private:
|
||||
friend class NimBLERemoteCharacteristic;
|
||||
|
||||
/*********************** Template Functions ************************/
|
||||
NimBLERemoteDescriptor(const NimBLERemoteCharacteristic* pRemoteCharacteristic, const ble_gatt_dsc* dsc);
|
||||
~NimBLERemoteDescriptor() = default;
|
||||
|
||||
/**
|
||||
* @brief Template to set the remote descriptor value to <type\>val.
|
||||
* @param [in] s The value to write.
|
||||
* @param [in] response True == request write response.
|
||||
* @details Only used for non-arrays and types without a `c_str()` method.
|
||||
*/
|
||||
template<typename T>
|
||||
#ifdef _DOXYGEN_
|
||||
bool
|
||||
#else
|
||||
typename std::enable_if<!std::is_array<T>::value && !Has_c_str_len<T>::value, bool>::type
|
||||
#endif
|
||||
writeValue(const T& s, bool response = false) {
|
||||
return writeValue((uint8_t*)&s, sizeof(T), response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Template to set the remote descriptor value to <type\>val.
|
||||
* @param [in] s The value to write.
|
||||
* @param [in] response True == request write response.
|
||||
* @details Only used if the <type\> has a `c_str()` method.
|
||||
*/
|
||||
template<typename T>
|
||||
#ifdef _DOXYGEN_
|
||||
bool
|
||||
#else
|
||||
typename std::enable_if<Has_c_str_len<T>::value, bool>::type
|
||||
#endif
|
||||
writeValue(const T& s, bool response = false) {
|
||||
return writeValue((uint8_t*)s.c_str(), s.length(), response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Template to convert the remote descriptor data to <type\>.
|
||||
* @tparam T The type to convert the data to.
|
||||
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
||||
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
||||
* less than <tt>sizeof(<type\>)</tt>.
|
||||
* @details <b>Use:</b> <tt>readValue<type>(skipSizeCheck);</tt>
|
||||
*/
|
||||
template<typename T>
|
||||
T readValue(bool skipSizeCheck = false) {
|
||||
NimBLEAttValue value = readValue();
|
||||
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
||||
return *((T *)value.data());
|
||||
}
|
||||
|
||||
private:
|
||||
friend class NimBLERemoteCharacteristic;
|
||||
|
||||
NimBLERemoteDescriptor (NimBLERemoteCharacteristic* pRemoteCharacteristic,
|
||||
const struct ble_gatt_dsc *dsc);
|
||||
static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
||||
struct ble_gatt_attr *attr, void *arg);
|
||||
static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
||||
struct ble_gatt_attr *attr, void *arg);
|
||||
|
||||
uint16_t m_handle;
|
||||
NimBLEUUID m_uuid;
|
||||
NimBLERemoteCharacteristic* m_pRemoteCharacteristic;
|
||||
const NimBLERemoteCharacteristic* m_pRemoteCharacteristic;
|
||||
};
|
||||
|
||||
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
|
||||
#endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */
|
||||
#endif /* NIMBLE_CPP_REMOTE_DESCRIPTOR_H_ */
|
||||
|
||||
Reference in New Issue
Block a user