From 1f9d289ee0ca53d767bbf0991e5b2f6b4eff4730 Mon Sep 17 00:00:00 2001
From: lly <lly@espressif.com>
Date: Fri, 21 Aug 2020 15:15:06 +0800
Subject: [PATCH] ble_mesh: stack: Avoid using assert in mesh stack

---
 components/bt/esp_ble_mesh/mesh_core/access.c       |  7 ++-----
 components/bt/esp_ble_mesh/mesh_core/friend.c       | 10 ++++++++--
 components/bt/esp_ble_mesh/mesh_core/lpn.c          |  2 +-
 components/bt/esp_ble_mesh/mesh_core/proxy_server.c | 10 ++++++++--
 4 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c
index 07019c5987..3b30bcaa5e 100644
--- a/components/bt/esp_ble_mesh/mesh_core/access.c
+++ b/components/bt/esp_ble_mesh/mesh_core/access.c
@@ -447,7 +447,7 @@ static void mod_publish(struct k_work *work)
     BT_DBG("%s", __func__);
 
     period_ms = bt_mesh_model_pub_period_get(pub->mod);
-    BT_INFO("period %u ms", period_ms);
+    BT_INFO("Publish period %u ms", period_ms);
 
     if (pub->count) {
         err = publish_retransmit(pub->mod);
@@ -469,14 +469,11 @@ static void mod_publish(struct k_work *work)
         return;
     }
 
-    __ASSERT_NO_MSG(pub->update != NULL);
-
     /* Callback the model publish update event to the application layer.
      * In the event, users can update the context of the publish message
      * which will be published in the next period.
      */
-    err = pub->update(pub->mod);
-    if (err) {
+    if (pub->update && pub->update(pub->mod)) {
         /* Cancel this publish attempt. */
         BT_ERR("Update failed, skipping publish (err %d)", err);
         pub->period_start = k_uptime_get_32();
diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c
index 0874142f35..b9955709a8 100644
--- a/components/bt/esp_ble_mesh/mesh_core/friend.c
+++ b/components/bt/esp_ble_mesh/mesh_core/friend.c
@@ -559,7 +559,10 @@ static struct net_buf *encode_update(struct bt_mesh_friend *frnd, u8_t md)
     NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*upd));
     struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx);
 
-    __ASSERT_NO_MSG(sub != NULL);
+    if (!sub) {
+        BT_ERR("Friend subnet 0x%04x not found", frnd->net_idx);
+        return NULL;
+    }
 
     BT_DBG("lpn 0x%04x md 0x%02x", frnd->lpn, md);
 
@@ -1194,7 +1197,10 @@ static void friend_timeout(struct k_work *work)
         .end = buf_send_end,
     };
 
-    __ASSERT_NO_MSG(frnd->pending_buf == 0U);
+    if (frnd->pending_buf != 0U) {
+        BT_ERR("Previous buffer not yet sent!");
+        return;
+    }
 
     BT_DBG("lpn 0x%04x send_last %u last %p", frnd->lpn,
            frnd->send_last, frnd->last);
diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c
index f398e7f7af..5ee5700460 100644
--- a/components/bt/esp_ble_mesh/mesh_core/lpn.c
+++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c
@@ -834,7 +834,7 @@ static void lpn_timeout(struct k_work *work)
         update_timeout(lpn);
         break;
     default:
-        __ASSERT(0, "Unhandled LPN state");
+        BT_ERR("Unhandled LPN state");
         break;
     }
 }
diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c
index 1527e0de4a..a47710205c 100644
--- a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c
+++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c
@@ -660,7 +660,10 @@ static ssize_t prov_ccc_write(struct bt_mesh_conn *conn,
 
     /* If a connection exists there must be a client */
     client = find_client(conn);
-    __ASSERT(client, "No client for connection");
+    if (!client) {
+        BT_ERR("No client for connection %p", conn);
+        return 0;
+    }
 
     if (client->filter_type == NONE) {
         client->filter_type = PROV;
@@ -795,7 +798,10 @@ static ssize_t proxy_ccc_write(struct bt_mesh_conn *conn,
 
     /* If a connection exists there must be a client */
     client = find_client(conn);
-    __ASSERT(client, "No client for connection");
+    if (!client) {
+        BT_ERR("No client for connection %p", conn);
+        return 0;
+    }
 
     if (client->filter_type == NONE) {
         client->filter_type = WHITELIST;