Bluetooth: Mesh: Introduce tag field

Introduce tag field.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
This commit is contained in:
Lingao Meng 2021-11-23 11:23:06 +08:00 committed by Johan Hedberg
commit 414250106f
3 changed files with 54 additions and 53 deletions

View file

@ -146,17 +146,33 @@ struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout)
return process_events(events, ARRAY_SIZE(events)); return process_events(events, ARRAY_SIZE(events));
} }
#endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
#if CONFIG_BT_MESH_RELAY_ADV_SETS static struct net_buf *bt_mesh_adv_buf_relay_get(k_timeout_t timeout)
{
return net_buf_get(&bt_mesh_relay_queue, timeout);
}
struct net_buf *bt_mesh_adv_buf_get_by_tag(uint8_t tag, k_timeout_t timeout)
{
if (tag & BT_MESH_LOCAL_ADV) {
return bt_mesh_adv_buf_get(timeout);
} else if (tag & BT_MESH_RELAY_ADV) {
return bt_mesh_adv_buf_relay_get(timeout);
} else {
return NULL;
}
}
#else /* !CONFIG_BT_MESH_RELAY_ADV_SETS */
struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout) struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout)
{ {
return net_buf_get(&bt_mesh_adv_queue, timeout); return net_buf_get(&bt_mesh_adv_queue, timeout);
} }
struct net_buf *bt_mesh_adv_buf_relay_get(k_timeout_t timeout) struct net_buf *bt_mesh_adv_buf_get_by_tag(uint8_t tag, k_timeout_t timeout)
{ {
return net_buf_get(&bt_mesh_relay_queue, timeout); ARG_UNUSED(tag);
return bt_mesh_adv_buf_get(timeout);
} }
#endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */ #endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */

View file

@ -26,8 +26,9 @@ enum bt_mesh_adv_type {
}; };
enum bt_mesh_adv_tag { enum bt_mesh_adv_tag {
BT_MESH_LOCAL_ADV, BT_MESH_LOCAL_ADV = BIT(0),
BT_MESH_RELAY_ADV, BT_MESH_RELAY_ADV = BIT(1),
BT_MESH_PROXY_ADV = BIT(2),
}; };
struct bt_mesh_adv { struct bt_mesh_adv {
@ -37,7 +38,7 @@ struct bt_mesh_adv {
uint8_t type:2, uint8_t type:2,
started:1, started:1,
busy:1, busy:1,
tag:1; tag:3;
uint8_t xmit; uint8_t xmit;
}; };
@ -55,7 +56,7 @@ void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb,
struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout); struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout);
struct net_buf *bt_mesh_adv_buf_relay_get(k_timeout_t timeout); struct net_buf *bt_mesh_adv_buf_get_by_tag(uint8_t tag, k_timeout_t timeout);
void bt_mesh_adv_gatt_update(void); void bt_mesh_adv_gatt_update(void);

View file

