From fbe661fd3bc142af200301ec592baec9ca6875ba Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Fri, 25 Jan 2019 15:57:45 +0200 Subject: [PATCH] Bluetooth: Mesh: Convert bit-fields into flags There's already a flags member in the bt_mesh context, so take advantage of that for any boolean members that have so far been bit-fields. This should produce more efficient code, also for the sequence number that's now its own u32_t. Signed-off-by: Johan Hedberg --- subsys/bluetooth/host/mesh/beacon.c | 11 +++++---- subsys/bluetooth/host/mesh/lpn.c | 5 ++-- subsys/bluetooth/host/mesh/main.c | 11 +++------ subsys/bluetooth/host/mesh/net.c | 34 ++++++++++++++------------ subsys/bluetooth/host/mesh/net.h | 22 ++++++++++------- subsys/bluetooth/host/mesh/settings.c | 18 +++++++------- subsys/bluetooth/host/mesh/transport.c | 3 +-- 7 files changed, 55 insertions(+), 49 deletions(-) diff --git a/subsys/bluetooth/host/mesh/beacon.c b/subsys/bluetooth/host/mesh/beacon.c index 570033b8594..68474f19c56 100644 --- a/subsys/bluetooth/host/mesh/beacon.c +++ b/subsys/bluetooth/host/mesh/beacon.c @@ -249,7 +249,7 @@ static void beacon_send(struct k_work *work) /* Only resubmit if beaconing is still enabled */ if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED || - bt_mesh.ivu_initiator) { + atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) { k_delayed_work_submit(&beacon_timer, PROVISIONED_INTERVAL); } @@ -314,8 +314,9 @@ static void secure_beacon_recv(struct net_buf_simple *buf) BT_DBG("net_idx 0x%04x iv_index 0x%08x, current iv_index 0x%08x", sub->net_idx, iv_index, bt_mesh.iv_index); - if (bt_mesh.ivu_initiator && - bt_mesh.iv_update == BT_MESH_IV_UPDATE(flags)) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR) && + (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) == + BT_MESH_IV_UPDATE(flags))) { bt_mesh_beacon_ivu_initiator(false); } @@ -373,7 +374,7 @@ void bt_mesh_beacon_init(void) void bt_mesh_beacon_ivu_initiator(bool enable) { - bt_mesh.ivu_initiator = enable; + atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_INITIATOR, enable); if (enable) { k_work_submit(&beacon_timer.work); @@ -409,7 +410,7 @@ void bt_mesh_beacon_enable(void) void bt_mesh_beacon_disable(void) { - if (!bt_mesh.ivu_initiator) { + if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) { k_delayed_work_cancel(&beacon_timer); } } diff --git a/subsys/bluetooth/host/mesh/lpn.c b/subsys/bluetooth/host/mesh/lpn.c index 535f8ec0113..48094bd9cff 100644 --- a/subsys/bluetooth/host/mesh/lpn.c +++ b/subsys/bluetooth/host/mesh/lpn.c @@ -936,8 +936,9 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, return 0; } - if (bt_mesh.ivu_initiator && - bt_mesh.iv_update == BT_MESH_IV_UPDATE(msg->flags)) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR) && + (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) == + BT_MESH_IV_UPDATE(msg->flags))) { bt_mesh_beacon_ivu_initiator(false); } diff --git a/subsys/bluetooth/host/mesh/main.c b/subsys/bluetooth/host/mesh/main.c index 38ede54d23e..2ad7120af65 100644 --- a/subsys/bluetooth/host/mesh/main.c +++ b/subsys/bluetooth/host/mesh/main.c @@ -77,17 +77,14 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, void bt_mesh_reset(void) { - if (!bt_mesh.valid) { + if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { return; } bt_mesh.iv_index = 0U; bt_mesh.seq = 0U; - bt_mesh.iv_update = 0U; - bt_mesh.pending_update = 0U; - bt_mesh.valid = 0U; - bt_mesh.ivu_duration = 0U; - bt_mesh.ivu_initiator = 0U; + + memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags)); k_delayed_work_cancel(&bt_mesh.ivu_timer); @@ -126,7 +123,7 @@ void bt_mesh_reset(void) bool bt_mesh_is_provisioned(void) { - return bt_mesh.valid; + return atomic_test_bit(bt_mesh.flags, BT_MESH_VALID); } int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) diff --git a/subsys/bluetooth/host/mesh/net.c b/subsys/bluetooth/host/mesh/net.c index 5b94e2c1903..48be1e73c2f 100644 --- a/subsys/bluetooth/host/mesh/net.c +++ b/subsys/bluetooth/host/mesh/net.c @@ -412,7 +412,7 @@ u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub) flags |= BT_MESH_NET_FLAG_KR; } - if (bt_mesh.iv_update) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) { flags |= BT_MESH_NET_FLAG_IVU; } @@ -448,7 +448,7 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], BT_DBG("NetKey %s", bt_hex(key, 16)); - if (bt_mesh.valid) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { return -EALREADY; } @@ -472,7 +472,7 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], } } - bt_mesh.valid = 1U; + atomic_set_bit(bt_mesh.flags, BT_MESH_VALID); sub->net_idx = idx; if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) { @@ -482,7 +482,8 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], } bt_mesh.iv_index = iv_index; - bt_mesh.iv_update = BT_MESH_IV_UPDATE(flags); + atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, + BT_MESH_IV_UPDATE(flags)); /* Set minimum required hours, since the 96-hour minimum requirement * doesn't apply straight after provisioning (since we can't know how @@ -583,7 +584,7 @@ void bt_mesh_rpl_reset(void) #if defined(CONFIG_BT_MESH_IV_UPDATE_TEST) void bt_mesh_iv_update_test(bool enable) { - bt_mesh.ivu_test = enable; + atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_TEST, enable); /* Reset the duration variable - needed for some PTS tests */ bt_mesh.ivu_duration = 0U; } @@ -595,7 +596,7 @@ bool bt_mesh_iv_update(void) return false; } - if (bt_mesh.iv_update) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) { bt_mesh_net_iv_update(bt_mesh.iv_index, false); } else { bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); @@ -603,7 +604,7 @@ bool bt_mesh_iv_update(void) bt_mesh_net_sec_update(NULL); - return bt_mesh.iv_update; + return atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS); } #endif /* CONFIG_BT_MESH_IV_UPDATE_TEST */ @@ -624,7 +625,7 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update) { int i; - if (bt_mesh.iv_update) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) { /* We're currently in IV Update mode */ if (iv_index != bt_mesh.iv_index) { @@ -673,7 +674,8 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update) } } - if (!(IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST) && bt_mesh.ivu_test)) { + if (!(IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST) && + atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST))) { if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) { BT_WARN("IV Update before minimum duration"); return false; @@ -683,15 +685,15 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update) /* Defer change to Normal Operation if there are pending acks */ if (!iv_update && bt_mesh_tx_in_progress()) { BT_WARN("IV Update deferred because of pending transfer"); - bt_mesh.pending_update = 1U; + atomic_set_bit(bt_mesh.flags, BT_MESH_IVU_PENDING); return false; } do_update: - bt_mesh.iv_update = iv_update; + atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv_update); bt_mesh.ivu_duration = 0U; - if (bt_mesh.iv_update) { + if (iv_update) { bt_mesh.iv_index = iv_index; BT_DBG("IV Update state entered. New index 0x%08x", bt_mesh.iv_index); @@ -774,7 +776,8 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, bt_mesh_adv_send(buf, cb, cb_data); - if (!bt_mesh.iv_update && bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) { + if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) && + bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) { bt_mesh_beacon_ivu_initiator(true); bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); bt_mesh_net_sec_update(NULL); @@ -1340,7 +1343,8 @@ static void ivu_refresh(struct k_work *work) bt_mesh.ivu_duration += BT_MESH_IVU_HOURS; BT_DBG("%s for %u hour%s", - bt_mesh.iv_update ? "IVU in Progress" : "IVU Normal mode", + atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) ? + "IVU in Progress" : "IVU Normal mode", bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1 ? "" : "s"); if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) { @@ -1352,7 +1356,7 @@ static void ivu_refresh(struct k_work *work) return; } - if (bt_mesh.iv_update) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) { bt_mesh_beacon_ivu_initiator(true); bt_mesh_net_iv_update(bt_mesh.iv_index, false); } else if (IS_ENABLED(CONFIG_BT_SETTINGS)) { diff --git a/subsys/bluetooth/host/mesh/net.h b/subsys/bluetooth/host/mesh/net.h index ba82d7c0165..ae7ba05f6d4 100644 --- a/subsys/bluetooth/host/mesh/net.h +++ b/subsys/bluetooth/host/mesh/net.h @@ -191,8 +191,15 @@ struct bt_mesh_lpn { ATOMIC_DEFINE(to_remove, LPN_GROUPS); }; -/* bt_mesh_net.flags, mainly used for pending storage actions */ +/* bt_mesh_net.flags */ enum { + BT_MESH_VALID, /* We have been provisioned */ + BT_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */ + BT_MESH_IVU_INITIATOR, /* IV Update initiated by us */ + BT_MESH_IVU_TEST, /* IV Update test mode */ + BT_MESH_IVU_PENDING, /* Update blocked by SDU in progress */ + + /* pending storage actions */ BT_MESH_RPL_PENDING, BT_MESH_KEYS_PENDING, BT_MESH_NET_PENDING, @@ -207,13 +214,8 @@ enum { }; struct bt_mesh_net { - u32_t iv_index; /* Current IV Index */ - u32_t seq:24, /* Next outgoing sequence number */ - iv_update:1, /* 1 if IV Update in Progress */ - ivu_initiator:1, /* IV Update initiated by us */ - ivu_test:1, /* IV Update test mode */ - pending_update:1, /* Update blocked by SDU in progress */ - valid:1; /* 0 if unused */ + u32_t iv_index; /* Current IV Index */ + u32_t seq; /* Next outgoing sequence number (24 bits) */ ATOMIC_DEFINE(flags, BT_MESH_FLAG_COUNT); @@ -281,7 +283,9 @@ struct bt_mesh_net_tx { extern struct bt_mesh_net bt_mesh; -#define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - bt_mesh.iv_update) +#define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - \ + atomic_test_bit(bt_mesh.flags, \ + BT_MESH_IVU_IN_PROGRESS)) #define BT_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv) #define BT_MESH_NET_HDR_LEN 9 diff --git a/subsys/bluetooth/host/mesh/settings.c b/subsys/bluetooth/host/mesh/settings.c index 91132fc73c8..8c2f4e0a024 100644 --- a/subsys/bluetooth/host/mesh/settings.c +++ b/subsys/bluetooth/host/mesh/settings.c @@ -183,7 +183,7 @@ static int iv_set(int argc, char **argv, void *val_ctx) BT_DBG("IV deleted"); bt_mesh.iv_index = 0U; - bt_mesh.iv_update = 0U; + atomic_clear_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS); return 0; } @@ -194,11 +194,11 @@ static int iv_set(int argc, char **argv, void *val_ctx) } bt_mesh.iv_index = iv.iv_index; - bt_mesh.iv_update = iv.iv_update; + atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv.iv_update); bt_mesh.ivu_duration = iv.iv_duration; BT_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours", - bt_mesh.iv_index, bt_mesh.iv_update, bt_mesh.ivu_duration); + iv.iv_index, iv.iv_update, iv.iv_duration); return 0; } @@ -785,7 +785,7 @@ static int mesh_commit(void) cfg->default_ttl = stored_cfg.cfg.default_ttl; } - bt_mesh.valid = 1U; + atomic_set_bit(bt_mesh.flags, BT_MESH_VALID); bt_mesh_net_start(); @@ -871,7 +871,7 @@ static void store_pending_iv(void) int err; iv.iv_index = bt_mesh.iv_index; - iv.iv_update = bt_mesh.iv_update; + iv.iv_update = atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS); iv.iv_duration = bt_mesh.ivu_duration; err = settings_save_one("bt/mesh/IV", &iv, sizeof(iv)); @@ -1293,7 +1293,7 @@ static void store_pending(struct k_work *work) BT_DBG(""); if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_RPL_PENDING)) { - if (bt_mesh.valid) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { store_pending_rpl(); } else { clear_rpl(); @@ -1305,7 +1305,7 @@ static void store_pending(struct k_work *work) } if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_NET_PENDING)) { - if (bt_mesh.valid) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { store_pending_net(); } else { clear_net(); @@ -1313,7 +1313,7 @@ static void store_pending(struct k_work *work) } if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IV_PENDING)) { - if (bt_mesh.valid) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { store_pending_iv(); } else { clear_iv(); @@ -1329,7 +1329,7 @@ static void store_pending(struct k_work *work) } if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_CFG_PENDING)) { - if (bt_mesh.valid) { + if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) { store_pending_cfg(); } else { clear_cfg(); diff --git a/subsys/bluetooth/host/mesh/transport.c b/subsys/bluetooth/host/mesh/transport.c index 5fa20fe29ae..8cf0f1fd8bc 100644 --- a/subsys/bluetooth/host/mesh/transport.c +++ b/subsys/bluetooth/host/mesh/transport.c @@ -194,9 +194,8 @@ static void seg_tx_reset(struct seg_tx *tx) tx->nack_count = 0U; - if (bt_mesh.pending_update) { + if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IVU_PENDING)) { BT_DBG("Proceding with pending IV Update"); - bt_mesh.pending_update = 0U; /* bt_mesh_net_iv_update() will re-enable the flag if this * wasn't the only transfer. */