Bluetooth: Mesh: Only settings load post mesh init

Prevent mesh stored settings from triggering unless bt_mesh_init
has been called.

Signed-off-by: Anders Storrø <anders.storro@nordicsemi.no>
This commit is contained in:
Anders Storrø 2022-11-24 12:05:02 +01:00 committed by Carles Cufí
commit 52a39c0180
4 changed files with 28 additions and 4 deletions

View file

@ -46,6 +46,11 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
const uint8_t dev_key[16])
{
int err;
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
return -ENODEV;
}
struct bt_mesh_cdb_subnet *subnet = NULL;
LOG_INF("Primary Element: 0x%04x", addr);
@ -172,7 +177,8 @@ int bt_mesh_provision_gatt(const uint8_t uuid[16], uint16_t net_idx, uint16_t ad
void bt_mesh_reset(void)
{
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID) ||
!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
return;
}
@ -181,6 +187,7 @@ void bt_mesh_reset(void)
bt_mesh.seq = 0U;
memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
atomic_set_bit(bt_mesh.flags, BT_MESH_INIT);
/* If this fails, the work handler will return early on the next
* execution, as the device is not provisioned. If the device is
@ -337,6 +344,10 @@ int bt_mesh_init(const struct bt_mesh_prov *prov,
{
int err;
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_INIT)) {
return -EALREADY;
}
err = bt_mesh_test();
if (err) {
return err;

View file

@ -167,6 +167,7 @@ struct bt_mesh_lpn {
/* bt_mesh_net.flags */
enum {
BT_MESH_INIT, /* We have been initialized */
BT_MESH_VALID, /* We have been provisioned */
BT_MESH_SUSPENDED, /* Network is temporarily suspended */
BT_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */

View file

@ -65,6 +65,10 @@ int bt_mesh_settings_set(settings_read_cb read_cb, void *cb_arg,
static int mesh_commit(void)
{
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
return 0;
}
if (!atomic_test_bit(bt_dev.flags, BT_DEV_ENABLE)) {
/* The Bluetooth mesh settings loader calls bt_mesh_start() immediately
* after loading the settings. This is not intended to work before

View file

@ -22,9 +22,17 @@ enum bt_mesh_settings_flag {
};
#ifdef CONFIG_BT_SETTINGS
#define BT_MESH_SETTINGS_DEFINE(_hname, _subtree, _set) \
SETTINGS_STATIC_HANDLER_DEFINE(bt_mesh_##_hname, "bt/mesh/" _subtree, \
NULL, _set, NULL, NULL)
#define BT_MESH_SETTINGS_DEFINE(_hname, _subtree, _set) \
static int pre_##_set(const char *name, size_t len_rd, settings_read_cb read_cb, \
void *cb_arg) \
{ \
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) { \
return 0; \
} \
return _set(name, len_rd, read_cb, cb_arg); \
} \
SETTINGS_STATIC_HANDLER_DEFINE(bt_mesh_##_hname, "bt/mesh/" _subtree, NULL, pre_##_set, \
NULL, NULL)
#else
/* Declaring non static settings handler helps avoid unnecessary ifdefs
* as well as unused function warning. Since the declared handler structure is