Commit dfdce2c1 authored by Pavel Vasilyev's avatar Pavel Vasilyev Committed by Carles Cufi
Browse files

bluetooth: mesh: brg_cfg: store tbl when changed only

Though nvs backend checks whether the data passed to it is identical to
the stored one, this isn't guaranteed by other the settings api. We can
also avoid going into the settings subsystem since we know what was
actually changed.

This commit adds 2 flags that allow to control whether Subnet Bridge
or Bridging Table states were changed and skips storing if the certain
state was not changed.

Signed-off-by: default avatarPavel Vasilyev <pavel.vasilyev@nordicsemi.no>
parent 67243453
Loading
Loading
Loading
Loading
+18 −1
Changes for subsys/bluetooth/mesh/brg_cfg.c: 18 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,6 +25,15 @@ static uint32_t bt_mesh_brg_cfg_row_cnt;
/* Bridging enabled state */
static bool brg_enabled;

enum {
	STATE_UPDATED,
	TABLE_UPDATED,
	BRG_CFG_FLAGS_COUNT,
};
#if defined(CONFIG_BT_SETTINGS)
static ATOMIC_DEFINE(brg_cfg_flags, BRG_CFG_FLAGS_COUNT);
#endif

static void brg_tbl_compact(void)
{
	int j = 0;
@@ -116,6 +125,7 @@ int bt_mesh_brg_cfg_enable_set(bool enable)

	brg_enabled = enable;
#if IS_ENABLED(CONFIG_BT_SETTINGS)
	atomic_set_bit(brg_cfg_flags, STATE_UPDATED);
	bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_BRG_PENDING);
#endif
	return 0;
@@ -128,6 +138,7 @@ void bt_mesh_brg_cfg_pending_store(void)
	char *path_tbl = "bt/mesh/brg_tbl";
	int err;

	if (atomic_test_and_clear_bit(brg_cfg_flags, STATE_UPDATED)) {
		if (brg_enabled) {
			err = settings_save_one(path_en, &brg_enabled, sizeof(brg_enabled));
		} else {
@@ -137,8 +148,9 @@ void bt_mesh_brg_cfg_pending_store(void)
		if (err) {
			LOG_ERR("Failed to store %s value", path_en);
		}
	}


	if (atomic_test_and_clear_bit(brg_cfg_flags, TABLE_UPDATED)) {
		if (bt_mesh_brg_cfg_row_cnt) {
			err = settings_save_one(path_tbl, &brg_tbl,
						bt_mesh_brg_cfg_row_cnt * sizeof(brg_tbl[0]));
@@ -149,6 +161,7 @@ void bt_mesh_brg_cfg_pending_store(void)
		if (err) {
			LOG_ERR("Failed to store %s value", path_tbl);
		}
	}
#endif
}

@@ -171,6 +184,7 @@ static void brg_tbl_netkey_removed_evt(struct bt_mesh_subnet *sub, enum bt_mesh_
	}

#if IS_ENABLED(CONFIG_BT_SETTINGS)
	atomic_set_bit(brg_cfg_flags, TABLE_UPDATED);
	bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_BRG_PENDING);
#endif
}
@@ -187,6 +201,7 @@ int bt_mesh_brg_cfg_tbl_reset(void)
	brg_enabled = false;
	bt_mesh_brg_cfg_row_cnt = 0;
	memset(brg_tbl, 0, sizeof(brg_tbl));
	atomic_clear(brg_cfg_flags);

#if CONFIG_BT_SETTINGS
	err = settings_delete("bt/mesh/brg_en");
@@ -256,6 +271,7 @@ int bt_mesh_brg_cfg_tbl_add(enum bt_mesh_brg_cfg_dir direction, uint16_t net_idx

store:
#if IS_ENABLED(CONFIG_BT_SETTINGS)
	atomic_set_bit(brg_cfg_flags, TABLE_UPDATED);
	bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_BRG_PENDING);
