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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! Backend-independent code for implementing and managing sensors.

use std::{
	collections::HashMap,
	sync::{Arc, Mutex as SyncMutex},
	time::Duration,
};
use dbus::{
	arg::{Arg, RefArg, Append, PropMap},
	nonblock::SyncConnection,
};
use tokio::sync::Mutex;

use crate::{
	DaemonState,
	types::*,
	i2c::I2CDevice,
	gpio::BridgeGPIO,
	powerstate::PowerState,
	sysfs,
	threshold,
	threshold::{
		ThresholdArr,
		ThresholdConfig,
		ThresholdIntfDataArr,
	},
	dbus_helpers::SignalProp,
};

#[cfg(feature = "adc")]
use crate::adc;

#[cfg(feature = "hwmon")]
use crate::hwmon;

#[cfg(feature = "fan")]
use crate::fan;

#[cfg(feature = "peci")]
use crate::peci;

#[cfg(feature = "external")]
use crate::external;

/// The type of a sensor.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SensorType {
	Temperature,
	RPM,
	Voltage,
	Current,
	Power,
}

impl SensorType {
	/// Return the dbus string used to represent the unit associated with a [`SensorType`].
	fn dbus_unit_str(&self) -> &'static str {
		match self {
			Self::Temperature => "xyz.openbmc_project.Sensor.Value.Unit.DegreesC",
			Self::RPM => "xyz.openbmc_project.Sensor.Value.Unit.RPMS",
			Self::Voltage => "xyz.openbmc_project.Sensor.Value.Unit.Volts",
			Self::Current => "xyz.openbmc_project.Sensor.Value.Unit.Amperes",
			Self::Power => "xyz.openbmc_project.Sensor.Value.Unit.Watts",
		}
	}

	/// Return the sensor type indicated by the given unit string.
	pub fn from_unit_str(unit: &str) -> Option<Self> {
		match unit {
			"DegreesC" => Some(Self::Temperature),
			"RPMS" => Some(Self::RPM),
			"Volts" => Some(Self::Voltage),
			"Amperes" => Some(Self::Current),
			"Watts" => Some(Self::Power),
			_ => None,
		}
	}

	/// Return the category in the dbus sensors hierarchy for a [`SensorType`] (i.e. a
	/// dbus path component).
	pub fn dbus_category(&self) -> &'static str {
		match self {
			Self::Temperature => "temperature",
			Self::RPM => "fan_tach",
			Self::Voltage => "voltage",
			Self::Current => "current",
			Self::Power => "power",
		}
	}

	/// Return the (multiplicative) scaling factor to convert a raw hwmon reading to
	/// the corresponding natural unit.
	pub fn hwmon_scale(&self) -> f64 {
		const UNIT: f64 = 1.0;
		const MILLI: f64 = 0.001;
		const MICRO: f64 = 0.000001;
		match self {
			Self::Voltage => MILLI,
			Self::RPM => UNIT,
			Self::Temperature => MILLI,
			Self::Current => MILLI,
			Self::Power => MICRO,
		}
	}

	/// Return the sensor type indicated by the given hwmon type tag (filename prefix).
	pub fn from_hwmon_typetag(tag: &str) -> Option<Self> {
		match tag {
			"temp" => Some(Self::Temperature),
			"fan" => Some(Self::RPM),
			"in" => Some(Self::Voltage),
			"curr" => Some(Self::Current),
			"power" => Some(Self::Power),
			_ => None,
		}
	}

	/// Return the hwmon type tag (filename prefix) for the sensor type.
	pub fn hwmon_typetag(&self) -> &'static str {
		match self {
			Self::Temperature => "temp",
			Self::RPM => "fan",
			Self::Voltage => "in",
			Self::Current => "curr",
			Self::Power => "power",
		}
	}
}

/// An enum of config data all supported sensor types.
pub enum SensorConfig {
	#[cfg(feature = "hwmon")]
	Hwmon(hwmon::HwmonSensorConfig),

	#[cfg(feature = "adc")]
	ADC(adc::ADCSensorConfig),

