Commit ee337087 authored by Håvard Reierstad's avatar Håvard Reierstad Committed by Anas Nashif
Browse files

bsim: Bluetooth: Mesh: extract common adv logic

Extracts logic related to waiting for packets from
`test_advertiser.c` and `test_beacon.c` to use in a sync mechanism.
Introduces a sync mechanism in bsim with the functions
`bt_mesh_test_send_over_adv` and `bt_mesh_test_wait_for_packet`

Extracts gatt-related logic from `test_advertiser.c` into a separate
file so it can be re-used.

Signed-off-by: default avatarHåvard Reierstad <haavard.reierstad@nordicsemi.no>
parent 4aa0e7af
Loading
Loading
Loading
Loading
+1 −0
Changes for tests/bsim/bluetooth/mesh/CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ target_sources(app PRIVATE
 src/main.c
 src/mesh_test.c
 src/friendship_common.c
 src/gatt_common.c
)

if(CONFIG_BT_MESH_V1d1)
+39 −0
Changes for tests/bsim/bluetooth/mesh/src/gatt_common.c: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2023 Nordic Semiconductor
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "gatt_common.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(gatt_common);

void bt_mesh_test_parse_mesh_gatt_preamble(struct net_buf_simple *buf)
{
	ASSERT_EQUAL(0x0201, net_buf_simple_pull_be16(buf));
	/* flags */
	(void)net_buf_simple_pull_u8(buf);
	ASSERT_EQUAL(0x0303, net_buf_simple_pull_be16(buf));
}

void bt_mesh_test_parse_mesh_pb_gatt_service(struct net_buf_simple *buf)
{
	/* MshPRT Figure 7.1: PB-GATT Advertising Data */
	/* mesh provisioning service */
	ASSERT_EQUAL(0x2718, net_buf_simple_pull_be16(buf));
	ASSERT_EQUAL(0x1516, net_buf_simple_pull_be16(buf));
	/* mesh provisioning service */
	ASSERT_EQUAL(0x2718, net_buf_simple_pull_be16(buf));
}

void bt_mesh_test_parse_mesh_proxy_service(struct net_buf_simple *buf)
{
	/* MshPRT Figure 7.2: Advertising with Network ID (Identification Type 0x00) */
	/* mesh proxy service */
	ASSERT_EQUAL(0x2818, net_buf_simple_pull_be16(buf));
	ASSERT_EQUAL(0x0c16, net_buf_simple_pull_be16(buf));
	/* mesh proxy service */
	ASSERT_EQUAL(0x2818, net_buf_simple_pull_be16(buf));
	/* network ID */
	ASSERT_EQUAL(0x00, net_buf_simple_pull_u8(buf));
}
+27 −0
Changes for tests/bsim/bluetooth/mesh/src/gatt_common.h: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2023 Nordic Semiconductor
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mesh_test.h"

enum bt_mesh_test_gatt_service {
	MESH_SERVICE_PROVISIONING,
	MESH_SERVICE_PROXY,
};

struct bt_mesh_test_gatt {
	uint8_t transmits; /* number of frame (pb gatt or proxy beacon) transmits */
	int64_t interval;  /* interval of transmitted frames */
	enum bt_mesh_test_gatt_service service;
};

struct bt_mesh_test_adv {
	uint8_t retr;     /* number of retransmits of adv frame */
	int64_t interval; /* interval of transmitted frames */
};

void bt_mesh_test_parse_mesh_gatt_preamble(struct net_buf_simple *buf);
void bt_mesh_test_parse_mesh_pb_gatt_service(struct net_buf_simple *buf);
void bt_mesh_test_parse_mesh_proxy_service(struct net_buf_simple *buf);
+45 −0
Changes for tests/bsim/bluetooth/mesh/src/mesh_test.c: 45 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include "argparse.h"
#include <bs_pc_backchannel.h>
#include "mesh/crypto.h"
#include <zephyr/bluetooth/hci.h>

#define LOG_MODULE_NAME mesh_test

@@ -546,6 +547,50 @@ uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr)
	return start_addr + get_device_nbr();
}

void bt_mesh_test_send_over_adv(void *data, size_t len)
{
	struct bt_mesh_adv *adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL,
						     BT_MESH_TRANSMIT(0, 20), K_NO_WAIT);
	net_buf_simple_add_mem(&adv->b, data, len);
	bt_mesh_adv_send(adv, NULL, NULL);
}

int bt_mesh_test_wait_for_packet(bt_le_scan_cb_t scan_cb, struct k_sem *observer_sem, uint16_t wait)
{
	struct bt_le_scan_param scan_param = {
		.type       = BT_HCI_LE_SCAN_PASSIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = BT_MESH_ADV_SCAN_UNIT(1000),
		.window     = BT_MESH_ADV_SCAN_UNIT(1000)
	};
	int err;
	int returned_value = 0;

	err = bt_le_scan_start(&scan_param, scan_cb);
	if (err && err != -EALREADY) {
		LOG_ERR("Starting scan failed (err %d)", err);
		return err;
	}

	err = k_sem_take(observer_sem, K_SECONDS(wait));
	if (err == -EAGAIN) {
		LOG_WRN("Taking sem timed out (err %d)", err);
		returned_value = -EAGAIN;
	} else if (err) {
		LOG_ERR("Taking sem failed (err %d)", err);
		return err;
	}

	err = bt_le_scan_stop();
	if (err && err != -EALREADY) {
		LOG_ERR("Stopping scan failed (err %d)", err);
		return err;
	}

	return returned_value;
}


#if defined(CONFIG_BT_MESH_SAR_CFG)
void bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx *tx_set, struct bt_mesh_sar_rx *rx_set)
{
+8 −0
Changes for tests/bsim/bluetooth/mesh/src/mesh_test.h: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/mesh.h>
#include <mesh/adv.h>

#define TEST_MOD_ID 0x8888
#define TEST_MSG_OP_1  BT_MESH_MODEL_OP_1(0x0f)
@@ -202,6 +203,13 @@ void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t));

uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr);

void bt_mesh_test_send_over_adv(void *data, size_t len);
/* Wait for a packet (i. e. an advertisement or a GATT frame) sent by a device.
 * `scan_cb` is triggered if the packet is received, and must release `observer_sem` when finished.
 */
int bt_mesh_test_wait_for_packet(bt_le_scan_cb_t scan_cb, struct k_sem *observer_sem,
				 uint16_t wait);

#if defined(CONFIG_BT_MESH_SAR_CFG)
void bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx *tx_set, struct bt_mesh_sar_rx *rx_set);
#endif
Loading