Bluetooth: Host: Add test for change-unaware client

Checks that the server returns "Database Out Of Sync" error on two bearers
when the client is change-unaware and returns success after the database
hash has been read.

Signed-off-by: Herman Berget <herman.berget@nordicsemi.no>
This commit is contained in:
Herman Berget 2022-02-21 14:09:33 +01:00 committed by Carles Cufí
commit 811980dc20
9 changed files with 717 additions and 0 deletions

View file

@ -0,0 +1,21 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set\
the environment variable BSIM_COMPONENTS_PATH to point to its components \
folder. More information can be found in\
https://babblesim.github.io/folder_structure_and_env.html")
endif()
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(bsim_test_gatt)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources} )
zephyr_include_directories(
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
)

View file

@ -0,0 +1,15 @@
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="GATT tester"
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_SMP=y
CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
CONFIG_BT_L2CAP_ECRED=y
CONFIG_BT_EATT=y
CONFIG_BT_EATT_MAX=1
CONFIG_ASSERT=y
CONFIG_BT_TESTING=y

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "common.h"
#include "argparse.h"
void test_tick(bs_time_t HW_device_time)
{
if (bst_result != Passed) {
FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME);
}
}
void test_init(void)
{
bst_ticker_set_next_tick_absolute(WAIT_TIME);
bst_result = In_progress;
}
#define CHANNEL_ID 0
void backchannel_init(void)
{
uint device_number = get_device_nbr();
uint peer_number = device_number ^ 1;
uint device_numbers[] = { peer_number };
uint channel_numbers[] = { CHANNEL_ID };
uint *ch;
ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
ARRAY_SIZE(channel_numbers));
if (!ch) {
FAIL("Unable to open backchannel\n");
}
}
void backchannel_sync_send(void)
{
/* Dummy message */
uint8_t sync_msg[] = { 'A' };
printk("Sending sync\n");
bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
}
void backchannel_sync_wait(void)
{
while (!bs_bc_is_msg_received(CHANNEL_ID)) {
k_sleep(K_MSEC(1));
}
printk("Sync received\n");
}

View file

@ -0,0 +1,70 @@
/**
* Common functions and helpers for BSIM GATT tests
*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "kernel.h"
#include "bs_types.h"
#include "bs_tracing.h"
#include "time_machine.h"
#include "bstests.h"
#include "bs_pc_backchannel.h"
#include <zephyr/types.h>
#include <stddef.h>
#include <errno.h>
#include <zephyr.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
extern enum bst_result_t bst_result;
#define WAIT_TIME (30 * 1e6) /*seconds*/
#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false
#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true)
#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false)
#define WAIT_FOR_FLAG(flag) \
while (!(bool)atomic_get(&flag)) { \
(void)k_sleep(K_MSEC(1)); \
}
#define FAIL(...) \
do { \
bst_result = Failed; \
bs_trace_error_time_line(__VA_ARGS__); \
} while (0)
#define PASS(...) \
do { \
bst_result = Passed; \
bs_trace_info_time(1, __VA_ARGS__); \
} while (0)
#define CHRC_SIZE 10
#define TEST_SERVICE_UUID \
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00)
#define TEST_CHRC_UUID \
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x00)
#define TEST_ADDITIONAL_CHRC_UUID \
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x11)
void test_tick(bs_time_t HW_device_time);
void test_init(void);
void backchannel_init(void);
void backchannel_sync_send(void);
void backchannel_sync_wait(void);

View file

