diff --git a/subsys/bluetooth/host/classic/br.c b/subsys/bluetooth/host/classic/br.c index 23a43a01959b3..53e4dcfe11a45 100644 --- a/subsys/bluetooth/host/classic/br.c +++ b/subsys/bluetooth/host/classic/br.c @@ -13,6 +13,7 @@ #include "common/bt_str.h" +#include "br.h" #include "host/hci_core.h" #include "host/conn_internal.h" #include "host/keys.h" @@ -805,7 +806,6 @@ int bt_br_init(void) struct net_buf *buf; struct bt_hci_cp_write_ssp_mode *ssp_cp; struct bt_hci_cp_write_inquiry_mode *inq_cp; - struct bt_hci_write_local_name *name_cp; struct bt_hci_rp_read_default_link_policy_settings *rp; struct net_buf *rsp; int err; @@ -858,19 +858,15 @@ int bt_br_init(void) } /* Set local name */ - buf = bt_hci_cmd_alloc(K_FOREVER); - if (!buf) { - return -ENOBUFS; - } - - name_cp = net_buf_add(buf, sizeof(*name_cp)); - strncpy((char *)name_cp->local_name, CONFIG_BT_DEVICE_NAME, sizeof(name_cp->local_name)); - - err = bt_hci_cmd_send_sync(BT_HCI_OP_WRITE_LOCAL_NAME, buf, NULL); + err = bt_br_write_local_name(CONFIG_BT_DEVICE_NAME); if (err) { return err; } +#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC) + strncpy(bt_dev.name, CONFIG_BT_DEVICE_NAME, CONFIG_BT_DEVICE_NAME_MAX); +#endif + /* Set Class of device */ buf = bt_hci_cmd_alloc(K_FOREVER); if (!buf) { @@ -1337,3 +1333,19 @@ int bt_br_unpair(const bt_addr_t *addr) return 0; } + +int bt_br_write_local_name(const char *name) +{ + struct net_buf *buf; + struct bt_hci_write_local_name *name_cp; + + buf = bt_hci_cmd_alloc(K_FOREVER); + if (!buf) { + return -ENOBUFS; + } + + name_cp = net_buf_add(buf, sizeof(*name_cp)); + strncpy((char *)name_cp->local_name, name, sizeof(name_cp->local_name)); + + return bt_hci_cmd_send_sync(BT_HCI_OP_WRITE_LOCAL_NAME, buf, NULL); +} diff --git a/subsys/bluetooth/host/classic/br.h b/subsys/bluetooth/host/classic/br.h index 80622aa637d4c..c326b20df56a9 100644 --- a/subsys/bluetooth/host/classic/br.h +++ b/subsys/bluetooth/host/classic/br.h @@ -1,4 +1,5 @@ /* + * Copyright (c) 2025 Xiaomi Corporation * Copyright (c) 2017-2021 Nordic Semiconductor ASA * Copyright (c) 2015-2016 Intel Corporation * @@ -10,3 +11,5 @@ int bt_br_init(void); void bt_br_discovery_reset(void); bool bt_br_update_sec_level(struct bt_conn *conn); + +int bt_br_write_local_name(const char *name); diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 33306596b2ce7..5b83d7076129f 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -48,6 +48,7 @@ #include "addr_internal.h" #include "adv.h" +#include "classic/br.h" #include "common/hci_common_internal.h" #include "common/bt_str.h" #include "common/rpa.h" @@ -66,10 +67,6 @@ #include "settings.h" #include "smp.h" -#if defined(CONFIG_BT_CLASSIC) -#include "classic/br.h" -#endif - #if defined(CONFIG_BT_DF) #include "direction_internal.h" #endif /* CONFIG_BT_DF */ @@ -3888,10 +3885,9 @@ static int le_init(void) return le_set_event_mask(); } -#if !defined(CONFIG_BT_CLASSIC) -static int bt_br_init(void) +static int br_hci_init(void) { -#if defined(CONFIG_BT_CONN) +#if !defined(CONFIG_BT_CLASSIC) && defined(CONFIG_BT_CONN) struct net_buf *rsp; int err; @@ -3907,11 +3903,10 @@ static int bt_br_init(void) read_buffer_size_complete(rsp); net_buf_unref(rsp); -#endif /* CONFIG_BT_CONN */ +#endif /* !CONFIG_BT_CLASSIC && CONFIG_BT_CONN */ return 0; } -#endif /* !defined(CONFIG_BT_CLASSIC) */ static int set_event_mask(void) { @@ -4198,7 +4193,7 @@ static int hci_init(void) } if (BT_FEAT_BREDR(bt_dev.features)) { - err = bt_br_init(); + err = br_hci_init(); if (err) { return err; } @@ -4766,6 +4761,14 @@ int bt_set_name(const char *name) } } + if (IS_ENABLED(CONFIG_BT_CLASSIC)) { + err = bt_br_write_local_name(bt_dev.name); + if (err) { + LOG_WRN("Unable to set local name"); + return err; + } + } + return 0; #else return -ENOMEM; diff --git a/subsys/bluetooth/host/settings.c b/subsys/bluetooth/host/settings.c index 84b4f1eb4b6a8..4a3b2bdb13501 100644 --- a/subsys/bluetooth/host/settings.c +++ b/subsys/bluetooth/host/settings.c @@ -255,8 +255,13 @@ static int commit_settings(void) } #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC) + /* If bt_dev name was empty, then the default name needs to be set. + * Otherwise set the name from settings tree. + */ if (bt_dev.name[0] == '\0') { bt_set_name(CONFIG_BT_DEVICE_NAME); + } else { + bt_set_name(bt_dev.name); } #endif if (!bt_dev.id_count) {