power_resources/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Power request resources, for powering off, restarting, and hibernating VMs.
5
6#![forbid(unsafe_code)]
7
8use mesh::payload::Protobuf;
9use std::sync::Arc;
10use vm_resource::CanResolveTo;
11use vm_resource::ResourceKind;
12
13/// Resource kind for power requests.
14pub enum PowerRequestHandleKind {}
15
16impl ResourceKind for PowerRequestHandleKind {
17    const NAME: &'static str = "power_request";
18}
19
20impl CanResolveTo<PowerRequestClient> for PowerRequestHandleKind {
21    type Input<'a> = ();
22}
23
24/// Type erased object for requesting power state changes.
25#[derive(Clone)]
26pub struct PowerRequestClient(Arc<dyn Fn(PowerRequest) + Send + Sync>);
27
28impl PowerRequestClient {
29    /// Issues a power request.
30    pub fn power_request(&self, request: PowerRequest) {
31        (self.0)(request)
32    }
33}
34
35impl<T: 'static + Fn(PowerRequest) + Send + Sync> From<T> for PowerRequestClient {
36    fn from(value: T) -> Self {
37        Self(Arc::new(value))
38    }
39}
40
41/// A VM power request.
42#[derive(Debug, Copy, Clone, PartialEq, Eq, Protobuf)]
43pub enum PowerRequest {
44    /// Power off the VM.
45    PowerOff,
46    /// Restart the VM.
47    Reset,
48    /// Hibernate the VM.
49    Hibernate,
50    /// Triple fault the VM.
51    TripleFault {
52        /// The VP that caused the triple fault.
53        vp: u32,
54    },
55}