From 6426cd58fc7ef2548dc2b6047a18cea9a0bc0659 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Fri, 27 Dec 2019 14:58:40 +0100 Subject: [PATCH] Bluetooth: mesh: Use 24-bit functions Use 24-bit functions for byteorder and net_buf in order to make the byteorder used more readable. Signed-off-by: Joakim Andersson --- subsys/bluetooth/mesh/cfg_srv.c | 4 +--- subsys/bluetooth/mesh/friend.c | 16 ++++------------ subsys/bluetooth/mesh/net.c | 16 +++------------- subsys/bluetooth/mesh/settings.c | 7 ++----- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/subsys/bluetooth/mesh/cfg_srv.c b/subsys/bluetooth/mesh/cfg_srv.c index 0301ab90675..e14ba086514 100644 --- a/subsys/bluetooth/mesh/cfg_srv.c +++ b/subsys/bluetooth/mesh/cfg_srv.c @@ -2785,9 +2785,7 @@ static void lpn_timeout_get(struct bt_mesh_model *model, timeout = k_delayed_work_remaining_get(&frnd->timer) / 100; send_rsp: - net_buf_simple_add_u8(&msg, timeout); - net_buf_simple_add_u8(&msg, timeout >> 8); - net_buf_simple_add_u8(&msg, timeout >> 16); + net_buf_simple_add_le24(&msg, timeout); if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { BT_ERR("Unable to send LPN PollTimeout Status"); diff --git a/subsys/bluetooth/mesh/friend.c b/subsys/bluetooth/mesh/friend.c index 55b0118f985..7355d038aee 100644 --- a/subsys/bluetooth/mesh/friend.c +++ b/subsys/bluetooth/mesh/friend.c @@ -461,9 +461,7 @@ static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf, } seq = bt_mesh_next_seq(); - buf->data[2] = seq >> 16; - buf->data[3] = seq >> 8; - buf->data[4] = seq; + sys_put_be24(seq, &buf->data[2]); iv_index = BT_MESH_NET_IVI_TX; FRIEND_ADV(buf)->app_idx = BT_MESH_KEY_UNUSED; @@ -921,9 +919,7 @@ int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) return -EINVAL; } - poll_to = (((u32_t)msg->poll_to[0] << 16) | - ((u32_t)msg->poll_to[1] << 8) | - ((u32_t)msg->poll_to[2])); + poll_to = sys_get_be24(msg->poll_to); if (poll_to <= 0x000009 || poll_to >= 0x34bc00) { BT_WARN("Prohibited PollTimeout (0x%06x)", poll_to); @@ -1328,9 +1324,7 @@ static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, info.ctl = rx->ctl; - info.seq[0] = (rx->seq >> 16); - info.seq[1] = (rx->seq >> 8); - info.seq[2] = rx->seq; + sys_put_be24(rx->seq, info.seq); info.iv_index = BT_MESH_NET_IVI_RX(rx); @@ -1367,9 +1361,7 @@ 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; + sys_put_be24(bt_mesh.seq, info.seq); info.iv_index = BT_MESH_NET_IVI_TX; diff --git a/subsys/bluetooth/mesh/net.c b/subsys/bluetooth/mesh/net.c index 2e1d5c1a533..17652bdd050 100644 --- a/subsys/bluetooth/mesh/net.c +++ b/subsys/bluetooth/mesh/net.c @@ -52,8 +52,7 @@ #define NID(pdu) ((pdu)[0] & 0x7f) #define CTL(pdu) ((pdu)[1] >> 7) #define TTL(pdu) ((pdu)[1] & 0x7f) -#define SEQ(pdu) (((u32_t)(pdu)[2] << 16) | \ - ((u32_t)(pdu)[3] << 8) | (u32_t)(pdu)[4]); +#define SEQ(pdu) (sys_get_be24(&pdu[2])) #define SRC(pdu) (sys_get_be16(&(pdu)[5])) #define DST(pdu) (sys_get_be16(&(pdu)[7])) @@ -759,9 +758,7 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, /* Update with a new sequence number */ seq = bt_mesh_next_seq(); - buf->data[2] = seq >> 16; - buf->data[3] = seq >> 8; - buf->data[4] = seq; + sys_put_be24(seq, &buf->data[2]); /* Get destination, in case it's a proxy client */ dst = DST(buf->data); @@ -803,10 +800,8 @@ 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; int err; if (ctl && net_buf_simple_tailroom(buf) < 8) { @@ -822,12 +817,7 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, net_buf_simple_push_be16(buf, tx->ctx->addr); net_buf_simple_push_be16(buf, tx->src); - - seq = net_buf_simple_push(buf, 3); - seq_val = bt_mesh_next_seq(); - seq[0] = seq_val >> 16; - seq[1] = seq_val >> 8; - seq[2] = seq_val; + net_buf_simple_push_be24(buf, bt_mesh_next_seq()); if (ctl) { net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80); diff --git a/subsys/bluetooth/mesh/settings.c b/subsys/bluetooth/mesh/settings.c index 97bef7ab379..04321a5c6e6 100644 --- a/subsys/bluetooth/mesh/settings.c +++ b/subsys/bluetooth/mesh/settings.c @@ -249,8 +249,7 @@ static int seq_set(const char *name, size_t len_rd, settings_read_cb read_cb, return err; } - bt_mesh.seq = ((u32_t)seq.val[0] | ((u32_t)seq.val[1] << 8) | - ((u32_t)seq.val[2] << 16)); + bt_mesh.seq = sys_get_le24(seq.val); if (CONFIG_BT_MESH_SEQ_STORE_RATE > 0) { /* Make sure we have a large enough sequence number. We @@ -1091,9 +1090,7 @@ static void store_pending_seq(void) struct seq_val seq; int err; - seq.val[0] = bt_mesh.seq; - seq.val[1] = bt_mesh.seq >> 8; - seq.val[2] = bt_mesh.seq >> 16; + sys_put_le24(bt_mesh.seq, seq.val); err = settings_save_one("bt/mesh/Seq", &seq, sizeof(seq)); if (err) {