1#!/usr/bin/env bash
2# Copyright (c) 2024 Nordic Semiconductor
3# SPDX-License-Identifier: Apache-2.0
4
5set -eu
6
7# Provides common functions for bsim tests.
8# Mainly `Execute`, and `wait_for_background_jobs`.
9source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
10
11# Helper variable. Expands to "tests_bsim_bluetooth_host_misc_sample_test".
12test_name="$(guess_test_long_name)"
13
14# Use a unique simulation id per test script. It is a necessity as the CI runner
15# will run all the test scripts in parallel. If multiple simulations share the
16# same ID, they will step on each other's toes in unpredictable ways.
17simulation_id=${test_name}
18
19# This controls the verbosity of the simulator. It goes up to 9 (not 11, sorry)
20# and is useful for figuring out what is happening on the PHYsical layer (e.g.
21# device 1 starts listening at T=100us) among other things.
22# `2` is the default value if not specified.
23verbosity_level=2
24
25# This sets the watchdog timeout for the `Execute` function.
26#
27# Although all simulations are started with a bounded end (the `sim_length`
28# option), something very wrong can still happen and this additional time-out
29# will ensure all executables started by the current script are killed.
30#
31# It measures wall-clock time, not simulated time. E.g. a test that simulates 5
32# hours might actually complete (have a runtime of) 10 seconds.
33#
34# The default is set in `sh_common.source`.
35# Guidelines to set this value:
36# - Do not set it to a value lower or equal to the default.
37# - If the test takes over 5 seconds of runtime, set `EXECUTE_TIMEOUT` to at
38#   least 5 times the run-time on your machine.
39EXECUTE_TIMEOUT=120
40
41# Set simulation length, in microseconds. The PHY will run for this amount of
42# simulated time, unless both devices exit.
43#
44# If you are not early-exiting the devices (e.g. using `TEST_PASS_AND_EXIT()`),
45# please run the test once and set the simulation time in the same ballpark. No
46# need to simulate hours of runtime if the test finishes in 10 seconds.
47#
48SIM_LEN_US=$((2 * 1000 * 1000))
49
50# This is the final path of the test executable.
51#
52# Some tests may have different executable images for each device in the test.
53#
54# In our case, both test cases are compiled in the same image, and the right one
55# will be run depending on what arguments we give the executable.
56test_exe="${BSIM_OUT_PATH}/bin/bs_${BOARD_TS}_${test_name}_prj_conf"
57
58# BabbleSim will by default search for its shared libraries assuming it is
59# running in the bin/ directory. Test results will also be placed in
60# `${BSIM_OUT_PATH}/results` if not specified.
61cd ${BSIM_OUT_PATH}/bin
62
63# Instantiate all devices in the simulation.
64# The `testid` parameter is used to run the right role or procedure (here "dut" vs "tester").
65Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=dut \
66		-argstest log_level 3
67Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69  -testid=peer \
68		-argstest log_level 3
69
70# Start the PHY. Double-check the `-D` parameter: it has to match the number of
71# devices started in the lines above.
72#
73# Also set a maximum simulation length. If the devices have not set a special
74# variable indicating they have passed before the simulation runs out of time,
75# the test will be reported as "in progress (not passed)".
76Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} -D=2 -sim_length=${SIM_LEN_US} $@
77
78# Wait until all executables started above have returned.
79# The exit code returned will be != 0 if any of them have failed.
80wait_for_background_jobs
81