ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
//! Motherboard and system identity, BIOS, firmware, TPM and chassis detail,
//! motherboard voltage rails and fans, batteries and power supplies.

#[allow(unused_imports)]
use super::super::resolve::*;
#[allow(unused_imports)]
use super::super::{Domain as D, Entity, EntityKind as K, Provenance as P, Unit as U};

pub(super) fn declare(add: &mut dyn FnMut(Entity)) {
    // ── System identity, from the firmware's identity tables ─────────────────
    add(Entity::new(
        "board.serial",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Specification,
        true,
        concat!(
            "Serial number from the firmware's identity tables. The source differs ",
            "by platform: on Windows it is the baseboard's serial, not the chassis's. ",
            "Null where the firmware reports none or a stand-in value."
        ),
    ));
    add(Entity::new(
        "board.uuid",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Specification,
        true,
        "System UUID from the firmware's identity tables. Null where the firmware reports the all-zero or all-F value that means unset.",
    ));
    add(Entity::new(
        "board.version",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Revision string the board vendor printed for the motherboard, from the firmware's baseboard table.",
    ));
    add(Entity::new(
        "board.bios.version",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "BIOS or UEFI version string, as the firmware reports it.",
    ));
    add(Entity::new(
        "board.bios.release_date",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Date the installed BIOS or UEFI image was released, in the firmware's own format, normally YYYY-MM-DD.",
    ));
    add(Entity::new(
        "board.bios.revision",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "BIOS revision as major.minor, from the SMBIOS system BIOS version fields. Null where the firmware reports no revision.",
    ));

    // ── Firmware inventory ──────────────────────────────────────────────────
    add(Entity::new(
        "board.firmware.<none>",
        D::Board,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when the firmware inventory enumerated no entries, carrying why.",
    ));
    add(Entity::new(
        "board.firmware.serial",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Specification,
        true,
        "System serial number from the firmware inventory. Null where the reader cannot read it, which on Linux needs root.",
    ));
    add(Entity::new(
        "board.firmware.{n}.vendor",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Vendor of this firmware entry, as the source declares it. Null where the source names none.",
    ));
    add(Entity::new(
        "board.firmware.{n}.date",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Release date of this firmware entry, in the source's own format, normally YYYY-MM-DD. Null where the source gives none.",
    ));
    add(Entity::new(
        "board.firmware.{n}.device",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Device path or identifier this firmware entry belongs to.",
    ));
    add(Entity::new(
        "board.firmware.{n}.updateable",
        D::Board,
        K::Identity,
        None,
        P::Measured,
        true,
        "Whether the operating system can update this firmware, where the source says so. Null means the source did not say, not that the component cannot be updated.",
    ));

    // ── TPM detail ──────────────────────────────────────────────────────────
    add(Entity::new(
        "board.tpm.device",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Name of the TPM device node, such as tpm0. Null where the TPM does not report one.",
    ));
    add(Entity::new(
        "board.tpm.firmware_version",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Firmware version the TPM reports for itself. Null where it does not report one.",
    ));
    add(Entity::new(
        "board.tpm.algorithms",
        D::Board,
        K::Identity,
        None,
        P::Measured,
        true,
        "JSON array of the hash and signing algorithms the TPM supports, such as SHA-256 and RSA. An empty array means the TPM enumerated none.",
    ));
    add(Entity::new(
        "board.tpm.pcr_banks",
        D::Board,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Number of platform configuration register banks the TPM exposes. Null where it was not read.",
    ));

    // ── Chassis, read live from SMBIOS on Windows and DMI on Linux ──────────
    // Every field is read from the running system's tables (or, on macOS, from
    // `system_profiler`), so each is a measurement of this machine. A placeholder
    // the firmware left in a field is reported as absent.
    add(Entity::new(
        "board.chassis.serial",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Serial number of the chassis, read from the firmware's system enclosure table. Null where it is unset, a placeholder, or unreadable.",
    ));
    add(Entity::new(
        "board.chassis.asset_tag",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Asset tag the owner or vendor wrote into the chassis firmware. Null where none is set.",
    ));
    add(Entity::new(
        "board.chassis.sku",
        D::Board,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Stock-keeping unit code the vendor assigned to this system. Null where the firmware reports none.",
    ));
    add(Entity::new(
        "board.chassis.version",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Version string of the chassis, read from the firmware's enclosure table. Null where none is reported.",
    ));
    add(Entity::new(
        "board.chassis.manufacturer",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Manufacturer of the chassis, read from the firmware's system tables. Null where the firmware reports none, and on macOS, where no manufacturer is read.",
    ));
    add(Entity::new(
        "board.chassis.product_name",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Product name of the chassis, read from the firmware's system tables. Null where the firmware reports none or a placeholder.",
    ));

    // ── Motherboard voltage rails and fans ──────────────────────────────────
    add(Entity::new(
        "board.voltage.<none>",
        D::Board,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no voltage rail was reported, carrying why.",
    ));
    add(Entity::new(
        "board.voltage.<unreadable>",
        D::Board,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when a motherboard sensor device was enumerated but its voltage rails could not be read, naming the device and the error.",
    ));
    add(Entity::new(
        "board.voltage.{n}.label",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        false,
        "Label the sensor chip gives this voltage rail, such as VCore or +12V.",
    ));
    add(Entity::new(
        "board.voltage.{n}.voltage",
        D::Board,
        K::Measurement,
        Some(U::Volts),
        P::Measured,
        true,
        "Current voltage on this rail. Null where the rail is listed but could not be sampled.",
    ));
    add(Entity::new(
        "board.voltage.{n}.min",
        D::Board,
        K::Limit,
        Some(U::Volts),
        P::Specification,
        true,
        "Lowest voltage this rail is specified to accept, as declared by the sensor source. Null where no limit was declared.",
    ));
    add(Entity::new(
        "board.voltage.{n}.max",
        D::Board,
        K::Limit,
        Some(U::Volts),
        P::Specification,
        true,
        "Highest voltage this rail is specified to accept, as declared by the sensor source. Null where no limit was declared.",
    ));
    add(Entity::new(
        "board.fan.<none>",
        D::Board,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no motherboard fan was reported, carrying why.",
    ));
    add(Entity::new(
        "board.fan.<unreadable>",
        D::Board,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when a motherboard sensor device was enumerated but its fans could not be read, naming the device and the error.",
    ));
    add(Entity::new(
        "board.fan.{n}.label",
        D::Board,
        K::Identity,
        Some(U::Text),
        P::Measured,
        false,
        "Label the sensor source gives this fan, such as CPU Fan or Case Fan 1.",
    ));
    add(Entity::new(
        "board.fan.{n}.rpm",
        D::Board,
        K::Measurement,
        Some(U::Rpm),
        P::Measured,
        true,
        "Current speed of this fan. Null where the fan is stopped or its speed cannot be measured.",
    ));
    add(Entity::new(
        "board.fan.{n}.pwm",
        D::Board,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Current pulse-width modulation duty for this fan, from 0 to 255. Null where the fan has no PWM control.",
    ));
    add(Entity::new(
        "board.fan.{n}.min_rpm",
        D::Board,
        K::Limit,
        Some(U::Rpm),
        P::Specification,
        true,
        "Lowest speed this fan is specified to run at, as declared by the sensor source. Null where no limit was declared.",
    ));
    add(Entity::new(
        "board.fan.{n}.max_rpm",
        D::Board,
        K::Limit,
        Some(U::Rpm),
        P::Specification,
        true,
        "Highest speed this fan is specified to reach, as declared by the sensor source. Null where no limit was declared.",
    ));
    add(Entity::new(
        "board.fan.{n}.controllable",
        D::Board,
        K::Identity,
        None,
        P::Measured,
        true,
        "Whether this fan's speed can be set. Null where the sensor source did not determine it.",
    ));

    // ── Batteries ───────────────────────────────────────────────────────────
    add(Entity::new(
        "power.battery.<none>",
        D::Power,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no battery was enumerated, carrying why.",
    ));
    add(Entity::new(
        "power.battery.{n}.manufacturer",
        D::Power,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Battery manufacturer, as the battery reports it.",
    ));
    add(Entity::new(
        "power.battery.{n}.model",
        D::Power,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Battery model name, as the battery reports it.",
    ));
    add(Entity::new(
        "power.battery.{n}.technology",
        D::Power,
        K::Identity,
        Some(U::Text),
        P::Specification,
        true,
        "Cell chemistry the battery reports, such as Li-ion or Li-poly.",
    ));
    add(Entity::new(
        "power.battery.{n}.serial_number",
        D::Power,
        K::Identity,
        Some(U::Identifier),
        P::Specification,
        true,
        "Serial number the battery reports for itself. Null where it reports none.",
    ));
    add(Entity::new(
        "power.battery.{n}.state",
        D::Power,
        K::Measurement,
        Some(U::Identifier),
        P::Measured,
        true,
        "Charging state: charging, discharging, full or notcharging. Null where the battery did not say.",
    ));
    add(Entity::new(
        "power.battery.{n}.health",
        D::Power,
        K::Measurement,
        Some(U::Identifier),
        P::Measured,
        true,
        "Health the battery reports: good, fair or poor. Null where the battery did not say.",
    ));
    add(Entity::new(
        "power.battery.{n}.voltage",
        D::Power,
        K::Measurement,
        Some(U::Volts),
        P::Measured,
        true,
        "Present terminal voltage of the battery.",
    ));
    add(Entity::new(
        "power.battery.{n}.power",
        D::Power,
        K::Measurement,
        Some(U::Watts),
        P::Measured,
        true,
        "Power the battery is currently drawing or supplying.",
    ));
    add(Entity::new(
        "power.battery.{n}.energy_now",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Energy the battery holds now, in watt-hours. The unit is not one the ontology names, so it is stated here.",
    ));
    add(Entity::new(
        "power.battery.{n}.energy_full",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Energy the battery holds when fully charged, in watt-hours. Falls as the battery wears.",
    ));
    add(Entity::new(
        "power.battery.{n}.energy_full_design",
        D::Power,
        K::Limit,
        Some(U::Count),
        P::Specification,
        true,
        "Energy the battery was designed to hold when new, in watt-hours, as the battery reports it.",
    ));
    add(Entity::new(
        "power.battery.{n}.time_to_empty",
        D::Power,
        K::Measurement,
        Some(U::Seconds),
        P::Measured,
        true,
        "Estimated time until the battery is empty at its present draw. Null while charging or where unreported.",
    ));
    add(Entity::new(
        "power.battery.{n}.time_to_full",
        D::Power,
        K::Measurement,
        Some(U::Seconds),
        P::Measured,
        true,
        "Estimated time until the battery is full at its present charge rate. Null while discharging or where unreported.",
    ));
    add(Entity::new(
        "power.battery.{n}.cycle_count",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Charge cycles the battery has completed, as its own counter reports them.",
    ));
    add(Entity::new(
        "power.battery.{n}.temperature",
        D::Power,
        K::Measurement,
        Some(U::Celsius),
        P::Measured,
        true,
        "Temperature the battery reports for itself.",
    ));

    // ── Power supplies: mains, USB, UPS and the batteries behind them ───────
    add(Entity::new(
        "power.supply.<none>",
        D::Power,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no power supply was enumerated, carrying why.",
    ));
    add(Entity::new(
        "power.supply.{n}.type",
        D::Power,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Kind of supply: mains, battery, usb, usbpd or ups.",
    ));
    add(Entity::new(
        "power.supply.{n}.online",
        D::Power,
        K::Measurement,
        None,
        P::Measured,
        true,
        "Whether this supply is currently online, such as a mains adapter plugged in.",
    ));
    add(Entity::new(
        "power.supply.{n}.status",
        D::Power,
        K::Measurement,
        Some(U::Identifier),
        P::Measured,
        true,
        "Charging status the supply reports: charging, discharging, full or notcharging.",
    ));
    add(Entity::new(
        "power.supply.{n}.health",
        D::Power,
        K::Measurement,
        Some(U::Identifier),
        P::Measured,
        true,
        "Health the supply reports, such as good or overheat. Null where it did not say.",
    ));
    add(Entity::new(
        "power.supply.{n}.voltage",
        D::Power,
        K::Measurement,
        Some(U::Volts),
        P::Measured,
        true,
        "Present voltage of the supply.",
    ));
    add(Entity::new(
        "power.supply.{n}.current",
        D::Power,
        K::Measurement,
        None,
        P::Measured,
        true,
        "Present current of the supply, in amperes. Negative while the supply is discharging. The unit is not one the ontology names, so it is stated here.",
    ));
    add(Entity::new(
        "power.supply.{n}.power",
        D::Power,
        K::Measurement,
        Some(U::Watts),
        P::Measured,
        true,
        "Power the supply is currently delivering or drawing.",
    ));
    add(Entity::new(
        "power.supply.{n}.energy_now",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Energy the supply holds now, in watt-hours. The unit is not one the ontology names, so it is stated here.",
    ));
    add(Entity::new(
        "power.supply.{n}.energy_full",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Energy the supply holds when fully charged, in watt-hours.",
    ));
    add(Entity::new(
        "power.supply.{n}.energy_full_design",
        D::Power,
        K::Limit,
        Some(U::Count),
        P::Specification,
        true,
        "Energy the supply was designed to hold when new, in watt-hours, as it reports.",
    ));
    add(Entity::new(
        "power.supply.{n}.capacity",
        D::Power,
        K::Measurement,
        Some(U::Percent),
        P::Measured,
        true,
        "Charge level of the supply as a percentage of its full capacity.",
    ));
    add(Entity::new(
        "power.supply.{n}.cycle_count",
        D::Power,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Charge cycles the supply has completed, as its own counter reports them.",
    ));
    add(Entity::new(
        "power.supply.{n}.serial_number",
        D::Power,
        K::Identity,
        Some(U::Identifier),
        P::Specification,
        true,
        "Serial number the supply reports for itself. Null where it reports none.",
    ));
}

