| // 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. |
| |
| #include "input_sample.h" |
| |
| #include <lib/async/cpp/task.h> |
| |
| #include "driver_compat.h" |
| |
| namespace { |
| |
| constexpr uint8_t kMouseButtonCount = 3; |
| |
| // FIDL server implementation for the `fuchsia.input.report/InputDevice` protocol |
| class InputSampleServer : public fidl::WireServer<fuchsia_input_report::InputDevice> { |
| public: |
| explicit InputSampleServer( |
| driver::Namespace& ns, async_dispatcher_t* dispatcher, |
| std::weak_ptr<input_report_reader::InputReportReaderManager<input_sample::SampleInputReport>> |
| input_report_readers) |
| : dispatcher_(dispatcher), input_report_readers_(input_report_readers) { |
| auto logger_result = driver::Logger::Create(ns, dispatcher, "input-sample-server"); |
| ZX_ASSERT(logger_result.is_ok()); |
| logger_ = std::move(*logger_result); |
| } |
| |
| // Handle incoming connection requests from FIDL clients |
| static fidl::ServerBindingRef<fuchsia_input_report::InputDevice> BindDeviceClient( |
| driver::Namespace& ns, async_dispatcher_t* dispatcher, |
| std::weak_ptr<input_report_reader::InputReportReaderManager<input_sample::SampleInputReport>> |
| input_report_readers, |
| fidl::ServerEnd<fuchsia_input_report::InputDevice> request) { |
| // Bind each connection request to a unique FIDL server instance |
| auto server_impl = std::make_unique<InputSampleServer>(ns, dispatcher, input_report_readers); |
| return fidl::BindServer(dispatcher, std::move(request), std::move(server_impl), |
| std::mem_fn(&InputSampleServer::OnUnbound)); |
| } |
| |
| // This method is called when a server connection is torn down. |
| void OnUnbound(fidl::UnbindInfo info, |
| fidl::ServerEnd<fuchsia_input_report::InputDevice> server_end) { |
| if (info.is_peer_closed()) { |
| FDF_LOG(DEBUG, "Client disconnected"); |
| } else if (!info.is_user_initiated()) { |
| FDF_LOG(ERROR, "Client connection unbound: %s", info.status_string()); |
| } |
| } |
| |
| void GetInputReportsReader(GetInputReportsReaderRequestView request, |
| GetInputReportsReaderCompleter::Sync& completer) override; |
| void GetDescriptor(GetDescriptorCompleter::Sync& completer) override; |
| |
| void SendOutputReport(SendOutputReportRequestView request, |
| SendOutputReportCompleter::Sync& completer) override { |
| completer.ReplyError(ZX_ERR_NOT_SUPPORTED); |
| } |
| void GetFeatureReport(GetFeatureReportCompleter::Sync& completer) override { |
| completer.ReplyError(ZX_ERR_NOT_SUPPORTED); |
| } |
| void SetFeatureReport(SetFeatureReportRequestView request, |
| SetFeatureReportCompleter::Sync& completer) override { |
| completer.ReplyError(ZX_ERR_NOT_SUPPORTED); |
| } |
| void GetInputReport(GetInputReportRequestView request, |
| GetInputReportCompleter::Sync& completer) override { |
| completer.ReplyError(ZX_ERR_NOT_SUPPORTED); |
| } |
| |
| private: |
| driver::Logger logger_; |
| async_dispatcher_t* const dispatcher_; |
| std::weak_ptr<input_report_reader::InputReportReaderManager<input_sample::SampleInputReport>> |
| input_report_readers_; |
| }; |
| |
| // Protocol method for `fuchsia.input.report/InputDevice` to begin serving input reports |
| void InputSampleServer::GetInputReportsReader(GetInputReportsReaderRequestView request, |
| GetInputReportsReaderCompleter::Sync& completer) { |
| auto reader_manager = input_report_readers_.lock(); |
| if (!reader_manager) { |
| FDF_LOG(ERROR, "Unable to access InputReport reader manager."); |
| request->reader.Close(ZX_ERR_BAD_STATE); |
| return; |
| } |
| |
| zx_status_t status = reader_manager->CreateReader(dispatcher_, std::move(request->reader)); |
| if (status != ZX_OK) { |
| FDF_LOG(ERROR, "Could not create input report reader: %d", status); |
| request->reader.Close(ZX_ERR_BAD_STATE); |
| } |
| } |
| |
| // Protocol method for `fuchsia.input.report/InputDevice` to return the device descriptor |
| void InputSampleServer::GetDescriptor(GetDescriptorCompleter::Sync& completer) { |
| fidl::Arena allocator; |
| auto descriptor = fuchsia_input_report::wire::DeviceDescriptor::Builder(allocator); |
| |
| fuchsia_input_report::wire::DeviceInfo device_info; |
| // We pick random VID/PIDs here, but these should be taken from the HID device, |
| // or defined in fuchsia.input.report/device_ids.fidl |
| device_info.vendor_id = 0x12345678; |
| device_info.product_id = 0x87654321; |
| |
| fidl::VectorView<uint8_t> buttons(allocator, kMouseButtonCount); |
| buttons[0] = 0x01; |
| buttons[1] = 0x02; |
| buttons[2] = 0x03; |
| |
| constexpr fuchsia_input_report::wire::Axis movement_x{ |
| .range = {.min = -127, .max = 127}, |
| .unit = {.type = fuchsia_input_report::wire::UnitType::kNone, .exponent = 0}, |
| }; |
| constexpr fuchsia_input_report::wire::Axis movement_y{ |
| .range = {.min = -127, .max = 127}, |
| .unit = {.type = fuchsia_input_report::wire::UnitType::kNone, .exponent = 0}, |
| }; |
| |
| auto mouse_in_desc = fuchsia_input_report::wire::MouseInputDescriptor::Builder(allocator); |
| mouse_in_desc.buttons(buttons); |
| mouse_in_desc.movement_x(movement_x); |
| mouse_in_desc.movement_y(movement_y); |
| |
| auto mouse_descriptor = fuchsia_input_report::wire::MouseDescriptor::Builder(allocator); |
| mouse_descriptor.input(mouse_in_desc.Build()); |
| descriptor.mouse(mouse_descriptor.Build()); |
| descriptor.device_info(device_info); |
| |
| completer.Reply(descriptor.Build()); |
| } |
| |
| } // namespace |
| |
| namespace input_sample { |
| |
| // static |
| zx::status<std::unique_ptr<InputSampleDriver>> InputSampleDriver::Start( |
| fuchsia_driver_framework::wire::DriverStartArgs& start_args, fdf::UnownedDispatcher dispatcher, |
| fidl::WireSharedClient<fuchsia_driver_framework::Node> node, driver::Namespace ns, |
| driver::Logger logger) { |
| auto driver = std::make_unique<InputSampleDriver>(dispatcher->async_dispatcher(), std::move(node), |
| std::move(ns), std::move(logger)); |
| auto result = driver->Run(std::move(start_args.outgoing_dir())); |
| if (result.is_error()) { |
| return result.take_error(); |
| } |
| return zx::ok(std::move(driver)); |
| } |
| |
| zx::status<> InputSampleDriver::Run(fidl::ServerEnd<fuchsia_io::Directory> outgoing_dir) { |
| // Connect to the parent device node. |
| auto parent = input_driver_compat::ConnectToParentDevice(&ns_, "default"); |
| if (parent.status_value() != ZX_OK) { |
| FDF_SLOG(ERROR, "Failed to connect to parent", KV("status", parent.status_string())); |
| return parent.take_error(); |
| } |
| |
| // Add the fuchsia.input.report/InputDevice protocol to be served as "/svc/input-sample" |
| auto result = outgoing_.AddProtocol<fuchsia_input_report::InputDevice>( |
| [this](fidl::ServerEnd<fuchsia_input_report::InputDevice> request) { |
| InputSampleServer::BindDeviceClient(ns_, dispatcher_, input_report_readers_, |
| std::move(request)); |
| }, |
| Name()); |
| if (result.status_value() != ZX_OK) { |
| return result; |
| } |
| |
| auto service_dir = fidl::StringView::FromExternal(std::string("svc/").append(Name())); |
| |
| // Construct a devfs path that matches the device nodes topological path |
| auto path_result = fidl::WireCall(*parent)->GetTopologicalPath(); |
| if (!path_result.ok()) { |
| FDF_SLOG(ERROR, "Failed to get topological path", KV("status", path_result.status_string())); |
| return zx::error(path_result.status()); |
| } |
| std::string parent_path(path_result->path.data(), path_result->path.size()); |
| auto devfs_path = fidl::StringView::FromExternal(parent_path.append("/").append(Name())); |
| |
| // Export an entry to devfs for fuchsia.input.report as an input device class |
| auto devfs_dir = |
| input_driver_compat::ExportDevfsEntry(&ns_, service_dir, devfs_path, /* INPUT_REPORT */ 26); |
| if (devfs_dir.is_error()) { |
| FDF_SLOG(ERROR, "Failed to export service", KV("status", devfs_dir.status_string())); |
| return devfs_dir.take_error(); |
| } |
| |
| // Serve the driver's FIDL protocol to clients. |
| auto status = outgoing_.Serve(std::move(devfs_dir.value())); |
| if (status.is_error()) { |
| FDF_SLOG(ERROR, "Failed to serve devfs directory", KV("status", status.status_string())); |
| return status.take_error(); |
| } |
| status = outgoing_.Serve(std::move(outgoing_dir)); |
| if (status.is_error()) { |
| FDF_SLOG(ERROR, "Failed to serve outgoing directory", KV("status", status.status_string())); |
| return status.take_error(); |
| } |
| |
| // Start sending fake reports |
| input_report_readers_ = |
| std::make_shared<input_report_reader::InputReportReaderManager<SampleInputReport>>(); |
| SendFakeReport(); |
| |
| return zx::ok(); |
| } |
| |
| // Convert local data structure to a `fuchsia.input.report/InputReport` format. |
| void SampleInputReport::ToFidlInputReport( |
| fidl::WireTableBuilder<::fuchsia_input_report::wire::InputReport>& input_report, |
| fidl::AnyArena& arena) { |
| std::vector<uint8_t> pressed_buttons; |
| for (uint8_t i = 0; i < kMouseButtonCount; i++) { |
| if (buttons & (1 << i)) { |
| pressed_buttons.push_back(i + 1); |
| } |
| } |
| fidl::VectorView<uint8_t> buttons_vec(arena, pressed_buttons.size()); |
| size_t idx = 0; |
| for (const auto& button : pressed_buttons) { |
| buttons_vec[idx++] = button; |
| } |
| |
| auto mouse_input_rpt = fuchsia_input_report::wire::MouseInputReport::Builder(arena); |
| mouse_input_rpt.pressed_buttons(buttons_vec); |
| mouse_input_rpt.movement_x(rel_x); |
| mouse_input_rpt.movement_y(rel_y); |
| |
| input_report.mouse(mouse_input_rpt.Build()); |
| input_report.event_time(event_time.get()); |
| } |
| |
| // Periodically send a new input report with fake data. |
| void InputSampleDriver::SendFakeReport() { |
| // Update fake mouse report |
| report_.event_time = zx::time(zx_clock_get_monotonic()); |
| report_.buttons++; |
| report_.rel_x++; |
| report_.rel_y++; |
| |
| // Send fake mouse report |
| input_report_readers_->SendReportToAllReaders(report_); |
| |
| // Run again in 1 second |
| async::PostDelayedTask( |
| dispatcher_, [this]() mutable { SendFakeReport(); }, zx::sec(1)); |
| } |
| |
| } // namespace input_sample |
| |
| FUCHSIA_DRIVER_RECORD_CPP_V1(input_sample::InputSampleDriver); |