tests: Bluetooth: make ctrl_advx a BabbleSim test

Convert the ctrl_advx application into a Babblesim test.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
This commit is contained in:
Vinayak Kariappa Chettimada 2020-05-04 17:51:32 +05:30 committed by Carles Cufí
commit 353bb6d5cf
3 changed files with 226 additions and 10 deletions

View file

@ -1,11 +1,21 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(bluetooth_advx)
zephyr_include_directories(
${ZEPHYR_BASE}/subsys/bluetooth/controller/include
)
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_advx)
target_sources(app PRIVATE src/main.c)
zephyr_include_directories(
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
${ZEPHYR_BASE}/subsys/bluetooth/controller/include
)

View file

@ -1,8 +1,11 @@
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="AdvX"
CONFIG_BT_BROADCASTER=y
CONFIG_BT_OBSERVER=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_CTLR_ADV_EXT=y
CONFIG_BT_CTLR_ADV_PERIODIC=y
CONFIG_BT_CTLR_DEBUG_PINS=y
# CONFIG_BT_DEBUG_LOG=y
# CONFIG_BT_DEBUG_HCI_CORE=y

View file

@ -17,6 +17,11 @@
#include "ll.h"
#include "bs_types.h"
#include "bs_tracing.h"
#include "time_machine.h"
#include "bstests.h"
#define HANDLE 0x0000
#define EVT_PROP_ANON BIT(5)
#define EVT_PROP_TXP BIT(6)
@ -39,6 +44,13 @@
#define ADV_INTERVAL_PERIODIC 0x30
#define SCAN_INTERVAL 0x04
#define SCAN_WINDOW 0x04
extern enum bst_result_t bst_result;
static uint8_t const own_addr[] = {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5};
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
};
@ -56,7 +68,7 @@ static uint8_t adv_data2[] = {
2, BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
};
void main(void)
static void test_advx_main(void)
{
uint16_t evt_prop;
uint8_t adv_type;
@ -65,7 +77,7 @@ void main(void)
uint8_t phy_s;
int err;
printk("\n*Extended Advertising Demo*\n");
printk("\n*Extended Advertising test*\n");
printk("Bluetooth initializing...");
err = bt_enable(NULL);
@ -93,8 +105,15 @@ void main(void)
k_sleep(K_MSEC(1000));
printk("Starting non-conn non-scan without aux 1M advertising...");
printk("Setting advertising random address...");
handle = 0x0000;
err = ll_adv_aux_random_addr_set(handle, own_addr);
if (err) {
goto exit;
}
printk("success.\n");
printk("Starting non-conn non-scan without aux 1M advertising...");
evt_prop = EVT_PROP_TXP;
adv_type = 0x05; /* Adv. Ext. */
phy_p = ADV_PHY_1M;
@ -172,7 +191,6 @@ void main(void)
k_sleep(K_MSEC(1000));
printk("Starting periodic 1M advertising...");
handle = 0x0000;
err = ll_adv_sync_param_set(handle, ADV_INTERVAL_PERIODIC, 0);
if (err) {
goto exit;
@ -229,8 +247,193 @@ void main(void)
}
printk("success.\n");
bst_result = Passed;
bs_trace_silent_exit(0);
return;
exit:
printk("failed (%d)\n", err);
bst_result = Failed;
bs_trace_silent_exit(0);
}
static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
struct net_buf_simple *buf)
{
printk("%s: type = 0x%x.\n", __func__, adv_type);
}
static const char *phy2str(uint8_t phy)
{
switch (phy) {
case 0: return "No packets";
case BT_GAP_LE_PHY_1M: return "LE 1M";
case BT_GAP_LE_PHY_2M: return "LE 2M";
case BT_GAP_LE_PHY_CODED: return "LE Coded";
default: return "Unknown";
}
}
#define NAME_LEN 30
static bool data_cb(struct bt_data *data, void *user_data)
{
char *name = user_data;
switch (data->type) {
case BT_DATA_NAME_SHORTENED:
case BT_DATA_NAME_COMPLETE:
memcpy(name, data->data, MIN(data->data_len, NAME_LEN - 1));
return false;
default:
return true;
}
}
static void scan_recv(const struct bt_le_scan_recv_info *info,
struct net_buf_simple *buf)
{
char le_addr[BT_ADDR_LE_STR_LEN];
char name[NAME_LEN];
(void)memset(name, 0, sizeof(name));
bt_data_parse(buf, data_cb, name);
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
printk("[DEVICE]: %s, AD evt type %u, Tx Pwr: %i, RSSI %i %s "
"C:%u S:%u D:%u SR:%u E:%u Prim: %s, Secn: %s "
"SID: %u\n",
le_addr, info->adv_type, info->tx_power, info->rssi, name,
(info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_DIRECTED) != 0,
(info->adv_props & BT_GAP_ADV_PROP_SCAN_RESPONSE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0,
phy2str(info->primary_phy), phy2str(info->secondary_phy),
info->sid);
}
static struct bt_le_scan_cb scan_callbacks = {
.recv = scan_recv,
};
static void test_scanx_main(void)
{
struct bt_le_scan_param scan_param = {
.type = BT_HCI_LE_SCAN_ACTIVE,
.options = BT_LE_SCAN_OPT_NONE,
.interval = 0x0004,
.window = 0x0004,
};
int err;
printk("\n*Extended Scanning test*\n");
printk("Bluetooth initializing...");
err = bt_enable(NULL);
if (err) {
goto exit;
}
printk("success.\n");
printk("Scan callbacks register...");
bt_le_scan_cb_register(&scan_callbacks);
printk("success.\n");
printk("Start scanning...");
err = bt_le_scan_start(&scan_param, scan_cb);
if (err) {
goto exit;
}
printk("success.\n");
k_sleep(K_SECONDS(5));
#if TEST_LOW_LEVEL
uint8_t type = (BIT(0) << 1) | 0x01; /* 1M PHY and active scanning */
printk("Setting scan parameters...");
err = ll_scan_params_set(type, SCAN_INTERVAL, SCAN_WINDOW,
OWN_ADDR_TYPE, FILTER_POLICY);
if (err) {
goto exit;
}
printk("success.\n");
printk("enabling...");
err = ll_scan_enable(1);
if (err) {
goto exit;
}
printk("success.\n");
k_sleep(K_SECONDS(5));
printk("Disabling...");
err = ll_scan_enable(0);
if (err) {
goto exit;
}
printk("success.\n");
#endif
bst_result = Passed;
return;
exit:
printk("failed (%d)\n", err);
bst_result = Failed;
bs_trace_silent_exit(0);
}
static void test_advx_init(void)
{
bst_ticker_set_next_tick_absolute(10e6);
bst_result = In_progress;
}
static void test_advx_tick(bs_time_t HW_device_time)
{
bst_result = Failed;
bs_trace_error_line("Test advx finished.\n");
}
static const struct bst_test_instance test_def[] = {
{
.test_id = "advx",
.test_descr = "Extended Advertising",
.test_post_init_f = test_advx_init,
.test_tick_f = test_advx_tick,
.test_main_f = test_advx_main
},
{
.test_id = "scanx",
.test_descr = "Extended scanning",
.test_post_init_f = test_advx_init,
.test_tick_f = test_advx_tick,
.test_main_f = test_scanx_main
},
BSTEST_END_MARKER
};
struct bst_test_list *test_advx_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_def);
}
bst_test_install_t test_installers[] = {
test_advx_install,
NULL
};
void main(void)
{
bst_main();
}