Skip to content

Commit 049a3e1

Browse files
committed
Merge branch 'bugfix/ble_mesh_add_misc_check' into 'master'
Bugfix/ble mesh add misc check See merge request espressif/esp-idf!9490
2 parents fb192fd + 82d4bd2 commit 049a3e1

File tree

10 files changed

+140
-90
lines changed

10 files changed

+140
-90
lines changed

components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,29 @@ bool esp_ble_mesh_node_is_provisioned(void)
3535
return bt_mesh_is_provisioned();
3636
}
3737

38+
static bool prov_bearers_valid(esp_ble_mesh_prov_bearer_t bearers)
39+
{
40+
if ((!(bearers & (ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT))) ||
41+
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
42+
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
43+
!(bearers & ESP_BLE_MESH_PROV_ADV)) ||
44+
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
45+
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
46+
!(bearers & ESP_BLE_MESH_PROV_GATT))) {
47+
return false;
48+
}
49+
return true;
50+
}
51+
3852
esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers)
3953
{
4054
btc_ble_mesh_prov_args_t arg = {0};
4155
btc_msg_t msg = {0};
4256

57+
if (prov_bearers_valid(bearers) == false) {
58+
return ESP_ERR_INVALID_ARG;
59+
}
60+
4361
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
4462

4563
msg.sig = BTC_SIG_API_CALL;
@@ -56,6 +74,10 @@ esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers)
5674
btc_ble_mesh_prov_args_t arg = {0};
5775
btc_msg_t msg = {0};
5876

77+
if (prov_bearers_valid(bearers) == false) {
78+
return ESP_ERR_INVALID_ARG;
79+
}
80+
5981
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
6082

6183
msg.sig = BTC_SIG_API_CALL;
@@ -232,6 +254,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearer
232254
btc_ble_mesh_prov_args_t arg = {0};
233255
btc_msg_t msg = {0};
234256

257+
if (prov_bearers_valid(bearers) == false) {
258+
return ESP_ERR_INVALID_ARG;
259+
}
260+
235261
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
236262

237263
msg.sig = BTC_SIG_API_CALL;
@@ -249,6 +275,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t beare
249275
btc_ble_mesh_prov_args_t arg = {0};
250276
btc_msg_t msg = {0};
251277

278+
if (prov_bearers_valid(bearers) == false) {
279+
return ESP_ERR_INVALID_ARG;
280+
}
281+
252282
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
253283

254284
msg.sig = BTC_SIG_API_CALL;

components/bt/esp_ble_mesh/mesh_core/adv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ int bt_mesh_scan_with_wl_enable(void)
971971
BT_DBG("%s", __func__);
972972

973973
err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb);
974-
if (err) {
974+
if (err && err != -EALREADY) {
975975
BT_ERR("starting scan failed (err %d)", err);
976976
return err;
977977
}

components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
458458
{
459459
int err = 0;
460460

461-
#if BLE_MESH_DEV
462461
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
462+
BT_INFO("Scan is already started");
463463
return -EALREADY;
464464
}
465-
#endif
466465

467466
if (!valid_scan_param(param)) {
468467
return -EINVAL;
@@ -481,26 +480,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
481480
return err;
482481
}
483482

484-
#if BLE_MESH_DEV
485483
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
486-
#endif
487-
488484
bt_mesh_scan_dev_found_cb = cb;
489-
return err;
485+
486+
return 0;
490487
}
491488

492489
int bt_le_scan_stop(void)
493490
{
494-
#if BLE_MESH_DEV
495-
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
496-
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
497-
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
491+
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
492+
BT_INFO("Scan is already stopped");
493+
return -EALREADY;
498494
}
499-
#else
495+
500496
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
501-
#endif
502497

498+
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
503499
bt_mesh_scan_dev_found_cb = NULL;
500+
504501
return 0;
505502
}
506503

@@ -1197,13 +1194,10 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
11971194
return -ENOMEM;
11981195
}
11991196

1200-
#if BLE_MESH_DEV
1201-
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
1197+
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
12021198
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
1199+
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
12031200
}
1204-
#else
1205-
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
1206-
#endif /* BLE_MESH_DEV */
12071201

12081202
BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
12091203

@@ -1607,30 +1601,20 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
16071601
break;
16081602
case BTA_GATTC_EXEC_EVT:
16091603
break;
1610-
case BTA_GATTC_OPEN_EVT: {
1604+
case BTA_GATTC_OPEN_EVT:
16111605
BT_DBG("BTA_GATTC_OPEN_EVT");
1612-
/** After current connection is established, provisioner can
1613-
* use BTA_DmBleScan() to re-enable scan.
1606+
/* After current connection is established, Provisioner can
1607+
* use BTM_BleScan() to re-enable scan.
16141608
*/
1615-
tBTM_STATUS status;
1616-
#if BLE_MESH_DEV
16171609
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
1618-
status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
1610+
tBTM_STATUS status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
16191611
if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
1620-
BT_ERR("%s, Invalid status %d", __func__, status);
1612+
BT_ERR("%s, Invalid scan status %d", __func__, status);
16211613
break;
16221614
}
16231615
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
16241616
}
1625-
#else
1626-
status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
1627-
if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
1628-
BT_ERR("%s, Invalid status %d", __func__, status);
1629-
break;
1630-
}
1631-
#endif /* BLE_MESH_DEV */
16321617
break;
1633-
}
16341618
case BTA_GATTC_CLOSE_EVT:
16351619
BT_DBG("BTA_GATTC_CLOSE_EVT");
16361620
break;

components/bt/esp_ble_mesh/mesh_core/main.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
8181
bt_mesh_store_iv(false);
8282
}
8383

84-
/* Add this to avoid "already active status" for bt_mesh_scan_enable() */
85-
bt_mesh_scan_disable();
86-
8784
bt_mesh_net_start();
8885

