Host Device Simulator#

This Pigweed target simulates the behavior of an embedded device, spawning threads for facilities like RPC and logging. Executables built by this target will perpetually run until they crash or are explicitly terminated. All communications with the process are over the RPC server hosted on a local socket rather than by directly interacting with the terminal via standard I/O. Host Device Simulator is built on top of pw_system.

Setup#

Note

The instructions below show you how to try out Host Device Simulator within an upstream Pigweed environment. To set up a target similar to Host Device Simulator in your own project, see Kudzu or the examples repo.

No extra setup steps are required for Bazel.

To use this target, Pigweed must be set up to use Nanopb and FreeRTOS. The required source repositories can be downloaded via pw package, and then the build must be manually configured to point to the location the repository was downloaded to using gn args.

pw package install nanopb
pw package install freertos

gn gen out --export-compile-commands --args="
  dir_pw_third_party_nanopb=\"//environment/packages/nanopb\"
  dir_pw_third_party_freertos=\"//environment/packages/freertos\"
"

Tip

Instead of the gn gen out with args set on the command line above you can run:

gn args out

Then add the following lines to that text file:

dir_pw_third_party_nanopb = "//environment/packages/nanopb"
dir_pw_third_party_freertos = "//environment/packages/freertos"

Building and running the demo#

See also

See the examples repo device_sim.py for a downstream project example of launching a device simulator with project specific RPC protos. That script uses Pigweed upstream device_sim.py which runs the simulated device as a subprocess and then connects to it via the default socket so you just have to pass the binary.

Build and run the demo application console with:

bazel run //pw_system:simulator_system_example_console

Build the demo application:

ninja -C out pw_system_demo

Launch the demo application and connect to it with the pw_system console:

pw-system-device-sim \
  --sim-binary \
  ./out/host_device_simulator.speed_optimized/obj/pw_system/bin/system_example

Exit the console via the GUI menu, running exit or quit in the Python repl or by pressing Ctrl-D twice.

Communicate#

In the bottom-most pane labeled Python Repl you should be able to send RPC commands to the simulated device process.

To send an RPC message that will be echoed back:

>>> device.rpcs.pw.rpc.EchoService.Echo(msg='Hello, world!')
(Status.OK, pw.rpc.EchoMessage(msg='Hello, world!'))

To run unit tests included on the simulated device:

>>> device.run_tests()
True

You are now up and running!

See also

The pw_console User Guide for more info on using the the pw_console UI.