From 683098728ab548a8fdb34c575e30e6b9b7a3ed40 Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Tue, 5 Dec 2023 10:01:19 +0800 Subject: [PATCH] Bluetooth: Mesh: Use memslab replace with net_buf_pool Use memslab more efficiency than net_buf_pool and consume less ram with flash resource. Signed-off-by: Lingao Meng --- subsys/bluetooth/mesh/access.c | 1 - subsys/bluetooth/mesh/adv.c | 219 ++++++++++-------- subsys/bluetooth/mesh/adv.h | 53 +++-- subsys/bluetooth/mesh/adv_ext.c | 193 ++++++++------- subsys/bluetooth/mesh/adv_legacy.c | 65 +++--- subsys/bluetooth/mesh/app_keys.c | 1 - subsys/bluetooth/mesh/beacon.c | 49 ++-- subsys/bluetooth/mesh/cfg.c | 1 - subsys/bluetooth/mesh/cfg_srv.c | 1 - subsys/bluetooth/mesh/friend.c | 15 +- subsys/bluetooth/mesh/gatt_cli.c | 1 - subsys/bluetooth/mesh/health_srv.c | 1 - subsys/bluetooth/mesh/lpn.c | 1 - subsys/bluetooth/mesh/main.c | 1 - subsys/bluetooth/mesh/net.c | 49 ++-- subsys/bluetooth/mesh/net.h | 3 +- subsys/bluetooth/mesh/pb_adv.c | 143 ++++++------ subsys/bluetooth/mesh/pb_gatt.c | 1 - subsys/bluetooth/mesh/pb_gatt_cli.c | 1 - subsys/bluetooth/mesh/pb_gatt_srv.c | 1 - subsys/bluetooth/mesh/priv_beacon_srv.c | 1 - subsys/bluetooth/mesh/prov_device.c | 1 - subsys/bluetooth/mesh/provisioner.c | 1 - subsys/bluetooth/mesh/proxy.h | 2 +- subsys/bluetooth/mesh/proxy_cli.c | 5 +- subsys/bluetooth/mesh/proxy_cli.h | 2 +- subsys/bluetooth/mesh/proxy_msg.c | 15 +- subsys/bluetooth/mesh/proxy_msg.h | 2 +- subsys/bluetooth/mesh/proxy_srv.c | 7 +- subsys/bluetooth/mesh/rpl.c | 1 - subsys/bluetooth/mesh/rpr_srv.c | 1 - subsys/bluetooth/mesh/solicitation.c | 1 - subsys/bluetooth/mesh/statistic.c | 17 +- subsys/bluetooth/mesh/statistic.h | 4 +- subsys/bluetooth/mesh/subnet.c | 1 - subsys/bluetooth/mesh/transport.c | 31 ++- subsys/bluetooth/mesh/transport_legacy.c | 31 ++- .../bsim/bluetooth/mesh/src/test_advertiser.c | 142 ++++++------ tests/bsim/bluetooth/mesh/src/test_beacon.c | 1 - tests/bsim/bluetooth/mesh/src/test_blob.c | 2 +- tests/bsim/bluetooth/mesh/src/test_dfu.c | 1 - .../bsim/bluetooth/mesh/src/test_provision.c | 1 - tests/bsim/bluetooth/mesh/src/test_scanner.c | 1 - 43 files changed, 534 insertions(+), 537 deletions(-) diff --git a/subsys/bluetooth/mesh/access.c b/subsys/bluetooth/mesh/access.c index 81e2902afd1..f7de46fe167 100644 --- a/subsys/bluetooth/mesh/access.c +++ b/subsys/bluetooth/mesh/access.c @@ -19,7 +19,6 @@ #include "host/testing.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "lpn.h" #include "transport.h" diff --git a/subsys/bluetooth/mesh/adv.c b/subsys/bluetooth/mesh/adv.c index 548e7f3fe15..00e2a4aa7bb 100644 --- a/subsys/bluetooth/mesh/adv.c +++ b/subsys/bluetooth/mesh/adv.c @@ -17,7 +17,6 @@ #include "common/bt_str.h" -#include "adv.h" #include "net.h" #include "foundation.h" #include "beacon.h" @@ -47,124 +46,144 @@ static K_FIFO_DEFINE(bt_mesh_adv_queue); static K_FIFO_DEFINE(bt_mesh_relay_queue); static K_FIFO_DEFINE(bt_mesh_friend_queue); -void bt_mesh_adv_send_start(uint16_t duration, int err, struct bt_mesh_adv *adv) -{ - if (!adv->started) { - adv->started = 1; +K_MEM_SLAB_DEFINE_STATIC(local_adv_pool, sizeof(struct bt_mesh_adv), + CONFIG_BT_MESH_ADV_BUF_COUNT, __alignof__(struct bt_mesh_adv)); - if (adv->cb && adv->cb->start) { - adv->cb->start(duration, err, adv->cb_data); - } - - if (err) { - adv->cb = NULL; - } else if (IS_ENABLED(CONFIG_BT_MESH_STATISTIC)) { - bt_mesh_stat_succeeded_count(adv); - } - } -} - -static void bt_mesh_adv_send_end(int err, struct bt_mesh_adv const *adv) -{ - if (adv->started && adv->cb && adv->cb->end) { - adv->cb->end(err, adv->cb_data); - } -} - -static void adv_buf_destroy(struct net_buf *buf) -{ - struct bt_mesh_adv adv = *BT_MESH_ADV(buf); - - net_buf_destroy(buf); - - bt_mesh_adv_send_end(0, &adv); -} - -NET_BUF_POOL_DEFINE(adv_buf_pool, CONFIG_BT_MESH_ADV_BUF_COUNT, - BT_MESH_ADV_DATA_SIZE, BT_MESH_ADV_USER_DATA_SIZE, - adv_buf_destroy); - -static struct bt_mesh_adv adv_local_pool[CONFIG_BT_MESH_ADV_BUF_COUNT]; - -#if defined(CONFIG_BT_MESH_RELAY) -NET_BUF_POOL_DEFINE(relay_buf_pool, CONFIG_BT_MESH_RELAY_BUF_COUNT, - BT_MESH_ADV_DATA_SIZE, BT_MESH_ADV_USER_DATA_SIZE, - adv_buf_destroy); - -static struct bt_mesh_adv adv_relay_pool[CONFIG_BT_MESH_RELAY_BUF_COUNT]; +#if defined(CONFIG_BT_MESH_RELAY_BUF_COUNT) +K_MEM_SLAB_DEFINE_STATIC(relay_adv_pool, sizeof(struct bt_mesh_adv), + CONFIG_BT_MESH_RELAY_BUF_COUNT, __alignof__(struct bt_mesh_adv)); #endif #if defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) -NET_BUF_POOL_DEFINE(friend_buf_pool, CONFIG_BT_MESH_FRIEND_LPN_COUNT, - BT_MESH_ADV_DATA_SIZE, BT_MESH_ADV_USER_DATA_SIZE, - adv_buf_destroy); - -static struct bt_mesh_adv adv_friend_pool[CONFIG_BT_MESH_FRIEND_LPN_COUNT]; +K_MEM_SLAB_DEFINE_STATIC(friend_adv_pool, sizeof(struct bt_mesh_adv), + CONFIG_BT_MESH_FRIEND_LPN_COUNT, __alignof__(struct bt_mesh_adv)); #endif -static struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *buf_pool, - struct bt_mesh_adv *adv_pool, - enum bt_mesh_adv_type type, - enum bt_mesh_adv_tag tag, - uint8_t xmit, k_timeout_t timeout) +void bt_mesh_adv_send_start(uint16_t duration, int err, struct bt_mesh_adv_ctx *ctx) { + if (!ctx->started) { + ctx->started = 1; + + if (ctx->cb && ctx->cb->start) { + ctx->cb->start(duration, err, ctx->cb_data); + } + + if (err) { + ctx->cb = NULL; + } else if (IS_ENABLED(CONFIG_BT_MESH_STATISTIC)) { + bt_mesh_stat_succeeded_count(ctx); + } + } +} + +static void bt_mesh_adv_send_end(int err, struct bt_mesh_adv_ctx const *ctx) +{ + if (ctx->started && ctx->cb && ctx->cb->end) { + ctx->cb->end(err, ctx->cb_data); + } +} + +static struct bt_mesh_adv *adv_create_from_pool(struct k_mem_slab *buf_pool, + enum bt_mesh_adv_type type, + enum bt_mesh_adv_tag tag, + uint8_t xmit, k_timeout_t timeout) +{ + struct bt_mesh_adv_ctx *ctx; struct bt_mesh_adv *adv; - struct net_buf *buf; + int err; if (atomic_test_bit(bt_mesh.flags, BT_MESH_SUSPENDED)) { LOG_WRN("Refusing to allocate buffer while suspended"); return NULL; } - buf = net_buf_alloc(buf_pool, timeout); - if (!buf) { + err = k_mem_slab_alloc(buf_pool, (void **)&adv, timeout); + if (err) { return NULL; } - adv = &adv_pool[net_buf_id(buf)]; - BT_MESH_ADV(buf) = adv; + adv->__ref = 1; - (void)memset(adv, 0, sizeof(*adv)); + net_buf_simple_init_with_data(&adv->b, adv->__bufs, BT_MESH_ADV_DATA_SIZE); + net_buf_simple_reset(&adv->b); - adv->type = type; - adv->tag = tag; - adv->xmit = xmit; + ctx = &adv->ctx; - return buf; + (void)memset(ctx, 0, sizeof(*ctx)); + + ctx->type = type; + ctx->tag = tag; + ctx->xmit = xmit; + + return adv; } -struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, - enum bt_mesh_adv_tag tag, - uint8_t xmit, k_timeout_t timeout) +struct bt_mesh_adv *bt_mesh_adv_ref(struct bt_mesh_adv *adv) +{ + __ASSERT_NO_MSG(adv->__ref < UINT8_MAX); + + adv->__ref++; + + return adv; +} + +void bt_mesh_adv_unref(struct bt_mesh_adv *adv) +{ + __ASSERT_NO_MSG(adv->__ref > 0); + + if (--adv->__ref > 0) { + return; + } + + struct k_mem_slab *slab = &local_adv_pool; + struct bt_mesh_adv_ctx ctx = adv->ctx; + +#if defined(CONFIG_BT_MESH_RELAY) + if (adv->ctx.tag == BT_MESH_ADV_TAG_RELAY) { + slab = &relay_adv_pool; + } +#endif + +#if defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) + if (adv->ctx.tag == BT_MESH_ADV_TAG_FRIEND) { + slab = &friend_adv_pool; + } +#endif + + k_mem_slab_free(slab, (void *)adv); + bt_mesh_adv_send_end(0, &ctx); +} + +struct bt_mesh_adv *bt_mesh_adv_create(enum bt_mesh_adv_type type, + enum bt_mesh_adv_tag tag, + uint8_t xmit, k_timeout_t timeout) { #if defined(CONFIG_BT_MESH_RELAY) if (tag == BT_MESH_ADV_TAG_RELAY) { - return bt_mesh_adv_create_from_pool(&relay_buf_pool, - adv_relay_pool, type, - tag, xmit, timeout); + return adv_create_from_pool(&relay_adv_pool, + type, tag, xmit, timeout); } #endif #if defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) if (tag == BT_MESH_ADV_TAG_FRIEND) { - return bt_mesh_adv_create_from_pool(&friend_buf_pool, - adv_friend_pool, type, - tag, xmit, timeout); + return adv_create_from_pool(&friend_adv_pool, + type, tag, xmit, timeout); } #endif - return bt_mesh_adv_create_from_pool(&adv_buf_pool, adv_local_pool, type, - tag, xmit, timeout); + return adv_create_from_pool(&local_adv_pool, type, + tag, xmit, timeout); } -static struct net_buf *process_events(struct k_poll_event *ev, int count) +static struct bt_mesh_adv *process_events(struct k_poll_event *ev, int count) { for (; count; ev++, count--) { LOG_DBG("ev->state %u", ev->state); switch (ev->state) { case K_POLL_STATE_FIFO_DATA_AVAILABLE: - return net_buf_get(ev->fifo, K_NO_WAIT); + return k_fifo_get(ev->fifo, K_NO_WAIT); case K_POLL_STATE_NOT_READY: case K_POLL_STATE_CANCELLED: break; @@ -177,7 +196,7 @@ static struct net_buf *process_events(struct k_poll_event *ev, int count) return NULL; } -struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout) +struct bt_mesh_adv *bt_mesh_adv_get(k_timeout_t timeout) { int err; struct k_poll_event events[] = { @@ -204,22 +223,22 @@ struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout) return process_events(events, ARRAY_SIZE(events)); } -struct net_buf *bt_mesh_adv_buf_get_by_tag(enum bt_mesh_adv_tag_bit tags, k_timeout_t timeout) +struct bt_mesh_adv *bt_mesh_adv_get_by_tag(enum bt_mesh_adv_tag_bit tags, k_timeout_t timeout) { if (IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) && tags & BT_MESH_ADV_TAG_BIT_FRIEND) { - return net_buf_get(&bt_mesh_friend_queue, timeout); + return k_fifo_get(&bt_mesh_friend_queue, timeout); } if (IS_ENABLED(CONFIG_BT_MESH_RELAY) && !(tags & BT_MESH_ADV_TAG_BIT_LOCAL)) { - return net_buf_get(&bt_mesh_relay_queue, timeout); + return k_fifo_get(&bt_mesh_relay_queue, timeout); } - return bt_mesh_adv_buf_get(timeout); + return bt_mesh_adv_get(timeout); } -void bt_mesh_adv_buf_get_cancel(void) +void bt_mesh_adv_get_cancel(void) { LOG_DBG(""); @@ -234,38 +253,38 @@ void bt_mesh_adv_buf_get_cancel(void) } } -void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, +void bt_mesh_adv_send(struct bt_mesh_adv *adv, const struct bt_mesh_send_cb *cb, void *cb_data) { - LOG_DBG("type 0x%02x len %u: %s", BT_MESH_ADV(buf)->type, buf->len, - bt_hex(buf->data, buf->len)); + LOG_DBG("type 0x%02x len %u: %s", adv->ctx.type, adv->b.len, + bt_hex(adv->b.data, adv->b.len)); - BT_MESH_ADV(buf)->cb = cb; - BT_MESH_ADV(buf)->cb_data = cb_data; - BT_MESH_ADV(buf)->busy = 1U; + adv->ctx.cb = cb; + adv->ctx.cb_data = cb_data; + adv->ctx.busy = 1U; if (IS_ENABLED(CONFIG_BT_MESH_STATISTIC)) { - bt_mesh_stat_planned_count(BT_MESH_ADV(buf)); + bt_mesh_stat_planned_count(&adv->ctx); } if (IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) && - BT_MESH_ADV(buf)->tag == BT_MESH_ADV_TAG_FRIEND) { - net_buf_put(&bt_mesh_friend_queue, net_buf_ref(buf)); - bt_mesh_adv_buf_friend_ready(); + adv->ctx.tag == BT_MESH_ADV_TAG_FRIEND) { + k_fifo_put(&bt_mesh_friend_queue, bt_mesh_adv_ref(adv)); + bt_mesh_adv_friend_ready(); return; } if ((IS_ENABLED(CONFIG_BT_MESH_RELAY) && - BT_MESH_ADV(buf)->tag == BT_MESH_ADV_TAG_RELAY) || + adv->ctx.tag == BT_MESH_ADV_TAG_RELAY) || (IS_ENABLED(CONFIG_BT_MESH_PB_ADV_USE_RELAY_SETS) && - BT_MESH_ADV(buf)->tag == BT_MESH_ADV_TAG_PROV)) { - net_buf_put(&bt_mesh_relay_queue, net_buf_ref(buf)); - bt_mesh_adv_buf_relay_ready(); + adv->ctx.tag == BT_MESH_ADV_TAG_PROV)) { + k_fifo_put(&bt_mesh_relay_queue, bt_mesh_adv_ref(adv)); + bt_mesh_adv_relay_ready(); return; } - net_buf_put(&bt_mesh_adv_queue, net_buf_ref(buf)); - bt_mesh_adv_buf_local_ready(); + k_fifo_put(&bt_mesh_adv_queue, bt_mesh_adv_ref(adv)); + bt_mesh_adv_local_ready(); } int bt_mesh_adv_gatt_send(void) diff --git a/subsys/bluetooth/mesh/adv.h b/subsys/bluetooth/mesh/adv.h index 42b07601ad5..0c936a1aa52 100644 --- a/subsys/bluetooth/mesh/adv.h +++ b/subsys/bluetooth/mesh/adv.h @@ -4,14 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ +#ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_ +#define ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_ + /* Maximum advertising data payload for a single data type */ #define BT_MESH_ADV_DATA_SIZE 29 -/* The user data is a pointer (4 bytes) to struct bt_mesh_adv */ -#define BT_MESH_ADV_USER_DATA_SIZE 4 - -#define BT_MESH_ADV(buf) (*(struct bt_mesh_adv **)net_buf_user_data(buf)) - #define BT_MESH_ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5) #define BT_MESH_SCAN_INTERVAL_MS 30 #define BT_MESH_SCAN_WINDOW_MS 30 @@ -41,7 +39,7 @@ enum bt_mesh_adv_tag_bit { BT_MESH_ADV_TAG_BIT_PROV = BIT(BT_MESH_ADV_TAG_PROV), }; -struct bt_mesh_adv { +struct bt_mesh_adv_ctx { const struct bt_mesh_send_cb *cb; void *cb_data; @@ -53,24 +51,39 @@ struct bt_mesh_adv { uint8_t xmit; }; +struct bt_mesh_adv { + sys_snode_t node; + + struct bt_mesh_adv_ctx ctx; + + struct net_buf_simple b; + + uint8_t __ref; + + uint8_t __bufs[BT_MESH_ADV_DATA_SIZE]; +}; + /* Lookup table for Advertising data types for bt_mesh_adv_type: */ extern const uint8_t bt_mesh_adv_type[BT_MESH_ADV_TYPES]; -/* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */ -struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, - enum bt_mesh_adv_tag tag, - uint8_t xmit, k_timeout_t timeout); +struct bt_mesh_adv *bt_mesh_adv_ref(struct bt_mesh_adv *adv); +void bt_mesh_adv_unref(struct bt_mesh_adv *adv); -void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, +/* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */ +struct bt_mesh_adv *bt_mesh_adv_create(enum bt_mesh_adv_type type, + enum bt_mesh_adv_tag tag, + uint8_t xmit, k_timeout_t timeout); + +void bt_mesh_adv_send(struct bt_mesh_adv *adv, const struct bt_mesh_send_cb *cb, void *cb_data); -struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout); +struct bt_mesh_adv *bt_mesh_adv_get(k_timeout_t timeout); -struct net_buf *bt_mesh_adv_buf_get_by_tag(enum bt_mesh_adv_tag_bit tags, k_timeout_t timeout); +struct bt_mesh_adv *bt_mesh_adv_get_by_tag(enum bt_mesh_adv_tag_bit tags, k_timeout_t timeout); void bt_mesh_adv_gatt_update(void); -void bt_mesh_adv_buf_get_cancel(void); +void bt_mesh_adv_get_cancel(void); void bt_mesh_adv_init(void); @@ -83,13 +96,13 @@ int bt_mesh_adv_enable(void); /* Should not be called from work queue due to undefined behavior */ int bt_mesh_adv_disable(void); -void bt_mesh_adv_buf_local_ready(void); +void bt_mesh_adv_local_ready(void); -void bt_mesh_adv_buf_relay_ready(void); +void bt_mesh_adv_relay_ready(void); -void bt_mesh_adv_buf_terminate(const struct net_buf *buf); +void bt_mesh_adv_terminate(struct bt_mesh_adv *adv); -void bt_mesh_adv_buf_friend_ready(void); +void bt_mesh_adv_friend_ready(void); int bt_mesh_adv_gatt_send(void); @@ -97,9 +110,11 @@ int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param, int32_t duration const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len); -void bt_mesh_adv_send_start(uint16_t duration, int err, struct bt_mesh_adv *adv); +void bt_mesh_adv_send_start(uint16_t duration, int err, struct bt_mesh_adv_ctx *ctx); int bt_mesh_scan_active_set(bool active); int bt_mesh_adv_bt_data_send(uint8_t num_events, uint16_t adv_interval, const struct bt_data *ad, size_t ad_len); + +#endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_ */ diff --git a/subsys/bluetooth/mesh/adv_ext.c b/subsys/bluetooth/mesh/adv_ext.c index 38efbb4901c..d3bc8a3af82 100644 --- a/subsys/bluetooth/mesh/adv_ext.c +++ b/subsys/bluetooth/mesh/adv_ext.c @@ -18,7 +18,6 @@ #include "host/hci_core.h" -#include "adv.h" #include "net.h" #include "proxy.h" #include "solicitation.h" @@ -60,14 +59,14 @@ struct bt_mesh_ext_adv { const enum bt_mesh_adv_tag_bit tags; ATOMIC_DEFINE(flags, ADV_FLAGS_NUM); struct bt_le_ext_adv *instance; - struct net_buf *buf; + struct bt_mesh_adv *adv; uint64_t timestamp; struct k_work_delayable work; struct bt_le_adv_param adv_param; }; static void send_pending_adv(struct k_work *work); -static bool schedule_send(struct bt_mesh_ext_adv *adv); +static bool schedule_send(struct bt_mesh_ext_adv *ext_adv); static struct bt_mesh_ext_adv advs[] = { [0] = { @@ -136,7 +135,7 @@ static inline struct bt_mesh_ext_adv *gatt_adv_get(void) } } -static int adv_start(struct bt_mesh_ext_adv *adv, +static int adv_start(struct bt_mesh_ext_adv *ext_adv, const struct bt_le_adv_param *param, struct bt_le_ext_adv_start_param *start, const struct bt_data *ad, size_t ad_len, @@ -144,47 +143,47 @@ static int adv_start(struct bt_mesh_ext_adv *adv, { int err; - if (!adv->instance) { + if (!ext_adv->instance) { LOG_ERR("Mesh advertiser not enabled"); return -ENODEV; } - if (atomic_test_and_set_bit(adv->flags, ADV_FLAG_ACTIVE)) { + if (atomic_test_and_set_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { LOG_ERR("Advertiser is busy"); return -EBUSY; } - if (atomic_test_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS)) { - err = bt_le_ext_adv_update_param(adv->instance, param); + if (atomic_test_bit(ext_adv->flags, ADV_FLAG_UPDATE_PARAMS)) { + err = bt_le_ext_adv_update_param(ext_adv->instance, param); if (err) { LOG_ERR("Failed updating adv params: %d", err); - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); return err; } - atomic_set_bit_to(adv->flags, ADV_FLAG_UPDATE_PARAMS, - param != &adv->adv_param); + atomic_set_bit_to(ext_adv->flags, ADV_FLAG_UPDATE_PARAMS, + param != &ext_adv->adv_param); } - err = bt_le_ext_adv_set_data(adv->instance, ad, ad_len, sd, sd_len); + err = bt_le_ext_adv_set_data(ext_adv->instance, ad, ad_len, sd, sd_len); if (err) { LOG_ERR("Failed setting adv data: %d", err); - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); return err; } - adv->timestamp = k_uptime_get(); + ext_adv->timestamp = k_uptime_get(); - err = bt_le_ext_adv_start(adv->instance, start); + err = bt_le_ext_adv_start(ext_adv->instance, start); if (err) { LOG_ERR("Advertising failed: err %d", err); - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); } return err; } -static int bt_data_send(struct bt_mesh_ext_adv *adv, uint8_t num_events, uint16_t adv_interval, +static int bt_data_send(struct bt_mesh_ext_adv *ext_adv, uint8_t num_events, uint16_t adv_interval, const struct bt_data *ad, size_t ad_len) { struct bt_le_ext_adv_start_param start = { @@ -194,41 +193,41 @@ static int bt_data_send(struct bt_mesh_ext_adv *adv, uint8_t num_events, uint16_ adv_interval = MAX(ADV_INT_FAST_MS, adv_interval); /* Only update advertising parameters if they're different */ - if (adv->adv_param.interval_min != BT_MESH_ADV_SCAN_UNIT(adv_interval)) { - adv->adv_param.interval_min = BT_MESH_ADV_SCAN_UNIT(adv_interval); - adv->adv_param.interval_max = adv->adv_param.interval_min; - atomic_set_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS); + if (ext_adv->adv_param.interval_min != BT_MESH_ADV_SCAN_UNIT(adv_interval)) { + ext_adv->adv_param.interval_min = BT_MESH_ADV_SCAN_UNIT(adv_interval); + ext_adv->adv_param.interval_max = ext_adv->adv_param.interval_min; + atomic_set_bit(ext_adv->flags, ADV_FLAG_UPDATE_PARAMS); } - return adv_start(adv, &adv->adv_param, &start, ad, ad_len, NULL, 0); + return adv_start(ext_adv, &ext_adv->adv_param, &start, ad, ad_len, NULL, 0); } -static int buf_send(struct bt_mesh_ext_adv *adv, struct net_buf *buf) +static int adv_send(struct bt_mesh_ext_adv *ext_adv, struct bt_mesh_adv *adv) { - uint8_t num_events = BT_MESH_TRANSMIT_COUNT(BT_MESH_ADV(buf)->xmit) + 1; + uint8_t num_events = BT_MESH_TRANSMIT_COUNT(adv->ctx.xmit) + 1; uint16_t duration, adv_int; struct bt_data ad; int err; - adv_int = BT_MESH_TRANSMIT_INT(BT_MESH_ADV(buf)->xmit); + adv_int = BT_MESH_TRANSMIT_INT(adv->ctx.xmit); /* Upper boundary estimate: */ duration = num_events * (adv_int + 10); - LOG_DBG("type %u len %u: %s", BT_MESH_ADV(buf)->type, - buf->len, bt_hex(buf->data, buf->len)); + LOG_DBG("type %u len %u: %s", adv->ctx.type, + adv->b.len, bt_hex(adv->b.data, adv->b.len)); LOG_DBG("count %u interval %ums duration %ums", num_events, adv_int, duration); - ad.type = bt_mesh_adv_type[BT_MESH_ADV(buf)->type]; - ad.data_len = buf->len; - ad.data = buf->data; + ad.type = bt_mesh_adv_type[adv->ctx.type]; + ad.data_len = adv->b.len; + ad.data = adv->b.data; - err = bt_data_send(adv, num_events, adv_int, &ad, 1); + err = bt_data_send(ext_adv, num_events, adv_int, &ad, 1); if (!err) { - adv->buf = net_buf_ref(buf); + ext_adv->adv = bt_mesh_adv_ref(adv); } - bt_mesh_adv_send_start(duration, err, BT_MESH_ADV(buf)); + bt_mesh_adv_send_start(duration, err, &adv->ctx); return err; } @@ -243,50 +242,50 @@ static const char * const adv_tag_to_str[] = { static void send_pending_adv(struct k_work *work) { - struct bt_mesh_ext_adv *adv; - struct net_buf *buf; + struct bt_mesh_ext_adv *ext_adv; + struct bt_mesh_adv *adv; int err; - adv = CONTAINER_OF(work, struct bt_mesh_ext_adv, work.work); + ext_adv = CONTAINER_OF(work, struct bt_mesh_ext_adv, work.work); - if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_SENT)) { + if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_SENT)) { /* Calling k_uptime_delta on a timestamp moves it to the current time. * This is essential here, as schedule_send() uses the end of the event * as a reference to avoid sending the next advertisement too soon. */ - int64_t duration = k_uptime_delta(&adv->timestamp); + int64_t duration = k_uptime_delta(&ext_adv->timestamp); LOG_DBG("Advertising stopped after %u ms for %s", (uint32_t)duration, - adv->buf ? adv_tag_to_str[BT_MESH_ADV(adv->buf)->tag] : + ext_adv->adv ? adv_tag_to_str[ext_adv->adv->ctx.tag] : adv_tag_to_str[BT_MESH_ADV_TAG_PROXY]); - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); - atomic_clear_bit(adv->flags, ADV_FLAG_PROXY); - atomic_clear_bit(adv->flags, ADV_FLAG_PROXY_START); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START); - if (adv->buf) { - net_buf_unref(adv->buf); - adv->buf = NULL; + if (ext_adv->adv) { + bt_mesh_adv_unref(ext_adv->adv); + ext_adv->adv = NULL; } - (void)schedule_send(adv); + (void)schedule_send(ext_adv); return; } - atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULED); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_SCHEDULED); - while ((buf = bt_mesh_adv_buf_get_by_tag(adv->tags, K_NO_WAIT))) { + while ((adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT))) { /* busy == 0 means this was canceled */ - if (!BT_MESH_ADV(buf)->busy) { - net_buf_unref(buf); + if (!adv->ctx.busy) { + bt_mesh_adv_unref(adv); continue; } - BT_MESH_ADV(buf)->busy = 0U; - err = buf_send(adv, buf); + adv->ctx.busy = 0U; + err = adv_send(ext_adv, adv); - net_buf_unref(buf); + bt_mesh_adv_unref(adv); if (!err) { return; /* Wait for advertising to finish */ @@ -294,7 +293,7 @@ static void send_pending_adv(struct k_work *work) } if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) || - !(adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { + !(ext_adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { return; } @@ -303,51 +302,51 @@ static void send_pending_adv(struct k_work *work) return; } - atomic_set_bit(adv->flags, ADV_FLAG_PROXY_START); + atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY_START); if (!bt_mesh_adv_gatt_send()) { - atomic_set_bit(adv->flags, ADV_FLAG_PROXY); + atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY); } - if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING)) { - schedule_send(adv); + if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING)) { + schedule_send(ext_adv); } } -static bool schedule_send(struct bt_mesh_ext_adv *adv) +static bool schedule_send(struct bt_mesh_ext_adv *ext_adv) { uint64_t timestamp; int64_t delta; - timestamp = adv->timestamp; + timestamp = ext_adv->timestamp; - if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_PROXY)) { - atomic_clear_bit(adv->flags, ADV_FLAG_PROXY_START); - (void)bt_le_ext_adv_stop(adv->instance); + if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { + atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START); + (void)bt_le_ext_adv_stop(ext_adv->instance); - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); } - if (atomic_test_bit(adv->flags, ADV_FLAG_ACTIVE)) { - atomic_set_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING); + if (atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { + atomic_set_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING); return false; - } else if (atomic_test_and_set_bit(adv->flags, ADV_FLAG_SCHEDULED)) { + } else if (atomic_test_and_set_bit(ext_adv->flags, ADV_FLAG_SCHEDULED)) { return false; } - atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING); + atomic_clear_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING); if ((IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) && - adv->tags & BT_MESH_ADV_TAG_BIT_FRIEND) || - (CONFIG_BT_MESH_RELAY_ADV_SETS > 0 && adv->tags & BT_MESH_ADV_TAG_BIT_RELAY)) { - k_work_reschedule(&adv->work, K_NO_WAIT); + ext_adv->tags & BT_MESH_ADV_TAG_BIT_FRIEND) || + (CONFIG_BT_MESH_RELAY_ADV_SETS > 0 && ext_adv->tags & BT_MESH_ADV_TAG_BIT_RELAY)) { + k_work_reschedule(&ext_adv->work, K_NO_WAIT); } else { /* The controller will send the next advertisement immediately. * Introduce a delay here to avoid sending the next mesh packet closer * to the previous packet than what's permitted by the specification. */ delta = k_uptime_delta(×tamp); - k_work_reschedule(&adv->work, K_MSEC(ADV_INT_FAST_MS - delta)); + k_work_reschedule(&ext_adv->work, K_MSEC(ADV_INT_FAST_MS - delta)); } return true; @@ -358,17 +357,17 @@ void bt_mesh_adv_gatt_update(void) (void)schedule_send(gatt_adv_get()); } -void bt_mesh_adv_buf_local_ready(void) +void bt_mesh_adv_local_ready(void) { (void)schedule_send(advs); } -void bt_mesh_adv_buf_relay_ready(void) +void bt_mesh_adv_relay_ready(void) { - struct bt_mesh_ext_adv *adv = relay_adv_get(); + struct bt_mesh_ext_adv *ext_adv = relay_adv_get(); for (int i = 0; i < CONFIG_BT_MESH_RELAY_ADV_SETS; i++) { - if (schedule_send(&adv[i])) { + if (schedule_send(&ext_adv[i])) { return; } } @@ -379,7 +378,7 @@ void bt_mesh_adv_buf_relay_ready(void) } } -void bt_mesh_adv_buf_friend_ready(void) +void bt_mesh_adv_friend_ready(void) { if (IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE)) { schedule_send(&advs[1 + CONFIG_BT_MESH_RELAY_ADV_SETS]); @@ -388,33 +387,33 @@ void bt_mesh_adv_buf_friend_ready(void) } } -void bt_mesh_adv_buf_terminate(const struct net_buf *buf) +void bt_mesh_adv_terminate(struct bt_mesh_adv *adv) { int err; for (int i = 0; i < ARRAY_SIZE(advs); i++) { - struct bt_mesh_ext_adv *adv = &advs[i]; + struct bt_mesh_ext_adv *ext_adv = &advs[i]; - if (adv->buf != buf) { + if (ext_adv->adv != adv) { continue; } - if (!atomic_test_bit(adv->flags, ADV_FLAG_ACTIVE)) { + if (!atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { return; } - err = bt_le_ext_adv_stop(adv->instance); + err = bt_le_ext_adv_stop(ext_adv->instance); if (err) { LOG_ERR("Failed to stop adv %d", err); return; } /* Do not call `cb:end`, since this user action */ - BT_MESH_ADV(adv->buf)->cb = NULL; + adv->ctx.cb = NULL; - atomic_set_bit(adv->flags, ADV_FLAG_SENT); + atomic_set_bit(ext_adv->flags, ADV_FLAG_SENT); - k_work_submit(&adv->work.work); + k_work_submit(&ext_adv->work.work); return; } @@ -450,31 +449,31 @@ static struct bt_mesh_ext_adv *adv_instance_find(struct bt_le_ext_adv *instance) static void adv_sent(struct bt_le_ext_adv *instance, struct bt_le_ext_adv_sent_info *info) { - struct bt_mesh_ext_adv *adv = adv_instance_find(instance); + struct bt_mesh_ext_adv *ext_adv = adv_instance_find(instance); - if (!adv) { + if (!ext_adv) { LOG_WRN("Unexpected adv instance"); return; } - if (!atomic_test_bit(adv->flags, ADV_FLAG_ACTIVE)) { + if (!atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { return; } - atomic_set_bit(adv->flags, ADV_FLAG_SENT); + atomic_set_bit(ext_adv->flags, ADV_FLAG_SENT); - k_work_submit(&adv->work.work); + k_work_submit(&ext_adv->work.work); } #if defined(CONFIG_BT_MESH_GATT_SERVER) static void connected(struct bt_le_ext_adv *instance, struct bt_le_ext_adv_connected_info *info) { - struct bt_mesh_ext_adv *adv = gatt_adv_get(); + struct bt_mesh_ext_adv *ext_adv = gatt_adv_get(); - if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_PROXY_START)) { - atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE); - (void)schedule_send(adv); + if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START)) { + atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); + (void)schedule_send(ext_adv); } } #endif /* CONFIG_BT_MESH_GATT_SERVER */ @@ -541,7 +540,7 @@ int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param, const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len) { - struct bt_mesh_ext_adv *adv = gatt_adv_get(); + struct bt_mesh_ext_adv *ext_adv = gatt_adv_get(); struct bt_le_ext_adv_start_param start = { /* Timeout is set in 10 ms steps, with 0 indicating "forever" */ .timeout = (duration == SYS_FOREVER_MS) ? 0 : MAX(1, duration / 10), @@ -549,9 +548,9 @@ int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param, LOG_DBG("Start advertising %d ms", duration); - atomic_set_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS); + atomic_set_bit(ext_adv->flags, ADV_FLAG_UPDATE_PARAMS); - return adv_start(adv, param, &start, ad, ad_len, sd, sd_len); + return adv_start(ext_adv, param, &start, ad, ad_len, sd, sd_len); } int bt_mesh_adv_bt_data_send(uint8_t num_events, uint16_t adv_interval, diff --git a/subsys/bluetooth/mesh/adv_legacy.c b/subsys/bluetooth/mesh/adv_legacy.c index aef13864131..d4003e497bb 100644 --- a/subsys/bluetooth/mesh/adv_legacy.c +++ b/subsys/bluetooth/mesh/adv_legacy.c @@ -19,7 +19,6 @@ #include "host/hci_core.h" -#include "adv.h" #include "net.h" #include "foundation.h" #include "beacon.h" @@ -43,7 +42,7 @@ static bool enabled; static int bt_data_send(uint8_t num_events, uint16_t adv_int, const struct bt_data *ad, size_t ad_len, - struct bt_mesh_adv *adv) + struct bt_mesh_adv_ctx *ctx) { struct bt_le_adv_param param = {}; uint64_t uptime = k_uptime_get(); @@ -101,8 +100,8 @@ static int bt_data_send(uint8_t num_events, uint16_t adv_int, LOG_DBG("Advertising started. Sleeping %u ms", duration); - if (adv) { - bt_mesh_adv_send_start(duration, err, adv); + if (ctx) { + bt_mesh_adv_send_start(duration, err, ctx); } k_sleep(K_MSEC(duration)); @@ -124,37 +123,37 @@ int bt_mesh_adv_bt_data_send(uint8_t num_events, uint16_t adv_int, return bt_data_send(num_events, adv_int, ad, ad_len, NULL); } -static inline void buf_send(struct net_buf *buf) +static inline void adv_send(struct bt_mesh_adv *adv) { - uint16_t num_events = BT_MESH_TRANSMIT_COUNT(BT_MESH_ADV(buf)->xmit) + 1; + uint16_t num_events = BT_MESH_TRANSMIT_COUNT(adv->ctx.xmit) + 1; uint16_t adv_int; struct bt_data ad; - adv_int = BT_MESH_TRANSMIT_INT(BT_MESH_ADV(buf)->xmit); + adv_int = BT_MESH_TRANSMIT_INT(adv->ctx.xmit); - LOG_DBG("type %u len %u: %s", BT_MESH_ADV(buf)->type, - buf->len, bt_hex(buf->data, buf->len)); + LOG_DBG("type %u len %u: %s", adv->ctx.type, + adv->b.len, bt_hex(adv->b.data, adv->b.len)); - ad.type = bt_mesh_adv_type[BT_MESH_ADV(buf)->type]; - ad.data_len = buf->len; - ad.data = buf->data; + ad.type = bt_mesh_adv_type[adv->ctx.type]; + ad.data_len = adv->b.len; + ad.data = adv->b.data; - bt_data_send(num_events, adv_int, &ad, 1, BT_MESH_ADV(buf)); + bt_data_send(num_events, adv_int, &ad, 1, &adv->ctx); } static void adv_thread(void *p1, void *p2, void *p3) { LOG_DBG("started"); - struct net_buf *buf; + struct bt_mesh_adv *adv; while (enabled) { if (IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER)) { - buf = bt_mesh_adv_buf_get(K_NO_WAIT); - if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && !buf) { + adv = bt_mesh_adv_get(K_NO_WAIT); + if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && !adv) { (void)bt_mesh_sol_send(); } - while (!buf) { + while (!adv) { /* Adv timeout may be set by a call from proxy * to bt_mesh_adv_gatt_start: @@ -162,58 +161,58 @@ static void adv_thread(void *p1, void *p2, void *p3) adv_timeout = SYS_FOREVER_MS; (void)bt_mesh_adv_gatt_send(); - buf = bt_mesh_adv_buf_get(SYS_TIMEOUT_MS(adv_timeout)); + adv = bt_mesh_adv_get(SYS_TIMEOUT_MS(adv_timeout)); bt_le_adv_stop(); - if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && !buf) { + if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && !adv) { (void)bt_mesh_sol_send(); } } } else { - buf = bt_mesh_adv_buf_get(K_FOREVER); + adv = bt_mesh_adv_get(K_FOREVER); } - if (!buf) { + if (!adv) { continue; } /* busy == 0 means this was canceled */ - if (BT_MESH_ADV(buf)->busy) { - BT_MESH_ADV(buf)->busy = 0U; - buf_send(buf); + if (adv->ctx.busy) { + adv->ctx.busy = 0U; + adv_send(adv); } - net_buf_unref(buf); + bt_mesh_adv_unref(adv); /* Give other threads a chance to run */ k_yield(); } /* Empty the advertising pool when advertising is disabled */ - while ((buf = bt_mesh_adv_buf_get(K_NO_WAIT))) { - bt_mesh_adv_send_start(0, -ENODEV, BT_MESH_ADV(buf)); - net_buf_unref(buf); + while ((adv = bt_mesh_adv_get(K_NO_WAIT))) { + bt_mesh_adv_send_start(0, -ENODEV, &adv->ctx); + bt_mesh_adv_unref(adv); } } -void bt_mesh_adv_buf_local_ready(void) +void bt_mesh_adv_local_ready(void) { /* Will be handled automatically */ } -void bt_mesh_adv_buf_relay_ready(void) +void bt_mesh_adv_relay_ready(void) { /* Will be handled automatically */ } void bt_mesh_adv_gatt_update(void) { - bt_mesh_adv_buf_get_cancel(); + bt_mesh_adv_get_cancel(); } -void bt_mesh_adv_buf_terminate(const struct net_buf *buf) +void bt_mesh_adv_terminate(struct bt_mesh_adv *adv) { - ARG_UNUSED(buf); + ARG_UNUSED(adv); } void bt_mesh_adv_init(void) diff --git a/subsys/bluetooth/mesh/app_keys.c b/subsys/bluetooth/mesh/app_keys.c index a47d31447c5..5afd887a8e8 100644 --- a/subsys/bluetooth/mesh/app_keys.c +++ b/subsys/bluetooth/mesh/app_keys.c @@ -17,7 +17,6 @@ #include "rpl.h" #include "settings.h" #include "crypto.h" -#include "adv.h" #include "proxy.h" #include "friend.h" #include "foundation.h" diff --git a/subsys/bluetooth/mesh/beacon.c b/subsys/bluetooth/mesh/beacon.c index afdea5b4c6a..ef337f9f510 100644 --- a/subsys/bluetooth/mesh/beacon.c +++ b/subsys/bluetooth/mesh/beacon.c @@ -16,7 +16,6 @@ #include "common/bt_str.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "prov.h" @@ -256,7 +255,7 @@ static bool net_beacon_send(struct bt_mesh_subnet *sub, struct bt_mesh_beacon *b .end = beacon_complete, }; uint32_t now = k_uptime_get_32(); - struct net_buf *buf; + struct bt_mesh_adv *adv; uint32_t time_diff; uint32_t time_since_last_recv; int err; @@ -271,19 +270,19 @@ static bool net_beacon_send(struct bt_mesh_subnet *sub, struct bt_mesh_beacon *b return false; } - buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_BEACON, BT_MESH_ADV_TAG_LOCAL, PROV_XMIT, K_NO_WAIT); - if (!buf) { - LOG_ERR("Unable to allocate beacon buffer"); + if (!adv) { + LOG_ERR("Unable to allocate beacon adv"); return true; /* Bail out */ } - err = beacon_create(sub, &buf->b); + err = beacon_create(sub, &adv->b); if (!err) { - bt_mesh_adv_send(buf, &send_cb, beacon); + bt_mesh_adv_send(adv, &send_cb, beacon); } - net_buf_unref(buf); + bt_mesh_adv_unref(adv); return err != 0; } @@ -330,22 +329,22 @@ static int unprovisioned_beacon_send(void) { const struct bt_mesh_prov *prov; uint8_t uri_hash[16] = { 0 }; - struct net_buf *buf; + struct bt_mesh_adv *adv; uint16_t oob_info; LOG_DBG(""); - buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_BEACON, BT_MESH_ADV_TAG_LOCAL, UNPROV_XMIT, K_NO_WAIT); - if (!buf) { - LOG_ERR("Unable to allocate beacon buffer"); + if (!adv) { + LOG_ERR("Unable to allocate beacon adv"); return -ENOBUFS; } prov = bt_mesh_prov_get(); - net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED); - net_buf_add_mem(buf, prov->uuid, 16); + net_buf_simple_add_u8(&adv->b, BEACON_TYPE_UNPROVISIONED); + net_buf_simple_add_mem(&adv->b, prov->uuid, 16); if (prov->uri && bt_mesh_s1_str(prov->uri, uri_hash) == 0) { oob_info = prov->oob_info | BT_MESH_PROV_OOB_URI; @@ -353,31 +352,31 @@ static int unprovisioned_beacon_send(void) oob_info = prov->oob_info; } - net_buf_add_be16(buf, oob_info); - net_buf_add_mem(buf, uri_hash, 4); + net_buf_simple_add_be16(&adv->b, oob_info); + net_buf_simple_add_mem(&adv->b, uri_hash, 4); - bt_mesh_adv_send(buf, NULL, NULL); - net_buf_unref(buf); + bt_mesh_adv_send(adv, NULL, NULL); + bt_mesh_adv_unref(adv); if (prov->uri) { size_t len; - buf = bt_mesh_adv_create(BT_MESH_ADV_URI, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_URI, BT_MESH_ADV_TAG_LOCAL, UNPROV_XMIT, K_NO_WAIT); - if (!buf) { - LOG_ERR("Unable to allocate URI buffer"); + if (!adv) { + LOG_ERR("Unable to allocate URI adv"); return -ENOBUFS; } len = strlen(prov->uri); - if (net_buf_tailroom(buf) < len) { + if (net_buf_simple_tailroom(&adv->b) < len) { LOG_WRN("Too long URI to fit advertising data"); } else { - net_buf_add_mem(buf, prov->uri, len); - bt_mesh_adv_send(buf, NULL, NULL); + net_buf_simple_add_mem(&adv->b, prov->uri, len); + bt_mesh_adv_send(adv, NULL, NULL); } - net_buf_unref(buf); + bt_mesh_adv_unref(adv); } return 0; diff --git a/subsys/bluetooth/mesh/cfg.c b/subsys/bluetooth/mesh/cfg.c index c7bea0d29b0..4fef60d5c8d 100644 --- a/subsys/bluetooth/mesh/cfg.c +++ b/subsys/bluetooth/mesh/cfg.c @@ -14,7 +14,6 @@ #include "settings.h" #include "heartbeat.h" #include "friend.h" -#include "adv.h" #include "cfg.h" #include "od_priv_proxy.h" #include "priv_beacon.h" diff --git a/subsys/bluetooth/mesh/cfg_srv.c b/subsys/bluetooth/mesh/cfg_srv.c index fa6bb48703a..268f883d8b8 100644 --- a/subsys/bluetooth/mesh/cfg_srv.c +++ b/subsys/bluetooth/mesh/cfg_srv.c @@ -21,7 +21,6 @@ #include "host/testing.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "lpn.h" diff --git a/subsys/bluetooth/mesh/friend.c b/subsys/bluetooth/mesh/friend.c index 73b75eadf4f..6fbd9f81a68 100644 --- a/subsys/bluetooth/mesh/friend.c +++ b/subsys/bluetooth/mesh/friend.c @@ -13,7 +13,6 @@ #include #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "app_keys.h" @@ -1239,7 +1238,7 @@ static void friend_timeout(struct k_work *work) .start = buf_send_start, .end = buf_send_end, }; - struct net_buf *buf; + struct bt_mesh_adv *adv; uint8_t md; if (!friend_is_allocated(frnd)) { @@ -1281,19 +1280,19 @@ static void friend_timeout(struct k_work *work) frnd->queue_size--; send_last: - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_FRIEND, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_FRIEND, FRIEND_XMIT, K_NO_WAIT); - if (!buf) { - LOG_ERR("Unable to allocate friend adv buffer"); + if (!adv) { + LOG_ERR("Unable to allocate friend adv"); return; } - net_buf_add_mem(buf, frnd->last->data, frnd->last->len); + net_buf_simple_add_mem(&adv->b, frnd->last->data, frnd->last->len); frnd->pending_req = 0U; frnd->pending_buf = 1U; - bt_mesh_adv_send(buf, &buf_sent_cb, frnd); - net_buf_unref(buf); + bt_mesh_adv_send(adv, &buf_sent_cb, frnd); + bt_mesh_adv_unref(adv); } static void subnet_evt(struct bt_mesh_subnet *sub, enum bt_mesh_key_evt evt) diff --git a/subsys/bluetooth/mesh/gatt_cli.c b/subsys/bluetooth/mesh/gatt_cli.c index bff9b567011..7814f32f27f 100644 --- a/subsys/bluetooth/mesh/gatt_cli.c +++ b/subsys/bluetooth/mesh/gatt_cli.c @@ -18,7 +18,6 @@ #include "common/bt_str.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" diff --git a/subsys/bluetooth/mesh/health_srv.c b/subsys/bluetooth/mesh/health_srv.c index 8611e1004f6..3db3410aa34 100644 --- a/subsys/bluetooth/mesh/health_srv.c +++ b/subsys/bluetooth/mesh/health_srv.c @@ -16,7 +16,6 @@ #include #include "mesh.h" -#include "adv.h" #include "net.h" #include "transport.h" #include "access.h" diff --git a/subsys/bluetooth/mesh/lpn.c b/subsys/bluetooth/mesh/lpn.c index 2b655f729f3..f6f906b12fe 100644 --- a/subsys/bluetooth/mesh/lpn.c +++ b/subsys/bluetooth/mesh/lpn.c @@ -13,7 +13,6 @@ #include #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "transport.h" diff --git a/subsys/bluetooth/mesh/main.c b/subsys/bluetooth/mesh/main.c index 24bbd7c7e94..89115ebe3e8 100644 --- a/subsys/bluetooth/mesh/main.c +++ b/subsys/bluetooth/mesh/main.c @@ -18,7 +18,6 @@ #include #include "test.h" -#include "adv.h" #include "prov.h" #include "provisioner.h" #include "net.h" diff --git a/subsys/bluetooth/mesh/net.c b/subsys/bluetooth/mesh/net.c index 6a9f29b4064..943a5e83c23 100644 --- a/subsys/bluetooth/mesh/net.c +++ b/subsys/bluetooth/mesh/net.c @@ -20,7 +20,6 @@ #include "common/bt_str.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "rpl.h" @@ -526,19 +525,19 @@ static int net_loopback(const struct bt_mesh_net_tx *tx, const uint8_t *data, return 0; } -int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, +int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct bt_mesh_adv *adv, const struct bt_mesh_send_cb *cb, void *cb_data) { const struct bt_mesh_net_cred *cred; int err; LOG_DBG("src 0x%04x dst 0x%04x len %u headroom %zu tailroom %zu", tx->src, tx->ctx->addr, - buf->len, net_buf_headroom(buf), net_buf_tailroom(buf)); - LOG_DBG("Payload len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + adv->b.len, net_buf_simple_headroom(&adv->b), net_buf_simple_tailroom(&adv->b)); + LOG_DBG("Payload len %u: %s", adv->b.len, bt_hex(adv->b.data, adv->b.len)); LOG_DBG("Seq 0x%06x", bt_mesh.seq); cred = net_tx_cred_get(tx); - err = net_header_encode(tx, cred->nid, &buf->b); + err = net_header_encode(tx, cred->nid, &adv->b); if (err) { goto done; } @@ -546,7 +545,7 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, /* Deliver to local network interface if necessary */ if (bt_mesh_fixed_group_match(tx->ctx->addr) || bt_mesh_has_addr(tx->ctx->addr)) { - err = net_loopback(tx, buf->data, buf->len); + err = net_loopback(tx, adv->b.data, adv->b.len); /* Local unicast messages should not go out to network */ if (BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr) || @@ -569,28 +568,28 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, goto done; } - err = net_encrypt(&buf->b, cred, BT_MESH_NET_IVI_TX, BT_MESH_NONCE_NETWORK); + err = net_encrypt(&adv->b, cred, BT_MESH_NET_IVI_TX, BT_MESH_NONCE_NETWORK); if (err) { goto done; } - BT_MESH_ADV(buf)->cb = cb; - BT_MESH_ADV(buf)->cb_data = cb_data; + adv->ctx.cb = cb; + adv->ctx.cb_data = cb_data; /* Deliver to GATT Proxy Clients if necessary. */ if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) { - (void)bt_mesh_proxy_relay(buf, tx->ctx->addr); + (void)bt_mesh_proxy_relay(adv, tx->ctx->addr); } /* Deliver to GATT Proxy Servers if necessary. */ if (IS_ENABLED(CONFIG_BT_MESH_PROXY_CLIENT)) { - (void)bt_mesh_proxy_cli_relay(buf); + (void)bt_mesh_proxy_cli_relay(adv); } - bt_mesh_adv_send(buf, cb, cb_data); + bt_mesh_adv_send(adv, cb, cb_data); done: - net_buf_unref(buf); + bt_mesh_adv_unref(adv); return err; } @@ -684,7 +683,7 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, struct bt_mesh_net_rx *rx) { const struct bt_mesh_net_cred *cred; - struct net_buf *buf; + struct bt_mesh_adv *adv; uint8_t transmit; if (rx->ctx.recv_ttl <= 1U) { @@ -711,10 +710,10 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, transmit = bt_mesh_net_transmit_get(); } - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_RELAY, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_RELAY, transmit, K_NO_WAIT); - if (!buf) { - LOG_DBG("Out of relay buffers"); + if (!adv) { + LOG_DBG("Out of relay advs"); return; } @@ -722,23 +721,23 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, sbuf->data[1] &= 0x80; sbuf->data[1] |= rx->ctx.recv_ttl - 1U; - net_buf_add_mem(buf, sbuf->data, sbuf->len); + net_buf_simple_add_mem(&adv->b, sbuf->data, sbuf->len); cred = &rx->sub->keys[SUBNET_KEY_TX_IDX(rx->sub)].msg; - LOG_DBG("Relaying packet. TTL is now %u", TTL(buf->data)); + LOG_DBG("Relaying packet. TTL is now %u", TTL(adv->b.data)); /* Update NID if RX or RX was with friend credentials */ if (rx->friend_cred) { - buf->data[0] &= 0x80; /* Clear everything except IVI */ - buf->data[0] |= cred->nid; + adv->b.data[0] &= 0x80; /* Clear everything except IVI */ + adv->b.data[0] |= cred->nid; } /* We re-encrypt and obfuscate using the received IVI rather than * the normal TX IVI (which may be different) since the transport * layer nonce includes the IVI. */ - if (net_encrypt(&buf->b, cred, BT_MESH_NET_IVI_RX(rx), BT_MESH_NONCE_NETWORK)) { + if (net_encrypt(&adv->b, cred, BT_MESH_NET_IVI_RX(rx), BT_MESH_NONCE_NETWORK)) { LOG_ERR("Re-encrypting failed"); goto done; } @@ -751,15 +750,15 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, (rx->friend_cred || bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED || bt_mesh_priv_gatt_proxy_get() == BT_MESH_PRIV_GATT_PROXY_ENABLED)) { - bt_mesh_proxy_relay(buf, rx->ctx.recv_dst); + bt_mesh_proxy_relay(adv, rx->ctx.recv_dst); } if (relay_to_adv(rx->net_if) || rx->friend_cred) { - bt_mesh_adv_send(buf, NULL, NULL); + bt_mesh_adv_send(adv, NULL, NULL); } done: - net_buf_unref(buf); + bt_mesh_adv_unref(adv); } void bt_mesh_net_header_parse(struct net_buf_simple *buf, diff --git a/subsys/bluetooth/mesh/net.h b/subsys/bluetooth/mesh/net.h index 04179a4dd49..28c21da3eaf 100644 --- a/subsys/bluetooth/mesh/net.h +++ b/subsys/bluetooth/mesh/net.h @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "adv.h" #include "subnet.h" #include @@ -291,7 +292,7 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update); int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, enum bt_mesh_nonce_type type); -int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, +int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct bt_mesh_adv *adv, const struct bt_mesh_send_cb *cb, void *cb_data); int bt_mesh_net_decode(struct net_buf_simple *in, enum bt_mesh_net_if net_if, diff --git a/subsys/bluetooth/mesh/pb_adv.c b/subsys/bluetooth/mesh/pb_adv.c index e273739ed72..f723ff48c14 100644 --- a/subsys/bluetooth/mesh/pb_adv.c +++ b/subsys/bluetooth/mesh/pb_adv.c @@ -11,7 +11,6 @@ #include #include "host/testing.h" #include "net.h" -#include "adv.h" #include "crypto.h" #include "beacon.h" #include "prov.h" @@ -101,8 +100,8 @@ struct pb_adv { /* Transaction timeout in seconds */ uint8_t timeout; - /* Pending outgoing buffer(s) */ - struct net_buf *buf[3]; + /* Pending outgoing adv(s) */ + struct bt_mesh_adv *adv[3]; prov_bearer_send_complete_t cb; @@ -170,24 +169,24 @@ static void free_segments(void) { int i; - for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { - struct net_buf *buf = link.tx.buf[i]; + for (i = 0; i < ARRAY_SIZE(link.tx.adv); i++) { + struct bt_mesh_adv *adv = link.tx.adv[i]; - if (!buf) { + if (!adv) { break; } - link.tx.buf[i] = NULL; + link.tx.adv[i] = NULL; /* Terminate active adv */ - if (BT_MESH_ADV(buf)->busy == 0U) { - bt_mesh_adv_buf_terminate(buf); + if (adv->ctx.busy == 0U) { + bt_mesh_adv_terminate(adv); } else { /* Mark as canceled */ - BT_MESH_ADV(buf)->busy = 0U; + adv->ctx.busy = 0U; } - net_buf_unref(buf); + bt_mesh_adv_unref(adv); } } @@ -200,7 +199,7 @@ static void prov_clear_tx(void) { LOG_DBG(""); - /* If this fails, the work handler will not find any buffers to send, + /* If this fails, the work handler will not find any advs to send, * and return without rescheduling. The work handler also checks the * LINK_ACTIVE flag, so if this call is part of reset_adv_link, it'll * exit early. @@ -254,19 +253,19 @@ static void close_link(enum prov_bearer_link_status reason) cb->link_closed(&bt_mesh_pb_adv, cb_data, reason); } -static struct net_buf *adv_buf_create(uint8_t retransmits) +static struct bt_mesh_adv *adv_create(uint8_t retransmits) { - struct net_buf *buf; + struct bt_mesh_adv *adv; - buf = bt_mesh_adv_create(BT_MESH_ADV_PROV, BT_MESH_ADV_TAG_PROV, + adv = bt_mesh_adv_create(BT_MESH_ADV_PROV, BT_MESH_ADV_TAG_PROV, BT_MESH_TRANSMIT(retransmits, 20), BUF_TIMEOUT); - if (!buf) { - LOG_ERR("Out of provisioning buffers"); + if (!adv) { + LOG_ERR("Out of provisioning advs"); return NULL; } - return buf; + return adv; } static void ack_complete(uint16_t duration, int err, void *user_data) @@ -328,7 +327,7 @@ static void gen_prov_ack_send(uint8_t xact_id) .start = ack_complete, }; const struct bt_mesh_send_cb *complete; - struct net_buf *buf; + struct bt_mesh_adv *adv; bool pending = atomic_test_and_set_bit(link.flags, ADV_ACK_PENDING); LOG_DBG("xact_id 0x%x", xact_id); @@ -338,8 +337,8 @@ static void gen_prov_ack_send(uint8_t xact_id) return; } - buf = adv_buf_create(RETRANSMITS_ACK); - if (!buf) { + adv = adv_create(RETRANSMITS_ACK); + if (!adv) { atomic_clear_bit(link.flags, ADV_ACK_PENDING); return; } @@ -351,12 +350,12 @@ static void gen_prov_ack_send(uint8_t xact_id) complete = &cb; } - net_buf_add_be32(buf, link.id); - net_buf_add_u8(buf, xact_id); - net_buf_add_u8(buf, GPC_ACK); + net_buf_simple_add_be32(&adv->b, link.id); + net_buf_simple_add_u8(&adv->b, xact_id); + net_buf_simple_add_u8(&adv->b, GPC_ACK); - bt_mesh_adv_send(buf, complete, NULL); - net_buf_unref(buf); + bt_mesh_adv_send(adv, complete, NULL); + bt_mesh_adv_unref(adv); } static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) @@ -431,7 +430,7 @@ static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf) { LOG_DBG("len %u", buf->len); - if (!link.tx.buf[0]) { + if (!link.tx.adv[0]) { return; } @@ -596,20 +595,20 @@ static void send_reliable(void) { int i; - for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { - struct net_buf *buf = link.tx.buf[i]; + for (i = 0; i < ARRAY_SIZE(link.tx.adv); i++) { + struct bt_mesh_adv *adv = link.tx.adv[i]; - if (!buf) { + if (!adv) { break; } - if (BT_MESH_ADV(buf)->busy) { + if (adv->ctx.busy) { continue; } - LOG_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + LOG_DBG("%u bytes: %s", adv->b.len, bt_hex(adv->b.data, adv->b.len)); - bt_mesh_adv_send(buf, NULL, NULL); + bt_mesh_adv_send(adv, NULL, NULL); } k_work_reschedule(&link.tx.retransmit, RETRANSMIT_TIMEOUT); @@ -633,30 +632,30 @@ static void prov_retransmit(struct k_work *work) send_reliable(); } -static struct net_buf *ctl_buf_create(uint8_t op, const void *data, uint8_t data_len, - uint8_t retransmits) +static struct bt_mesh_adv *ctl_adv_create(uint8_t op, const void *data, uint8_t data_len, + uint8_t retransmits) { - struct net_buf *buf; + struct bt_mesh_adv *adv; LOG_DBG("op 0x%02x data_len %u", op, data_len); - buf = adv_buf_create(retransmits); - if (!buf) { + adv = adv_create(retransmits); + if (!adv) { return NULL; } - net_buf_add_be32(buf, link.id); + net_buf_simple_add_be32(&adv->b, link.id); /* Transaction ID, always 0 for Bearer messages */ - net_buf_add_u8(buf, 0x00); - net_buf_add_u8(buf, GPC_CTL(op)); - net_buf_add_mem(buf, data, data_len); + net_buf_simple_add_u8(&adv->b, 0x00); + net_buf_simple_add_u8(&adv->b, GPC_CTL(op)); + net_buf_simple_add_mem(&adv->b, data, data_len); - return buf; + return adv; } -static int bearer_ctl_send(struct net_buf *buf) +static int bearer_ctl_send(struct bt_mesh_adv *adv) { - if (!buf) { + if (!adv) { return -ENOMEM; } @@ -664,23 +663,23 @@ static int bearer_ctl_send(struct net_buf *buf) k_work_reschedule(&link.prot_timer, bt_mesh_prov_protocol_timeout_get()); link.tx.start = k_uptime_get(); - link.tx.buf[0] = buf; + link.tx.adv[0] = adv; send_reliable(); return 0; } -static int bearer_ctl_send_unacked(struct net_buf *buf, void *user_data) +static int bearer_ctl_send_unacked(struct bt_mesh_adv *adv, void *user_data) { - if (!buf) { + if (!adv) { return -ENOMEM; } prov_clear_tx(); k_work_reschedule(&link.prot_timer, bt_mesh_prov_protocol_timeout_get()); - bt_mesh_adv_send(buf, &buf_sent_cb, user_data); - net_buf_unref(buf); + bt_mesh_adv_send(adv, &buf_sent_cb, user_data); + bt_mesh_adv_unref(adv); return 0; } @@ -688,26 +687,26 @@ static int bearer_ctl_send_unacked(struct net_buf *buf, void *user_data) static int prov_send_adv(struct net_buf_simple *msg, prov_bearer_send_complete_t cb, void *cb_data) { - struct net_buf *start, *buf; + struct bt_mesh_adv *start, *adv; uint8_t seg_len, seg_id; prov_clear_tx(); k_work_reschedule(&link.prot_timer, bt_mesh_prov_protocol_timeout_get()); - start = adv_buf_create(RETRANSMITS_RELIABLE); + start = adv_create(RETRANSMITS_RELIABLE); if (!start) { return -ENOBUFS; } link.tx.id = next_transaction_id(link.tx.id); - net_buf_add_be32(start, link.id); - net_buf_add_u8(start, link.tx.id); + net_buf_simple_add_be32(&start->b, link.id); + net_buf_simple_add_u8(&start->b, link.tx.id); - net_buf_add_u8(start, GPC_START(last_seg(msg->len))); - net_buf_add_be16(start, msg->len); - net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); + net_buf_simple_add_u8(&start->b, GPC_START(last_seg(msg->len))); + net_buf_simple_add_be16(&start->b, msg->len); + net_buf_simple_add_u8(&start->b, bt_mesh_fcs_calc(msg->data, msg->len)); - link.tx.buf[0] = start; + link.tx.adv[0] = start; link.tx.cb = cb; link.tx.cb_data = cb_data; link.tx.start = k_uptime_get(); @@ -716,33 +715,33 @@ static int prov_send_adv(struct net_buf_simple *msg, seg_len = MIN(msg->len, START_PAYLOAD_MAX); LOG_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); - net_buf_add_mem(start, msg->data, seg_len); + net_buf_simple_add_mem(&start->b, msg->data, seg_len); net_buf_simple_pull(msg, seg_len); - buf = start; + adv = start; for (seg_id = 1U; msg->len > 0; seg_id++) { - if (seg_id >= ARRAY_SIZE(link.tx.buf)) { + if (seg_id >= ARRAY_SIZE(link.tx.adv)) { LOG_ERR("Too big message"); free_segments(); return -E2BIG; } - buf = adv_buf_create(RETRANSMITS_RELIABLE); - if (!buf) { + adv = adv_create(RETRANSMITS_RELIABLE); + if (!adv) { free_segments(); return -ENOBUFS; } - link.tx.buf[seg_id] = buf; + link.tx.adv[seg_id] = adv; seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); LOG_DBG("seg %u len %u: %s", seg_id, seg_len, bt_hex(msg->data, seg_len)); - net_buf_add_be32(buf, link.id); - net_buf_add_u8(buf, link.tx.id); - net_buf_add_u8(buf, GPC_CONT(seg_id)); - net_buf_add_mem(buf, msg->data, seg_len); + net_buf_simple_add_be32(&adv->b, link.id); + net_buf_simple_add_u8(&adv->b, link.tx.id); + net_buf_simple_add_u8(&adv->b, GPC_CONT(seg_id)); + net_buf_simple_add_mem(&adv->b, msg->data, seg_len); net_buf_simple_pull(msg, seg_len); } @@ -776,7 +775,7 @@ static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) LOG_DBG("Resending link ack"); /* Ignore errors, message will be attempted again if we keep receiving link open: */ (void)bearer_ctl_send_unacked( - ctl_buf_create(LINK_ACK, NULL, 0, RETRANSMITS_ACK), + ctl_adv_create(LINK_ACK, NULL, 0, RETRANSMITS_ACK), (void *)PROV_BEARER_LINK_STATUS_SUCCESS); return; } @@ -791,7 +790,7 @@ static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) net_buf_simple_reset(link.rx.buf); err = bearer_ctl_send_unacked( - ctl_buf_create(LINK_ACK, NULL, 0, RETRANSMITS_ACK), + ctl_adv_create(LINK_ACK, NULL, 0, RETRANSMITS_ACK), (void *)PROV_BEARER_LINK_STATUS_SUCCESS); if (err) { reset_adv_link(); @@ -891,7 +890,7 @@ static int prov_link_open(const uint8_t uuid[16], uint8_t timeout, net_buf_simple_reset(link.rx.buf); - return bearer_ctl_send(ctl_buf_create(LINK_OPEN, uuid, 16, RETRANSMITS_RELIABLE)); + return bearer_ctl_send(ctl_adv_create(LINK_OPEN, uuid, 16, RETRANSMITS_RELIABLE)); } static int prov_link_accept(const struct prov_bearer_cb *cb, void *cb_data) @@ -936,7 +935,7 @@ static void prov_link_close(enum prov_bearer_link_status status) link.tx.timeout = CLOSING_TIMEOUT; /* Ignore errors, the link will time out eventually if this doesn't get sent */ bearer_ctl_send_unacked( - ctl_buf_create(LINK_CLOSE, &status, 1, RETRANSMITS_LINK_CLOSE), + ctl_adv_create(LINK_CLOSE, &status, 1, RETRANSMITS_LINK_CLOSE), (void *)status); } diff --git a/subsys/bluetooth/mesh/pb_gatt.c b/subsys/bluetooth/mesh/pb_gatt.c index f8acc8f6c5a..849914f4b5f 100644 --- a/subsys/bluetooth/mesh/pb_gatt.c +++ b/subsys/bluetooth/mesh/pb_gatt.c @@ -8,7 +8,6 @@ #include #include "net.h" #include "proxy.h" -#include "adv.h" #include "prov.h" #include "pb_gatt.h" #include "proxy_msg.h" diff --git a/subsys/bluetooth/mesh/pb_gatt_cli.c b/subsys/bluetooth/mesh/pb_gatt_cli.c index 9231cc0f0b1..bf7dc14029a 100644 --- a/subsys/bluetooth/mesh/pb_gatt_cli.c +++ b/subsys/bluetooth/mesh/pb_gatt_cli.c @@ -16,7 +16,6 @@ #include #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" diff --git a/subsys/bluetooth/mesh/pb_gatt_srv.c b/subsys/bluetooth/mesh/pb_gatt_srv.c index f6d9298fc78..f4fdac53570 100644 --- a/subsys/bluetooth/mesh/pb_gatt_srv.c +++ b/subsys/bluetooth/mesh/pb_gatt_srv.c @@ -17,7 +17,6 @@ #include "common/bt_str.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" diff --git a/subsys/bluetooth/mesh/priv_beacon_srv.c b/subsys/bluetooth/mesh/priv_beacon_srv.c index 5b5e62f1736..98be589fc22 100644 --- a/subsys/bluetooth/mesh/priv_beacon_srv.c +++ b/subsys/bluetooth/mesh/priv_beacon_srv.c @@ -5,7 +5,6 @@ */ #include #include "net.h" -#include "adv.h" #include #include "proxy.h" #include "foundation.h" diff --git a/subsys/bluetooth/mesh/prov_device.c b/subsys/bluetooth/mesh/prov_device.c index af890396ac7..6ca65c2ec63 100644 --- a/subsys/bluetooth/mesh/prov_device.c +++ b/subsys/bluetooth/mesh/prov_device.c @@ -20,7 +20,6 @@ #include "common/bt_str.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "rpl.h" diff --git a/subsys/bluetooth/mesh/provisioner.c b/subsys/bluetooth/mesh/provisioner.c index aba2449892f..ba02723e830 100644 --- a/subsys/bluetooth/mesh/provisioner.c +++ b/subsys/bluetooth/mesh/provisioner.c @@ -21,7 +21,6 @@ #include "common/bt_str.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "rpl.h" diff --git a/subsys/bluetooth/mesh/proxy.h b/subsys/bluetooth/mesh/proxy.h index a2f6bb45ff6..34a119c3df6 100644 --- a/subsys/bluetooth/mesh/proxy.h +++ b/subsys/bluetooth/mesh/proxy.h @@ -34,6 +34,6 @@ int bt_mesh_proxy_adv_start(void); void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub, bool private); void bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub); -bool bt_mesh_proxy_relay(struct net_buf *buf, uint16_t dst); +bool bt_mesh_proxy_relay(struct bt_mesh_adv *adv, uint16_t dst); void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, uint16_t addr); uint8_t bt_mesh_proxy_srv_connected_cnt(void); diff --git a/subsys/bluetooth/mesh/proxy_cli.c b/subsys/bluetooth/mesh/proxy_cli.c index a0a25751b41..5dd6b02361f 100644 --- a/subsys/bluetooth/mesh/proxy_cli.c +++ b/subsys/bluetooth/mesh/proxy_cli.c @@ -16,7 +16,6 @@ #include #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" @@ -79,7 +78,7 @@ static struct bt_mesh_proxy_server *find_proxy_srv_by_conn(struct bt_conn *conn) return NULL; } -bool bt_mesh_proxy_cli_relay(struct net_buf *buf) +bool bt_mesh_proxy_cli_relay(struct bt_mesh_adv *adv) { bool relayed = false; int i; @@ -91,7 +90,7 @@ bool bt_mesh_proxy_cli_relay(struct net_buf *buf) continue; } - if (bt_mesh_proxy_relay_send(server->role->conn, buf)) { + if (bt_mesh_proxy_relay_send(server->role->conn, adv)) { continue; } diff --git a/subsys/bluetooth/mesh/proxy_cli.h b/subsys/bluetooth/mesh/proxy_cli.h index 8c1fae10e84..c0b69f4aaf6 100644 --- a/subsys/bluetooth/mesh/proxy_cli.h +++ b/subsys/bluetooth/mesh/proxy_cli.h @@ -8,6 +8,6 @@ void bt_mesh_proxy_cli_adv_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf); -bool bt_mesh_proxy_cli_relay(struct net_buf *buf); +bool bt_mesh_proxy_cli_relay(struct bt_mesh_adv *adv); bool bt_mesh_proxy_cli_is_connected(uint16_t net_idx); diff --git a/subsys/bluetooth/mesh/proxy_msg.c b/subsys/bluetooth/mesh/proxy_msg.c index 025909e0503..b2fa113d456 100644 --- a/subsys/bluetooth/mesh/proxy_msg.c +++ b/subsys/bluetooth/mesh/proxy_msg.c @@ -21,7 +21,6 @@ #include "common/bt_str.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" @@ -196,12 +195,12 @@ int bt_mesh_proxy_msg_send(struct bt_conn *conn, uint8_t type, static void buf_send_end(struct bt_conn *conn, void *user_data) { - struct net_buf *buf = user_data; + struct bt_mesh_adv *adv = user_data; - net_buf_unref(buf); + bt_mesh_adv_unref(adv); } -int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct net_buf *buf) +int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct bt_mesh_adv *adv) { int err; @@ -211,12 +210,12 @@ int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct net_buf *buf) * so we need to make a copy. */ net_buf_simple_reserve(&msg, 1); - net_buf_simple_add_mem(&msg, buf->data, buf->len); + net_buf_simple_add_mem(&msg, adv->b.data, adv->b.len); err = bt_mesh_proxy_msg_send(conn, BT_MESH_PROXY_NET_PDU, - &msg, buf_send_end, net_buf_ref(buf)); + &msg, buf_send_end, bt_mesh_adv_ref(adv)); - bt_mesh_adv_send_start(0, err, BT_MESH_ADV(buf)); + bt_mesh_adv_send_start(0, err, &adv->ctx); if (err) { LOG_ERR("Failed to send proxy message (err %d)", err); @@ -225,7 +224,7 @@ int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct net_buf *buf) * which is just opaque data to segment_and send) reference given * to segment_and_send() here. */ - net_buf_unref(buf); + bt_mesh_adv_unref(adv); } return err; diff --git a/subsys/bluetooth/mesh/proxy_msg.h b/subsys/bluetooth/mesh/proxy_msg.h index 7d4bd51ae4f..7ad4be7ae5d 100644 --- a/subsys/bluetooth/mesh/proxy_msg.h +++ b/subsys/bluetooth/mesh/proxy_msg.h @@ -51,7 +51,7 @@ ssize_t bt_mesh_proxy_msg_recv(struct bt_conn *conn, int bt_mesh_proxy_msg_send(struct bt_conn *conn, uint8_t type, struct net_buf_simple *msg, bt_gatt_complete_func_t end, void *user_data); -int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct net_buf *buf); +int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct bt_mesh_adv *adv); struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(struct bt_conn *conn, proxy_send_cb_t send, proxy_recv_cb_t recv); diff --git a/subsys/bluetooth/mesh/proxy_srv.c b/subsys/bluetooth/mesh/proxy_srv.c index 571144ceaed..45e29915325 100644 --- a/subsys/bluetooth/mesh/proxy_srv.c +++ b/subsys/bluetooth/mesh/proxy_srv.c @@ -20,7 +20,6 @@ #include "common/bt_str.h" #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "transport.h" @@ -1022,12 +1021,12 @@ static bool client_filter_match(struct bt_mesh_proxy_client *client, return false; } -bool bt_mesh_proxy_relay(struct net_buf *buf, uint16_t dst) +bool bt_mesh_proxy_relay(struct bt_mesh_adv *adv, uint16_t dst) { bool relayed = false; int i; - LOG_DBG("%u bytes to dst 0x%04x", buf->len, dst); + LOG_DBG("%u bytes to dst 0x%04x", adv->b.len, dst); for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; @@ -1040,7 +1039,7 @@ bool bt_mesh_proxy_relay(struct net_buf *buf, uint16_t dst) continue; } - if (bt_mesh_proxy_relay_send(client->cli->conn, buf)) { + if (bt_mesh_proxy_relay_send(client->cli->conn, adv)) { continue; } diff --git a/subsys/bluetooth/mesh/rpl.c b/subsys/bluetooth/mesh/rpl.c index e19df33908f..df67ddf9d2e 100644 --- a/subsys/bluetooth/mesh/rpl.c +++ b/subsys/bluetooth/mesh/rpl.c @@ -20,7 +20,6 @@ #include #include "mesh.h" -#include "adv.h" #include "net.h" #include "rpl.h" #include "settings.h" diff --git a/subsys/bluetooth/mesh/rpr_srv.c b/subsys/bluetooth/mesh/rpr_srv.c index 9813842a367..e97df35f28f 100644 --- a/subsys/bluetooth/mesh/rpr_srv.c +++ b/subsys/bluetooth/mesh/rpr_srv.c @@ -14,7 +14,6 @@ #include #include #include "access.h" -#include "adv.h" #include "prov.h" #include "crypto.h" #include "rpr.h" diff --git a/subsys/bluetooth/mesh/solicitation.c b/subsys/bluetooth/mesh/solicitation.c index e2100fa42db..642abfd87f1 100644 --- a/subsys/bluetooth/mesh/solicitation.c +++ b/subsys/bluetooth/mesh/solicitation.c @@ -12,7 +12,6 @@ #include #include #include "access.h" -#include "adv.h" #include "cfg.h" #include "crypto.h" #include "mesh.h" diff --git a/subsys/bluetooth/mesh/statistic.c b/subsys/bluetooth/mesh/statistic.c index 21c451bee73..b9f542a5923 100644 --- a/subsys/bluetooth/mesh/statistic.c +++ b/subsys/bluetooth/mesh/statistic.c @@ -6,7 +6,6 @@ #include -#include "adv.h" #include "net.h" #include "statistic.h" @@ -22,24 +21,24 @@ void bt_mesh_stat_reset(void) memset(&stat, 0, sizeof(struct bt_mesh_statistic)); } -void bt_mesh_stat_planned_count(struct bt_mesh_adv *adv) +void bt_mesh_stat_planned_count(struct bt_mesh_adv_ctx *ctx) { - if (adv->tag == BT_MESH_ADV_TAG_LOCAL) { + if (ctx->tag == BT_MESH_ADV_TAG_LOCAL) { stat.tx_local_planned++; - } else if (adv->tag == BT_MESH_ADV_TAG_RELAY) { + } else if (ctx->tag == BT_MESH_ADV_TAG_RELAY) { stat.tx_adv_relay_planned++; - } else if (adv->tag == BT_MESH_ADV_TAG_FRIEND) { + } else if (ctx->tag == BT_MESH_ADV_TAG_FRIEND) { stat.tx_friend_planned++; } } -void bt_mesh_stat_succeeded_count(struct bt_mesh_adv *adv) +void bt_mesh_stat_succeeded_count(struct bt_mesh_adv_ctx *ctx) { - if (adv->tag == BT_MESH_ADV_TAG_LOCAL) { + if (ctx->tag == BT_MESH_ADV_TAG_LOCAL) { stat.tx_local_succeeded++; - } else if (adv->tag == BT_MESH_ADV_TAG_RELAY) { + } else if (ctx->tag == BT_MESH_ADV_TAG_RELAY) { stat.tx_adv_relay_succeeded++; - } else if (adv->tag == BT_MESH_ADV_TAG_FRIEND) { + } else if (ctx->tag == BT_MESH_ADV_TAG_FRIEND) { stat.tx_friend_succeeded++; } } diff --git a/subsys/bluetooth/mesh/statistic.h b/subsys/bluetooth/mesh/statistic.h index fdb488f0d81..4cd9187f45a 100644 --- a/subsys/bluetooth/mesh/statistic.h +++ b/subsys/bluetooth/mesh/statistic.h @@ -7,8 +7,8 @@ #ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_STATISTIC_H_ #define ZEPHYR_SUBSYS_BLUETOOTH_MESH_STATISTIC_H_ -void bt_mesh_stat_planned_count(struct bt_mesh_adv *adv); -void bt_mesh_stat_succeeded_count(struct bt_mesh_adv *adv); +void bt_mesh_stat_planned_count(struct bt_mesh_adv_ctx *ctx); +void bt_mesh_stat_succeeded_count(struct bt_mesh_adv_ctx *ctx); void bt_mesh_stat_rx(enum bt_mesh_net_if net_if); #endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_STATISTIC_H_ */ diff --git a/subsys/bluetooth/mesh/subnet.c b/subsys/bluetooth/mesh/subnet.c index ef90ff8f725..695dd321b49 100644 --- a/subsys/bluetooth/mesh/subnet.c +++ b/subsys/bluetooth/mesh/subnet.c @@ -22,7 +22,6 @@ #include "common/bt_str.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "lpn.h" diff --git a/subsys/bluetooth/mesh/transport.c b/subsys/bluetooth/mesh/transport.c index 94c1f698e91..47e8492a901 100644 --- a/subsys/bluetooth/mesh/transport.c +++ b/subsys/bluetooth/mesh/transport.c @@ -22,7 +22,6 @@ #include "host/testing.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "app_keys.h" @@ -122,26 +121,26 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, const struct bt_mesh_send_cb *cb, void *cb_data, const uint8_t *ctl_op) { - struct net_buf *buf; + struct bt_mesh_adv *adv; - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, tx->xmit, BUF_TIMEOUT); - if (!buf) { - LOG_ERR("Out of network buffers"); + if (!adv) { + LOG_ERR("Out of network advs"); return -ENOBUFS; } - net_buf_reserve(buf, BT_MESH_NET_HDR_LEN); + net_buf_simple_reserve(&adv->b, BT_MESH_NET_HDR_LEN); if (ctl_op) { - net_buf_add_u8(buf, TRANS_CTL_HDR(*ctl_op, 0)); + net_buf_simple_add_u8(&adv->b, TRANS_CTL_HDR(*ctl_op, 0)); } else if (BT_MESH_IS_DEV_KEY(tx->ctx->app_idx)) { - net_buf_add_u8(buf, UNSEG_HDR(0, 0)); + net_buf_simple_add_u8(&adv->b, UNSEG_HDR(0, 0)); } else { - net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid)); + net_buf_simple_add_u8(&adv->b, UNSEG_HDR(1, tx->aid)); } - net_buf_add_mem(buf, sdu->data, sdu->len); + net_buf_simple_add_mem(&adv->b, sdu->data, sdu->len); if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) { if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx, @@ -149,7 +148,7 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, NULL, 1)) { if (BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { LOG_ERR("Not enough space in Friend Queue"); - net_buf_unref(buf); + bt_mesh_adv_unref(adv); return -ENOBUFS; } else { LOG_WRN("No space in Friend Queue"); @@ -158,19 +157,19 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, } if (bt_mesh_friend_enqueue_tx(tx, BT_MESH_FRIEND_PDU_SINGLE, - NULL, 1, &buf->b) && + NULL, 1, &adv->b) && BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ - net_buf_unref(buf); + bt_mesh_adv_unref(adv); send_cb_finalize(cb, cb_data); return 0; } } send: - return bt_mesh_net_send(tx, buf, cb, cb_data); + return bt_mesh_net_send(tx, adv, cb, cb_data); } static inline uint8_t seg_len(bool ctl) @@ -405,7 +404,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) (uint16_t)(tx->seq_auth & TRANS_SEQ_ZERO_MASK), tx->attempts_left); while (tx->seg_o <= tx->seg_n) { - struct net_buf *seg; + struct bt_mesh_adv *seg; int err; if (!tx->seg[tx->seg_o]) { @@ -421,7 +420,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) goto end; } - net_buf_reserve(seg, BT_MESH_NET_HDR_LEN); + net_buf_simple_reserve(&seg->b, BT_MESH_NET_HDR_LEN); seg_tx_buf_build(tx, tx->seg_o, &seg->b); LOG_DBG("Sending %u/%u", tx->seg_o, tx->seg_n); diff --git a/subsys/bluetooth/mesh/transport_legacy.c b/subsys/bluetooth/mesh/transport_legacy.c index da0c1830f73..1a826db4ac4 100644 --- a/subsys/bluetooth/mesh/transport_legacy.c +++ b/subsys/bluetooth/mesh/transport_legacy.c @@ -22,7 +22,6 @@ #include "host/testing.h" #include "crypto.h" -#include "adv.h" #include "mesh.h" #include "net.h" #include "app_keys.h" @@ -129,26 +128,26 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, const struct bt_mesh_send_cb *cb, void *cb_data, const uint8_t *ctl_op) { - struct net_buf *buf; + struct bt_mesh_adv *adv; - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, tx->xmit, BUF_TIMEOUT); - if (!buf) { - LOG_ERR("Out of network buffers"); + if (!adv) { + LOG_ERR("Out of network advs"); return -ENOBUFS; } - net_buf_reserve(buf, BT_MESH_NET_HDR_LEN); + net_buf_simple_reserve(&adv->b, BT_MESH_NET_HDR_LEN); if (ctl_op) { - net_buf_add_u8(buf, TRANS_CTL_HDR(*ctl_op, 0)); + net_buf_simple_add_u8(&adv->b, TRANS_CTL_HDR(*ctl_op, 0)); } else if (BT_MESH_IS_DEV_KEY(tx->ctx->app_idx)) { - net_buf_add_u8(buf, UNSEG_HDR(0, 0)); + net_buf_simple_add_u8(&adv->b, UNSEG_HDR(0, 0)); } else { - net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid)); + net_buf_simple_add_u8(&adv->b, UNSEG_HDR(1, tx->aid)); } - net_buf_add_mem(buf, sdu->data, sdu->len); + net_buf_simple_add_mem(&adv->b, sdu->data, sdu->len); if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) { if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx, @@ -156,7 +155,7 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, NULL, 1)) { if (BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { LOG_ERR("Not enough space in Friend Queue"); - net_buf_unref(buf); + bt_mesh_adv_unref(adv); return -ENOBUFS; } @@ -165,19 +164,19 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, } if (bt_mesh_friend_enqueue_tx(tx, BT_MESH_FRIEND_PDU_SINGLE, - NULL, 1, &buf->b) && + NULL, 1, &adv->b) && BT_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ - net_buf_unref(buf); + bt_mesh_adv_unref(adv); send_cb_finalize(cb, cb_data); return 0; } } send: - return bt_mesh_net_send(tx, buf, cb, cb_data); + return bt_mesh_net_send(tx, adv, cb, cb_data); } static inline uint8_t seg_len(bool ctl) @@ -392,7 +391,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) tx->attempts); while (tx->seg_o <= tx->seg_n) { - struct net_buf *seg; + struct bt_mesh_adv *seg; int err; if (!tx->seg[tx->seg_o]) { @@ -408,7 +407,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) goto end; } - net_buf_reserve(seg, BT_MESH_NET_HDR_LEN); + net_buf_simple_reserve(&seg->b, BT_MESH_NET_HDR_LEN); seg_tx_buf_build(tx, tx->seg_o, &seg->b); LOG_DBG("Sending %u/%u", tx->seg_o, tx->seg_n); diff --git a/tests/bsim/bluetooth/mesh/src/test_advertiser.c b/tests/bsim/bluetooth/mesh/src/test_advertiser.c index 073651ae843..47851fc72f4 100644 --- a/tests/bsim/bluetooth/mesh/src/test_advertiser.c +++ b/tests/bsim/bluetooth/mesh/src/test_advertiser.c @@ -7,7 +7,6 @@ #include #include #include "mesh_test.h" -#include "mesh/adv.h" #include "mesh/net.h" #include "mesh/mesh.h" #include "mesh/foundation.h" @@ -78,25 +77,25 @@ static void adv_init(void) ASSERT_OK_MSG(bt_mesh_adv_enable(), "Mesh adv init failed"); } -static void allocate_all_array(struct net_buf **buf, size_t num_buf, uint8_t xmit) +static void allocate_all_array(struct bt_mesh_adv **adv, size_t num_adv, uint8_t xmit) { - for (int i = 0; i < num_buf; i++) { - *buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + for (int i = 0; i < num_adv; i++) { + *adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, xmit, K_NO_WAIT); - ASSERT_FALSE_MSG(!*buf, "Out of buffers\n"); - buf++; + ASSERT_FALSE_MSG(!*adv, "Out of advs\n"); + adv++; } } static void verify_adv_queue_overflow(void) { - struct net_buf *dummy_buf; + struct bt_mesh_adv *dummy_adv; /* Verity Queue overflow */ - dummy_buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + dummy_adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, BT_MESH_TRANSMIT(2, 20), K_NO_WAIT); - ASSERT_TRUE_MSG(!dummy_buf, "Unexpected extra buffer\n"); + ASSERT_TRUE_MSG(!dummy_adv, "Unexpected extra adv\n"); } static bool check_delta_time(uint8_t transmit, uint64_t interval) @@ -157,12 +156,12 @@ static void single_end_cb(int err, void *cb_data) static void realloc_end_cb(int err, void *cb_data) { - struct net_buf *buf = (struct net_buf *)cb_data; + struct bt_mesh_adv *adv = (struct bt_mesh_adv *)cb_data; ASSERT_EQUAL(0, err); - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, BT_MESH_TRANSMIT(2, 20), K_NO_WAIT); - ASSERT_FALSE_MSG(!buf, "Out of buffers\n"); + ASSERT_FALSE_MSG(!adv, "Out of advs\n"); k_sem_give(&observer_sem); } @@ -305,13 +304,13 @@ static void rx_xmit_adv(void) static void send_order_start_cb(uint16_t duration, int err, void *user_data) { - struct net_buf *buf = (struct net_buf *)user_data; + struct bt_mesh_adv *adv = (struct bt_mesh_adv *)user_data; ASSERT_OK_MSG(err, "Failed adv start cb err (%d)", err); - ASSERT_EQUAL(2, buf->len); + ASSERT_EQUAL(2, adv->b.len); - uint8_t current = buf->data[0]; - uint8_t previous = buf->data[1]; + uint8_t current = adv->b.data[0]; + uint8_t previous = adv->b.data[1]; LOG_INF("tx start: current(%d) previous(%d)", current, previous); @@ -321,10 +320,7 @@ static void send_order_start_cb(uint16_t duration, int err, void *user_data) static void send_order_end_cb(int err, void *user_data) { - struct net_buf *buf = (struct net_buf *)user_data; - ASSERT_OK_MSG(err, "Failed adv start cb err (%d)", err); - ASSERT_TRUE_MSG(!buf->data, "Data not cleared!\n"); seq_checker++; LOG_INF("tx end: seq(%d)", seq_checker); @@ -380,19 +376,19 @@ static void receive_order(int expect_adv) ASSERT_FALSE_MSG(err && err != -EALREADY, "Stopping scan failed (err %d)\n", err); } -static void send_adv_buf(struct net_buf *buf, uint8_t curr, uint8_t prev) +static void send_adv_buf(struct bt_mesh_adv *adv, uint8_t curr, uint8_t prev) { send_cb.start = send_order_start_cb; send_cb.end = send_order_end_cb; - (void)net_buf_add_u8(buf, curr); - (void)net_buf_add_u8(buf, prev); + (void)net_buf_simple_add_u8(&adv->b, curr); + (void)net_buf_simple_add_u8(&adv->b, prev); - bt_mesh_adv_send(buf, &send_cb, buf); - net_buf_unref(buf); + bt_mesh_adv_send(adv, &send_cb, adv); + bt_mesh_adv_unref(adv); } -static void send_adv_array(struct net_buf **buf, size_t num_buf, bool reverse) +static void send_adv_array(struct bt_mesh_adv **adv, size_t num_buf, bool reverse) { uint8_t previous; int i; @@ -405,13 +401,13 @@ static void send_adv_array(struct net_buf **buf, size_t num_buf, bool reverse) i = num_buf - 1; } while ((!reverse && i < num_buf) || (reverse && i >= 0)) { - send_adv_buf(*buf, (uint8_t)i, previous); + send_adv_buf(*adv, (uint8_t)i, previous); previous = (uint8_t)i; if (!reverse) { - buf++; + adv++; i++; } else { - buf--; + adv--; i--; } } @@ -419,24 +415,24 @@ static void send_adv_array(struct net_buf **buf, size_t num_buf, bool reverse) static void test_tx_cb_single(void) { - struct net_buf *buf; + struct bt_mesh_adv *adv; int err; bt_init(); adv_init(); - buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, BT_MESH_TRANSMIT(2, 20), K_NO_WAIT); - ASSERT_FALSE_MSG(!buf, "Out of buffers\n"); + ASSERT_FALSE_MSG(!adv, "Out of advs\n"); send_cb.start = single_start_cb; send_cb.end = single_end_cb; - net_buf_add_mem(buf, txt_msg, sizeof(txt_msg)); + net_buf_simple_add_mem(&adv->b, txt_msg, sizeof(txt_msg)); seq_checker = 0; tx_timestamp = k_uptime_get(); - bt_mesh_adv_send(buf, &send_cb, (void *)cb_msg); - net_buf_unref(buf); + bt_mesh_adv_send(adv, &send_cb, (void *)cb_msg); + bt_mesh_adv_unref(adv); err = k_sem_take(&observer_sem, K_SECONDS(1)); ASSERT_OK_MSG(err, "Didn't call end tx cb."); @@ -457,37 +453,37 @@ static void test_rx_xmit(void) static void test_tx_cb_multi(void) { - struct net_buf *buf[CONFIG_BT_MESH_ADV_BUF_COUNT]; + struct bt_mesh_adv *adv[CONFIG_BT_MESH_ADV_BUF_COUNT]; int err; bt_init(); adv_init(); - /* Allocate all network buffers. */ - allocate_all_array(buf, ARRAY_SIZE(buf), BT_MESH_TRANSMIT(2, 20)); + /* Allocate all network advs. */ + allocate_all_array(adv, ARRAY_SIZE(adv), BT_MESH_TRANSMIT(2, 20)); - /* Start single adv to reallocate one network buffer in callback. - * Check that the buffer is freed before cb is triggered. + /* Start single adv to reallocate one network adv in callback. + * Check that the adv is freed before cb is triggered. */ send_cb.start = NULL; send_cb.end = realloc_end_cb; - net_buf_add_mem(buf[0], txt_msg, sizeof(txt_msg)); + net_buf_simple_add_mem(&(adv[0]->b), txt_msg, sizeof(txt_msg)); - bt_mesh_adv_send(buf[0], &send_cb, buf[0]); - net_buf_unref(buf[0]); + bt_mesh_adv_send(adv[0], &send_cb, adv[0]); + bt_mesh_adv_unref(adv[0]); err = k_sem_take(&observer_sem, K_SECONDS(1)); - ASSERT_OK_MSG(err, "Didn't call the end tx cb that reallocates buffer one more time."); + ASSERT_OK_MSG(err, "Didn't call the end tx cb that reallocates adv one more time."); - /* Start multi advs to check that all buffers are sent and cbs are triggered. */ + /* Start multi advs to check that all advs are sent and cbs are triggered. */ send_cb.start = seq_start_cb; send_cb.end = seq_end_cb; seq_checker = 0; for (int i = 0; i < CONFIG_BT_MESH_ADV_BUF_COUNT; i++) { - net_buf_add_le32(buf[i], i); - bt_mesh_adv_send(buf[i], &send_cb, (void *)(intptr_t)i); - net_buf_unref(buf[i]); + net_buf_simple_add_le32(&(adv[i]->b), i); + bt_mesh_adv_send(adv[i], &send_cb, (void *)(intptr_t)i); + bt_mesh_adv_unref(adv[i]); } err = k_sem_take(&observer_sem, K_SECONDS(10)); @@ -530,10 +526,10 @@ static void test_tx_proxy_mixin(void) * Advertising the proxy service should be resumed after * finishing advertising the message. */ - struct net_buf *buf = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + struct bt_mesh_adv *adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, BT_MESH_TRANSMIT(5, 20), K_NO_WAIT); - net_buf_add_mem(buf, txt_msg, sizeof(txt_msg)); - bt_mesh_adv_send(buf, NULL, NULL); + net_buf_simple_add_mem(&adv->b, txt_msg, sizeof(txt_msg)); + bt_mesh_adv_send(adv, NULL, NULL); k_sleep(K_MSEC(150)); /* Let the tester to measure an interval between advertisements again. */ @@ -577,46 +573,46 @@ static void test_rx_proxy_mixin(void) static void test_tx_send_order(void) { - struct net_buf *buf[CONFIG_BT_MESH_ADV_BUF_COUNT]; + struct bt_mesh_adv *adv[CONFIG_BT_MESH_ADV_BUF_COUNT]; uint8_t xmit = BT_MESH_TRANSMIT(2, 20); bt_init(); adv_init(); /* Verify sending order */ - allocate_all_array(buf, ARRAY_SIZE(buf), xmit); + allocate_all_array(adv, ARRAY_SIZE(adv), xmit); verify_adv_queue_overflow(); - send_adv_array(&buf[0], ARRAY_SIZE(buf), false); + send_adv_array(&adv[0], ARRAY_SIZE(adv), false); /* Wait for no message receive window to end. */ ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), "Didn't call the last end tx cb."); - /* Verify buffer allocation/deallocation after sending */ - allocate_all_array(buf, ARRAY_SIZE(buf), xmit); + /* Verify adv allocation/deallocation after sending */ + allocate_all_array(adv, ARRAY_SIZE(adv), xmit); verify_adv_queue_overflow(); for (int i = 0; i < CONFIG_BT_MESH_ADV_BUF_COUNT; i++) { - net_buf_unref(buf[i]); - buf[i] = NULL; + bt_mesh_adv_unref(adv[i]); + adv[i] = NULL; } - /* Check that it possible to add just one net buf. */ - allocate_all_array(buf, 1, xmit); + /* Check that it possible to add just one net adv. */ + allocate_all_array(adv, 1, xmit); PASS(); } static void test_tx_reverse_order(void) { - struct net_buf *buf[CONFIG_BT_MESH_ADV_BUF_COUNT]; + struct bt_mesh_adv *adv[CONFIG_BT_MESH_ADV_BUF_COUNT]; uint8_t xmit = BT_MESH_TRANSMIT(2, 20); bt_init(); adv_init(); /* Verify reversed sending order */ - allocate_all_array(buf, ARRAY_SIZE(buf), xmit); + allocate_all_array(adv, ARRAY_SIZE(adv), xmit); - send_adv_array(&buf[CONFIG_BT_MESH_ADV_BUF_COUNT - 1], ARRAY_SIZE(buf), true); + send_adv_array(&adv[CONFIG_BT_MESH_ADV_BUF_COUNT - 1], ARRAY_SIZE(adv), true); /* Wait for no message receive window to end. */ ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), @@ -627,31 +623,31 @@ static void test_tx_reverse_order(void) static void test_tx_random_order(void) { - struct net_buf *buf[3]; + struct bt_mesh_adv *adv[3]; uint8_t xmit = BT_MESH_TRANSMIT(0, 20); bt_init(); adv_init(); /* Verify random order calls */ - num_adv_sent = ARRAY_SIZE(buf); + num_adv_sent = ARRAY_SIZE(adv); previous_checker = 0xff; - buf[0] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv[0] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, xmit, K_NO_WAIT); - ASSERT_FALSE_MSG(!buf[0], "Out of buffers\n"); - buf[1] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + ASSERT_FALSE_MSG(!adv[0], "Out of advs\n"); + adv[1] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, xmit, K_NO_WAIT); - ASSERT_FALSE_MSG(!buf[1], "Out of buffers\n"); + ASSERT_FALSE_MSG(!adv[1], "Out of advs\n"); - send_adv_buf(buf[0], 0, 0xff); + send_adv_buf(adv[0], 0, 0xff); - buf[2] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + adv[2] = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, xmit, K_NO_WAIT); - ASSERT_FALSE_MSG(!buf[2], "Out of buffers\n"); + ASSERT_FALSE_MSG(!adv[2], "Out of advs\n"); - send_adv_buf(buf[2], 2, 0); + send_adv_buf(adv[2], 2, 0); - send_adv_buf(buf[1], 1, 2); + send_adv_buf(adv[1], 1, 2); /* Wait for no message receive window to end. */ ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), diff --git a/tests/bsim/bluetooth/mesh/src/test_beacon.c b/tests/bsim/bluetooth/mesh/src/test_beacon.c index e93c2301e46..038c705f2cb 100644 --- a/tests/bsim/bluetooth/mesh/src/test_beacon.c +++ b/tests/bsim/bluetooth/mesh/src/test_beacon.c @@ -6,7 +6,6 @@ #include #include #include "mesh_test.h" -#include "mesh/adv.h" #include "mesh/net.h" #include "mesh/beacon.h" #include "mesh/mesh.h" diff --git a/tests/bsim/bluetooth/mesh/src/test_blob.c b/tests/bsim/bluetooth/mesh/src/test_blob.c index 4258ff77c62..0d3a69da2fc 100644 --- a/tests/bsim/bluetooth/mesh/src/test_blob.c +++ b/tests/bsim/bluetooth/mesh/src/test_blob.c @@ -6,9 +6,9 @@ #include "mesh_test.h" #include "dfu_blob_common.h" #include "friendship_common.h" +#include "mesh/adv.h" #include "mesh/blob.h" #include "argparse.h" -#include "mesh/adv.h" #define LOG_MODULE_NAME test_blob diff --git a/tests/bsim/bluetooth/mesh/src/test_dfu.c b/tests/bsim/bluetooth/mesh/src/test_dfu.c index 06f7248a6ad..1b54e873cc5 100644 --- a/tests/bsim/bluetooth/mesh/src/test_dfu.c +++ b/tests/bsim/bluetooth/mesh/src/test_dfu.c @@ -6,7 +6,6 @@ #include "mesh_test.h" #include "mesh/dfd_srv_internal.h" #include "mesh/dfu_slot.h" -#include "mesh/adv.h" #include "mesh/dfu.h" #include "mesh/blob.h" #include "argparse.h" diff --git a/tests/bsim/bluetooth/mesh/src/test_provision.c b/tests/bsim/bluetooth/mesh/src/test_provision.c index 0d7f1811833..9a83f874e11 100644 --- a/tests/bsim/bluetooth/mesh/src/test_provision.c +++ b/tests/bsim/bluetooth/mesh/src/test_provision.c @@ -28,7 +28,6 @@ #define LOG_MODULE_NAME mesh_prov #include -#include "mesh/adv.h" #include "mesh/rpr.h" LOG_MODULE_REGISTER(LOG_MODULE_NAME); diff --git a/tests/bsim/bluetooth/mesh/src/test_scanner.c b/tests/bsim/bluetooth/mesh/src/test_scanner.c index 12557b7e1b2..a63284701df 100644 --- a/tests/bsim/bluetooth/mesh/src/test_scanner.c +++ b/tests/bsim/bluetooth/mesh/src/test_scanner.c @@ -6,7 +6,6 @@ #include #include "mesh_test.h" #include "mesh/net.h" -#include "mesh/adv.h" #include "mesh/mesh.h" #include "mesh/foundation.h"