Skip to content

Commit edec56a

Browse files
thekurtovich2zero
authored andcommitted
NimBLEAddress::toString allow formatting to be configured
- Added option CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE If enabled, allows address to be printed in uppercase letters. (Default: 0) - Added option CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER If enabled, allows address to be printed without colon characters included. (Default: 0)
1 parent 74ac317 commit edec56a

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/NimBLEAddress.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323

2424
# include <algorithm>
2525

26+
# ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER
27+
# define NIMBLE_CPP_ADDR_DELIMITER ":"
28+
# else
29+
# define NIMBLE_CPP_ADDR_DELIMITER ""
30+
# endif
31+
32+
# ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE
33+
# define NIMBLE_CPP_ADDR_FMT "%02X%s%02X%s%02X%s%02X%s%02X%s%02X"
34+
# else
35+
# define NIMBLE_CPP_ADDR_FMT "%02x%s%02x%s%02x%s%02x%s%02x%s%02x"
36+
# endif
37+
2638
static const char* LOG_TAG = "NimBLEAddress";
2739

2840
/*************************************************
@@ -211,12 +223,12 @@ NimBLEAddress::operator std::string() const {
211223
char buffer[18];
212224
snprintf(buffer,
213225
sizeof(buffer),
214-
"%02x:%02x:%02x:%02x:%02x:%02x",
215-
this->val[5],
216-
this->val[4],
217-
this->val[3],
218-
this->val[2],
219-
this->val[1],
226+
NIMBLE_CPP_ADDR_FMT,
227+
this->val[5], NIMBLE_CPP_ADDR_DELIMITER,
228+
this->val[4], NIMBLE_CPP_ADDR_DELIMITER,
229+
this->val[3], NIMBLE_CPP_ADDR_DELIMITER,
230+
this->val[2], NIMBLE_CPP_ADDR_DELIMITER,
231+
this->val[1], NIMBLE_CPP_ADDR_DELIMITER,
220232
this->val[0]);
221233
return std::string{buffer};
222234
} // operator std::string

src/nimconfig.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@
155155
*/
156156
// #define CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT 31
157157

158+
/**
159+
* @brief Un-comment to disable showing colon characters when printing address.
160+
*/
161+
// #define CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER 1
162+
163+
/**
164+
* @brief Un-comment to use uppercase letters when printing address.
165+
*/
166+
//#define CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE 1
167+
158168
/**********************************
159169
End Arduino user-config
160170
**********************************/
@@ -361,6 +371,14 @@
361371
#define CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT 31
362372
#endif
363373

374+
#ifndef CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER
375+
#define CONFIG_NIMBLE_CPP_ADDR_FMT_INCLUDE_DELIMITER 0
376+
#endif
377+
378+
#ifndef CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE
379+
#define CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE 0
380+
#endif
381+
364382
#if CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED && !defined NDEBUG
365383
void nimble_cpp_assert(const char *file, unsigned line) __attribute((weak, noreturn));
366384
# define NIMBLE_ATT_VAL_FILE (__builtin_strrchr(__FILE__, '/') ? \

0 commit comments

Comments
 (0)