pub(super) fn resolve(out: &mut Vec<Reading>) {
    resolve_identity(out);
    resolve_firmware_detail(out);
    resolve_tpm_detail(out);
    resolve_chassis(out);
    resolve_motherboard_sensors(out);
    resolve_batteries(out);
    resolve_power_supplies(out);
}

// ── Helpers shared with `peripherals` ───────────────────────────────────────

/// Strings that firmware fills in where a vendor left a field unset. They are
/// stand-ins, not identities: an all-zero or all-F UUID is the SMBIOS "not set"
/// value, and "To Be Filled By O.E.M." is what boards ship with in serial fields.
fn is_standin(value: &str) -> bool {
    // One list for the reader and the ontology. It used to live only here, so
    // the chassis reader passed these strings through.
    crate::datacenter::is_firmware_placeholder(value)
}

/// A text reading. A blank or absent value is unavailable with `why`. A
/// firmware-declared value that is a stand-in is unavailable too, since passing
/// it through would present "To Be Filled By O.E.M." as a serial number.
pub(super) fn text_reading(
    out: &mut Vec<Reading>,
    id: impl Into<String>,
    value: Option<&str>,
    unit: U,
    why: &str,
    provenance: P,
) {
    let id = id.into();
    match value.map(str::trim).filter(|v| !v.is_empty()) {
        None => out.push(Reading::unavailable(id, Some(unit), why)),
        Some(v) if provenance == P::Specification && is_standin(v) => {
            out.push(Reading::unavailable(
                id,
                Some(unit),
                format!(
                    "the firmware filled this field with {v:?}, a stand-in rather than a value"
                ),
            ))
        }
        Some(v) => push_str_as(out, id, v, unit, provenance),
    }
}

