NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
NimBLEAttValue Class Reference

A specialized container class to hold BLE attribute values. More...

#include <NimBLEAttValue.h>

Public Member Functions

 NimBLEAttValue (uint16_t init_len=CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Default constructor.
 
 NimBLEAttValue (const uint8_t *value, uint16_t len, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initial value from a buffer.
 
 NimBLEAttValue (const char *value, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initial value from a const char string.
 
 NimBLEAttValue (std::initializer_list< uint8_t > list, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initializer list.
 
 NimBLEAttValue (const std::string str, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initial value from a std::string.
 
 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>.
 
 NimBLEAttValue (const NimBLEAttValue &source)
 Copy constructor.
 
 NimBLEAttValue (NimBLEAttValue &&source)
 Move constructor.
 
 ~NimBLEAttValue ()
 Destructor.
 
uint16_t max_size () const
 Returns the max size in bytes.
 
uint16_t capacity () const
 Returns the currently allocated capacity in bytes.
 
uint16_t length () const
 Returns the current length of the value in bytes.
 
uint16_t size () const
 Returns the current size of the value in bytes.
 
const uint8_t * data () const
 Returns a pointer to the internal buffer of the value.
 
const char * c_str () const
 Returns a pointer to the internal buffer of the value as a const char*.
 
const uint8_t * begin () const
 Iterator begin.
 
const uint8_t * end () const
 Iterator end.
 
bool setValue (const uint8_t *value, uint16_t len)
 Set the value from a buffer.
 
bool setValue (const char *s, uint16_t len=0)
 Set value to the value of const char*.
 
NimBLEAttValueappend (const uint8_t *value, uint16_t len)
 Append data to the value.
 
template<typename T >
bool setValue (const T &v)
 Template to set value to the value of <type>val.
 
template<typename T >
bool setValue (const T &s)
 Template to set value to the value of <type>val.
 
template<typename T >
bool setValue (const T &v)
 Template to set value to the value of <type>val.
 
template<typename T >
getValue (time_t *timestamp=nullptr, bool skipSizeCheck=false) const
 Template to return the value as a <type>.
 
uint8_t operator[] (int pos) const
 Subscript operator.
 
 operator std::vector< uint8_t > () const
 Operator; Get the value as a std::vector<uint8_t>.
 
 operator std::string () const
 Operator; Get the value as a std::string.
 
 operator const uint8_t * () const
 Operator; Get the value as a const uint8_t*.
 
NimBLEAttValueoperator+= (const NimBLEAttValue &source)
 Operator; Append another NimBLEAttValue.
 
NimBLEAttValueoperator= (const std::string &source)
 Operator; Set the value from a std::string source.
 
NimBLEAttValueoperator= (NimBLEAttValue &&source)
 Move assignment operator.
 
NimBLEAttValueoperator= (const NimBLEAttValue &source)
 Copy assignment operator.
 
bool operator== (const NimBLEAttValue &source) const
 Equality operator.
 
bool operator!= (const NimBLEAttValue &source) const
 Inequality operator.
 

Detailed Description

A specialized container class to hold BLE attribute values.

This class is designed to be more memory efficient than using
standard container types for value storage, while being convertible to
many different container classes.

Constructor & Destructor Documentation

◆ NimBLEAttValue() [1/6]

NimBLEAttValue::NimBLEAttValue ( uint16_t  init_len = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)

Default constructor.

Parameters
[in]init_lenThe initial size in bytes.
[in]max_lenThe max size in bytes that the value can be.

◆ NimBLEAttValue() [2/6]

NimBLEAttValue::NimBLEAttValue ( const uint8_t *  value,
uint16_t  len,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)

Construct with an initial value from a buffer.

Parameters
valueA pointer to the initial value to set.
[in]lenThe size in bytes of the value to set.
[in]max_lenThe max size in bytes that the value can be.

◆ NimBLEAttValue() [3/6]

NimBLEAttValue::NimBLEAttValue ( const char *  value,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)
inline

Construct with an initial value from a const char string.

Parameters
valueA pointer to the initial value to set.
[in]max_lenThe max size in bytes that the value can be.

◆ NimBLEAttValue() [4/6]

NimBLEAttValue::NimBLEAttValue ( std::initializer_list< uint8_t >  list,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)
inline

Construct with an initializer list.

Parameters
listAn initializer list containing the initial value to set.
[in]max_lenThe max size in bytes that the value can be.

◆ NimBLEAttValue() [5/6]

NimBLEAttValue::NimBLEAttValue ( const std::string  str,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)
inline

Construct with an initial value from a std::string.

Parameters
strA std::string containing to the initial value to set.
[in]max_lenThe max size in bytes that the value can be.

◆ NimBLEAttValue() [6/6]

NimBLEAttValue::NimBLEAttValue ( const std::vector< uint8_t >  vec,
uint16_t  max_len = BLE_ATT_ATTR_MAX_LEN 
)
inline

Construct with an initial value from a std::vector<uint8_t>.

Parameters
vecA std::vector<uint8_t> containing to the initial value to set.
[in]max_lenThe max size in bytes that the value can be.

Member Function Documentation

◆ append()

NimBLEAttValue & NimBLEAttValue::append ( const uint8_t *  value,
uint16_t  len 
)

Append data to the value.

Parameters
[in]valueA ponter to a data buffer with the value to append.
[in]lenThe length of the value to append in bytes.
Returns
A reference to the appended NimBLEAttValue.

◆ getValue()

template<typename T >
T NimBLEAttValue::getValue ( time_t *  timestamp = nullptr,
bool  skipSizeCheck = false 
) const
inline

Template to return the value as a <type>.

Template Parameters
TThe type to convert the data to.
Parameters
[in]timestampA pointer to a time_t struct to store the time the value was read.
[in]skipSizeCheckIf true it will skip checking if the data size is less than
sizeof(<type>).
Returns
The data converted to <type> or NULL if skipSizeCheck is false and the data is
less than sizeof(<type>).

Use: getValue<type>(&timestamp, skipSizeCheck);

◆ setValue() [1/5]

bool NimBLEAttValue::setValue ( const char *  s,
uint16_t  len = 0 
)
inline

Set value to the value of const char*.

Parameters
[in]sA pointer to a const char value to set.
[in]lenThe length of the value in bytes, defaults to strlen(s).

◆ setValue() [2/5]

template<typename T >
bool NimBLEAttValue::setValue ( const T &  s)
inline

Template to set value to the value of <type>val.

Parameters
[in]sThe <type>value to set.

Only used if the <type> has a c_str() method.

◆ setValue() [3/5]

template<typename T >
bool NimBLEAttValue::setValue ( const T &  v)
inline

Template to set value to the value of <type>val.

Parameters
[in]vThe <type>value to set.

Only used for types without a c_str() and length() or data() and size() method. <type> size must be evaluatable by sizeof().

◆ setValue() [4/5]

template<typename T >
bool NimBLEAttValue::setValue ( const T &  v)
inline

Template to set value to the value of <type>val.

Parameters
[in]vThe <type>value to set.

Only used if the <type> has a data() and size() method.

◆ setValue() [5/5]

bool NimBLEAttValue::setValue ( const uint8_t *  value,
uint16_t  len 
)

Set the value from a buffer.

Parameters
[in]valueA pointer to a buffer containing the value.
[in]lenThe length of the value in bytes.
Returns
True if successful.