Skip to content

Commit fbe661f

Browse files
Johan Hedbergjhedberg
authored andcommitted
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 <[email protected]>
1 parent cb2e4b6 commit fbe661f

File tree

7 files changed

+55
-49
lines changed

7 files changed

+55
-49
lines changed

subsys/bluetooth/host/mesh/beacon.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static void beacon_send(struct k_work *work)
249249

250250
/* Only resubmit if beaconing is still enabled */
251251
if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED ||
252-
bt_mesh.ivu_initiator) {
252+
atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
253253
k_delayed_work_submit(&beacon_timer,
254254
PROVISIONED_INTERVAL);
255255
}
@@ -314,8 +314,9 @@ static void secure_beacon_recv(struct net_buf_simple *buf)
314314
BT_DBG("net_idx 0x%04x iv_index 0x%08x, current iv_index 0x%08x",
315315
sub->net_idx, iv_index, bt_mesh.iv_index);
316316

317-
if (bt_mesh.ivu_initiator &&
318-
bt_mesh.iv_update == BT_MESH_IV_UPDATE(flags)) {
317+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR) &&
318+
(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) ==
319+
BT_MESH_IV_UPDATE(flags))) {
319320
bt_mesh_beacon_ivu_initiator(false);
320321
}
321322

@@ -373,7 +374,7 @@ void bt_mesh_beacon_init(void)
373374

374375
void bt_mesh_beacon_ivu_initiator(bool enable)
375376
{
376-
bt_mesh.ivu_initiator = enable;
377+
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_INITIATOR, enable);
377378

378379
if (enable) {
379380
k_work_submit(&beacon_timer.work);
@@ -409,7 +410,7 @@ void bt_mesh_beacon_enable(void)
409410

410411
void bt_mesh_beacon_disable(void)
411412
{
412-
if (!bt_mesh.ivu_initiator) {
413+
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
413414
k_delayed_work_cancel(&beacon_timer);
414415
}
415416
}

subsys/bluetooth/host/mesh/lpn.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,9 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx,
936936
return 0;
937937
}
938938

939-
if (bt_mesh.ivu_initiator &&
940-
bt_mesh.iv_update == BT_MESH_IV_UPDATE(msg->flags)) {
939+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR) &&
940+
(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) ==
941+
BT_MESH_IV_UPDATE(msg->flags))) {
941942
bt_mesh_beacon_ivu_initiator(false);
942943
}
943944

subsys/bluetooth/host/mesh/main.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,14 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
7777

7878
void bt_mesh_reset(void)
7979
{
80-
if (!bt_mesh.valid) {
80+
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
8181
return;
8282
}
8383

8484
bt_mesh.iv_index = 0U;
8585
bt_mesh.seq = 0U;
86-
bt_mesh.iv_update = 0U;
87-
bt_mesh.pending_update = 0U;
88-
bt_mesh.valid = 0U;
89-
bt_mesh.ivu_duration = 0U;
90-
bt_mesh.ivu_initiator = 0U;
86+
87+
memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
9188

9289
k_delayed_work_cancel(&bt_mesh.ivu_timer);
9390

@@ -126,7 +123,7 @@ void bt_mesh_reset(void)
126123

127124
bool bt_mesh_is_provisioned(void)
128125
{
129-
return bt_mesh.valid;
126+
return atomic_test_bit(bt_mesh.flags, BT_MESH_VALID);
130127
}
131128

132129
int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)

subsys/bluetooth/host/mesh/net.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub)
412412
flags |= BT_MESH_NET_FLAG_KR;
413413
}
414414

415-
if (bt_mesh.iv_update) {
415+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
416416
flags |= BT_MESH_NET_FLAG_IVU;
417417
}
418418

@@ -448,7 +448,7 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
448448

449449
BT_DBG("NetKey %s", bt_hex(key, 16));
450450

451-
if (bt_mesh.valid) {
451+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
452452
return -EALREADY;
453453
}
454454

@@ -472,7 +472,7 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
472472
}
473473
}
474474

475-
bt_mesh.valid = 1U;
475+
atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
476476
sub->net_idx = idx;
477477

478478
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],
482482
}
483483

484484
bt_mesh.iv_index = iv_index;
485-
bt_mesh.iv_update = BT_MESH_IV_UPDATE(flags);
485+
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS,
486+
BT_MESH_IV_UPDATE(flags));
486487

