Bluetooth: Mesh: Create dedicated helper for incrementing seq
The sequence number is incremented from several different places in the stack. The way it was done was potentially race condition prone, and was also problematic from the perspective of updating the sequence number in persistent storage. Create a dedicated helper for incrementing the sequence number (solves the race) which can in later patches be used to add the persistent storage support. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
parent
ca10b6bc94
commit
2be496a03b
3 changed files with 27 additions and 12 deletions
|
@ -381,6 +381,7 @@ static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd,
|
|||
struct net_buf_simple *sdu)
|
||||
{
|
||||
struct friend_pdu_info info;
|
||||
u32_t seq;
|
||||
|
||||
BT_DBG("LPN 0x%04x", frnd->lpn);
|
||||
|
||||
|
@ -392,9 +393,10 @@ static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd,
|
|||
info.ctl = 1;
|
||||
info.ttl = 0;
|
||||
|
||||
info.seq[0] = (bt_mesh.seq >> 16);
|
||||
info.seq[1] = (bt_mesh.seq >> 8);
|
||||
info.seq[2] = bt_mesh.seq++;
|
||||
seq = bt_mesh_next_seq();
|
||||
info.seq[0] = seq >> 16;
|
||||
info.seq[1] = seq >> 8;
|
||||
info.seq[2] = seq;
|
||||
|
||||
info.iv_index = BT_MESH_NET_IVI_TX;
|
||||
|
||||
|
@ -1139,6 +1141,7 @@ static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd,
|
|||
{
|
||||
struct friend_pdu_info info;
|
||||
struct net_buf *buf;
|
||||
u32_t seq;
|
||||
|
||||
BT_DBG("LPN 0x%04x", frnd->lpn);
|
||||
|
||||
|
@ -1152,9 +1155,10 @@ static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd,
|
|||
info.ttl = tx->ctx->send_ttl;
|
||||
info.ctl = (tx->ctx->app_idx == BT_MESH_KEY_UNUSED);
|
||||
|
||||
info.seq[0] = (bt_mesh.seq >> 16);
|
||||
info.seq[1] = (bt_mesh.seq >> 8);
|
||||
info.seq[2] = bt_mesh.seq++;
|
||||
seq = bt_mesh_next_seq();
|
||||
info.seq[0] = seq >> 16;
|
||||
info.seq[1] = seq >> 8;
|
||||
info.seq[2] = seq;
|
||||
|
||||
info.iv_index = BT_MESH_NET_IVI_TX;
|
||||
|
||||
|
|
|
@ -728,11 +728,17 @@ do_update:
|
|||
return true;
|
||||
}
|
||||
|
||||
u32_t bt_mesh_next_seq(void)
|
||||
{
|
||||
return bt_mesh.seq++;
|
||||
}
|
||||
|
||||
int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
|
||||
bool new_key, const struct bt_mesh_send_cb *cb,
|
||||
void *cb_data)
|
||||
{
|
||||
const u8_t *enc, *priv;
|
||||
u32_t seq;
|
||||
int err;
|
||||
|
||||
BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key,
|
||||
|
@ -754,9 +760,10 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
|
|||
}
|
||||
|
||||
/* Update with a new sequence number */
|
||||
buf->data[2] = (bt_mesh.seq >> 16);
|
||||
buf->data[3] = (bt_mesh.seq >> 8);
|
||||
buf->data[4] = bt_mesh.seq++;
|
||||
seq = bt_mesh_next_seq();
|
||||
buf->data[2] = seq >> 16;
|
||||
buf->data[3] = seq >> 8;
|
||||
buf->data[4] = seq;
|
||||
|
||||
err = bt_mesh_net_encrypt(enc, &buf->b, BT_MESH_NET_IVI_TX, false);
|
||||
if (err) {
|
||||
|
@ -796,6 +803,7 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
|
|||
bool proxy)
|
||||
{
|
||||
const bool ctl = (tx->ctx->app_idx == BT_MESH_KEY_UNUSED);
|
||||
u32_t seq_val;
|
||||
u8_t nid;
|
||||
const u8_t *enc, *priv;
|
||||
u8_t *seq;
|
||||
|
@ -816,9 +824,10 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
|
|||
net_buf_simple_push_be16(buf, tx->src);
|
||||
|
||||
seq = net_buf_simple_push(buf, 3);
|
||||
seq[0] = (bt_mesh.seq >> 16);
|
||||
seq[1] = (bt_mesh.seq >> 8);
|
||||
seq[2] = bt_mesh.seq++;
|
||||
seq_val = bt_mesh_next_seq();
|
||||
seq[0] = seq_val >> 16;
|
||||
seq[1] = seq_val >> 8;
|
||||
seq[2] = seq_val;
|
||||
|
||||
if (ctl) {
|
||||
net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80);
|
||||
|
|
|
@ -314,6 +314,8 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if,
|
|||
void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi,
|
||||
enum bt_mesh_net_if net_if);
|
||||
|
||||
u32_t bt_mesh_next_seq(void);
|
||||
|
||||
void bt_mesh_net_init(void);
|
||||
|
||||
/* Friendship Credential Management */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue