ble_mesh: stack: Add more checks about input prov bearers

This commit is contained in:
lly 2020-07-04 20:55:48 +08:00
parent daf8098a00
commit 37322ce93f
2 changed files with 68 additions and 11 deletions

View file

@ -35,11 +35,29 @@ bool esp_ble_mesh_node_is_provisioned(void)
return bt_mesh_is_provisioned();
}
static bool prov_bearers_valid(esp_ble_mesh_prov_bearer_t bearers)
{
if ((!(bearers & (ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT))) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & ESP_BLE_MESH_PROV_ADV)) ||
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & ESP_BLE_MESH_PROV_GATT))) {
return false;
}
return true;
}
esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers)
{
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
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)
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
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
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
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
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;

View file

@ -171,12 +171,32 @@ bool bt_mesh_is_provisioner_en(void)
}
}
static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers)
{
if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & BLE_MESH_PROV_ADV)) ||
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & BLE_MESH_PROV_GATT))) {
BT_ERR("Invalid bearers 0x%02x", bearers);
return false;
}
return true;
}
int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
BT_WARN("%s, Already", __func__);
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@ -206,6 +226,10 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@ -509,13 +533,6 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers)
}
#endif
if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
(bearers & BLE_MESH_PROV_ADV)) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
(bearers & BLE_MESH_PROV_GATT))) {
bt_mesh_scan_enable();
}
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
(bearers & BLE_MESH_PROV_GATT)) {
bt_mesh_provisioner_pb_gatt_enable();
@ -523,13 +540,15 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers)
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
bt_mesh_beacon_enable();
}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}
bt_mesh_scan_enable();
return 0;
}
@ -543,6 +562,10 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
err = bt_mesh_provisioner_set_prov_info();
if (err) {
BT_ERR("%s, Failed to set provisioning info", __func__);
@ -573,9 +596,13 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
enable = bt_mesh_provisioner_get_prov_bearer();
if (!(enable & bearers)) {
BT_ERR("%s, Bearers mismatch", __func__);
BT_ERR("Mismatch bearers 0x%02x", bearers);
return -EINVAL;
}