Bluetooth: Convert string helpers to use log_strdup
This reduces the multiple buffer requirement to a single buffer.
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
diff --git a/subsys/bluetooth/common/CMakeLists.txt b/subsys/bluetooth/common/CMakeLists.txt
index 50d1afe..b0191da 100644
--- a/subsys/bluetooth/common/CMakeLists.txt
+++ b/subsys/bluetooth/common/CMakeLists.txt
@@ -1,8 +1,8 @@
zephyr_library()
zephyr_library_sources(dummy.c)
+zephyr_library_sources(log.c)
-zephyr_library_sources_ifdef(CONFIG_BT_DEBUG log.c)
zephyr_library_sources_ifdef(CONFIG_BT_RPA rpa.c)
zephyr_library_link_libraries(subsys__bluetooth)
diff --git a/subsys/bluetooth/common/log.c b/subsys/bluetooth/common/log.c
index 83f939a..33ecdde 100644
--- a/subsys/bluetooth/common/log.c
+++ b/subsys/bluetooth/common/log.c
@@ -19,23 +19,14 @@
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
-#if defined(CONFIG_BT_DEBUG)
-const char *bt_hex(const void *buf, size_t len)
+const char *bt_hex_real(const void *buf, size_t len)
{
static const char hex[] = "0123456789abcdef";
- static char hexbufs[4][129];
- static u8_t curbuf;
+ static char str[129];
const u8_t *b = buf;
- unsigned int mask;
- char *str;
int i;
- mask = irq_lock();
- str = hexbufs[curbuf++];
- curbuf %= ARRAY_SIZE(hexbufs);
- irq_unlock(mask);
-
- len = min(len, (sizeof(hexbufs[0]) - 1) / 2);
+ len = min(len, (sizeof(hex) - 1) / 2);
for (i = 0; i < len; i++) {
str[i * 2] = hex[b[i] >> 4];
@@ -47,38 +38,20 @@
return str;
}
-const char *bt_addr_str(const bt_addr_t *addr)
+const char *bt_addr_str_real(const bt_addr_t *addr)
{
- static char bufs[4][BT_ADDR_STR_LEN];
- unsigned int mask;
- static u8_t cur;
- char *str;
+ static char str[BT_ADDR_STR_LEN];
- mask = irq_lock();
- str = bufs[cur++];
- cur %= ARRAY_SIZE(bufs);
- irq_unlock(mask);
-
- bt_addr_to_str(addr, str, sizeof(bufs[cur]));
+ bt_addr_to_str(addr, str, sizeof(str));
return str;
}
-const char *bt_addr_le_str(const bt_addr_le_t *addr)
+const char *bt_addr_le_str_real(const bt_addr_le_t *addr)
{
- static char bufs[8][BT_ADDR_LE_STR_LEN];
- unsigned int mask;
- static u8_t cur;
- char *str;
+ static char str[BT_ADDR_LE_STR_LEN];
- mask = irq_lock();
- str = bufs[cur++];
- cur %= ARRAY_SIZE(bufs);
- irq_unlock(mask);
-
- bt_addr_le_to_str(addr, str, sizeof(bufs[cur]));
+ bt_addr_le_to_str(addr, str, sizeof(str));
return str;
}
-#endif /* CONFIG_BT_DEBUG */
-
diff --git a/subsys/bluetooth/common/log.h b/subsys/bluetooth/common/log.h
index e72e92e..e557f3a 100644
--- a/subsys/bluetooth/common/log.h
+++ b/subsys/bluetooth/common/log.h
@@ -92,15 +92,13 @@
k_oops(); \
}
-/* This helper is only available when BT_DEBUG is enabled */
-const char *bt_hex(const void *buf, size_t len);
+const char *bt_hex_real(const void *buf, size_t len);
+const char *bt_addr_str_real(const bt_addr_t *addr);
+const char *bt_addr_le_str_real(const bt_addr_le_t *addr);
-/* These helpers are only safe to be called from internal threads as they're
- * not multi-threading safe
- */
-const char *bt_addr_str(const bt_addr_t *addr);
-const char *bt_addr_le_str(const bt_addr_le_t *addr);
-
+#define bt_hex(buf, len) log_strdup(bt_hex_real(buf, len))
+#define bt_addr_str(addr) log_strdup(bt_addr_str_real(addr))
+#define bt_addr_le_str(addr) log_strdup(bt_addr_le_str_real(addr))
#ifdef __cplusplus
}