	#[cfg(feature = "fan")]
	Fan(fan::FanSensorConfig),

	#[cfg(feature = "peci")]
	PECI(peci::PECISensorConfig),

	#[cfg(feature = "external")]
	External(external::ExternalSensorConfig),
}

impl SensorConfig {
	/// Construct a [`SensorConfig`] from dbus properties `props`
	///
	/// `props` should be the entry for `intf` in `all_intfs`.
	///
	/// Returns:
	///  * `Some(Ok(_))`: successful parse.
	///  * `Some(Err(_))`: we tried to parse a config, but something was wrong with it.
	///  * `None`: `intf` isn't a sensor configuration, no attempt at parsing.
	pub fn from_dbus(props: &PropMap, intf: &str,
	                 all_intfs: &HashMap<String, PropMap>) -> Option<ErrResult<Self>> {
		let parts: Vec<&str> = intf.split('.').collect();
		if parts.len() != 4
			|| parts[0] != "xyz"
			|| parts[1] != "openbmc_project"
			|| parts[2] != "Configuration"
		{
			return None;
		}
		let cfgtype = parts[3];

		#[cfg(feature = "adc")]
		if adc::match_cfgtype(cfgtype) {
			return Some(adc::ADCSensorConfig::from_dbus(props, intf, all_intfs)
			            .map(SensorConfig::ADC));
		}

		#[cfg(feature = "hwmon")]
		if hwmon::match_cfgtype(cfgtype) {
			return Some(hwmon::HwmonSensorConfig::from_dbus(props, intf, all_intfs)
			            .map(SensorConfig::Hwmon));
		}

		#[cfg(feature = "fan")]
		if fan::match_cfgtype(cfgtype) {
			return Some(fan::FanSensorConfig::from_dbus(props, intf, all_intfs)
			            .map(SensorConfig::Fan));
		}

		#[cfg(feature = "peci")]
		if peci::match_cfgtype(cfgtype) {
			return Some(peci::PECISensorConfig::from_dbus(props, intf, all_intfs)
			            .map(SensorConfig::PECI));
		}

		#[cfg(feature = "external")]
		if external::match_cfgtype(cfgtype) {
			return Some(external::ExternalSensorConfig::from_dbus(props, intf, all_intfs)
			            .map(SensorConfig::External));
		}

		return Some(Err(err_unsupported(format!("unsupported Configuration type '{}'",
		                                        cfgtype))));
	}
}

/// A map of all sensor configs retrieved from entity-manager.
pub type SensorConfigMap = HashMap<Arc<InventoryPath>, SensorConfig>;

/// An enum of underlying I/O mechanisms used to retrieve sensor readings.
pub enum SensorIO {
	Sysfs(sysfs::SysfsSensorIO),

	#[cfg(feature = "external")]
	External(external::ExternalSensorIO),
}

impl SensorIO {
	/// Read a sample for a sensor.
	async fn read(&mut self) -> ErrResult<f64> {
		match self {
			Self::Sysfs(x) => x.read().await,

			#[cfg(feature = "external")]
			Self::External(x) => x.read().await,
		}
	}
}

/// Type alias for an async function used to determine when a sensor's reading should next be updated.
///
/// The update will occur when the returned future resolves.
type NextUpdateFn = Box<dyn FnMut(&Sensor) -> futures::future::BoxFuture<'static, ()> + Send + Sync>;

/// A [`SensorIO`] plus some surrounding context.
pub struct SensorIOCtx {
	/// The sensor I/O mechanism.
	io: SensorIO,
	/// A GPIO that must be asserted before reading the sensor.
	bridge_gpio: Option<BridgeGPIO>,
	/// A reference to an I2C device associated with the sensor.
	i2cdev: Option<Arc<I2CDevice>>,
	/// Function returning a future to await before performing an update of the sensor's value.
	next_update: NextUpdateFn,
}

/// A running task that updates a sensor's value.
///
/// Wrapped in a struct so that we can stop it in its Drop implementation.
struct SensorIOTask(tokio::task::JoinHandle<()>);

