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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Backend providing support for PMBus and I2C-based hwmon sensors.
//!
//! Combines the functionality of dbus-sensors's `psusensor` and
//! `hwmontempsensor` daemons.

use std::{
	collections::{HashMap, HashSet},
	time::Duration,
};
use dbus::arg::RefArg;

use crate::{
	DaemonState,
	types::*,
	i2c::{
		I2CDeviceParams,
		I2CDeviceType,
		get_device_type,
		get_i2cdev,
	},
	powerstate::PowerState,
	sensor,
	sensor::{
		Sensor,
		SensorConfig,
		SensorIOCtx,
		SensorMode::ReadOnly,
		SensorType,
	},
	sysfs,
	threshold,
	threshold::ThresholdConfig,
	dbus_helpers::props::*,
};

/// Internal representation of dbus config data for an I2C/PMBus hwmon sensor.
#[derive(Debug)]
pub struct HwmonSensorConfig {
	/// The sensor names used for the channels of this sensor.
	names: Vec<String>,
	/// Alternate names provided by `"<foo>_Name"` config keys.
	name_overrides: HashMap<String, String>,
	/// I2C parameters for the sensor.
	i2c: I2CDeviceParams,
	/// Polling interval for the sensor.
	poll_interval: Duration,
	/// Host power state in which this sensor is active.
	power_state: PowerState,
	/// Threshold settings for the sensor.
	thresholds: Vec<ThresholdConfig>,
	/// Which channel labels are enabled (i.e. should have a sensor created for them).
	enabled_labels: FilterSet<String>,
}

