Reviewed on: 2024-02-05
Simple Driver is an example that showcase how to write a DFv2 driver with common driver patterns:
The example also implements the constructor and destructor to showcase the driver lifecycle. View the logs to see what order the constructor, DriverBase functions, and destructor are called in.
To include the driver to your build, append --with //examples/drivers:drivers
to your fx set
command. For example:
$ fx set core.x64 --with //examples/drivers:drivers $ fx build
Register the driver by running this command
$ ffx driver register fuchsia-pkg://fuchsia.com/simple_driver#meta/simple.cm
Verify that fuchsia-pkg://fuchsia.com/simple_driver#meta/simple_driver.cc
shows up in this command
ffx driver list
Add a test node that binds to the driver:
$ ffx driver test-node add simple_node gizmo.example.TEST_NODE_ID=simple_driver
Run the following command to verify that the driver is bound to the node:
$ ffx driver list-devices -v simple_node
You should see something like this:
Name : simple_node Moniker : dev.simple_node Driver : fuchsia-pkg://fuchsia.com/simple_driver#meta/simple_driver.cm 2 Properties [ 1/ 2] : Key "gizmo.example.TEST_NODE_ID" Value "simple_driver" [ 2/ 2] : Key "fuchsia.platform.DRIVER_FRAMEWORK_VERSION" Value 0x000002 0 Offers Name : simple_child Moniker : dev.simple_node.simple_child Driver : unbound 2 Properties [ 1/ 2] : Key "fuchsia.test.TEST_CHILD" Value "simple" [ 2/ 2] : Key "fuchsia.platform.DRIVER_FRAMEWORK_VERSION" Value 0x000002 0 Offers
If you want to explore the driver lifecycle and test the driver's stop functions, stop the driver by removing the node:
$ ffx driver test-node remove simple_node
View the driver logs with ffx log --filter simple
to see the execution order of the driver functions.
Include the tests to your build by appending --with //examples/drivers:tests
to your fx set
command. For example:
$ fx set core.x64 --with //examples/drivers:drivers --with //examples:tests $ fx build
Run unit tests with the command:
$ fx test simple_driver_test
The unit test runs the simple DFv2 driver and then verifies to see if a child was added.
The core implementation is in simple_driver.cc
. Unit tests are located in tests
.