impl Drop for SensorIOTask {
	/// Stops the contained task when the [`SensorIOTask`] goes away.
	fn drop(&mut self) {
		self.0.abort();
	}
}

impl SensorIOCtx {
	/// Create a new sensor I/O context from a given I/O mechanism.
	pub fn new(io: SensorIO) -> Self {
		Self {
			io,
			bridge_gpio: None,
			i2cdev: None,
			next_update: Box::new(|s| Box::pin(tokio::time::sleep(s.poll_interval))),
		}
	}

	/// Add a [`BridgeGPIO`] to a sensor I/O context.
	pub fn with_bridge_gpio(mut self, bridge_gpio: Option<BridgeGPIO>) -> Self {
		self.bridge_gpio = bridge_gpio;
		self
	}

	/// Add an [`I2CDevice`] to a sensor I/O context.
	pub fn with_i2cdev(mut self, i2cdev: Option<Arc<I2CDevice>>) -> Self {
		self.i2cdev = i2cdev;
		self
	}

	/// Replace the default (periodic polling) [`next_update`](Self::next_update)
	/// function of a sensor I/O context.
	pub fn with_next_update(mut self, next: NextUpdateFn) -> Self {
		self.next_update = next;
		self
	}

	/// Take a sample from the sensor, asserting its bridge GPIO if required.
	pub async fn read(&mut self) -> ErrResult<f64> {
		let _gpio_hold = match self.bridge_gpio.as_mut().map(|g| g.activate()) {
			Some(x) => Some(x.await?),
			None => None,
		};
		self.io.read().await
	}
}

/// A sensor's (dbus) read-only or read-write mode.
pub enum SensorMode {
	/// The sensor's value is read-only via dbus.
	ReadOnly,

	/// The sensor's value can be written via dbus.
	///
	/// The boxed callback will be called when a dbus write occurs.
	ReadWrite(Box<dyn Fn(&Sensor, f64) + Send>),
}

/// The top-level representation of a sensor.
pub struct Sensor {
	/// The globally-unique name of the sensor.
	pub name: String,
	/// The dbus path of the sensor object.
	dbuspath: Arc<SensorPath>,
	/// The type of the sensor.
	pub kind: SensorType,
	/// The period of the sensor polling loop.
	poll_interval: Duration,
	/// The host power state in which this sensor is enabled.
	pub power_state: PowerState,
	/// Threshold values (warning, critical, etc.) for the sensor.
	pub thresholds: ThresholdArr,

	/// A multiplicative scaling factor for readings from the sensor.
	///
	/// This is only the config-specified scaling factor (converted to a multiplier);
	/// scaling to convert sysfs hwmon values to the desired units (e.g. 0.001 to
	/// convert a sysfs millivolt value to volts) happens
	/// [elsewhere](sysfs::SysfsSensorIO::read).
	scale: f64,

	/// The number of failed update attempts since the last successful one.
	errcount: usize,

	/// The dbus property that provides the sensor's value.
	cache: SignalProp<f64>,
	/// The dbus property that provides the sensor's minimum value.
	minvalue: SignalProp<f64>,
	/// The dbus property that provides the sensor's maximum value.
	maxvalue: SignalProp<f64>,
	/// The dbus property that provides the sensor's availability state.
	available: SignalProp<bool>,
	/// The dbus property that provides the sensor's functionality state.
	functional: SignalProp<bool>,
	/// The dbus property that provides the sensor's associations.
	associations: SignalProp<Vec<(String, String, String)>>,

	/// The sensor's dbus RO/RW mode.
	mode: SensorMode,

	/// The sensor's running I/O task.
	///
	/// This is [`Some`] when the sensor is active and [`None`] when it's inactive
	/// (e.g. when the host's current power state doesn't match the sensor's
	/// `power_state`).
	iotask: Option<SensorIOTask>,
}

/// The number of consecutive update errors after which a sensor's
/// Functional attribute is set to false.
const MAX_ERRORS: usize = 5;

