From 25dfee4bddeef41a08ffc1ffcf0944bdf25529df Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 27 Mar 2020 11:18:02 +0800 Subject: [PATCH] ble_mesh: Add and use transport macro definitions [Zephyr] --- .../api/core/esp_ble_mesh_networking_api.c | 4 ++-- .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 6 +++++ .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 8 +++---- components/bt/esp_ble_mesh/mesh_core/access.c | 10 ++++---- components/bt/esp_ble_mesh/mesh_core/net.c | 4 ++-- .../bt/esp_ble_mesh/mesh_core/transport.c | 24 +++++++++---------- .../bt/esp_ble_mesh/mesh_core/transport.h | 3 +++ .../mesh_models/client/client_common.c | 6 ++--- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index 0f36723e50..12a45f79e6 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -71,9 +71,9 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, } if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { - mic_len = 4; + mic_len = ESP_BLE_MESH_MIC_SHORT; } else { - mic_len = ctx->send_rel ? 8 : 4; + mic_len = ctx->send_rel ? ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT; } if (op_len + length + mic_len > MIN(ESP_BLE_MESH_SDU_MAX_LEN, ESP_BLE_MESH_TX_SDU_MAX)) { diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index c2a5e7c924..8b7a301280 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -38,6 +38,12 @@ extern "C" { /*!< The maximum length of a BLE Mesh message, including Opcode, Payload and TransMIC */ #define ESP_BLE_MESH_SDU_MAX_LEN 384 +/*!< Length of a short Mesh MIC. */ +#define ESP_BLE_MESH_MIC_SHORT 4 + +/*!< Length of a long Mesh MIC. */ +#define ESP_BLE_MESH_MIC_LONG 8 + /*!< The maximum length of a BLE Mesh provisioned node name */ #define ESP_BLE_MESH_NODE_NAME_MAX_LEN 31 diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index f136b615ad..704a18f03d 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -1965,8 +1965,8 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) break; } case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: { - /* arg->model_send.length contains opcode & message, 4 is used for TransMIC */ - struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 4); + /* arg->model_send.length contains opcode & payload, plus extra 4-bytes TransMIC */ + struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT); if (!buf) { BT_ERR("%s, Failed to allocate memory", __func__); break; @@ -1983,8 +1983,8 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) } case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: { bt_mesh_role_param_t common = {0}; - /* arg->model_send.length contains opcode & message, 4 is used for TransMIC */ - struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 4); + /* arg->model_send.length contains opcode & message, plus extra 4-bytes TransMIC */ + struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT); if (!buf) { BT_ERR("%s, Failed to allocate memory", __func__); break; diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index effc9d47ce..b48266755f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -411,7 +411,7 @@ static int publish_retransmit(struct bt_mesh_model *mod) ctx.net_idx = key->net_idx; ctx.app_idx = key->app_idx; - sdu = bt_mesh_alloc_buf(pub->msg->len + 4); + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_SHORT); if (!sdu) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -976,12 +976,12 @@ static int model_send(struct bt_mesh_model *model, return -EINVAL; } - if (net_buf_simple_tailroom(msg) < 4) { + if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { BT_ERR("%s, Not enough tailroom for TransMIC", __func__); return -EINVAL; } - if (msg->len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN) - 4) { + if (msg->len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN) - BLE_MESH_MIC_SHORT) { BT_ERR("%s, Too big message", __func__); return -EMSGSIZE; } @@ -1061,7 +1061,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) return -EADDRNOTAVAIL; } - if (pub->msg->len + 4 > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN)) { + if (pub->msg->len + BLE_MESH_MIC_SHORT > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN)) { BT_ERR("%s, Message does not fit maximum SDU size", __func__); return -EMSGSIZE; } @@ -1090,7 +1090,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) BT_INFO("Publish Retransmit Count %u Interval %ums", pub->count, BLE_MESH_PUB_TRANSMIT_INT(pub->retransmit)); - sdu = bt_mesh_alloc_buf(pub->msg->len + 4); + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_SHORT); if (!sdu) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 89d4cb79eb..a2e111bc35 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -829,10 +829,10 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, u8_t nid = 0U; int err = 0; - if (ctl && net_buf_simple_tailroom(buf) < 8) { + if (ctl && net_buf_simple_tailroom(buf) < BLE_MESH_MIC_LONG) { BT_ERR("%s, Insufficient MIC space for CTL PDU", __func__); return -EINVAL; - } else if (net_buf_simple_tailroom(buf) < 4) { + } else if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) { BT_ERR("%s, Insufficient MIC space for PDU", __func__); return -EINVAL; } diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 1844f33976..4ac1b13769 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -40,7 +40,7 @@ _Static_assert(CONFIG_BLE_MESH_ADV_BUF_COUNT >= (CONFIG_BLE_MESH_TX_SEG_MAX + 3) #define AID(data) ((data)[0] & AID_MASK) #define ASZMIC(data) (((data)[1] >> 7) & 1) -#define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4) +#define APP_MIC_LEN(aszmic) ((aszmic) ? BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT) #define UNSEG_HDR(akf, aid) ((akf << 6) | (aid & AID_MASK)) #define SEG_HDR(akf, aid) (UNSEG_HDR(akf, aid) | 0x80) @@ -428,7 +428,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, seg_o = 0U; tx->dst = net_tx->ctx->addr; - tx->seg_n = (sdu->len - 1) / 12U; + tx->seg_n = (sdu->len - 1) / BLE_MESH_APP_SEG_SDU_MAX; tx->nack_count = tx->seg_n + 1; tx->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); tx->sub = net_tx->sub; @@ -480,7 +480,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, (seg_o >> 3))); net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx->seg_n); - len = MIN(sdu->len, 12); + len = MIN(sdu->len, BLE_MESH_APP_SEG_SDU_MAX); net_buf_add_mem(seg, sdu->data, len); net_buf_simple_pull(sdu, len); @@ -563,12 +563,12 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, u8_t aid = 0U; int err = 0; - if (net_buf_simple_tailroom(msg) < 4) { + if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { BT_ERR("%s, Insufficient tailroom for Transport MIC", __func__); return -EINVAL; } - if (msg->len > 11) { + if (msg->len > BLE_MESH_SDU_UNSEG_MAX) { tx->ctx->send_rel = 1U; } @@ -590,7 +590,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, tx->aid = aid; - if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < 8) { + if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) { tx->aszmic = 0U; } else { tx->aszmic = 1U; @@ -732,7 +732,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, /* Use bt_mesh_alloc_buf() instead of NET_BUF_SIMPLE_DEFINE to avoid * causing btu task stackoverflow. */ - sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - 4); + sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - BLE_MESH_MIC_SHORT); if (!sdu) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -1134,7 +1134,7 @@ static int ctl_send_seg(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, } tx_seg->dst = tx->ctx->addr; - tx_seg->seg_n = (data_len - 1) / 8; + tx_seg->seg_n = (data_len - 1) / BLE_MESH_CTL_SEG_SDU_MAX; tx_seg->nack_count = tx_seg->seg_n + 1; tx_seg->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); tx_seg->sub = tx->sub; @@ -1174,7 +1174,7 @@ static int ctl_send_seg(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | (seg_o >> 3))); net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx_seg->seg_n); - len = MIN(unsent, 8); + len = MIN(unsent, BLE_MESH_CTL_SEG_SDU_MAX); net_buf_add_mem(seg, (u8_t *)data + (data_len - unsent), len); unsent -= len; @@ -1204,7 +1204,7 @@ int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, tx->ctx->addr, tx->ctx->send_ttl, ctl_op); BT_DBG("len %zu: %s", data_len, bt_hex(data, data_len)); - if (data_len <= 11) { + if (data_len <= BLE_MESH_SDU_UNSEG_MAX) { return ctl_send_unseg(tx, ctl_op, data, data_len, cb, cb_data); } else { @@ -1321,9 +1321,9 @@ static void seg_ack(struct k_work *work) static inline u8_t seg_len(bool ctl) { if (ctl) { - return 8; + return BLE_MESH_CTL_SEG_SDU_MAX; } else { - return 12; + return BLE_MESH_APP_SEG_SDU_MAX; } } diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.h b/components/bt/esp_ble_mesh/mesh_core/transport.h index 661fc4340d..ac80273b0d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.h +++ b/components/bt/esp_ble_mesh/mesh_core/transport.h @@ -18,6 +18,9 @@ extern "C" { #define TRANS_SEQ_AUTH_NVAL 0xffffffffffffffff +#define BLE_MESH_SDU_UNSEG_MAX 11 +#define BLE_MESH_CTL_SEG_SDU_MAX 8 +#define BLE_MESH_APP_SEG_SDU_MAX 12 #define BLE_MESH_TX_SDU_MAX (CONFIG_BLE_MESH_TX_SEG_MAX * 12) #define TRANS_SEQ_ZERO_MASK ((u16_t)BIT_MASK(13)) diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c index 5718d5202e..fed5408b99 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -22,9 +22,7 @@ #include "client_common.h" #include "mesh_common.h" -#define UNSEG_ACCESS_MSG_MAX_LEN 11 /* 11 octets (Opcode + Payload), 4 octets TransMIC */ -#define SEG_ACCESS_MSG_SEG_LEN 12 /* 12 * 32 = 384 octets (Opcode + Payload + TransMIC) */ -#define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */ +#define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */ static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, u16_t tx_dst) { @@ -183,7 +181,7 @@ static s32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx, bool need_seg = false; u8_t mic_size = 0; - if (msg->len > UNSEG_ACCESS_MSG_MAX_LEN || ctx->send_rel) { + if (msg->len > BLE_MESH_SDU_UNSEG_MAX || ctx->send_rel) { need_seg = true; /* Needs segmentation */ }