Skip to content

Commit

Permalink
feat(bluetooth) Initial nRF52 BSIM based test support.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Oct 14, 2023
1 parent 7cd5093 commit 0fc9947
Show file tree
Hide file tree
Showing 34 changed files with 1,045 additions and 1 deletion.
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]')
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]')
echo "::set-output name=test-dirs::${TESTS}"
run-tests:
needs: collect-tests
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>;
};
};
93 changes: 93 additions & 0 deletions app/run-ble-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

# 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 "$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"

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" 2>&1 > /dev/null
rm "${start_dir}/build/$testcase/output.log"
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 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=`expr 2 + $central_counts` -sim_length=50e6 2>&1 > "${output_dev}"

popd 2>&1 > /dev/null

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
echo "FAILED: $testcase" | tee -a ./build/tests/pass-fail.log
exit 1
fi

echo "PASS: $testcase" | tee -a ./build/tests/pass-fail.log
exit 0
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)
8 changes: 8 additions & 0 deletions app/tests/ble/central/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CONFIG_BT=y
CONFIG_LOG=y
CONFIG_BOOT_BANNER=n
CONFIG_BT_LOG_LEVEL_WRN=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_SCAN_WITH_IDENTITY=y
CONFIG_BT_GATT_CLIENT=y
Loading

0 comments on commit 0fc9947

Please sign in to comment.