impl Sensor {
	/// Construct a new [`Sensor`] with the given parameters.
	///
	/// It will initially be disabled (no running I/O task); it can subsequently be
	/// enabled by a call to its [`activate()`](Sensor::activate) method.
	pub fn new(cfgpath: &InventoryPath, name: &str, kind: SensorType,
	           intfs: &SensorIntfData, conn: &Arc<SyncConnection>, mode: SensorMode) -> Self {
		let badchar = |c: char| !(c.is_ascii_alphanumeric() || c == '_');
		let cleanname = name.replace(badchar, "_");
		let dbuspath = format!("/xyz/openbmc_project/sensors/{}/{}", kind.dbus_category(),
		                       cleanname);
		let dbuspath = Arc::new(SensorPath(dbuspath.into()));
		let value_msgfn = &intfs.value_intf(&mode).msgfns.value;
		let cache = SignalProp::new(f64::NAN, value_msgfn, &dbuspath, conn);
		let minvalue = SignalProp::new(f64::NAN, &intfs.value.msgfns.minvalue,
		                               &dbuspath, conn);
		let maxvalue = SignalProp::new(f64::NAN, &intfs.value.msgfns.maxvalue,
		                               &dbuspath, conn);
		let available = SignalProp::new(false, &intfs.availability.msgfns.available,
		                                &dbuspath, conn);
		let functional = SignalProp::new(true, &intfs.opstatus.msgfns.functional,
		                                 &dbuspath, conn);

		// dbus::strings::Path doesn't have a .parent() method, so momentarily
		// pretend it's a filesystem path...
		let parentpath = match std::path::Path::new(&*cfgpath.0).parent() {
			Some(p) => p,
			None => {
				// Presumably unlikely to ever be hit, but I guess this
				// seems slightly better than just calling .unwrap()...
				eprintln!("Warning: bogus-looking config inventory path '{}', \
				           faking parent", cfgpath.0);
				std::path::Path::new("/xyz/openbmc_project/inventory/system")
			},
		};
		let assoc = ("chassis".to_string(), "all_sensors".to_string(),
		             parentpath.to_string_lossy().into());
		let associations = SignalProp::new(vec![assoc], &intfs.assoc.msgfns.associations,
		                                   &dbuspath, conn);

		Self {
			name: name.into(),
			dbuspath,
			kind,
			cache,
			minvalue,
			maxvalue,
			poll_interval: Duration::from_secs(1),
			power_state: PowerState::Always,
			thresholds: ThresholdArr::default(),
			scale: 1.0,
			errcount: 0,
			available,
			functional,
			associations,
			mode,

			iotask: None,
		}
	}

	/// Set the sensor's [`poll_interval`](Sensor::poll_interval).
	pub fn with_poll_interval(mut self, poll_interval: Duration) -> Self {
		self.poll_interval = poll_interval;
		self
	}

	/// Set the sensor's [`power_state`](Sensor::power_state).
	pub fn with_power_state(mut self, power_state: PowerState) -> Self {
		self.power_state = power_state;
		self
	}

	/// Set the sensor's [`thresholds`](Sensor::thresholds).
	///
	/// The thresholds are constructed from the provided config data, interfaces, and
	/// dbus connection.
	pub fn with_thresholds_from(mut self, cfg: &[ThresholdConfig],
	                            threshold_intfs: &ThresholdIntfDataArr,
	                            conn: &Arc<SyncConnection>) -> Self {
		self.thresholds = threshold::get_thresholds_from_configs(cfg, threshold_intfs,
		                                                         &self.dbuspath, conn);
		self
	}

	/// Set the sensor's [`scale`](Sensor::scale).
	pub fn with_scale(mut self, scale: f64) -> Self {
		self.scale = scale;
		self
	}

	/// Set the sensor's [`maxvalue`](Sensor::maxvalue).
	pub fn with_maxval(mut self, max: f64) -> Self {
		self.maxvalue.set(max);
		self
	}

	/// Set the sensor's [`minvalue`](Sensor::minvalue).
	pub fn with_minval(mut self, min: f64) -> Self {
		self.minvalue.set(min);
		self
	}

