18#ifndef NIMBLE_CPP_ATTVALUE_H
19#define NIMBLE_CPP_ATTVALUE_H
24# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
34# ifndef CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
35# define CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED 0
38# ifndef BLE_ATT_ATTR_MAX_LEN
39# define BLE_ATT_ATTR_MAX_LEN 512
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
51template <
typename T,
typename =
void,
typename =
void>
52struct Has_data_size : std::false_type {};
55struct Has_data_size<T, decltype(void(std::declval<T&>().data())), decltype(void(std::declval<T&>().size()))>
59template <
typename T,
typename =
void,
typename =
void>
60struct Has_c_str_length : std::false_type {};
63struct Has_c_str_length<T, decltype(void(std::declval<T&>().c_str())), decltype(void(std::declval<T&>().length()))>
67template <
typename T,
typename =
void>
68struct Has_value_type : std::false_type {};
71struct Has_value_type<T, decltype(void(sizeof(typename T::value_type)))>
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
96 NimBLEAttValue(uint16_t init_len = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
104 NimBLEAttValue(
const uint8_t* value, uint16_t len, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
112 :
NimBLEAttValue((uint8_t*)value, (uint16_t)strlen(value), max_len) {}
119 NimBLEAttValue(std::initializer_list<uint8_t> list, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
135 NimBLEAttValue(
const std::vector<uint8_t> vec, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
138# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
144 NimBLEAttValue(
const String str, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN)
158 uint16_t
max_size()
const {
return m_attr_max_len; }
164 uint16_t
length()
const {
return m_attr_len; }
167 uint16_t
size()
const {
return m_attr_len; }
170 const uint8_t*
data()
const {
return m_attr_value; }
173 const char*
c_str()
const {
return reinterpret_cast<const char*
>(m_attr_value); }
176 const uint8_t*
begin()
const {
return m_attr_value; }
179 const uint8_t*
end()
const {
return m_attr_value + m_attr_len; }
181# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
183 time_t getTimeStamp()
const {
return m_timestamp; }
186 void setTimeStamp() { m_timestamp = time(
nullptr); }
192 void setTimeStamp(time_t t) { m_timestamp = t; }
194 time_t getTimeStamp()
const {
return 0; }
195 void setTimeStamp() {}
196 void setTimeStamp(time_t t) {}
205 bool setValue(
const uint8_t* value, uint16_t len);
216 return setValue(
reinterpret_cast<const uint8_t*
>(s), len);
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;
240# if __cplusplus < 201703L
247 template <
typename T>
251 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
254 return setValue(
reinterpret_cast<const uint8_t*
>(&v),
sizeof(T));
262 template <
typename T>
266 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
269 return setValue(
reinterpret_cast<const uint8_t*
>(s.c_str()), s.length());
278 template <
typename T>
282 typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value,
bool>::type
286 reinterpret_cast<const uint8_t*
>(v.data()),
287 v.size() *
sizeof(
typename T::value_type)
296 template <
typename T>
300 typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value,
bool>::type
303 return setValue(
reinterpret_cast<const uint8_t*
>(v.data()), v.size());
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));
318 return setValue(
reinterpret_cast<const uint8_t*
>(s.data()), s.size());
320 }
else if constexpr (Has_c_str_length<T>::value) {
321 return setValue(
reinterpret_cast<const uint8_t*
>(s.c_str()), s.length());
323 return setValue(
reinterpret_cast<const uint8_t*
>(&s),
sizeof(s));
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;
348 if (!skipSizeCheck &&
size() <
sizeof(T)) {
351 return *(
reinterpret_cast<const T*
>(m_attr_value));
360 operator std::vector<uint8_t>()
const {
return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); }
363 operator std::string()
const {
return std::string(
reinterpret_cast<char*
>(m_attr_value), m_attr_len); }
366 operator const uint8_t*()
const {
return m_attr_value; }
373 setValue(
reinterpret_cast<const uint8_t*
>(&source[0]), source.size());
385 return (m_attr_len == source.
size()) ? memcmp(m_attr_value, source.
data(), m_attr_len) == 0 :
false;
391# ifdef NIMBLE_CPP_ARDUINO_STRING_AVAILABLE
393 operator String()
const {
return String(
reinterpret_cast<char*
>(m_attr_value)); }
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