/// An enumerated state as its lowercase name. `Unknown` and a missing value are
/// the reader failing to decide, so they are unavailable rather than a state.
pub(super) fn enum_reading(
    out: &mut Vec<Reading>,
    id: impl Into<String>,
    debug_name: Option<String>,
    why: &str,
) {
    let id = id.into();
    match debug_name {
        Some(name) if name != "Unknown" => push_id(out, id, &name.to_lowercase()),
        _ => out.push(Reading::unavailable(id, Some(U::Identifier), why)),
    }
}

/// A JSON number rounded to three places, or `None` for a value that is not finite.
pub(super) fn finite(value: f64) -> Option<serde_json::Value> {
    value
        .is_finite()
        .then(|| serde_json::json!((value * 1000.0).round() / 1000.0))
}

// ── Resolvers ───────────────────────────────────────────────────────────────

fn resolve_identity(out: &mut Vec<Reading>) {
    const IDS: [(&str, U); 6] = [
        ("board.serial", U::Identifier),
        ("board.uuid", U::Identifier),
        ("board.version", U::Text),
        ("board.bios.version", U::Text),
        ("board.bios.release_date", U::Text),
        ("board.bios.revision", U::Text),
    ];

    let info = match crate::motherboard::get_system_info() {
        Ok(info) => info,
        Err(e) => {
            for (id, unit) in IDS {
                out.push(Reading::unavailable(
                    id,
                    Some(unit),
                    format!("the platform identity reader failed: {e}"),
                ));
            }
            return;
        }
    };

    let why = "the firmware's identity tables did not report this field on this platform";
    let spec = P::Specification;
    text_reading(
        out,
        "board.serial",
        info.serial_number.as_deref(),
        U::Identifier,
        why,
        spec,
    );
    text_reading(
        out,
        "board.uuid",
        info.uuid.as_deref(),
        U::Identifier,
        why,
        spec,
    );
    text_reading(
        out,
        "board.version",
        info.board_version.as_deref(),
        U::Text,
        why,
        spec,
    );
    text_reading(
        out,
        "board.bios.version",
        info.bios.version.as_deref(),
        U::Text,
        why,
        spec,
    );
    text_reading(
        out,
        "board.bios.release_date",
        info.bios.release_date.as_deref(),
        U::Text,
        why,
        spec,
    );
    text_reading(
        out,
        "board.bios.revision",
        info.bios.revision.as_deref(),
        U::Text,
        why,
        spec,
    );
}

