Skip to content

Commit

Permalink
host/mesh: sync lpn.c with Zephyr
Browse files Browse the repository at this point in the history
  • Loading branch information
KKopyscinski committed May 5, 2021
1 parent d9f64ee commit 7bf2030
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions nimble/host/mesh/src/lpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ static inline void group_clear(atomic_t *target, atomic_t *source)

static void clear_friendship(bool force, bool disable);

static int32_t poll_timeout(struct bt_mesh_lpn *lpn)
{
/* If we're waiting for segment acks keep polling at high freq */
if (bt_mesh_tx_in_progress()) {
return min(POLL_TIMEOUT_MAX(lpn), K_SECONDS(1));
}

if (lpn->poll_timeout < POLL_TIMEOUT_MAX(lpn)) {
lpn->poll_timeout *= 2;
lpn->poll_timeout = min(lpn->poll_timeout,
POLL_TIMEOUT_MAX(lpn));
}

BT_DBG("Poll Timeout is %ums", (unsigned) lpn->poll_timeout);

return lpn->poll_timeout;
}

static void friend_clear_sent(int err, void *user_data)
{
struct bt_mesh_lpn *lpn = &bt_mesh.lpn;
Expand Down Expand Up @@ -211,6 +229,7 @@ static void clear_friendship(bool force, bool disable)

bt_mesh_rx_reset();

lpn_set_state(BT_MESH_LPN_DISABLED);
k_delayed_work_cancel(&lpn->timer);

if (lpn->clear_success) {
Expand All @@ -229,7 +248,7 @@ static void clear_friendship(bool force, bool disable)
lpn->recv_win = 0;
lpn->queue_size = 0;
lpn->disable = 0;
lpn->sent_req = 0;
lpn->sent_req = 0U;
lpn->established = 0;
lpn->clear_success = 0;
lpn->sub = NULL;
Expand All @@ -246,13 +265,12 @@ static void clear_friendship(bool force, bool disable)

bt_mesh_hb_feature_changed(BT_MESH_FEAT_LOW_POWER);

if (disable) {
if (!disable) {
lpn_set_state(BT_MESH_LPN_DISABLED);
k_delayed_work_submit(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT);
return;
}

lpn_set_state(BT_MESH_LPN_ENABLED);
k_delayed_work_submit(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT);
}

static void friend_req_sent(uint16_t duration, int err, void *user_data)
Expand Down Expand Up @@ -331,7 +349,7 @@ static void req_sent(uint16_t duration, int err, void *user_data)

if (err) {
BT_ERR("Sending request failed (err %d)", err);
lpn->sent_req = 0;
lpn->sent_req = 0U;
group_zero(lpn->pending);
return;
}
Expand Down Expand Up @@ -459,11 +477,14 @@ static void friend_response_received(struct bt_mesh_lpn *lpn)
lpn->fsn++;
}

k_delayed_work_cancel(&lpn->timer);
bt_mesh_scan_disable();
lpn_set_state(BT_MESH_LPN_ESTABLISHED);
lpn->req_attempts = 0;
lpn->sent_req = 0;
lpn->sent_req = 0U;

int32_t timeout = poll_timeout(lpn);

k_delayed_work_submit(&lpn->timer, K_MSEC(timeout));
}

void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx)
Expand All @@ -476,6 +497,10 @@ void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx)
return;
}

if (lpn->state != BT_MESH_LPN_WAIT_UPDATE) {
return;
}

if (lpn->sent_req != TRANS_CTL_OP_FRIEND_POLL) {
BT_WARN("Unexpected message withouth a preceding Poll");
return;
Expand Down Expand Up @@ -545,7 +570,6 @@ int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx,
}
}
/* TODO: Add offer acceptance criteria check */
k_delayed_work_cancel(&lpn->timer);

lpn->recv_win = msg->recv_win;
lpn->queue_size = msg->queue_size;
Expand Down Expand Up @@ -738,7 +762,7 @@ static void update_timeout(struct bt_mesh_lpn *lpn)

if (lpn->req_attempts < REQ_ATTEMPTS(lpn)) {
BT_WARN("Retrying first Friend Poll");
lpn->sent_req = 0;
lpn->sent_req = 0U;
if (send_friend_poll() == 0) {
return;
}
Expand Down Expand Up @@ -793,7 +817,7 @@ static void lpn_timeout(struct ble_npl_event *work)
if (lpn->req_attempts < REQ_ATTEMPTS(lpn)) {
uint8_t req = lpn->sent_req;

lpn->sent_req = 0;
lpn->sent_req = 0U;

if (!req || req == TRANS_CTL_OP_FRIEND_POLL) {
send_friend_poll();
Expand Down Expand Up @@ -856,24 +880,6 @@ void bt_mesh_lpn_group_del(uint16_t *groups, size_t group_count)
sub_update(TRANS_CTL_OP_FRIEND_SUB_REM);
}

static int32_t poll_timeout(struct bt_mesh_lpn *lpn)
{
/* If we're waiting for segment acks keep polling at high freq */
if (bt_mesh_tx_in_progress()) {
return min(POLL_TIMEOUT_MAX(lpn), K_SECONDS(1));
}

if (lpn->poll_timeout < POLL_TIMEOUT_MAX(lpn)) {
lpn->poll_timeout *= 2;
lpn->poll_timeout = min(lpn->poll_timeout,
POLL_TIMEOUT_MAX(lpn));
}

BT_DBG("Poll Timeout is %ums", (unsigned) lpn->poll_timeout);

return lpn->poll_timeout;
}

int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx,
struct os_mbuf *buf)
{
Expand Down Expand Up @@ -932,12 +938,6 @@ int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx,
send_friend_poll();
}

if (!lpn->sent_req) {
int32_t timeout = poll_timeout(lpn);

k_delayed_work_submit(&lpn->timer, K_MSEC(timeout));
}

return 0;
}

Expand Down Expand Up @@ -1020,12 +1020,6 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx,
send_friend_poll();
}

if (!lpn->sent_req) {
int32_t timeout = poll_timeout(lpn);

k_delayed_work_submit(&lpn->timer, K_MSEC(timeout));
}

return 0;
}

Expand Down Expand Up @@ -1073,8 +1067,6 @@ int bt_mesh_lpn_init(void)

BT_DBG("");

lpn->groups_changed = 0;

k_delayed_work_init(&lpn->timer, lpn_timeout);

if (lpn->state == BT_MESH_LPN_ENABLED) {
Expand Down

0 comments on commit 7bf2030

Please sign in to comment.