	/// Set the sensor's [`cache`](Sensor::cache)d value and update the state of its
	/// [`thresholds`](Sensor::thresholds).
	async fn set_value(&mut self, newval: f64) {
		self.cache.set(newval);

		for threshold in self.thresholds.iter_mut() {
			if let Some(t) = threshold {
				t.update(newval);
			}
		}
	}

	/// Start a sensor's update task using the provided `ioctx` and mark it available.
	///
	/// This is an associated function rather than a method on `self` because it needs
	/// to pass a weak reference to itself into the future that gets spawned as
	/// [`Sensor::iotask`], and we need an [`Arc`] for that.
	pub async fn activate(sensor: &Arc<Mutex<Sensor>>, mut ioctx: SensorIOCtx) {
		// Use a weak reference in the update task closure so it doesn't hold a
		// strong reference to the sensor (which would create a reference loop and
		// make it un-droppable)
		let weakref = Arc::downgrade(sensor);

		let update_loop = async move {
			loop {
				let Some(sensor) = weakref.upgrade() else {
					break;
				};

				// Set up the sleep here (but don't wait for it) to
				// schedule the timeout for the next update (so that the
				// interval includes the time spent acquiring the lock and
				// doing the update itself, and hence is the period of the
				// whole cyclic operation
				let next = (ioctx.next_update)(&*sensor.lock().await);

				// Stringify the error message here, because 'dyn Error'
				// isn't Send, so we can't hold the ErrResult across an
				// await.
				let readresult = ioctx.read().await.map_err(|e| e.to_string());

				let mut sensor = sensor.lock().await;

				if sensor.iotask.is_none() {
					eprintln!("BUG: update task running on inactive sensor {}",
					          sensor.name);
					break;
				}

				match readresult {
					Ok(v) => {
						let newval = v * sensor.scale;
						sensor.set_value(newval).await;
						sensor.errcount = 0;
						sensor.functional.set(true);
					},
					Err(e) => {
						if sensor.functional.get() {
							eprintln!("{}: update failed: {}",
							          sensor.name, e);
						}
						sensor.errcount += 1;
						if sensor.errcount == MAX_ERRORS {
							eprintln!("{}: error limit exceeded",
							          sensor.name);
							sensor.functional.set(false);
							sensor.set_value(f64::NAN).await;
						}
					},
				};

				drop(sensor); // Release the lock while we await the next update

				// Wait until it's time for the next update.  Do this
				// after the read instead of before so the first read
				// happens promptly (so we avoid a long wait before the
				// first sample for sensors with large poll intervals).
				next.await;
			}
		};

		// Acquire the lock before spawning the iotask so that its iotask.is_none()
		// check doesn't fail because we haven't set it yet.
		let mut sensor = sensor.lock().await;

		let iotask = SensorIOTask(tokio::spawn(update_loop));

		// It'd be nice to find some way to arrange things such that this error
		// case (and the mirror one in deactivate()) vanished by construction, but
		// I haven't been able to do so thus far...
		if sensor.iotask.replace(iotask).is_some() {
			eprintln!("BUG: re-activating already-active sensor {}", sensor.name);
		}

		sensor.available.set(true)
	}

	/// Stop a sensor's update task and mark it unavailable.
	pub async fn deactivate(&mut self) {
		let oldiotask = self.iotask.take();
		if oldiotask.is_none() {
			eprintln!("BUG: deactivate already-inactive sensor {}", self.name);
		}

		// Could just let this go out of scope, but might as well be explicit
		// (this is what aborts the update task)
		drop(oldiotask);

		self.set_value(f64::NAN).await;
		self.available.set(false)
	}

	/// Publish a sensor's properties on dbus.
	///
	/// `cbdata` must contain `self`.  (Perhaps this should instead be an associated
	/// function that just takes that instead of both separately.)
	pub fn add_to_dbus(&self, cr: &SyncMutex<dbus_crossroads::Crossroads>,
	                   sensor_intfs: &SensorIntfData, cbdata: &Arc<Mutex<Sensor>>)
	{
		let mut ifaces = vec![
			sensor_intfs.value_intf(&self.mode).token,
			sensor_intfs.availability.token,
			sensor_intfs.opstatus.token,
			sensor_intfs.assoc.token,
		];
		for (threshold, intf) in self.thresholds.iter().zip(&sensor_intfs.thresholds) {
			if threshold.is_some() {
				ifaces.push(intf.token);
			}
		}
		cr.lock().unwrap().insert(self.dbuspath.0.clone(), &ifaces, cbdata.clone());
	}
}