fn resolve_firmware_detail(out: &mut Vec<Reading>) {
    let inventory = match crate::firmware::FirmwareInventory::new() {
        Ok(i) => i,
        Err(e) => {
            out.push(Reading::unavailable(
                "board.firmware.serial",
                Some(U::Identifier),
                format!("firmware inventory unavailable: {e}"),
            ));
            return;
        }
    };

    text_reading(
        out,
        "board.firmware.serial",
        inventory.system_serial.as_deref(),
        U::Identifier,
        "the firmware inventory did not report a system serial number on this platform",
        P::Specification,
    );

    let items = inventory.items();
    if items.is_empty() {
        out.push(Reading::unavailable(
            "board.firmware.<none>",
            None,
            "the firmware inventory enumerated no entries on this machine",
        ));
        return;
    }

    for (i, entry) in items.iter().enumerate() {
        let base = format!("board.firmware.{i}");
        text_reading(
            out,
            format!("{base}.vendor"),
            entry.vendor.as_deref(),
            U::Text,
            "the source did not name a vendor for this firmware entry",
            P::Specification,
        );
        text_reading(
            out,
            format!("{base}.date"),
            entry.date.as_deref(),
            U::Text,
            "the source did not give a release date for this firmware entry",
            P::Specification,
        );
        text_reading(
            out,
            format!("{base}.device"),
            Some(entry.device.as_str()),
            U::Identifier,
            "the firmware entry names no device",
            P::Measured,
        );
        push_opt(
            out,
            format!("{base}.updateable"),
            entry.updateable.map(|u| serde_json::json!(u)),
            None,
            "the source did not say whether the operating system can update this entry",
        );
    }
}

