From 6c868a070b7e5a64f48d05c06a33bf36e6fcc8b4 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 25 Mar 2020 00:04:32 +0800 Subject: [PATCH] ble_mesh: Skip publish if update fails [Zephyr] Allow models to skip a periodic publish interval by returning an error from the publish update callback. Previously, an error return from publish update would cancel periodic publishing. This can't be recovered from, and as such, no valid model implementation could return an error from this callback, and there was no way to skip a periodic publish. --- components/bt/esp_ble_mesh/mesh_core/access.c | 18 +++++++++++++----- .../mesh_core/include/mesh_access.h | 3 +++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index dab4175347..529cca1b31 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -427,6 +427,14 @@ static int publish_retransmit(struct bt_mesh_model *mod) return err; } +static void publish_retransmit_end(int err, struct bt_mesh_model_pub *pub) +{ + /* Cancel all retransmits for this publish attempt */ + pub->count = 0U; + /* Make sure the publish timer gets reset */ + publish_sent(err, pub->mod); +} + static void mod_publish(struct k_work *work) { struct bt_mesh_model_pub *pub = CONTAINER_OF(work, @@ -468,7 +476,10 @@ static void mod_publish(struct k_work *work) */ err = pub->update(pub->mod); if (err) { - BT_ERR("%s, Failed to update publication message", __func__); + /* Cancel this publish attempt. */ + BT_ERR("Update failed, skipping publish (err %d)", err); + pub->period_start = k_uptime_get_32(); + publish_retransmit_end(err, pub); return; } @@ -1079,10 +1090,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) err = model_send(model, &tx, true, sdu, &pub_sent_cb, model); if (err) { - /* Don't try retransmissions for this publish attempt */ - pub->count = 0U; - /* Make sure the publish timer gets reset */ - publish_sent(err, model); + publish_retransmit_end(err, pub); } bt_mesh_free_buf(sdu); diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h index 11a63b1d3a..261056f60a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h @@ -367,6 +367,9 @@ struct bt_mesh_model_pub { * @ref bt_mesh_model_pub.msg with a valid publication * message. * + * If the callback returns non-zero, the publication is skipped + * and will resume on the next periodic publishing interval. + * * @param mod The Model the Publication Context belogs to. * * @return Zero on success or (negative) error code otherwise.