From 91ad0f9f4abafc92a585c057bc3ad78ff70be170 Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 4 Feb 2020 15:26:44 +0800 Subject: [PATCH] ble_mesh: Start the timer when starting to send the client message --- .../mesh_models/client/client_common.c | 44 ++++++++++++++----- .../client/include/client_common.h | 1 + 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c index 75c88135f..a3f68834b 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -235,6 +235,27 @@ static s32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx, return time; } +static void msg_send_start(u16_t duration, int err, void *cb_data) +{ + bt_mesh_client_node_t *node = cb_data; + + BT_DBG("%s, duration %ums", __func__, duration); + + if (err) { + if (!k_delayed_work_free(&node->timer)) { + bt_mesh_client_free_node(node); + } + return; + } + + k_delayed_work_submit(&node->timer, node->timeout); +} + +static const struct bt_mesh_send_cb send_cb = { + .start = msg_send_start, + .end = NULL, +}; + int bt_mesh_client_send_msg(struct bt_mesh_model *model, u32_t opcode, struct bt_mesh_msg_ctx *ctx, @@ -311,23 +332,26 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, bt_mesh_free(node); return -EINVAL; } + node->timeout = bt_mesh_client_calc_timeout(ctx, msg, opcode, timeout ? timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT); - s32_t time = bt_mesh_client_calc_timeout(ctx, msg, opcode, timeout ? timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT); - - err = bt_mesh_model_send(model, ctx, msg, cb, cb_data); - if (err) { - bt_mesh_free(node); - return err; - } + k_delayed_work_init(&node->timer, timer_handler); bt_mesh_list_lock(); sys_slist_append(&internal->queue, &node->client_node); bt_mesh_list_unlock(); - k_delayed_work_init(&node->timer, timer_handler); - k_delayed_work_submit(&node->timer, time); + /* "bt_mesh_model_send" will post the mesh packet to the mesh adv queue. + * Due to the higher priority of adv_thread (than btc task), we need to + * send the packet after the list item "node" is initialized properly. + */ + err = bt_mesh_model_send(model, ctx, msg, &send_cb, node); + if (err) { + BT_ERR("Failed to send client message 0x%08x", node->opcode); + k_delayed_work_free(&node->timer); + bt_mesh_client_free_node(node); + } - return 0; + return err; } static bt_mesh_mutex_t client_model_lock; diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h index 0e337d34b..ddf743b32 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h @@ -65,6 +65,7 @@ typedef struct { struct bt_mesh_msg_ctx ctx; /* Message context */ u32_t opcode; /* Message opcode */ u32_t op_pending; /* Expected status message opcode */ + s32_t timeout; /* Calculated message timeout value */ struct k_delayed_work timer; /* Time used to get response. Only for internal use. */ } bt_mesh_client_node_t;