@ -0,0 +1,359 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <bluetooth/bluetooth.h>
#include <bluetooth/gatt.h>
#include "common.h"
CREATE_FLAG(flag_is_connected);
CREATE_FLAG(flag_discover_complete);
CREATE_FLAG(flag_write_complete);
CREATE_FLAG(flag_chan_1_read);
CREATE_FLAG(flag_chan_2_read);
CREATE_FLAG(flag_db_hash_read);
static struct bt_conn *g_conn;
static uint16_t chrc_handle;
static uint16_t csf_handle;
static const struct bt_uuid *test_svc_uuid = TEST_SERVICE_UUID;
static void connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (err != 0) {
FAIL("Failed to connect to %s (%u)\n", addr, err);
return;
}
printk("Connected to %s\n", addr);
SET_FLAG(flag_is_connected);
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
if (conn != g_conn) {
return;
}
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
bt_conn_unref(g_conn);
g_conn = NULL;
UNSET_FLAG(flag_is_connected);
}
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
};
void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad)
{
char addr_str[BT_ADDR_LE_STR_LEN];
int err;
if (g_conn != NULL) {
return;
}
/* We're only interested in connectable events */
if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
return;
}
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
printk("Stopping scan\n");
err = bt_le_scan_stop();
if (err != 0) {
FAIL("Could not stop scan (err %d)\n");
return;
}
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn);
if (err != 0) {
FAIL("Could not connect to peer (err %d)", err);
}
}
static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params)
{
int err;
if (attr == NULL) {
if (chrc_handle == 0) {
FAIL("Did not discover chrc (%x)\n", chrc_handle);
}
(void)memset(params, 0, sizeof(*params));
SET_FLAG(flag_discover_complete);
return BT_GATT_ITER_STOP;
}
printk("[ATTRIBUTE] handle %u\n", attr->handle);
if (params->type == BT_GATT_DISCOVER_PRIMARY &&
bt_uuid_cmp(params->uuid, TEST_SERVICE_UUID) == 0) {
printk("Found test service\n");
params->uuid = NULL;
params->start_handle = attr->handle + 1;
params->type = BT_GATT_DISCOVER_CHARACTERISTIC;
err = bt_gatt_discover(conn, params);
if (err != 0) {
FAIL("Discover failed (err %d)\n", err);
}
return BT_GATT_ITER_STOP;
} else if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
const struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
if (bt_uuid_cmp(chrc->uuid, TEST_CHRC_UUID) == 0) {
printk("Found chrc\n");
chrc_handle = chrc->value_handle;
} else if (bt_uuid_cmp(chrc->uuid, BT_UUID_GATT_CLIENT_FEATURES) == 0) {
printk("Found csf\n");
csf_handle = chrc->value_handle;
}
}
return BT_GATT_ITER_CONTINUE;
}
static void gatt_discover(const struct bt_uuid *uuid, uint8_t type)
{
static struct bt_gatt_discover_params discover_params;
int err;
printk("Discovering services and characteristics\n");
discover_params.uuid = uuid;
discover_params.func = discover_func;
discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
discover_params.type = type;
UNSET_FLAG(flag_discover_complete);
err = bt_gatt_discover(g_conn, &discover_params);
if (err != 0) {
FAIL("Discover failed(err %d)\n", err);
}
WAIT_FOR_FLAG(flag_discover_complete);
printk("Discover complete\n");
}
static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_read_params *params,
const void *data, uint16_t length);
static struct bt_gatt_read_params chan_1_read = {
.func = gatt_read_cb,
.handle_count = 1,
.single = {
.handle = 0, /* Will be set later */
.offset = 0,
},
};
static struct bt_gatt_read_params chan_2_read = {
.func = gatt_read_cb,
.handle_count = 1,
.single = {
.handle = 0, /* Will be set later */
.offset = 0,
},
};
static struct bt_gatt_read_params db_hash_read = {
.func = gatt_read_cb,
.handle_count = 0,
.by_uuid = {
.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE,
.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
.uuid = BT_UUID_GATT_DB_HASH,
},
};
void expect_status(uint8_t err, uint8_t status)
{
if (err != status) {
FAIL("Unexpected status from read: 0x%02X, expected 0x%02X\n", err, status);
}
}
static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_read_params *params,
const void *data, uint16_t length)
{
printk("GATT read cb: err 0x%02X\n", err);
if (params == &db_hash_read) {
expect_status(err, BT_ATT_ERR_SUCCESS);
SET_FLAG(flag_db_hash_read);
} else if (params == &chan_1_read) {
if (flag_db_hash_read) {
expect_status(err, BT_ATT_ERR_SUCCESS);
} else {
expect_status(err, BT_ATT_ERR_DB_OUT_OF_SYNC);
}
SET_FLAG(flag_chan_1_read);
} else if (params == &chan_2_read) {
if (flag_db_hash_read) {
expect_status(err, BT_ATT_ERR_SUCCESS);
} else {
expect_status(err, BT_ATT_ERR_DB_OUT_OF_SYNC);
}
SET_FLAG(flag_chan_2_read);
} else {
FAIL("Unexpected params\n");
}
return 0;
}
static void gatt_read(struct bt_gatt_read_params *read_params)
{
int err;
printk("Reading\n");
err = bt_gatt_read(g_conn, read_params);
if (err != 0) {
FAIL("bt_gatt_read failed: %d\n", err);
}
}
static void write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params)
{
if (err != BT_ATT_ERR_SUCCESS) {
FAIL("Write failed: 0x%02X\n", err);
}
SET_FLAG(flag_write_complete);
}
static void enable_robust_caching(void)
{
/* Client Supported Features Characteristic Value
* Bit 0: Robust Caching
* Bit 1: EATT
*/
static const uint8_t csf[] = { BIT(0) | BIT(1) };
static struct bt_gatt_write_params write_params = {
.func = write_cb,
.offset = 0,
.data = csf,
.length = sizeof(csf),
};
int err;
printk("Writing to Client Supported Features Characteristic\n");
write_params.handle = csf_handle;
UNSET_FLAG(flag_write_complete);
err = bt_gatt_write(g_conn, &write_params);
if (err) {
FAIL("bt_gatt_write failed (err %d)\n", err);
}
WAIT_FOR_FLAG(flag_write_complete);
printk("Success\n");
}
static void test_main(void)
{
int err;
backchannel_init();
err = bt_enable(NULL);
if (err != 0) {
FAIL("Bluetooth discover failed (err %d)\n", err);
}
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
if (err != 0) {
FAIL("Scanning failed to start (err %d)\n", err);
}
printk("Scanning successfully started\n");
WAIT_FOR_FLAG(flag_is_connected);
gatt_discover(test_svc_uuid, BT_GATT_DISCOVER_PRIMARY);
gatt_discover(BT_UUID_GATT_CLIENT_FEATURES, BT_GATT_DISCOVER_CHARACTERISTIC);
enable_robust_caching();
while (bt_eatt_count(g_conn) < 1) {
/* Wait for EATT channel to connect, in case it hasn't already */
k_sleep(K_MSEC(10));
}
/* Tell the server to register additional service */
backchannel_sync_send();
/* Wait for new service to be added by server */
backchannel_sync_wait();
chan_1_read.single.handle = chrc_handle;
chan_2_read.single.handle = chrc_handle;
gatt_read(&chan_1_read);
gatt_read(&chan_2_read);
/* Wait until received response on both reads. When robust caching is implemented
* on the client side, the waiting shall be done automatically by the host when
* reading the DB hash.
*/
WAIT_FOR_FLAG(flag_chan_1_read);
WAIT_FOR_FLAG(flag_chan_2_read);
gatt_read(&db_hash_read);
WAIT_FOR_FLAG(flag_db_hash_read);
UNSET_FLAG(flag_chan_1_read);
UNSET_FLAG(flag_chan_2_read);
gatt_read(&chan_1_read);
gatt_read(&chan_2_read);
WAIT_FOR_FLAG(flag_chan_1_read);
WAIT_FOR_FLAG(flag_chan_2_read);
PASS("GATT client Passed\n");
}
static const struct bst_test_instance test_vcs[] = {
{
.test_id = "gatt_client",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main,
},
BSTEST_END_MARKER,
};
struct bst_test_list *test_gatt_client_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_vcs);
}

