Bluetooth: Mesh: Simplify & fix net credentials selection

The Mesh spec expects us to fall back to master credentials if
friendship ones are not available. Also remove an unnecessary branch
with the help of a new 'idx' variable.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-11-06 12:28:29 +02:00 committed by Johan Hedberg
commit 1ecb486f17
2 changed files with 21 additions and 25 deletions

View file

@ -748,7 +748,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);
u8_t nid;
u8_t nid, idx = (tx->sub->kr_phase == BT_MESH_KR_PHASE_2);
const u8_t *enc, *priv;
u8_t *seq;
int err;
@ -778,32 +778,23 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
net_buf_simple_push_u8(buf, tx->ctx->send_ttl);
}
if (tx->sub->kr_phase == BT_MESH_KR_PHASE_2) {
if (tx->ctx->friend_cred) {
err = bt_mesh_friend_cred_get(tx->sub->net_idx,
BT_MESH_ADDR_UNASSIGNED,
1, &nid, &enc, &priv);
if (err) {
return err;
}
} else {
nid = tx->sub->keys[1].nid;
enc = tx->sub->keys[1].enc;
priv = tx->sub->keys[1].privacy;
if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && tx->ctx->friend_cred) {
if (bt_mesh_friend_cred_get(tx->sub->net_idx,
BT_MESH_ADDR_UNASSIGNED,
idx, &nid, &enc, &priv)) {
BT_WARN("Falling back to master credentials");
tx->ctx->friend_cred = 0;
nid = tx->sub->keys[idx].nid;
enc = tx->sub->keys[idx].enc;
priv = tx->sub->keys[idx].privacy;
}
} else {
if (tx->ctx->friend_cred) {
err = bt_mesh_friend_cred_get(tx->sub->net_idx,
BT_MESH_ADDR_UNASSIGNED,
0, &nid, &enc, &priv);
if (err) {
return err;
}
} else {
nid = tx->sub->keys[0].nid;
enc = tx->sub->keys[0].enc;
priv = tx->sub->keys[0].privacy;
}
tx->ctx->friend_cred = 0;
nid = tx->sub->keys[idx].nid;
enc = tx->sub->keys[idx].enc;
priv = tx->sub->keys[idx].privacy;
}
net_buf_simple_push_u8(buf, (nid | (BT_MESH_NET_IVI_TX & 1) << 7));

View file

@ -365,6 +365,11 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, u8_t aid,
}
}
/* bt_mesh_net_send() may have modified this if the needed
* Friendship credentials were not found.
*/
tx->friend_cred = net_tx->ctx->friend_cred;
if (bt_mesh_lpn_established()) {
bt_mesh_lpn_friend_poll();
}