From 807a055a44e17953396c6c68e9719a3b64316b2e Mon Sep 17 00:00:00 2001 From: v-yichenxi Date: Tue, 23 Dec 2025 17:08:03 +0800 Subject: [PATCH] bluetooth: Adapt log interface to Zephyr stack. bug: 81365 Implement the SAL debug interface to map framework debug types to Zephyr logging modules. This allows dynamic control of log levels for different Bluetooth modules (HCI, L2CAP, SMP, etc.) via the framework's log configuration API. Signed-off-by: v-yichenxi --- service/stacks/zephyr/sal_debug_interface.c | 137 ++++++++++++++++++-- 1 file changed, 129 insertions(+), 8 deletions(-) diff --git a/service/stacks/zephyr/sal_debug_interface.c b/service/stacks/zephyr/sal_debug_interface.c index d8280f2ce..518ed92e9 100644 --- a/service/stacks/zephyr/sal_debug_interface.c +++ b/service/stacks/zephyr/sal_debug_interface.c @@ -14,16 +14,137 @@ * limitations under the License. ***************************************************************************/ #include - +#include +#include "bluetooth.h" #include "bluetooth_define.h" - #include "utils/log.h" +#include "zephyr/logging/log.h" + +struct _debug_type_map { + bt_debug_type_t service_type; + const char *module_name; +}; + +static const struct _debug_type_map g_dbg_type_map[] = { + /* HCI / General Connection / System */ + { BT_DBG_TYPE_HCI, "bt_hci_core" }, + { BT_DBG_TYPE_HCI, "bt_br" }, + { BT_DBG_TYPE_HCI, "bt_conn" }, + { BT_DBG_TYPE_HCI, "bt_driver" }, + { BT_DBG_TYPE_HCI, "bt_id" }, + { BT_DBG_TYPE_HCI, "bt_settings" }, + { BT_DBG_TYPE_HCI, "bt_scan" }, + { BT_DBG_TYPE_HCI, "bt_adv" }, + { BT_DBG_TYPE_HCI, "bt_iso" }, + { BT_DBG_TYPE_HCI, "bt_buf" }, + + /* L2CAP */ + { BT_DBG_TYPE_L2CAP, "bt_l2cap" }, + { BT_DBG_TYPE_L2CAP, "bt_l2cap_br" }, + + /* SDP */ + { BT_DBG_TYPE_SDP, "bt_sdp" }, + + /* ATT / GATT */ + { BT_DBG_TYPE_ATT, "bt_att" }, + { BT_DBG_TYPE_ATT, "bt_gatt" }, + + /* SMP / Security */ + { BT_DBG_TYPE_SMP, "bt_smp" }, + { BT_DBG_TYPE_SMP, "bt_keys" }, + { BT_DBG_TYPE_SMP, "bt_keys_br" }, + { BT_DBG_TYPE_SMP, "bt_ssp" }, + { BT_DBG_TYPE_SMP, "bt_hci_ecc" }, + { BT_DBG_TYPE_SMP, "bt_ecc" }, + + /* RFCOMM */ + { BT_DBG_TYPE_RFCOMM, "bt_rfcomm" }, + + /* Profiles */ + { BT_DBG_TYPE_OBEX, "bt_obex" }, + { BT_DBG_TYPE_AVCTP, "bt_avctp" }, + { BT_DBG_TYPE_AVDTP, "bt_avdtp" }, + { BT_DBG_TYPE_AVRCP, "bt_avrcp" }, + { BT_DBG_TYPE_A2DP, "bt_a2dp" }, + + /* HFP */ + { BT_DBG_TYPE_HFP, "bt_hfp_hf" }, + { BT_DBG_TYPE_HFP, "bt_hfp_ag" }, + { BT_DBG_TYPE_HFP, "bt_sco" }, + + /* HCI Raw / Dump */ + { BT_DBG_TYPE_HCI_RAW, "bt_hci_raw" }, +}; + +static uint32_t support_types(void) +{ + uint32_t mask = 0; + for (size_t i = 0; i < sizeof(g_dbg_type_map) / sizeof(g_dbg_type_map[0]); i++) { + mask |= (1 << g_dbg_type_map[i].service_type); + } + return mask; +} + +bt_status_t bt_sal_debug_update_log_mask(int mask); void bt_sal_debug_init(void) { } void bt_sal_debug_cleanup(void) { } -bt_status_t bt_sal_debug_enable(void) { return BT_STATUS_NOT_SUPPORTED; } -bt_status_t bt_sal_debug_disable(void) { return BT_STATUS_NOT_SUPPORTED; } -bt_status_t bt_sal_debug_set_log_level(uint32_t level) { return BT_STATUS_NOT_SUPPORTED; } -bool bt_sal_debug_is_type_support(bt_debug_type_t type) { return false; } -bt_status_t bt_sal_debug_set_log_enable(bt_debug_type_t type, bool enable) { return BT_STATUS_NOT_SUPPORTED; } -bt_status_t bt_sal_debug_update_log_mask(int mask) { return BT_STATUS_NOT_SUPPORTED; } \ No newline at end of file + +bt_status_t bt_sal_debug_enable(void) +{ + /* This function could enable all logging as a master switch. */ + /* For now, we control logs per type/mask. */ + BT_LOGI("Enable all logging"); + port_log_output_enable(); + return BT_STATUS_SUCCESS; +} + +bt_status_t bt_sal_debug_disable(void) +{ + /* This function could disable all logging. */ + BT_LOGI("Disable all logging"); + port_log_output_disable(); + return BT_STATUS_SUCCESS; +} + +bt_status_t bt_sal_debug_set_log_level(uint32_t level) +{ + return BT_STATUS_NOT_SUPPORTED; +} + +bool bt_sal_debug_is_type_support(bt_debug_type_t type) +{ + BT_LOGI("Is type support %d", type); + return (support_types() & (1 << type)) != 0; +} + +bt_status_t bt_sal_debug_set_log_enable(bt_debug_type_t type, bool enable) +{ + bool found = false; + int new_level = enable ? CONFIG_BT_DEBUG_LOG_LEVEL : LOG_LEVEL_NONE; + if (!bt_sal_debug_is_type_support(type)) { + BT_LOGI("Type %d not supported", type); + return BT_STATUS_NOT_SUPPORTED; + } + + for (size_t i = 0; i < sizeof(g_dbg_type_map) / sizeof(g_dbg_type_map[0]); i++) { + if (g_dbg_type_map[i].service_type == type) { + port_log_module_set_level(g_dbg_type_map[i].module_name, new_level); + found = true; + } + } + + return found ? BT_STATUS_SUCCESS : BT_STATUS_NOT_SUPPORTED; +} + +bt_status_t bt_sal_debug_update_log_mask(int mask) +{ + BT_LOGI("Update log mask to %d", mask); + for (size_t i = 0; i < sizeof(g_dbg_type_map) / sizeof(g_dbg_type_map[0]); i++) { + bool enable = (mask & (1 << g_dbg_type_map[i].service_type)) != 0; + int new_level = enable ? CONFIG_BT_DEBUG_LOG_LEVEL : LOG_LEVEL_NONE; + port_log_module_set_level(g_dbg_type_map[i].module_name, new_level); + } + + return BT_STATUS_SUCCESS; +}