Bluetooth: Host: Add test for GATT tx complete callback
Checks that the callback given to bt_gatt_notify_cb is called. Signed-off-by: Herman Berget <herman.berget@nordicsemi.no>
This commit is contained in:
parent
c8b2279f89
commit
3aef3dae02
9 changed files with 723 additions and 0 deletions
21
tests/bluetooth/bsim_bt/bsim_test_notify/CMakeLists.txt
Normal file
21
tests/bluetooth/bsim_bt/bsim_test_notify/CMakeLists.txt
Normal 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/
|
||||||
|
)
|
17
tests/bluetooth/bsim_bt/bsim_test_notify/prj.conf
Normal file
17
tests/bluetooth/bsim_bt/bsim_test_notify/prj.conf
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
CONFIG_BT=y
|
||||||
|
CONFIG_BT_DEVICE_NAME="GATT tester"
|
||||||
|
CONFIG_BT_PERIPHERAL=y
|
||||||
|
CONFIG_BT_CENTRAL=y
|
||||||
|
CONFIG_BT_GATT_CLIENT=y
|
||||||
|
|
||||||
|
CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y
|
||||||
|
|
||||||
|
CONFIG_BT_SMP=y
|
||||||
|
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
|
||||||
|
CONFIG_BT_L2CAP_ECRED=y
|
||||||
|
CONFIG_BT_EATT=y
|
||||||
|
CONFIG_BT_EATT_MAX=5
|
||||||
|
|
||||||
|
CONFIG_ASSERT=y
|
||||||
|
CONFIG_BT_TESTING=y
|
||||||
|
CONFIG_BT_DEBUG_LOG=y
|
20
tests/bluetooth/bsim_bt/bsim_test_notify/src/common.c
Normal file
20
tests/bluetooth/bsim_bt/bsim_test_notify/src/common.c
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "common.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;
|
||||||
|
}
|
70
tests/bluetooth/bsim_bt/bsim_test_notify/src/common.h
Normal file
70
tests/bluetooth/bsim_bt/bsim_test_notify/src/common.h
Normal 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 <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 (60 * 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 LONG_CHRC_SIZE 40
|
||||||
|
|
||||||
|
#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_LONG_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);
|
||||||
|
|
||||||
|
#define NOTIFICATION_COUNT 10
|
||||||
|
BUILD_ASSERT(NOTIFICATION_COUNT % 2 == 0);
|
309
tests/bluetooth/bsim_bt/bsim_test_notify/src/gatt_client_test.c
Normal file
309
tests/bluetooth/bsim_bt/bsim_test_notify/src/gatt_client_test.c
Normal file
|
@ -0,0 +1,309 @@
|
||||||
|
/*
|
||||||
|
* 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_is_encrypted);
|
||||||
|
CREATE_FLAG(flag_discover_complete);
|
||||||
|
CREATE_FLAG(flag_subscribed);
|
||||||
|
|
||||||
|
static struct bt_conn *g_conn;
|
||||||
|
static uint16_t chrc_handle;
|
||||||
|
static uint16_t long_chrc_handle;
|
||||||
|
static 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
|
||||||
|
{
|
||||||
|
if (err) {
|
||||||
|
FAIL("Encryption failer (%d)\n", err);
|
||||||
|
} else if (level < BT_SECURITY_L2) {
|
||||||
|
FAIL("Insufficient sec level (%d)\n", level);
|
||||||
|
} else {
|
||||||
|
SET_FLAG(flag_is_encrypted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
||||||
|
.connected = connected,
|
||||||
|
.disconnected = disconnected,
|
||||||
|
.security_changed = security_changed,
|
||||||
|
};
|
||||||
|
|
||||||
|
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: %d");
|
||||||
|
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: %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 || long_chrc_handle == 0) {
|
||||||
|
FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle,
|
||||||
|
long_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, TEST_LONG_CHRC_UUID) == 0) {
|
||||||
|
printk("Found long_chrc\n");
|
||||||
|
long_chrc_handle = chrc->value_handle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return BT_GATT_ITER_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gatt_discover(void)
|
||||||
|
{
|
||||||
|
static struct bt_gatt_discover_params discover_params;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("Discovering services and characteristics\n");
|
||||||
|
|
||||||
|
discover_params.uuid = test_svc_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 = BT_GATT_DISCOVER_PRIMARY;
|
||||||
|
|
||||||
|
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 void test_subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params)
|
||||||
|
{
|
||||||
|
if (err) {
|
||||||
|
FAIL("Subscribe failed (err %d)\n", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
SET_FLAG(flag_subscribed);
|
||||||
|
|
||||||
|
if (!params) {
|
||||||
|
printk("params NULL\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params->handle == chrc_handle) {
|
||||||
|
printk("Subscribed to short characteristic\n");
|
||||||
|
} else if (params->handle == long_chrc_handle) {
|
||||||
|
printk("Subscribed to long characteristic\n");
|
||||||
|
} else {
|
||||||
|
FAIL("Unknown handle %d\n", params->handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static volatile size_t num_notifications;
|
||||||
|
uint8_t test_notify(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data,
|
||||||
|
uint16_t length)
|
||||||
|
{
|
||||||
|
printk("Received notification #%u with length %d\n", num_notifications++, length);
|
||||||
|
|
||||||
|
return BT_GATT_ITER_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct bt_gatt_discover_params disc_params_short;
|
||||||
|
static struct bt_gatt_subscribe_params sub_params_short = {
|
||||||
|
.notify = test_notify,
|
||||||
|
.write = test_subscribed,
|
||||||
|
.ccc_handle = 0, /* Auto-discover CCC*/
|
||||||
|
.disc_params = &disc_params_short, /* Auto-discover CCC */
|
||||||
|
.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
|
||||||
|
.value = BT_GATT_CCC_NOTIFY,
|
||||||
|
};
|
||||||
|
static struct bt_gatt_discover_params disc_params_long;
|
||||||
|
static struct bt_gatt_subscribe_params sub_params_long = {
|
||||||
|
.notify = test_notify,
|
||||||
|
.write = test_subscribed,
|
||||||
|
.ccc_handle = 0, /* Auto-discover CCC*/
|
||||||
|
.disc_params = &disc_params_long, /* Auto-discover CCC */
|
||||||
|
.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
|
||||||
|
.value = BT_GATT_CCC_NOTIFY,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void gatt_subscribe_short(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
sub_params_short.value_handle = chrc_handle;
|
||||||
|
err = bt_gatt_subscribe(g_conn, &sub_params_short);
|
||||||
|
if (err < 0) {
|
||||||
|
FAIL("Failed to subscribe\n");
|
||||||
|
} else {
|
||||||
|
printk("Subscribe request sent\n");
|
||||||
|
}
|
||||||
|
WAIT_FOR_FLAG(flag_subscribed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gatt_subscribe_long(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
UNSET_FLAG(flag_subscribed);
|
||||||
|
sub_params_long.value_handle = long_chrc_handle;
|
||||||
|
err = bt_gatt_subscribe(g_conn, &sub_params_long);
|
||||||
|
if (err < 0) {
|
||||||
|
FAIL("Failed to subscribe\n");
|
||||||
|
} else {
|
||||||
|
printk("Subscribe request sent\n");
|
||||||
|
}
|
||||||
|
WAIT_FOR_FLAG(flag_subscribed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_main(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
err = bt_conn_set_security(g_conn, BT_SECURITY_L2);
|
||||||
|
if (err) {
|
||||||
|
FAIL("Starting encryption procedure failed (%d)\n", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
WAIT_FOR_FLAG(flag_is_encrypted);
|
||||||
|
|
||||||
|
while (bt_eatt_count(g_conn) < CONFIG_BT_EATT_MAX) {
|
||||||
|
k_sleep(K_MSEC(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("EATT connected\n");
|
||||||
|
|
||||||
|
gatt_discover();
|
||||||
|
gatt_subscribe_short();
|
||||||
|
gatt_subscribe_long();
|
||||||
|
|
||||||
|
printk("Subscribed\n");
|
||||||
|
|
||||||
|
while (num_notifications < NOTIFICATION_COUNT) {
|
||||||
|
k_sleep(K_MSEC(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
224
tests/bluetooth/bsim_bt/bsim_test_notify/src/gatt_server_test.c
Normal file
224
tests/bluetooth/bsim_bt/bsim_test_notify/src/gatt_server_test.c
Normal file
|
@ -0,0 +1,224 @@
|
||||||
|
/*
|
||||||
|
* 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_short_subscribe);
|
||||||
|
CREATE_FLAG(flag_long_subscribe);
|
||||||
|
|
||||||
|
static struct bt_conn *g_conn;
|
||||||
|
|
||||||
|
#define ARRAY_ITEM(i, _) i
|
||||||
|
const uint8_t chrc_data[] = { LISTIFY(CHRC_SIZE, ARRAY_ITEM, (,)) }; /* 1, 2, 3 ... */
|
||||||
|
const uint8_t long_chrc_data[] = { LISTIFY(LONG_CHRC_SIZE, ARRAY_ITEM, (,)) }; /* 1, 2, 3 ... */
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
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("Read short\n");
|
||||||
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, (void *)chrc_data,
|
||||||
|
sizeof(chrc_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t read_long_test_chrc(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
|
||||||
|
uint16_t len, uint16_t offset)
|
||||||
|
{
|
||||||
|
printk("Read long\n");
|
||||||
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, (void *)long_chrc_data,
|
||||||
|
sizeof(long_chrc_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void short_subscribe(const struct bt_gatt_attr *attr, uint16_t value)
|
||||||
|
{
|
||||||
|
const bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
|
||||||
|
|
||||||
|
if (notif_enabled) {
|
||||||
|
SET_FLAG(flag_short_subscribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("Short notifications %s\n", notif_enabled ? "enabled" : "disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void long_subscribe(const struct bt_gatt_attr *attr, uint16_t value)
|
||||||
|
{
|
||||||
|
const bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
|
||||||
|
|
||||||
|
if (notif_enabled) {
|
||||||
|
SET_FLAG(flag_long_subscribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("Long notifications %s\n", notif_enabled ? "enabled" : "disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
BT_GATT_SERVICE_DEFINE(test_svc, BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
|
||||||
|
BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID,
|
||||||
|
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_READ,
|
||||||
|
BT_GATT_PERM_READ, read_test_chrc, NULL, NULL),
|
||||||
|
BT_GATT_CCC(short_subscribe, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||||
|
BT_GATT_CHARACTERISTIC(TEST_LONG_CHRC_UUID,
|
||||||
|
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_READ,
|
||||||
|
BT_GATT_PERM_READ, read_long_test_chrc, NULL, NULL),
|
||||||
|
BT_GATT_CCC(long_subscribe, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE));
|
||||||
|
|
||||||
|
static volatile size_t num_notifications_sent;
|
||||||
|
|
||||||
|
static void notification_sent(struct bt_conn *conn, void *user_data)
|
||||||
|
{
|
||||||
|
size_t *length = user_data;
|
||||||
|
|
||||||
|
printk("Sent notification #%u with length %d\n", num_notifications_sent++, *length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void short_notify(void)
|
||||||
|
{
|
||||||
|
static size_t length = CHRC_SIZE;
|
||||||
|
static struct bt_gatt_notify_params params = {
|
||||||
|
.attr = &attr_test_svc[1],
|
||||||
|
.data = chrc_data,
|
||||||
|
.len = CHRC_SIZE,
|
||||||
|
.func = notification_sent,
|
||||||
|
.user_data = &length,
|
||||||
|
.uuid = NULL,
|
||||||
|
};
|
||||||
|
int err;
|
||||||
|
|
||||||
|
do {
|
||||||
|
err = bt_gatt_notify_cb(g_conn, ¶ms);
|
||||||
|
|
||||||
|
if (err == -ENOMEM) {
|
||||||
|
k_sleep(K_MSEC(10));
|
||||||
|
} else if (err) {
|
||||||
|
FAIL("Short notify failed (err %d)\n", err);
|
||||||
|
}
|
||||||
|
} while (err);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void long_notify(void)
|
||||||
|
{
|
||||||
|
static size_t length = LONG_CHRC_SIZE;
|
||||||
|
static struct bt_gatt_notify_params params = {
|
||||||
|
.attr = &attr_test_svc[4],
|
||||||
|
.data = long_chrc_data,
|
||||||
|
.len = LONG_CHRC_SIZE,
|
||||||
|
.func = notification_sent,
|
||||||
|
.user_data = &length,
|
||||||
|
.uuid = NULL,
|
||||||
|
};
|
||||||
|
int err;
|
||||||
|
|
||||||
|
do {
|
||||||
|
err = bt_gatt_notify_cb(g_conn, ¶ms);
|
||||||
|
|
||||||
|
if (err == -ENOMEM) {
|
||||||
|
k_sleep(K_MSEC(10));
|
||||||
|
} else if (err) {
|
||||||
|
FAIL("Long notify failed (err %d)\n", err);
|
||||||
|
}
|
||||||
|
} while (err);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)),
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
while (bt_eatt_count(g_conn) < CONFIG_BT_EATT_MAX) {
|
||||||
|
k_sleep(K_MSEC(100));
|
||||||
|
}
|
||||||
|
printk("EATT connected\n");
|
||||||
|
|
||||||
|
WAIT_FOR_FLAG(flag_short_subscribe);
|
||||||
|
WAIT_FOR_FLAG(flag_long_subscribe);
|
||||||
|
|
||||||
|
for (int i = 0; i < NOTIFICATION_COUNT / 2; i++) {
|
||||||
|
short_notify();
|
||||||
|
long_notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (num_notifications_sent < NOTIFICATION_COUNT) {
|
||||||
|
k_sleep(K_MSEC(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
21
tests/bluetooth/bsim_bt/bsim_test_notify/src/main.c
Normal file
21
tests/bluetooth/bsim_bt/bsim_test_notify/src/main.c
Normal 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();
|
||||||
|
}
|
40
tests/bluetooth/bsim_bt/bsim_test_notify/test_scripts/notify.sh
Executable file
40
tests/bluetooth/bsim_bt/bsim_test_notify/test_scripts/notify.sh
Executable file
|
@ -0,0 +1,40 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# Copyright 2022 Nordic Semiconductor ASA
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
# Basic GATT test: A central acting as a GATT client scans for and connects
|
||||||
|
# to a peripheral acting as a GATT server. The GATT client will then attempt
|
||||||
|
# to write and read to and from a few GATT characteristics.
|
||||||
|
simulation_id="notify_cb"
|
||||||
|
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_notify_prj_conf \
|
||||||
|
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=gatt_client
|
||||||
|
|
||||||
|
Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_notify_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
|
|
@ -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_notify compile
|
||||||
app=tests/bluetooth/bsim_bt/bsim_test_gatt_caching compile
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue