From f9fad553ac71df8d6e45f031c355ac46f3bbba8e Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Sat, 18 Nov 2017 10:01:43 +0200 Subject: [PATCH] Bluetooth: Mesh: Fix encoding/decoding Publish Retransmit value The Model Publish Retransmit Interval is in units of 50ms and not 10ms like the other transmit/retransmit states. Create dedicated macros for the Publish Retransmit State and use them where appropriate. Signed-off-by: Johan Hedberg --- include/bluetooth/mesh/access.h | 34 ++++++++++++++++++++++++++++ subsys/bluetooth/host/mesh/cfg_srv.c | 8 +++---- subsys/bluetooth/host/mesh/shell.c | 15 ++++++------ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/include/bluetooth/mesh/access.h b/include/bluetooth/mesh/access.h index b8be5d951a6..f8b3b833f6c 100644 --- a/include/bluetooth/mesh/access.h +++ b/include/bluetooth/mesh/access.h @@ -219,6 +219,40 @@ struct bt_mesh_model_op { */ #define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10) +/** @def BT_MESH_PUB_TRANSMIT + * + * @brief Encode Publish Retransmit count & interval steps. + * + * @param count Number of retransmissions (first transmission is excluded). + * @param int_ms Interval steps in milliseconds. Must be greater than 0 + * and a multiple of 50. + * + * @return Mesh transmit value that can be used e.g. for the default + * values of the configuration model data. + */ +#define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count, \ + (int_ms) / 5) + +/** @def BT_MESH_PUB_TRANSMIT_COUNT + * + * @brief Decode Pubhlish Retransmit count from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Retransmission count (actual transmissions is N + 1). + */ +#define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit) + +/** @def BT_MESH_PUB_TRANSMIT_INT + * + * @brief Decode Publish Retransmit interval from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Transmission interval in milliseconds. + */ +#define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50) + struct bt_mesh_model_pub { /* Self-reference for easy lookup */ struct bt_mesh_model *mod; diff --git a/subsys/bluetooth/host/mesh/cfg_srv.c b/subsys/bluetooth/host/mesh/cfg_srv.c index a43ea4f7eac..da3b5fc6505 100644 --- a/subsys/bluetooth/host/mesh/cfg_srv.c +++ b/subsys/bluetooth/host/mesh/cfg_srv.c @@ -1023,8 +1023,8 @@ static void mod_pub_set(struct bt_mesh_model *model, BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", pub_app_idx, pub_ttl, pub_period); BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit, - BT_MESH_TRANSMIT_COUNT(retransmit), - BT_MESH_TRANSMIT_INT(retransmit)); + BT_MESH_PUB_TRANSMIT_COUNT(retransmit), + BT_MESH_PUB_TRANSMIT_INT(retransmit)); elem = bt_mesh_elem_find(elem_addr); if (!elem) { @@ -1130,8 +1130,8 @@ static void mod_pub_va_set(struct bt_mesh_model *model, BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", pub_app_idx, pub_ttl, pub_period); BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit, - BT_MESH_TRANSMIT_COUNT(retransmit), - BT_MESH_TRANSMIT_INT(retransmit)); + BT_MESH_PUB_TRANSMIT_COUNT(retransmit), + BT_MESH_PUB_TRANSMIT_INT(retransmit)); elem = bt_mesh_elem_find(elem_addr); if (!elem) { diff --git a/subsys/bluetooth/host/mesh/shell.c b/subsys/bluetooth/host/mesh/shell.c index d52c990fa5a..23b8b2da436 100644 --- a/subsys/bluetooth/host/mesh/shell.c +++ b/subsys/bluetooth/host/mesh/shell.c @@ -708,10 +708,10 @@ static int mod_pub_get(u16_t addr, u16_t mod_id, u16_t cid) "\tPublishTTL: %u\n" "\tPublishPeriod: 0x%02x\n" "\tPublishRetransmitCount: %u\n" - "\tPublishRetransmitIntervalSteps: %u\n", + "\tPublishRetransmitInterval: %ums\n", addr, mod_id, pub.addr, pub.app_idx, pub.cred_flag, pub.ttl, - pub.period, BT_MESH_TRANSMIT_COUNT(pub.transmit), - BT_MESH_TRANSMIT_INT(pub.transmit)); + pub.period, BT_MESH_PUB_TRANSMIT_COUNT(pub.transmit), + BT_MESH_PUB_TRANSMIT_INT(pub.transmit)); return 0; } @@ -719,7 +719,8 @@ static int mod_pub_get(u16_t addr, u16_t mod_id, u16_t cid) static int mod_pub_set(u16_t addr, u16_t mod_id, u16_t cid, char *argv[]) { struct bt_mesh_cfg_mod_pub pub; - u8_t status, count, interval; + u8_t status, count; + u16_t interval; int err; pub.addr = strtoul(argv[0], NULL, 0); @@ -735,12 +736,12 @@ static int mod_pub_set(u16_t addr, u16_t mod_id, u16_t cid, char *argv[]) } interval = strtoul(argv[6], NULL, 0); - if (interval > 31) { - printk("Invalid retransmit interval\n"); + if (interval > (31 * 50) || (interval % 50)) { + printk("Invalid retransmit interval %u\n", interval); return -EINVAL; } - pub.transmit = BT_MESH_TRANSMIT(count, interval); + pub.transmit = BT_MESH_PUB_TRANSMIT(count, interval); if (cid == CID_NVAL) { err = bt_mesh_cfg_mod_pub_set(net.net_idx, net.dst, addr,