SDK  23.9.2
For IoT System Software Development
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
BLEUUID Class Reference

A model of a BLE UUID. More...

#include <BLEUUID.hpp>

Public Member Functions

 BLEUUID (std::string uuid)
 
 BLEUUID (uint16_t uuid)
 Create a UUID from the 16bit value. More...
 
 BLEUUID (uint32_t uuid)
 Create a UUID from the 32bit value. More...
 
 BLEUUID (uint8_t *pData, size_t size, bool msbFirst)
 Create a UUID from 16 bytes of memory. More...
 
uint8_t bitSize ()
 Get the number of bits in this uuid. More...
 
bool equals (BLEUUID uuid)
 Compare a UUID against this UUID. More...
 
BLEUUID to128 ()
 Convert a UUID to its 128 bit representation. More...
 
std::string toString ()
 Get a string representation of the UUID. More...
 
bool operator== (uint16_t other) const
 
bool operator== (uint32_t other) const
 

Static Public Member Functions

static BLEUUID fromString (std::string uuid)
 

Public Attributes

uint16_t len
 
union {
   uint16_t   raw16
 
   uint32_t   raw32
 
   uint8_t   raw128 [16]
 
}; 
 

Detailed Description

A model of a BLE UUID.

Constructor & Destructor Documentation

◆ BLEUUID() [1/3]

BLEUUID::BLEUUID ( uint16_t  uuid)

Create a UUID from the 16bit value.

Parameters
[in]uuidThe 16bit short form UUID.
151  {
152  this->len = 2;
153  this->raw16 = uuid;
154  m_valueSet = true;
155 } // BLEUUID

◆ BLEUUID() [2/3]

BLEUUID::BLEUUID ( uint32_t  uuid)

Create a UUID from the 32bit value.

Parameters
[in]uuidThe 32bit short form UUID.
163  {
164  this->len = 4;
165  this->raw32 = uuid;
166  m_valueSet = true;
167 } // BLEUUID

◆ BLEUUID() [3/3]

BLEUUID::BLEUUID ( uint8_t *  pData,
size_t  size,
bool  msbFirst 
)

Create a UUID from 16 bytes of memory.

Parameters
[in]pDataThe pointer to the start of the UUID.
[in]sizeThe size of the data.
[in]msbFirstIs the MSB first in pData memory?
131  {
132  if (size != 16) {
133  _debug_print(TAG, "ERROR: UUID length not 16 bytes");
134  return;
135  }
136  this->len = 16;
137  if (msbFirst) {
138  memrcpy(this->raw128, pData, 16);
139  } else {
140  memcpy(this->raw128, pData, 16);
141  }
142  m_valueSet = true;
143 } // BLEUUID

Member Function Documentation

◆ bitSize()

uint8_t BLEUUID::bitSize ( )

Get the number of bits in this uuid.

Returns
The number of bits in the UUID. One of 16, 32 or 128.
179  {
180  if (!m_valueSet) return 0;
181  switch(this->len) {
182  case 2:
183  return 16;
184  case 4:
185  return 32;
186  case 16:
187  return 128;
188  default:
189  _debug_print_d(TAG, "Unknown UUID length", len);
190  return 0;
191  } // End of switch
192 } // bitSize

◆ equals()

bool BLEUUID::equals ( BLEUUID  uuid)

Compare a UUID against this UUID.

Parameters
[in]uuidThe UUID to compare against.
Returns
True if the UUIDs are equal and false otherwise.
201  {
202  if (m_valueSet == false || uuid.m_valueSet == false) {
203  return false;
204  }
205 
206  if (uuid.len != len) {
207  return uuid.toString() == toString();
208  }
209 
210  if (uuid.len == 2) {
211  return uuid.raw16 == raw16;
212  }
213 
214  if (uuid.len == 4) {
215  return uuid.raw32 == raw32;
216  }
217 
218  return memcmp(uuid.raw128, raw128, 16) == 0;
219 } // equals

◆ fromString()

BLEUUID BLEUUID::fromString ( std::string  _uuid)
static

Create a BLEUUID from a string of the form: 0xNNNN 0xNNNNNNNN 0x<UUID> NNNN NNNNNNNN <UUID>

