struct zx_device

Defined at line 100 of file ../../src/devices/testing/mock-ddk/mock-device.h

MockDevice is an implementation of the opaque type zx_device which mocks much of

device host functionality.

Mock devices are created in one of two ways:

1) Calling FakeRootParent.

2) Calling device_add_from_driver (which may be done indirectly by DdkAdd)

Since device_add_from_driver takes zx_device_t* parent as an argument, FakeRootParent()

must be called before adding any other devices.

MockDevice does not use any global variables, so multiple fake parents can exist without

interfering with each other.

The FakeRootParent is limited in functionality, but protocols and metadata can be added to it

to facilitate a child device's needs.

The fake root parent is also important because it controls the lifecycle of its descendants.

Unlike the device host, the parent of each MockDevice holds the MockDevice's refptr.

This allows drivers to "leak" the pointer to the device like normal during bind.

However, it means that tests using MockDevices must retain the reference to the fake root parent,

or all the descendent devices will be deleted, because

the root parent will recursively release all of its children upon destruction.

When this happens, the release() op will be called on the device, allowing it to delete

any context it created.

Importantly, this is a mock implementation, not a fake. Any libDriver calls will be

recorded, but the mock will not take any automatic action. This may result in unexpected

behavior during device initialization and removal.

Initialization:

The init() op is not automatically called on newly added devices. It can be manually called

by calling MockDevice::InitOp().

Removal:

When attempting to remove dynamically devices, device_async_remove will not result in

device removal.

To process device removal, a helper function is provided below:

ReleaseFlaggedDevices recursively searches the device tree and calls unbind and release

on any device that has had device_async_remove called on it.

Calling ReleaseOp() on a MockDevice will also cause it to be deleted.

Things that this MockDevice does not handle (yet)

Rebinding

Composite devices

Fidl messages

Any automatic responses from the DDK

This needs to be a struct, not a class, to match the public definition

Public Methods

void ~zx_device ()

Defined at line 132 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void InitOp ()

Functions for calling into the driver.

These are functions that the DDK normally calls, but are exposed here for testing purposes.

They should always be called from the main test thread.

Defined at line 108 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void UnbindOp ()

Defined at line 110 of file ../../src/devices/testing/mock-ddk/mock-device.cc

std::shared_ptr<zx_device> FakeRootParent ()

Create a Root Parent. This device has limited functionality.

This will create the |fdf_testing::DriverRuntime| instance for the user. The instance will

start the driver runtime, and create a foreground driver dispatcher for the user.

Background dispatchers can be created through this object. Grab the instance using the

|fdf_testing::DriverRuntime::GetInstance()| call.

The test thread and the driver dispatcher thread are the same when using this. All driver

'op' hooks are therefore run directly on the main test thread.

Defined at line 111 of file ../../src/devices/testing/mock-ddk/mock-device.h

void ReleaseOp ()

Defined at line 112 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void SuspendNewOp (uint8_t requested_state, bool enable_wake, uint8_t suspend_reason)

Defined at line 138 of file ../../src/devices/testing/mock-ddk/mock-device.cc

bool MessageOp (fidl::IncomingHeaderAndMessage msg, device_fidl_txn_t txn)

Defined at line 142 of file ../../src/devices/testing/mock-ddk/mock-device.cc

zx_device * GetLatestChild ()

Gets the child that was added to this parent most recently.

Returns nullptr if no children exist.

Defined at line 79 of file ../../src/devices/testing/mock-ddk/mock-device.cc

zx_status_t InitReplyCallStatus ()

Defined at line 146 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool InitReplyCalled ()

Defined at line 146 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordInitReply (zx_status_t status)

Defined at line 146 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilInitReplyCalled (zx::time deadline)

Defined at line 146 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t AsyncRemoveCallStatus ()

Defined at line 147 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool AsyncRemoveCalled ()

Defined at line 147 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordAsyncRemove (zx_status_t status)

Defined at line 147 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilAsyncRemoveCalled (zx::time deadline)

Defined at line 147 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordUnbindReply (zx_status_t status)

Defined at line 148 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t UnbindReplyCallStatus ()

Defined at line 148 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool UnbindReplyCalled ()

Defined at line 148 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilUnbindReplyCalled (zx::time deadline)

Defined at line 148 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordSuspendReply (zx_status_t status)

Defined at line 149 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t SuspendReplyCallStatus ()

Defined at line 149 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool SuspendReplyCalled ()

Defined at line 149 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilSuspendReplyCalled (zx::time deadline)

Defined at line 149 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordRemove (zx_status_t status)

Defined at line 150 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t RemoveCallStatus ()

Defined at line 150 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool RemoveCalled ()

Defined at line 150 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilRemoveCalled (zx::time deadline)

Defined at line 150 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t ChildPreReleaseCallStatus ()

Defined at line 151 of file ../../src/devices/testing/mock-ddk/mock-device.h

bool ChildPreReleaseCalled ()

Defined at line 151 of file ../../src/devices/testing/mock-ddk/mock-device.h

void RecordChildPreRelease (zx_status_t status)

Defined at line 151 of file ../../src/devices/testing/mock-ddk/mock-device.h

zx_status_t WaitUntilChildPreReleaseCalled (zx::time deadline)