fn resolve_tpm_detail(out: &mut Vec<Reading>) {
    const IDS: [(&str, Option<U>); 4] = [
        ("board.tpm.device", Some(U::Identifier)),
        ("board.tpm.firmware_version", Some(U::Text)),
        ("board.tpm.algorithms", None),
        ("board.tpm.pcr_banks", Some(U::Count)),
    ];

    let monitor = match crate::tpm::TpmMonitor::new() {
        Ok(m) => m,
        Err(e) => {
            for (id, unit) in IDS {
                out.push(Reading::unavailable(
                    id,
                    unit,
                    format!("TPM enumeration failed: {e}"),
                ));
            }
            return;
        }
    };

    let Some(tpm) = monitor.tpm() else {
        for (id, unit) in IDS {
            out.push(Reading::unavailable(
                id,
                unit,
                "no TPM present on this machine",
            ));
        }
        return;
    };

    text_reading(
        out,
        "board.tpm.device",
        Some(tpm.device.as_str()),
        U::Identifier,
        "the TPM reports no device name",
        P::Measured,
    );
    text_reading(
        out,
        "board.tpm.firmware_version",
        Some(tpm.firmware_version.as_str()),
        U::Text,
        "the TPM does not report a firmware version to this reader",
        P::Measured,
    );
    match &tpm.algorithms {
        Some(list) => {
            let names: Vec<&String> = list
                .iter()
                .filter(|a| !names_an_absence(a.as_str()))
                .collect();
            out.push(Reading::measured(
                "board.tpm.algorithms",
                serde_json::json!(names),
                None,
            ));
        }
        None => out.push(Reading::unavailable(
            "board.tpm.algorithms",
            None,
            "the TPM's algorithm list was not read on this platform",
        )),
    }
    push_opt(
        out,
        "board.tpm.pcr_banks",
        tpm.pcr_banks.map(|n| serde_json::json!(n)),
        Some(U::Count),
        "the PCR bank count was not read on this platform",
    );
}

