NimBLE-Arduino 2.3.7
Loading...
Searching...
No Matches
NimBLEAttValue.h
1/*
2 * Copyright 2020-2025 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# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
25# include <Arduino.h>
26# endif
27
28# include <string>
29# include <vector>
30# include <ctime>
31# include <cstring>
32# include <cstdint>
33
34# ifndef CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
35# define CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED 0
36# endif
37
38# ifndef BLE_ATT_ATTR_MAX_LEN
39# define BLE_ATT_ATTR_MAX_LEN 512
40# endif
41
42# if !defined(CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH)
43# define CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH 20
44# elif CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH > BLE_ATT_ATTR_MAX_LEN
45# error CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH cannot be larger than 512 (BLE_ATT_ATTR_MAX_LEN)
46# elif CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH < 1
47# error CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH cannot be less than 1; Range = 1 : 512
48# endif
49
50/* Used to determine if the type passed to a template has a data() and size() method. */
51template <typename T, typename = void, typename = void>
52struct Has_data_size : std::false_type {};
53
54template <typename T>
55struct Has_data_size<T, decltype(void(std::declval<T&>().data())), decltype(void(std::declval<T&>().size()))>
56 : std::true_type {};
57
58/* Used to determine if the type passed to a template has a c_str() and length() method. */
59template <typename T, typename = void, typename = void>
60struct Has_c_str_length : std::false_type {};
61
62template <typename T>
63struct Has_c_str_length<T, decltype(void(std::declval<T&>().c_str())), decltype(void(std::declval<T&>().length()))>
64 : std::true_type {};
65
66/* Used to determine if the type passed to a template has a value_type member (std::vector, std::array, std::string, etc.). */
67template <typename T, typename = void>
68struct Has_value_type : std::false_type {};
69
70template <typename T>
71struct Has_value_type<T, decltype(void(sizeof(typename T::value_type)))>
72 : std::true_type {};
73
81 uint8_t* m_attr_value{};
82 uint16_t m_attr_max_len{};
83 uint16_t m_attr_len{};
84 uint16_t m_capacity{};
85# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
86 time_t m_timestamp{};
87# endif
88 void deepCopy(const NimBLEAttValue& source);
89
90 public:
96 NimBLEAttValue(uint16_t init_len = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
97
104 NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
105
111 NimBLEAttValue(const char* value, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
112 : NimBLEAttValue((uint8_t*)value, (uint16_t)strlen(value), max_len) {}
113
119 NimBLEAttValue(std::initializer_list<uint8_t> list, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
120 : NimBLEAttValue(list.begin(), list.size(), max_len) {}
121
127 NimBLEAttValue(const std::string str, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
128 : NimBLEAttValue(reinterpret_cast<const uint8_t*>(&str[0]), str.length(), max_len) {}
129
135 NimBLEAttValue(const std::vector<uint8_t> vec, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
136 : NimBLEAttValue(&vec[0], vec.size(), max_len) {}
137
138# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
144 NimBLEAttValue(const String str, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
145 : NimBLEAttValue(reinterpret_cast<const uint8_t*>(str.c_str()), str.length(), max_len) {}
146# endif
147
149 NimBLEAttValue(const NimBLEAttValue& source) { deepCopy(source); }
150
152 NimBLEAttValue(NimBLEAttValue&& source) { *this = std::move(source); }
153
156
158 uint16_t max_size() const { return m_attr_max_len; }
159
161 uint16_t capacity() const { return m_capacity; }
162
164 uint16_t length() const { return m_attr_len; }
165
167 uint16_t size() const { return m_attr_len; }
168
170 const uint8_t* data() const { return m_attr_value; }
171
173 const char* c_str() const { return reinterpret_cast<const char*>(m_attr_value); }
174
176 const uint8_t* begin() const { return m_attr_value; }
177
179 const uint8_t* end() const { return m_attr_value + m_attr_len; }
180
181# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
183 time_t getTimeStamp() const { return m_timestamp; }
184
186 void setTimeStamp() { m_timestamp = time(nullptr); }
187
192 void setTimeStamp(time_t t) { m_timestamp = t; }
193# else
194 time_t getTimeStamp() const { return 0; }
195 void setTimeStamp() {}
196 void setTimeStamp(time_t t) {}
197# endif
198
205 bool setValue(const uint8_t* value, uint16_t len);
206
212 bool setValue(const char* s, uint16_t len = 0) {
213 if (len == 0) {
214 len = strlen(s);
215 }
216 return setValue(reinterpret_cast<const uint8_t*>(s), len);
217 }
218
219 const NimBLEAttValue& getValue(time_t* timestamp = nullptr) const {
220 if (timestamp != nullptr) {
221# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
222 *timestamp = m_timestamp;
223# else
224 *timestamp = 0;
225# endif
226 }
227 return *this;
228 }
229
236 NimBLEAttValue& append(const uint8_t* value, uint16_t len);
237
238 /*********************** Template Functions ************************/
239
240# if __cplusplus < 201703L
247 template <typename T>
248# ifdef _DOXYGEN_
249 bool
250# else
251 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
252# endif
253 setValue(const T& v) {
254 return setValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T));
255 }
256
262 template <typename T>
263# ifdef _DOXYGEN_
264 bool
265# else
266 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
267# endif
268 setValue(const T& s) {
269 return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
270 }
271
278 template <typename T>
279# ifdef _DOXYGEN_
280 bool
281# else
282 typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool>::type
283# endif
284 setValue(const T& v) {
285 return setValue(
286 reinterpret_cast<const uint8_t*>(v.data()),
287 v.size() * sizeof(typename T::value_type)
288 );
289 }
290
296 template <typename T>
297# ifdef _DOXYGEN_
298 bool
299# else
300 typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value, bool>::type
301# endif
302 setValue(const T& v) {
303 return setValue(reinterpret_cast<const uint8_t*>(v.data()), v.size());
304 }
305
306# else
312 template <typename T>
313 typename std::enable_if<!std::is_pointer<T>::value, bool>::type setValue(const T& s) {
314 if constexpr (Has_data_size<T>::value) {
315 if constexpr (Has_value_type<T>::value) {
316 return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size() * sizeof(typename T::value_type));
317 } else {
318 return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size());
319 }
320 } else if constexpr (Has_c_str_length<T>::value) {
321 return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
322 } else {
323 return setValue(reinterpret_cast<const uint8_t*>(&s), sizeof(s));
324 }
325 }
326# endif
327
338 template <typename T>
339 T getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
340 if (timestamp != nullptr) {
341# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
342 *timestamp = m_timestamp;
343# else
344 *timestamp = 0;
345# endif
346 }
347
348 if (!skipSizeCheck && size() < sizeof(T)) {
349 return T();
350 }
351 return *(reinterpret_cast<const T*>(m_attr_value));
352 }
353
354 /*********************** Operators ************************/
355
357 uint8_t operator[](int pos) const;
358
360 operator std::vector<uint8_t>() const { return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); }
361
363 operator std::string() const { return std::string(reinterpret_cast<char*>(m_attr_value), m_attr_len); }
364
366 operator const uint8_t*() const { return m_attr_value; }
367
369 NimBLEAttValue& operator+=(const NimBLEAttValue& source) { return append(source.data(), source.size()); }
370
372 NimBLEAttValue& operator=(const std::string& source) {
373 setValue(reinterpret_cast<const uint8_t*>(&source[0]), source.size());
374 return *this;
375 }
376
379
382
384 bool operator==(const NimBLEAttValue& source) const {
385 return (m_attr_len == source.size()) ? memcmp(m_attr_value, source.data(), m_attr_len) == 0 : false;
386 }
387
389 bool operator!=(const NimBLEAttValue& source) const { return !(*this == source); }
390
391# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
393 operator String() const { return String(reinterpret_cast<char*>(m_attr_value)); }
394# endif
395};
396
397#endif // CONFIG_BT_ENABLED
398#endif // NIMBLE_CPP_ATTVALUE_H_
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:80
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:135
~NimBLEAttValue()
Destructor.
Definition NimBLEAttValue.cpp:57
uint16_t length() const
Returns the current length of the value in bytes.
Definition NimBLEAttValue.h:164
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:111
uint16_t capacity() const
Returns the currently allocated capacity in bytes.
Definition NimBLEAttValue.h:161
const char * c_str() const
Returns a pointer to the internal buffer of the value as a const char*.
Definition NimBLEAttValue.h:173
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:127
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:119
NimBLEAttValue & operator+=(const NimBLEAttValue &source)
Operator; Append another NimBLEAttValue.
Definition NimBLEAttValue.h:369
NimBLEAttValue(const NimBLEAttValue &source)
Copy constructor.
Definition NimBLEAttValue.h:149
NimBLEAttValue(NimBLEAttValue &&source)
Move constructor.
Definition NimBLEAttValue.h:152
const uint8_t * data() const
Returns a pointer to the internal buffer of the value.
Definition NimBLEAttValue.h:170
uint8_t operator[](int pos) const
Subscript operator.
Definition NimBLEAttValue.cpp:153
const uint8_t * end() const
Iterator end.
Definition NimBLEAttValue.h:179
bool setValue(const T &v)
Template to set value to the value of <type>val.
Definition NimBLEAttValue.h:253
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:167
bool setValue(const char *s, uint16_t len=0)
Set value to the value of const char*.
Definition NimBLEAttValue.h:212
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false) const
Template to return the value as a <type>.
Definition NimBLEAttValue.h:339
uint16_t max_size() const
Returns the max size in bytes.
Definition NimBLEAttValue.h:158
bool setValue(const T &s)
Template to set value to the value of <type>val.
Definition NimBLEAttValue.h:268
bool operator==(const NimBLEAttValue &source) const
Equality operator.
Definition NimBLEAttValue.h:384
NimBLEAttValue & operator=(const std::string &source)
Operator; Set the value from a std::string source.
Definition NimBLEAttValue.h:372
const uint8_t * begin() const
Iterator begin.
Definition NimBLEAttValue.h:176
bool operator!=(const NimBLEAttValue &source) const
Inequality operator.
Definition NimBLEAttValue.h:389