Просмотр исходного кода

Merge branch 'bugfix/ble_mesh_miscellaneous_fix' into 'master'

Bugfix/ble mesh miscellaneous fix

Closes BLEMESH-174

See merge request espressif/esp-idf!7881
Jiang Jiang Jian 5 лет назад
Родитель
Сommit
0367fe461e

+ 13 - 1
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c

@@ -73,10 +73,22 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp
 
 esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param)
 {
+    btc_ble_mesh_prov_args_t arg = {0};
+    btc_msg_t msg = {0};
+
     if (param == NULL) {
         return ESP_ERR_INVALID_ARG;
     }
 
-    return btc_ble_mesh_deinit(param);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
+
+    arg.mesh_deinit.param.erase_flash = param->erase_flash;
+
+    msg.sig = BTC_SIG_API_CALL;
+    msg.pid = BTC_PID_PROV;
+    msg.act = BTC_BLE_MESH_ACT_DEINIT_MESH;
+
+    return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL)
+            == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
 }
 

+ 10 - 3
components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h

@@ -380,11 +380,11 @@ typedef struct {
     /** Callback used to update publish message. Initialized by the stack. */
     esp_ble_mesh_cb_t update;
 
-    /** Role of the device that is going to publish messages */
-    uint8_t dev_role;
-
     /** Publish Period Timer. Initialized by the stack. */
     struct k_delayed_work timer;
+
+    /** Role of the device that is going to publish messages */
+    uint8_t dev_role;
 } esp_ble_mesh_model_pub_t;
 
 /** @def ESP_BLE_MESH_MODEL_PUB_DEFINE
@@ -792,6 +792,7 @@ typedef enum {
     ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT,         /*!< Proxy Client set filter type completion event */
     ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT,         /*!< Proxy Client add filter address completion event */
     ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT,      /*!< Proxy Client remove filter address completion event */
+    ESP_BLE_MESH_DEINIT_MESH_COMP_EVT,                          /*!< De-initialize BLE Mesh stack completion event */
     ESP_BLE_MESH_PROV_EVT_MAX,
 } esp_ble_mesh_prov_cb_event_t;
 
@@ -1259,6 +1260,12 @@ typedef union {
         uint8_t conn_handle;                    /*!< Proxy connection handle */
         uint16_t net_idx;                       /*!< Corresponding NetKey Index */
     } proxy_client_remove_filter_addr_comp;     /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT */
+    /**
+     * @brief ESP_BLE_MESH_DEINIT_MESH_COMP_EVT
+     */
+    struct ble_mesh_deinit_mesh_comp_param {
+        int err_code;                           /*!< Indicate the result of BLE Mesh deinitialization */
+    } deinit_mesh_comp;                         /*!< Event parameter of ESP_BLE_MESH_DEINIT_MESH_COMP_EVT */
 } esp_ble_mesh_prov_cb_param_t;
 
 /**

+ 4 - 5
components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c

@@ -934,11 +934,6 @@ static void btc_ble_mesh_proxy_client_filter_status_recv_cb(u8_t conn_handle,
 }
 #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
 
-int btc_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param)
-{
-    return bt_mesh_deinit((struct bt_mesh_deinit_param *)param);
-}
-
 int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model)
 {
     __ASSERT(model && model->op, "%s, Invalid parameter", __func__);
@@ -1884,6 +1879,10 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
         break;
     }
 #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
+    case BTC_BLE_MESH_ACT_DEINIT_MESH:
+        act = ESP_BLE_MESH_DEINIT_MESH_COMP_EVT;
+        param.deinit_mesh_comp.err_code = bt_mesh_deinit((struct bt_mesh_deinit_param *)&arg->mesh_deinit.param);
+        break;
     default:
         BT_WARN("%s, Invalid msg->act %d", __func__, msg->act);
         return;

+ 4 - 2
components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h

@@ -63,6 +63,7 @@ typedef enum {
     BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE,
     BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR,
     BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR,
+    BTC_BLE_MESH_ACT_DEINIT_MESH,
 } btc_ble_mesh_prov_act_t;
 
 typedef enum {
@@ -234,6 +235,9 @@ typedef union {
         uint16_t  addr_num;
         uint16_t *addr;
     } proxy_client_remove_filter_addr;
+    struct ble_mesh_deinit_args {
+        esp_ble_mesh_deinit_param_t param;
+    } mesh_deinit;
 } btc_ble_mesh_prov_args_t;
 
 typedef union {
@@ -266,8 +270,6 @@ esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t u
 
 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr);
 
-int btc_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param);
-
 int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model);
 
 int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model);

+ 23 - 3
components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c

@@ -186,18 +186,21 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler)
         }
         if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) {
             BT_ERR("%s Unable to add the timer to hash map.", __func__);
+            bt_mesh_alarm_unlock();
+            return;
         }
     }
-    bt_mesh_alarm_unlock();
 
     alarm = hash_map_get(bm_alarm_hash_map, work);
     if (alarm == NULL) {
         BT_WARN("%s, Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return;
     }
 
     // Just init the work timer only, don't start it.
     osi_alarm_cancel(alarm);
+    bt_mesh_alarm_unlock();
     return;
 }
 
@@ -208,15 +211,18 @@ int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay)
         return -EINVAL;
     }
 
+    bt_mesh_alarm_lock();
     osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work);
     if (alarm == NULL) {
         BT_WARN("%s, Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return -EINVAL;
     }
 
     // Cancel the alarm first, before start the alarm.
     osi_alarm_cancel(alarm);
     osi_alarm_set(alarm, delay);
+    bt_mesh_alarm_unlock();
     return 0;
 }
 
@@ -227,16 +233,18 @@ int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period)
         return -EINVAL;
     }
 
+    bt_mesh_alarm_lock();
     osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work);
     if (alarm == NULL) {
         BT_WARN("%s, Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return -EINVAL;
     }
 
     /* Cancel the alarm first before starting it. */
     osi_alarm_cancel(alarm);
     osi_alarm_set_periodic(alarm, period);
