vmbus_serial_host/
resolver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Resolver for vmbus serial devices.

use crate::Port;
use crate::Serial;
use async_trait::async_trait;
use serial_core::resources::ResolveSerialBackendParams;
use vm_resource::AsyncResolveResource;
use vm_resource::ResourceResolver;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::VmbusDeviceHandleKind;
use vmbus_channel::resources::ResolveVmbusDeviceHandleParams;
use vmbus_channel::resources::ResolvedVmbusDevice;
use vmbus_channel::simple::SimpleDeviceWrapper;
use vmbus_serial_resources::VmbusSerialDeviceHandle;
use vmbus_serial_resources::VmbusSerialPort;

/// Resolver for [`VmbusSerialDeviceHandle`].
pub struct VmbusSerialDeviceResolver;

declare_static_async_resolver!(
    VmbusSerialDeviceResolver,
    (VmbusDeviceHandleKind, VmbusSerialDeviceHandle)
);

#[async_trait]
impl AsyncResolveResource<VmbusDeviceHandleKind, VmbusSerialDeviceHandle>
    for VmbusSerialDeviceResolver
{
    type Output = ResolvedVmbusDevice;
    type Error = anyhow::Error;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: VmbusSerialDeviceHandle,
        input: ResolveVmbusDeviceHandleParams<'_>,
    ) -> Result<Self::Output, Self::Error> {
        let driver = input.driver_source.simple();
        let port = match resource.port {
            VmbusSerialPort::Com1 => Port::Com1,
            VmbusSerialPort::Com2 => Port::Com2,
        };
        let io = resolver
            .resolve(
                resource.backend,
                ResolveSerialBackendParams {
                    driver: Box::new(driver.clone()),
                    _async_trait_workaround: &(),
                },
            )
            .await?
            .0
            .into_io();

        Ok(SimpleDeviceWrapper::new(driver, Serial::new(port, io)).into())
    }
}