8986
return 0;
@@ -171,12 +168,32 @@ bool bt_mesh_is_provisioner_en(void)
171168
}
172169
}
173170

171+
static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers)
172+
{
173+
if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) ||
174+
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
175+
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
176+
!(bearers & BLE_MESH_PROV_ADV)) ||
177+
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
178+
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
179+
!(bearers & BLE_MESH_PROV_GATT))) {
180+
BT_ERR("Invalid bearers 0x%02x", bearers);
181+
return false;
182+
}
183+
return true;
184+
}
185+
174186
int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
175187
{
176188
if (bt_mesh_is_provisioned()) {
189+
BT_WARN("%s, Already", __func__);
177190
return -EALREADY;
178191
}
179192

193+
if (prov_bearers_valid(bearers) == false) {
194+
return -EINVAL;
195+
}
196+
180197
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
181198

182199
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@@ -206,6 +223,10 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
206223
return -EALREADY;
207224
}
208225

226+
if (prov_bearers_valid(bearers) == false) {
227+
return -EINVAL;
228+
}
229+
209230
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE);
210231

211232
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@@ -509,27 +530,22 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers)
509530
}
510531
#endif
511532

512-
if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
513-
(bearers & BLE_MESH_PROV_ADV)) ||
514-
(IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
515-
(bearers & BLE_MESH_PROV_GATT))) {
516-
bt_mesh_scan_enable();
517-
}
518-
519533
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
520534
(bearers & BLE_MESH_PROV_GATT)) {
521535
bt_mesh_provisioner_pb_gatt_enable();
522536
}
523537

524538
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
525539

540+
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
541+
bt_mesh_friend_init();
542+
}
543+
526544
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
527545
bt_mesh_beacon_enable();
528546
}
529547

530-
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
531-
bt_mesh_friend_init();
532-
}
548+
bt_mesh_scan_enable();
533549

534550
return 0;
535551
}
@@ -543,6 +559,10 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
543559
return -EALREADY;
544560
}
545561

562+
if (prov_bearers_valid(bearers) == false) {
563+
return -EINVAL;
564+
}
565+
546566
err = bt_mesh_provisioner_set_prov_info();
547567
if (err) {
548568
BT_ERR("%s, Failed to set provisioning info", __func__);
@@ -573,9 +593,13 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers)
573593
return -EALREADY;
574594
}
575595

596+
if (prov_bearers_valid(bearers) == false) {
597+
return -EINVAL;
598+
}
599+
576600
enable = bt_mesh_provisioner_get_prov_bearer();
577601
if (!(enable & bearers)) {
578-
BT_ERR("%s, Bearers mismatch", __func__);
602+
BT_ERR("Mismatch bearers 0x%02x", bearers);
579603
return -EINVAL;
580604
}
581605

components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -418,22 +418,14 @@ static int disc_cb(struct ble_gap_event *event, void *arg)
418418
}
419419
}
420420
}
421-
#if BLE_MESH_DEV
422421
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
423422
rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
424423
if (rc != 0) {
425-
BT_ERR("%s, Invalid status %d", __func__, rc);
424+
BT_ERR("%s, Invalid scan status %d", __func__, rc);
426425
break;
427426
}
428427
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
429428
}
430-
#else
431-
rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
432-
if (rc != 0) {
433-
BT_ERR("%s, Invalid status %d", __func__, rc);
434-
break;
435-
}
436-
#endif /* BLE_MESH_DEV */
437429
break;
438430
case BLE_GAP_EVENT_DISCONNECT:
439431
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
937929
{
938930
int err;
939931

940-
#if BLE_MESH_DEV
941932
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
933+
BT_INFO("Scan is already started");
942934
return -EALREADY;
943935
}
944-
#endif
945936

946937
#if BLE_MESH_DEV
947938
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
956947
return err;
957948
}
958949

959-
#if BLE_MESH_DEV
960950
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
961-
#endif
962-
963951
bt_mesh_scan_dev_found_cb = cb;
964-
return err;
952+
953+
return 0;
965954
}
966955

967956
int bt_le_scan_stop(void)
968957
{
969-
#if BLE_MESH_DEV
970-
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
971-
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
972-
ble_gap_disc_cancel();
958+
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
959+
BT_INFO("Scan is already stopped");
960+
return -EALREADY;
973961
}
974-
#else
962+
975963
ble_gap_disc_cancel();
976-
#endif
977964

965+
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
978966
bt_mesh_scan_dev_found_cb = NULL;
967+
979968
return 0;
980969
}
981970

@@ -1419,19 +1408,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
14191408
return -ENOMEM;
14201409
}
14211410

1422-
#if BLE_MESH_DEV
1423-
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
1411+
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
14241412
rc = ble_gap_disc_cancel();
14251413
if (rc != 0) {
14261414
return -1;
14271415
}
1416+
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
14281417
}
1429-
#else
1430-
rc = ble_gap_disc_cancel();
1431-
if (rc != 0) {
1432-
return -1;
1433-
}
1434-
#endif /* BLE_MESH_DEV */
14351418

14361419
BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
14371420

components/bt/esp_ble_mesh/mesh_core/proxy_client.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ static void proxy_cfg(struct bt_mesh_proxy_server *server)
154154
u8_t opcode = 0U;
155155
int err = 0;
156156

157+
if (server->buf.len > 29) {
158+
BT_ERR("Too large proxy cfg pdu (len %d)", server->buf.len);
159+
return;
160+
}
161+
157162
err = bt_mesh_net_decode(&server->buf, BLE_MESH_NET_IF_PROXY_CFG,
158163
&rx, &buf);
159164
if (err) {

0 commit comments

Comments
 (0)