View file

@ -0,0 +1,137 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "common.h"
extern enum bst_result_t bst_result;
CREATE_FLAG(flag_is_connected);
CREATE_FLAG(flag_read_done);
static struct bt_conn *g_conn;
static void connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (err != 0) {
FAIL("Failed to connect to %s (%u)\n", addr, err);
return;
}
printk("Connected to %s\n", addr);
g_conn = bt_conn_ref(conn);
SET_FLAG(flag_is_connected);
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
if (conn != g_conn) {
return;
}
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
bt_conn_unref(g_conn);
g_conn = NULL;
UNSET_FLAG(flag_is_connected);
}
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
};
#define ARRAY_ITEM(i, _) i
static const uint8_t chrc_data[] = { LISTIFY(CHRC_SIZE, ARRAY_ITEM, (,)) }; /* 1, 2, 3 ... */
static ssize_t read_test_chrc(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
printk("Characteristic read\n");
SET_FLAG(flag_read_done);
return bt_gatt_attr_read(conn, attr, buf, len, offset, (const void *)chrc_data, CHRC_SIZE);
}
BT_GATT_SERVICE_DEFINE(test_svc, BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID, BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
read_test_chrc, NULL, NULL));
static struct bt_gatt_attr additional_attributes[] = {
BT_GATT_CHARACTERISTIC(TEST_ADDITIONAL_CHRC_UUID, 0, BT_GATT_PERM_NONE, NULL, NULL, NULL),
};
static struct bt_gatt_service additional_gatt_service = BT_GATT_SERVICE(additional_attributes);
static void test_main(void)
{
int err;
const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS,
(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)) };
backchannel_init();
err = bt_enable(NULL);
if (err != 0) {
FAIL("Bluetooth init failed (err %d)\n", err);
return;
}
printk("Bluetooth initialized\n");
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err != 0) {
FAIL("Advertising failed to start (err %d)\n", err);
return;
}
printk("Advertising successfully started\n");
WAIT_FOR_FLAG(flag_is_connected);
/* Wait for client to do discovery and configuration */
backchannel_sync_wait();
printk("Registering additional service\n");
err = bt_gatt_service_register(&additional_gatt_service);
if (err < 0) {
FAIL("Registering additional service failed (err %d)\n", err);
}
/* Signal to client that additional service is registered */
backchannel_sync_send();
WAIT_FOR_FLAG(flag_read_done);
PASS("GATT server passed\n");
}
static const struct bst_test_instance test_gatt_server[] = {
{
.test_id = "gatt_server",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main,
},
BSTEST_END_MARKER,
};
struct bst_test_list *test_gatt_server_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_gatt_server);
}

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "bstests.h"
extern struct bst_test_list *test_gatt_server_install(struct bst_test_list *tests);
extern struct bst_test_list *test_gatt_client_install(struct bst_test_list *tests);
bst_test_install_t test_installers[] = {
test_gatt_server_install,
test_gatt_client_install,
NULL
};
void main(void)
{
bst_main();
}

