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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::types::*;
#[derive(Debug, Copy, Clone)]
pub enum PowerState {
On,
BiosPost,
Always,
ChassisOn,
}
impl PowerState {
pub fn active_now(&self) -> bool {
#[cfg(feature = "hostpower")] {
let host = host_state::HOST_STATE.lock().unwrap();
match self {
Self::On => host.power_on,
Self::BiosPost => host.power_on && host.post_complete,
Self::Always => true,
Self::ChassisOn => host.chassis_on,
}
}
#[cfg(not(feature = "hostpower"))] {
matches!(self, Self::Always)
}
}
}
impl TryFrom<&String> for PowerState {
type Error = Box<dyn std::error::Error>;
fn try_from(s: &String) -> ErrResult<Self> {
match s.as_ref() {
"Always" => Ok(Self::Always),
"On" => Ok(Self::On),
"BiosPost" => Ok(Self::BiosPost),
"ChassisOn" => Ok(Self::ChassisOn),
_ => Err(err_invalid_data("PowerState must be \"Always\", \"On\", \
\"BiosPost\", or \"ChassisOn\"")),
}
}
}
#[cfg(feature = "hostpower")]
pub mod host_state {
use super::*;
use std::{
ops::DerefMut,
sync::Mutex as SyncMutex,
};
use dbus::{
message::MatchRule,
nonblock,
nonblock::stdintf::org_freedesktop_dbus::Properties,
};
pub struct HostState {
pub chassis_on: bool,
pub power_on: bool,
pub post_complete: bool,
}
pub(super) static HOST_STATE: SyncMutex<HostState> = SyncMutex::new(HostState {
chassis_on: false,
power_on: false,
post_complete: false,
});
pub struct DBusPowerStateProperty {
pub busname: &'static str,
pub path: &'static str,
pub interface: &'static str,
pub property: &'static str,
pub power_state: PowerState,
pub is_active: fn(s: &str) -> bool,
pub update: fn(st: &mut HostState, new: bool),
}
impl DBusPowerStateProperty {
pub async fn get(&self, bus: &nonblock::SyncConnection) -> ErrResult<bool> {
let p = nonblock::Proxy::new(self.busname, self.path,
std::time::Duration::from_secs(30), bus);
p.get::<String>(self.interface, self.property).await
.map(|s| (self.is_active)(&s))
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
}
}
mod dbus_properties {
use super::{PowerState, DBusPowerStateProperty};
pub const CHASSIS: DBusPowerStateProperty = DBusPowerStateProperty {
busname: "xyz.openbmc_project.State.Chassis",
path: "/xyz/openbmc_project/state/chassis0",
interface: "xyz.openbmc_project.State.Chassis",
property: "CurrentPowerState",
power_state: PowerState::ChassisOn,
is_active: |s| s.ends_with("On"),
update: |s, n| { s.chassis_on = n; }
};
pub const POWER: DBusPowerStateProperty = DBusPowerStateProperty {
busname: "xyz.openbmc_project.State.Host",
path: "/xyz/openbmc_project/state/host0",
interface: "xyz.openbmc_project.State.Host",
property: "CurrentHostState",
power_state: PowerState::On,
is_active: |s| s.ends_with(".Running"),
update: |s, n| { s.power_on = n; }
};
const OS_STATUS_INACTIVE: &str =
"xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive";
pub const POST: DBusPowerStateProperty = DBusPowerStateProperty {
busname: "xyz.openbmc_project.State.OperatingSystem",
path: "/xyz/openbmc_project/state/os",
interface: "xyz.openbmc_project.State.OperatingSystem.Status",
property: "OperatingSystemState",
power_state: PowerState::BiosPost,
is_active: |s| s != "Inactive" && s != OS_STATUS_INACTIVE,
update: |s, n| { s.post_complete = n; }
};
}
pub async fn init_host_state(bus: &nonblock::SyncConnection) {
use dbus_properties::*;
let chassis = CHASSIS.get(bus);
let power = POWER.get(bus);
let post = POST.get(bus);
let retrieve = |res: ErrResult<bool>, name| {
res.unwrap_or_else(|e| {
eprintln!("Failed to retrieve {} status from dbus: {}", name, e);
false })
};
let chassis = retrieve(chassis.await, "chassis");
let power = retrieve(power.await, "host power");
let post = retrieve(post.await, "POST");
let mut state = HOST_STATE.lock().unwrap();
let state = state.deref_mut();
state.chassis_on = chassis;
state.power_on = power;
state.post_complete = post;
}
pub async fn register_power_signal_handler<F, R>(bus: &nonblock::SyncConnection, cb: F)
-> ErrResult<Vec<nonblock::MsgMatch>>
where F: FnOnce(PowerState, bool) -> R + Send + Copy + Sync + 'static,
R: futures::Future<Output = ()> + Send
{
use dbus_properties::*;
use dbus::message::SignalArgs;
use nonblock::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged as PPC;
use futures::StreamExt;
let mut signals = vec![];
for prop in &[CHASSIS, POWER, POST] {
let rule = MatchRule::new_signal(PPC::INTERFACE, PPC::NAME)
.with_path(prop.path);
let (signal, stream) = bus.add_match(rule).await?.stream();
let handler = move |(_, (intf, props)): (_, (String, dbus::arg::PropMap))| {
async move {
if intf != prop.interface {
return; }
let Some(newstate) = dbus::arg::prop_cast::<String>(&props,
prop.property)
.map(|s| (prop.is_active)(s)) else {
return;
};
{
let mut hoststate = HOST_STATE.lock().unwrap();
let hoststate = hoststate.deref_mut();
(prop.update)(hoststate, newstate);
}
tokio::spawn(async move {
cb(prop.power_state, newstate).await
});
}
};
let stream = stream.for_each(handler);
signals.push(signal);
tokio::spawn(async { stream.await });
}
Ok(signals)
}
}