NimBLEAddress::toString allow formatting to be configured

This commit is contained in:
thekurtovic 2025-01-06 14:13:02 -05:00 committed by Ryan Powell
parent 0b543d9f62
commit 1088ad8fe1
2 changed files with 32 additions and 6 deletions

14
Kconfig
View file

@ -157,6 +157,20 @@ config NIMBLE_CPP_ENABLE_ADVERTISEMENT_TYPE_TEXT
while scanning as text messages in the debug log.
This will use approximately 250 bytes of flash memory.
config NIMBLE_CPP_ADDR_FMT_INCLUDE_DELIMITER
bool "Show colon characters when printing address."
default "y"
help
Enabling this option will format MAC addresses with
colon characters included when printing.
config NIMBLE_CPP_ADDR_FMT_UPPERCASE
bool "Use uppercase letters when printing address."
default "n"
help
Enabling this option will format MAC addresses in
uppercase letters when printing.
config NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
bool "Enable timestamps to be stored with attribute values."
default "n"

View file

@ -23,6 +23,18 @@
# include <algorithm>
# ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_INCLUDE_DELIMITER
# define NIMBLE_CPP_ADDR_DELIMITER ":"
# else
# define NIMBLE_CPP_ADDR_DELIMITER ""
# endif
# ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE
# define NIMBLE_CPP_ADDR_FMT "%02X%s%02X%s%02X%s%02X%s%02X%s%02X"
# else
# define NIMBLE_CPP_ADDR_FMT "%02x%s%02x%s%02x%s%02x%s%02x%s%02x"
# endif
static const char* LOG_TAG = "NimBLEAddress";
/*************************************************
@ -211,12 +223,12 @@ NimBLEAddress::operator std::string() const {
char buffer[18];
snprintf(buffer,
sizeof(buffer),
"%02x:%02x:%02x:%02x:%02x:%02x",
this->val[5],
this->val[4],
this->val[3],
this->val[2],
this->val[1],
NIMBLE_CPP_ADDR_FMT,
this->val[5], NIMBLE_CPP_ADDR_DELIMITER,
this->val[4], NIMBLE_CPP_ADDR_DELIMITER,
this->val[3], NIMBLE_CPP_ADDR_DELIMITER,
this->val[2], NIMBLE_CPP_ADDR_DELIMITER,
this->val[1], NIMBLE_CPP_ADDR_DELIMITER,
this->val[0]);
return std::string{buffer};
} // operator std::string