From d0e0af74da393a78ae9f544ad0443468ffda02e7 Mon Sep 17 00:00:00 2001 From: Magdalena Kasenberg Date: Sat, 17 Apr 2021 13:33:20 +0200 Subject: [PATCH] Bluetooth: Add option to log btsnoops over RTT There is a choice to log btmon logs over UART or RTT. You can choose number of RTT buffer, set its name and size. Replaced CONFIG_BT_DEBUG_MONITOR with CONFIG_BT_DEBUG_MONITOR_UART for UART usage and CONFIG_BT_DEBUG_MONITOR_RTT for RTT. Signed-off-by: Magdalena Kasenberg --- doc/guides/bluetooth/bluetooth-dev.rst | 4 +- samples/bluetooth/mesh/microbit_gatt.conf | 2 +- samples/bluetooth/mesh/nrf51_qfaa.conf | 2 +- samples/boards/nrf/mesh/onoff-app/prj.conf | 2 +- .../onoff_level_lighting_vnd_app/prj.conf | 2 +- soc/x86/atom/Kconfig.defconfig | 2 +- subsys/bluetooth/common/Kconfig | 48 +++++++++++++++++-- subsys/bluetooth/host/CMakeLists.txt | 2 +- subsys/bluetooth/host/monitor.c | 29 ++++++++++- subsys/bluetooth/host/monitor.h | 4 +- tests/bluetooth/init/prj_20.conf | 3 +- tests/bluetooth/init/prj_21.conf | 3 +- tests/bluetooth/init/prj_ctlr_dbg.conf | 2 +- tests/bluetooth/mesh/dbg.conf | 2 +- tests/bluetooth/mesh/ext_adv.conf | 2 +- tests/bluetooth/mesh/friend.conf | 2 +- tests/bluetooth/mesh/gatt.conf | 2 +- tests/bluetooth/mesh/lpn.conf | 2 +- tests/bluetooth/mesh/microbit_gatt.conf | 2 +- tests/bluetooth/mesh/nrf51-16k.conf | 2 +- tests/bluetooth/mesh/pb_gatt.conf | 2 +- tests/bluetooth/mesh/prj.conf | 2 +- tests/bluetooth/mesh/proxy.conf | 2 +- 23 files changed, 95 insertions(+), 30 deletions(-) diff --git a/doc/guides/bluetooth/bluetooth-dev.rst b/doc/guides/bluetooth/bluetooth-dev.rst index 3299f1045bb..fd5294bb574 100644 --- a/doc/guides/bluetooth/bluetooth-dev.rst +++ b/doc/guides/bluetooth/bluetooth-dev.rst @@ -57,10 +57,10 @@ application: .. code-block:: console - CONFIG_BT_DEBUG_MONITOR=y + CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_UART_CONSOLE=n -Setting :option:`CONFIG_BT_DEBUG_MONITOR` to ``y`` replaces the +Setting :option:`CONFIG_BT_DEBUG_MONITOR_UART` to ``y`` replaces the :option:`CONFIG_BT_DEBUG_LOG` option, and setting :option:`CONFIG_UART_CONSOLE` to ``n`` disables the default ``printk``/``printf`` hooks. diff --git a/samples/bluetooth/mesh/microbit_gatt.conf b/samples/bluetooth/mesh/microbit_gatt.conf index 034fdefd54c..96c6ba97e77 100644 --- a/samples/bluetooth/mesh/microbit_gatt.conf +++ b/samples/bluetooth/mesh/microbit_gatt.conf @@ -49,7 +49,7 @@ CONFIG_BT_MESH_MODEL_GROUP_COUNT=1 CONFIG_BT_MESH_LABEL_COUNT=0 #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/samples/bluetooth/mesh/nrf51_qfaa.conf b/samples/bluetooth/mesh/nrf51_qfaa.conf index 2b4d238101a..031eef45080 100644 --- a/samples/bluetooth/mesh/nrf51_qfaa.conf +++ b/samples/bluetooth/mesh/nrf51_qfaa.conf @@ -32,7 +32,7 @@ CONFIG_BT_MESH_MODEL_GROUP_COUNT=1 CONFIG_BT_MESH_LABEL_COUNT=0 #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_SELF_TEST=y diff --git a/samples/boards/nrf/mesh/onoff-app/prj.conf b/samples/boards/nrf/mesh/onoff-app/prj.conf index 0edf96d7b3a..828e22444a3 100644 --- a/samples/boards/nrf/mesh/onoff-app/prj.conf +++ b/samples/boards/nrf/mesh/onoff-app/prj.conf @@ -64,7 +64,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_UART_CONSOLE=y # this outputs btmon formatted data to the serial port -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_LOG=y diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/prj.conf b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/prj.conf index 578a297912a..81f647e11a3 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/prj.conf +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/prj.conf @@ -60,7 +60,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 #CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y ##CONFIG_BT_MESH_DEBUG=y diff --git a/soc/x86/atom/Kconfig.defconfig b/soc/x86/atom/Kconfig.defconfig index 6b2b61a00d0..fcfa887ed83 100644 --- a/soc/x86/atom/Kconfig.defconfig +++ b/soc/x86/atom/Kconfig.defconfig @@ -18,7 +18,7 @@ config UART_PIPE_ON_DEV_NAME depends on UART_PIPE config BT_MONITOR_ON_DEV_NAME - default "UART_1" if BT_DEBUG_MONITOR + default "UART_1" if BT_DEBUG_MONITOR_UART endif diff --git a/subsys/bluetooth/common/Kconfig b/subsys/bluetooth/common/Kconfig index 66631d0a0b5..2b51293047a 100644 --- a/subsys/bluetooth/common/Kconfig +++ b/subsys/bluetooth/common/Kconfig @@ -84,6 +84,9 @@ config BT_DEBUG # Hidden option to make the conditions more intuitive bool +config BT_MONITOR + bool + choice prompt "Bluetooth debug type" default BT_DEBUG_NONE @@ -101,11 +104,12 @@ config BT_DEBUG_LOG This option enables Bluetooth debug going to standard serial console. -config BT_DEBUG_MONITOR +config BT_DEBUG_MONITOR_UART bool "Monitor protocol over UART" select BT_DEBUG select LOG select CONSOLE_HAS_DRIVER + select BT_MONITOR help Use a custom logging protocol over the console UART instead of plain-text output. Requires a special application @@ -118,7 +122,45 @@ config BT_DEBUG_MONITOR UART_CONSOLE needs to be disabled (in which case printk/printf will get encoded into the monitor protocol). -endchoice +config BT_DEBUG_MONITOR_RTT + bool "Monitor protocol over RTT" + depends on USE_SEGGER_RTT + depends on SEGGER_RTT_MAX_NUM_UP_BUFFERS >= 2 + select BT_DEBUG + select BT_MONITOR + help + Use a custom logging protocol over the RTT buffer instead of + plain-text output. Requires a special application + on the host side that can decode this protocol. Currently + the 'btmon' tool from BlueZ is capable of doing this. + +if BT_DEBUG_MONITOR_RTT + +config BT_DEBUG_MONITOR_RTT_BUFFER + int "Buffer number used for custom logger output." + range 1 SEGGER_RTT_MAX_NUM_UP_BUFFERS + default 1 + help + Select index of up-buffer used for logger output. + Make sure the buffer number is not used by other logger, + e.g. in LOG_BACKEND_RTT_BUFFER, otherwise the buffer will be + overwritten. + +config BT_DEBUG_MONITOR_RTT_BUFFER_NAME + string "Buffer name used for custom logger output." + default "btmonitor" + help + Select index of up-buffer used for logger output. + +config BT_DEBUG_MONITOR_RTT_BUFFER_SIZE + int "Size of reserved up-buffer for custom logger output." + default 1024 + help + Specify reserved size of up-buffer used for custom logger output. + +endif # BT_DEBUG_MONITOR_RTT + +endchoice # Bluetooth debug type if BT_DEBUG @@ -127,7 +169,7 @@ DT_CHOSEN_Z_BT_MON_UART := zephyr,bt-mon-uart config BT_MONITOR_ON_DEV_NAME string "Device Name of Bluetooth monitor logging UART" - depends on BT_DEBUG_MONITOR + depends on BT_DEBUG_MONITOR_UART default "$(dt_chosen_label,$(DT_CHOSEN_Z_BT_MON_UART))" if HAS_DTS default "UART_0" help diff --git a/subsys/bluetooth/host/CMakeLists.txt b/subsys/bluetooth/host/CMakeLists.txt index 7063f6a8151..03a60f08bd3 100644 --- a/subsys/bluetooth/host/CMakeLists.txt +++ b/subsys/bluetooth/host/CMakeLists.txt @@ -4,7 +4,7 @@ zephyr_library() zephyr_library_link_libraries(subsys__bluetooth) zephyr_library_sources_ifdef(CONFIG_BT_HCI_RAW hci_raw.c hci_common.c) -zephyr_library_sources_ifdef(CONFIG_BT_DEBUG_MONITOR monitor.c) +zephyr_library_sources_ifdef(CONFIG_BT_MONITOR monitor.c) zephyr_library_sources_ifdef(CONFIG_BT_TINYCRYPT_ECC hci_ecc.c) zephyr_library_sources_ifdef(CONFIG_BT_A2DP a2dp.c) zephyr_library_sources_ifdef(CONFIG_BT_AVDTP avdtp.c) diff --git a/subsys/bluetooth/host/monitor.c b/subsys/bluetooth/host/monitor.c index acb1c7e3136..dbf2d94179a 100644 --- a/subsys/bluetooth/host/monitor.c +++ b/subsys/bluetooth/host/monitor.c @@ -27,6 +27,16 @@ #include "monitor.h" +#ifdef CONFIG_BT_DEBUG_MONITOR_RTT +#include + +#define RTT_BUFFER_NAME CONFIG_BT_DEBUG_MONITOR_RTT_BUFFER_NAME +#define RTT_BUF_SIZE CONFIG_BT_DEBUG_MONITOR_RTT_BUFFER_SIZE +static uint8_t rtt_buf[RTT_BUF_SIZE]; +#elif CONFIG_BT_DEBUG_MONITOR_UART +static const struct device *monitor_dev; +#endif + /* This is the same default priority as for other console handlers, * except that we're not exporting it as a Kconfig variable until a * clear need arises. @@ -45,8 +55,6 @@ /* Maximum (string) length of a log message */ #define MONITOR_MSG_MAX 128 -static const struct device *monitor_dev; - enum { BT_LOG_BUSY, BT_CONSOLE_BUSY, @@ -68,11 +76,15 @@ static struct { static void monitor_send(const void *data, size_t len) { +#ifdef CONFIG_BT_DEBUG_MONITOR_RTT + SEGGER_RTT_Write(CONFIG_BT_DEBUG_MONITOR_RTT_BUFFER, data, len); +#elif CONFIG_BT_DEBUG_MONITOR_UART const uint8_t *buf = data; while (len--) { uart_poll_out(monitor_dev, *buf++); } +#endif } static void encode_drops(struct bt_monitor_hdr *hdr, uint8_t type, @@ -179,6 +191,18 @@ void bt_monitor_new_index(uint8_t type, uint8_t bus, bt_addr_t *addr, bt_monitor_send(BT_MONITOR_NEW_INDEX, &pkt, sizeof(pkt)); } +#ifdef CONFIG_BT_DEBUG_MONITOR_RTT +static int bt_monitor_init(const struct device *d) +{ + ARG_UNUSED(d); + + SEGGER_RTT_ConfigUpBuffer(CONFIG_BT_DEBUG_MONITOR_RTT_BUFFER, + RTT_BUFFER_NAME, rtt_buf, RTT_BUF_SIZE, + SEGGER_RTT_MODE_NO_BLOCK_SKIP); + return 0; +} +#elif CONFIG_BT_DEBUG_MONITOR_UART + #if !defined(CONFIG_UART_CONSOLE) && !defined(CONFIG_LOG_PRINTK) static int monitor_console_out(int c) { @@ -339,5 +363,6 @@ static int bt_monitor_init(const struct device *d) return 0; } +#endif /* CONFIG_BT_DEBUG_MONITOR_UART */ SYS_INIT(bt_monitor_init, PRE_KERNEL_1, MONITOR_INIT_PRIORITY); diff --git a/subsys/bluetooth/host/monitor.h b/subsys/bluetooth/host/monitor.h index a7a3751c8eb..30e4869cf99 100644 --- a/subsys/bluetooth/host/monitor.h +++ b/subsys/bluetooth/host/monitor.h @@ -8,7 +8,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#if defined(CONFIG_BT_DEBUG_MONITOR) +#if defined(CONFIG_BT_MONITOR) #define BT_MONITOR_NEW_INDEX 0 #define BT_MONITOR_DEL_INDEX 1 @@ -100,7 +100,7 @@ void bt_monitor_send(uint16_t opcode, const void *data, size_t len); void bt_monitor_new_index(uint8_t type, uint8_t bus, bt_addr_t *addr, const char *name); -#else /* !CONFIG_BT_DEBUG_MONITOR */ +#else /* !CONFIG_BT_MONITOR */ #define bt_monitor_send(opcode, data, len) #define bt_monitor_new_index(type, bus, addr, name) diff --git a/tests/bluetooth/init/prj_20.conf b/tests/bluetooth/init/prj_20.conf index c0d6ca845e7..73f75688aca 100644 --- a/tests/bluetooth/init/prj_20.conf +++ b/tests/bluetooth/init/prj_20.conf @@ -8,7 +8,7 @@ CONFIG_BT_TINYCRYPT_ECC=y CONFIG_BT_USE_DEBUG_KEYS=y CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y CONFIG_BT_GATT_CLIENT=y -CONFIG_BT_DEBUG_MONITOR=y +CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_UART_CONSOLE=n CONFIG_BT_DEBUG_HCI_CORE=y CONFIG_BT_DEBUG_CONN=y @@ -27,4 +27,3 @@ CONFIG_BT_A2DP=y CONFIG_BT_HFP_HF=y CONFIG_BT_DEBUG_HFP_HF=y CONFIG_ZTEST=y - diff --git a/tests/bluetooth/init/prj_21.conf b/tests/bluetooth/init/prj_21.conf index d6b506fae2d..6d0649a0d1c 100644 --- a/tests/bluetooth/init/prj_21.conf +++ b/tests/bluetooth/init/prj_21.conf @@ -8,7 +8,7 @@ CONFIG_BT_TINYCRYPT_ECC=y CONFIG_BT_USE_DEBUG_KEYS=y CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y CONFIG_BT_GATT_CLIENT=y -CONFIG_BT_DEBUG_MONITOR=y +CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_HCI_CORE=y CONFIG_BT_DEBUG_CONN=y CONFIG_BT_DEBUG_KEYS=y @@ -20,4 +20,3 @@ CONFIG_BT_DEBUG_ATT=y CONFIG_BT_DEBUG_GATT=y CONFIG_BT_BREDR=y CONFIG_ZTEST=y - diff --git a/tests/bluetooth/init/prj_ctlr_dbg.conf b/tests/bluetooth/init/prj_ctlr_dbg.conf index f17d49b325e..9229149c6a8 100644 --- a/tests/bluetooth/init/prj_ctlr_dbg.conf +++ b/tests/bluetooth/init/prj_ctlr_dbg.conf @@ -46,7 +46,7 @@ CONFIG_BT_TINYCRYPT_ECC=y CONFIG_BT_USE_DEBUG_KEYS=y CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y CONFIG_BT_GATT_CLIENT=y -CONFIG_BT_DEBUG_MONITOR=y +CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_HCI_CORE=y CONFIG_BT_DEBUG_CONN=y CONFIG_BT_DEBUG_KEYS=y diff --git a/tests/bluetooth/mesh/dbg.conf b/tests/bluetooth/mesh/dbg.conf index 19f44173020..0adeaea82e4 100644 --- a/tests/bluetooth/mesh/dbg.conf +++ b/tests/bluetooth/mesh/dbg.conf @@ -43,7 +43,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_LOG=y CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/ext_adv.conf b/tests/bluetooth/mesh/ext_adv.conf index 941152f4753..4d87fd75c3d 100644 --- a/tests/bluetooth/mesh/ext_adv.conf +++ b/tests/bluetooth/mesh/ext_adv.conf @@ -44,7 +44,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_LOG=y CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/friend.conf b/tests/bluetooth/mesh/friend.conf index da39b203b74..f685cdb570b 100644 --- a/tests/bluetooth/mesh/friend.conf +++ b/tests/bluetooth/mesh/friend.conf @@ -38,7 +38,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 #CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/gatt.conf b/tests/bluetooth/mesh/gatt.conf index 5120ff7a86a..85e769747fa 100644 --- a/tests/bluetooth/mesh/gatt.conf +++ b/tests/bluetooth/mesh/gatt.conf @@ -43,7 +43,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 #CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/lpn.conf b/tests/bluetooth/mesh/lpn.conf index 5fe4922be7e..11ad46a3f2d 100644 --- a/tests/bluetooth/mesh/lpn.conf +++ b/tests/bluetooth/mesh/lpn.conf @@ -43,7 +43,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_LOG=y CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/microbit_gatt.conf b/tests/bluetooth/mesh/microbit_gatt.conf index e34965d8042..88dcd82399c 100644 --- a/tests/bluetooth/mesh/microbit_gatt.conf +++ b/tests/bluetooth/mesh/microbit_gatt.conf @@ -45,7 +45,7 @@ CONFIG_BT_MESH_MODEL_GROUP_COUNT=1 CONFIG_BT_MESH_LABEL_COUNT=0 #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/nrf51-16k.conf b/tests/bluetooth/mesh/nrf51-16k.conf index b469d8b419f..fa991836b7c 100644 --- a/tests/bluetooth/mesh/nrf51-16k.conf +++ b/tests/bluetooth/mesh/nrf51-16k.conf @@ -24,7 +24,7 @@ CONFIG_BT_MESH_MODEL_GROUP_COUNT=2 CONFIG_BT_MESH_LABEL_COUNT=0 #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/pb_gatt.conf b/tests/bluetooth/mesh/pb_gatt.conf index bf15568d04b..8bbffb7eac8 100644 --- a/tests/bluetooth/mesh/pb_gatt.conf +++ b/tests/bluetooth/mesh/pb_gatt.conf @@ -43,7 +43,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 #CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/prj.conf b/tests/bluetooth/mesh/prj.conf index f8a3d20df87..179cc5d2222 100644 --- a/tests/bluetooth/mesh/prj.conf +++ b/tests/bluetooth/mesh/prj.conf @@ -45,7 +45,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y CONFIG_BT_DEBUG_LOG=y CONFIG_BT_MESH_DEBUG=y diff --git a/tests/bluetooth/mesh/proxy.conf b/tests/bluetooth/mesh/proxy.conf index 06992610af8..346d17238a8 100644 --- a/tests/bluetooth/mesh/proxy.conf +++ b/tests/bluetooth/mesh/proxy.conf @@ -43,7 +43,7 @@ CONFIG_BT_MESH_LABEL_COUNT=3 CONFIG_BT_MESH_IV_UPDATE_TEST=y #CONFIG_UART_CONSOLE=n -#CONFIG_BT_DEBUG_MONITOR=y +#CONFIG_BT_DEBUG_MONITOR_UART=y #CONFIG_BT_DEBUG_LOG=y #CONFIG_BT_MESH_DEBUG=y