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
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "dbus_utility.hpp"
#include "error_messages.hpp"
#include "generated/enums/resource.hpp"
#include "generated/enums/sensor.hpp"
#include "generated/enums/thermal.hpp"
#include "logging.hpp"
#include "str_utility.hpp"
#include "utils/dbus_utils.hpp"
#include <boost/url/format.hpp>
#include <nlohmann/json.hpp>
#include <sdbusplus/message/native_types.hpp>
#include <sdbusplus/unpack_properties.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <format>
#include <functional>
#include <iterator>
#include <optional>
#include <ranges>
#include <set>
#include <span>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
namespace redfish
{
namespace sensor_utils
{
enum class ChassisSubNode
{
powerNode,
sensorsNode,
thermalNode,
thermalMetricsNode,
unknownNode,
};
constexpr std::string_view chassisSubNodeToString(ChassisSubNode subNode)
{
switch (subNode)
{
case ChassisSubNode::powerNode:
return "Power";
case ChassisSubNode::sensorsNode:
return "Sensors";
case ChassisSubNode::thermalNode:
return "Thermal";
case ChassisSubNode::thermalMetricsNode:
return "ThermalMetrics";
case ChassisSubNode::unknownNode:
default:
return "";
}
}
inline ChassisSubNode chassisSubNodeFromString(const std::string& subNodeStr)
{
// If none match unknownNode is returned
ChassisSubNode subNode = ChassisSubNode::unknownNode;
if (subNodeStr == "Power")
{
subNode = ChassisSubNode::powerNode;
}
else if (subNodeStr == "Sensors")
{
subNode = ChassisSubNode::sensorsNode;
}
else if (subNodeStr == "Thermal")
{
subNode = ChassisSubNode::thermalNode;
}
else if (subNodeStr == "ThermalMetrics")
{
subNode = ChassisSubNode::thermalMetricsNode;
}
return subNode;
}
inline bool isExcerptNode(const ChassisSubNode subNode)
{
return (subNode == ChassisSubNode::thermalMetricsNode);
}
/**
* Possible states for physical inventory leds
*/
enum class LedState
{
OFF,
ON,
BLINK,
UNKNOWN
};
/**
* D-Bus inventory item associated with one or more sensors.
*/
class InventoryItem
{
public:
explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
{
// Set inventory item name to last node of object path
sdbusplus::message::object_path path(objectPath);
name = path.filename();
if (name.empty())
{
BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
}
}
std::string objectPath;
std::string name;
bool isPresent = true;
bool isFunctional = true;
bool isPowerSupply = false;
int powerSupplyEfficiencyPercent = -1;
std::string manufacturer;
std::string model;
std::string partNumber;
std::string serialNumber;
std::set<std::string> sensors;
std::string ledObjectPath;
LedState ledState = LedState::UNKNOWN;
};
inline std::string getSensorId(std::string_view sensorName,
std::string_view sensorType)
{
std::string normalizedType(sensorType);
auto remove = std::ranges::remove(normalizedType, '_');
normalizedType.erase(std::ranges::begin(remove), normalizedType.end());
return std::format("{}_{}", normalizedType, sensorName);
}
inline std::pair<std::string, std::string> splitSensorNameAndType(
std::string_view sensorId)
{
size_t index = sensorId.find('_');
if (index == std::string::npos)
{
return std::make_pair<std::string, std::string>("", "");
}
std::string sensorType{sensorId.substr(0, index)};
std::string sensorName{sensorId.substr(index + 1)};
// fan_pwm and fan_tach need special handling
if (sensorType == "fantach" || sensorType == "fanpwm")
{
sensorType.insert(3, 1, '_');
}
return std::make_pair(sensorType, sensorName);
}
namespace sensors
{
inline std::string_view toReadingUnits(std::string_view sensorType)
{
if (sensorType == "voltage")
{
return "V";
}
if (sensorType == "power")
{
return "W";
}
if (sensorType == "current")
{
return "A";
}
if (sensorType == "fan_tach")
{
return "RPM";
}
if (sensorType == "temperature")
{
return "Cel";
}
if (sensorType == "fan_pwm" || sensorType == "utilization" ||
sensorType == "humidity")
{
return "%";
}
if (sensorType == "altitude")
{
return "m";
}
if (sensorType == "airflow")
{
return "cft_i/min";
}
if (sensorType == "energy")
{
return "J";
}
if (sensorType == "liquidflow")
{
return "L/min";
}
if (sensorType == "pressure")
{
return "Pa";
}
return "";
}
inline sensor::ReadingType toReadingType(std::string_view sensorType)
{
if (sensorType == "voltage")
{
return sensor::ReadingType::Voltage;
}
if (sensorType == "power")
{
return sensor::ReadingType::Power;
}
if (sensorType == "current")
{
return sensor::ReadingType::Current;
}
if (sensorType == "fan_tach")
{
return sensor::ReadingType::Rotational;
}
if (sensorType == "temperature")
{
return sensor::ReadingType::Temperature;
}
if (sensorType == "fan_pwm" || sensorType == "utilization")
{
return sensor::ReadingType::Percent;
}
if (sensorType == "humidity")
{
return sensor::ReadingType::Humidity;
}
if (sensorType == "altitude")
{
return sensor::ReadingType::Altitude;
}
if (sensorType == "airflow")
{
return sensor::ReadingType::AirFlow;
}
if (sensorType == "energy")
{
return sensor::ReadingType::EnergyJoules;
}
if (sensorType == "liquidflow")
{
return sensor::ReadingType::LiquidFlowLPM;
}
if (sensorType == "pressure")
{
return sensor::ReadingType::PressurePa;
}
return sensor::ReadingType::Invalid;
}
} // namespace sensors
/**
* @brief Returns the Redfish State value for the specified inventory item.
* @param inventoryItem D-Bus inventory item associated with a sensor.
* @param sensorAvailable Boolean representing if D-Bus sensor is marked as
* available.
* @return State value for inventory item.
*/
inline resource::State getState(const InventoryItem* inventoryItem,
const bool sensorAvailable)
{
if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
{
return resource::State::Absent;
}
if (!sensorAvailable)
{
return resource::State::UnavailableOffline;
}
return resource::State::Enabled;
}
/**
* @brief Returns the Redfish Health value for the specified sensor.
* @param sensorJson Sensor JSON object.
* @param valuesDict Map of all sensor DBus values.
* @param inventoryItem D-Bus inventory item associated with the sensor. Will
* be nullptr if no associated inventory item was found.
* @return Health value for sensor.
*/
inline std::string getHealth(nlohmann::json& sensorJson,
const dbus::utility::DBusPropertiesMap& valuesDict,
const InventoryItem* inventoryItem)
{
// Get current health value (if any) in the sensor JSON object. Some JSON
// objects contain multiple sensors (such as PowerSupplies). We want to set
// the overall health to be the most severe of any of the sensors.
std::string currentHealth;
auto statusIt = sensorJson.find("Status");
if (statusIt != sensorJson.end())
{
auto healthIt = statusIt->find("Health");
if (healthIt != statusIt->end())
{
std::string* health = healthIt->get_ptr<std::string*>();
if (health != nullptr)
{
currentHealth = *health;
}
}
}
// If current health in JSON object is already Critical, return that. This
// should override the sensor health, which might be less severe.
if (currentHealth == "Critical")
{
return "Critical";
}
const bool* criticalAlarmHigh = nullptr;
const bool* criticalAlarmLow = nullptr;
const bool* warningAlarmHigh = nullptr;
const bool* warningAlarmLow = nullptr;
const bool success = sdbusplus::unpackPropertiesNoThrow(
dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
"WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
warningAlarmLow);
if (success)
{
// Check if sensor has critical threshold alarm
if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
(criticalAlarmLow != nullptr && *criticalAlarmLow))
{
return "Critical";
}
}
// Check if associated inventory item is not functional
if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
{
return "Critical";
}
// If current health in JSON object is already Warning, return that. This
// should override the sensor status, which might be less severe.
if (currentHealth == "Warning")
{
return "Warning";
}
if (success)
{
// Check if sensor has warning threshold alarm
if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
(warningAlarmLow != nullptr && *warningAlarmLow))
{
return "Warning";
}
}
return "OK";
}
inline void setLedState(nlohmann::json& sensorJson,
const InventoryItem* inventoryItem)
{
if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
{
switch (inventoryItem->ledState)
{
case LedState::OFF:
sensorJson["IndicatorLED"] = resource::IndicatorLED::Off;
break;
case LedState::ON:
sensorJson["IndicatorLED"] = resource::IndicatorLED::Lit;
break;
case LedState::BLINK:
sensorJson["IndicatorLED"] = resource::IndicatorLED::Blinking;
break;
default:
break;
}
}
}
/**
* @brief Builds a json sensor representation of a sensor.
* @param sensorName The name of the sensor to be built
* @param sensorType The type (temperature, fan_tach, etc) of the sensor to
* build
* @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
* @param propertiesDict A dictionary of the properties to build the sensor
* from.
* @param sensorJson The json object to fill
* @param inventoryItem D-Bus inventory item associated with the sensor. Will
* be nullptr if no associated inventory item was found.
*/
inline void objectPropertiesToJson(
std::string_view sensorName, std::string_view sensorType,
ChassisSubNode chassisSubNode,
const dbus::utility::DBusPropertiesMap& propertiesDict,
nlohmann::json& sensorJson, InventoryItem* inventoryItem)
{
// Parameter to set to override the type we get from dbus, and force it to
// int, regardless of what is available. This is used for schemas like fan,
// that require integers, not floats.
bool forceToInt = false;
nlohmann::json::json_pointer unit("/Reading");
// This ChassisSubNode builds sensor excerpts
bool isExcerpt = isExcerptNode(chassisSubNode);
/* Sensor excerpts use different keys to reference the sensor. These are
* built by the caller.
* Additionally they don't include these additional properties.
*/
if (!isExcerpt)
{
if (chassisSubNode == ChassisSubNode::sensorsNode)
{
std::string subNodeEscaped = getSensorId(sensorName, sensorType);
// For sensors in SensorCollection we set Id instead of MemberId,
// including power sensors.
sensorJson["Id"] = std::move(subNodeEscaped);
std::string sensorNameEs(sensorName);
std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
sensorJson["Name"] = std::move(sensorNameEs);
}
else if (sensorType != "power")
{
// Set MemberId and Name for non-power sensors. For PowerSupplies
// and PowerControl, those properties have more general values
// because multiple sensors can be stored in the same JSON object.
std::string sensorNameEs(sensorName);
std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
sensorJson["Name"] = std::move(sensorNameEs);
}
const bool* checkAvailable = nullptr;
bool available = true;
const bool success = sdbusplus::unpackPropertiesNoThrow(
dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
checkAvailable);
if (!success)
{
messages::internalError();
}
if (checkAvailable != nullptr)
{
available = *checkAvailable;
}
sensorJson["Status"]["State"] = getState(inventoryItem, available);
sensorJson["Status"]["Health"] =
getHealth(sensorJson, propertiesDict, inventoryItem);
if (chassisSubNode == ChassisSubNode::sensorsNode)
{
sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
sensor::ReadingType readingType =
sensors::toReadingType(sensorType);
if (readingType == sensor::ReadingType::Invalid)
{
BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
sensorType);
}
else
{
sensorJson["ReadingType"] = readingType;
}
std::string_view readingUnits = sensors::toReadingUnits(sensorType);
if (readingUnits.empty())
{
BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
sensorType);
}
else
{
sensorJson["ReadingUnits"] = readingUnits;
}
}
else if (sensorType == "temperature")
{
unit = "/ReadingCelsius"_json_pointer;
sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
// TODO(ed) Documentation says that path should be type fan_tach,
// implementation seems to implement fan
}
else if (sensorType == "fan" || sensorType == "fan_tach")
{
unit = "/Reading"_json_pointer;
sensorJson["ReadingUnits"] = thermal::ReadingUnits::RPM;
sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_INDICATORLED)
{
setLedState(sensorJson, inventoryItem);
}
forceToInt = true;
}
else if (sensorType == "fan_pwm")
{
unit = "/Reading"_json_pointer;
sensorJson["ReadingUnits"] = thermal::ReadingUnits::Percent;
sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_INDICATORLED)
{
setLedState(sensorJson, inventoryItem);
}
forceToInt = true;
}
else if (sensorType == "voltage")
{
unit = "/ReadingVolts"_json_pointer;
sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
}
else if (sensorType == "power")
{
std::string lower;
std::ranges::transform(sensorName, std::back_inserter(lower),
bmcweb::asciiToLower);
if (lower == "total_power")
{
sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
// Put multiple "sensors" into a single PowerControl, so have
// generic names for MemberId and Name. Follows Redfish mockup.
sensorJson["MemberId"] = "0";
sensorJson["Name"] = "Chassis Power Control";
unit = "/PowerConsumedWatts"_json_pointer;
}
else if (lower.find("input") != std::string::npos)
{
unit = "/PowerInputWatts"_json_pointer;
}
else
{
unit = "/PowerOutputWatts"_json_pointer;
}
}
else
{
BMCWEB_LOG_ERROR("Redfish cannot map object type for {}",
sensorName);
return;
}
}
// Map of dbus interface name, dbus property name and redfish property_name
std::vector<
std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
properties;
properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
if (!isExcerpt)
{
if (chassisSubNode == ChassisSubNode::sensorsNode)
{
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
"/Thresholds/UpperCaution/Reading"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
"/Thresholds/LowerCaution/Reading"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
"/Thresholds/UpperCritical/Reading"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
"/Thresholds/LowerCritical/Reading"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.HardShutdown",
"HardShutdownHigh",
"/Thresholds/UpperFatal/Reading"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.HardShutdown",
"HardShutdownLow",
"/Thresholds/LowerFatal/Reading"_json_pointer);
/* Add additional properties specific to sensorType */
if (sensorType == "fan_tach")
{
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"Value", "/SpeedRPM"_json_pointer);
}
}
else if (sensorType != "power")
{
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
"/UpperThresholdNonCritical"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
"/LowerThresholdNonCritical"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
"/UpperThresholdCritical"_json_pointer);
properties.emplace_back(
"xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
"/LowerThresholdCritical"_json_pointer);
}
// TODO Need to get UpperThresholdFatal and LowerThresholdFatal
if (chassisSubNode == ChassisSubNode::sensorsNode)
{
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MinValue",
"/ReadingRangeMin"_json_pointer);
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MaxValue",
"/ReadingRangeMax"_json_pointer);
properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
"Accuracy", "/Accuracy"_json_pointer);
}
else if (sensorType == "temperature")
{
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MinValue",
"/MinReadingRangeTemp"_json_pointer);
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MaxValue",
"/MaxReadingRangeTemp"_json_pointer);
}
else if (sensorType != "power")
{
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MinValue",
"/MinReadingRange"_json_pointer);
properties.emplace_back("xyz.openbmc_project.Sensor.Value",
"MaxValue",
"/MaxReadingRange"_json_pointer);
}
}
for (const std::tuple<const char*, const char*,
nlohmann::json::json_pointer>& p : properties)
{
for (const auto& [valueName, valueVariant] : propertiesDict)
{
if (valueName != std::get<1>(p))
{
continue;
}
// The property we want to set may be nested json, so use
// a json_pointer for easy indexing into the json structure.
const nlohmann::json::json_pointer& key = std::get<2>(p);
const double* doubleValue = std::get_if<double>(&valueVariant);
if (doubleValue == nullptr)
{
BMCWEB_LOG_ERROR("Got value interface that wasn't double");
continue;
}
if (!std::isfinite(*doubleValue))
{
if (valueName == "Value")
{
// Readings are allowed to be NAN for unavailable; coerce
// them to null in the json response.
sensorJson[key] = nullptr;
continue;
}
BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
valueName, *doubleValue);
continue;
}
if (forceToInt)
{
sensorJson[key] = static_cast<int64_t>(*doubleValue);
}
else
{
sensorJson[key] = *doubleValue;
}
}
}
}
/**
* @brief Builds a json sensor excerpt representation of a sensor.
*
* @details This is a wrapper function to provide consistent setting of
* "DataSourceUri" for sensor excerpts and filling of properties. Since sensor
* excerpts usually have just the D-Bus path for the sensor that is accepted
* and used to build "DataSourceUri".
* @param path The D-Bus path to the sensor to be built
* @param chassisId The Chassis Id for the sensor
* @param chassisSubNode The subnode (e.g. ThermalMetrics) of the sensor
* @param sensorTypeExpected The expected type of the sensor
* @param propertiesDict A dictionary of the properties to build the sensor
* from.
* @param sensorJson The json object to fill
* @returns True if sensorJson object filled. False on any error.
* Caller is responsible for handling error.
*/
inline bool objectExcerptToJson(
const std::string& path, const std::string_view chassisId,
ChassisSubNode chassisSubNode,
const std::optional<std::string>& sensorTypeExpected,
const dbus::utility::DBusPropertiesMap& propertiesDict,
nlohmann::json& sensorJson)
{
if (!isExcerptNode(chassisSubNode))
{
BMCWEB_LOG_DEBUG("{} is not a sensor excerpt",
chassisSubNodeToString(chassisSubNode));
return false;
}
sdbusplus::message::object_path sensorPath(path);
std::string sensorName = sensorPath.filename();
std::string sensorType = sensorPath.parent_path().filename();
if (sensorName.empty() || sensorType.empty())
{
BMCWEB_LOG_DEBUG("Invalid sensor path {}", path);
return false;
}
if (sensorTypeExpected && (sensorType != *sensorTypeExpected))
{
BMCWEB_LOG_DEBUG("{} is not expected type {}", path,
*sensorTypeExpected);
return false;
}
// Sensor excerpts use DataSourceUri to reference full sensor Redfish path
sensorJson["DataSourceUri"] =
boost::urls::format("/redfish/v1/Chassis/{}/Sensors/{}", chassisId,
getSensorId(sensorName, sensorType));
// Fill in sensor excerpt properties
objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
propertiesDict, sensorJson, nullptr);
return true;
}
// Maps D-Bus: Service, SensorPath
using SensorServicePathMap = std::pair<std::string, std::string>;
using SensorServicePathList = std::vector<SensorServicePathMap>;
inline void getAllSensorObjects(
const std::string& associatedPath, const std::string& path,
std::span<const std::string_view> interfaces, const int32_t depth,
std::function<void(const boost::system::error_code& ec,
SensorServicePathList&)>&& callback)
{
sdbusplus::message::object_path endpointPath{associatedPath};
endpointPath /= "all_sensors";
dbus::utility::getAssociatedSubTree(
endpointPath, sdbusplus::message::object_path(path), depth, interfaces,
[callback = std::move(callback)](
const boost::system::error_code& ec,
const dbus::utility::MapperGetSubTreeResponse& subtree) {
SensorServicePathList sensorsServiceAndPath;
if (ec)
{
callback(ec, sensorsServiceAndPath);
return;
}
for (const auto& [sensorPath, serviceMaps] : subtree)
{
for (const auto& [service, mapInterfaces] : serviceMaps)
{
sensorsServiceAndPath.emplace_back(service, sensorPath);
}
}
callback(ec, sensorsServiceAndPath);
});
}
} // namespace sensor_utils
} // namespace redfish
|