fn resolve_chassis(out: &mut Vec<Reading>) {
    const IDS: [(&str, U); 6] = [
        ("board.chassis.serial", U::Identifier),
        ("board.chassis.asset_tag", U::Identifier),
        ("board.chassis.sku", U::Identifier),
        ("board.chassis.version", U::Text),
        ("board.chassis.manufacturer", U::Text),
        ("board.chassis.product_name", U::Text),
    ];

    let chassis = match crate::datacenter::ChassisInfo::detect() {
        Ok(c) => c,
        Err(e) => {
            for (id, unit) in IDS {
                out.push(Reading::unavailable(
                    id,
                    Some(unit),
                    format!("chassis detection failed: {e}"),
                ));
            }
            return;
        }
    };

    // Read live by the chassis reader, so Measured. The reader has already
    // dropped placeholders; `text_reading` only checks the empty case here.
    // The reader returns `None` for an absent field and for a firmware
    // placeholder ("System Product Name" on this board), so the reason names both.
    let why =
        "the chassis reader obtained no value: the field is absent or holds a firmware placeholder";
    let measured = P::Measured;
    text_reading(
        out,
        "board.chassis.serial",
        chassis.serial_number.as_deref(),
        U::Identifier,
        why,
        measured,
    );
    text_reading(
        out,
        "board.chassis.asset_tag",
        chassis.asset_tag.as_deref(),
        U::Identifier,
        why,
        measured,
    );
    text_reading(
        out,
        "board.chassis.sku",
        chassis.sku.as_deref(),
        U::Identifier,
        why,
        measured,
    );
    text_reading(
        out,
        "board.chassis.version",
        chassis.version.as_deref(),
        U::Text,
        why,
        measured,
    );
    text_reading(
        out,
        "board.chassis.manufacturer",
        chassis.manufacturer.as_deref(),
        U::Text,
        why,
        measured,
    );
    text_reading(
        out,
        "board.chassis.product_name",
        chassis.product_name.as_deref(),
        U::Text,
        why,
        measured,
    );
}

