Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions subsys/bluetooth/host/classic/br.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions subsys/bluetooth/host/classic/br.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2025 Xiaomi Corporation
* Copyright (c) 2017-2021 Nordic Semiconductor ASA
* Copyright (c) 2015-2016 Intel Corporation
*
Expand All @@ -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);
23 changes: 13 additions & 10 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 */
Expand Down Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions subsys/bluetooth/host/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a potential issue that the life of Flash will be reduced. Since there is not any change of device name, I think it is better to write device name to controller directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is to fix board reboot scenario.
I try to simulate the boot scenario here using settings_load to ensure commit_settings takes effect.

When I initiate the bt_set_name method, my log shows that commit_settings is not triggered.
Is this because my environment is not right?

do you means that if the name has been set before, and the same name would set again?
bt_set_name would check and will not take effect if it is set again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I initiate the bt_set_name method, my log shows that commit_settings is not triggered. Is this because my environment is not right?

The commit_settings() would be called when the function settings_load() called.

do you means that if the name has been set before, and the same name would set again?

Uh-huh, that is what your change is doing.

bt_set_name would check and will not take effect if it is set again

In theory I think this can be done. But currently, the new name is not checked in function bt_set_name().

}
#endif
if (!bt_dev.id_count) {
Expand Down
Loading