/// Maps sensor names to Sensors.
pub type SensorMap = HashMap<String, Arc<Mutex<Sensor>>>;
/// A convenience alias for use with [`get_nonactive_sensor_entry()`] and [`install_or_activate()`].
pub type SensorMapEntry<'a> = std::collections::hash_map::Entry<'a, String, Arc<Mutex<Sensor>>>;

/// Find a [`SensorMap`] entry for the given `key`.
///
/// Returns [`Some`] if there was no previous entry for `key` or if the sensor for `key`
/// is inactive.  If there is an active sensor for `key`, returns [`None`].
pub async fn get_nonactive_sensor_entry(sensors: &mut SensorMap, key: String)
                                        -> Option<SensorMapEntry<'_>>
{
	let entry = sensors.entry(key);
	if let SensorMapEntry::Occupied(ref e) = entry {
		if e.get().lock().await.iotask.is_some() {
			return None;
		}
	}
	Some(entry)
}

/// Given a [`SensorMapEntry`] from [`get_nonactive_sensor_entry()`], either activate the
/// inactive sensor or instantiate a new one.
///
/// If needed (there's no existing inactive sensor), a new sensor is constructed by
/// calling `ctor()`, added to dbus, and inserted into `entry`.  In either case, the
/// sensor is activated with `io` as its I/O context.
pub async fn install_or_activate<F>(entry: SensorMapEntry<'_>,
                                    cr: &SyncMutex<dbus_crossroads::Crossroads>,
                                    io: SensorIOCtx, sensor_intfs: &SensorIntfData, ctor: F)
where F: FnOnce() -> Sensor
{
	match entry {
		SensorMapEntry::Vacant(e) => {
			let sensor = Arc::new(Mutex::new(ctor()));
			Sensor::activate(&sensor, io).await;
			sensor.lock().await.add_to_dbus(cr, sensor_intfs, &sensor);
			e.insert(sensor);
		},
		SensorMapEntry::Occupied(e) => {
			// FIXME: update sensor config from hwmcfg
			Sensor::activate(e.get(), io).await;
		},
	};
}

/// Construct a property for a sensor interface.
///
/// The value will be retrieved by calling `getter()` on the sensor.
fn build_sensor_property<G, S, V>(b: &mut dbus_crossroads::IfaceBuilder<Arc<Mutex<Sensor>>>,
                                  name: &str, getter: G, setter: Option<S>) -> Box<PropChgMsgFn>
where G: Fn(&Sensor) -> V + Send + Copy + 'static,
      S: Fn(&mut Sensor, V) + Send + Copy + 'static,
      V: RefArg + Arg + Append + for<'a> dbus::arg::Get<'a> + Send + 'static
{
	let pb = b.property(name)
		.get_async(move |mut ctx, sensor| {
			let sensor = sensor.clone();
			async move {
				let value = {
					let s = sensor.lock().await;
					getter(&s)
				};
				ctx.reply(Ok(value))
			}
		});

	let pb = if let Some(setter) = setter {
		pb.set_async(move |mut ctx, sensor, value| {
			let sensor = sensor.clone();
			async move {
				{
					let mut s = sensor.lock().await;
					setter(&mut s, value);
				}

				// Skip signal emission; we assume that the provided
				// setter already takes care of it (presumably via a
				// SignalProp, which includes skipping it if the new value
				// is the same as the old one).
				ctx.reply_noemit(Ok(()));

				// FIXME: is there some better way to make the return type
				// work out here?
				core::marker::PhantomData
			}
		})
	} else {
		pb
	};

	// FIXME: they're not all SignalProps, so .emits_changed_true() isn't really
	// guaranteed for everything
	pb.emits_changed_true()
		.changed_msg_fn()
}

