浏览代码

ble_mesh: Start the timer when starting to send the client message

lly 6 年之前
父节点
当前提交
f2e5ee5d25

+ 34 - 10
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;

+ 1 - 0
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;