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.
This commit is contained in:
lly
2020-07-06 11:41:01 +08:00
committed by bot
parent 43ffec0bd7
commit 55489bb41a
4 changed files with 31 additions and 67 deletions

View File

@ -971,7 +971,7 @@ int bt_mesh_scan_with_wl_enable(void)
BT_DBG("%s", __func__); BT_DBG("%s", __func__);
err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); 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); BT_ERR("starting scan failed (err %d)", err);
return err; return err;
} }

View File

@ -459,11 +459,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
{ {
int err = 0; int err = 0;
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already started");
return -EALREADY; return -EALREADY;
} }
#endif
if (!valid_scan_param(param)) { if (!valid_scan_param(param)) {
return -EINVAL; return -EINVAL;
@ -482,26 +481,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
return err; return err;
} }
#if BLE_MESH_DEV
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
#endif
bt_mesh_scan_dev_found_cb = cb; bt_mesh_scan_dev_found_cb = cb;
return err;
return 0;
} }
int bt_le_scan_stop(void) int bt_le_scan_stop(void)
{ {
#if BLE_MESH_DEV if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { BT_INFO("Scan is already stopped");
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); return -EALREADY;
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
} }
#else
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
#endif
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);
bt_mesh_scan_dev_found_cb = NULL; bt_mesh_scan_dev_found_cb = NULL;
return 0; return 0;
} }
@ -1198,13 +1195,10 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
return -ENOMEM; return -ENOMEM;
} }
#if BLE_MESH_DEV if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL)); 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)); BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
@ -1608,30 +1602,20 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
break; break;
case BTA_GATTC_EXEC_EVT: case BTA_GATTC_EXEC_EVT:
break; break;
case BTA_GATTC_OPEN_EVT: { case BTA_GATTC_OPEN_EVT:
BT_DBG("BTA_GATTC_OPEN_EVT"); BT_DBG("BTA_GATTC_OPEN_EVT");
/** After current connection is established, provisioner can /* After current connection is established, Provisioner can
* use BTA_DmBleScan() to re-enable scan. * 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)) { 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) { 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; break;
} }
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); 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; break;
}
case BTA_GATTC_CLOSE_EVT: case BTA_GATTC_CLOSE_EVT:
BT_DBG("BTA_GATTC_CLOSE_EVT"); BT_DBG("BTA_GATTC_CLOSE_EVT");
break; break;

View File

@ -81,9 +81,6 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
bt_mesh_store_iv(false); 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(); bt_mesh_net_start();
return 0; return 0;

View File

@ -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)) { 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); rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
if (rc != 0) { if (rc != 0) {
BT_ERR("%s, Invalid status %d", __func__, rc); BT_ERR("%s, Invalid scan status %d", __func__, rc);
break; break;
} }
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); 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; break;
case BLE_GAP_EVENT_DISCONNECT: case BLE_GAP_EVENT_DISCONNECT:
if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) { 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; int err;
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already started");
return -EALREADY; return -EALREADY;
} }
#endif
#if BLE_MESH_DEV #if BLE_MESH_DEV
if (param->filter_dup) { 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; return err;
} }
#if BLE_MESH_DEV
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
#endif
bt_mesh_scan_dev_found_cb = cb; bt_mesh_scan_dev_found_cb = cb;
return err;
return 0;
} }
int bt_le_scan_stop(void) int bt_le_scan_stop(void)
{ {
#if BLE_MESH_DEV if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { BT_INFO("Scan is already stopped");
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); return -EALREADY;
ble_gap_disc_cancel();
} }
#else
ble_gap_disc_cancel();
#endif
ble_gap_disc_cancel();
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
bt_mesh_scan_dev_found_cb = NULL; bt_mesh_scan_dev_found_cb = NULL;
return 0; return 0;
} }
@ -1419,19 +1408,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
return -ENOMEM; return -ENOMEM;
} }
#if BLE_MESH_DEV if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
rc = ble_gap_disc_cancel(); rc = ble_gap_disc_cancel();
if (rc != 0) { if (rc != 0) {
return -1; 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)); BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));