diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h index e6e31ae93d..e325695890 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h @@ -168,6 +168,19 @@ static inline void net_buf_simple_reset(struct net_buf_simple *buf) buf->data = buf->__buf; } +/** + * Clone buffer state, using the same data buffer. + * + * Initializes a buffer to point to the same data as an existing buffer. + * Allows operations on the same data without altering the length and + * offset of the original. + * + * @param original Buffer to clone. + * @param clone The new clone. + */ +void net_buf_simple_clone(const struct net_buf_simple *original, + struct net_buf_simple *clone); + /** * @brief Prepare data to be added at the end of the buffer * diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c index 09198491b3..2cb31d6d56 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -31,6 +31,12 @@ static inline struct net_buf *pool_get_uninit(struct net_buf_pool *pool, return buf; } +void net_buf_simple_clone(const struct net_buf_simple *original, + struct net_buf_simple *clone) +{ + memcpy(clone, original, sizeof(struct net_buf_simple)); +} + void *net_buf_simple_add(struct net_buf_simple *buf, size_t len) { u8_t *tail = net_buf_simple_tail(buf); diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c index 2531e4171e..880b123e63 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.c +++ b/components/bt/esp_ble_mesh/mesh_core/friend.c @@ -384,12 +384,11 @@ static int unseg_app_sdu_decrypt(struct bt_mesh_friend *frnd, struct net_buf *buf, const struct unseg_app_sdu_meta *meta) { - struct net_buf_simple sdu = { - .len = buf->len - 14, - .data = &buf->data[10], - .__buf = &buf->data[10], - .size = buf->len - 10, - }; + struct net_buf_simple sdu; + + net_buf_simple_clone(&buf->b, &sdu); + net_buf_simple_pull(&sdu, 10); + sdu.len -= 4; return bt_mesh_app_decrypt(meta->key, meta->is_dev_key, 0, &sdu, &sdu, meta->ad, meta->net.ctx.addr, @@ -401,12 +400,11 @@ static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd, struct net_buf *buf, const struct unseg_app_sdu_meta *meta) { - struct net_buf_simple sdu = { - .len = buf->len - 14, - .data = &buf->data[10], - .__buf = &buf->data[10], - .size = buf->len - 10, - }; + struct net_buf_simple sdu; + + net_buf_simple_clone(&buf->b, &sdu); + net_buf_simple_pull(&sdu, 10); + sdu.len -= 4; return bt_mesh_app_encrypt(meta->key, meta->is_dev_key, 0, &sdu, meta->ad, meta->net.ctx.addr,