Metadata-Version: 2.4
Name: bsclock
Version: 0.1.1
Summary: Asyncio event loop driven by an application-supplied simulation clock
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/XiaoMi/bsworld/tree/main/components/bsclock
Project-URL: Issues, https://github.com/XiaoMi/bsworld/issues
Classifier: Operating System :: POSIX :: Linux
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest>=8.4; extra == "test"
Requires-Dist: ruff==0.14.10; extra == "test"
Requires-Dist: build>=1; extra == "test"
Dynamic: license-file

# BSClock

BSClock runs Python `asyncio` applications on simulation time. Use it to test a timer-driven service, a protocol host, or a simulator control loop with an application-supplied clock. With a BabbleSim device clock, the Python loop can share the same simulation timeline as native radio participants.

## Install

Use Linux and Python 3.11 or newer. BSClock uses the Python standard library.

```sh
python3 -m venv .venv
. .venv/bin/activate
python -m pip install bsclock
```

## Samples

Download the linked scripts or use the copies under `samples/` in the source distribution. Run the commands from the directory containing the scripts.

### Drive timers with your own clock

[timers.py](https://github.com/XiaoMi/bsworld/blob/main/components/bsclock/samples/timers.py) supplies an in-memory clock. The standard `asyncio` timer fires at 2 ms, and a subsequent 3 ms sleep ends at 5 ms, both in simulation time.

```sh
python timers.py
```

```text
Callback at 2000 us
Sleep completed at 5000 us
```

### Share time with a BabbleSim PHY

[babblesim_clock.py](https://github.com/XiaoMi/bsworld/blob/main/components/bsclock/samples/babblesim_clock.py) launches a PHY with one slot for the clock, waits for the first grant, then runs an `asyncio` timer.

Install `bs2g4phy` for this example and follow its [native PHY setup](https://github.com/XiaoMi/bsworld/tree/main/components/bs2g4phy#build-the-native-phy) to build the PHY, BLE modem, and default `NtNcable` channel.

```sh
python -m pip install bs2g4phy
export BSIM_OUT_PATH=/path/to/babblesim/build
python babblesim_clock.py
```

```text
First PHY grant at 0 us
Sleep completed at 5000 us
PHY log: babblesim-clock-logs/phy.log
```

## Python API

Pass `SimulationEventLoop` as the loop factory to `asyncio.Runner`. Bind any clock that exposes `now_us` and `advance_to_us(target_us)`. The clock uses integer microseconds. While it is bound, pass delays and deadlines to asyncio in seconds, aligned to whole microseconds. This applies to `asyncio.sleep()`, `loop.call_later()`, and `loop.call_at()`. The loop allows floating-point roundoff and raises `ValueError` for deadlines between simulation ticks.

Bind the clock before scheduling application timers. After `loop.unbind_clock()`, time advances at wall-clock speed from the last simulation time. Pending timers keep their deadlines.

For an existing BabbleSim simulation, reserve a device ID for `bs2g4phy.BabbleSimClock`. Connect the other participants and the clock, obtain the first grant with `clock.advance_to_us(0)`, then call `loop.bind_clock(clock)`. During the run, the loop advances only after the PHY acknowledges the requested time. Unbind the loop and close the clock as part of the simulation teardown.

### Schedule work before advancing time

Pass an `on_idle(deadline_us)` callback to `SimulationEventLoop` to admit pending work when asyncio has no ready callbacks or I/O. The loop calls it before an idle wait or clock advance. The argument is the next timer's absolute deadline in simulation microseconds, or `None` if no simulation clock or timer is active.

Return `True` after scheduling ready work to give it a turn at the current time. Return `False` to let the loop continue its usual wait or advance. The callback runs synchronously and should only decide whether to schedule work.

The `clock` and `max_step_us` properties expose the bound clock and maximum idle step. The default step is `bsclock.DEFAULT_MAX_STEP_US` (1,000 µs).

## Develop from source

From `components/bsclock` in the repository checkout, install the test tools and run the tests.

```sh
python -m pip install -e '.[test]'
python -m pytest
python -m build
```

The `bsclock-v<version>` release tag selects this package in the PyPI workflow. Its version must match `pyproject.toml`, and the workflow uses the `pypi-bsclock` Trusted Publisher environment.
