From 1338fc4ff1977d6e19751f4a3be48288202db0bd Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 4 Nov 2019 17:54:50 +0800 Subject: [PATCH] ble_mesh: Model message macros Creates macros for determining model message lengths based on opcode, payload length and MIC size. Also adds macro wrapping NET_BUF_SIMPLE_DEFINE to serve the most common use case. --- components/bt/esp_ble_mesh/mesh_core/access.c | 24 ++--- .../bt/esp_ble_mesh/mesh_core/cfg_cli.c | 65 ++++++------- .../bt/esp_ble_mesh/mesh_core/cfg_srv.c | 92 ++++++++----------- .../bt/esp_ble_mesh/mesh_core/health_cli.c | 14 +-- .../bt/esp_ble_mesh/mesh_core/health_srv.c | 6 +- .../mesh_core/include/mesh_access.h | 50 ++++++++++ 6 files changed, 143 insertions(+), 108 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 43e6e746a3..989b1f0a66 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -761,21 +761,21 @@ void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode) { net_buf_simple_init(msg, 0); - if (opcode < 0x100) { - /* 1-byte OpCode */ + switch (BLE_MESH_MODEL_OP_LEN(opcode)) { + case 1: net_buf_simple_add_u8(msg, opcode); - return; - } - - if (opcode < 0x10000) { - /* 2-byte OpCode */ + break; + case 2: net_buf_simple_add_be16(msg, opcode); - return; + break; + case 3: + net_buf_simple_add_u8(msg, ((opcode >> 16) & 0xff)); + net_buf_simple_add_le16(msg, opcode & 0xffff); + break; + default: + BT_WARN("Unknown opcode format"); + break; } - - /* 3-byte OpCode */ - net_buf_simple_add_u8(msg, ((opcode >> 16) & 0xff)); - net_buf_simple_add_le16(msg, opcode & 0xffff); } static bool ready_to_send(u8_t role, u16_t dst) diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c index c3e73c23fe..3cb08f0765 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -29,6 +29,9 @@ #define CID_NVAL 0xffff +/* 2 byte dummy opcode for getting compile time buffer sizes. */ +#define DUMMY_2_BYTE_OP BLE_MESH_MODEL_OP_2(0xff, 0xff) + s32_t config_msg_timeout; static bt_mesh_config_client_t *cli; @@ -685,7 +688,7 @@ const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = { int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEV_COMP_DATA_GET, 1); int err; if (!ctx || !ctx->addr) { @@ -707,7 +710,7 @@ int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page) static int get_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 0); int err; bt_mesh_model_msg_init(&msg, op); @@ -723,7 +726,7 @@ static int get_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op) static int set_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op, u8_t new_val) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 1); int err; bt_mesh_model_msg_init(&msg, op); @@ -804,7 +807,7 @@ int bt_mesh_cfg_gatt_proxy_set(struct bt_mesh_msg_ctx *ctx, u8_t val) int bt_mesh_cfg_relay_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -826,7 +829,7 @@ int bt_mesh_cfg_relay_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_cfg_relay_set(struct bt_mesh_msg_ctx *ctx, u8_t new_relay, u8_t new_transmit) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_SET, 2); int err; if (!ctx || !ctx->addr) { @@ -850,7 +853,7 @@ int bt_mesh_cfg_relay_set(struct bt_mesh_msg_ctx *ctx, u8_t new_relay, int bt_mesh_cfg_net_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, const u8_t net_key[16]) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 18 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_ADD, 18); int err; if (!ctx || !ctx->addr || !net_key) { @@ -874,7 +877,7 @@ int bt_mesh_cfg_net_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, int bt_mesh_cfg_app_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, u16_t key_app_idx, const u8_t app_key[16]) { - NET_BUF_SIMPLE_DEFINE(msg, 1 + 19 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_ADD, 19); int err; if (!ctx || !ctx->addr || !app_key) { @@ -898,7 +901,7 @@ int bt_mesh_cfg_app_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, int bt_mesh_cfg_mod_app_bind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_app_idx, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_BIND, 8); int err; if (!ctx || !ctx->addr) { @@ -926,7 +929,7 @@ int bt_mesh_cfg_mod_app_bind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, static int mod_sub(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t sub_addr, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 8); int err; bt_mesh_model_msg_init(&msg, op); @@ -976,7 +979,7 @@ int bt_mesh_cfg_mod_sub_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, static int mod_sub_va(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, const u8_t label[16], u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 22 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 22); int err; BT_DBG("net_idx 0x%04x addr 0x%04x elem_addr 0x%04x label %s", @@ -1030,7 +1033,7 @@ int bt_mesh_cfg_mod_sub_va_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_add int bt_mesh_cfg_mod_pub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 6 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_GET, 6); int err; if (!ctx || !ctx->addr) { @@ -1058,7 +1061,7 @@ int bt_mesh_cfg_mod_pub_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid, struct bt_mesh_cfg_mod_pub *pub) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 13 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_SET, 13); int err; if (!ctx || !ctx->addr || !pub) { @@ -1090,7 +1093,7 @@ int bt_mesh_cfg_mod_pub_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, struct bt_mesh_cfg_hb_sub *sub) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 5 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_SET, 5); int err; if (!ctx || !ctx->addr || !sub) { @@ -1114,7 +1117,7 @@ int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_cfg_hb_sub_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -1136,7 +1139,7 @@ int bt_mesh_cfg_hb_sub_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_cfg_hb_pub *pub) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_SET, 9); int err; if (!ctx || !ctx->addr || !pub) { @@ -1163,7 +1166,7 @@ int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -1184,7 +1187,7 @@ int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_cfg_node_reset(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET, 0); int err; if (!ctx || !ctx->addr) { @@ -1207,7 +1210,7 @@ int bt_mesh_cfg_mod_pub_va_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid, const u8_t label[16], struct bt_mesh_cfg_mod_pub *pub) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 27 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_VA_SET, 27); int err; if (!ctx || !ctx->addr || !label || !pub) { @@ -1239,7 +1242,7 @@ int bt_mesh_cfg_mod_pub_va_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, int bt_mesh_cfg_mod_sub_del_all(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 6 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_DEL_ALL, 6); int err; if (!ctx || !ctx->addr) { @@ -1266,7 +1269,7 @@ int bt_mesh_cfg_mod_sub_del_all(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, static int mod_sub_get(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 6 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); int err; bt_mesh_model_msg_init(&msg, op); @@ -1305,7 +1308,7 @@ int bt_mesh_cfg_mod_sub_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, const u8_t net_key[16]) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 18 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_UPDATE, 18); int err; if (!ctx || !ctx->addr || !net_key) { @@ -1328,7 +1331,7 @@ int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_DEL, 2); int err; if (!ctx || !ctx->addr) { @@ -1350,7 +1353,7 @@ int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) int bt_mesh_cfg_net_key_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -1372,7 +1375,7 @@ int bt_mesh_cfg_net_key_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx, const u8_t app_key[16]) { - NET_BUF_SIMPLE_DEFINE(msg, 1 + 19 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_UPDATE, 19); int err; if (!ctx || !ctx->addr || !app_key) { @@ -1395,7 +1398,7 @@ int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_DEL, 3); int err; if (!ctx || !ctx->addr) { @@ -1417,7 +1420,7 @@ int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t int bt_mesh_cfg_app_key_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_GET, 2); int err; if (!ctx || !ctx->addr) { @@ -1440,7 +1443,7 @@ int bt_mesh_cfg_app_key_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) static int node_identity_op(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t identity) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); int err; bt_mesh_model_msg_init(&msg, op); @@ -1477,7 +1480,7 @@ int bt_mesh_cfg_node_identity_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8 int bt_mesh_cfg_mod_app_unbind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t app_idx, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_UNBIND, 8); int err; if (!ctx || !ctx->addr) { @@ -1505,7 +1508,7 @@ int bt_mesh_cfg_mod_app_unbind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, static int mod_app_get(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 6 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); int err; bt_mesh_model_msg_init(&msg, op); @@ -1544,7 +1547,7 @@ int bt_mesh_cfg_mod_app_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, static int kr_phase_op(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t transition) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); int err; bt_mesh_model_msg_init(&msg, op); @@ -1580,7 +1583,7 @@ int bt_mesh_cfg_kr_phase_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t tr int bt_mesh_cfg_lpn_timeout_get(struct bt_mesh_msg_ctx *ctx, u16_t lpn_addr) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_GET, 2); int err; if (!ctx || !ctx->addr) { diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index c7f72a07dd..a17c3fd938 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -451,7 +451,7 @@ static void app_key_add(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); u16_t key_net_idx, key_app_idx; u8_t status; @@ -486,7 +486,7 @@ static void app_key_update(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); u16_t key_net_idx, key_app_idx; u8_t status; @@ -549,7 +549,7 @@ static void app_key_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); u16_t key_net_idx, key_app_idx; struct bt_mesh_app_key *key; u8_t status; @@ -607,8 +607,8 @@ static void app_key_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4 + - IDX_LEN(CONFIG_BLE_MESH_APP_KEY_COUNT)); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_LIST, + 3 + IDX_LEN(CONFIG_BLE_MESH_APP_KEY_COUNT)); u16_t get_idx, i, prev; u8_t status; @@ -666,8 +666,7 @@ static void beacon_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1); BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -685,8 +684,7 @@ static void beacon_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1); struct bt_mesh_cfg_srv *cfg = model->user_data; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", @@ -728,8 +726,7 @@ static void default_ttl_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1); BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -747,8 +744,7 @@ static void default_ttl_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1); struct bt_mesh_cfg_srv *cfg = model->user_data; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", @@ -781,8 +777,7 @@ static void default_ttl_set(struct bt_mesh_model *model, static void send_gatt_proxy_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_GATT_PROXY_STATUS, 1); bt_mesh_model_msg_init(&msg, OP_GATT_PROXY_STATUS); net_buf_simple_add_u8(&msg, bt_mesh_gatt_proxy_get()); @@ -878,8 +873,7 @@ static void net_transmit_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1); BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -897,8 +891,7 @@ static void net_transmit_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1); struct bt_mesh_cfg_srv *cfg = model->user_data; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", @@ -931,8 +924,7 @@ static void relay_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2); BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -951,8 +943,7 @@ static void relay_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2); struct bt_mesh_cfg_srv *cfg = model->user_data; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", @@ -1005,8 +996,7 @@ static void send_mod_pub_status(struct bt_mesh_model *cfg_mod, bool vnd, struct bt_mesh_model *mod, u8_t status, u8_t *mod_id) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 14 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_STATUS, 14); bt_mesh_model_msg_init(&msg, OP_MOD_PUB_STATUS); @@ -1386,8 +1376,7 @@ static void send_mod_sub_status(struct bt_mesh_model *model, u16_t elem_addr, u16_t sub_addr, u8_t *mod_id, bool vnd) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_STATUS, 9); BT_DBG("status 0x%02x elem_addr 0x%04x sub_addr 0x%04x", status, elem_addr, sub_addr); @@ -1696,8 +1685,8 @@ static void mod_sub_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 5 + 4 + - CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST, + 5 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); struct bt_mesh_model *mod; struct bt_mesh_elem *elem; u16_t addr, id; @@ -1752,8 +1741,8 @@ static void mod_sub_get_vnd(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 7 + 4 + - CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST_VND, + 7 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); struct bt_mesh_model *mod; struct bt_mesh_elem *elem; u16_t company, addr, id; @@ -2145,8 +2134,7 @@ static void send_net_key_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u16_t idx, u8_t status) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_STATUS, 3); bt_mesh_model_msg_init(&msg, OP_NET_KEY_STATUS); @@ -2379,8 +2367,8 @@ static void net_key_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, - 2 + 4 + IDX_LEN(CONFIG_BLE_MESH_SUBNET_COUNT)); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_LIST, + IDX_LEN(CONFIG_BLE_MESH_SUBNET_COUNT)); u16_t prev, i; bt_mesh_model_msg_init(&msg, OP_NET_KEY_LIST); @@ -2415,8 +2403,7 @@ static void node_identity_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); struct bt_mesh_subnet *sub; u8_t node_id; u16_t idx; @@ -2454,8 +2441,7 @@ static void node_identity_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); struct bt_mesh_subnet *sub; u8_t node_id; u16_t idx; @@ -2531,7 +2517,7 @@ static void mod_app_bind(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); u16_t elem_addr, key_app_idx; struct bt_mesh_model *mod; struct bt_mesh_elem *elem; @@ -2594,7 +2580,7 @@ static void mod_app_unbind(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); u16_t elem_addr, key_app_idx; struct bt_mesh_model *mod; struct bt_mesh_elem *elem; @@ -2652,7 +2638,11 @@ static void mod_app_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + KEY_LIST_LEN + 4); + NET_BUF_SIMPLE_DEFINE(msg, + MAX(BLE_MESH_MODEL_BUF_LEN(OP_VND_MOD_APP_LIST, + 9 + KEY_LIST_LEN), + BLE_MESH_MODEL_BUF_LEN(OP_SIG_MOD_APP_LIST, + 9 + KEY_LIST_LEN))); struct bt_mesh_model *mod; struct bt_mesh_elem *elem; u8_t *mod_id, status; @@ -2720,8 +2710,7 @@ static void node_reset(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET_STATUS, 0); BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -2745,8 +2734,7 @@ static void node_reset(struct bt_mesh_model *model, static void send_friend_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_FRIEND_STATUS, 1); struct bt_mesh_cfg_srv *cfg = model->user_data; bt_mesh_model_msg_init(&msg, OP_FRIEND_STATUS); @@ -2818,8 +2806,7 @@ static void lpn_timeout_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 5 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5); struct bt_mesh_friend *frnd; u16_t lpn_addr; s32_t timeout; @@ -2864,8 +2851,7 @@ static void send_krp_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u16_t idx, u8_t phase, u8_t status) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 4 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_KRP_STATUS, 4); bt_mesh_model_msg_init(&msg, OP_KRP_STATUS); @@ -3008,8 +2994,7 @@ static void hb_pub_send_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u8_t status, struct hb_pub_param *orig_msg) { - /* Needed size: opcode (1 byte) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 1 + 10 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_STATUS, 10); struct bt_mesh_cfg_srv *cfg = model->user_data; BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status); @@ -3147,8 +3132,7 @@ failed: static void hb_sub_send_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u8_t status) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 9 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_STATUS, 9); struct bt_mesh_cfg_srv *cfg = model->user_data; u16_t period; s64_t uptime; diff --git a/components/bt/esp_ble_mesh/mesh_core/health_cli.c b/components/bt/esp_ble_mesh/mesh_core/health_cli.c index 0d5d5c8138..66c15d44ff 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -235,7 +235,7 @@ const struct bt_mesh_model_op bt_mesh_health_cli_op[] = { int bt_mesh_health_attention_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -257,7 +257,7 @@ int bt_mesh_health_attention_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, u8_t attention, bool need_ack) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_SET, 1); u32_t opcode; int err; @@ -285,7 +285,7 @@ int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_period_get(struct bt_mesh_msg_ctx *ctx) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 0 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_GET, 0); int err; if (!ctx || !ctx->addr) { @@ -307,7 +307,7 @@ int bt_mesh_health_period_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_health_period_set(struct bt_mesh_msg_ctx *ctx, u8_t divisor, bool need_ack) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_SET, 1); u32_t opcode; int err; @@ -336,7 +336,7 @@ int bt_mesh_health_period_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_fault_test(struct bt_mesh_msg_ctx *ctx, u16_t cid, u8_t test_id, bool need_ack) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 3 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_TEST, 3); u32_t opcode; int err; @@ -366,7 +366,7 @@ int bt_mesh_health_fault_test(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, u16_t cid, bool need_ack) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_CLEAR, 2); u32_t opcode; int err; @@ -394,7 +394,7 @@ int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_fault_get(struct bt_mesh_msg_ctx *ctx, u16_t cid) { - NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_GET, 2); int err; if (!ctx || !ctx->addr) { diff --git a/components/bt/esp_ble_mesh/mesh_core/health_srv.c b/components/bt/esp_ble_mesh/mesh_core/health_srv.c index 344b62d10d..6b039b6f45 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_srv.c @@ -227,8 +227,7 @@ static void health_fault_test(struct bt_mesh_model *model, static void send_attention_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_STATUS, 1); struct bt_mesh_health_srv *srv = model->user_data; u8_t time; @@ -286,8 +285,7 @@ static void attention_set(struct bt_mesh_model *model, static void send_health_period_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx) { - /* Needed size: opcode (2 bytes) + msg + MIC */ - NET_BUF_SIMPLE_DEFINE(msg, 2 + 1 + 4); + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_STATUS, 1); bt_mesh_model_msg_init(&msg, OP_HEALTH_PERIOD_STATUS); net_buf_simple_add_u8(&msg, model->pub->period_div); diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h index cdc6205200..0d5e4f66fb 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h @@ -188,6 +188,56 @@ struct bt_mesh_model_op { /** Helper to define an empty model array */ #define BLE_MESH_MODEL_NONE ((struct bt_mesh_model []){}) +/** Length of a short Mesh MIC. */ +#define BLE_MESH_MIC_SHORT 4 +/** Length of a long Mesh MIC. */ +#define BLE_MESH_MIC_LONG 8 + +/** @def BLE_MESH_MODEL_OP_LEN + * + * @brief Helper to determine the length of an opcode. + * + * @param _op Opcode. + */ +#define BLE_MESH_MODEL_OP_LEN(_op) ((_op) <= 0xff ? 1 : (_op) <= 0xffff ? 2 : 3) + +/** @def BLE_MESH_MODEL_BUF_LEN + * + * @brief Helper for model message buffer length. + * + * Returns the length of a Mesh model message buffer, including the opcode + * length and a short MIC. + * + * @param _op Opcode of the message. + * @param _payload_len Length of the model payload. + */ +#define BLE_MESH_MODEL_BUF_LEN(_op, _payload_len) \ + (BLE_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BLE_MESH_MIC_SHORT) + +/** @def BLE_MESH_MODEL_BUF_LEN_LONG_MIC + * + * @brief Helper for model message buffer length. + * + * Returns the length of a Mesh model message buffer, including the opcode + * length and a long MIC. + * + * @param _op Opcode of the message. + * @param _payload_len Length of the model payload. + */ +#define BLE_MESH_MODEL_BUF_LEN_LONG_MIC(_op, _payload_len) \ + (BLE_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BLE_MESH_MIC_LONG) + +/** @def BLE_MESH_MODEL_BUF_DEFINE + * + * @brief Define a Mesh model message buffer using @ref NET_BUF_SIMPLE_DEFINE. + * + * @param _buf Buffer name. + * @param _op Opcode of the message. + * @param _payload_len Length of the model message payload. + */ +#define BLE_MESH_MODEL_BUF_DEFINE(_buf, _op, _payload_len) \ + NET_BUF_SIMPLE_DEFINE(_buf, BLE_MESH_MODEL_BUF_LEN(_op, (_payload_len))) + #define BLE_MESH_MODEL(_id, _op, _pub, _user_data) \ { \ .id = (_id), \