Skip to content

Changes to bluetooth mesh for k_work API #33782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions subsys/bluetooth/mesh/adv_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static struct {
const struct bt_mesh_send_cb *cb;
void *cb_data;
uint64_t timestamp;
struct k_delayed_work work;
struct k_work_delayable work;
} adv;

static int adv_start(const struct bt_le_adv_param *param,
Expand Down Expand Up @@ -201,7 +201,7 @@ static void schedule_send(void)
* to the previous packet than what's permitted by the specification.
*/
delta = k_uptime_delta(&timestamp);
k_delayed_work_submit(&adv.work, K_MSEC(ADV_INT_FAST_MS - delta));
k_work_reschedule(&adv.work, K_MSEC(ADV_INT_FAST_MS - delta));
}

void bt_mesh_adv_update(void)
Expand All @@ -218,7 +218,7 @@ void bt_mesh_adv_buf_ready(void)

void bt_mesh_adv_init(void)
{
k_delayed_work_init(&adv.work, send_pending_adv);
k_work_init_delayable(&adv.work, send_pending_adv);
}

static void adv_sent(struct bt_le_ext_adv *instance,
Expand Down
56 changes: 37 additions & 19 deletions subsys/bluetooth/mesh/friend.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ static struct bt_mesh_adv *adv_alloc(int id)
return &adv_pool[id].adv;
}

static bool friend_is_allocated(const struct bt_mesh_friend *frnd)
{
return frnd->subnet != NULL;
}

static bool is_lpn_unicast(struct bt_mesh_friend *frnd, uint16_t addr)
{
if (frnd->lpn == BT_MESH_ADDR_UNASSIGNED) {
Expand All @@ -86,7 +91,7 @@ struct bt_mesh_friend *bt_mesh_friend_find(uint16_t net_idx, uint16_t lpn_addr,
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];

if (valid && !frnd->subnet) {
if (valid && !friend_is_allocated(frnd)) {
continue;
}

Expand Down Expand Up @@ -149,7 +154,8 @@ static void friend_clear(struct bt_mesh_friend *frnd)

BT_DBG("LPN 0x%04x", frnd->lpn);

k_delayed_work_cancel(&frnd->timer);
/* If cancelling the timer fails, we'll exit early in the work handler. */
(void)k_work_cancel_delayable(&frnd->timer);

memset(frnd->cred, 0, sizeof(frnd->cred));

Expand Down Expand Up @@ -197,7 +203,7 @@ void bt_mesh_friends_clear(void)
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];

if (!frnd->subnet) {
if (!friend_is_allocated(frnd)) {
continue;
}

Expand All @@ -216,7 +222,7 @@ void bt_mesh_friend_sec_update(uint16_t net_idx)
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];

if (!frnd->subnet) {
if (!friend_is_allocated(frnd)) {
continue;
}

Expand Down Expand Up @@ -531,7 +537,7 @@ static struct net_buf *encode_update(struct bt_mesh_friend *frnd, uint8_t md)
struct bt_mesh_ctl_friend_update *upd;
NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*upd));

__ASSERT_NO_MSG(frnd->subnet);
__ASSERT_NO_MSG(friend_is_allocated(frnd));

BT_DBG("lpn 0x%04x md 0x%02x", frnd->lpn, md);

Expand Down Expand Up @@ -582,7 +588,7 @@ static void friend_recv_delay(struct bt_mesh_friend *frnd)
int32_t delay = recv_delay(frnd);

frnd->pending_req = 1U;
k_delayed_work_submit(&frnd->timer, K_MSEC(delay));
k_work_reschedule(&frnd->timer, K_MSEC(delay));
BT_DBG("Waiting RecvDelay of %d ms", delay);
}

Expand Down Expand Up @@ -762,8 +768,8 @@ static void friend_clear_sent(int err, void *user_data)
{
struct bt_mesh_friend *frnd = user_data;

k_delayed_work_submit(&frnd->clear.timer,
K_SECONDS(frnd->clear.repeat_sec));
k_work_reschedule(&frnd->clear.timer,
K_SECONDS(frnd->clear.repeat_sec));
frnd->clear.repeat_sec *= 2U;
}

Expand Down Expand Up @@ -798,10 +804,16 @@ static void send_friend_clear(struct bt_mesh_friend *frnd)

static void clear_timeout(struct k_work *work)
{
struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend,
clear.timer.work);
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct bt_mesh_friend *frnd = CONTAINER_OF(dwork, struct bt_mesh_friend,
clear.timer);
uint32_t duration;

if (frnd->clear.frnd == BT_MESH_ADDR_UNASSIGNED) {
/* Failed cancelling timer, return early. */
return;
}

BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd);

duration = k_uptime_get_32() - frnd->clear.start;
Expand Down Expand Up @@ -858,7 +870,8 @@ int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx,
return 0;
}

k_delayed_work_cancel(&frnd->clear.timer);
/* If this fails, the unassigned check will make the handler return early. */
(void)k_work_cancel_delayable(&frnd->clear.timer);
frnd->clear.frnd = BT_MESH_ADDR_UNASSIGNED;

return 0;
Expand Down Expand Up @@ -1028,7 +1041,7 @@ int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
}

delay = offer_delay(frnd, rx->ctx.recv_rssi, msg->criteria);
k_delayed_work_submit(&frnd->timer, K_MSEC(delay));
k_work_reschedule(&frnd->timer, K_MSEC(delay));

enqueue_offer(frnd, rx->ctx.recv_rssi);

Expand Down Expand Up @@ -1145,11 +1158,12 @@ static void buf_send_end(int err, void *user_data)
}