#endif

@@ -327,6 +343,7 @@ int bt_mesh_brg_cfg_tbl_remove(uint16_t net_idx1, uint16_t net_idx2, uint16_t ad

#if IS_ENABLED(CONFIG_BT_SETTINGS)
	if (store) {
		atomic_set_bit(brg_cfg_flags, TABLE_UPDATED);
		bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_BRG_PENDING);
	}
#endif
+8 −12
Changes for tests/bluetooth/mesh/brg/src/main.c: 8 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -205,8 +205,7 @@ ZTEST(bt_mesh_brg_cfg, test_basic_functionality_storage)
	}
}

static void pending_store_enable_create_expectations(bool *enable_val,
		int n, const struct bt_mesh_brg_cfg_row *tbl_val)
static void pending_store_enable_create_expectations(bool *enable_val)
{
	if (*enable_val) {
		ztest_expect_data(settings_save_one, name, "bt/mesh/brg_en");
@@ -215,7 +214,11 @@ static void pending_store_enable_create_expectations(bool *enable_val,
	} else {
		ztest_expect_data(settings_delete, name, "bt/mesh/brg_en");
	}
}

static void pending_store_tbl_create_expectations(int n,
						  const struct bt_mesh_brg_cfg_row *tbl_val)
{
	if (n > 0) {
		ztest_expect_data(settings_save_one, name, "bt/mesh/brg_tbl");
		ztest_expect_value(settings_save_one, val_len,
@@ -230,16 +233,12 @@ static void pending_store_enable_create_expectations(bool *enable_val,
ZTEST(bt_mesh_brg_cfg, test_brg_cfg_en)
{
	int err;
	int n;
	bool val;
	const struct bt_mesh_brg_cfg_row *tbl;

	check_bt_mesh_brg_cfg_tbl_reset();
	val = bt_mesh_brg_cfg_enable_get();
	n = bt_mesh_brg_cfg_tbl_get(&tbl);
	zassert_equal(val, false, NULL);
	pending_store_enable_create_expectations(&val, n, tbl);
	bt_mesh_brg_cfg_pending_store();
	/* No changed to the states, nothing to check. */


	ztest_expect_value(bt_mesh_settings_store_schedule, flag,
@@ -247,8 +246,7 @@ ZTEST(bt_mesh_brg_cfg, test_brg_cfg_en)
	err = bt_mesh_brg_cfg_enable_set(true);
	zassert_equal(err, 0, NULL);
	val = bt_mesh_brg_cfg_enable_get();
	n = bt_mesh_brg_cfg_tbl_get(&tbl);
	pending_store_enable_create_expectations(&val, n, tbl);
	pending_store_enable_create_expectations(&val);
	bt_mesh_brg_cfg_pending_store();

	zassert_equal(bt_mesh_brg_cfg_enable_get(), true, NULL);
@@ -258,7 +256,6 @@ ZTEST(bt_mesh_brg_cfg, test_brg_cfg_en)
ZTEST(bt_mesh_brg_cfg, test_brg_tbl_pending_store)
{
	int n, err;
	bool b_en;
	struct bt_mesh_brg_cfg_row test_vec = {
		.direction = BT_MESH_BRG_CFG_DIR_ONEWAY,
		.net_idx1 = 1,
@@ -277,12 +274,11 @@ ZTEST(bt_mesh_brg_cfg, test_brg_tbl_pending_store)
	const struct bt_mesh_brg_cfg_row *tbl;

	n = bt_mesh_brg_cfg_tbl_get(&tbl);
	b_en = bt_mesh_brg_cfg_enable_get();

	zassert_equal(n, 1);
	zassert_true(tbl);

	pending_store_enable_create_expectations(&b_en, 1, &test_vec);
	pending_store_tbl_create_expectations(1, &test_vec);
	bt_mesh_brg_cfg_pending_store();
}