/// Convenience args for building read-only properties with build_sensor_property()
mod no_setter {
	use super::Sensor;
	pub(super) const F64: Option<fn(&mut Sensor, f64)> = None;
	pub(super) const STRING: Option<fn(&mut Sensor, String)> = None;
	pub(super) const BOOL: Option<fn(&mut Sensor, bool)> = None;
	pub(super) const ASSOCS: Option<fn(&mut Sensor, Vec<(String, String, String)>)> = None;
}

/// A collection of [`PropChgMsgFn`]s for the `xyz.openbmc_project.Sensor.Value`
/// interface.
pub struct ValueIntfMsgFns {
	pub unit: Arc<PropChgMsgFn>,
	pub value: Arc<PropChgMsgFn>,
	pub minvalue: Arc<PropChgMsgFn>,
	pub maxvalue: Arc<PropChgMsgFn>,
}

impl ValueIntfMsgFns {
	/// Construct the `xyz.openbmc_project.Sensor.Value` interface.
	fn build(cr: &mut dbus_crossroads::Crossroads, writable: bool) -> SensorIntf<Self> {
		SensorIntf::build(cr, "xyz.openbmc_project.Sensor.Value", |b| {
			let value_setter = if writable {
				Some(|s: &mut Sensor, v| {
					s.cache.set(v);
					match &s.mode {
						SensorMode::ReadWrite(cb) => cb(s, v),
						SensorMode::ReadOnly => {
							eprintln!("BUG: dbus set ReadOnly sensor {}",
							          s.name);
						},
					};
				})
			} else {
				None
			};
			Self {
				unit: build_sensor_property(b, "Unit",
				                            |s| s.kind.dbus_unit_str().to_string(),
				                            no_setter::STRING).into(),
				value: build_sensor_property(b, "Value", |s| s.cache.get(),
				                             value_setter).into(),
				minvalue: build_sensor_property(b, "MinValue", |s| s.minvalue.get(),
				                                no_setter::F64).into(),
				maxvalue: build_sensor_property(b, "MaxValue", |s| s.maxvalue.get(),
				                                no_setter::F64).into(),
			}
		})
	}

}

/// The [`PropChgMsgFn`] for the `xyz.openbmc_project.State.Decorator.Availability`
/// interface.
pub struct AvailabilityIntfMsgFns {
	pub available: Arc<PropChgMsgFn>,
}

impl AvailabilityIntfMsgFns {
	/// Construct the `xyz.openbmc_project.State.Decorator.Availability` interface.
	fn build(cr: &mut dbus_crossroads::Crossroads) -> SensorIntf<Self> {
		SensorIntf::build(cr, "xyz.openbmc_project.State.Decorator.Availability", |b| {
			Self {
				available: build_sensor_property(b, "Available", |s| s.available.get(),
				                                 no_setter::BOOL).into(),
			}
		})
	}
}

/// The [`PropChgMsgFn`] for the `xyz.openbmc_project.State.Decorator.OperationalStatus`
/// interface.
pub struct OpStatusIntfMsgFns {
	pub functional: Arc<PropChgMsgFn>,
}

impl OpStatusIntfMsgFns {
	/// Construct the `xyz.openbmc_project.State.Decorator.OperationalStatus` interface.
	fn build(cr: &mut dbus_crossroads::Crossroads) -> SensorIntf<Self> {
		SensorIntf::build(cr, "xyz.openbmc_project.State.Decorator.OperationalStatus", |b| {
			Self {
				functional: build_sensor_property(b, "Functional", |s| s.functional.get(),
				                                  no_setter::BOOL).into(),
			}
		})
	}
}

/// The [`PropChgMsgFn`] for the `xyz.openbmc_project.Association.Definitions` interface.
pub struct AssocIntfMsgFns {
	pub associations: Arc<PropChgMsgFn>,
}