if (frnd->established) {
k_delayed_work_submit(&frnd->timer, K_MSEC(frnd->poll_to));
/* Always restart poll timeout timer after sending */
k_work_reschedule(&frnd->timer, K_MSEC(frnd->poll_to));
BT_DBG("Waiting %u ms for next poll", frnd->poll_to);
} else {
/* Friend offer timeout is 1 second */
k_delayed_work_submit(&frnd->timer, K_SECONDS(1));
k_work_reschedule(&frnd->timer, K_SECONDS(1));
BT_DBG("Waiting for first poll");
}
}
Expand Down Expand Up @@ -1188,15 +1202,19 @@ static void update_overwrite(struct net_buf *buf, uint8_t md)

static void friend_timeout(struct k_work *work)
{
struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend,
timer.work);
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct bt_mesh_friend *frnd = CONTAINER_OF(dwork, struct bt_mesh_friend,
timer);
static const struct bt_mesh_send_cb buf_sent_cb = {
.start = buf_send_start,
.end = buf_send_end,
};

uint8_t md;

if (!friend_is_allocated(frnd)) {
return;
}

__ASSERT_NO_MSG(frnd->pending_buf == 0U);

BT_DBG("lpn 0x%04x send_last %u last %p", frnd->lpn,
Expand Down Expand Up @@ -1301,8 +1319,8 @@ int bt_mesh_friend_init(void)

sys_slist_init(&frnd->queue);

k_delayed_work_init(&frnd->timer, friend_timeout);
k_delayed_work_init(&frnd->clear.timer, clear_timeout);
k_work_init_delayable(&frnd->timer, friend_timeout);
k_work_init_delayable(&frnd->clear.timer, clear_timeout);

for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) {
sys_slist_init(&frnd->seg[j].queue);
Expand Down
6 changes: 5 additions & 1 deletion subsys/bluetooth/mesh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ void bt_mesh_reset(void)

memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));

k_delayed_work_cancel(&bt_mesh.ivu_timer);
/* If this fails, the work handler will return early on the next
* execution, as the device is not provisioned. If the device is
* reprovisioned, the timer is always restarted.
*/
(void)k_work_cancel_delayable(&bt_mesh.ivu_timer);

bt_mesh_cfg_reset();
bt_mesh_trans_reset();
Expand Down
12 changes: 8 additions & 4 deletions subsys/bluetooth/mesh/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update)
bt_mesh.seq = 0U;
}

k_delayed_work_submit(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);

/* Notify other modules */
if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
Expand Down Expand Up @@ -844,6 +844,10 @@ void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,

static void ivu_refresh(struct k_work *work)
{
if (!bt_mesh_is_provisioned()) {
return;
}

bt_mesh.ivu_duration = MIN(UINT8_MAX,
bt_mesh.ivu_duration + BT_MESH_IVU_HOURS);

Expand All @@ -857,7 +861,7 @@ static void ivu_refresh(struct k_work *work)
store_iv(true);
}

k_delayed_work_submit(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
return;
}

Expand All @@ -871,7 +875,7 @@ static void ivu_refresh(struct k_work *work)

void bt_mesh_net_init(void)
{
k_delayed_work_init(&bt_mesh.ivu_timer, ivu_refresh);
k_work_init_delayable(&bt_mesh.ivu_timer, ivu_refresh);

k_work_init(&bt_mesh.local_work, bt_mesh_net_local);
}
Expand Down Expand Up @@ -1080,6 +1084,6 @@ void bt_mesh_net_clear(void)
void bt_mesh_net_settings_commit(void)
{
if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
k_delayed_work_submit(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
k_work_reschedule(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
}
}
6 changes: 3 additions & 3 deletions subsys/bluetooth/mesh/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct bt_mesh_friend {

uint16_t sub_list[FRIEND_SUB_LIST_SIZE];

struct k_delayed_work timer;
struct k_work_delayable timer;

struct bt_mesh_friend_seg {
sys_slist_t queue;
Expand All @@ -88,7 +88,7 @@ struct bt_mesh_friend {
uint32_t start; /* Clear Procedure start */
uint16_t frnd; /* Previous Friend's address */
uint16_t repeat_sec; /* Repeat timeout in seconds */
struct k_delayed_work timer; /* Repeat timer */
struct k_work_delayable timer; /* Repeat timer */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a space added before a tab here, though if checkpatch doesn't complain it doesn't need to be fixed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so. The comment alignment is added manually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK; then it's probably aligned with spaces, with one added. The codebase isn't consistent on spaces or tabs for internal alignment whitespace on lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general approach we've tried to follow for Bluetooth code is "tabs for indentation, spaces for alignment", or in other words "tabs should only exist in the beginning of a line but never somewhere in the middle". The reason for that is that this tends to guarantee that when viewed e.g. in the GitHub web UI everything will look correctly aligned. I think it should also help viewing in general regardless of what tab-width people have configured in their editor, but this I'm not 100% sure of. Anyway, I haven't cared enough to propose having this specified in some project-wide coding style document, but maybe it would be worth doing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there's significant variation throughout the codebase for alignment of backslashes at the end of multi-line macros (one space, space aligned, tab aligned). Something to consider if #21392 gets some attention.

} clear;
};

Expand Down Expand Up @@ -213,7 +213,7 @@ struct bt_mesh_net {
uint8_t default_ttl;

/* Timer to track duration in current IV Update state */
struct k_delayed_work ivu_timer;
struct k_work_delayable ivu_timer;

uint8_t dev_key[16];
};
Expand Down
Loading