Skip to content
Merged
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
59 changes: 53 additions & 6 deletions src/NimBLEAttValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ class NimBLEAttValue {
* @param[in] len The size in bytes of the value to set.
* @param[in] max_len The max size in bytes that the value can be.
*/
NimBLEAttValue(const uint8_t *value, uint16_t len,
uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);

/**
* @brief Construct with an initial value from a const char string.
* @param value A pointer to the initial value to set.
* @param[in] max_len The max size in bytes that the value can be.
*/
NimBLEAttValue(const char *value, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
:NimBLEAttValue((uint8_t*)value, (uint16_t)strlen(value), max_len){}
NimBLEAttValue(const char* value, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
: NimBLEAttValue((uint8_t*)value, (uint16_t)strlen(value), max_len) {}

/**
* @brief Construct with an initializer list.
Expand Down Expand Up @@ -221,14 +220,61 @@ class NimBLEAttValue {

/*********************** Template Functions ************************/

# if __cplusplus < 201703L
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] v The <type\>value to set.
* @details Only used for types without a `c_str()` and `length()` or `data()` and `size()` method.
* <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
setValue(const T& v) {
return setValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T));
}

/**
* @brief Template to set value to the value of <type\>val.
* @param [in] s The <type\>value to set.
* @note This function is only availabe if the type T is not a pointer.
* @details Only used if the <type\> has a `c_str()` method.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
setValue(const T& s) {
return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
}

/**
* @brief Template to set value to the value of <type\>val.
* @param [in] v The <type\>value to set.
* @details Only used if the <type\> has a `data()` and `size()` method.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
setValue(const T& v) {
return setValue(reinterpret_cast<const uint8_t*>(v.data()), v.size());
}

# else
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] s The <type\>value to set.
* @note This function is only available if the type T is not a pointer.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type setValue(const T& s) {
if constexpr (Has_data_size<T>::value) {
return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size());
} else if constexpr (Has_c_str_length<T>::value) {
Expand All @@ -237,6 +283,7 @@ class NimBLEAttValue {
return setValue(reinterpret_cast<const uint8_t*>(&s), sizeof(s));
}
}
# endif

/**
* @brief Template to return the value as a <type\>.
Expand Down
96 changes: 96 additions & 0 deletions src/NimBLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,101 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {

/*********************** Template Functions ************************/

# if __cplusplus < 201703L
/**
* @brief Template to send a notification with a value from a struct or array.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
* @details <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
}

/**
* @brief Template to send a notification with a value from a class that has a c_str() and length() method.
* @param [in] s The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
notify(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
}

/**
* @brief Template to send a notification with a value from a class that has a data() and size() method.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
}

/**
* @brief Template to send an indication with a value from a struct or array.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
* @details <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
}

/**
* @brief Template to send a indication with a value from a class that has a c_str() and length() method.
* @param [in] s The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
indicate(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
}

/**
* @brief Template to send a indication with a value from a class that has a data() and size() method.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
}

# else

/**
* @brief Template to send a notification for classes which may have
* data()/size() or c_str()/length() methods. Falls back to sending
Expand Down Expand Up @@ -115,6 +210,7 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
return indicate(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
}
}
# endif

private:
friend class NimBLEServer;
Expand Down
55 changes: 53 additions & 2 deletions src/NimBLERemoteValueAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,65 @@ class NimBLERemoteValueAttribute : public NimBLEAttribute {
return writeValue(reinterpret_cast<const uint8_t*>(str), length ? length : strlen(str), response);
}

# if __cplusplus < 201703L
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] v The value to write.
* @param [in] response True == request write response.
* @details Only used for types without a `c_str()` and `length()` or `data()` and `size()` method.
* <type\> size must be evaluatable by `sizeof()` if no length is provided.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& v, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T), response);
}

/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] s The value to write.
* @param [in] response True == request write response.
* @note This function is only available if the type T is not a pointer.
* @details Only used if the <type\> has a `c_str()` and `length()` method.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& s, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), response);
}

/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] v The value to write.
* @param [in] response True == request write response.
* @details Only used if the <type\> has a `data()` and `size()` method.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& v, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
}

# else
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] s The value to write.
* @param [in] response True == request write response.
* @note This function is only available if the type T is not a pointer.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type writeValue(const T& v, bool response = false) const {
if constexpr (Has_data_size<T>::value) {
return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
} else if constexpr (Has_c_str_length<T>::value) {
Expand All @@ -91,6 +141,7 @@ class NimBLERemoteValueAttribute : public NimBLEAttribute {
return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(v), response);
}
}
# endif

/**
* @brief Template to convert the remote characteristic data to <type\>.
Expand Down