impl AssocIntfMsgFns {
	/// Construct the `xyz.openbmc_project.Association.Definitions` interface.
	pub fn build(cr: &mut dbus_crossroads::Crossroads) -> SensorIntf<Self> {
		SensorIntf::build(cr, "xyz.openbmc_project.Association.Definitions", |b| {
			Self {
				associations: build_sensor_property(b, "Associations",
				                                    |s| { s.associations.get_clone() },
				                                    no_setter::ASSOCS).into()
			}
		})
	}
}


/// The aggregate of all the dbus interfaces for a sensor.
pub struct SensorIntfData {
	/// The `xyz.openbmc_project.Sensor.Value` interface.
	pub value: SensorIntf<ValueIntfMsgFns>,
	/// The `xyz.openbmc_project.Sensor.Value` interface, with a writable Value property.
	pub writable_value: SensorIntf<ValueIntfMsgFns>,
	/// The `xyz.openbmc_project.State.Decorator.Availability` interface.
	pub availability: SensorIntf<AvailabilityIntfMsgFns>,
	/// The `xyz.openbmc_project.State.Decorator.OperationalStatus` interface.
	pub opstatus: SensorIntf<OpStatusIntfMsgFns>,
	/// A per-severity-level array of `xyz.openbmc_project.Sensor.Threshold.$everity`
	/// interfaces.
	pub thresholds: threshold::ThresholdIntfDataArr,
	/// The `xyz.openbmc_project.Association.Definitions` interface.
	pub assoc: SensorIntf<AssocIntfMsgFns>,
}

impl SensorIntfData {
	/// Construct [`SensorIntfData`].
	pub fn build(cr: &mut dbus_crossroads::Crossroads) -> Self {
		Self {
			value: ValueIntfMsgFns::build(cr, false),
			writable_value: ValueIntfMsgFns::build(cr, true),
			availability: AvailabilityIntfMsgFns::build(cr),
			opstatus: OpStatusIntfMsgFns::build(cr),
			thresholds: threshold::build_sensor_threshold_intfs(cr),
			assoc: AssocIntfMsgFns::build(cr),
		}
	}

	/// Retrieve the appropriate Value interface for the specified writability.
	pub fn value_intf(&self, mode: &SensorMode) -> &SensorIntf<ValueIntfMsgFns> {
		match mode {
			SensorMode::ReadOnly => &self.value,
			SensorMode::ReadWrite(_) => &self.writable_value,
		}
	}
}

/// Iterate through a sensor map and deactivate all the sensors whose
/// [`power_state`](Sensor::power_state) no longer matches the current host power state.
pub async fn deactivate(sensors: &mut SensorMap) {
	for sensor in sensors.values_mut() {
		let sensor = &mut *sensor.lock().await;
		if sensor.iotask.is_none() { // FIXME: wrap this check or something?
			continue;
		};
		if sensor.power_state.active_now() {
			continue;
		}
		sensor.deactivate().await;
	}
}

/// Instantiate sensors from all backends to match the given `cfg`.
pub async fn instantiate_all(daemonstate: &DaemonState, filter: &FilterSet<InventoryPath>) {
	#[cfg(feature = "adc")]
	adc::instantiate_sensors(daemonstate, filter).await.unwrap_or_else(|e| {
		eprintln!("Failed to instantiate ADC sensors: {}", e);
	});

	#[cfg(feature = "fan")]
	fan::instantiate_sensors(daemonstate, filter).await.unwrap_or_else(|e| {
		eprintln!("Failed to instantiate fan sensors: {}", e);
	});

	#[cfg(feature = "peci")]
	peci::instantiate_sensors(daemonstate, filter).await.unwrap_or_else(|e| {
		eprintln!("Failed to instantiate PECI sensors: {}", e);
	});

	#[cfg(feature = "hwmon")]
	hwmon::instantiate_sensors(daemonstate, filter).await.unwrap_or_else(|e| {
		eprintln!("Failed to instantiate hwmon sensors: {}", e);
	});

	#[cfg(feature = "external")]
	external::instantiate_sensors(daemonstate, filter).await.unwrap_or_else(|e| {
		eprintln!("Failed to instantiate external sensors: {}", e);
	});
}