blob: c1efd9f5e4dbc69d7f31744d7e1fa6e1d536cac7 [file] [log] [blame]
// 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 "src/qemu_edu/qemu_edu.h"
#include <fidl/fuchsia.driver.compat/cpp/wire.h>
namespace fdf {
using namespace fuchsia_driver_framework;
} // namespace fdf
namespace qemu_edu {
namespace {
namespace fio = fuchsia_io;
zx::status<fidl::ClientEnd<fuchsia_driver_compat::Device>>
ConnectToParentDevice(const driver::Namespace* ns, std::string_view name) {
auto result = ns->OpenService<fuchsia_driver_compat::Service>(name);
if (result.is_error()) {
return result.take_error();
}
return result.value().connect_device();
}
} // namespace
// static
zx::status<std::unique_ptr<QemuEduDriver>> QemuEduDriver::Start(
fdf::wire::DriverStartArgs& start_args, fdf::UnownedDispatcher dispatcher,
fidl::WireSharedClient<fdf::Node> node, driver::Namespace ns,
driver::Logger logger) {
auto driver = std::make_unique<QemuEduDriver>(
dispatcher->async_dispatcher(), std::move(node), std::move(ns), std::move(logger));
auto result = driver->Run(dispatcher->async_dispatcher(), std::move(start_args.outgoing_dir()));
if (result.is_error()) {
return result.take_error();
}
return zx::ok(std::move(driver));
}
zx::status<> QemuEduDriver::Run(async_dispatcher* dispatcher,
fidl::ServerEnd<fio::Directory> outgoing_dir) {
// Connect to DevfsExporter.
auto exporter = ns_.Connect<fuchsia_device_fs::Exporter>();
if (exporter.status_value() != ZX_OK) {
return exporter.take_error();
}
exporter_ = fidl::WireClient(std::move(*exporter), dispatcher);
// Connect to parent.
auto parent = 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();
}
auto result = fidl::WireCall(*parent)->GetTopologicalPath();
if (!result.ok()) {
FDF_SLOG(ERROR, "Failed to get topological path",
KV("status", result.status_string()));
return zx::error(result.status());
}
std::string path(result->path.data(), result->path.size());
auto status =
outgoing_.AddProtocol<fuchsia_hardware_qemuedu::Device>(this, Name());
if (status.status_value() != ZX_OK) {
FDF_SLOG(ERROR, "Failed to add protocol",
KV("status", status.status_string()));
return status;
}
// Serve a connection to outgoing.
auto endpoints = fidl::CreateEndpoints<fuchsia_io::Directory>();
if (endpoints.is_error()) {
FDF_SLOG(ERROR, "Failed to create endpoints",
KV("status", endpoints.status_string()));
return endpoints.take_error();
}
{
auto status = outgoing_.Serve(std::move(endpoints->server));
if (status.is_error()) {
FDF_SLOG(ERROR, "Failed to serve outgoing directory",
KV("status", status.status_string()));
return status.take_error();
}
}
exporter_
->Export(
std::move(endpoints->client),
fidl::StringView::FromExternal(std::string("svc/").append(Name())),
fidl::StringView::FromExternal(path.append("/").append(Name())), 0)
.Then([this](fidl::WireUnownedResult<fuchsia_device_fs::Exporter::Export>&
result) {
if (!result.ok()) {
FDF_SLOG(ERROR, "Failed to export",
KV("status", result.status_string()));
}
});
return outgoing_.Serve(std::move(outgoing_dir));
}
void QemuEduDriver::ComputeFactorial(
ComputeFactorialRequestView request,
ComputeFactorialCompleter::Sync& completer) {
uint32_t factorial = 1;
for (uint32_t i = 1; i < request->input; i++) {
factorial *= i;
}
FDF_SLOG(INFO, "Replying with", KV("factorial", std::to_string(factorial).c_str()));
completer.Reply(factorial);
}
void QemuEduDriver::LivenessCheck(LivenessCheckRequestView request,
LivenessCheckCompleter::Sync& completer) {
completer.Reply(true);
}
} // namespace qemu_edu
FUCHSIA_DRIVER_RECORD_CPP_V1(qemu_edu::QemuEduDriver);