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
//! Backend providing support for hosting externally-supplied sensor data in dbus objects.
//!
//! A la dbus-sensors's `externalsensor` daemon.

use std::{
	collections::HashMap,
	sync::Arc,
	time::Duration,
};

use tokio::sync::{
	Mutex,
	watch,
};

use crate::{
	DaemonState,
	dbus_helpers::props::*,
	powerstate::PowerState,
	sensor,
	sensor::{
		Sensor,
		SensorConfig,
		SensorMode::ReadWrite,
		SensorType,
	},
	threshold,
	threshold::ThresholdConfig,
	types::*,
};

/// Internal representation of the dbus config data for an external sensor.
#[derive(Debug)]
pub struct ExternalSensorConfig {
	/// Sensor name.
	name: String,
	/// Sensor type.
	kind: SensorType,
	/// Minimum sensor reading value.
	minvalue: f64,
	/// Maximum sensor reading value.
	maxvalue: f64,
	/// Maximum time allowed between updates until data is considered stale.
	timeout: Option<Duration>,
	/// Threshold settings for the sensor.
	thresholds: Vec<ThresholdConfig>,
	/// Host power state in which this sensor is active.
	power_state: PowerState,
}

impl ExternalSensorConfig {
	/// Construct an [`ExternalSensorConfig`] from raw dbus config data.
	pub fn from_dbus(basecfg: &dbus::arg::PropMap, baseintf: &str,
	                 intfs: &HashMap<String, dbus::arg::PropMap>) -> ErrResult<Self> {
		let name: &String = prop_get_mandatory(basecfg, "Name")?;
		let kind: &String = prop_get_mandatory(basecfg, "Units")?;
		let minvalue = *prop_get_mandatory(basecfg, "MinValue")?;
		let maxvalue = *prop_get_mandatory(basecfg, "MaxValue")?;
		let timeout = prop_get_optional(basecfg, "Timeout")?.map(|p| Duration::from_secs(*p));
		let thresholds = threshold::get_configs_from_dbus(baseintf, intfs);
		let power_state = prop_get_default_from(basecfg, "PowerState", PowerState::Always)?;

		let Some(kind) = SensorType::from_unit_str(kind) else {
			return Err(err_invalid_data(format!("invalid unit string '{}'", kind)));
		};

		Ok(Self {
			name: name.clone(),
			kind,
			minvalue,
			maxvalue,
			timeout,
			power_state,
			thresholds,
		})
	}
}

/// Core implementation of [`ExternalSensorIO`].
///
/// This simply receives update notifications sent by a callback in
/// the dbus property-set handler path.
struct ExternalSensorIOCore {
	/// Channel by which we get notified of updates.
	rx: watch::Receiver<f64>,
	/// True if the sensor hasn't received an update within its timeout window.
	stale: bool,
}

impl ExternalSensorIOCore {
	/// Construct a new [`ExternalSensorIOCore`].
	fn new(rx: watch::Receiver<f64>) -> Self {
		Self {
			rx,
			stale: true,
		}
	}

	/// Return the most recently sent value for the sensor (or NaN if it's timed out).
	fn read(&self) -> ErrResult<f64> {
		let val = if self.stale {
			f64::NAN
		} else {
			*self.rx.borrow()
		};
		Ok(val)
	}
}

/// "I/O" mechanism for external sensors.
///
/// This is simply a wrapper around [`ExternalSensorIOCore`] so that it can be shared.
pub struct ExternalSensorIO {
	core: Arc<Mutex<ExternalSensorIOCore>>,
}

impl ExternalSensorIO {
	/// Construct a new [`ExternalSensorIO`].
	fn new(rx: watch::Receiver<f64>) -> Self {
		Self { core: Arc::new(Mutex::new(ExternalSensorIOCore::new(rx))) }
	}

	/// Returns the most-recently-sent sensor reading value.
	pub async fn read(&self) -> ErrResult<f64> {
		self.core.lock().await.read()
	}
}

/// Async function that returns when the sensor's value should be updated.
async fn get_next_update(extiocore: Arc<Mutex<ExternalSensorIOCore>>, timeout: Option<Duration>)
{
	let mut extiocore = extiocore.lock().await;
	let update = extiocore.rx.changed();
	if let Some(t) = timeout {
		let res = tokio::time::timeout(t, update).await;
		extiocore.stale = res.is_err();
	} else {
		let res = update.await;
		if let Err(e) = res {
			// FIXME: would be nice to include the sensor name here
			eprintln!("BUG: failed to receive value update: {}", e);
			extiocore.stale = true;
		}
	}
}

/// Instantiate any active external sensors configured in `cfgmap`.
pub async fn instantiate_sensors(daemonstate: &DaemonState, dbuspaths: &FilterSet<InventoryPath>)
                                 -> ErrResult<()>
{
	let cfgmap = daemonstate.config.lock().await;
	let configs = cfgmap.iter()
		.filter_map(|(path, cfg)| {
			match cfg {
				SensorConfig::External(c) if dbuspaths.contains(path) => Some((path, c)),
				_ => None,
			}
		});
	for (path, extcfg) in configs {
		let mut sensors = daemonstate.sensors.lock().await;

		let Some(entry) = sensor::get_nonactive_sensor_entry(&mut sensors,
		                                                     extcfg.name.clone()).await else {
			continue;
		};

		let (tx, rx) = watch::channel(f64::NAN);
		let extio = ExternalSensorIO::new(rx);
		let extiocore = extio.core.clone();
		let timeout = extcfg.timeout;

		let io = sensor::SensorIO::External(extio);

		let ioctx = sensor::SensorIOCtx::new(io)
			.with_next_update(Box::new(move |_| {
				let extiocore = extiocore.clone();
				Box::pin(async move {
					get_next_update(extiocore, timeout).await;
				})
			}));

		let mode = ReadWrite(Box::new(move |s, v| {
			if let Err(e) = tx.send(v) {
				eprintln!("{}: failed to send value update: {}", s.name, e);
			}
		}));

		let ctor = || {
			Sensor::new(path, &extcfg.name, extcfg.kind, &daemonstate.sensor_intfs,
			            &daemonstate.bus, mode)
				.with_power_state(extcfg.power_state)
				.with_thresholds_from(&extcfg.thresholds,
				                      &daemonstate.sensor_intfs.thresholds,
				                      &daemonstate.bus)
				.with_minval(extcfg.minvalue)
				.with_maxval(extcfg.maxvalue)
		};
		sensor::install_or_activate(entry, &daemonstate.crossroads, ioctx,
		                            &daemonstate.sensor_intfs, ctor).await
	}
	Ok(())
}

/// Whether or not the given `cfgtype` is supported by the `external` sensor backend.
pub fn match_cfgtype(cfgtype: &str) -> bool {
	cfgtype == "ExternalSensor"
}