-
+    bt_mesh_alarm_unlock();
     return 0;
 }
 
@@ -247,14 +255,17 @@ int k_delayed_work_cancel(struct k_delayed_work *work)
         return -EINVAL;
     }
 
+    bt_mesh_alarm_lock();
     osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work);
     if (alarm == NULL) {
         BT_WARN("%s, Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return -EINVAL;
     }
 
     osi_alarm_cancel(alarm);
     alarm->deadline_us = 0;
+    bt_mesh_alarm_unlock();
     return 0;
 }
 
@@ -265,29 +276,38 @@ int k_delayed_work_free(struct k_delayed_work *work)
         return -EINVAL;
     }
 
+    bt_mesh_alarm_lock();
     osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work);
     if (alarm == NULL) {
         BT_WARN("%s Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return -EINVAL;
     }
 
     osi_alarm_cancel(alarm);
     hash_map_erase(bm_alarm_hash_map, work);
+    bt_mesh_alarm_unlock();
     return 0;
 }
 
 s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
 {
+    s32_t time = 0;
+
     if (!work || !bm_alarm_hash_map) {
         BT_ERR("%s, Invalid parameter", __func__);
         return 0;
     }
 
+    bt_mesh_alarm_lock();
     osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work);
     if (alarm == NULL) {
         BT_WARN("%s Unable to find expected alarm in hash map", __func__);
+        bt_mesh_alarm_unlock();
         return 0;
     }
 
-    return osi_alarm_get_remaining_ms(alarm);
+    time = osi_alarm_get_remaining_ms(alarm);
+    bt_mesh_alarm_unlock();
+    return time;
 }

+ 6 - 2
components/bt/esp_ble_mesh/mesh_core/access.c

@@ -396,13 +396,17 @@ static int publish_retransmit(struct bt_mesh_model *mod)
     };
     int err = 0;
 
-    key = bt_mesh_app_key_find(pub->key);
+    key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key);
     if (!key) {
         BT_ERR("%s, Failed to find AppKey", __func__);
         return -EADDRNOTAVAIL;
     }
 
-    tx.sub = bt_mesh_subnet_get(key->net_idx);
+    tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, key->net_idx);
+    if (!tx.sub) {
+        BT_ERR("%s, Failed to get subnet", __func__);
+        return -EADDRNOTAVAIL;
+    }
 
     ctx.net_idx = key->net_idx;
     ctx.app_idx = key->app_idx;

+ 14 - 13
components/bt/esp_ble_mesh/mesh_core/adv.c

@@ -329,8 +329,8 @@ void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool)
         struct net_buf *buf = &pool->__bufs[i];
         if (buf->ref > 1U) {
             buf->ref = 1U;
-            net_buf_unref(buf);
         }
+        net_buf_unref(buf);
     }
 }
 
@@ -372,6 +372,9 @@ static void bt_mesh_unref_buf(bt_mesh_msg_t *msg)
     if (msg->arg) {
         buf = (struct net_buf *)msg->arg;
         BLE_MESH_ADV(buf)->busy = 0U;
+        if (buf->ref > 1U) {
+            buf->ref = 1U;
+        }
         net_buf_unref(buf);
     }
 
