From 87d9eadf501e95f39f3274af971cf228c85427f6 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Mon, 6 Apr 2020 13:56:14 +0200 Subject: [PATCH] Bluetooth: mesh: Convert bluetooth mesh to using k_timeout struct Convert bluetooth mesh to using k_timeout struct. Many of the mesh modules uses timeout calculations, so it is most practical to keep the s32_t type and only initialize a k_timeout_t struct when calling the kernel. Signed-off-by: Joakim Andersson --- include/bluetooth/mesh/cfg_cli.h | 2 +- include/bluetooth/mesh/health_cli.h | 2 +- subsys/bluetooth/mesh/access.c | 17 ++++++------ subsys/bluetooth/mesh/adv.c | 10 +++---- subsys/bluetooth/mesh/adv.h | 4 +-- subsys/bluetooth/mesh/beacon.c | 6 ++-- subsys/bluetooth/mesh/cfg_cli.c | 5 ++-- subsys/bluetooth/mesh/cfg_srv.c | 17 ++++++------ subsys/bluetooth/mesh/friend.c | 14 ++++------ subsys/bluetooth/mesh/health_cli.c | 5 ++-- subsys/bluetooth/mesh/health_srv.c | 6 ++-- subsys/bluetooth/mesh/lpn.c | 43 +++++++++++++++-------------- subsys/bluetooth/mesh/main.c | 3 +- subsys/bluetooth/mesh/pb_adv.c | 15 +++++----- subsys/bluetooth/mesh/prov_bearer.h | 2 +- subsys/bluetooth/mesh/proxy.c | 18 ++++++------ subsys/bluetooth/mesh/proxy.h | 2 +- subsys/bluetooth/mesh/settings.c | 19 +++++++------ subsys/bluetooth/mesh/shell.c | 30 ++++++++------------ subsys/bluetooth/mesh/transport.c | 22 +++++++-------- 20 files changed, 120 insertions(+), 122 deletions(-) diff --git a/include/bluetooth/mesh/cfg_cli.h b/include/bluetooth/mesh/cfg_cli.h index 8833dbb426f..1f28e7d9e0f 100644 --- a/include/bluetooth/mesh/cfg_cli.h +++ b/include/bluetooth/mesh/cfg_cli.h @@ -832,7 +832,7 @@ int bt_mesh_cfg_hb_pub_get(u16_t net_idx, u16_t addr, /** @brief Get the current transmission timeout value. * - * @return The configured transmission timeout. + * @return The configured transmission timeout in milliseconds. */ s32_t bt_mesh_cfg_cli_timeout_get(void); diff --git a/include/bluetooth/mesh/health_cli.h b/include/bluetooth/mesh/health_cli.h index 82553c4e2f1..ecc279cad7e 100644 --- a/include/bluetooth/mesh/health_cli.h +++ b/include/bluetooth/mesh/health_cli.h @@ -182,7 +182,7 @@ int bt_mesh_health_attention_set(u16_t addr, u16_t app_idx, u8_t attention, /** @brief Get the current transmission timeout value. * - * @return The configured transmission timeout. + * @return The configured transmission timeout in milliseconds. */ s32_t bt_mesh_health_cli_timeout_get(void); diff --git a/subsys/bluetooth/mesh/access.c b/subsys/bluetooth/mesh/access.c index 82509cf1dd3..216064e9031 100644 --- a/subsys/bluetooth/mesh/access.c +++ b/subsys/bluetooth/mesh/access.c @@ -57,7 +57,7 @@ void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod) { - int period; + s32_t period; if (!mod->pub) { return 0; @@ -66,19 +66,19 @@ s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod) switch (mod->pub->period >> 6) { case 0x00: /* 1 step is 100 ms */ - period = K_MSEC((mod->pub->period & BIT_MASK(6)) * 100U); + period = (mod->pub->period & BIT_MASK(6)) * 100U; break; case 0x01: /* 1 step is 1 second */ - period = K_SECONDS(mod->pub->period & BIT_MASK(6)); + period = (mod->pub->period & BIT_MASK(6)) * MSEC_PER_SEC; break; case 0x02: /* 1 step is 10 seconds */ - period = K_SECONDS((mod->pub->period & BIT_MASK(6)) * 10U); + period = (mod->pub->period & BIT_MASK(6)) * 10U * MSEC_PER_SEC; break; case 0x03: /* 1 step is 10 minutes */ - period = K_MINUTES((mod->pub->period & BIT_MASK(6)) * 10U); + period = (mod->pub->period & BIT_MASK(6)) * 600U * MSEC_PER_SEC; break; default: CODE_UNREACHABLE; @@ -108,7 +108,7 @@ static s32_t next_period(struct bt_mesh_model *mod) if (elapsed >= period) { BT_WARN("Publication sending took longer than the period"); /* Return smallest positive number since 0 means disabled */ - return K_MSEC(1); + return 1; } return period - elapsed; @@ -129,7 +129,7 @@ static void publish_sent(int err, void *user_data) if (delay) { BT_DBG("Publishing next time in %dms", delay); - k_delayed_work_submit(&mod->pub->timer, delay); + k_delayed_work_submit(&mod->pub->timer, K_MSEC(delay)); } } @@ -217,7 +217,8 @@ static void mod_publish(struct k_work *work) /* Continue with normal publication */ if (period_ms) { - k_delayed_work_submit(&pub->timer, period_ms); + k_delayed_work_submit(&pub->timer, + K_MSEC(period_ms)); } } diff --git a/subsys/bluetooth/mesh/adv.c b/subsys/bluetooth/mesh/adv.c index e9a8ee2ad2c..70242a58862 100644 --- a/subsys/bluetooth/mesh/adv.c +++ b/subsys/bluetooth/mesh/adv.c @@ -158,11 +158,11 @@ static void adv_thread(void *p1, void *p2, void *p3) if (IS_ENABLED(CONFIG_BT_MESH_PROXY)) { buf = net_buf_get(&adv_queue, K_NO_WAIT); while (!buf) { - s32_t timeout; + k_timeout_t timeout; timeout = bt_mesh_proxy_adv_start(); - BT_DBG("Proxy Advertising up to %d ms", - timeout); + BT_DBG("Proxy Advertising"); + buf = net_buf_get(&adv_queue, timeout); bt_mesh_proxy_adv_stop(); } @@ -197,7 +197,7 @@ void bt_mesh_adv_update(void) struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, bt_mesh_adv_alloc_t get_id, enum bt_mesh_adv_type type, - u8_t xmit, s32_t timeout) + u8_t xmit, k_timeout_t timeout) { struct bt_mesh_adv *adv; struct net_buf *buf; @@ -224,7 +224,7 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, } struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, u8_t xmit, - s32_t timeout) + k_timeout_t timeout) { return bt_mesh_adv_create_from_pool(&adv_buf_pool, adv_alloc, type, xmit, timeout); diff --git a/subsys/bluetooth/mesh/adv.h b/subsys/bluetooth/mesh/adv.h index 7a890cee04b..9432a60a718 100644 --- a/subsys/bluetooth/mesh/adv.h +++ b/subsys/bluetooth/mesh/adv.h @@ -37,12 +37,12 @@ typedef struct bt_mesh_adv *(*bt_mesh_adv_alloc_t)(int id); /* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */ struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, u8_t xmit, - s32_t timeout); + k_timeout_t timeout); struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, bt_mesh_adv_alloc_t get_id, enum bt_mesh_adv_type type, - u8_t xmit, s32_t timeout); + u8_t xmit, k_timeout_t timeout); void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, void *cb_data); diff --git a/subsys/bluetooth/mesh/beacon.c b/subsys/bluetooth/mesh/beacon.c index bbf126545d2..96dd5576255 100644 --- a/subsys/bluetooth/mesh/beacon.c +++ b/subsys/bluetooth/mesh/beacon.c @@ -105,8 +105,8 @@ void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, } /* If the interval has passed or is within 5 seconds from now send a beacon */ -#define BEACON_THRESHOLD(sub) (K_SECONDS(10 * ((sub)->beacons_last + 1)) - \ - K_SECONDS(5)) +#define BEACON_THRESHOLD(sub) \ + ((10 * ((sub)->beacons_last + 1)) * MSEC_PER_SEC - (5 * MSEC_PER_SEC)) static int secure_beacon_send(void) { @@ -128,7 +128,7 @@ static int secure_beacon_send(void) } time_diff = now - sub->beacon_sent; - if (time_diff < K_SECONDS(600) && + if (time_diff < (600 * MSEC_PER_SEC) && time_diff < BEACON_THRESHOLD(sub)) { continue; } diff --git a/subsys/bluetooth/mesh/cfg_cli.c b/subsys/bluetooth/mesh/cfg_cli.c index 73769910feb..7e5537fad3e 100644 --- a/subsys/bluetooth/mesh/cfg_cli.c +++ b/subsys/bluetooth/mesh/cfg_cli.c @@ -35,7 +35,7 @@ struct comp_data { struct net_buf_simple *comp; }; -static s32_t msg_timeout = K_SECONDS(2); +static s32_t msg_timeout; static struct bt_mesh_cfg_cli *cli; @@ -709,6 +709,7 @@ static int cfg_cli_init(struct bt_mesh_model *model) cli = model->user_data; cli->model = model; + msg_timeout = 2 * MSEC_PER_SEC; /* * Configuration Model security is device-key based and both the local @@ -753,7 +754,7 @@ static int cli_wait(void) { int err; - err = k_sem_take(&cli->op_sync, msg_timeout); + err = k_sem_take(&cli->op_sync, SYS_TIMEOUT_MS(msg_timeout)); cli_reset(); diff --git a/subsys/bluetooth/mesh/cfg_srv.c b/subsys/bluetooth/mesh/cfg_srv.c index 347c8bac770..72972c28fde 100644 --- a/subsys/bluetooth/mesh/cfg_srv.c +++ b/subsys/bluetooth/mesh/cfg_srv.c @@ -247,8 +247,9 @@ static u8_t _mod_pub_set(struct bt_mesh_model *model, u16_t pub_addr, period_ms = bt_mesh_model_pub_period_get(model); BT_DBG("period %u ms", period_ms); - if (period_ms) { - k_delayed_work_submit(&model->pub->timer, period_ms); + if (period_ms > 0) { + k_delayed_work_submit(&model->pub->timer, + K_MSEC(period_ms)); } else { k_delayed_work_cancel(&model->pub->timer); } @@ -2730,7 +2731,7 @@ static void lpn_timeout_get(struct bt_mesh_model *model, BT_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5); struct bt_mesh_friend *frnd; u16_t lpn_addr; - s32_t timeout; + s32_t timeout_ms; lpn_addr = net_buf_simple_pull_le16(buf); @@ -2746,20 +2747,20 @@ static void lpn_timeout_get(struct bt_mesh_model *model, net_buf_simple_add_le16(&msg, lpn_addr); if (!IS_ENABLED(CONFIG_BT_MESH_FRIEND)) { - timeout = 0; + timeout_ms = 0; goto send_rsp; } frnd = bt_mesh_friend_find(BT_MESH_KEY_ANY, lpn_addr, true, true); if (!frnd) { - timeout = 0; + timeout_ms = 0; goto send_rsp; } - timeout = k_delayed_work_remaining_get(&frnd->timer) / 100; + timeout_ms = k_delayed_work_remaining_get(&frnd->timer) / 100; send_rsp: - net_buf_simple_add_le24(&msg, timeout); + net_buf_simple_add_le24(&msg, timeout_ms); if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { BT_ERR("Unable to send LPN PollTimeout Status"); @@ -3226,7 +3227,7 @@ static void hb_publish(struct k_work *work) period_ms = hb_pwr2(cfg->hb_pub.period, 1) * 1000U; if (period_ms && cfg->hb_pub.count > 1) { - k_delayed_work_submit(&cfg->hb_pub.timer, period_ms); + k_delayed_work_submit(&cfg->hb_pub.timer, K_MSEC(period_ms)); } bt_mesh_heartbeat_send(); diff --git a/subsys/bluetooth/mesh/friend.c b/subsys/bluetooth/mesh/friend.c index 76acf8e1bb6..f9a7eacd871 100644 --- a/subsys/bluetooth/mesh/friend.c +++ b/subsys/bluetooth/mesh/friend.c @@ -563,7 +563,7 @@ static void enqueue_sub_cfm(struct bt_mesh_friend *frnd, u8_t xact) static void friend_recv_delay(struct bt_mesh_friend *frnd) { frnd->pending_req = 1U; - k_delayed_work_submit(&frnd->timer, recv_delay(frnd)); + k_delayed_work_submit(&frnd->timer, K_MSEC(recv_delay(frnd))); BT_DBG("Waiting RecvDelay of %d ms", recv_delay(frnd)); } @@ -895,11 +895,7 @@ static s32_t offer_delay(struct bt_mesh_friend *frnd, s8_t rssi, u8_t crit) BT_DBG("Local Delay calculated as %d ms", delay); - if (delay < 100) { - return K_MSEC(100); - } - - return K_MSEC(delay); + return MAX(delay, 100); } int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) @@ -986,8 +982,8 @@ init_friend: } k_delayed_work_submit(&frnd->timer, - offer_delay(frnd, rx->ctx.recv_rssi, - msg->criteria)); + K_MSEC(offer_delay(frnd, rx->ctx.recv_rssi, + msg->criteria))); friend_cred_create(rx->sub, frnd->lpn, frnd->lpn_counter, frnd->counter); @@ -1107,7 +1103,7 @@ static void buf_send_end(int err, void *user_data) } if (frnd->established) { - k_delayed_work_submit(&frnd->timer, frnd->poll_to); + k_delayed_work_submit(&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 */ diff --git a/subsys/bluetooth/mesh/health_cli.c b/subsys/bluetooth/mesh/health_cli.c index 16e76abcfda..791b2109af0 100644 --- a/subsys/bluetooth/mesh/health_cli.c +++ b/subsys/bluetooth/mesh/health_cli.c @@ -25,7 +25,7 @@ #include "net.h" #include "foundation.h" -static s32_t msg_timeout = K_SECONDS(2); +static s32_t msg_timeout; static struct bt_mesh_health_cli *health_cli; @@ -199,7 +199,7 @@ static int cli_wait(void) { int err; - err = k_sem_take(&health_cli->op_sync, msg_timeout); + err = k_sem_take(&health_cli->op_sync, SYS_TIMEOUT_MS(msg_timeout)); cli_reset(); @@ -497,6 +497,7 @@ int bt_mesh_health_cli_set(struct bt_mesh_model *model) } health_cli = model->user_data; + msg_timeout = 2 * MSEC_PER_SEC; return 0; } diff --git a/subsys/bluetooth/mesh/health_srv.c b/subsys/bluetooth/mesh/health_srv.c index 249ea9d31d8..e1745f18c2c 100644 --- a/subsys/bluetooth/mesh/health_srv.c +++ b/subsys/bluetooth/mesh/health_srv.c @@ -363,7 +363,7 @@ int bt_mesh_fault_update(struct bt_mesh_elem *elem) /* Let periodic publishing, if enabled, take care of sending the * Health Current Status. */ - if (bt_mesh_model_pub_period_get(mod)) { + if (bt_mesh_model_pub_period_get(mod) > 0) { return 0; } @@ -435,12 +435,12 @@ void bt_mesh_attention(struct bt_mesh_model *model, u8_t time) srv = model->user_data; } - if (time) { + if (time > 0) { if (srv->cb && srv->cb->attn_on) { srv->cb->attn_on(model); } - k_delayed_work_submit(&srv->attn_timer, time * 1000U); + k_delayed_work_submit(&srv->attn_timer, K_SECONDS(time)); } else { k_delayed_work_cancel(&srv->attn_timer); diff --git a/subsys/bluetooth/mesh/lpn.c b/subsys/bluetooth/mesh/lpn.c index bfa127dc0ac..63e96b4a0aa 100644 --- a/subsys/bluetooth/mesh/lpn.c +++ b/subsys/bluetooth/mesh/lpn.c @@ -29,9 +29,9 @@ #include "lpn.h" #if defined(CONFIG_BT_MESH_LPN_AUTO) -#define LPN_AUTO_TIMEOUT K_SECONDS(CONFIG_BT_MESH_LPN_AUTO_TIMEOUT) +#define LPN_AUTO_TIMEOUT (CONFIG_BT_MESH_LPN_AUTO_TIMEOUT * MSEC_PER_SEC) #else -#define LPN_AUTO_TIMEOUT 0 +#define LPN_AUTO_TIMEOUT 0 #endif #define LPN_RECV_DELAY CONFIG_BT_MESH_LPN_RECV_DELAY @@ -40,11 +40,11 @@ #define FRIEND_REQ_RETRY_TIMEOUT K_SECONDS(CONFIG_BT_MESH_LPN_RETRY_TIMEOUT) -#define FRIEND_REQ_WAIT K_MSEC(100) -#define FRIEND_REQ_SCAN K_SECONDS(1) +#define FRIEND_REQ_WAIT 100 +#define FRIEND_REQ_SCAN (1 * MSEC_PER_SEC) #define FRIEND_REQ_TIMEOUT (FRIEND_REQ_WAIT + FRIEND_REQ_SCAN) -#define POLL_RETRY_TIMEOUT K_MSEC(100) +#define POLL_RETRY_TIMEOUT 100 #define REQ_RETRY_DURATION(lpn) (LPN_RECV_DELAY + (lpn)->adv_duration + \ (lpn)->recv_win + POLL_RETRY_TIMEOUT) @@ -165,7 +165,7 @@ static void friend_clear_sent(int err, void *user_data) } lpn_set_state(BT_MESH_LPN_CLEAR); - k_delayed_work_submit(&lpn->timer, FRIEND_REQ_TIMEOUT); + k_delayed_work_submit(&lpn->timer, K_MSEC(FRIEND_REQ_TIMEOUT)); } static const struct bt_mesh_send_cb clear_sent_cb = { @@ -272,11 +272,11 @@ static void friend_req_sent(u16_t duration, int err, void *user_data) lpn->adv_duration = duration; if (IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { - k_delayed_work_submit(&lpn->timer, FRIEND_REQ_WAIT); + k_delayed_work_submit(&lpn->timer, K_MSEC(FRIEND_REQ_WAIT)); lpn_set_state(BT_MESH_LPN_REQ_WAIT); } else { k_delayed_work_submit(&lpn->timer, - duration + FRIEND_REQ_TIMEOUT); + K_MSEC(duration + FRIEND_REQ_TIMEOUT)); lpn_set_state(BT_MESH_LPN_WAIT_OFFER); } } @@ -340,11 +340,11 @@ static void req_sent(u16_t duration, int err, void *user_data) * response data due to HCI and other latencies. */ k_delayed_work_submit(&lpn->timer, - LPN_RECV_DELAY - SCAN_LATENCY); + K_MSEC(LPN_RECV_DELAY - SCAN_LATENCY)); } else { k_delayed_work_submit(&lpn->timer, - LPN_RECV_DELAY + duration + - lpn->recv_win); + K_MSEC(LPN_RECV_DELAY + duration + + lpn->recv_win)); } } @@ -466,7 +466,7 @@ void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx) if (lpn->state == BT_MESH_LPN_TIMER) { BT_DBG("Restarting establishment timer"); - k_delayed_work_submit(&lpn->timer, LPN_AUTO_TIMEOUT); + k_delayed_work_submit(&lpn->timer, K_MSEC(LPN_AUTO_TIMEOUT)); return; } @@ -710,7 +710,7 @@ static void update_timeout(struct bt_mesh_lpn *lpn) BT_WARN("No response from Friend during ReceiveWindow"); bt_mesh_scan_disable(); lpn_set_state(BT_MESH_LPN_ESTABLISHED); - k_delayed_work_submit(&lpn->timer, POLL_RETRY_TIMEOUT); + k_delayed_work_submit(&lpn->timer, K_MSEC(POLL_RETRY_TIMEOUT)); } else { if (IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { bt_mesh_scan_disable(); @@ -755,8 +755,8 @@ static void lpn_timeout(struct k_work *work) break; case BT_MESH_LPN_REQ_WAIT: bt_mesh_scan_enable(); - k_delayed_work_submit(&lpn->timer, - lpn->adv_duration + FRIEND_REQ_SCAN); + k_delayed_work_submit(&lpn->timer, K_MSEC(lpn->adv_duration + + FRIEND_REQ_SCAN)); lpn_set_state(BT_MESH_LPN_WAIT_OFFER); break; case BT_MESH_LPN_WAIT_OFFER: @@ -791,8 +791,8 @@ static void lpn_timeout(struct k_work *work) break; case BT_MESH_LPN_RECV_DELAY: k_delayed_work_submit(&lpn->timer, - lpn->adv_duration + SCAN_LATENCY + - lpn->recv_win); + K_MSEC(lpn->adv_duration + SCAN_LATENCY + + lpn->recv_win)); bt_mesh_scan_enable(); lpn_set_state(BT_MESH_LPN_WAIT_UPDATE); break; @@ -840,7 +840,7 @@ static s32_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)); + return MIN(POLL_TIMEOUT_MAX(lpn), 1 * MSEC_PER_SEC); } if (lpn->poll_timeout < POLL_TIMEOUT_MAX(lpn)) { @@ -913,7 +913,7 @@ int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx, } if (!lpn->sent_req) { - k_delayed_work_submit(&lpn->timer, poll_timeout(lpn)); + k_delayed_work_submit(&lpn->timer, K_MSEC(poll_timeout(lpn))); } return 0; @@ -1007,7 +1007,7 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, } if (!lpn->sent_req) { - k_delayed_work_submit(&lpn->timer, poll_timeout(lpn)); + k_delayed_work_submit(&lpn->timer, K_MSEC(poll_timeout(lpn))); } return 0; @@ -1051,7 +1051,8 @@ int bt_mesh_lpn_init(void) if (IS_ENABLED(CONFIG_BT_MESH_LPN_AUTO)) { BT_DBG("Waiting %u ms for messages", LPN_AUTO_TIMEOUT); lpn_set_state(BT_MESH_LPN_TIMER); - k_delayed_work_submit(&lpn->timer, LPN_AUTO_TIMEOUT); + k_delayed_work_submit(&lpn->timer, + K_MSEC(LPN_AUTO_TIMEOUT)); } } diff --git a/subsys/bluetooth/mesh/main.c b/subsys/bluetooth/mesh/main.c index b4359d76492..64a474d4906 100644 --- a/subsys/bluetooth/mesh/main.c +++ b/subsys/bluetooth/mesh/main.c @@ -263,7 +263,8 @@ static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, s32_t period_ms = bt_mesh_model_pub_period_get(mod); if (period_ms) { - k_delayed_work_submit(&mod->pub->timer, period_ms); + k_delayed_work_submit(&mod->pub->timer, + K_MSEC(period_ms)); } } } diff --git a/subsys/bluetooth/mesh/pb_adv.c b/subsys/bluetooth/mesh/pb_adv.c index 9430012649c..939339ae693 100644 --- a/subsys/bluetooth/mesh/pb_adv.c +++ b/subsys/bluetooth/mesh/pb_adv.c @@ -47,8 +47,8 @@ #define RETRANSMIT_TIMEOUT K_MSEC(500) #define BUF_TIMEOUT K_MSEC(400) -#define CLOSING_TIMEOUT K_SECONDS(3) -#define TRANSACTION_TIMEOUT K_SECONDS(30) +#define CLOSING_TIMEOUT (3 * MSEC_PER_SEC) +#define TRANSACTION_TIMEOUT (30 * MSEC_PER_SEC) /* Acked messages, will do retransmissions manually, taking acks into account: */ @@ -534,7 +534,8 @@ static void send_reliable(void) static void prov_retransmit(struct k_work *work) { - int i, timeout; + s32_t timeout_ms; + int i; BT_DBG(""); @@ -549,12 +550,12 @@ static void prov_retransmit(struct k_work *work) * message until CLOSING_TIMEOUT has elapsed. */ if (atomic_test_bit(link.flags, LINK_CLOSING)) { - timeout = CLOSING_TIMEOUT; + timeout_ms = CLOSING_TIMEOUT; } else { - timeout = TRANSACTION_TIMEOUT; + timeout_ms = TRANSACTION_TIMEOUT; } - if (k_uptime_get() - link.tx.start > timeout) { + if (k_uptime_get() - link.tx.start > timeout_ms) { if (atomic_test_bit(link.flags, LINK_CLOSING)) { close_link(PROV_BEARER_LINK_STATUS_SUCCESS); } else { @@ -778,7 +779,7 @@ void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) gen_prov_recv(&rx, buf); } -static int prov_link_open(const u8_t uuid[16], s32_t timeout, +static int prov_link_open(const u8_t uuid[16], k_timeout_t timeout, const struct prov_bearer_cb *cb, void *cb_data) { BT_DBG("uuid %s", bt_hex(uuid, 16)); diff --git a/subsys/bluetooth/mesh/prov_bearer.h b/subsys/bluetooth/mesh/prov_bearer.h index d34c3a33bb8..a453320dd47 100644 --- a/subsys/bluetooth/mesh/prov_bearer.h +++ b/subsys/bluetooth/mesh/prov_bearer.h @@ -93,7 +93,7 @@ struct prov_bearer { * * @return Zero on success, or (negative) error code otherwise. */ - int (*link_open)(const u8_t uuid[16], s32_t timeout, + int (*link_open)(const u8_t uuid[16], k_timeout_t timeout, const struct prov_bearer_cb *cb, void *cb_data); /** @brief Close the current link. diff --git a/subsys/bluetooth/mesh/proxy.c b/subsys/bluetooth/mesh/proxy.c index d36fa00c120..0edb503e41b 100644 --- a/subsys/bluetooth/mesh/proxy.c +++ b/subsys/bluetooth/mesh/proxy.c @@ -1010,7 +1010,7 @@ static const struct bt_data prov_ad[] = { #define NODE_ID_LEN 19 #define NET_ID_LEN 11 -#define NODE_ID_TIMEOUT K_SECONDS(CONFIG_BT_MESH_NODE_ID_TIMEOUT) +#define NODE_ID_TIMEOUT (CONFIG_BT_MESH_NODE_ID_TIMEOUT * MSEC_PER_SEC) static u8_t proxy_svc_data[NODE_ID_LEN] = { 0x28, 0x18, }; @@ -1130,21 +1130,21 @@ static int sub_count(void) return count; } -static s32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) +static k_timeout_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) { - s32_t remaining = K_FOREVER; + s32_t remaining = SYS_FOREVER_MS; int subnet_count; BT_DBG(""); if (conn_count == CONFIG_BT_MAX_CONN) { BT_DBG("Connectable advertising deferred (max connections)"); - return remaining; + return K_FOREVER; } if (!sub) { BT_WARN("No subnets to advertise on"); - return remaining; + return K_FOREVER; } if (sub->node_id == BT_MESH_NODE_IDENTITY_RUNNING) { @@ -1176,16 +1176,16 @@ static s32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) * second long (to avoid excessive rotation). */ max_timeout = NODE_ID_TIMEOUT / MAX(subnet_count, 6); - max_timeout = MAX(max_timeout, K_SECONDS(1)); + max_timeout = MAX(max_timeout, 1 * MSEC_PER_SEC); - if (remaining > max_timeout || remaining < 0) { + if (remaining > max_timeout || remaining == SYS_FOREVER_MS) { remaining = max_timeout; } } BT_DBG("Advertising %d ms for net_idx 0x%04x", remaining, sub->net_idx); - return remaining; + return SYS_TIMEOUT_MS(remaining); } #endif /* GATT_PROXY */ @@ -1235,7 +1235,7 @@ static size_t gatt_prov_adv_create(struct bt_data prov_sd[2]) } #endif /* CONFIG_BT_MESH_PB_GATT */ -s32_t bt_mesh_proxy_adv_start(void) +k_timeout_t bt_mesh_proxy_adv_start(void) { BT_DBG(""); diff --git a/subsys/bluetooth/mesh/proxy.h b/subsys/bluetooth/mesh/proxy.h index a4bddbe0d59..5a5e9232b35 100644 --- a/subsys/bluetooth/mesh/proxy.h +++ b/subsys/bluetooth/mesh/proxy.h @@ -25,7 +25,7 @@ void bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub); struct net_buf_simple *bt_mesh_proxy_get_buf(void); -s32_t bt_mesh_proxy_adv_start(void); +k_timeout_t bt_mesh_proxy_adv_start(void); void bt_mesh_proxy_adv_stop(void); void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub); diff --git a/subsys/bluetooth/mesh/settings.c b/subsys/bluetooth/mesh/settings.c index d91f615224c..637888daaae 100644 --- a/subsys/bluetooth/mesh/settings.c +++ b/subsys/bluetooth/mesh/settings.c @@ -1093,9 +1093,10 @@ static void commit_mod(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, if (mod->pub && mod->pub->update && mod->pub->addr != BT_MESH_ADDR_UNASSIGNED) { s32_t ms = bt_mesh_model_pub_period_get(mod); - if (ms) { + + if (ms > 0) { BT_DBG("Starting publish timer (period %u ms)", ms); - k_delayed_work_submit(&mod->pub->timer, ms); + k_delayed_work_submit(&mod->pub->timer, K_MSEC(ms)); } } @@ -1188,30 +1189,30 @@ SETTINGS_STATIC_HANDLER_DEFINE(bt_mesh, "bt/mesh", NULL, mesh_set, mesh_commit, static void schedule_store(int flag) { - s32_t timeout, remaining; + s32_t timeout_ms, remaining; atomic_set_bit(bt_mesh.flags, flag); if (atomic_get(bt_mesh.flags) & NO_WAIT_PENDING_BITS) { - timeout = K_NO_WAIT; + timeout_ms = 0; } else if (atomic_test_bit(bt_mesh.flags, BT_MESH_RPL_PENDING) && (!(atomic_get(bt_mesh.flags) & GENERIC_PENDING_BITS) || (CONFIG_BT_MESH_RPL_STORE_TIMEOUT < CONFIG_BT_MESH_STORE_TIMEOUT))) { - timeout = K_SECONDS(CONFIG_BT_MESH_RPL_STORE_TIMEOUT); + timeout_ms = CONFIG_BT_MESH_RPL_STORE_TIMEOUT * MSEC_PER_SEC; } else { - timeout = K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT); + timeout_ms = CONFIG_BT_MESH_STORE_TIMEOUT * MSEC_PER_SEC; } remaining = k_delayed_work_remaining_get(&pending_store); - if (remaining && remaining < timeout) { + if ((remaining > 0) && remaining < timeout_ms) { BT_DBG("Not rescheduling due to existing earlier deadline"); return; } - BT_DBG("Waiting %d seconds", timeout / MSEC_PER_SEC); + BT_DBG("Waiting %d seconds", timeout_ms / MSEC_PER_SEC); - k_delayed_work_submit(&pending_store, timeout); + k_delayed_work_submit(&pending_store, K_MSEC(timeout_ms)); } static void clear_iv(void) diff --git a/subsys/bluetooth/mesh/shell.c b/subsys/bluetooth/mesh/shell.c index 9e0bdb8ac3e..26e9871b273 100644 --- a/subsys/bluetooth/mesh/shell.c +++ b/subsys/bluetooth/mesh/shell.c @@ -1851,33 +1851,27 @@ static int cmd_provision(const struct shell *shell, size_t argc, char *argv[]) int cmd_timeout(const struct shell *shell, size_t argc, char *argv[]) { - s32_t timeout; + s32_t timeout_ms; - if (argc < 2) { - timeout = bt_mesh_cfg_cli_timeout_get(); - if (timeout == K_FOREVER) { - shell_print(shell, "Message timeout: forever"); + if (argc == 2) { + s32_t timeout_s; + + timeout_s = strtol(argv[1], NULL, 0); + if (timeout_s < 0 || timeout_s > (INT32_MAX / 1000)) { + timeout_ms = SYS_FOREVER_MS; } else { - shell_print(shell, "Message timeout: %u seconds", - timeout / 1000); + timeout_ms = timeout_s * MSEC_PER_SEC; } - return 0; + bt_mesh_cfg_cli_timeout_set(timeout_ms); } - timeout = strtol(argv[1], NULL, 0); - if (timeout < 0 || timeout > (INT32_MAX / 1000)) { - timeout = K_FOREVER; - } else { - timeout = timeout * 1000; - } - - bt_mesh_cfg_cli_timeout_set(timeout); - if (timeout == K_FOREVER) { + timeout_ms = bt_mesh_cfg_cli_timeout_get(); + if (timeout_ms == SYS_FOREVER_MS) { shell_print(shell, "Message timeout: forever"); } else { shell_print(shell, "Message timeout: %u seconds", - timeout / 1000); + timeout_ms / 1000); } return 0; diff --git a/subsys/bluetooth/mesh/transport.c b/subsys/bluetooth/mesh/transport.c index 214bfc0d95a..56eefdd728f 100644 --- a/subsys/bluetooth/mesh/transport.c +++ b/subsys/bluetooth/mesh/transport.c @@ -58,12 +58,12 @@ * We use 400 since 300 is a common send duration for standard HCI, and we * need to have a timeout that's bigger than that. */ -#define SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) (K_MSEC(400) + 50 * (tx)->ttl) +#define SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) (400 + 50 * (tx)->ttl) /* When sending to a group, the messages are not acknowledged, and there's no * reason to delay the repetitions significantly. Delaying by more than 0 ms * to avoid flooding the network. */ -#define SEG_RETRANSMIT_TIMEOUT_GROUP K_MSEC(50) +#define SEG_RETRANSMIT_TIMEOUT_GROUP 50 #define SEG_RETRANSMIT_TIMEOUT(tx) \ (BT_MESH_ADDR_IS_UNICAST(tx->dst) ? \ @@ -317,8 +317,8 @@ static void schedule_retransmit(struct seg_tx *tx) */ k_delayed_work_submit(&tx->retransmit, (tx->sending || !tx->seg_o) ? - SEG_RETRANSMIT_TIMEOUT(tx) : - 0); + K_MSEC(SEG_RETRANSMIT_TIMEOUT(tx)) : + K_NO_WAIT); } static void seg_send_start(u16_t duration, int err, void *user_data) @@ -429,7 +429,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) end: if (!tx->seg_pending) { k_delayed_work_submit(&tx->retransmit, - SEG_RETRANSMIT_TIMEOUT(tx)); + K_MSEC(SEG_RETRANSMIT_TIMEOUT(tx))); } tx->sending = 0U; @@ -1255,15 +1255,15 @@ static inline s32_t ack_timeout(struct seg_rx *rx) /* The acknowledgment timer shall be set to a minimum of * 150 + 50 * TTL milliseconds. */ - to = K_MSEC(150 + (ttl * 50U)); + to = 150 + (ttl * 50U); /* 100 ms for every not yet received segment */ - to += K_MSEC(((rx->seg_n + 1) - popcount(rx->block)) * 100U); + to += ((rx->seg_n + 1) - popcount(rx->block)) * 100U; /* Make sure we don't send more frequently than the duration for * each packet (default is 300ms). */ - return MAX(to, K_MSEC(400)); + return MAX(to, 400); } int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, @@ -1375,7 +1375,7 @@ static void seg_ack(struct k_work *work) BT_DBG("rx %p", rx); - if (k_uptime_get_32() - rx->last > K_SECONDS(60)) { + if (k_uptime_get_32() - rx->last > (60 * MSEC_PER_SEC)) { BT_WARN("Incomplete timer expired"); seg_rx_reset(rx, false); @@ -1389,7 +1389,7 @@ static void seg_ack(struct k_work *work) send_ack(rx->sub, rx->dst, rx->src, rx->ttl, &rx->seq_auth, rx->block, rx->obo); - k_delayed_work_submit(&rx->ack, ack_timeout(rx)); + k_delayed_work_submit(&rx->ack, K_MSEC(ack_timeout(rx))); } static inline bool sdu_len_is_ok(bool ctl, u8_t seg_n) @@ -1671,7 +1671,7 @@ found_rx: if (!k_delayed_work_remaining_get(&rx->ack) && !bt_mesh_lpn_established()) { - k_delayed_work_submit(&rx->ack, ack_timeout(rx)); + k_delayed_work_submit(&rx->ack, K_MSEC(ack_timeout(rx))); } /* Allocated segment here */