esp-nimble-cpp  1.4.0
NimBLEAttValue Class Reference

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

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. More...
 
 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. More...
 
 NimBLEAttValue (std::initializer_list< uint8_t > list, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initializer list. More...
 
 NimBLEAttValue (const char *value, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initial value from a const char string. More...
 
 NimBLEAttValue (const std::string str, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
 Construct with an initial value from a std::string. More...
 
 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>. More...
 
 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. More...
 
bool setValue (const char *s)
 Set value to the value of const char*. More...
 
const uint8_t * getValue (time_t *timestamp)
 Get a pointer to the value buffer with timestamp. More...
 
NimBLEAttValueappend (const uint8_t *value, uint16_t len)
 Append data to the value. More...
 
template<typename T >
bool setValue (const T &s)
 Template to set value to the value of <type>val. More...
 
template<typename T >
bool setValue (const T &s)
 Template to set value to the value of <type>val. More...
 
template<typename T >
getValue (time_t *timestamp=nullptr, bool skipSizeCheck=false)
 Template to return the value as a <type>. More...
 
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)
 Equality operator.
 
bool operator!= (const NimBLEAttValue &source)
 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 
)
inline

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 
)
inline

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 ( 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() [4/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() [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 
)
inline

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() [1/2]

const uint8_t * NimBLEAttValue::getValue ( time_t *  timestamp)
inline

Get a pointer to the value buffer with timestamp.

Parameters
[in]timestampA ponter to a time_t variable to store the timestamp.
Returns
A pointer to the internal value buffer.

◆ getValue() [2/2]

template<typename T >
T NimBLEAttValue::getValue ( time_t *  timestamp = nullptr,
bool  skipSizeCheck = false 
)
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/4]

bool NimBLEAttValue::setValue ( const char *  s)
inline

Set value to the value of const char*.

Parameters
[in]sA ponter to a const char value to set.

◆ setValue() [2/4]

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 for types without a c_str() method.

◆ setValue() [3/4]

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() [4/4]

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

Set the value from a buffer.

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