231  {
232  uint8_t start = 0;
233  if (strstr(_uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters.
234  start = 2;
235  }
236  uint8_t len = _uuid.length() - start; // Calculate the length of the string we are going to use.
237 
238  if(len == 4) {
239  uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
240  return BLEUUID(x);
241  } else if (len == 8) {
242  uint32_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
243  return BLEUUID(x);
244  } else if (len == 36) {
245  return BLEUUID(_uuid);
246  }
247  return BLEUUID();
248 } // fromString

◆ to128()

BLEUUID BLEUUID::to128 ( )

Convert a UUID to its 128 bit representation.

A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method will convert 16 or 32 bit representations to the full 128bit.

257  {
258  //_debug_print_s(TAG, ">> toFull()", toString().c_str());
259 
260  // If we either don't have a value or are already a 128 bit UUID, nothing further to do.
261  if (!m_valueSet || this->len == 16) {
262  return *this;
263  }
264 
265  // If we are 16 bit or 32 bit, then set the 4 bytes of the variable part of the UUID.
266  if (this->len == 2) {
267  uint16_t temp = this->raw16;
268  this->raw128[15] = 0;
269  this->raw128[14] = 0;
270  this->raw128[13] = (temp >> 8) & 0xff;
271  this->raw128[12] = temp & 0xff;
272 
273  }
274  else if (this->len == 4) {
275  uint32_t temp = this->raw32;
276  this->raw128[15] = (temp >> 24) & 0xff;
277  this->raw128[14] = (temp >> 16) & 0xff;
278  this->raw128[13] = (temp >> 8) & 0xff;
279  this->raw128[12] = temp & 0xff;
280  }
281 
282  // Set the fixed parts of the UUID.
283  this->raw128[11] = 0x00;
284  this->raw128[10] = 0x00;
285 
286  this->raw128[9] = 0x10;
287  this->raw128[8] = 0x00;
288 
289  this->raw128[7] = 0x80;
290  this->raw128[6] = 0x00;
291 
292  this->raw128[5] = 0x00;
293  this->raw128[4] = 0x80;
294  this->raw128[3] = 0x5f;
295  this->raw128[2] = 0x9b;
296  this->raw128[1] = 0x34;
297  this->raw128[0] = 0xfb;
298 
299  this->len = 16;
300  //_debug_print_s(TAG, "<< toFull", toString().c_str());
301  return *this;
302 } // to128

◆ toString()

std::string BLEUUID::toString ( )

Get a string representation of the UUID.

The format of a string is: 01234567 8901 2345 6789 012345678901 0000180d-0000-1000-8000-00805f9b34fb 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

Returns
A string representation of the UUID.
317  {
318  if (!m_valueSet) return "<NULL>"; // If we have no value, nothing to format.
319 
320  // If the UUIDs are 16 or 32 bit, pad correctly.
321  std::stringstream ss;
322 
323  if (this->len == 2) { // If the UUID is 16bit, pad correctly.
324  ss << "0000" <<
325  std::hex <<
326  std::setfill('0') <<
327  std::setw(4) <<
328  this->raw16 <<
329  "-0000-1000-8000-00805f9b34fb";
330  return ss.str(); // Return the string
331  } // End 16bit UUID
332 
333  if (this->len == 4) { // If the UUID is 32bit, pad correctly.
334  ss << std::hex <<
335  std::setfill('0') <<
336  std::setw(8) <<
337  this->raw32 <<
338  "-0000-1000-8000-00805f9b34fb";
339  return ss.str(); // return the string
340  } // End 32bit UUID
341 
342  // The UUID is not 16bit or 32bit which means that it is 128bit.
343  //
344  // UUID string format:
345  // AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
346  //
347  ss << std::hex << std::setfill('0') <<
348  std::setw(2) << (int)raw128[15] <<
349  std::setw(2) << (int)raw128[14] <<
350  std::setw(2) << (int)raw128[13] <<
351  std::setw(2) << (int)raw128[12] << "-" <<
352  std::setw(2) << (int)raw128[11] <<
353  std::setw(2) << (int)raw128[10] << "-" <<
354  std::setw(2) << (int)raw128[9] <<
355  std::setw(2) << (int)raw128[8] << "-" <<
356  std::setw(2) << (int)raw128[7] <<
357  std::setw(2) << (int)raw128[6] << "-" <<
358  std::setw(2) << (int)raw128[5] <<
359  std::setw(2) << (int)raw128[4] <<
360  std::setw(2) << (int)raw128[3] <<
361  std::setw(2) << (int)raw128[2] <<
362  std::setw(2) << (int)raw128[1] <<
363  std::setw(2) << (int)raw128[0];
364  return ss.str();
365 } // toString

The documentation for this class was generated from the following files:
BLEUUID::toString
std::string toString()
Get a string representation of the UUID.
Definition: BLEUUID.cpp:317
BLEUUID
A model of a BLE UUID.
Definition: BLEUUID.hpp:41