From e3b304997f02f01ab8dee325f23eadf2e53accef Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:00:24 +0800 Subject: [PATCH] ble_mesh: fix finding netkey/appkey/devkey for tx/rx msg --- components/bt/esp_ble_mesh/mesh_core/access.c | 466 +++++++++++++----- components/bt/esp_ble_mesh/mesh_core/access.h | 18 + components/bt/esp_ble_mesh/mesh_core/net.c | 86 +--- .../esp_ble_mesh/mesh_core/provisioner_main.c | 14 +- .../esp_ble_mesh/mesh_core/provisioner_main.h | 8 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 146 +----- 6 files changed, 397 insertions(+), 341 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index d8365ecdd5..529d8ebab1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -728,67 +728,72 @@ void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode) net_buf_simple_add_le16(msg, opcode & 0xffff); } +static bool ready_to_send(u8_t role, u16_t dst) +{ +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (!bt_mesh_is_provisioned()) { + BT_ERR("%s, Local node is not yet provisioned", __func__); + return false; + } + if (!bt_mesh_is_provisioner_en()) { + return true; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (!provisioner_check_msg_dst_addr(dst)) { + BT_ERR("%s, Failed to find DST 0x%04x", __func__, dst); + return false; + } + if (bt_mesh_is_provisioner_en()) { + return true; + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (!provisioner_check_msg_dst_addr(dst)) { + BT_ERR("%s, Failed to check DST", __func__); + return false; + } + if (bt_mesh_is_provisioner_en()) { + return true; + } + } else { + if (!bt_mesh_is_provisioned()) { + BT_ERR("%s, Local node is not yet provisioned", __func__); + return false; + } + + return true; + } +#endif + + return false; +} + static int model_send(struct bt_mesh_model *model, struct bt_mesh_net_tx *tx, bool implicit_bind, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data) { - bool check = false; u8_t role; - BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, - tx->ctx->app_idx, tx->ctx->addr); - BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - role = bt_mesh_get_device_role(model, tx->ctx->srv_send); if (role == ROLE_NVAL) { BT_ERR("%s, Failed to get model role", __func__); return -EINVAL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return -EAGAIN; - } - if (!bt_mesh_is_provisioner_en()) { - check = true; - } - } -#endif + BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, + tx->ctx->app_idx, tx->ctx->addr); + BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(tx->ctx->addr)) { - BT_ERR("%s, Failed to check DST", __func__); - return -EINVAL; - } - if (bt_mesh_is_provisioner_en()) { - check = true; - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(tx->ctx->addr)) { - BT_ERR("%s, Failed to check DST", __func__); - return -EINVAL; - } - if (bt_mesh_is_provisioner_en()) { - check = true; - } - } else { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return -EAGAIN; - } - check = true; - } -#endif - - if (!check) { + if (!ready_to_send(role, tx->ctx->addr)) { BT_ERR("%s, fail", __func__); return -EINVAL; } @@ -825,36 +830,7 @@ int bt_mesh_model_send(struct bt_mesh_model *model, return -EINVAL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - sub = bt_mesh_subnet_get(ctx->net_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(ctx->net_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - sub = bt_mesh_subnet_get(ctx->net_idx); - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(ctx->net_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - sub = get_fast_prov_subnet(ctx->net_idx); -#endif - } -#endif - + sub = bt_mesh_tx_netkey_get(role, ctx->net_idx); if (!sub) { BT_ERR("%s, Failed to get subnet", __func__); return -EINVAL; @@ -899,34 +875,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) return -EADDRNOTAVAIL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(pub->key); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(pub->key); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(pub->key); - } - } else if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(pub->key); - } - } -#endif - + key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key, BLE_MESH_KEY_ANY); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return -EADDRNOTAVAIL; @@ -950,34 +899,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) tx.friend_cred = pub->cred; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - tx.sub = bt_mesh_subnet_get(ctx.net_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - tx.sub = provisioner_subnet_get(ctx.net_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - tx.sub = bt_mesh_subnet_get(ctx.net_idx); - } - } else if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - tx.sub = provisioner_subnet_get(ctx.net_idx); - } - } -#endif - + tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, ctx.net_idx); if (!tx.sub) { BT_ERR("%s, Failed to get subnet", __func__); return -EADDRNOTAVAIL; @@ -1041,3 +963,281 @@ const struct bt_mesh_comp *bt_mesh_comp_get(void) { return dev_comp; } + +/* APIs used by messages encryption in upper transport layer & network layer */ +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + sub = bt_mesh_subnet_get(net_idx); + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + sub = provisioner_subnet_get(net_idx); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + sub = bt_mesh_subnet_get(net_idx); + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + sub = provisioner_subnet_get(net_idx); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + sub = fast_prov_subnet_get(net_idx); +#endif + } +#endif + + return sub; +} + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst) +{ + const u8_t *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(dst); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(dst); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + key = fast_prov_dev_key_get(dst); +#endif + } +#endif + + return key; +} + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx) +{ + struct bt_mesh_app_key *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh_app_key_find(app_idx); + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_app_key_find(app_idx); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh_app_key_find(app_idx); + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_app_key_find(app_idx); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + key = fast_prov_app_key_find(net_idx, app_idx); +#endif + } +#endif + + return key; +} + +/* APIs used by messages decryption in network layer & upper transport layer */ +size_t bt_mesh_rx_netkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.sub); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.sub); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + + return size; +} + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index) +{ + struct bt_mesh_subnet *sub = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + sub = &bt_mesh.sub[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + sub = bt_mesh.p_sub[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.sub)) { + sub = &bt_mesh.sub[index]; + } else { + sub = bt_mesh.p_sub[index - ARRAY_SIZE(bt_mesh.sub)]; + } +#endif + + return sub; +} + +size_t bt_mesh_rx_devkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (!bt_mesh_is_provisioner_en()) { + size = 1; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = 1; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = 1; + if (bt_mesh_is_provisioner_en()) { + size += 1; + } +#endif + + return size; +} + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) +{ + const u8_t *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(src); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < 1) { + key = bt_mesh.dev_key; + } else { + key = provisioner_dev_key_get(src); + } +#endif + + return key; +} + +size_t bt_mesh_rx_appkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.app_keys); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.app_keys); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + + return size; +} + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index) +{ + struct bt_mesh_app_key *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = &bt_mesh.app_keys[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = bt_mesh.p_app_keys[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.app_keys)) { + key = &bt_mesh.app_keys[index]; + } else { + key = bt_mesh.p_app_keys[index - ARRAY_SIZE(bt_mesh.app_keys)]; + } +#endif + + return key; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/access.h b/components/bt/esp_ble_mesh/mesh_core/access.h index 114c1662be..119ad23627 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.h +++ b/components/bt/esp_ble_mesh/mesh_core/access.h @@ -57,4 +57,22 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); int bt_mesh_comp_register(const struct bt_mesh_comp *comp); +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx); + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst); + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx); + +size_t bt_mesh_rx_netkey_size(void); + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index); + +size_t bt_mesh_rx_devkey_size(void); + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src); + +size_t bt_mesh_rx_appkey_size(void); + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index); + #endif /* _ACCESS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 020ea02707..a944f83092 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -1078,56 +1078,15 @@ static bool net_find_and_decrypt(const u8_t *data, size_t data_len, struct net_buf_simple *buf) { struct bt_mesh_subnet *sub = NULL; - u32_t array_size = 0; + size_t array_size = 0; int i; BT_DBG("%s", __func__); -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.sub); - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.p_sub); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = ARRAY_SIZE(bt_mesh.sub); - if (bt_mesh_is_provisioner_en()) { - array_size += ARRAY_SIZE(bt_mesh.p_sub); - } -#endif - - if (!array_size) { - BT_ERR("%s, Unable to get subnet size", __func__); - return false; - } + array_size = bt_mesh_rx_netkey_size(); for (i = 0; i < array_size; i++) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - sub = &bt_mesh.sub[i]; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - sub = bt_mesh.p_sub[i]; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < ARRAY_SIZE(bt_mesh.sub)) { - sub = &bt_mesh.sub[i]; - } else { - sub = bt_mesh.p_sub[i - ARRAY_SIZE(bt_mesh.sub)]; - } -#endif - + sub = bt_mesh_rx_netkey_get(i); if (!sub) { BT_DBG("%s, NULL subnet", __func__); continue; @@ -1373,6 +1332,29 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, return 0; } +static bool ready_to_recv(void) +{ +#if CONFIG_BLE_MESH_NODE + if (!bt_mesh_is_provisioner_en()) { + if (!bt_mesh_is_provisioned()) { + return false; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (!bt_mesh_is_provisioner_en()) { + BT_WARN("%s, Provisioner is disabled", __func__); + return false; + } + if (!provisioner_get_prov_node_count()) { + return false; + } +#endif + + return true; +} + void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, enum bt_mesh_net_if net_if) { @@ -1382,23 +1364,9 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, BT_DBG("rssi %d net_if %u", rssi, net_if); -#if CONFIG_BLE_MESH_NODE - if (!bt_mesh_is_provisioner_en()) { - if (!bt_mesh_is_provisioned()) { - return; - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is disabled", __func__); + if (!ready_to_recv()) { return; } - if (!provisioner_get_prov_node_count()) { - return; - } -#endif if (bt_mesh_net_decode(data, net_if, &rx, &buf)) { return; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 586fd33842..44049fd0c8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -330,7 +330,7 @@ bool provisioner_check_msg_dst_addr(u16_t dst_addr) return false; } -const u8_t *provisioner_get_device_key(u16_t dst_addr) +const u8_t *provisioner_dev_key_get(u16_t dst_addr) { /* Device key is only used to encrypt configuration messages. * Configuration model shall only be supported by the primary @@ -1140,7 +1140,7 @@ int bt_mesh_provisioner_print_local_element_info(void) #if CONFIG_BLE_MESH_FAST_PROV -const u8_t *get_fast_prov_device_key(u16_t addr) +const u8_t *fast_prov_dev_key_get(u16_t addr) { struct bt_mesh_node_t *node = NULL; @@ -1165,7 +1165,7 @@ const u8_t *get_fast_prov_device_key(u16_t addr) return NULL; } -struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx) +struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; @@ -1188,7 +1188,7 @@ struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx) return NULL; } -struct bt_mesh_app_key *get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) +struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; @@ -1216,7 +1216,7 @@ u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) struct bt_mesh_subnet *sub = NULL; struct bt_mesh_subnet_keys *key = NULL; - sub = get_fast_prov_subnet(net_idx); + sub = fast_prov_subnet_get(net_idx); if (sub) { key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; return provisioner_set_fast_prov_net_idx(key->net, net_idx); @@ -1253,7 +1253,7 @@ const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; - sub = get_fast_prov_subnet(net_idx); + sub = fast_prov_subnet_get(net_idx); if (!sub) { BT_ERR("%s, Failed to get subnet", __func__); return NULL; @@ -1266,7 +1266,7 @@ const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; - key = get_fast_prov_app_key(net_idx, app_idx); + key = fast_prov_app_key_find(net_idx, app_idx); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h index 50bb656b2c..6fe4150a88 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -55,7 +55,7 @@ struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx); bool provisioner_check_msg_dst_addr(u16_t dst_addr); -const u8_t *provisioner_get_device_key(u16_t dst_addr); +const u8_t *provisioner_dev_key_get(u16_t dst_addr); struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx); @@ -105,11 +105,11 @@ int bt_mesh_provisioner_print_local_element_info(void); /* The following APIs are for fast provisioning */ -const u8_t *get_fast_prov_device_key(u16_t dst_addr); +const u8_t *fast_prov_dev_key_get(u16_t dst_addr); -struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx); +struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx); -struct bt_mesh_app_key *get_fast_prov_app_key(u16_t net_idx, u16_t app_idx); +struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx); u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 98f93cb04e..82a0ddfae9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -481,36 +481,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, } if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - key = bt_mesh.dev_key; - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_get_device_key(tx->ctx->addr); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - key = bt_mesh.dev_key; - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_get_device_key(tx->ctx->addr); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - key = get_fast_prov_device_key(tx->ctx->addr); -#endif - } -#endif - + key = bt_mesh_tx_devkey_get(role, tx->ctx->addr); if (!key) { BT_ERR("%s, Failed to get Device Key", __func__); return -EINVAL; @@ -520,36 +491,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, } else { struct bt_mesh_app_key *app_key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - app_key = bt_mesh_app_key_find(tx->ctx->app_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - app_key = provisioner_app_key_find(tx->ctx->app_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - app_key = bt_mesh_app_key_find(tx->ctx->app_idx); - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - app_key = provisioner_app_key_find(tx->ctx->app_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - app_key = get_fast_prov_app_key(tx->ctx->net_idx, tx->ctx->app_idx); -#endif - } -#endif - + app_key = bt_mesh_tx_appkey_get(role, tx->ctx->app_idx, tx->ctx->net_idx); if (!app_key) { BT_ERR("%s, Failed to get AppKey", __func__); return -EINVAL; @@ -677,7 +619,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, u8_t aszmic, struct net_buf_simple *buf) { struct net_buf_simple *sdu = NULL; - u32_t array_size = 0; + size_t array_size = 0; u8_t *ad; u16_t i; int err; @@ -715,48 +657,12 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, } if (!AKF(&hdr)) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = 1; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = 1; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = 1; - if (bt_mesh_is_provisioner_en()) { - array_size += 1; - } -#endif + array_size = bt_mesh_rx_devkey_size(); for (i = 0; i < array_size; i++) { const u8_t *dev_key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - dev_key = bt_mesh.dev_key; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - dev_key = provisioner_get_device_key(rx->ctx.addr); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < 1) { - dev_key = bt_mesh.dev_key; - } else { - dev_key = provisioner_get_device_key(rx->ctx.addr); - } -#endif - + dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr); if (!dev_key) { BT_DBG("%s, NULL Device Key", __func__); continue; @@ -783,49 +689,13 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, return -ENODEV; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.app_keys); - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.p_app_keys); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = ARRAY_SIZE(bt_mesh.app_keys); - if (bt_mesh_is_provisioner_en()) { - array_size += ARRAY_SIZE(bt_mesh.p_app_keys); - } -#endif + array_size = bt_mesh_rx_appkey_size(); for (i = 0; i < array_size; i++) { - struct bt_mesh_app_key *key = NULL; struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - key = &bt_mesh.app_keys[i]; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - key = bt_mesh.p_app_keys[i]; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < ARRAY_SIZE(bt_mesh.app_keys)) { - key = &bt_mesh.app_keys[i]; - } else { - key = bt_mesh.p_app_keys[i - ARRAY_SIZE(bt_mesh.app_keys)]; - } -#endif - + key = bt_mesh_rx_appkey_get(i); if (!key) { BT_DBG("%s, NULL AppKey", __func__); continue;