From 6559de3238d3ff6f5da5535811457ad29b6e9677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Storr=C3=B8?= Date: Wed, 6 Sep 2023 13:06:40 +0200 Subject: [PATCH] Bluetooth: Mesh: Store priv proxy in sep entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stores persistent on-demand private GATT proxy state in separate settings entry. This is implemented to avoid issues related to backwards compatibility between device firmware updates. Signed-off-by: Anders Storrø --- subsys/bluetooth/mesh/cfg.c | 15 ++---- subsys/bluetooth/mesh/od_priv_proxy.h | 7 +++ subsys/bluetooth/mesh/od_priv_proxy_srv.c | 65 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 subsys/bluetooth/mesh/od_priv_proxy.h diff --git a/subsys/bluetooth/mesh/cfg.c b/subsys/bluetooth/mesh/cfg.c index 4a59f76bbbd..c7bea0d29b0 100644 --- a/subsys/bluetooth/mesh/cfg.c +++ b/subsys/bluetooth/mesh/cfg.c @@ -16,6 +16,7 @@ #include "friend.h" #include "adv.h" #include "cfg.h" +#include "od_priv_proxy.h" #include "priv_beacon.h" #define LOG_LEVEL CONFIG_BT_MESH_CFG_LOG_LEVEL @@ -31,9 +32,6 @@ struct cfg_val { uint8_t gatt_proxy; uint8_t frnd; uint8_t default_ttl; -#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) - uint8_t on_demand_state; -#endif }; void bt_mesh_beacon_set(bool beacon) @@ -157,9 +155,9 @@ int bt_mesh_od_priv_proxy_set(uint8_t on_demand_proxy) bt_mesh.on_demand_state = on_demand_proxy; } - if (IS_ENABLED(CONFIG_BT_SETTINGS) && + if (IS_ENABLED(CONFIG_BT_SETTINGS) && IS_ENABLED(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) && atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { - bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING); + bt_mesh_od_priv_proxy_srv_store_schedule(); } return 0; #endif @@ -449,9 +447,6 @@ static int cfg_set(const char *name, size_t len_rd, bt_mesh_gatt_proxy_set(cfg.gatt_proxy); bt_mesh_friend_set(cfg.frnd); bt_mesh_default_ttl_set(cfg.default_ttl); -#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) - bt_mesh_od_priv_proxy_set(cfg.on_demand_state); -#endif LOG_DBG("Restored configuration state"); @@ -484,10 +479,6 @@ static void store_pending_cfg(void) val.gatt_proxy = bt_mesh_gatt_proxy_get(); val.frnd = bt_mesh_friend_get(); val.default_ttl = bt_mesh_default_ttl_get(); -#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) - val.on_demand_state = bt_mesh_od_priv_proxy_get(); -#endif - err = settings_save_one("bt/mesh/Cfg", &val, sizeof(val)); if (err) { diff --git a/subsys/bluetooth/mesh/od_priv_proxy.h b/subsys/bluetooth/mesh/od_priv_proxy.h new file mode 100644 index 00000000000..7baa7f067fc --- /dev/null +++ b/subsys/bluetooth/mesh/od_priv_proxy.h @@ -0,0 +1,7 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +void bt_mesh_od_priv_proxy_srv_store_schedule(void); diff --git a/subsys/bluetooth/mesh/od_priv_proxy_srv.c b/subsys/bluetooth/mesh/od_priv_proxy_srv.c index 32f5ea44179..b18a8ec7530 100644 --- a/subsys/bluetooth/mesh/od_priv_proxy_srv.c +++ b/subsys/bluetooth/mesh/od_priv_proxy_srv.c @@ -9,11 +9,28 @@ #include "access.h" #include "cfg.h" #include "foundation.h" +#include "settings.h" #define LOG_LEVEL CONFIG_BT_MESH_MODEL_LOG_LEVEL #include LOG_MODULE_REGISTER(bt_mesh_od_priv_proxy_srv); + +static struct bt_mesh_model *od_priv_proxy_srv; +static uint8_t on_demand_state; + +static int od_priv_proxy_store(bool delete) +{ + if (!IS_ENABLED(CONFIG_BT_SETTINGS)) { + return 0; + } + + const void *data = delete ? NULL : &on_demand_state; + size_t len = delete ? 0 : sizeof(uint8_t); + + return bt_mesh_model_data_store(od_priv_proxy_srv, false, "pp", data, len); +} + static int proxy_status_rsp(struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx) { @@ -64,6 +81,8 @@ const struct bt_mesh_model_op _bt_mesh_od_priv_proxy_srv_op[] = { static int od_priv_proxy_srv_init(struct bt_mesh_model *mod) { + od_priv_proxy_srv = mod; + struct bt_mesh_model *priv_beacon_srv = bt_mesh_model_find( bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_PRIV_BEACON_SRV); struct bt_mesh_model *sol_pdu_rpl_srv = bt_mesh_model_find( @@ -89,6 +108,52 @@ static int od_priv_proxy_srv_init(struct bt_mesh_model *mod) return 0; } +static void od_priv_proxy_srv_reset(struct bt_mesh_model *model) +{ + on_demand_state = 0; + od_priv_proxy_store(true); +} + +#ifdef CONFIG_BT_SETTINGS +static int od_priv_proxy_srv_settings_set(struct bt_mesh_model *model, const char *name, + size_t len_rd, settings_read_cb read_cb, void *cb_data) +{ + int err; + + if (len_rd == 0) { + LOG_DBG("Cleared configuration state"); + return 0; + } + + err = bt_mesh_settings_set(read_cb, cb_data, &on_demand_state, sizeof(uint8_t)); + if (err) { + LOG_ERR("Failed to set OD private proxy state"); + return err; + } + + bt_mesh_od_priv_proxy_set(on_demand_state); + return 0; +} + +static void od_priv_proxy_srv_pending_store(struct bt_mesh_model *model) +{ + on_demand_state = bt_mesh_od_priv_proxy_get(); + od_priv_proxy_store(false); +} +#endif + const struct bt_mesh_model_cb _bt_mesh_od_priv_proxy_srv_cb = { .init = od_priv_proxy_srv_init, + .reset = od_priv_proxy_srv_reset, +#ifdef CONFIG_BT_SETTINGS + .settings_set = od_priv_proxy_srv_settings_set, + .pending_store = od_priv_proxy_srv_pending_store, +#endif }; + +void bt_mesh_od_priv_proxy_srv_store_schedule(void) +{ + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_model_data_store_schedule(od_priv_proxy_srv); + } +}