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 <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2019-03-22 12:45:09 +02:00 committed by Johan Hedberg
commit 12a413779d
3 changed files with 26 additions and 16 deletions

View file

@ -4309,7 +4309,7 @@ static const char *ver_str(u8_t ver)
return "unknown";
}
void bt_dev_show_info(void)
static void bt_dev_show_info(void)
{
int i;
@ -4329,7 +4329,7 @@ void bt_dev_show_info(void)
bt_dev.lmp_subversion);
}
#else
void bt_dev_show_info(void)
static inline void bt_dev_show_info(void)
{
}
#endif /* CONFIG_BT_DEBUG */
@ -4493,8 +4493,6 @@ static int hci_init(void)
BT_ERR("Unable to set identity address");
return err;
}
bt_dev_show_info();
}
return 0;
@ -4614,6 +4612,17 @@ static int irk_init(void)
}
#endif /* CONFIG_BT_PRIVACY */
void bt_finalize_init(void)
{
atomic_set_bit(bt_dev.flags, BT_DEV_READY);
if (IS_ENABLED(CONFIG_BT_OBSERVER)) {
bt_le_scan_update(false);
}
bt_dev_show_info();
}
static int bt_init(void)
{
int err;
@ -4639,18 +4648,16 @@ static int bt_init(void)
k_delayed_work_init(&bt_dev.rpa_update, rpa_timeout);
#endif
bt_monitor_send(BT_MONITOR_OPEN_INDEX, NULL, 0);
atomic_set_bit(bt_dev.flags, BT_DEV_READY);
if (IS_ENABLED(CONFIG_BT_OBSERVER)) {
bt_le_scan_update(false);
}
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
if (!bt_dev.id_count) {
BT_WARN("No ID address. App must call settings_load()");
return 0;
}
if (bt_dev.id_count > 0) {
atomic_set_bit(bt_dev.flags, BT_DEV_PRESET_ID);
} else if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
BT_WARN("No ID address. Expecting one to come from storage.");
}
bt_finalize_init();
return 0;
}
@ -4753,6 +4760,8 @@ int bt_enable(bt_ready_cb_t cb)
return err;
}
bt_monitor_send(BT_MONITOR_OPEN_INDEX, NULL, 0);
if (!cb) {
return bt_init();
}

View file

@ -197,8 +197,7 @@ void bt_id_add(struct bt_keys *keys);
void bt_id_del(struct bt_keys *keys);
int bt_setup_id_addr(void);
void bt_dev_show_info(void);
void bt_finalize_init(void);
int bt_le_adv_start_internal(const struct bt_le_adv_param *param,
const struct bt_data *ad, size_t ad_len,

View file

@ -231,14 +231,16 @@ static int commit(void)
}
}
if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
bt_finalize_init();
}
for (h = _bt_settings_start; h < _bt_settings_end; h++) {
if (h->commit) {
h->commit();
}
}
bt_dev_show_info();
return 0;
}