| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef FUCHSIA_SDK_EXAMPLES_SRC_I2C_TEMPERATURE_TESTING_TEST_I2C_CONTROLLER_H_ |
| #define FUCHSIA_SDK_EXAMPLES_SRC_I2C_TEMPERATURE_TESTING_TEST_I2C_CONTROLLER_H_ |
| |
| #include <fidl/fuchsia.driver.framework/cpp/wire.h> |
| #include <fidl/fuchsia.hardware.i2c/cpp/wire.h> |
| #include <lib/async/cpp/executor.h> |
| #include <lib/driver2/namespace.h> |
| #include <lib/driver2/structured_logger.h> |
| #include <lib/fdf/cpp/dispatcher.h> |
| #include <lib/sys/component/llcpp/outgoing_directory.h> |
| |
| namespace test_i2c_controller { |
| |
| // Test driver that interacts with the i2c temperature driver. It implements the I2c protocol |
| // and emulates a behavior where it receives commands for a temperature and returns a fake |
| // temperature. The temperature it returns increase by 5 each time it receives a measurement |
| // command. When it receives a reset command, the temperature is reset back to the starting |
| // value. |
| class TestI2cController : public fidl::WireServer<fuchsia_hardware_i2c::Device> { |
| public: |
| TestI2cController(async_dispatcher_t* dispatcher, |
| fidl::WireSharedClient<fuchsia_driver_framework::Node> node, |
| driver::Namespace ns, driver::Logger logger) |
| : dispatcher_(dispatcher), |
| outgoing_(component::OutgoingDirectory::Create(dispatcher)), |
| ns_(std::move(ns)), |
| logger_(std::move(logger)), |
| node_(std::move(node)) {} |
| |
| virtual ~TestI2cController() = default; |
| |
| static constexpr const char* Name() { return "test-i2c-controller"; } |
| |
| static zx::status<std::unique_ptr<TestI2cController>> Start( |
| fuchsia_driver_framework::wire::DriverStartArgs& start_args, |
| fdf::UnownedDispatcher dispatcher, |
| fidl::WireSharedClient<fuchsia_driver_framework::Node> node, driver::Namespace ns, |
| driver::Logger logger); |
| |
| void Transfer(TransferRequestView request, TransferCompleter::Sync& completer); |
| |
| private: |
| zx::status<> Run(fidl::ServerEnd<fuchsia_io::Directory> outgoing_dir); |
| zx::status<> AddChild(); |
| |
| void HandleWrite(const fidl::VectorView<uint8_t> write_data); |
| |
| uint32_t temperature_; |
| |
| async_dispatcher_t* const dispatcher_; |
| |
| component::OutgoingDirectory outgoing_; |
| driver::Namespace ns_; |
| driver::Logger logger_; |
| |
| fidl::WireSharedClient<fuchsia_driver_framework::Node> node_; |
| fidl::WireSharedClient<fuchsia_driver_framework::NodeController> controller_; |
| }; |
| |
| } // namespace test_i2c_controller |
| |
| #endif // FUCHSIA_SDK_EXAMPLES_SRC_I2C_TEMPERATURE_TESTING_TEST_I2C_CONTROLLER_H_ |