impl HwmonSensorConfig {
	/// Construct a [`HwmonSensorConfig`] 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 mut name_overrides: HashMap<String, String> = HashMap::new();
		let r#type = prop_get_mandatory::<String>(basecfg, "Type")?.clone();
		let poll_sec: u64 = *prop_get_default(basecfg, "PollRate", &1u64)?;
		let poll_interval = Duration::from_secs(poll_sec);
		let power_state = prop_get_default_from(basecfg, "PowerState", PowerState::Always)?;
		let mut names = vec![name.clone()];
		for i in 1.. {
			let key = format!("Name{}", i);
			if let Some(s) = prop_get_optional::<String>(basecfg, &key)? {
				names.push(s.clone());
			} else {
				break;
			}
		}
		let Some(devtype) = get_device_type(&r#type) else {
			return Err(err_unsupported(format!("unsupported device type '{}'", r#type)));
		};
		let i2c = I2CDeviceParams::from_dbus(basecfg, devtype)?;
		let enabled_labels: FilterSet<String> = prop_get_optional(basecfg, "Labels")?
			.map(|v: &Vec<_>| HashSet::from_iter(v.iter().cloned()))
			.into();
		let thresholds = threshold::get_configs_from_dbus(baseintf, intfs);

		for (key, value) in basecfg {
			if let Some(lbl) = key.strip_suffix("_Name") {
				if let Some(s) = value.as_str() {
					name_overrides.insert(lbl.into(), s.into());
				} else {
					eprintln!("{}: {} value not string, ignored", name, key);
				}
			}
		}

		Ok(Self {
			names,
			name_overrides,
			i2c,
			poll_interval,
			power_state,
			thresholds,
			enabled_labels,
		})
	}

	/// Determine the sensor name to use for a given `idx` and `label`.
	fn sensor_name(&self, idx: usize, label: &str) -> Option<String> {
		// PSU-style configs and hwmon-style configs use
		// different naming schemes
		match self.i2c.devtype {
			I2CDeviceType::PMBus(_) => {
				let subname: &str = self.name_overrides.get(label)
					.map(|s| s.as_str())
					.unwrap_or_else(|| name_for_label(label));

				Some(format!("{} {}", self.names[0], subname))
			},
			I2CDeviceType::Hwmon(_) => self.names.get(idx).map(|s| s.into()),
		}
	}
}

/// Determine the sensor name (component) corresponding to a given hwmon label tag.
fn name_for_label(label: &str) -> &str {
	let tag = label.trim_end_matches(|c: char| c.is_ascii_digit());
	match tag {
		"pin"          => "Input Power",
		"pout"|"power" => "Output Power",
		"maxpin"       => "Max Input Power",
		"vin"          => "Input Voltage",
		"maxvin"       => "Max Input Voltage",
		"vout"|"in"    => "Output Voltage",
		"vmon"         => "Auxiliary Input Voltage",
		"iin"          => "Input Current",
		"iout"|"curr"  => "Output Current",
		"maxiout"      => "Max Output Current",
		"temp"         => "Temperature",
		"maxtemp"      => "Max Temperature",

		// PSUSensorMain.cpp in dbus-sensors uses hard-coded
		// numeric suffixes for these (unlike all the others)
		// -- I'm not sure why or if it matters, but we'll
		// replicate it for now at least...
		"fan" => match label {
			"fan1" => "Fan Speed 1",
			"fan2" => "Fan Speed 2",
			"fan3" => "Fan Speed 3",
			"fan4" => "Fan Speed 4",
			_ => label,
		},

		_ => label,
	}
}

/// Instantiate any active PMBus/I2C hwmon 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::Hwmon(c) if dbuspaths.contains(path) => Some((path, c)),
				_ => None,
			}
		});
	for (path, hwmcfg) in configs {
		let mainname = &hwmcfg.names[0];

		if !hwmcfg.power_state.active_now() {
			// FIXME: log noise
			eprintln!("{}: not active, skipping...", mainname);
			continue;
		}

		let i2cdev = {
			let mut i2cdevs = daemonstate.i2cdevs.lock().await;
			match get_i2cdev(&mut i2cdevs, &hwmcfg.i2c) {
				Ok(d) => d,
				Err(e) => {
					eprintln!("{}: i2c device instantiation failed, skipping: {}",
					          mainname, e);
					continue;
				},
			}
		};

		let prefix = match hwmcfg.i2c.devtype {
			I2CDeviceType::PMBus(_) => None,
			I2CDeviceType::Hwmon(_) => Some("temp"),
		};

		let sysfs_dir = hwmcfg.i2c.sysfs_device_dir();
		let inputs = match sysfs::scan_hwmon_input_files(&sysfs_dir, prefix) {
			Ok(v) => v,
			Err(e) => {
				eprintln!("{}: error scanning {}, skipping sensor: {}", mainname,
				          sysfs_dir.display(), e);
				continue;
			},
		};

		for (idx, file) in inputs.iter().enumerate() {
			let label = match file.get_label() {
				Ok(s) => s,
				Err(e) => {
					eprintln!("{}: error finding label for {}, skipping entry: {}",
					          mainname, file.abspath.display(), e);
					continue;
				},
			};

			if !hwmcfg.enabled_labels.contains(&label) {
				continue;
			}

			let Some(sensorname) = hwmcfg.sensor_name(idx, &label) else {
				eprintln!("{}: {} does not appear to be in use, skipping",
				          mainname, label);
				continue;
			};

			let mut sensors = daemonstate.sensors.lock().await;

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

			let io = match sysfs::SysfsSensorIO::new(file).await {
				Ok(io) => sensor::SensorIO::Sysfs(io),
				Err(e) => {
					eprintln!("{}: skipping {}: {}", sensorname,
					          file.abspath.display(), e);
					continue;
				},
			};

			let (minval, maxval) = match file.kind {
				SensorType::Temperature => (-128.0, 127.0),
				SensorType::RPM => (0.0, 30000.0),
				SensorType::Voltage => (0.0, 255.0),
				// FIXME: PSUSensorMain.cpp has 20 as max for input currents
				SensorType::Current => (0.0, 255.0),
				SensorType::Power => (0.0, 3000.0),
			};

			let io = SensorIOCtx::new(io).with_i2cdev(i2cdev.clone());

			let ctor = || {
				Sensor::new(path, &sensorname, file.kind, &daemonstate.sensor_intfs,
				            &daemonstate.bus, ReadOnly)
					.with_poll_interval(hwmcfg.poll_interval)
					.with_power_state(hwmcfg.power_state)
					.with_thresholds_from(&hwmcfg.thresholds,
					                      &daemonstate.sensor_intfs.thresholds,
					                      &daemonstate.bus)
					.with_minval(minval)
					.with_maxval(maxval)
			};
			sensor::install_or_activate(entry, &daemonstate.crossroads, io,
			                            &daemonstate.sensor_intfs, ctor).await;
		}
	}

	Ok(())
}

/// Whether or not the given `cfgtype` is supported by the `hwmon` sensor backend.
pub fn match_cfgtype(cfgtype: &str) -> bool {
	get_device_type(cfgtype).is_some()
}