Defined at line 151 of file ../../src/devices/testing/mock-ddk/mock-device.h

void ChildPreReleaseOp (void * child_ctx)

Defined at line 151 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void SetMetadata (uint32_t type, const void * data, size_t data_length)

Metadata is often set for the parent of a device, to be available when the device

calls device_get_metadata

Defined at line 155 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void SetConfigVmo (zx::vmo config_vmo)

Structured configuration VMO is often set for the parent of a device, to be available when the

device calls device_get_config_vmo

Defined at line 162 of file ../../src/devices/testing/mock-ddk/mock-device.cc

bool HasUnbindOp ()

Defined at line 163 of file ../../src/devices/testing/mock-ddk/mock-device.h

cpp20::span<const zx_device_str_prop_t> GetStringProperties ()

Defined at line 165 of file ../../src/devices/testing/mock-ddk/mock-device.h

const zx::vmo & GetInspectVmo ()

Defined at line 167 of file ../../src/devices/testing/mock-ddk/mock-device.h

fidl::UnownedClientEnd<fuchsia_io::Directory> outgoing ()

Defined at line 169 of file ../../src/devices/testing/mock-ddk/mock-device.h

void AddProtocol (uint32_t id, const void * ops, void * ctx, const char * name)

device_get_protocol is usually called by child devices to get their parent protocols.

You can add protocols here to your device or your parent device.

if you want to add a protocol to a fragment, add the fragment's name as 'name'.

Defined at line 188 of file ../../src/devices/testing/mock-ddk/mock-device.cc

template <typename Protocol>
void AddNsProtocol (mock_ddk::TypedHandler<Protocol> handler, const char * protocol_name)

Add a protocol that is served by the namespace.

This is similar to AddProtocol, but the protocol is served by the namespace

instead of the device.

Defined at line 188 of file ../../src/devices/testing/mock-ddk/mock-device.h

void AddNsProtocol (const char * protocol_name, mock_ddk::AnyHandler callback)

Add a protocol that is served by the namespace.

This is similar to AddProtocol, but the protocol is served by the namespace

instead of the device.

Defined at line 192 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void AddFidlService (const char * service_name, fidl::ClientEnd<fuchsia_io::Directory> ns, const char * name)

You can add FIDL service here to your device or your parent device.

if you want to add a service to a fragment, add the fragment's name as 'name'.

Devices will use `device_connect_fragment_fidl_protocol` to connect to these protocols

Defined at line 196 of file ../../src/devices/testing/mock-ddk/mock-device.cc

bool IsRootParent ()

This struct can also be a root parent device, with reduced functionality.

This allows the parent to store protocols that can be accessed by a child device.

If IsRootParent returns true, only the following calls may target this device:

device_get_protocol

device_get_metadata

These calls will be able to target this type of device soon:

device_get_fragment

Defined at line 216 of file ../../src/devices/testing/mock-ddk/mock-device.h

const char * name ()

Defined at line 217 of file ../../src/devices/testing/mock-ddk/mock-device.h

size_t child_count ()

Defined at line 219 of file ../../src/devices/testing/mock-ddk/mock-device.h

std::list<std::shared_ptr<zx_device>> & children ()

Defined at line 224 of file ../../src/devices/testing/mock-ddk/mock-device.h

template <class DeviceType>
DeviceType * GetDeviceContext ()

Access the Device class that created this MockDevice (for example, devices which

inherit from ddk::Device create a MockDevice when they call DdkAdd.)

Defined at line 232 of file ../../src/devices/testing/mock-ddk/mock-device.h

size_t descendant_count ()

Count all the descendants of this device.

Defined at line 293 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void SetFirmware (std::vector<uint8_t> firmware, std::string_view path)

load_firmware support

First, set firmware by calling SetFirmware on a device.

Then, when the device calls load_firmware, a vmo will be created with the

firmware that was set.

SetFirmware can store any number of path:firmware pairs.

If path is empty, the firmware is stored in a way that will match any path

that has not also been provided to SetFirmware to store another firmware blob.

Defined at line 203 of file ../../src/devices/testing/mock-ddk/mock-device.cc

void SetFirmware (std::string firmware, std::string_view path)

Convenience version which takes string:

Defined at line 207 of file ../../src/devices/testing/mock-ddk/mock-device.cc

Friends

zx_status_t zx_device (zx_device_t * device, zx_handle_t * config_vmo)
zx_status_t zx_device (zx_device_t * device, uint32_t type, void * buf, size_t buflen, size_t * actual)
zx_status_t zx_device (zx_device_t * device, const char * protocol_name, zx_handle_t request)
zx_status_t zx_device (zx_device_t * device, const char * fragment_name, const char * service_name, const char * protocol_name, fdf_handle_t request)
zx_status_t zx_device (zx_device_t * device, const char * fragment_name, const char * service_name, const char * protocol_name, zx_handle_t request)
zx_status_t zx_device (zx_device_t * device, const char * fragment_name, uint32_t proto_id, void * protocol)
zx_status_t zx_device (const zx_device_t * device, uint32_t proto_id, void * protocol)
zx_status_t zx_device (zx_driver_t * drv, zx_device_t * device, const char * path, zx_handle_t * fw, size_t * size)
zx_status_t zx_device (zx_driver_t * drvzx_device_t * parentdevice_add_args_t * argszx_device_t ** out)