@@ -770,7 +773,7 @@ void bt_mesh_adv_init(void)
     __ASSERT(adv_task.stack, "%s, Failed to create adv thread stack", __func__);
     adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL,
                                   configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE);
-    __ASSERT(adv_task.stack, "%s, Failed to create static adv thread stack", __func__);
+    __ASSERT(adv_task.handle, "%s, Failed to create static adv thread", __func__);
 #endif
 }
 
@@ -780,6 +783,15 @@ void bt_mesh_adv_deinit(void)
         return;
     }
 
+    vTaskDelete(adv_task.handle);
+    adv_task.handle = NULL;
+#if CONFIG_SPIRAM_USE_MALLOC
+    heap_caps_free(adv_task.stack);
+    adv_task.stack = NULL;
+    heap_caps_free(adv_task.task);
+    adv_task.task = NULL;
+#endif
+
 #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF)
     xQueueRemoveFromSet(xBleMeshQueue.queue, xBleMeshQueueSet);
     xQueueRemoveFromSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet);
@@ -811,17 +823,6 @@ void bt_mesh_adv_deinit(void)
 
     bt_mesh_unref_buf_from_pool(&adv_buf_pool);
     memset(adv_pool, 0, sizeof(adv_pool));
-
-    vTaskDelete(adv_task.handle);
-    adv_task.handle = NULL;
-#if CONFIG_SPIRAM_USE_MALLOC
-    heap_caps_free(adv_task.stack);
-    adv_task.stack = NULL;
-    /* Delay certain period for free adv_task.task */
-    vTaskDelay(10 / portTICK_PERIOD_MS);
-    heap_caps_free(adv_task.task);
-    adv_task.task = NULL;
-#endif
 }
 
 int bt_mesh_scan_enable(void)

+ 3 - 3
components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h

@@ -369,11 +369,11 @@ struct bt_mesh_model_pub {
      */
     int (*update)(struct bt_mesh_model *mod);
 
-    /* Change by Espressif, role of the device going to publish messages */
-    u8_t dev_role;
-
     /** Publish Period Timer. Only for stack-internal use. */
     struct k_delayed_work timer;
+
+    /* Change by Espressif, role of the device going to publish messages */
+    u8_t dev_role;
 };
 
 /** @def BLE_MESH_MODEL_PUB_DEFINE

+ 15 - 3
components/bt/esp_ble_mesh/mesh_core/main.c

@@ -33,6 +33,8 @@
 #define ACTION_SUSPEND  0x02
 #define ACTION_EXIT     0x03
 
+static bool mesh_init = false;
+
 int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
                       u8_t flags, u32_t iv_index, u16_t addr,
                       const u8_t dev_key[16])
@@ -309,6 +311,11 @@ int bt_mesh_init(const struct bt_mesh_prov *prov,
 {
     int err = 0;
 
+    if (mesh_init == true) {
+        BT_WARN("%s, Already", __func__);
+        return -EALREADY;
+    }
+
     bt_mesh_k_init();
 
     bt_mesh_hci_init();
@@ -374,6 +381,7 @@ int bt_mesh_init(const struct bt_mesh_prov *prov,
         bt_mesh_settings_init();
     }
 
+    mesh_init = true;
     return 0;
 }
 
@@ -386,6 +394,11 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
         return -EINVAL;
     }
 
+    if (mesh_init == false) {
+        BT_WARN("%s, Already", __func__);
+        return -EALREADY;
+    }
+
     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
         if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV)) {
             bt_mesh_beacon_disable();
@@ -424,9 +437,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
     bt_mesh_trans_deinit(param->erase);
     bt_mesh_net_deinit(param->erase);
 
-    if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
-        bt_mesh_beacon_deinit();
-    }
+    bt_mesh_beacon_deinit();
 
     if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) {
         if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
@@ -473,6 +484,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
 
     bt_mesh_k_deinit();
 
+    mesh_init = false;
     return 0;
 }
 

+ 6 - 3
components/bt/esp_ble_mesh/mesh_core/net.c

@@ -712,9 +712,12 @@ do_update:
 
     k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
 
-    for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
-        if (bt_mesh.sub[i].net_idx != BLE_MESH_KEY_UNUSED) {
-            bt_mesh_net_beacon_update(&bt_mesh.sub[i]);
+    size_t subnet_size = bt_mesh_rx_netkey_size();
+
+    for (i = 0; i < subnet_size; i++) {
+        struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
+        if (sub && sub->net_idx != BLE_MESH_KEY_UNUSED) {
+            bt_mesh_net_beacon_update(sub);
         }
     }