@@ -112,15 +112,35 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
112112 }
113113
114114 /* *
115- * @brief Template to send a notification with a value from a class that has a data() and size() method.
115+ * @brief Template to send a notification with a value from a class that has a data() and size() method with value_type .
116116 * @param [in] v The value to send.
117117 * @param [in] connHandle Optional, a connection handle to send the notification to.
118+ * @details Correctly calculates byte size for containers with multi-byte element types.
118119 */
119120 template <typename T>
120121# ifdef _DOXYGEN_
121122 bool
122123# else
123- typename std::enable_if<Has_data_size<T>::value, bool >::type
124+ typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool >::type
125+ # endif
126+ notify (const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
127+ return notify (
128+ reinterpret_cast <const uint8_t *>(v.data ()),
129+ v.size () * sizeof (typename T::value_type),
130+ connHandle
131+ );
132+ }
133+
134+ /* *
135+ * @brief Template to send a notification with a value from a class that has a data() and size() method without value_type.
136+ * @param [in] v The value to send.
137+ * @param [in] connHandle Optional, a connection handle to send the notification to.
138+ */
139+ template <typename T>
140+ # ifdef _DOXYGEN_
141+ bool
142+ # else
143+ typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value, bool >::type
124144# endif
125145 notify (const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
126146 return notify (reinterpret_cast <const uint8_t *>(v.data ()), v.size (), connHandle);
@@ -160,15 +180,35 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
160180 }
161181
162182 /* *
163- * @brief Template to send a indication with a value from a class that has a data() and size() method.
183+ * @brief Template to send a indication with a value from a class that has a data() and size() method with value_type.
184+ * @param [in] v The value to send.
185+ * @param [in] connHandle Optional, a connection handle to send the notification to.
186+ * @details Correctly calculates byte size for containers with multi-byte element types.
187+ */
188+ template <typename T>
189+ # ifdef _DOXYGEN_
190+ bool
191+ # else
192+ typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool >::type
193+ # endif
194+ indicate (const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
195+ return indicate (
196+ reinterpret_cast <const uint8_t *>(v.data ()),
197+ v.size () * sizeof (typename T::value_type),
198+ connHandle
199+ );
200+ }
201+
202+ /* *
203+ * @brief Template to send a indication with a value from a class that has a data() and size() method without value_type.
164204 * @param [in] v The value to send.
165205 * @param [in] connHandle Optional, a connection handle to send the notification to.
166206 */
167207 template <typename T>
168208# ifdef _DOXYGEN_
169209 bool
170210# else
171- typename std::enable_if<Has_data_size<T>::value, bool >::type
211+ typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value , bool >::type
172212# endif
173213 indicate (const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
174214 return indicate (reinterpret_cast <const uint8_t *>(v.data ()), v.size (), connHandle);
@@ -190,7 +230,11 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
190230 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool >::type notify (
191231 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
192232 if constexpr (Has_data_size<T>::value) {
193- return notify (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), connHandle);
233+ if constexpr (Has_value_type<T>::value) {
234+ return notify (reinterpret_cast <const uint8_t *>(value.data ()), value.size () * sizeof (typename T::value_type), connHandle);
235+ } else {
236+ return notify (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), connHandle);
237+ }
194238 } else if constexpr (Has_c_str_length<T>::value) {
195239 return notify (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length (), connHandle);
196240 } else {
@@ -212,7 +256,11 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
212256 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool >::type indicate (
213257 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
214258 if constexpr (Has_data_size<T>::value) {
215- return indicate (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), connHandle);
259+ if constexpr (Has_value_type<T>::value) {
260+ return indicate (reinterpret_cast <const uint8_t *>(value.data ()), value.size () * sizeof (typename T::value_type), connHandle);
261+ } else {
262+ return indicate (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), connHandle);
263+ }
216264 } else if constexpr (Has_c_str_length<T>::value) {
217265 return indicate (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length (), connHandle);
218266 } else {
0 commit comments