Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BSIM based BLE tests #1875

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .devcontainer/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ fi
if [ -f "$WORKSPACE_DIR/zephyr/zephyr-env.sh" ]; then
source "$WORKSPACE_DIR/zephyr/zephyr-env.sh"
fi

if [ -d "$WORKSPACE_DIR/tools/bsim" ]; then
export BSIM_OUT_PATH="$WORKSPACE_DIR/tools/bsim/"
export BSIM_COMPONENTS_PATH="$WORKSPACE_DIR/tools/bsim/components/"
fi
78 changes: 78 additions & 0 deletions .github/workflows/ble-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: BLE Tests

on:
push:
paths:
- ".github/workflows/ble-test.yml"
- "app/tests/ble/**"
- "app/src/**"
- "app/run-ble-test.sh"
pull_request:
paths:
- ".github/workflows/ble-test.yml"
- "app/tests/ble/**"
- "app/src/**"
- "app/run-ble-test.sh"

jobs:
collect-tests:
outputs:
test-dirs: ${{ steps.test-dirs.outputs.test-dirs }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Find test directories
id: test-dirs
run: |
cd app/tests/ble
export TESTS=$(ls -d * | grep -v central | jq -R -s -c 'split("\n")[:-1]')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to not have the test cases and the test builds be sibling folders. If we ever need to add a "peripheral" build, then this command has to become more complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not sure it's worth the complexity trying to generalize it at this point.

echo "::set-output name=test-dirs::${TESTS}"
run-tests:
needs: collect-tests
strategy:
matrix:
test: ${{ fromJSON(needs.collect-tests.outputs.test-dirs) }}
runs-on: ubuntu-latest
container:
image: docker.io/zmkfirmware/zmk-build-arm:3.2
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Cache west modules
uses: actions/[email protected]
env:
cache-name: cache-zephyr-modules
with:
path: |
modules/
tools/
zephyr/
bootloader/
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('app/west.yml') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
timeout-minutes: 2
continue-on-error: true
- name: Initialize workspace (west init)
run: west init -l app
- name: Enable babblesim group filter
run: west config manifest.group-filter -- +babblesim
- name: Update modules (west update)
run: west update
- name: Export Zephyr CMake package (west zephyr-export)
run: west zephyr-export
- name: Build BabbleSim components
working-directory: tools/bsim
run: make everything
- name: Test ${{ matrix.test }}
working-directory: app
run: BSIM_COMPONENTS_PATH="${GITHUB_WORKSPACE}/tools/bsim/components/" BSIM_OUT_PATH="${GITHUB_WORKSPACE}/tools/bsim/" ./run-ble-test.sh tests/ble/${{ matrix.test }}
- name: Archive artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: "log-files"
path: app/build/**/*.log
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
id: test-dirs
run: |
cd app/tests/
export TESTS=$(ls -d * | jq -R -s -c 'split("\n")[:-1]')
export TESTS=$(ls -d * | grep -v ble | jq -R -s -c 'split("\n")[:-1]')
petejohanson marked this conversation as resolved.
Show resolved Hide resolved
echo "::set-output name=test-dirs::${TESTS}"
run-tests:
needs: collect-tests
Expand Down
6 changes: 3 additions & 3 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ menuconfig ZMK_BLE
select BT_SMP_APP_PAIRING_ACCEPT
select BT_PERIPHERAL
select BT_DIS
select BT_SETTINGS
select SETTINGS
imply ZMK_BATTERY_REPORTING
imply BT_SETTINGS if !ARCH_POSIX
imply SETTINGS if !ARCH_POSIX
imply ZMK_BATTERY_REPORTING if !ARCH_POSIX

if ZMK_BLE

Expand Down
10 changes: 10 additions & 0 deletions app/boards/native_posix_64.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/ {
chosen {
zephyr,console = &uart0;
zmk,kscan = &kscan;
};

Expand All @@ -15,4 +16,13 @@
columns = <2>;
exit-after;
};

uart0: uart {
status = "okay";
compatible = "zephyr,native-posix-uart";
/* Dummy current-speed entry to comply with serial
* DTS binding
*/
current-speed = <0>;
};
};
4 changes: 4 additions & 0 deletions app/boards/nrf52_bsim.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_ZMK_BLE=y
CONFIG_LOG=y
CONFIG_LOG_BACKEND_SHOW_COLOR=n
CONFIG_ZMK_LOG_LEVEL_DBG=y
17 changes: 17 additions & 0 deletions app/boards/nrf52_bsim.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/kscan_mock.h>

/ {
chosen {
zmk,kscan = &kscan;
};

kscan: kscan {
compatible = "zmk,kscan-mock";
label = "KSCAN_MOCK";

rows = <2>;
columns = <2>;
};
};
107 changes: 107 additions & 0 deletions app/run-ble-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/bin/bash
petejohanson marked this conversation as resolved.
Show resolved Hide resolved

# Copyright (c) 2023 The ZMK Contributors
# SPDX-License-Identifier: MIT

if [ -z "$1" ]; then
echo "Usage: ./run-ble-test.sh <path to testcase>"
exit 1
fi

path=$1
if [ "$path" = "all" ]; then
path="tests"
fi

if [ -z "${BSIM_OUT_PATH}" ]; then
echo "BSIM_OUT_PATH needs to be set before running this script."
exit 1
fi

if [ -z "$BLE_TESTS_NO_CENTRAL_BUILD" ]; then
if ! [ -e build/tests/ble/central ]; then
west build -d build/tests/ble/central -b nrf52_bsim tests/ble/central > /dev/null 2>&1
else
west build -d build/tests/ble/central
fi

