2020-11-13 16:21:32 +01:00
|
|
|
/*
|
2021-11-03 17:05:10 +08:00
|
|
|
* Copyright (c) 2021 Xiaomi Corporation
|
2020-11-13 16:21:32 +01:00
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/debug/stack.h>
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/net/buf.h>
|
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
2022-11-02 14:31:13 +01:00
|
|
|
#include <zephyr/bluetooth/hci.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/bluetooth/mesh.h>
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2020-11-13 16:21:32 +01:00
|
|
|
|
|
|
|
#include "host/hci_core.h"
|
|
|
|
|
|
|
|
#include "adv.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#define LOG_LEVEL CONFIG_BT_MESH_ADV_LOG_LEVEL
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(bt_mesh_adv_ext);
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
/* Convert from ms to 0.625ms units */
|
|
|
|
#define ADV_INT_FAST_MS 20
|
|
|
|
|
2022-03-01 16:40:27 +01:00
|
|
|
#ifndef CONFIG_BT_MESH_RELAY_ADV_SETS
|
|
|
|
#define CONFIG_BT_MESH_RELAY_ADV_SETS 0
|
|
|
|
#endif
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
enum {
|
|
|
|
/** Controller is currently advertising */
|
|
|
|
ADV_FLAG_ACTIVE,
|
2022-03-23 14:54:01 +08:00
|
|
|
/** Advertising sending completed */
|
|
|
|
ADV_FLAG_SENT,
|
2020-11-13 16:21:32 +01:00
|
|
|
/** Currently performing proxy advertising */
|
|
|
|
ADV_FLAG_PROXY,
|
|
|
|
/** The send-call has been scheduled. */
|
|
|
|
ADV_FLAG_SCHEDULED,
|
2022-02-28 10:32:52 +08:00
|
|
|
/** The send-call has been pending. */
|
|
|
|
ADV_FLAG_SCHEDULE_PENDING,
|
2020-11-13 16:21:32 +01:00
|
|
|
/** Custom adv params have been set, we need to update the parameters on
|
|
|
|
* the next send.
|
|
|
|
*/
|
|
|
|
ADV_FLAG_UPDATE_PARAMS,
|
|
|
|
|
|
|
|
/* Number of adv flags. */
|
|
|
|
ADV_FLAGS_NUM
|
|
|
|
};
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
struct bt_mesh_ext_adv {
|
2021-11-23 11:23:06 +08:00
|
|
|
uint8_t tag;
|
2020-11-13 16:21:32 +01:00
|
|
|
ATOMIC_DEFINE(flags, ADV_FLAGS_NUM);
|
|
|
|
struct bt_le_ext_adv *instance;
|
2021-05-26 20:32:18 -07:00
|
|
|
struct net_buf *buf;
|
2020-11-13 16:21:32 +01:00
|
|
|
uint64_t timestamp;
|
2021-03-29 08:28:43 -05:00
|
|
|
struct k_work_delayable work;
|
2021-11-03 17:05:10 +08:00
|
|
|
struct bt_le_adv_param adv_param;
|
|
|
|
};
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
static void send_pending_adv(struct k_work *work);
|
2022-02-28 10:32:52 +08:00
|
|
|
static bool schedule_send(struct bt_mesh_ext_adv *adv);
|
2021-11-23 14:18:01 +08:00
|
|
|
|
|
|
|
static STRUCT_SECTION_ITERABLE(bt_mesh_ext_adv, adv_main) = {
|
2022-11-10 12:23:07 +08:00
|
|
|
.tag = (
|
2023-01-11 10:35:46 +01:00
|
|
|
#if !defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE)
|
|
|
|
BT_MESH_FRIEND_ADV |
|
|
|
|
#endif
|
2021-11-23 11:23:06 +08:00
|
|
|
#if !defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
|
|
|
|
BT_MESH_PROXY_ADV |
|
|
|
|
#endif /* !CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
|
2022-11-10 12:23:07 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_ADV_EXT_RELAY_USING_MAIN_ADV_SET)
|
|
|
|
BT_MESH_RELAY_ADV |
|
|
|
|
#endif /* CONFIG_BT_MESH_ADV_EXT_RELAY_USING_MAIN_ADV_SET */
|
|
|
|
BT_MESH_LOCAL_ADV),
|
2021-11-23 11:23:06 +08:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
|
|
|
|
};
|
|
|
|
|
2021-11-23 11:23:06 +08:00
|
|
|
#if CONFIG_BT_MESH_RELAY_ADV_SETS
|
2021-11-23 14:18:01 +08:00
|
|
|
static STRUCT_SECTION_ITERABLE(bt_mesh_ext_adv, adv_relay[CONFIG_BT_MESH_RELAY_ADV_SETS]) = {
|
2021-11-23 11:23:06 +08:00
|
|
|
[0 ... CONFIG_BT_MESH_RELAY_ADV_SETS - 1] = {
|
|
|
|
.tag = BT_MESH_RELAY_ADV,
|
2021-11-03 17:05:10 +08:00
|
|
|
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
|
|
|
|
}
|
|
|
|
};
|
2021-11-23 11:23:06 +08:00
|
|
|
#endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
|
2021-11-03 17:05:10 +08:00
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
#if defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE)
|
|
|
|
#define ADV_EXT_FRIEND 1
|
|
|
|
static STRUCT_SECTION_ITERABLE(bt_mesh_ext_adv, adv_friend) = {
|
|
|
|
.tag = BT_MESH_FRIEND_ADV,
|
|
|
|
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
|
|
|
|
};
|
|
|
|
#else /* CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE */
|
|
|
|
#define ADV_EXT_FRIEND 0
|
|
|
|
#endif /* CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE */
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
|
2023-01-11 10:35:46 +01:00
|
|
|
#define ADV_EXT_GATT 1
|
2021-11-23 14:18:01 +08:00
|
|
|
static STRUCT_SECTION_ITERABLE(bt_mesh_ext_adv, adv_gatt) = {
|
2021-11-23 11:23:06 +08:00
|
|
|
.tag = BT_MESH_PROXY_ADV,
|
|
|
|
.work = Z_WORK_DELAYABLE_INITIALIZER(send_pending_adv),
|
2021-11-03 17:05:10 +08:00
|
|
|
};
|
2021-11-23 14:27:31 +08:00
|
|
|
#else /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
|
2023-01-11 10:35:46 +01:00
|
|
|
#define ADV_EXT_GATT 0
|
2021-11-03 17:05:10 +08:00
|
|
|
#endif /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
|
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
#define BT_MESH_ADV_COUNT (1 + CONFIG_BT_MESH_RELAY_ADV_SETS + ADV_EXT_FRIEND + ADV_EXT_GATT)
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
BUILD_ASSERT(CONFIG_BT_EXT_ADV_MAX_ADV_SET >= BT_MESH_ADV_COUNT,
|
|
|
|
"Insufficient adv instances");
|
|
|
|
|
2021-11-23 14:27:31 +08:00
|
|
|
static inline struct bt_mesh_ext_adv *relay_adv_get(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_BT_MESH_RELAY_ADV_SETS
|
|
|
|
return adv_relay;
|
|
|
|
#else /* !CONFIG_BT_MESH_RELAY_ADV_SETS */
|
|
|
|
return &adv_main;
|
|
|
|
#endif /* CONFIG_BT_MESH_RELAY_ADV_SETS */
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bt_mesh_ext_adv *gatt_adv_get(void)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE)
|
|
|
|
return &adv_gatt;
|
|
|
|
#else /* !CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
|
|
|
|
return &adv_main;
|
|
|
|
#endif /* CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE */
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
static int adv_start(struct bt_mesh_ext_adv *adv,
|
2021-11-03 17:05:10 +08:00
|
|
|
const struct bt_le_adv_param *param,
|
2020-11-13 16:21:32 +01:00
|
|
|
struct bt_le_ext_adv_start_param *start,
|
|
|
|
const struct bt_data *ad, size_t ad_len,
|
|
|
|
const struct bt_data *sd, size_t sd_len)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
if (!adv->instance) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Mesh advertiser not enabled");
|
2020-11-13 16:21:32 +01:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
if (atomic_test_and_set_bit(adv->flags, ADV_FLAG_ACTIVE)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Advertiser is busy");
|
2020-11-13 16:21:32 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
if (atomic_test_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS)) {
|
|
|
|
err = bt_le_ext_adv_update_param(adv->instance, param);
|
2020-11-13 16:21:32 +01:00
|
|
|
if (err) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed updating adv params: %d", err);
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
2020-11-13 16:21:32 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_set_bit_to(adv->flags, ADV_FLAG_UPDATE_PARAMS,
|
|
|
|
param != &adv->adv_param);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
err = bt_le_ext_adv_set_data(adv->instance, ad, ad_len, sd, sd_len);
|
2020-11-13 16:21:32 +01:00
|
|
|
if (err) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed setting adv data: %d", err);
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
2020-11-13 16:21:32 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
adv->timestamp = k_uptime_get();
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
err = bt_le_ext_adv_start(adv->instance, start);
|
2020-11-13 16:21:32 +01:00
|
|
|
if (err) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Advertising failed: err %d", err);
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
static int buf_send(struct bt_mesh_ext_adv *adv, struct net_buf *buf)
|
2020-11-13 16:21:32 +01:00
|
|
|
{
|
|
|
|
struct bt_le_ext_adv_start_param start = {
|
|
|
|
.num_events =
|
|
|
|
BT_MESH_TRANSMIT_COUNT(BT_MESH_ADV(buf)->xmit) + 1,
|
|
|
|
};
|
|
|
|
uint16_t duration, adv_int;
|
|
|
|
struct bt_data ad;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
adv_int = MAX(ADV_INT_FAST_MS,
|
|
|
|
BT_MESH_TRANSMIT_INT(BT_MESH_ADV(buf)->xmit));
|
|
|
|
/* Upper boundary estimate: */
|
|
|
|
duration = start.num_events * (adv_int + 10);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("type %u len %u: %s", BT_MESH_ADV(buf)->type, buf->len,
|
|
|
|
bt_hex(buf->data, buf->len));
|
|
|
|
LOG_DBG("count %u interval %ums duration %ums",
|
|
|
|
BT_MESH_TRANSMIT_COUNT(BT_MESH_ADV(buf)->xmit) + 1, adv_int, duration);
|
2020-11-13 16:21:32 +01:00
|
|
|
|
|
|
|
ad.type = bt_mesh_adv_type[BT_MESH_ADV(buf)->type];
|
|
|
|
ad.data_len = buf->len;
|
|
|
|
ad.data = buf->data;
|
|
|
|
|
|
|
|
/* Only update advertising parameters if they're different */
|
2021-11-03 17:05:10 +08:00
|
|
|
if (adv->adv_param.interval_min != BT_MESH_ADV_SCAN_UNIT(adv_int)) {
|
|
|
|
adv->adv_param.interval_min = BT_MESH_ADV_SCAN_UNIT(adv_int);
|
|
|
|
adv->adv_param.interval_max = adv->adv_param.interval_min;
|
|
|
|
atomic_set_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
err = adv_start(adv, &adv->adv_param, &start, &ad, 1, NULL, 0);
|
2021-05-26 20:32:18 -07:00
|
|
|
if (!err) {
|
2021-11-03 17:05:10 +08:00
|
|
|
adv->buf = net_buf_ref(buf);
|
2021-05-26 20:32:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_adv_send_start(duration, err, BT_MESH_ADV(buf));
|
2020-11-13 16:21:32 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
static const char *adv_tag_to_str(enum bt_mesh_adv_tag tag)
|
|
|
|
{
|
|
|
|
if (tag & BT_MESH_LOCAL_ADV) {
|
|
|
|
return "local adv";
|
|
|
|
} else if (tag & BT_MESH_PROXY_ADV) {
|
|
|
|
return "proxy adv";
|
|
|
|
} else if (tag & BT_MESH_RELAY_ADV) {
|
|
|
|
return "relay adv";
|
|
|
|
} else if (tag & BT_MESH_FRIEND_ADV) {
|
|
|
|
return "friend adv";
|
|
|
|
} else {
|
|
|
|
return "(unknown tag)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
static void send_pending_adv(struct k_work *work)
|
|
|
|
{
|
2021-11-23 14:18:01 +08:00
|
|
|
struct bt_mesh_ext_adv *adv;
|
2020-11-13 16:21:32 +01:00
|
|
|
struct net_buf *buf;
|
|
|
|
int err;
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
adv = CONTAINER_OF(work, struct bt_mesh_ext_adv, work.work);
|
2022-03-23 14:54:01 +08:00
|
|
|
|
|
|
|
if (atomic_test_and_clear_bit(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);
|
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
LOG_DBG("Advertising stopped after %u ms for (%u) %s", (uint32_t)duration, adv->tag,
|
|
|
|
adv_tag_to_str(adv->tag));
|
2022-03-23 14:54:01 +08:00
|
|
|
|
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_PROXY);
|
|
|
|
|
|
|
|
if (adv->buf) {
|
|
|
|
net_buf_unref(adv->buf);
|
|
|
|
adv->buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)schedule_send(adv);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULED);
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-23 11:23:06 +08:00
|
|
|
while ((buf = bt_mesh_adv_buf_get_by_tag(adv->tag, K_NO_WAIT))) {
|
2020-11-13 16:21:32 +01:00
|
|
|
/* busy == 0 means this was canceled */
|
|
|
|
if (!BT_MESH_ADV(buf)->busy) {
|
|
|
|
net_buf_unref(buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_MESH_ADV(buf)->busy = 0U;
|
2021-11-03 17:05:10 +08:00
|
|
|
err = buf_send(adv, buf);
|
2021-05-26 20:32:18 -07:00
|
|
|
|
|
|
|
net_buf_unref(buf);
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
if (!err) {
|
|
|
|
return; /* Wait for advertising to finish */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:32:52 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) ||
|
|
|
|
!(adv->tag & BT_MESH_PROXY_ADV)) {
|
2021-11-23 11:23:06 +08:00
|
|
|
return;
|
2021-11-18 10:13:03 +08:00
|
|
|
}
|
2022-02-28 10:32:52 +08:00
|
|
|
|
|
|
|
if (!bt_mesh_adv_gatt_send()) {
|
|
|
|
atomic_set_bit(adv->flags, ADV_FLAG_PROXY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING)) {
|
|
|
|
schedule_send(adv);
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
static bool schedule_send(struct bt_mesh_ext_adv *adv)
|
2020-11-13 16:21:32 +01:00
|
|
|
{
|
2021-11-03 17:05:10 +08:00
|
|
|
uint64_t timestamp;
|
2020-11-13 16:21:32 +01:00
|
|
|
int64_t delta;
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
if (!adv) {
|
|
|
|
return false;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
timestamp = adv->timestamp;
|
|
|
|
|
|
|
|
if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_PROXY)) {
|
|
|
|
(void)bt_le_ext_adv_stop(adv->instance);
|
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:32:52 +08:00
|
|
|
if (atomic_test_bit(adv->flags, ADV_FLAG_ACTIVE)) {
|
|
|
|
atomic_set_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING);
|
|
|
|
return false;
|
|
|
|
} else if (atomic_test_and_set_bit(adv->flags, ADV_FLAG_SCHEDULED)) {
|
2021-11-03 17:05:10 +08:00
|
|
|
return false;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:32:52 +08:00
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_SCHEDULE_PENDING);
|
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE) && adv->tag & BT_MESH_FRIEND_ADV) {
|
|
|
|
k_work_reschedule(&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));
|
|
|
|
}
|
2021-11-03 17:05:10 +08:00
|
|
|
|
|
|
|
return true;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-18 09:23:08 +08:00
|
|
|
void bt_mesh_adv_gatt_update(void)
|
2020-11-13 16:21:32 +01:00
|
|
|
{
|
2021-11-23 14:27:31 +08:00
|
|
|
(void)schedule_send(gatt_adv_get());
|
2021-11-03 17:05:10 +08:00
|
|
|
}
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
void bt_mesh_adv_buf_local_ready(void)
|
|
|
|
{
|
|
|
|
(void)schedule_send(&adv_main);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
void bt_mesh_adv_buf_relay_ready(void)
|
2020-11-13 16:21:32 +01:00
|
|
|
{
|
2021-11-03 17:05:10 +08:00
|
|
|
struct bt_mesh_ext_adv *adv = relay_adv_get();
|
|
|
|
|
2021-11-23 11:23:06 +08:00
|
|
|
for (int i = 0; i < CONFIG_BT_MESH_RELAY_ADV_SETS; i++) {
|
2021-11-03 17:05:10 +08:00
|
|
|
if (schedule_send(&adv[i])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 14:44:12 +08:00
|
|
|
|
|
|
|
/* Attempt to use the main adv set for the sending of relay messages. */
|
2022-11-10 12:23:07 +08:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MESH_ADV_EXT_RELAY_USING_MAIN_ADV_SET)) {
|
|
|
|
(void)schedule_send(&adv_main);
|
|
|
|
}
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 10:35:46 +01:00
|
|
|
void bt_mesh_adv_buf_friend_ready(void)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_BT_MESH_ADV_EXT_FRIEND_SEPARATE)
|
|
|
|
(void)schedule_send(&adv_friend);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
void bt_mesh_adv_init(void)
|
|
|
|
{
|
2021-11-23 14:18:01 +08:00
|
|
|
struct bt_le_adv_param adv_param = {
|
|
|
|
.id = BT_ID_DEFAULT,
|
|
|
|
.interval_min = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS),
|
|
|
|
.interval_max = BT_MESH_ADV_SCAN_UNIT(ADV_INT_FAST_MS),
|
|
|
|
#if defined(CONFIG_BT_MESH_DEBUG_USE_ID_ADDR)
|
|
|
|
.options = BT_LE_ADV_OPT_USE_IDENTITY,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
STRUCT_SECTION_FOREACH(bt_mesh_ext_adv, adv) {
|
|
|
|
(void)memcpy(&adv->adv_param, &adv_param, sizeof(adv_param));
|
|
|
|
}
|
2021-11-03 17:05:10 +08:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
static struct bt_mesh_ext_adv *adv_instance_find(struct bt_le_ext_adv *instance)
|
2021-11-03 17:05:10 +08:00
|
|
|
{
|
2021-11-23 14:18:01 +08:00
|
|
|
STRUCT_SECTION_FOREACH(bt_mesh_ext_adv, adv) {
|
|
|
|
if (adv->instance == instance) {
|
|
|
|
return adv;
|
2021-11-03 17:05:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void adv_sent(struct bt_le_ext_adv *instance,
|
|
|
|
struct bt_le_ext_adv_sent_info *info)
|
|
|
|
{
|
2021-11-23 14:18:01 +08:00
|
|
|
struct bt_mesh_ext_adv *adv = adv_instance_find(instance);
|
2021-11-03 17:05:10 +08:00
|
|
|
|
|
|
|
if (!adv) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Unexpected adv instance");
|
2021-11-03 17:05:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-23 14:54:01 +08:00
|
|
|
if (!atomic_test_bit(adv->flags, ADV_FLAG_ACTIVE)) {
|
|
|
|
return;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 14:54:01 +08:00
|
|
|
atomic_set_bit(adv->flags, ADV_FLAG_SENT);
|
|
|
|
|
|
|
|
k_work_submit(&adv->work.work);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_GATT_SERVER)
|
2020-11-13 16:21:32 +01:00
|
|
|
static void connected(struct bt_le_ext_adv *instance,
|
|
|
|
struct bt_le_ext_adv_connected_info *info)
|
|
|
|
{
|
2021-11-23 14:27:31 +08:00
|
|
|
struct bt_mesh_ext_adv *adv = gatt_adv_get();
|
2021-11-03 17:05:10 +08:00
|
|
|
|
|
|
|
if (atomic_test_and_clear_bit(adv->flags, ADV_FLAG_PROXY)) {
|
|
|
|
atomic_clear_bit(adv->flags, ADV_FLAG_ACTIVE);
|
|
|
|
(void)schedule_send(adv);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 17:05:10 +08:00
|
|
|
#endif /* CONFIG_BT_MESH_GATT_SERVER */
|
2020-11-13 16:21:32 +01:00
|
|
|
|
|
|
|
int bt_mesh_adv_enable(void)
|
|
|
|
{
|
2021-11-03 17:05:10 +08:00
|
|
|
int err;
|
|
|
|
|
2020-11-13 16:21:32 +01:00
|
|
|
static const struct bt_le_ext_adv_cb adv_cb = {
|
|
|
|
.sent = adv_sent,
|
2021-11-03 17:05:10 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_GATT_SERVER)
|
2020-11-13 16:21:32 +01:00
|
|
|
.connected = connected,
|
2021-11-03 17:05:10 +08:00
|
|
|
#endif /* CONFIG_BT_MESH_GATT_SERVER */
|
2020-11-13 16:21:32 +01:00
|
|
|
};
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
if (adv_main.instance) {
|
2020-11-13 16:21:32 +01:00
|
|
|
/* Already initialized */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
STRUCT_SECTION_FOREACH(bt_mesh_ext_adv, adv) {
|
|
|
|
err = bt_le_ext_adv_create(&adv->adv_param, &adv_cb,
|
|
|
|
&adv->instance);
|
2021-11-03 17:05:10 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:18:01 +08:00
|
|
|
int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param,
|
|
|
|
int32_t duration,
|
2021-11-18 09:23:08 +08:00
|
|
|
const struct bt_data *ad, size_t ad_len,
|
|
|
|
const struct bt_data *sd, size_t sd_len)
|
2020-11-13 16:21:32 +01:00
|
|
|
{
|
2021-11-23 14:27:31 +08:00
|
|
|
struct bt_mesh_ext_adv *adv = gatt_adv_get();
|
2020-11-13 16:21:32 +01:00
|
|
|
struct bt_le_ext_adv_start_param start = {
|
|
|
|
/* Timeout is set in 10 ms steps, with 0 indicating "forever" */
|
2022-09-21 09:37:08 +02:00
|
|
|
.timeout = (duration == SYS_FOREVER_MS) ? 0 : MAX(1, duration / 10),
|
2020-11-13 16:21:32 +01:00
|
|
|
};
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Start advertising %d ms", duration);
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
atomic_set_bit(adv->flags, ADV_FLAG_UPDATE_PARAMS);
|
2020-11-13 16:21:32 +01:00
|
|
|
|
2021-11-03 17:05:10 +08:00
|
|
|
return adv_start(adv, param, &start, ad, ad_len, sd, sd_len);
|
2020-11-13 16:21:32 +01:00
|
|
|
}
|