Skip to content

Commit 103829a

Browse files
author
Johan Hedberg
committed
Bluetooth: Fix init procedure handling with BT_SETTINGS
The treatment of the BT_DEV_READY flag was broken when used together with BT_SETTINGS. The flag would get set even though the stack was still in a partially initialized state. Even worse, for central role the stack would potentially try to initiate passive scanning without having an identity address. Refactor the code that sets the BT_DEV_READY flag (among other initialization) into a separate bt_finalize_init() helper function and call it when the settings have been loaded. Also clarify the warning message given to the user in case settings_load() needs to be called. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 5d35b66 commit 103829a

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

subsys/bluetooth/host/hci_core.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,7 +4309,7 @@ static const char *ver_str(u8_t ver)
43094309
return "unknown";
43104310
}
43114311

4312-
void bt_dev_show_info(void)
4312+
static void bt_dev_show_info(void)
43134313
{
43144314
int i;
43154315

@@ -4329,7 +4329,7 @@ void bt_dev_show_info(void)
43294329
bt_dev.lmp_subversion);
43304330
}
43314331
#else
4332-
void bt_dev_show_info(void)
4332+
static inline void bt_dev_show_info(void)
43334333
{
43344334
}
43354335
#endif /* CONFIG_BT_DEBUG */
@@ -4493,8 +4493,6 @@ static int hci_init(void)
44934493
BT_ERR("Unable to set identity address");
44944494
return err;
44954495
}
4496-
4497-
bt_dev_show_info();
44984496
}
44994497

45004498
return 0;
@@ -4614,6 +4612,17 @@ static int irk_init(void)
46144612
}
46154613
#endif /* CONFIG_BT_PRIVACY */
46164614

4615+
void bt_finalize_init(void)
4616+
{
4617+
atomic_set_bit(bt_dev.flags, BT_DEV_READY);
4618+
4619+
if (IS_ENABLED(CONFIG_BT_OBSERVER)) {
4620+
bt_le_scan_update(false);
4621+
}
4622+
4623+
bt_dev_show_info();
4624+
}
4625+
46174626
static int bt_init(void)
46184627
{
46194628
int err;
@@ -4639,18 +4648,16 @@ static int bt_init(void)
46394648
k_delayed_work_init(&bt_dev.rpa_update, rpa_timeout);
46404649
#endif
46414650

4642-
bt_monitor_send(BT_MONITOR_OPEN_INDEX, NULL, 0);
4643-
atomic_set_bit(bt_dev.flags, BT_DEV_READY);
4644-
if (IS_ENABLED(CONFIG_BT_OBSERVER)) {
4645-
bt_le_scan_update(false);
4646-
}
4651+
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
4652+
if (!bt_dev.id_count) {
4653+
BT_WARN("No ID address. App must call settings_load()");
4654+
return 0;
4655+
}
46474656

4648-
if (bt_dev.id_count > 0) {
46494657
atomic_set_bit(bt_dev.flags, BT_DEV_PRESET_ID);
4650-
} else if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
4651-
BT_WARN("No ID address. Expecting one to come from storage.");
46524658
}
46534659

4660+
bt_finalize_init();
46544661
return 0;
46554662
}
46564663

@@ -4753,6 +4760,8 @@ int bt_enable(bt_ready_cb_t cb)
47534760
return err;
47544761
}
47554762

4763+
bt_monitor_send(BT_MONITOR_OPEN_INDEX, NULL, 0);
4764+
47564765
if (!cb) {
47574766
return bt_init();
47584767
}

subsys/bluetooth/host/hci_core.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ void bt_id_add(struct bt_keys *keys);
197197
void bt_id_del(struct bt_keys *keys);
198198

199199
int bt_setup_id_addr(void);
200-
201-
void bt_dev_show_info(void);
200+
void bt_finalize_init(void);
202201

203202
int bt_le_adv_start_internal(const struct bt_le_adv_param *param,
204203
const struct bt_data *ad, size_t ad_len,

subsys/bluetooth/host/settings.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,16 @@ static int commit(void)
231231
}
232232
}
233233

234+
if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
235+
bt_finalize_init();
236+
}
237+
234238
for (h = _bt_settings_start; h < _bt_settings_end; h++) {
235239
if (h->commit) {
236240
h->commit();
237241
}
238242
}
239243

240-
bt_dev_show_info();
241-
242244
return 0;
243245
}
244246

0 commit comments

Comments
 (0)