Explorar el Código

ble_mesh: stack: Rework using dev flag to check scan status

Different bluetooth host has different behaviors, so it's better
to maintain a scan check mechanism of BLE Mesh itself.
Fixes an issue when only PB-GATT is enabled for node, which will
output a scan error log when the device is provisioned.
lly hace 5 años
padre
commit
aeb44745bf

+ 1 - 1
components/bt/esp_ble_mesh/mesh_core/adv.c

@@ -971,7 +971,7 @@ int bt_mesh_scan_with_wl_enable(void)
     BT_DBG("%s", __func__);
 
     err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb);
-    if (err) {
+    if (err && err != -EALREADY) {
         BT_ERR("starting scan failed (err %d)", err);
         return err;
     }

+ 16 - 32
components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c

@@ -458,11 +458,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
 {
     int err = 0;
 
-#if BLE_MESH_DEV
     if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        BT_INFO("Scan is already started");
         return -EALREADY;
     }
-#endif
 
     if (!valid_scan_param(param)) {
         return -EINVAL;
@@ -481,26 +480,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
         return err;
     }
 
-#if BLE_MESH_DEV
     bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
-#endif
-
     bt_mesh_scan_dev_found_cb = cb;
-    return err;
+
+    return 0;
 }
 
 int bt_le_scan_stop(void)
 {
-#if BLE_MESH_DEV
-    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
-        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
-        BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
+    if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        BT_INFO("Scan is already stopped");
+        return -EALREADY;
     }
-#else
+
     BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
-#endif
 
+    bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
     bt_mesh_scan_dev_found_cb = NULL;
+
     return 0;
 }
 
@@ -1197,13 +1194,10 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
         return -ENOMEM;
     }
 
-#if BLE_MESH_DEV
-    if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
         BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
+        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
     }
-#else
-    BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
-#endif /* BLE_MESH_DEV */
 
     BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
 
@@ -1607,30 +1601,20 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
         break;
     case BTA_GATTC_EXEC_EVT:
         break;
-    case BTA_GATTC_OPEN_EVT: {
+    case BTA_GATTC_OPEN_EVT:
         BT_DBG("BTA_GATTC_OPEN_EVT");
-        /** After current connection is established, provisioner can
-         *  use BTA_DmBleScan() to re-enable scan.
+        /* After current connection is established, Provisioner can
+         * use BTM_BleScan() to re-enable scan.
          */
-        tBTM_STATUS status;
-#if BLE_MESH_DEV
         if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
-            status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
+            tBTM_STATUS status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
             if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
-                BT_ERR("%s, Invalid status %d", __func__, status);
+                BT_ERR("%s, Invalid scan status %d", __func__, status);
                 break;
             }
             bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
         }
-#else
-        status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
-        if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
-            BT_ERR("%s, Invalid status %d", __func__, status);
-            break;
-        }
-#endif /* BLE_MESH_DEV */
         break;
-    }
     case BTA_GATTC_CLOSE_EVT:
         BT_DBG("BTA_GATTC_CLOSE_EVT");
         break;

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

@@ -81,9 +81,6 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
         bt_mesh_store_iv(false);
     }
 
-    /* Add this to avoid "already active status" for bt_mesh_scan_enable() */
-    bt_mesh_scan_disable();
-
     bt_mesh_net_start();
 
     return 0;

+ 12 - 29
components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c

@@ -418,22 +418,14 @@ static int disc_cb(struct ble_gap_event *event, void *arg)
                 }
             }
         }
-#if BLE_MESH_DEV
         if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
             rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
             if (rc != 0) {
-                BT_ERR("%s, Invalid status %d", __func__, rc);
+                BT_ERR("%s, Invalid scan status %d", __func__, rc);
                 break;
             }
             bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
         }
-#else
-        rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
-        if (rc != 0) {
-            BT_ERR("%s, Invalid status %d", __func__, rc);
-            break;
-        }
-#endif /* BLE_MESH_DEV */
         break;
     case BLE_GAP_EVENT_DISCONNECT:
         if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) {
@@ -937,11 +929,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
 {
     int err;
 
-#if BLE_MESH_DEV
     if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        BT_INFO("Scan is already started");
         return -EALREADY;
     }
-#endif
 
 #if BLE_MESH_DEV
     if (param->filter_dup) {
@@ -956,26 +947,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
         return err;
     }
 
-#if BLE_MESH_DEV
     bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
-#endif
-
     bt_mesh_scan_dev_found_cb = cb;
-    return err;
+
+    return 0;
 }
 
 int bt_le_scan_stop(void)
 {
-#if BLE_MESH_DEV
-    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
-        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
-        ble_gap_disc_cancel();
+    if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        BT_INFO("Scan is already stopped");
+        return -EALREADY;
     }
-#else
+
     ble_gap_disc_cancel();
-#endif
 
+    bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
     bt_mesh_scan_dev_found_cb = NULL;
+
     return 0;
 }
 
@@ -1419,19 +1408,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
         return -ENOMEM;
     }
 
-#if BLE_MESH_DEV
-    if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
         rc = ble_gap_disc_cancel();
         if (rc != 0) {
             return -1;
         }
+        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
     }
-#else
-    rc = ble_gap_disc_cancel();
-    if (rc != 0) {
-        return -1;
-    }
-#endif /* BLE_MESH_DEV */
 
     BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));