From 77e017306e52fb6ad897489f6f79f36ed3700323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Reierstad?= Date: Tue, 19 Nov 2024 08:53:27 +0100 Subject: [PATCH] Bsim: Bluetooth: Mesh: Rename/refactor `send_ra` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renames `bt_mesh_test_send_ra` to `bt_mesh_test_send_data` and changes the corresponding callback funciton as it is used outside of the replay attack tests, and can be used to send custom data. The function is extended to accept a UUID parameter to allow sending data to virtual addresses. Signed-off-by: HÃ¥vard Reierstad --- tests/bsim/bluetooth/mesh/src/mesh_test.c | 25 ++++----- tests/bsim/bluetooth/mesh/src/mesh_test.h | 7 ++- tests/bsim/bluetooth/mesh/src/test_brg.c | 52 +++++++++---------- .../bluetooth/mesh/src/test_replay_cache.c | 20 +++---- 4 files changed, 50 insertions(+), 54 deletions(-) diff --git a/tests/bsim/bluetooth/mesh/src/mesh_test.c b/tests/bsim/bluetooth/mesh/src/mesh_test.c index 757e150c350..255f8d8aa43 100644 --- a/tests/bsim/bluetooth/mesh/src/mesh_test.c +++ b/tests/bsim/bluetooth/mesh/src/mesh_test.c @@ -28,7 +28,7 @@ K_MEM_SLAB_DEFINE_STATIC(msg_pool, sizeof(struct bt_mesh_test_msg), static K_QUEUE_DEFINE(recv); struct bt_mesh_test_stats test_stats; struct bt_mesh_msg_ctx test_send_ctx; -static void (*ra_cb)(uint8_t *, size_t); +static void (*data_cb)(uint8_t *, size_t); static int msg_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) @@ -78,8 +78,8 @@ static int msg_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx, return 0; } -static int ra_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static int data_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { LOG_INF("\tlen: %d bytes", buf->len); LOG_INF("\tsrc: 0x%04x", ctx->addr); @@ -87,18 +87,15 @@ static int ra_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx, LOG_INF("\tttl: %u", ctx->recv_ttl); LOG_INF("\trssi: %d", ctx->recv_rssi); - if (ra_cb) { - ra_cb(buf->data, buf->len); + if (data_cb) { + data_cb(buf->data, buf->len); } return 0; } static const struct bt_mesh_model_op model_op[] = { - { TEST_MSG_OP_1, 0, msg_rx }, - { TEST_MSG_OP_2, 0, ra_rx }, - BT_MESH_MODEL_OP_END -}; + {TEST_MSG_OP_1, 0, msg_rx}, {TEST_MSG_OP_2, 0, data_rx}, BT_MESH_MODEL_OP_END}; int __weak test_model_pub_update(const struct bt_mesh_model *mod) { @@ -534,15 +531,15 @@ int bt_mesh_test_send(uint16_t addr, const uint8_t *uuid, size_t len, return 0; } -int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len, - const struct bt_mesh_send_cb *send_cb, - void *cb_data) +int bt_mesh_test_send_data(uint16_t addr, const uint8_t *uuid, uint8_t *data, size_t len, + const struct bt_mesh_send_cb *send_cb, void *cb_data) { int err; test_send_ctx.addr = addr; test_send_ctx.send_rel = 0; test_send_ctx.send_ttl = BT_MESH_TTL_DEFAULT; + test_send_ctx.uuid = uuid; BT_MESH_MODEL_BUF_DEFINE(buf, TEST_MSG_OP_2, BT_MESH_TX_SDU_MAX); bt_mesh_model_msg_init(&buf, TEST_MSG_OP_2); @@ -558,9 +555,9 @@ int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len, return 0; } -void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t)) +void bt_mesh_test_data_cb_setup(void (*cb)(uint8_t *, size_t)) { - ra_cb = cb; + data_cb = cb; } uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr) diff --git a/tests/bsim/bluetooth/mesh/src/mesh_test.h b/tests/bsim/bluetooth/mesh/src/mesh_test.h index 2a88d4156ca..4ec0648b7fa 100644 --- a/tests/bsim/bluetooth/mesh/src/mesh_test.h +++ b/tests/bsim/bluetooth/mesh/src/mesh_test.h @@ -196,10 +196,9 @@ int bt_mesh_test_send_async(uint16_t addr, const uint8_t *uuid, size_t len, enum bt_mesh_test_send_flags flags, const struct bt_mesh_send_cb *send_cb, void *cb_data); -int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len, - const struct bt_mesh_send_cb *send_cb, - void *cb_data); -void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t)); +int bt_mesh_test_send_data(uint16_t addr, const uint8_t *uuid, uint8_t *data, size_t len, + const struct bt_mesh_send_cb *send_cb, void *cb_data); +void bt_mesh_test_data_cb_setup(void (*cb)(uint8_t *, size_t)); uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr); diff --git a/tests/bsim/bluetooth/mesh/src/test_brg.c b/tests/bsim/bluetooth/mesh/src/test_brg.c index 5c82c4dce74..0637ea1c48c 100644 --- a/tests/bsim/bluetooth/mesh/src/test_brg.c +++ b/tests/bsim/bluetooth/mesh/src/test_brg.c @@ -293,7 +293,7 @@ static void tester_device_configure(uint16_t net_key_idx, uint16_t addr) LOG_INF("Device 0x%04x configured", addr); } -static void tester_ra_cb(uint8_t *data, size_t length) +static void tester_data_cb(uint8_t *data, size_t length) { uint8_t type = data[0]; @@ -309,18 +309,18 @@ static void tester_ra_cb(uint8_t *data, size_t length) k_sem_give(&status_msg_recvd_sem); } -static int send_data(uint16_t dst, uint8_t payload) +static int send_data(uint16_t dst, uint8_t payload, const uint8_t *uuid) { uint8_t data[2] = {MSG_TYPE_DATA, payload}; - return bt_mesh_test_send_ra(dst, data, sizeof(data), NULL, NULL); + return bt_mesh_test_send_data(dst, uuid, data, sizeof(data), NULL, NULL); } -static int send_get(uint16_t dst) +static int send_get(uint16_t dst, const uint8_t *uuid) { uint8_t data[1] = {MSG_TYPE_GET}; - return bt_mesh_test_send_ra(dst, data, sizeof(data), NULL, NULL); + return bt_mesh_test_send_data(dst, uuid, data, sizeof(data), NULL, NULL); } struct bridged_addresses_entry { @@ -363,7 +363,7 @@ static void bridge_table_verify(uint16_t net_idx1, uint16_t net_idx2, uint16_t s } } -static void device_ra_cb(uint8_t *data, size_t length) +static void device_data_cb(uint8_t *data, size_t length) { uint8_t type = data[0]; @@ -385,7 +385,7 @@ static void device_ra_cb(uint8_t *data, size_t length) memcpy(&test_data[2], recvd_msgs, recvd_msgs_cnt * sizeof(recvd_msgs[0])); - ASSERT_OK(bt_mesh_test_send_ra(PROV_ADDR, test_data, + ASSERT_OK(bt_mesh_test_send_data(PROV_ADDR, NULL, test_data, 2 + recvd_msgs_cnt * sizeof(recvd_msgs[0]), NULL, NULL)); @@ -432,7 +432,7 @@ static void send_and_receive(void) uint8_t payload = i | i << 4; for (int j = 0; j < msgs_cnt; j++) { - ASSERT_OK(send_data(DEVICE_ADDR_START + i, payload + j)); + ASSERT_OK(send_data(DEVICE_ADDR_START + i, payload + j, NULL)); } } @@ -441,7 +441,7 @@ static void send_and_receive(void) for (int i = 0; i < REMOTE_NODES; i++) { uint8_t payload = i | i << 4; - ASSERT_OK(send_get(DEVICE_ADDR_START + i)); + ASSERT_OK(send_get(DEVICE_ADDR_START + i, NULL)); ASSERT_OK(k_sem_take(&status_msg_recvd_sem, K_SECONDS(5))); ASSERT_EQUAL(recvd_msgs_cnt, msgs_cnt); @@ -480,7 +480,7 @@ static void test_tester_simple(void) tester_workaround(); - bt_mesh_test_ra_cb_setup(tester_ra_cb); + bt_mesh_test_data_cb_setup(tester_data_cb); LOG_INF("Step 1: Checking bridging table..."); @@ -499,7 +499,7 @@ static void test_tester_simple(void) uint8_t payload = i | i << 4; for (int j = 0; j < 3; j++) { - ASSERT_OK(send_data(DEVICE_ADDR_START + i, payload + j)); + ASSERT_OK(send_data(DEVICE_ADDR_START + i, payload + j, NULL)); } } @@ -512,7 +512,7 @@ static void test_tester_simple(void) LOG_INF("Checking data..."); for (int i = 0; i < REMOTE_NODES; i++) { - ASSERT_OK(send_get(DEVICE_ADDR_START + i)); + ASSERT_OK(send_get(DEVICE_ADDR_START + i, NULL)); ASSERT_OK(k_sem_take(&status_msg_recvd_sem, K_SECONDS(5))); ASSERT_EQUAL(recvd_msgs_cnt, 0); @@ -543,26 +543,26 @@ static void test_tester_table_state_change(void) tester_workaround(); - bt_mesh_test_ra_cb_setup(tester_ra_cb); + bt_mesh_test_data_cb_setup(tester_data_cb); /* Bridge Table is empty, will not get any message back. */ - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); err = k_sem_take(&status_msg_recvd_sem, K_SECONDS(5)); ASSERT_EQUAL(err, -EAGAIN); /* DATA and GET messages should reach Device 1, but STATUS message won't be received. */ bridge_entry_add(PROV_ADDR, DEVICE_ADDR_START, 0, 1, BT_MESH_BRG_CFG_DIR_ONEWAY); - ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA)); + ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA, NULL)); - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); err = k_sem_take(&status_msg_recvd_sem, K_SECONDS(5)); ASSERT_EQUAL(err, -EAGAIN); /* Sending DATA message again before adding a new entry as the previous GET message resets * received messages counter on Devices */ - ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA)); + ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA, NULL)); /* Adding a reverse entry. This should be added to the bridge table as a separate entry as * the addresses and net keys indexs are provided in the opposite order. */ @@ -581,7 +581,7 @@ static void test_tester_table_state_change(void) k_sleep(K_SECONDS(1)); /* Now we should receive STATUS message. */ - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); ASSERT_OK(k_sem_take(&status_msg_recvd_sem, K_SECONDS(5))); ASSERT_EQUAL(recvd_msgs_cnt, 1); @@ -599,7 +599,7 @@ static void test_tester_table_state_change(void) 1); bridge_table_verify(1, 0, 0, NULL, 0); - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); ASSERT_OK(k_sem_take(&status_msg_recvd_sem, K_SECONDS(5))); ASSERT_EQUAL(recvd_msgs_cnt, 0); @@ -640,7 +640,7 @@ static void test_tester_net_key_remove(void) tester_workaround(); - bt_mesh_test_ra_cb_setup(tester_ra_cb); + bt_mesh_test_data_cb_setup(tester_data_cb); /* Adding devices to bridge table */ for (int i = 0; i < REMOTE_NODES; i++) { @@ -648,8 +648,8 @@ static void test_tester_net_key_remove(void) BT_MESH_BRG_CFG_DIR_TWOWAY); } - ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA)); - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_data(DEVICE_ADDR_START, 0xAA, NULL)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); ASSERT_OK(k_sem_take(&status_msg_recvd_sem, K_SECONDS(5))); ASSERT_EQUAL(recvd_msgs_cnt, 1); ASSERT_EQUAL(recvd_msgs[0], 0xAA); @@ -657,7 +657,7 @@ static void test_tester_net_key_remove(void) /* Removing subnet 1 from Subnet Bridge. */ net_key_remove(BRIDGE_ADDR, 0, 1); - ASSERT_OK(send_get(DEVICE_ADDR_START)); + ASSERT_OK(send_get(DEVICE_ADDR_START, NULL)); err = k_sem_take(&status_msg_recvd_sem, K_SECONDS(5)); ASSERT_EQUAL(err, -EAGAIN); @@ -762,7 +762,7 @@ static void msg_cache_workaround(void) for (int i = 0; i < REMOTE_NODES; i++) { for (int j = 0; j < CONFIG_BT_MESH_MSG_CACHE_SIZE; j++) { - ASSERT_OK(send_get(DEVICE_ADDR_START + i)); + ASSERT_OK(send_get(DEVICE_ADDR_START + i, NULL)); /* k_sem_take is needed to not overflow network buffer pool. The result * of the semaphor is not important as we just need to bump sequence number * enough to bypass message cache. @@ -841,7 +841,7 @@ static void test_tester_ivu(void) tester_workaround(); - bt_mesh_test_ra_cb_setup(tester_ra_cb); + bt_mesh_test_data_cb_setup(tester_data_cb); ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)); ASSERT_TRUE(bt_mesh.iv_index == test_ividx); @@ -929,7 +929,7 @@ static void device_setup(void) ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40))); LOG_INF("Node is provisioned"); - bt_mesh_test_ra_cb_setup(device_ra_cb); + bt_mesh_test_data_cb_setup(device_data_cb); } static void test_device_simple(void) diff --git a/tests/bsim/bluetooth/mesh/src/test_replay_cache.c b/tests/bsim/bluetooth/mesh/src/test_replay_cache.c index 636e112715a..49f86db95aa 100644 --- a/tests/bsim/bluetooth/mesh/src/test_replay_cache.c +++ b/tests/bsim/bluetooth/mesh/src/test_replay_cache.c @@ -137,8 +137,8 @@ static void test_tx_immediate_replay_attack(void) is_tx_succeeded = false; memset(test_data, i, sizeof(test_data)); - ASSERT_OK(bt_mesh_test_send_ra(rx_cfg.addr, test_data, - sizeof(test_data), &send_cb, &sem)); + ASSERT_OK(bt_mesh_test_send_data(rx_cfg.addr, NULL, test_data, sizeof(test_data), + &send_cb, &sem)); if (k_sem_take(&sem, K_SECONDS(TEST_DATA_WAITING_TIME))) { LOG_ERR("Send timed out"); @@ -155,8 +155,8 @@ static void test_tx_immediate_replay_attack(void) is_tx_succeeded = true; memset(test_data, i, sizeof(test_data)); - ASSERT_OK(bt_mesh_test_send_ra(rx_cfg.addr, test_data, - sizeof(test_data), &send_cb, &sem)); + ASSERT_OK(bt_mesh_test_send_data(rx_cfg.addr, NULL, test_data, sizeof(test_data), + &send_cb, &sem)); if (k_sem_take(&sem, K_SECONDS(TEST_DATA_WAITING_TIME))) { LOG_ERR("Send timed out"); @@ -174,7 +174,7 @@ static void test_rx_immediate_replay_attack(void) { bt_mesh_test_setup(); rx_sar_conf(); - bt_mesh_test_ra_cb_setup(rx_ended); + bt_mesh_test_data_cb_setup(rx_ended); k_sleep(K_SECONDS(6 * TEST_DATA_WAITING_TIME)); @@ -200,8 +200,8 @@ static void test_tx_power_replay_attack(void) is_tx_succeeded = true; memset(test_data, i, sizeof(test_data)); - ASSERT_OK(bt_mesh_test_send_ra(rx_cfg.addr, test_data, - sizeof(test_data), &send_cb, &sem)); + ASSERT_OK(bt_mesh_test_send_data(rx_cfg.addr, NULL, test_data, sizeof(test_data), + &send_cb, &sem)); if (k_sem_take(&sem, K_SECONDS(TEST_DATA_WAITING_TIME))) { LOG_ERR("Send timed out"); @@ -216,8 +216,8 @@ static void test_tx_power_replay_attack(void) is_tx_succeeded = false; memset(test_data, i, sizeof(test_data)); - ASSERT_OK(bt_mesh_test_send_ra(rx_cfg.addr, test_data, - sizeof(test_data), &send_cb, &sem)); + ASSERT_OK(bt_mesh_test_send_data(rx_cfg.addr, NULL, test_data, sizeof(test_data), + &send_cb, &sem)); if (k_sem_take(&sem, K_SECONDS(TEST_DATA_WAITING_TIME))) { LOG_ERR("Send timed out"); @@ -235,7 +235,7 @@ static void test_rx_power_replay_attack(void) { bt_mesh_test_setup(); rx_sar_conf(); - bt_mesh_test_ra_cb_setup(rx_ended); + bt_mesh_test_data_cb_setup(rx_ended); k_sleep(K_SECONDS(6 * TEST_DATA_WAITING_TIME));