487488
/* Set minimum required hours, since the 96-hour minimum requirement
488489
* doesn't apply straight after provisioning (since we can't know how
@@ -583,7 +584,7 @@ void bt_mesh_rpl_reset(void)
583584
#if defined(CONFIG_BT_MESH_IV_UPDATE_TEST)
584585
void bt_mesh_iv_update_test(bool enable)
585586
{
586-
bt_mesh.ivu_test = enable;
587+
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_TEST, enable);
587588
/* Reset the duration variable - needed for some PTS tests */
588589
bt_mesh.ivu_duration = 0U;
589590
}
@@ -595,15 +596,15 @@ bool bt_mesh_iv_update(void)
595596
return false;
596597
}
597598

598-
if (bt_mesh.iv_update) {
599+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
599600
bt_mesh_net_iv_update(bt_mesh.iv_index, false);
600601
} else {
601602
bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
602603
}
603604

604605
bt_mesh_net_sec_update(NULL);
605606

606-
return bt_mesh.iv_update;
607+
return atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
607608
}
608609
#endif /* CONFIG_BT_MESH_IV_UPDATE_TEST */
609610

@@ -624,7 +625,7 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update)
624625
{
625626
int i;
626627

627-
if (bt_mesh.iv_update) {
628+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
628629
/* We're currently in IV Update mode */
629630

630631
if (iv_index != bt_mesh.iv_index) {
@@ -673,7 +674,8 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update)
673674
}
674675
}
675676

676-
if (!(IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST) && bt_mesh.ivu_test)) {
677+
if (!(IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST) &&
678+
atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST))) {
677679
if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
678680
BT_WARN("IV Update before minimum duration");
679681
return false;
@@ -683,15 +685,15 @@ bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update)
683685
/* Defer change to Normal Operation if there are pending acks */
684686
if (!iv_update && bt_mesh_tx_in_progress()) {
685687
BT_WARN("IV Update deferred because of pending transfer");
686-
bt_mesh.pending_update = 1U;
688+
atomic_set_bit(bt_mesh.flags, BT_MESH_IVU_PENDING);
687689
return false;
688690
}
689691

690692
do_update:
691-
bt_mesh.iv_update = iv_update;
693+
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv_update);
692694
bt_mesh.ivu_duration = 0U;
693695

694-
if (bt_mesh.iv_update) {
696+
if (iv_update) {
695697
bt_mesh.iv_index = iv_index;
696698
BT_DBG("IV Update state entered. New index 0x%08x",
697699
bt_mesh.iv_index);
@@ -774,7 +776,8 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
774776

775777
bt_mesh_adv_send(buf, cb, cb_data);
776778

777-
if (!bt_mesh.iv_update && bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) {
779+
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) &&
780+
bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) {
778781
bt_mesh_beacon_ivu_initiator(true);
779782
bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
780783
bt_mesh_net_sec_update(NULL);
@@ -1340,7 +1343,8 @@ static void ivu_refresh(struct k_work *work)
13401343
bt_mesh.ivu_duration += BT_MESH_IVU_HOURS;
13411344

13421345
BT_DBG("%s for %u hour%s",
1343-
bt_mesh.iv_update ? "IVU in Progress" : "IVU Normal mode",
1346+
atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) ?
1347+
"IVU in Progress" : "IVU Normal mode",
13441348
bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1 ? "" : "s");
13451349

13461350
if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
@@ -1352,7 +1356,7 @@ static void ivu_refresh(struct k_work *work)
13521356
return;
13531357
}
13541358