/// Voltage rails and fans, from every motherboard sensor device. One device
/// failing is reported as `<unreadable>` naming it, and the others still publish.
fn resolve_motherboard_sensors(out: &mut Vec<Reading>) {
    let devices = match crate::motherboard::enumerate_sensors() {
        Ok(d) => d,
        Err(e) => {
            for prefix in ["board.voltage", "board.fan"] {
                out.push(Reading::unavailable(
                    format!("{prefix}.<none>"),
                    None,
                    format!("motherboard sensor enumeration failed: {e}"),
                ));
            }
            return;
        }
    };

    let mut rails = Vec::new();
    let mut voltage_errors = Vec::new();
    let mut fans = Vec::new();
    let mut fan_errors = Vec::new();
    for device in &devices {
        let name = device.name().to_string();
        match device.voltage_rails() {
            Ok(list) => rails.extend(list),
            Err(e) => voltage_errors.push(format!("{name}: {e}")),
        }
        match device.fans() {
            Ok(list) => fans.extend(list),
            Err(e) => fan_errors.push(format!("{name}: {e}")),
        }
    }

    const NO_RAILS: &str = concat!(
        "no voltage rail was reported by the motherboard sensor source. On Windows ",
        "this means LibreHardwareMonitor or OpenHardwareMonitor is not running, because ",
        "WMI does not expose voltages"
    );
    if !voltage_errors.is_empty() {
        out.push(Reading::unavailable(
            "board.voltage.<unreadable>",
            None,
            voltage_errors.join("; "),
        ));
    } else if rails.is_empty() {
        out.push(Reading::unavailable("board.voltage.<none>", None, NO_RAILS));
    }
    for (i, rail) in rails.iter().enumerate() {
        let base = format!("board.voltage.{i}");
        push_text(out, format!("{base}.label"), &rail.label);
        push_opt(
            out,
            format!("{base}.voltage"),
            rail.voltage.and_then(|v| finite(f64::from(v))),
            Some(U::Volts),
            "the rail is listed by the sensor source but its voltage could not be sampled",
        );
        push_spec_opt(
            out,
            format!("{base}.min"),
            rail.min.and_then(|v| finite(f64::from(v))),
            Some(U::Volts),
            "the sensor source declares no lower limit for this rail",
        );
        push_spec_opt(
            out,
            format!("{base}.max"),
            rail.max.and_then(|v| finite(f64::from(v))),
            Some(U::Volts),
            "the sensor source declares no upper limit for this rail",
        );
    }

    const NO_FANS: &str = concat!(
        "no fan was reported by the motherboard sensor source. On Windows this means ",
        "LibreHardwareMonitor is not running and the WMI fan class is empty"
    );
    if !fan_errors.is_empty() {
        out.push(Reading::unavailable(
            "board.fan.<unreadable>",
            None,
            fan_errors.join("; "),
        ));
    } else if fans.is_empty() {
        out.push(Reading::unavailable("board.fan.<none>", None, NO_FANS));
    }
    for (i, fan) in fans.iter().enumerate() {
        let base = format!("board.fan.{i}");
        push_text(out, format!("{base}.label"), &fan.label);
        push_opt(
            out,
            format!("{base}.rpm"),
            fan.rpm.map(|r| serde_json::json!(r)),
            Some(U::Rpm),
            "the fan is stopped or its speed cannot be measured from this source",
        );
        push_opt(
            out,
            format!("{base}.pwm"),
            fan.pwm.map(|p| serde_json::json!(p)),
            Some(U::Count),
            "this fan exposes no PWM duty cycle",
        );
        push_spec_opt(
            out,
            format!("{base}.min_rpm"),
            fan.min_rpm.map(|r| serde_json::json!(r)),
            Some(U::Rpm),
            "the sensor source declares no minimum speed for this fan",
        );
        push_spec_opt(
            out,
            format!("{base}.max_rpm"),
            fan.max_rpm.map(|r| serde_json::json!(r)),
            Some(U::Rpm),
            "the sensor source declares no maximum speed for this fan",
        );
        push_opt(
            out,
            format!("{base}.controllable"),
            fan.controllable.map(|c| serde_json::json!(c)),
            None,
            "the sensor source did not determine whether this fan can be controlled",
        );
    }
}