View file

@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Copyright 2022 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
simulation_id="gatt_change_aware"
verbosity_level=2
process_ids=""; exit_code=0
function Execute(){
if [ ! -f $1 ]; then
echo -e " \e[91m`pwd`/`basename $1` cannot be found (did you forget to\
compile it?)\e[39m"
exit 1
fi
timeout 120 $@ & process_ids="$process_ids $!"
}
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
#Give a default value to BOARD if it does not have one yet:
BOARD="${BOARD:-nrf52_bsim}"
cd ${BSIM_OUT_PATH}/bin
Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_gatt_caching_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=gatt_client
Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_gatt_caching_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=1 -testid=gatt_server
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=60e6 $@
for process_id in $process_ids; do
wait $process_id || let "exit_code=$?"
done
exit $exit_code #the last exit code != 0

View file

@ -20,6 +20,7 @@ mkdir -p ${WORK_DIR}
source ${ZEPHYR_BASE}/tests/bluetooth/bsim_bt/compile.source source ${ZEPHYR_BASE}/tests/bluetooth/bsim_bt/compile.source
app=tests/bluetooth/bsim_bt/bsim_test_gatt_caching compile
app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_encryption.conf compile app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_encryption.conf compile
app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_collision.conf compile app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_collision.conf compile
app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_multiple_conn.conf compile app=tests/bluetooth/bsim_bt/bsim_test_eatt conf_file=prj_multiple_conn.conf compile