cp build/tests/ble/central/zephyr/zephyr.exe "${BSIM_OUT_PATH}/bin/ble_test_central.exe"
caksoylar marked this conversation as resolved.
Show resolved Hide resolved

if ! [ -e build/tests/ble/private_central ]; then
west build -d build/tests/ble/private_central -b nrf52_bsim tests/ble/central -- -DCONFIG_BT_PRIVACY=y -DCONFIG_BT_SCAN_WITH_IDENTITY=n > /dev/null 2>&1
else
west build -d build/tests/ble/private_central
fi

cp build/tests/ble/private_central/zephyr/zephyr.exe "${BSIM_OUT_PATH}/bin/ble_test_private_central.exe"
fi

testcases=$(find $path -name nrf52_bsim.keymap -exec dirname \{\} \;)
num_cases=$(echo "$testcases" | wc -l)
if [ $num_cases -gt 1 ] || [ "$testcases" != "${path%%/}" ]; then
echo "$testcases"
echo "" > ./build/tests/pass-fail.log
echo "$testcases" | BLE_TESTS_QUIET_OUTPUT=y BLE_TESTS_NO_CENTRAL_BUILD=y xargs -L 1 -P ${J:-4} ./run-ble-test.sh
err=$?
sort -k2 ./build/tests/pass-fail.log
exit $err
fi

testcase="$path"
echo "Running $testcase:"

west build -d build/$testcase -b nrf52_bsim -- -DZMK_CONFIG="$(pwd)/$testcase" > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "FAILED: $testcase did not build" | tee -a ./build/tests/pass-fail.log
exit 1
fi

if [ -n "${BLE_TESTS_QUIET_OUTPUT}" ]; then
output_dev="/dev/null"
else
output_dev="/dev/stdout"
fi

exe_name=${testcase//\//_}

start_dir=$(pwd)
cp build/$testcase/zephyr/zmk.exe "${BSIM_OUT_PATH}/bin/${exe_name}"
pushd "${BSIM_OUT_PATH}/bin" > /dev/null 2>&1
if [ -e "${start_dir}/build/$testcase/output.log" ]; then
rm "${start_dir}/build/$testcase/output.log"
fi

central_counts=$(wc -l ${start_dir}/${testcase}/centrals.txt | cut -d' ' -f1)
./${exe_name} -d=0 -s=${exe_name} | tee -a "${start_dir}/build/$testcase/output.log" > "${output_dev}" &
./bs_device_handbrake -s=${exe_name} -d=1 -r=10 > "${output_dev}" &

cat "${start_dir}/${testcase}/centrals.txt" |
while IFS= read -r line
do
${line} -s=${exe_name} | tee -a "${start_dir}/build/$testcase/output.log" > "${output_dev}" &
done

./bs_2G4_phy_v1 -s=${exe_name} -D=$(( 2 + central_counts )) -sim_length=50e6 > "${output_dev}" 2>&1

popd > /dev/null 2>&1

cat build/$testcase/output.log | sed -E -n -f $testcase/events.patterns > build/$testcase/filtered_output.log

diff -auZ $testcase/snapshot.log build/$testcase/filtered_output.log
if [ $? -gt 0 ]; then
if [ -f $testcase/pending ]; then
echo "PENDING: $testcase" | tee -a ./build/tests/pass-fail.log
exit 0
fi

if [ -n "${ZMK_TESTS_AUTO_ACCEPT}" ]; then
echo "Auto-accepting failure for $testcase"
cp build/$testcase/filtered_output.log $testcase/snapshot.log
else
echo "FAILED: $testcase" | tee -a ./build/tests/pass-fail.log
exit 1
fi
fi

echo "PASS: $testcase" | tee -a ./build/tests/pass-fail.log
exit 0
11 changes: 9 additions & 2 deletions app/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ if [ $? -gt 0 ]; then
echo "PENDING: $testcase" | tee -a ./build/tests/pass-fail.log
exit 0
fi
echo "FAILED: $testcase" | tee -a ./build/tests/pass-fail.log
exit 1


if [ -n "${ZMK_TESTS_AUTO_ACCEPT}" ]; then
echo "Auto-accepting failure for $testcase"
cp build/$testcase/keycode_events.log $testcase/keycode_events.snapshot
else
echo "FAILED: $testcase" | tee -a ./build/tests/pass-fail.log
exit 1
fi
fi

echo "PASS: $testcase" | tee -a ./build/tests/pass-fail.log
Expand Down
2 changes: 2 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ void set_profile_address(uint8_t index, const bt_addr_le_t *addr) {
memcpy(&profiles[index].peer, addr, sizeof(bt_addr_le_t));
sprintf(setting_name, "ble/profiles/%d", index);
LOG_DBG("Setting profile addr for %s to %s", setting_name, addr_str);
#if IS_ENABLED(CONFIG_SETTINGS)
settings_save_one(setting_name, &profiles[index], sizeof(struct zmk_ble_profile));
#endif
k_work_submit(&raise_profile_changed_event_work);
}

Expand Down
10 changes: 10 additions & 0 deletions app/tests/ble/central/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(ble_test_central)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

# zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth)
9 changes: 9 additions & 0 deletions app/tests/ble/central/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG_BT=y
CONFIG_LOG=y
CONFIG_BOOT_BANNER=n
CONFIG_BT_LOG_LEVEL_WRN=y
CONFIG_LOG_BACKEND_SHOW_COLOR=n
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_SCAN_WITH_IDENTITY=y
CONFIG_BT_GATT_CLIENT=y
Loading
Loading