fn resolve_batteries(out: &mut Vec<Reading>) {
    let monitor = match crate::battery::BatteryMonitor::new() {
        Ok(m) => m,
        Err(e) => {
            out.push(Reading::unavailable(
                "power.battery.<none>",
                None,
                format!("battery enumeration failed: {e}"),
            ));
            return;
        }
    };

    let batteries = monitor.batteries();
    if batteries.is_empty() {
        out.push(Reading::unavailable(
            "power.battery.<none>",
            None,
            "no battery present (desktop or always-AC machine)",
        ));
        return;
    }

    for (i, b) in batteries.iter().enumerate() {
        let base = format!("power.battery.{i}");
        let why = "the battery did not report this value";
        text_reading(
            out,
            format!("{base}.manufacturer"),
            b.manufacturer.as_deref(),
            U::Text,
            why,
            P::Specification,
        );
        text_reading(
            out,
            format!("{base}.model"),
            b.model.as_deref(),
            U::Text,
            why,
            P::Specification,
        );
        text_reading(
            out,
            format!("{base}.technology"),
            b.technology.as_deref(),
            U::Text,
            why,
            P::Specification,
        );
        text_reading(
            out,
            format!("{base}.serial_number"),
            b.serial_number.as_deref(),
            U::Identifier,
            why,
            P::Specification,
        );
        enum_reading(
            out,
            format!("{base}.state"),
            b.state.as_ref().map(|s| format!("{s:?}")),
            "the battery did not say whether it is charging",
        );
        enum_reading(
            out,
            format!("{base}.health"),
            b.health.as_ref().map(|h| format!("{h:?}")),
            "the battery did not report its health",
        );
        push_opt(
            out,
            format!("{base}.voltage"),
            b.voltage_mv.and_then(|mv| finite(f64::from(mv) / 1000.0)),
            Some(U::Volts),
            why,
        );
        push_opt(
            out,
            format!("{base}.power"),
            b.power_w.and_then(|w| finite(f64::from(w))),
            Some(U::Watts),
            why,
        );
        push_opt(
            out,
            format!("{base}.energy_now"),
            b.energy_wh.and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_opt(
            out,
            format!("{base}.energy_full"),
            b.energy_full_wh.and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_spec_opt(
            out,
            format!("{base}.energy_full_design"),
            b.energy_full_design_wh.and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_opt(
            out,
            format!("{base}.time_to_empty"),
            b.time_to_empty.map(|d| serde_json::json!(d.as_secs())),
            Some(U::Seconds),
            "the battery did not estimate time to empty",
        );
        push_opt(
            out,
            format!("{base}.time_to_full"),
            b.time_to_full.map(|d| serde_json::json!(d.as_secs())),
            Some(U::Seconds),
            "the battery did not estimate time to full",
        );
        push_opt(
            out,
            format!("{base}.cycle_count"),
            b.cycle_count.map(|c| serde_json::json!(c)),
            Some(U::Count),
            "the battery does not report a cycle count",
        );
        push_opt(
            out,
            format!("{base}.temperature"),
            b.temperature_c.and_then(|t| finite(f64::from(t))),
            Some(U::Celsius),
            "the battery does not report a temperature",
        );
    }
}

fn resolve_power_supplies(out: &mut Vec<Reading>) {
    let monitor = match crate::power_supply::PowerSupplyMonitor::new() {
        Ok(m) => m,
        Err(e) => {
            out.push(Reading::unavailable(
                "power.supply.<none>",
                None,
                format!("power supply enumeration failed: {e}"),
            ));
            return;
        }
    };

    let supplies = monitor.supplies();
    if supplies.is_empty() {
        out.push(Reading::unavailable(
            "power.supply.<none>",
            None,
            "no power supply was enumerated on this machine",
        ));
        return;
    }

    for (i, s) in supplies.iter().enumerate() {
        let base = format!("power.supply.{i}");
        enum_reading(
            out,
            format!("{base}.type"),
            Some(format!("{:?}", s.supply_type)),
            "the supply did not report its kind",
        );
        push_opt(
            out,
            format!("{base}.online"),
            s.online.map(|o| serde_json::json!(o)),
            None,
            "the supply did not report whether it is online",
        );
        enum_reading(
            out,
            format!("{base}.status"),
            s.status.as_ref().map(|c| format!("{c:?}")),
            "the supply did not report its charging status",
        );
        enum_reading(
            out,
            format!("{base}.health"),
            s.health.as_ref().map(|h| format!("{h:?}")),
            "the supply did not report its health",
        );
        let why = "the supply did not report this value";
        push_opt(
            out,
            format!("{base}.voltage"),
            s.voltage_v().and_then(|v| finite(f64::from(v))),
            Some(U::Volts),
            why,
        );
        push_opt(
            out,
            format!("{base}.current"),
            s.current_a().and_then(|c| finite(f64::from(c))),
            None,
            why,
        );
        push_opt(
            out,
            format!("{base}.power"),
            s.power_w().and_then(|w| finite(f64::from(w))),
            Some(U::Watts),
            why,
        );
        push_opt(
            out,
            format!("{base}.energy_now"),
            s.energy_wh().and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_opt(
            out,
            format!("{base}.energy_full"),
            s.energy_full_wh().and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_spec_opt(
            out,
            format!("{base}.energy_full_design"),
            s.energy_full_design_wh().and_then(|e| finite(f64::from(e))),
            Some(U::Count),
            why,
        );
        push_opt(
            out,
            format!("{base}.capacity"),
            s.capacity_percent.map(|c| serde_json::json!(c)),
            Some(U::Percent),
            why,
        );
        push_opt(
            out,
            format!("{base}.cycle_count"),
            s.cycle_count.map(|c| serde_json::json!(c)),
            Some(U::Count),
            "the supply does not report a cycle count",
        );
        text_reading(
            out,
            format!("{base}.serial_number"),
            s.serial_number.as_deref(),
            U::Identifier,
            "the supply reports no serial number",
            P::Specification,
        );
    }
}