@ -42,22 +42,25 @@ enum {
ADV_FLAGS_NUM ADV_FLAGS_NUM
}; };
typedef struct net_buf* (*adv_buf_handler_t)(k_timeout_t timeout);
struct ext_adv { struct ext_adv {
uint8_t tag;
ATOMIC_DEFINE(flags, ADV_FLAGS_NUM); ATOMIC_DEFINE(flags, ADV_FLAGS_NUM);
struct bt_le_ext_adv *instance; struct bt_le_ext_adv *instance;
struct net_buf *buf; struct net_buf *buf;
uint64_t timestamp; uint64_t timestamp;
struct k_work_delayable work; struct k_work_delayable work;
adv_buf_handler_t handler;
struct bt_le_adv_param adv_param; struct bt_le_adv_param adv_param;
}; };
static void send_pending_adv(struct k_work *work); static void send_pending_adv(struct k_work *work);
static struct ext_adv adv_main = { static struct ext_adv adv_main = {
.tag = (BT_MESH_LOCAL_ADV |
#if !defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
BT_MESH_PROXY_ADV |
#endif /* !CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
BT_MESH_RELAY_ADV),
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv), .work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
.handler = bt_mesh_adv_buf_get,
.adv_param = { .adv_param = {
.id = BT_ID_DEFAULT, .id = BT_ID_DEFAULT,
.interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS), .interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS),
@ -68,13 +71,11 @@ static struct ext_adv adv_main = {
} }
}; };
#if defined(CONFIG_BT_MESH_RELAY) #if CONFIG_BT_MESH_RELAY_ADV_SETS
#define RELAY_ADV_COUNT CONFIG_BT_MESH_RELAY_ADV_SETS static struct ext_adv adv_relay[CONFIG_BT_MESH_RELAY_ADV_SETS] = {
#if RELAY_ADV_COUNT [0 ... CONFIG_BT_MESH_RELAY_ADV_SETS - 1] = {
static struct ext_adv adv_relay[RELAY_ADV_COUNT] = { .tag = BT_MESH_RELAY_ADV,
[0 ... RELAY_ADV_COUNT - 1] = {
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv), .work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
.handler = bt_mesh_adv_buf_relay_get,
.adv_param = { .adv_param = {
.id = BT_ID_DEFAULT, .id = BT_ID_DEFAULT,
.interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS), .interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS),
@ -86,19 +87,15 @@ static struct ext_adv adv_relay[RELAY_ADV_COUNT] = {
} }
}; };
#define BT_MESH_RELAY_ADV_INS (adv_relay) #define BT_MESH_RELAY_ADV_INS (adv_relay)
#else /* !RELAY_ADV_COUNT */ #else /* !CONFIG_BT_MESH_RELAY_ADV_SETS */
#define BT_MESH_RELAY_ADV_INS (&adv_main) #define BT_MESH_RELAY_ADV_INS (&adv_main)
#endif /* RELAY_ADV_COUNT */ #endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
#else /* !CONFIG_BT_MESH_RELAY */
#define RELAY_ADV_COUNT 0
#define BT_MESH_RELAY_ADV_INS (struct ext_adv)NULL
#endif /* CONFIG_BT_MESH_RELAY */
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE) #if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
#define BT_MESH_ADV_COUNT (1 + RELAY_ADV_COUNT + 1) #define BT_MESH_ADV_COUNT (1 + CONFIG_BT_MESH_RELAY_ADV_SETS + 1)
static void send_pending_adv_gatt_handler(struct k_work *work);
static struct ext_adv adv_gatt = { static struct ext_adv adv_gatt = {
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv_gatt_handler), .tag = BT_MESH_PROXY_ADV,
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
.adv_param = { .adv_param = {
.id = BT_ID_DEFAULT, .id = BT_ID_DEFAULT,
.interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS), .interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS),
@ -110,10 +107,10 @@ static struct ext_adv adv_gatt = {
}; };
#define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (&adv_gatt) #define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (&adv_gatt)
#elif defined(CONFIG_BT_MESH_GATT_SERVER) #elif defined(CONFIG_BT_MESH_GATT_SERVER)
#define BT_MESH_ADV_COUNT (1 + RELAY_ADV_COUNT) #define BT_MESH_ADV_COUNT (1 + CONFIG_BT_MESH_RELAY_ADV_SETS)
#define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (&adv_main) #define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (&adv_main)
#else /* !CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */ #else /* !CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
#define BT_MESH_ADV_COUNT (1 + RELAY_ADV_COUNT) #define BT_MESH_ADV_COUNT (1 + CONFIG_BT_MESH_RELAY_ADV_SETS)
#define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (struct ext_adv)NULL #define BT_MESH_ADV_EXT_GATT_SEPARATE_INS (struct ext_adv)NULL
#endif /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */ #endif /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
@ -218,7 +215,7 @@ static void send_pending_adv(struct k_work *work)
atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULED); atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULED);
while ((buf = adv->handler(K_NO_WAIT))) { while ((buf = bt_mesh_adv_buf_get_by_tag(adv->tag, K_NO_WAIT))) {
/* busy == 0 means this was canceled */ /* busy == 0 means this was canceled */
if (!BT_MESH_ADV(buf)->busy) { if (!BT_MESH_ADV(buf)->busy) {
net_buf_unref(buf); net_buf_unref(buf);
@ -235,27 +232,14 @@ static void send_pending_adv(struct k_work *work)
} }
} }
if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) || if (IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) &&
adv != BT_MESH_ADV_EXT_GATT_SEPARATE_INS) { (adv->tag & BT_MESH_PROXY_ADV) &&
!bt_mesh_adv_gatt_send()) {
atomic_set_bit(adv->flags, ADV_FLAG_PROXY);
return; return;
} }
if (!bt_mesh_adv_gatt_send()) {
atomic_set_bit(adv->flags, ADV_FLAG_PROXY);
}
} }
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
static void send_pending_adv_gatt_handler(struct k_work *work)
{
struct ext_adv *adv = CONTAINER_OF(work, struct ext_adv, work.work);
if (!bt_mesh_adv_gatt_send()) {
atomic_set_bit(adv->flags, ADV_FLAG_PROXY);
}
}
#endif /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
static bool schedule_send(struct ext_adv *adv) static bool schedule_send(struct ext_adv *adv)
{ {
uint64_t timestamp; uint64_t timestamp;
@ -301,7 +285,7 @@ void bt_mesh_adv_buf_relay_ready(void)
{ {
struct bt_mesh_ext_adv *adv = relay_adv_get(); struct bt_mesh_ext_adv *adv = relay_adv_get();
for (int i = 0; i < RELAY_ADV_COUNT; i++) { for (int i = 0; i < CONFIG_BT_MESH_RELAY_ADV_SETS; i++) {
if (schedule_send(&adv[i])) { if (schedule_send(&adv[i])) {
return; return;
} }
@ -318,13 +302,13 @@ static struct ext_adv *adv_instance_find(struct bt_le_ext_adv *instance)
return &adv_main; return &adv_main;
} }
#if RELAY_ADV_COUNT #if CONFIG_BT_MESH_RELAY_ADV_SETS
for (int i = 0; i < ARRAY_SIZE(adv_relay); i++) { for (int i = 0; i < ARRAY_SIZE(adv_relay); i++) {
if (adv_relay[i].instance == instance) { if (adv_relay[i].instance == instance) {
return &adv_relay[i]; return &adv_relay[i];
} }
} }
#endif /* RELAY_ADV_COUNT */ #endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE) #if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
if (adv_gatt.instance == instance) { if (adv_gatt.instance == instance) {
@ -398,15 +382,15 @@ int bt_mesh_adv_enable(void)
return err; return err;
} }
#if RELAY_ADV_COUNT #if CONFIG_BT_MESH_RELAY_ADV_SETS
for (int i = 0; i < RELAY_ADV_COUNT; i++) { for (int i = 0; i < CONFIG_BT_MESH_RELAY_ADV_SETS; i++) {
err = bt_le_ext_adv_create(&adv_relay[i].adv_param, &adv_cb, err = bt_le_ext_adv_create(&adv_relay[i].adv_param, &adv_cb,
&adv_relay[i].instance); &adv_relay[i].instance);
if (err) { if (err) {
return err; return err;
} }
} }
#endif /* RELAY_ADV_COUNT */ #endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE) #if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
err = bt_le_ext_adv_create(&adv_gatt.adv_param, &adv_cb, err = bt_le_ext_adv_create(&adv_gatt.adv_param, &adv_cb,