1355-
if (bt_mesh.iv_update) {
1359+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
13561360
bt_mesh_beacon_ivu_initiator(true);
13571361
bt_mesh_net_iv_update(bt_mesh.iv_index, false);
13581362
} else if (IS_ENABLED(CONFIG_BT_SETTINGS)) {

subsys/bluetooth/host/mesh/net.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,15 @@ struct bt_mesh_lpn {
191191
ATOMIC_DEFINE(to_remove, LPN_GROUPS);
192192
};
193193

194-
/* bt_mesh_net.flags, mainly used for pending storage actions */
194+
/* bt_mesh_net.flags */
195195
enum {
196+
BT_MESH_VALID, /* We have been provisioned */
197+
BT_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */
198+
BT_MESH_IVU_INITIATOR, /* IV Update initiated by us */
199+
BT_MESH_IVU_TEST, /* IV Update test mode */
200+
BT_MESH_IVU_PENDING, /* Update blocked by SDU in progress */
201+
202+
/* pending storage actions */
196203
BT_MESH_RPL_PENDING,
197204
BT_MESH_KEYS_PENDING,
198205
BT_MESH_NET_PENDING,
@@ -207,13 +214,8 @@ enum {
207214
};
208215

209216
struct bt_mesh_net {
210-
u32_t iv_index; /* Current IV Index */
211-
u32_t seq:24, /* Next outgoing sequence number */
212-
iv_update:1, /* 1 if IV Update in Progress */
213-
ivu_initiator:1, /* IV Update initiated by us */
214-
ivu_test:1, /* IV Update test mode */
215-
pending_update:1, /* Update blocked by SDU in progress */
216-
valid:1; /* 0 if unused */
217+
u32_t iv_index; /* Current IV Index */
218+
u32_t seq; /* Next outgoing sequence number (24 bits) */
217219

218220
ATOMIC_DEFINE(flags, BT_MESH_FLAG_COUNT);
219221

@@ -281,7 +283,9 @@ struct bt_mesh_net_tx {
281283

282284
extern struct bt_mesh_net bt_mesh;
283285

284-
#define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - bt_mesh.iv_update)
286+
#define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - \
287+
atomic_test_bit(bt_mesh.flags, \
288+
BT_MESH_IVU_IN_PROGRESS))
285289
#define BT_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv)
286290

287291
#define BT_MESH_NET_HDR_LEN 9

subsys/bluetooth/host/mesh/settings.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static int iv_set(int argc, char **argv, void *val_ctx)
183183
BT_DBG("IV deleted");
184184

185185
bt_mesh.iv_index = 0U;
186-
bt_mesh.iv_update = 0U;
186+
atomic_clear_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
187187
return 0;
188188
}
189189

@@ -194,11 +194,11 @@ static int iv_set(int argc, char **argv, void *val_ctx)
194194
}
195195

196196
bt_mesh.iv_index = iv.iv_index;
197-
bt_mesh.iv_update = iv.iv_update;
197+
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv.iv_update);
198198
bt_mesh.ivu_duration = iv.iv_duration;
199199

200200
BT_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours",
201-
bt_mesh.iv_index, bt_mesh.iv_update, bt_mesh.ivu_duration);
201+
iv.iv_index, iv.iv_update, iv.iv_duration);
202202

203203
return 0;
204204
}
@@ -785,7 +785,7 @@ static int mesh_commit(void)
785785
cfg->default_ttl = stored_cfg.cfg.default_ttl;
786786
}
787787

788-
bt_mesh.valid = 1U;
788+
atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
789789

790790
bt_mesh_net_start();
791791

@@ -871,7 +871,7 @@ static void store_pending_iv(void)
871871
int err;
872872

873873
iv.iv_index = bt_mesh.iv_index;
874-
iv.iv_update = bt_mesh.iv_update;
874+
iv.iv_update = atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
875875
iv.iv_duration = bt_mesh.ivu_duration;
876876

877877
err = settings_save_one("bt/mesh/IV", &iv, sizeof(iv));
@@ -1293,7 +1293,7 @@ static void store_pending(struct k_work *work)
12931293
BT_DBG("");
12941294

12951295
if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_RPL_PENDING)) {
1296-
if (bt_mesh.valid) {
1296+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
12971297
store_pending_rpl();
12981298
} else {
12991299
clear_rpl();
@@ -1305,15 +1305,15 @@ static void store_pending(struct k_work *work)
13051305
}
13061306

13071307
if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_NET_PENDING)) {
1308-
if (bt_mesh.valid) {
1308+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
13091309
store_pending_net();
13101310
} else {
13111311
clear_net();
13121312
}
13131313
}
13141314

13151315
if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IV_PENDING)) {
1316-
if (bt_mesh.valid) {
1316+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
13171317
store_pending_iv();
13181318
} else {
13191319
clear_iv();
@@ -1329,7 +1329,7 @@ static void store_pending(struct k_work *work)
13291329
}
13301330

13311331
if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_CFG_PENDING)) {
1332-
if (bt_mesh.valid) {
1332+
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
13331333
store_pending_cfg();
13341334
} else {
13351335
clear_cfg();

subsys/bluetooth/host/mesh/transport.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ static void seg_tx_reset(struct seg_tx *tx)
194194

195195
tx->nack_count = 0U;
196196

197-
if (bt_mesh.pending_update) {
197+
if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IVU_PENDING)) {
198198
BT_DBG("Proceding with pending IV Update");
199-
bt_mesh.pending_update = 0U;
200199
/* bt_mesh_net_iv_update() will re-enable the flag if this
201200
* wasn't the only transfer.
202201
*/

0 commit comments

Comments
 (0)