diff --git a/src/NimBLEAttValue.h b/src/NimBLEAttValue.h index 56ceae46..9ef92d52 100644 --- a/src/NimBLEAttValue.h +++ b/src/NimBLEAttValue.h @@ -253,6 +253,23 @@ class NimBLEAttValue { /*********************** Template Functions ************************/ # if __cplusplus < 201703L + /** + * @brief Template to set value to the value of a char array using strnlen. + * @param [in] s A reference to a char array. + * @details Only used for char array types to correctly determine length via strnlen. + */ + template +# ifdef _DOXYGEN_ + bool +# else + typename std::enable_if::value && + std::is_same::type, char>::value, + bool>::type +# endif + setValue(const T& s) { + return setValue(reinterpret_cast(s), strnlen(s, sizeof(T))); + } + /** * @brief Template to set value to the value of val. * @param [in] v The value to set. @@ -263,7 +280,10 @@ class NimBLEAttValue { # ifdef _DOXYGEN_ bool # else - typename std::enable_if::value && !Has_c_str_length::value && !Has_data_size::value, bool>::type + typename std::enable_if::value && !Has_c_str_length::value && !Has_data_size::value && + !(std::is_array::value && + std::is_same::type, char>::value), + bool>::type # endif setValue(const T& v) { return setValue(reinterpret_cast(&v), sizeof(T)); @@ -334,6 +354,9 @@ class NimBLEAttValue { } } else if constexpr (Has_c_str_length::value) { return setValue(reinterpret_cast(s.c_str()), s.length()); + } else if constexpr (std::is_array::value && + std::is_same::type, char>::value) { + return setValue(reinterpret_cast(s), strnlen(s, sizeof(s))); } else { return setValue(reinterpret_cast(&s), sizeof(s)); }