NimBLE-Arduino 2.5.0
Loading...
Searching...
No Matches
NimBLEAttValue.h
1/*
2 * Copyright 2020-2026 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_ATTVALUE_H
19#define NIMBLE_CPP_ATTVALUE_H
20
21#include "nimconfig.h"
22#if CONFIG_BT_ENABLED
23
24/* Enables the use of Arduino String class for attribute values */
25# ifndef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
26# define NIMBLE_CPP_ARDUINO_STRING_AVAILABLE (__has_include(<Arduino.h>))
27# endif
28
29# if NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
30# include <WString.h>
31# endif
32
33# include <string>
34# include <vector>
35# include <ctime>
36# include <cstring>
37# include <cstdint>
38
39# ifndef CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
40# define CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED 0
41# endif
42
43# ifndef BLE_ATT_ATTR_MAX_LEN
44# define BLE_ATT_ATTR_MAX_LEN 512
45# endif
46
47# if !defined(CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH)
48# define CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH 20
49# elif CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH > BLE_ATT_ATTR_MAX_LEN
50# error CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH cannot be larger than 512 (BLE_ATT_ATTR_MAX_LEN)
51# elif CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH < 1
52# error CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH cannot be less than 1; Range = 1 : 512
53# endif
54
55/* Used to determine if the type passed to a template has a data() and size() method. */
56template <typename T, typename = void, typename = void>
57struct Has_data_size : std::false_type {};
58
59template <typename T>
60struct Has_data_size<T, decltype(void(std::declval<T&>().data())), decltype(void(std::declval<T&>().size()))>
61 : std::true_type {};
62
63/* Used to determine if the type passed to a template has a c_str() and length() method. */
64template <typename T, typename = void, typename = void>
65struct Has_c_str_length : std::false_type {};
66
67template <typename T>
68struct Has_c_str_length<T, decltype(void(std::declval<T&>().c_str())), decltype(void(std::declval<T&>().length()))>
69 : std::true_type {};
70
71/* Used to determine if the type passed to a template has a value_type member (std::vector, std::array, std::string, etc.). */
72template <typename T, typename = void>
73struct Has_value_type : std::false_type {};
74
75template <typename T>
76struct Has_value_type<T, decltype(void(sizeof(typename T::value_type)))>
77 : std::true_type {};
78
86 uint8_t* m_attr_value{};
87 uint16_t m_attr_max_len{};
88 uint16_t m_attr_len{};
89 uint16_t m_capacity{};
90# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
91 time_t m_timestamp{};
92# endif
93 void deepCopy(const NimBLEAttValue& source);
94
95 public:
101 NimBLEAttValue(uint16_t init_len = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
102
109 NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
110
116 NimBLEAttValue(const char* value, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
117 : NimBLEAttValue((uint8_t*)value, (uint16_t)strlen(value), max_len) {}
118
124 NimBLEAttValue(std::initializer_list<uint8_t> list, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
125 : NimBLEAttValue(list.begin(), list.size(), max_len) {}
126
132 NimBLEAttValue(const std::string str, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
133 : NimBLEAttValue(reinterpret_cast<const uint8_t*>(&str[0]), str.length(), max_len) {}
134
140 NimBLEAttValue(const std::vector<uint8_t> vec, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
141 : NimBLEAttValue(&vec[0], vec.size(), max_len) {}
142
143# if NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
149 NimBLEAttValue(const String str, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
150 : NimBLEAttValue(reinterpret_cast<const uint8_t*>(str.c_str()), str.length(), max_len) {}
151# endif
152
154 NimBLEAttValue(const NimBLEAttValue& source) { deepCopy(source); }
155
157 NimBLEAttValue(NimBLEAttValue&& source) { *this = std::move(source); }
158
161
163 uint16_t max_size() const { return m_attr_max_len; }
164
166 uint16_t capacity() const { return m_capacity; }
167
169 uint16_t length() const { return m_attr_len; }
170
172 uint16_t size() const { return m_attr_len; }
173
175 const uint8_t* data() const { return m_attr_value; }
176
178 const char* c_str() const { return reinterpret_cast<const char*>(m_attr_value); }
179
181 const uint8_t* begin() const { return m_attr_value; }
182
184 const uint8_t* end() const { return m_attr_value + m_attr_len; }
185
186# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
188 time_t getTimeStamp() const { return m_timestamp; }
189
191 void setTimeStamp() { m_timestamp = time(nullptr); }
192
197 void setTimeStamp(time_t t) { m_timestamp = t; }
198# else
199 time_t getTimeStamp() const { return 0; }
200 void setTimeStamp() {}
201 void setTimeStamp(time_t t) {}
202# endif
203
210 bool setValue(const uint8_t* value, uint16_t len);
211
217 bool setValue(const char* s, uint16_t len = 0) {
218 if (len == 0) {
219 len = strlen(s);
220 }
221 return setValue(reinterpret_cast<const uint8_t*>(s), len);
222 }
223
224 const NimBLEAttValue& getValue(time_t* timestamp = nullptr) const {
225 if (timestamp != nullptr) {
226# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
227 *timestamp = m_timestamp;
228# else
229 *timestamp = 0;
230# endif
231 }
232 return *this;
233 }
234
241 NimBLEAttValue& append(const uint8_t* value, uint16_t len);
242
243 /*********************** Template Functions ************************/
244
245# if __cplusplus < 201703L
251 template <typename T>
252# ifdef _DOXYGEN_
253 bool
254# else
255 typename std::enable_if<std::is_array<T>::value &&
256 std::is_same<typename std::remove_extent<T>::type, char>::value,
257 bool>::type
258# endif
259 setValue(const T& s) {
260 return setValue(reinterpret_cast<const uint8_t*>(s), strnlen(s, sizeof(T)));
261 }
262
269 template <typename T>
270# ifdef _DOXYGEN_
271 bool
272# else
273 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value &&
274 !(std::is_array<T>::value &&
275 std::is_same<typename std::remove_extent<T>::type, char>::value),
276 bool>::type
277# endif
278 setValue(const T& v) {
279 return setValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T));
280 }
281
287 template <typename T>
288# ifdef _DOXYGEN_
289 bool
290# else
291 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
292# endif
293 setValue(const T& s) {
294 return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
295 }
296
303 template <typename T>
304# ifdef _DOXYGEN_
305 bool
306# else
307 typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool>::type
308# endif
309 setValue(const T& v) {
310 return setValue(
311 reinterpret_cast<const uint8_t*>(v.data()),
312 v.size() * sizeof(typename T::value_type)
313 );
314 }
315
321 template <typename T>
322# ifdef _DOXYGEN_
323 bool
324# else
325 typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value, bool>::type
326# endif
327 setValue(const T& v) {
328 return setValue(reinterpret_cast<const uint8_t*>(v.data()), v.size());
329 }
330
331# else
337 template <typename T>
338 typename std::enable_if<!std::is_pointer<T>::value, bool>::type setValue(const T& s) {
339 if constexpr (Has_data_size<T>::value) {
340 if constexpr (Has_value_type<T>::value) {
341 return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size() * sizeof(typename T::value_type));
342 } else {
343 return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size());
344 }
345 } else if constexpr (Has_c_str_length<T>::value) {
346 return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
347 } else if constexpr (std::is_array<T>::value &&
348 std::is_same<typename std::remove_extent<T>::type, char>::value) {
349 return setValue(reinterpret_cast<const uint8_t*>(s), strnlen(s, sizeof(s)));
350 } else {
351 return setValue(reinterpret_cast<const uint8_t*>(&s), sizeof(s));
352 }
353 }
354# endif
355
366 template <typename T>
367 T getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
368 if (timestamp != nullptr) {
369# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
370 *timestamp = m_timestamp;
371# else
372 *timestamp = 0;
373# endif
374 }
375
376 if (!skipSizeCheck && size() < sizeof(T)) {
377 return T();
378 }
379 return *(reinterpret_cast<const T*>(m_attr_value));
380 }
381
382 /*********************** Operators ************************/
383
385 uint8_t operator[](int pos) const;
386
388 operator std::vector<uint8_t>() const { return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); }
389
391 operator std::string() const { return std::string(reinterpret_cast<char*>(m_attr_value), m_attr_len); }
392
394 operator const uint8_t*() const { return m_attr_value; }
395
397 NimBLEAttValue& operator+=(const NimBLEAttValue& source) { return append(source.data(), source.size()); }
398
400 NimBLEAttValue& operator=(const std::string& source) {
401 setValue(reinterpret_cast<const uint8_t*>(&source[0]), source.size());
402 return *this;
403 }
404
407
410
412 bool operator==(const NimBLEAttValue& source) const {
413 return (m_attr_len == source.size()) ? memcmp(m_attr_value, source.data(), m_attr_len) == 0 : false;
414 }
415
417 bool operator!=(const NimBLEAttValue& source) const { return !(*this == source); }
418
419# if NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
421 operator String() const { return String(reinterpret_cast<char*>(m_attr_value)); }
422# endif
423};
424
425#endif // CONFIG_BT_ENABLED
426#endif // NIMBLE_CPP_ATTVALUE_H_
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:85
NimBLEAttValue(const std::vector< uint8_t > vec, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Construct with an initial value from a std::vector<uint8_t>.
Definition NimBLEAttValue.h:140
~NimBLEAttValue()
Destructor.
Definition NimBLEAttValue.cpp:57
uint16_t length() const
Returns the current length of the value in bytes.
Definition NimBLEAttValue.h:169
NimBLEAttValue(const char *value, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Construct with an initial value from a const char string.
Definition NimBLEAttValue.h:116
uint16_t capacity() const
Returns the currently allocated capacity in bytes.
Definition NimBLEAttValue.h:166
const char * c_str() const
Returns a pointer to the internal buffer of the value as a const char*.
Definition NimBLEAttValue.h:178
NimBLEAttValue(const std::string str, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Construct with an initial value from a std::string.
Definition NimBLEAttValue.h:132
bool setValue(const uint8_t *value, uint16_t len)
Set the value from a buffer.
Definition NimBLEAttValue.cpp:106
NimBLEAttValue(std::initializer_list< uint8_t > list, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Construct with an initializer list.
Definition NimBLEAttValue.h:124
NimBLEAttValue & operator+=(const NimBLEAttValue &source)
Operator; Append another NimBLEAttValue.
Definition NimBLEAttValue.h:397
NimBLEAttValue(const NimBLEAttValue &source)
Copy constructor.
Definition NimBLEAttValue.h:154
NimBLEAttValue(NimBLEAttValue &&source)
Move constructor.
Definition NimBLEAttValue.h:157
const uint8_t * data() const
Returns a pointer to the internal buffer of the value.
Definition NimBLEAttValue.h:175
uint8_t operator[](int pos) const
Subscript operator.
Definition NimBLEAttValue.cpp:153
const uint8_t * end() const
Iterator end.
Definition NimBLEAttValue.h:184
bool setValue(const T &v)
Template to set value to the value of <type>val.
Definition NimBLEAttValue.h:278
NimBLEAttValue & append(const uint8_t *value, uint16_t len)
Append data to the value.
Definition NimBLEAttValue.cpp:114
uint16_t size() const
Returns the current size of the value in bytes.
Definition NimBLEAttValue.h:172
bool setValue(const char *s, uint16_t len=0)
Set value to the value of const char*.
Definition NimBLEAttValue.h:217
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false) const
Template to return the value as a <type>.
Definition NimBLEAttValue.h:367
uint16_t max_size() const
Returns the max size in bytes.
Definition NimBLEAttValue.h:163
bool setValue(const T &s)
Template to set value to the value of a char array using strnlen.
Definition NimBLEAttValue.h:259
bool operator==(const NimBLEAttValue &source) const
Equality operator.
Definition NimBLEAttValue.h:412
NimBLEAttValue & operator=(const std::string &source)
Operator; Set the value from a std::string source.
Definition NimBLEAttValue.h:400
const uint8_t * begin() const
Iterator begin.
Definition NimBLEAttValue.h:181
bool operator!=(const NimBLEAttValue &source) const
Inequality operator.
Definition NimBLEAttValue.h:417