1 /* 2 // Copyright (c) 2018 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 18 #include "app.hpp" 19 #include "dbus_singleton.hpp" 20 #include "dbus_utility.hpp" 21 #include "generated/enums/resource.hpp" 22 #include "generated/enums/sensor.hpp" 23 #include "query.hpp" 24 #include "registries/privilege_registry.hpp" 25 #include "str_utility.hpp" 26 #include "utils/dbus_utils.hpp" 27 #include "utils/json_utils.hpp" 28 #include "utils/query_param.hpp" 29 30 #include <boost/system/error_code.hpp> 31 #include <boost/url/format.hpp> 32 #include <sdbusplus/asio/property.hpp> 33 #include <sdbusplus/unpack_properties.hpp> 34 35 #include <array> 36 #include <cmath> 37 #include <iterator> 38 #include <limits> 39 #include <map> 40 #include <ranges> 41 #include <set> 42 #include <string> 43 #include <string_view> 44 #include <utility> 45 #include <variant> 46 47 namespace redfish 48 { 49 50 namespace sensors 51 { 52 namespace node 53 { 54 static constexpr std::string_view power = "Power"; 55 static constexpr std::string_view sensors = "Sensors"; 56 static constexpr std::string_view thermal = "Thermal"; 57 } // namespace node 58 59 // clang-format off 60 namespace dbus 61 { 62 constexpr auto powerPaths = std::to_array<std::string_view>({ 63 "/xyz/openbmc_project/sensors/voltage", 64 "/xyz/openbmc_project/sensors/power" 65 }); 66 67 constexpr auto sensorPaths = std::to_array<std::string_view>({ 68 "/xyz/openbmc_project/sensors/power", 69 "/xyz/openbmc_project/sensors/current", 70 "/xyz/openbmc_project/sensors/airflow", 71 "/xyz/openbmc_project/sensors/humidity", 72 #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM 73 "/xyz/openbmc_project/sensors/voltage", 74 "/xyz/openbmc_project/sensors/fan_tach", 75 "/xyz/openbmc_project/sensors/temperature", 76 "/xyz/openbmc_project/sensors/fan_pwm", 77 "/xyz/openbmc_project/sensors/altitude", 78 "/xyz/openbmc_project/sensors/energy", 79 #endif 80 "/xyz/openbmc_project/sensors/utilization" 81 }); 82 83 constexpr auto thermalPaths = std::to_array<std::string_view>({ 84 "/xyz/openbmc_project/sensors/fan_tach", 85 "/xyz/openbmc_project/sensors/temperature", 86 "/xyz/openbmc_project/sensors/fan_pwm" 87 }); 88 89 } // namespace dbus 90 // clang-format on 91 92 using sensorPair = 93 std::pair<std::string_view, std::span<const std::string_view>>; 94 static constexpr std::array<sensorPair, 3> paths = { 95 {{node::power, dbus::powerPaths}, 96 {node::sensors, dbus::sensorPaths}, 97 {node::thermal, dbus::thermalPaths}}}; 98 99 inline sensor::ReadingType toReadingType(std::string_view sensorType) 100 { 101 if (sensorType == "voltage") 102 { 103 return sensor::ReadingType::Voltage; 104 } 105 if (sensorType == "power") 106 { 107 return sensor::ReadingType::Power; 108 } 109 if (sensorType == "current") 110 { 111 return sensor::ReadingType::Current; 112 } 113 if (sensorType == "fan_tach") 114 { 115 return sensor::ReadingType::Rotational; 116 } 117 if (sensorType == "temperature") 118 { 119 return sensor::ReadingType::Temperature; 120 } 121 if (sensorType == "fan_pwm" || sensorType == "utilization") 122 { 123 return sensor::ReadingType::Percent; 124 } 125 if (sensorType == "humidity") 126 { 127 return sensor::ReadingType::Humidity; 128 } 129 if (sensorType == "altitude") 130 { 131 return sensor::ReadingType::Altitude; 132 } 133 if (sensorType == "airflow") 134 { 135 return sensor::ReadingType::AirFlow; 136 } 137 if (sensorType == "energy") 138 { 139 return sensor::ReadingType::EnergyJoules; 140 } 141 return sensor::ReadingType::Invalid; 142 } 143 144 inline std::string_view toReadingUnits(std::string_view sensorType) 145 { 146 if (sensorType == "voltage") 147 { 148 return "V"; 149 } 150 if (sensorType == "power") 151 { 152 return "W"; 153 } 154 if (sensorType == "current") 155 { 156 return "A"; 157 } 158 if (sensorType == "fan_tach") 159 { 160 return "RPM"; 161 } 162 if (sensorType == "temperature") 163 { 164 return "Cel"; 165 } 166 if (sensorType == "fan_pwm" || sensorType == "utilization" || 167 sensorType == "humidity") 168 { 169 return "%"; 170 } 171 if (sensorType == "altitude") 172 { 173 return "m"; 174 } 175 if (sensorType == "airflow") 176 { 177 return "cft_i/min"; 178 } 179 if (sensorType == "energy") 180 { 181 return "J"; 182 } 183 return ""; 184 } 185 } // namespace sensors 186 187 /** 188 * SensorsAsyncResp 189 * Gathers data needed for response processing after async calls are done 190 */ 191 class SensorsAsyncResp 192 { 193 public: 194 using DataCompleteCb = std::function<void( 195 const boost::beast::http::status status, 196 const std::map<std::string, std::string>& uriToDbus)>; 197 198 struct SensorData 199 { 200 const std::string name; 201 std::string uri; 202 const std::string dbusPath; 203 }; 204 205 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 206 const std::string& chassisIdIn, 207 std::span<const std::string_view> typesIn, 208 std::string_view subNode) : 209 asyncResp(asyncRespIn), 210 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 211 efficientExpand(false) 212 {} 213 214 // Store extra data about sensor mapping and return it in callback 215 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 216 const std::string& chassisIdIn, 217 std::span<const std::string_view> typesIn, 218 std::string_view subNode, 219 DataCompleteCb&& creationComplete) : 220 asyncResp(asyncRespIn), 221 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 222 efficientExpand(false), metadata{std::vector<SensorData>()}, 223 dataComplete{std::move(creationComplete)} 224 {} 225 226 // sensor collections expand 227 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 228 const std::string& chassisIdIn, 229 std::span<const std::string_view> typesIn, 230 const std::string_view& subNode, bool efficientExpandIn) : 231 asyncResp(asyncRespIn), 232 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 233 efficientExpand(efficientExpandIn) 234 {} 235 236 ~SensorsAsyncResp() 237 { 238 if (asyncResp->res.result() == 239 boost::beast::http::status::internal_server_error) 240 { 241 // Reset the json object to clear out any data that made it in 242 // before the error happened todo(ed) handle error condition with 243 // proper code 244 asyncResp->res.jsonValue = nlohmann::json::object(); 245 } 246 247 if (dataComplete && metadata) 248 { 249 std::map<std::string, std::string> map; 250 if (asyncResp->res.result() == boost::beast::http::status::ok) 251 { 252 for (auto& sensor : *metadata) 253 { 254 map.emplace(sensor.uri, sensor.dbusPath); 255 } 256 } 257 dataComplete(asyncResp->res.result(), map); 258 } 259 } 260 261 SensorsAsyncResp(const SensorsAsyncResp&) = delete; 262 SensorsAsyncResp(SensorsAsyncResp&&) = delete; 263 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 264 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 265 266 void addMetadata(const nlohmann::json& sensorObject, 267 const std::string& dbusPath) 268 { 269 if (metadata) 270 { 271 metadata->emplace_back(SensorData{ 272 sensorObject["Name"], sensorObject["@odata.id"], dbusPath}); 273 } 274 } 275 276 void updateUri(const std::string& name, const std::string& uri) 277 { 278 if (metadata) 279 { 280 for (auto& sensor : *metadata) 281 { 282 if (sensor.name == name) 283 { 284 sensor.uri = uri; 285 } 286 } 287 } 288 } 289 290 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 291 const std::string chassisId; 292 const std::span<const std::string_view> types; 293 const std::string chassisSubNode; 294 const bool efficientExpand; 295 296 private: 297 std::optional<std::vector<SensorData>> metadata; 298 DataCompleteCb dataComplete; 299 }; 300 301 /** 302 * Possible states for physical inventory leds 303 */ 304 enum class LedState 305 { 306 OFF, 307 ON, 308 BLINK, 309 UNKNOWN 310 }; 311 312 /** 313 * D-Bus inventory item associated with one or more sensors. 314 */ 315 class InventoryItem 316 { 317 public: 318 explicit InventoryItem(const std::string& objPath) : objectPath(objPath) 319 { 320 // Set inventory item name to last node of object path 321 sdbusplus::message::object_path path(objectPath); 322 name = path.filename(); 323 if (name.empty()) 324 { 325 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath); 326 } 327 } 328 329 std::string objectPath; 330 std::string name; 331 bool isPresent = true; 332 bool isFunctional = true; 333 bool isPowerSupply = false; 334 int powerSupplyEfficiencyPercent = -1; 335 std::string manufacturer; 336 std::string model; 337 std::string partNumber; 338 std::string serialNumber; 339 std::set<std::string> sensors; 340 std::string ledObjectPath; 341 LedState ledState = LedState::UNKNOWN; 342 }; 343 344 /** 345 * @brief Get objects with connection necessary for sensors 346 * @param SensorsAsyncResp Pointer to object holding response data 347 * @param sensorNames Sensors retrieved from chassis 348 * @param callback Callback for processing gathered connections 349 */ 350 template <typename Callback> 351 void getObjectsWithConnection( 352 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 353 const std::shared_ptr<std::set<std::string>>& sensorNames, 354 Callback&& callback) 355 { 356 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter"); 357 const std::string path = "/xyz/openbmc_project/sensors"; 358 constexpr std::array<std::string_view, 1> interfaces = { 359 "xyz.openbmc_project.Sensor.Value"}; 360 361 // Make call to ObjectMapper to find all sensors objects 362 dbus::utility::getSubTree( 363 path, 2, interfaces, 364 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 365 sensorNames](const boost::system::error_code& ec, 366 const dbus::utility::MapperGetSubTreeResponse& subtree) { 367 // Response handler for parsing objects subtree 368 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter"); 369 if (ec) 370 { 371 messages::internalError(sensorsAsyncResp->asyncResp->res); 372 BMCWEB_LOG_ERROR( 373 "getObjectsWithConnection resp_handler: Dbus error {}", ec); 374 return; 375 } 376 377 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size()); 378 379 // Make unique list of connections only for requested sensor types and 380 // found in the chassis 381 std::set<std::string> connections; 382 std::set<std::pair<std::string, std::string>> objectsWithConnection; 383 384 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size()); 385 for (const std::string& tsensor : *sensorNames) 386 { 387 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor); 388 } 389 390 for (const std::pair< 391 std::string, 392 std::vector<std::pair<std::string, std::vector<std::string>>>>& 393 object : subtree) 394 { 395 if (sensorNames->find(object.first) != sensorNames->end()) 396 { 397 for (const std::pair<std::string, std::vector<std::string>>& 398 objData : object.second) 399 { 400 BMCWEB_LOG_DEBUG("Adding connection: {}", objData.first); 401 connections.insert(objData.first); 402 objectsWithConnection.insert( 403 std::make_pair(object.first, objData.first)); 404 } 405 } 406 } 407 BMCWEB_LOG_DEBUG("Found {} connections", connections.size()); 408 callback(std::move(connections), std::move(objectsWithConnection)); 409 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit"); 410 }); 411 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit"); 412 } 413 414 /** 415 * @brief Create connections necessary for sensors 416 * @param SensorsAsyncResp Pointer to object holding response data 417 * @param sensorNames Sensors retrieved from chassis 418 * @param callback Callback for processing gathered connections 419 */ 420 template <typename Callback> 421 void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 422 const std::shared_ptr<std::set<std::string>> sensorNames, 423 Callback&& callback) 424 { 425 auto objectsWithConnectionCb = 426 [callback](const std::set<std::string>& connections, 427 const std::set<std::pair<std::string, std::string>>& 428 /*objectsWithConnection*/) { callback(connections); }; 429 getObjectsWithConnection(sensorsAsyncResp, sensorNames, 430 std::move(objectsWithConnectionCb)); 431 } 432 433 /** 434 * @brief Shrinks the list of sensors for processing 435 * @param SensorsAysncResp The class holding the Redfish response 436 * @param allSensors A list of all the sensors associated to the 437 * chassis element (i.e. baseboard, front panel, etc...) 438 * @param activeSensors A list that is a reduction of the incoming 439 * allSensors list. Eliminate Thermal sensors when a Power request is 440 * made, and eliminate Power sensors when a Thermal request is made. 441 */ 442 inline void reduceSensorList( 443 crow::Response& res, std::string_view chassisSubNode, 444 std::span<const std::string_view> sensorTypes, 445 const std::vector<std::string>* allSensors, 446 const std::shared_ptr<std::set<std::string>>& activeSensors) 447 { 448 if ((allSensors == nullptr) || (activeSensors == nullptr)) 449 { 450 messages::resourceNotFound(res, chassisSubNode, 451 chassisSubNode == sensors::node::thermal 452 ? "Temperatures" 453 : "Voltages"); 454 455 return; 456 } 457 if (allSensors->empty()) 458 { 459 // Nothing to do, the activeSensors object is also empty 460 return; 461 } 462 463 for (std::string_view type : sensorTypes) 464 { 465 for (const std::string& sensor : *allSensors) 466 { 467 if (sensor.starts_with(type)) 468 { 469 activeSensors->emplace(sensor); 470 } 471 } 472 } 473 } 474 475 /* 476 *Populates the top level collection for a given subnode. Populates 477 *SensorCollection, Power, or Thermal schemas. 478 * 479 * */ 480 inline void populateChassisNode(nlohmann::json& jsonValue, 481 std::string_view chassisSubNode) 482 { 483 if (chassisSubNode == sensors::node::power) 484 { 485 jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; 486 } 487 else if (chassisSubNode == sensors::node::thermal) 488 { 489 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; 490 jsonValue["Fans"] = nlohmann::json::array(); 491 jsonValue["Temperatures"] = nlohmann::json::array(); 492 } 493 else if (chassisSubNode == sensors::node::sensors) 494 { 495 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; 496 jsonValue["Description"] = "Collection of Sensors for this Chassis"; 497 jsonValue["Members"] = nlohmann::json::array(); 498 jsonValue["Members@odata.count"] = 0; 499 } 500 501 if (chassisSubNode != sensors::node::sensors) 502 { 503 jsonValue["Id"] = chassisSubNode; 504 } 505 jsonValue["Name"] = chassisSubNode; 506 } 507 508 /** 509 * @brief Retrieves requested chassis sensors and redundancy data from DBus . 510 * @param SensorsAsyncResp Pointer to object holding response data 511 * @param callback Callback for next step in gathered sensor processing 512 */ 513 template <typename Callback> 514 void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 515 std::string_view chassisId, std::string_view chassisSubNode, 516 std::span<const std::string_view> sensorTypes, 517 Callback&& callback) 518 { 519 BMCWEB_LOG_DEBUG("getChassis enter"); 520 constexpr std::array<std::string_view, 2> interfaces = { 521 "xyz.openbmc_project.Inventory.Item.Board", 522 "xyz.openbmc_project.Inventory.Item.Chassis"}; 523 524 // Get the Chassis Collection 525 dbus::utility::getSubTreePaths( 526 "/xyz/openbmc_project/inventory", 0, interfaces, 527 [callback{std::forward<Callback>(callback)}, asyncResp, 528 chassisIdStr{std::string(chassisId)}, 529 chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( 530 const boost::system::error_code& ec, 531 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { 532 BMCWEB_LOG_DEBUG("getChassis respHandler enter"); 533 if (ec) 534 { 535 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec); 536 messages::internalError(asyncResp->res); 537 return; 538 } 539 const std::string* chassisPath = nullptr; 540 for (const std::string& chassis : chassisPaths) 541 { 542 sdbusplus::message::object_path path(chassis); 543 std::string chassisName = path.filename(); 544 if (chassisName.empty()) 545 { 546 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis); 547 continue; 548 } 549 if (chassisName == chassisIdStr) 550 { 551 chassisPath = &chassis; 552 break; 553 } 554 } 555 if (chassisPath == nullptr) 556 { 557 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr); 558 return; 559 } 560 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); 561 562 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 563 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode); 564 565 // Get the list of all sensors for this Chassis element 566 std::string sensorPath = *chassisPath + "/all_sensors"; 567 dbus::utility::getAssociationEndPoints( 568 sensorPath, 569 [asyncResp, chassisSubNode, sensorTypes, 570 callback{std::forward<const Callback>(callback)}]( 571 const boost::system::error_code& ec2, 572 const dbus::utility::MapperEndPoints& nodeSensorList) { 573 if (ec2) 574 { 575 if (ec2.value() != EBADR) 576 { 577 messages::internalError(asyncResp->res); 578 return; 579 } 580 } 581 const std::shared_ptr<std::set<std::string>> culledSensorList = 582 std::make_shared<std::set<std::string>>(); 583 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes, 584 &nodeSensorList, culledSensorList); 585 BMCWEB_LOG_DEBUG("Finishing with {}", culledSensorList->size()); 586 callback(culledSensorList); 587 }); 588 }); 589 BMCWEB_LOG_DEBUG("getChassis exit"); 590 } 591 592 /** 593 * @brief Returns the Redfish State value for the specified inventory item. 594 * @param inventoryItem D-Bus inventory item associated with a sensor. 595 * @param sensorAvailable Boolean representing if D-Bus sensor is marked as 596 * available. 597 * @return State value for inventory item. 598 */ 599 inline resource::State getState(const InventoryItem* inventoryItem, 600 const bool sensorAvailable) 601 { 602 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 603 { 604 return resource::State::Absent; 605 } 606 607 if (!sensorAvailable) 608 { 609 return resource::State::UnavailableOffline; 610 } 611 612 return resource::State::Enabled; 613 } 614 615 /** 616 * @brief Returns the Redfish Health value for the specified sensor. 617 * @param sensorJson Sensor JSON object. 618 * @param valuesDict Map of all sensor DBus values. 619 * @param inventoryItem D-Bus inventory item associated with the sensor. Will 620 * be nullptr if no associated inventory item was found. 621 * @return Health value for sensor. 622 */ 623 inline std::string getHealth(nlohmann::json& sensorJson, 624 const dbus::utility::DBusPropertiesMap& valuesDict, 625 const InventoryItem* inventoryItem) 626 { 627 // Get current health value (if any) in the sensor JSON object. Some JSON 628 // objects contain multiple sensors (such as PowerSupplies). We want to set 629 // the overall health to be the most severe of any of the sensors. 630 std::string currentHealth; 631 auto statusIt = sensorJson.find("Status"); 632 if (statusIt != sensorJson.end()) 633 { 634 auto healthIt = statusIt->find("Health"); 635 if (healthIt != statusIt->end()) 636 { 637 std::string* health = healthIt->get_ptr<std::string*>(); 638 if (health != nullptr) 639 { 640 currentHealth = *health; 641 } 642 } 643 } 644 645 // If current health in JSON object is already Critical, return that. This 646 // should override the sensor health, which might be less severe. 647 if (currentHealth == "Critical") 648 { 649 return "Critical"; 650 } 651 652 const bool* criticalAlarmHigh = nullptr; 653 const bool* criticalAlarmLow = nullptr; 654 const bool* warningAlarmHigh = nullptr; 655 const bool* warningAlarmLow = nullptr; 656 657 const bool success = sdbusplus::unpackPropertiesNoThrow( 658 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh", 659 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow, 660 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow", 661 warningAlarmLow); 662 663 if (success) 664 { 665 // Check if sensor has critical threshold alarm 666 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) || 667 (criticalAlarmLow != nullptr && *criticalAlarmLow)) 668 { 669 return "Critical"; 670 } 671 } 672 673 // Check if associated inventory item is not functional 674 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 675 { 676 return "Critical"; 677 } 678 679 // If current health in JSON object is already Warning, return that. This 680 // should override the sensor status, which might be less severe. 681 if (currentHealth == "Warning") 682 { 683 return "Warning"; 684 } 685 686 if (success) 687 { 688 // Check if sensor has warning threshold alarm 689 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) || 690 (warningAlarmLow != nullptr && *warningAlarmLow)) 691 { 692 return "Warning"; 693 } 694 } 695 696 return "OK"; 697 } 698 699 inline void setLedState(nlohmann::json& sensorJson, 700 const InventoryItem* inventoryItem) 701 { 702 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 703 { 704 switch (inventoryItem->ledState) 705 { 706 case LedState::OFF: 707 sensorJson["IndicatorLED"] = "Off"; 708 break; 709 case LedState::ON: 710 sensorJson["IndicatorLED"] = "Lit"; 711 break; 712 case LedState::BLINK: 713 sensorJson["IndicatorLED"] = "Blinking"; 714 break; 715 case LedState::UNKNOWN: 716 break; 717 } 718 } 719 } 720 721 /** 722 * @brief Builds a json sensor representation of a sensor. 723 * @param sensorName The name of the sensor to be built 724 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 725 * build 726 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor 727 * @param propertiesDict A dictionary of the properties to build the sensor 728 * from. 729 * @param sensorJson The json object to fill 730 * @param inventoryItem D-Bus inventory item associated with the sensor. Will 731 * be nullptr if no associated inventory item was found. 732 */ 733 inline void objectPropertiesToJson( 734 std::string_view sensorName, std::string_view sensorType, 735 std::string_view chassisSubNode, 736 const dbus::utility::DBusPropertiesMap& propertiesDict, 737 nlohmann::json& sensorJson, InventoryItem* inventoryItem) 738 { 739 if (chassisSubNode == sensors::node::sensors) 740 { 741 std::string subNodeEscaped(sensorType); 742 auto remove = std::ranges::remove(subNodeEscaped, '_'); 743 subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end()); 744 745 // For sensors in SensorCollection we set Id instead of MemberId, 746 // including power sensors. 747 subNodeEscaped += '_'; 748 subNodeEscaped += sensorName; 749 sensorJson["Id"] = std::move(subNodeEscaped); 750 751 std::string sensorNameEs(sensorName); 752 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 753 sensorJson["Name"] = std::move(sensorNameEs); 754 } 755 else if (sensorType != "power") 756 { 757 // Set MemberId and Name for non-power sensors. For PowerSupplies and 758 // PowerControl, those properties have more general values because 759 // multiple sensors can be stored in the same JSON object. 760 std::string sensorNameEs(sensorName); 761 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 762 sensorJson["Name"] = std::move(sensorNameEs); 763 } 764 765 const bool* checkAvailable = nullptr; 766 bool available = true; 767 const bool success = sdbusplus::unpackPropertiesNoThrow( 768 dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available", 769 checkAvailable); 770 if (!success) 771 { 772 messages::internalError(); 773 } 774 if (checkAvailable != nullptr) 775 { 776 available = *checkAvailable; 777 } 778 779 sensorJson["Status"]["State"] = getState(inventoryItem, available); 780 sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict, 781 inventoryItem); 782 783 // Parameter to set to override the type we get from dbus, and force it to 784 // int, regardless of what is available. This is used for schemas like fan, 785 // that require integers, not floats. 786 bool forceToInt = false; 787 788 nlohmann::json::json_pointer unit("/Reading"); 789 if (chassisSubNode == sensors::node::sensors) 790 { 791 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor"; 792 793 sensor::ReadingType readingType = sensors::toReadingType(sensorType); 794 if (readingType == sensor::ReadingType::Invalid) 795 { 796 BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}", 797 sensorType); 798 } 799 else 800 { 801 sensorJson["ReadingType"] = readingType; 802 } 803 804 std::string_view readingUnits = sensors::toReadingUnits(sensorType); 805 if (readingUnits.empty()) 806 { 807 BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}", 808 sensorType); 809 } 810 else 811 { 812 sensorJson["ReadingUnits"] = readingUnits; 813 } 814 } 815 else if (sensorType == "temperature") 816 { 817 unit = "/ReadingCelsius"_json_pointer; 818 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 819 // TODO(ed) Documentation says that path should be type fan_tach, 820 // implementation seems to implement fan 821 } 822 else if (sensorType == "fan" || sensorType == "fan_tach") 823 { 824 unit = "/Reading"_json_pointer; 825 sensorJson["ReadingUnits"] = "RPM"; 826 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 827 setLedState(sensorJson, inventoryItem); 828 forceToInt = true; 829 } 830 else if (sensorType == "fan_pwm") 831 { 832 unit = "/Reading"_json_pointer; 833 sensorJson["ReadingUnits"] = "Percent"; 834 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 835 setLedState(sensorJson, inventoryItem); 836 forceToInt = true; 837 } 838 else if (sensorType == "voltage") 839 { 840 unit = "/ReadingVolts"_json_pointer; 841 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 842 } 843 else if (sensorType == "power") 844 { 845 std::string lower; 846 std::ranges::transform(sensorName, std::back_inserter(lower), 847 bmcweb::asciiToLower); 848 if (lower == "total_power") 849 { 850 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 851 // Put multiple "sensors" into a single PowerControl, so have 852 // generic names for MemberId and Name. Follows Redfish mockup. 853 sensorJson["MemberId"] = "0"; 854 sensorJson["Name"] = "Chassis Power Control"; 855 unit = "/PowerConsumedWatts"_json_pointer; 856 } 857 else if (lower.find("input") != std::string::npos) 858 { 859 unit = "/PowerInputWatts"_json_pointer; 860 } 861 else 862 { 863 unit = "/PowerOutputWatts"_json_pointer; 864 } 865 } 866 else 867 { 868 BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName); 869 return; 870 } 871 // Map of dbus interface name, dbus property name and redfish property_name 872 std::vector< 873 std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 874 properties; 875 properties.reserve(7); 876 877 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 878 879 if (chassisSubNode == sensors::node::sensors) 880 { 881 properties.emplace_back( 882 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 883 "/Thresholds/UpperCaution/Reading"_json_pointer); 884 properties.emplace_back( 885 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 886 "/Thresholds/LowerCaution/Reading"_json_pointer); 887 properties.emplace_back( 888 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 889 "/Thresholds/UpperCritical/Reading"_json_pointer); 890 properties.emplace_back( 891 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 892 "/Thresholds/LowerCritical/Reading"_json_pointer); 893 } 894 else if (sensorType != "power") 895 { 896 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 897 "WarningHigh", 898 "/UpperThresholdNonCritical"_json_pointer); 899 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 900 "WarningLow", 901 "/LowerThresholdNonCritical"_json_pointer); 902 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 903 "CriticalHigh", 904 "/UpperThresholdCritical"_json_pointer); 905 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 906 "CriticalLow", 907 "/LowerThresholdCritical"_json_pointer); 908 } 909 910 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 911 912 if (chassisSubNode == sensors::node::sensors) 913 { 914 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 915 "/ReadingRangeMin"_json_pointer); 916 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 917 "/ReadingRangeMax"_json_pointer); 918 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy", 919 "Accuracy", "/Accuracy"_json_pointer); 920 } 921 else if (sensorType == "temperature") 922 { 923 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 924 "/MinReadingRangeTemp"_json_pointer); 925 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 926 "/MaxReadingRangeTemp"_json_pointer); 927 } 928 else if (sensorType != "power") 929 { 930 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 931 "/MinReadingRange"_json_pointer); 932 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 933 "/MaxReadingRange"_json_pointer); 934 } 935 936 for (const std::tuple<const char*, const char*, 937 nlohmann::json::json_pointer>& p : properties) 938 { 939 for (const auto& [valueName, valueVariant] : propertiesDict) 940 { 941 if (valueName != std::get<1>(p)) 942 { 943 continue; 944 } 945 946 // The property we want to set may be nested json, so use 947 // a json_pointer for easy indexing into the json structure. 948 const nlohmann::json::json_pointer& key = std::get<2>(p); 949 950 const double* doubleValue = std::get_if<double>(&valueVariant); 951 if (doubleValue == nullptr) 952 { 953 BMCWEB_LOG_ERROR("Got value interface that wasn't double"); 954 continue; 955 } 956 if (!std::isfinite(*doubleValue)) 957 { 958 if (valueName == "Value") 959 { 960 // Readings are allowed to be NAN for unavailable; coerce 961 // them to null in the json response. 962 sensorJson[key] = nullptr; 963 continue; 964 } 965 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}", 966 valueName, *doubleValue); 967 continue; 968 } 969 if (forceToInt) 970 { 971 sensorJson[key] = static_cast<int64_t>(*doubleValue); 972 } 973 else 974 { 975 sensorJson[key] = *doubleValue; 976 } 977 } 978 } 979 } 980 981 /** 982 * @brief Builds a json sensor representation of a sensor. 983 * @param sensorName The name of the sensor to be built 984 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 985 * build 986 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor 987 * @param interfacesDict A dictionary of the interfaces and properties of said 988 * interfaces to be built from 989 * @param sensorJson The json object to fill 990 * @param inventoryItem D-Bus inventory item associated with the sensor. Will 991 * be nullptr if no associated inventory item was found. 992 */ 993 inline void objectInterfacesToJson( 994 const std::string& sensorName, const std::string& sensorType, 995 const std::string& chassisSubNode, 996 const dbus::utility::DBusInterfacesMap& interfacesDict, 997 nlohmann::json& sensorJson, InventoryItem* inventoryItem) 998 { 999 for (const auto& [interface, valuesDict] : interfacesDict) 1000 { 1001 objectPropertiesToJson(sensorName, sensorType, chassisSubNode, 1002 valuesDict, sensorJson, inventoryItem); 1003 } 1004 BMCWEB_LOG_DEBUG("Added sensor {}", sensorName); 1005 } 1006 1007 inline void populateFanRedundancy( 1008 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 1009 { 1010 constexpr std::array<std::string_view, 1> interfaces = { 1011 "xyz.openbmc_project.Control.FanRedundancy"}; 1012 dbus::utility::getSubTree( 1013 "/xyz/openbmc_project/control", 2, interfaces, 1014 [sensorsAsyncResp]( 1015 const boost::system::error_code& ec, 1016 const dbus::utility::MapperGetSubTreeResponse& resp) { 1017 if (ec) 1018 { 1019 return; // don't have to have this interface 1020 } 1021 for (const std::pair<std::string, dbus::utility::MapperServiceMap>& 1022 pathPair : resp) 1023 { 1024 const std::string& path = pathPair.first; 1025 const dbus::utility::MapperServiceMap& objDict = pathPair.second; 1026 if (objDict.empty()) 1027 { 1028 continue; // this should be impossible 1029 } 1030 1031 const std::string& owner = objDict.begin()->first; 1032 dbus::utility::getAssociationEndPoints( 1033 path + "/chassis", 1034 [path, owner, sensorsAsyncResp]( 1035 const boost::system::error_code& ec2, 1036 const dbus::utility::MapperEndPoints& endpoints) { 1037 if (ec2) 1038 { 1039 return; // if they don't have an association we 1040 // can't tell what chassis is 1041 } 1042 auto found = std::ranges::find_if( 1043 endpoints, [sensorsAsyncResp](const std::string& entry) { 1044 return entry.find(sensorsAsyncResp->chassisId) != 1045 std::string::npos; 1046 }); 1047 1048 if (found == endpoints.end()) 1049 { 1050 return; 1051 } 1052 sdbusplus::asio::getAllProperties( 1053 *crow::connections::systemBus, owner, path, 1054 "xyz.openbmc_project.Control.FanRedundancy", 1055 [path, sensorsAsyncResp]( 1056 const boost::system::error_code& ec3, 1057 const dbus::utility::DBusPropertiesMap& ret) { 1058 if (ec3) 1059 { 1060 return; // don't have to have this 1061 // interface 1062 } 1063 1064 const uint8_t* allowedFailures = nullptr; 1065 const std::vector<std::string>* collection = nullptr; 1066 const std::string* status = nullptr; 1067 1068 const bool success = sdbusplus::unpackPropertiesNoThrow( 1069 dbus_utils::UnpackErrorPrinter(), ret, 1070 "AllowedFailures", allowedFailures, "Collection", 1071 collection, "Status", status); 1072 1073 if (!success) 1074 { 1075 messages::internalError( 1076 sensorsAsyncResp->asyncResp->res); 1077 return; 1078 } 1079 1080 if (allowedFailures == nullptr || collection == nullptr || 1081 status == nullptr) 1082 { 1083 BMCWEB_LOG_ERROR("Invalid redundancy interface"); 1084 messages::internalError( 1085 sensorsAsyncResp->asyncResp->res); 1086 return; 1087 } 1088 1089 sdbusplus::message::object_path objectPath(path); 1090 std::string name = objectPath.filename(); 1091 if (name.empty()) 1092 { 1093 // this should be impossible 1094 messages::internalError( 1095 sensorsAsyncResp->asyncResp->res); 1096 return; 1097 } 1098 std::ranges::replace(name, '_', ' '); 1099 1100 std::string health; 1101 1102 if (status->ends_with("Full")) 1103 { 1104 health = "OK"; 1105 } 1106 else if (status->ends_with("Degraded")) 1107 { 1108 health = "Warning"; 1109 } 1110 else 1111 { 1112 health = "Critical"; 1113 } 1114 nlohmann::json::array_t redfishCollection; 1115 const auto& fanRedfish = 1116 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"]; 1117 for (const std::string& item : *collection) 1118 { 1119 sdbusplus::message::object_path itemPath(item); 1120 std::string itemName = itemPath.filename(); 1121 if (itemName.empty()) 1122 { 1123 continue; 1124 } 1125 /* 1126 todo(ed): merge patch that fixes the names 1127 std::replace(itemName.begin(), 1128 itemName.end(), '_', ' ');*/ 1129 auto schemaItem = std::ranges::find_if( 1130 fanRedfish, [itemName](const nlohmann::json& fan) { 1131 return fan["Name"] == itemName; 1132 }); 1133 if (schemaItem != fanRedfish.end()) 1134 { 1135 nlohmann::json::object_t collectionId; 1136 collectionId["@odata.id"] = 1137 (*schemaItem)["@odata.id"]; 1138 redfishCollection.emplace_back( 1139 std::move(collectionId)); 1140 } 1141 else 1142 { 1143 BMCWEB_LOG_ERROR("failed to find fan in schema"); 1144 messages::internalError( 1145 sensorsAsyncResp->asyncResp->res); 1146 return; 1147 } 1148 } 1149 1150 size_t minNumNeeded = collection->empty() 1151 ? 0 1152 : collection->size() - 1153 *allowedFailures; 1154 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res 1155 .jsonValue["Redundancy"]; 1156 1157 nlohmann::json::object_t redundancy; 1158 boost::urls::url url = 1159 boost::urls::format("/redfish/v1/Chassis/{}/{}", 1160 sensorsAsyncResp->chassisId, 1161 sensorsAsyncResp->chassisSubNode); 1162 url.set_fragment(("/Redundancy"_json_pointer / jResp.size()) 1163 .to_string()); 1164 redundancy["@odata.id"] = std::move(url); 1165 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy"; 1166 redundancy["MinNumNeeded"] = minNumNeeded; 1167 redundancy["Mode"] = "N+m"; 1168 redundancy["Name"] = name; 1169 redundancy["RedundancySet"] = redfishCollection; 1170 redundancy["Status"]["Health"] = health; 1171 redundancy["Status"]["State"] = "Enabled"; 1172 1173 jResp.emplace_back(std::move(redundancy)); 1174 }); 1175 }); 1176 } 1177 }); 1178 } 1179 1180 inline void 1181 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 1182 { 1183 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 1184 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 1185 if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 1186 { 1187 sensorHeaders = {"Voltages", "PowerSupplies"}; 1188 } 1189 for (const std::string& sensorGroup : sensorHeaders) 1190 { 1191 nlohmann::json::iterator entry = response.find(sensorGroup); 1192 if (entry != response.end()) 1193 { 1194 std::sort(entry->begin(), entry->end(), 1195 [](const nlohmann::json& c1, const nlohmann::json& c2) { 1196 return c1["Name"] < c2["Name"]; 1197 }); 1198 1199 // add the index counts to the end of each entry 1200 size_t count = 0; 1201 for (nlohmann::json& sensorJson : *entry) 1202 { 1203 nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 1204 if (odata == sensorJson.end()) 1205 { 1206 continue; 1207 } 1208 std::string* value = odata->get_ptr<std::string*>(); 1209 if (value != nullptr) 1210 { 1211 *value += "/" + std::to_string(count); 1212 sensorJson["MemberId"] = std::to_string(count); 1213 count++; 1214 sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 1215 } 1216 } 1217 } 1218 } 1219 } 1220 1221 /** 1222 * @brief Finds the inventory item with the specified object path. 1223 * @param inventoryItems D-Bus inventory items associated with sensors. 1224 * @param invItemObjPath D-Bus object path of inventory item. 1225 * @return Inventory item within vector, or nullptr if no match found. 1226 */ 1227 inline InventoryItem* findInventoryItem( 1228 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1229 const std::string& invItemObjPath) 1230 { 1231 for (InventoryItem& inventoryItem : *inventoryItems) 1232 { 1233 if (inventoryItem.objectPath == invItemObjPath) 1234 { 1235 return &inventoryItem; 1236 } 1237 } 1238 return nullptr; 1239 } 1240 1241 /** 1242 * @brief Finds the inventory item associated with the specified sensor. 1243 * @param inventoryItems D-Bus inventory items associated with sensors. 1244 * @param sensorObjPath D-Bus object path of sensor. 1245 * @return Inventory item within vector, or nullptr if no match found. 1246 */ 1247 inline InventoryItem* findInventoryItemForSensor( 1248 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1249 const std::string& sensorObjPath) 1250 { 1251 for (InventoryItem& inventoryItem : *inventoryItems) 1252 { 1253 if (inventoryItem.sensors.contains(sensorObjPath)) 1254 { 1255 return &inventoryItem; 1256 } 1257 } 1258 return nullptr; 1259 } 1260 1261 /** 1262 * @brief Finds the inventory item associated with the specified led path. 1263 * @param inventoryItems D-Bus inventory items associated with sensors. 1264 * @param ledObjPath D-Bus object path of led. 1265 * @return Inventory item within vector, or nullptr if no match found. 1266 */ 1267 inline InventoryItem* 1268 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1269 const std::string& ledObjPath) 1270 { 1271 for (InventoryItem& inventoryItem : inventoryItems) 1272 { 1273 if (inventoryItem.ledObjectPath == ledObjPath) 1274 { 1275 return &inventoryItem; 1276 } 1277 } 1278 return nullptr; 1279 } 1280 1281 /** 1282 * @brief Adds inventory item and associated sensor to specified vector. 1283 * 1284 * Adds a new InventoryItem to the vector if necessary. Searches for an 1285 * existing InventoryItem with the specified object path. If not found, one is 1286 * added to the vector. 1287 * 1288 * Next, the specified sensor is added to the set of sensors associated with the 1289 * InventoryItem. 1290 * 1291 * @param inventoryItems D-Bus inventory items associated with sensors. 1292 * @param invItemObjPath D-Bus object path of inventory item. 1293 * @param sensorObjPath D-Bus object path of sensor 1294 */ 1295 inline void addInventoryItem( 1296 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1297 const std::string& invItemObjPath, const std::string& sensorObjPath) 1298 { 1299 // Look for inventory item in vector 1300 InventoryItem* inventoryItem = findInventoryItem(inventoryItems, 1301 invItemObjPath); 1302 1303 // If inventory item doesn't exist in vector, add it 1304 if (inventoryItem == nullptr) 1305 { 1306 inventoryItems->emplace_back(invItemObjPath); 1307 inventoryItem = &(inventoryItems->back()); 1308 } 1309 1310 // Add sensor to set of sensors associated with inventory item 1311 inventoryItem->sensors.emplace(sensorObjPath); 1312 } 1313 1314 /** 1315 * @brief Stores D-Bus data in the specified inventory item. 1316 * 1317 * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1318 * specified InventoryItem. 1319 * 1320 * This data is later used to provide sensor property values in the JSON 1321 * response. 1322 * 1323 * @param inventoryItem Inventory item where data will be stored. 1324 * @param interfacesDict Map containing D-Bus interfaces and their properties 1325 * for the specified inventory item. 1326 */ 1327 inline void storeInventoryItemData( 1328 InventoryItem& inventoryItem, 1329 const dbus::utility::DBusInterfacesMap& interfacesDict) 1330 { 1331 // Get properties from Inventory.Item interface 1332 1333 for (const auto& [interface, values] : interfacesDict) 1334 { 1335 if (interface == "xyz.openbmc_project.Inventory.Item") 1336 { 1337 for (const auto& [name, dbusValue] : values) 1338 { 1339 if (name == "Present") 1340 { 1341 const bool* value = std::get_if<bool>(&dbusValue); 1342 if (value != nullptr) 1343 { 1344 inventoryItem.isPresent = *value; 1345 } 1346 } 1347 } 1348 } 1349 // Check if Inventory.Item.PowerSupply interface is present 1350 1351 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 1352 { 1353 inventoryItem.isPowerSupply = true; 1354 } 1355 1356 // Get properties from Inventory.Decorator.Asset interface 1357 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1358 { 1359 for (const auto& [name, dbusValue] : values) 1360 { 1361 if (name == "Manufacturer") 1362 { 1363 const std::string* value = 1364 std::get_if<std::string>(&dbusValue); 1365 if (value != nullptr) 1366 { 1367 inventoryItem.manufacturer = *value; 1368 } 1369 } 1370 if (name == "Model") 1371 { 1372 const std::string* value = 1373 std::get_if<std::string>(&dbusValue); 1374 if (value != nullptr) 1375 { 1376 inventoryItem.model = *value; 1377 } 1378 } 1379 if (name == "SerialNumber") 1380 { 1381 const std::string* value = 1382 std::get_if<std::string>(&dbusValue); 1383 if (value != nullptr) 1384 { 1385 inventoryItem.serialNumber = *value; 1386 } 1387 } 1388 if (name == "PartNumber") 1389 { 1390 const std::string* value = 1391 std::get_if<std::string>(&dbusValue); 1392 if (value != nullptr) 1393 { 1394 inventoryItem.partNumber = *value; 1395 } 1396 } 1397 } 1398 } 1399 1400 if (interface == 1401 "xyz.openbmc_project.State.Decorator.OperationalStatus") 1402 { 1403 for (const auto& [name, dbusValue] : values) 1404 { 1405 if (name == "Functional") 1406 { 1407 const bool* value = std::get_if<bool>(&dbusValue); 1408 if (value != nullptr) 1409 { 1410 inventoryItem.isFunctional = *value; 1411 } 1412 } 1413 } 1414 } 1415 } 1416 } 1417 1418 /** 1419 * @brief Gets D-Bus data for inventory items associated with sensors. 1420 * 1421 * Uses the specified connections (services) to obtain D-Bus data for inventory 1422 * items associated with sensors. Stores the resulting data in the 1423 * inventoryItems vector. 1424 * 1425 * This data is later used to provide sensor property values in the JSON 1426 * response. 1427 * 1428 * Finds the inventory item data asynchronously. Invokes callback when data has 1429 * been obtained. 1430 * 1431 * The callback must have the following signature: 1432 * @code 1433 * callback(void) 1434 * @endcode 1435 * 1436 * This function is called recursively, obtaining data asynchronously from one 1437 * connection in each call. This ensures the callback is not invoked until the 1438 * last asynchronous function has completed. 1439 * 1440 * @param sensorsAsyncResp Pointer to object holding response data. 1441 * @param inventoryItems D-Bus inventory items associated with sensors. 1442 * @param invConnections Connections that provide data for the inventory items. 1443 * implements ObjectManager. 1444 * @param callback Callback to invoke when inventory data has been obtained. 1445 * @param invConnectionsIndex Current index in invConnections. Only specified 1446 * in recursive calls to this function. 1447 */ 1448 template <typename Callback> 1449 static void getInventoryItemsData( 1450 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1451 std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1452 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback, 1453 size_t invConnectionsIndex = 0) 1454 { 1455 BMCWEB_LOG_DEBUG("getInventoryItemsData enter"); 1456 1457 // If no more connections left, call callback 1458 if (invConnectionsIndex >= invConnections->size()) 1459 { 1460 callback(); 1461 BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); 1462 return; 1463 } 1464 1465 // Get inventory item data from current connection 1466 auto it = invConnections->begin(); 1467 std::advance(it, invConnectionsIndex); 1468 if (it != invConnections->end()) 1469 { 1470 const std::string& invConnection = *it; 1471 1472 // Get all object paths and their interfaces for current connection 1473 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory"); 1474 dbus::utility::getManagedObjects( 1475 invConnection, path, 1476 [sensorsAsyncResp, inventoryItems, invConnections, 1477 callback{std::forward<Callback>(callback)}, invConnectionsIndex]( 1478 const boost::system::error_code& ec, 1479 const dbus::utility::ManagedObjectType& resp) { 1480 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter"); 1481 if (ec) 1482 { 1483 BMCWEB_LOG_ERROR( 1484 "getInventoryItemsData respHandler DBus error {}", ec); 1485 messages::internalError(sensorsAsyncResp->asyncResp->res); 1486 return; 1487 } 1488 1489 // Loop through returned object paths 1490 for (const auto& objDictEntry : resp) 1491 { 1492 const std::string& objPath = 1493 static_cast<const std::string&>(objDictEntry.first); 1494 1495 // If this object path is one of the specified inventory items 1496 InventoryItem* inventoryItem = findInventoryItem(inventoryItems, 1497 objPath); 1498 if (inventoryItem != nullptr) 1499 { 1500 // Store inventory data in InventoryItem 1501 storeInventoryItemData(*inventoryItem, objDictEntry.second); 1502 } 1503 } 1504 1505 // Recurse to get inventory item data from next connection 1506 getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1507 invConnections, std::move(callback), 1508 invConnectionsIndex + 1); 1509 1510 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit"); 1511 }); 1512 } 1513 1514 BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); 1515 } 1516 1517 /** 1518 * @brief Gets connections that provide D-Bus data for inventory items. 1519 * 1520 * Gets the D-Bus connections (services) that provide data for the inventory 1521 * items that are associated with sensors. 1522 * 1523 * Finds the connections asynchronously. Invokes callback when information has 1524 * been obtained. 1525 * 1526 * The callback must have the following signature: 1527 * @code 1528 * callback(std::shared_ptr<std::set<std::string>> invConnections) 1529 * @endcode 1530 * 1531 * @param sensorsAsyncResp Pointer to object holding response data. 1532 * @param inventoryItems D-Bus inventory items associated with sensors. 1533 * @param callback Callback to invoke when connections have been obtained. 1534 */ 1535 template <typename Callback> 1536 static void getInventoryItemsConnections( 1537 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1538 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1539 Callback&& callback) 1540 { 1541 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter"); 1542 1543 const std::string path = "/xyz/openbmc_project/inventory"; 1544 constexpr std::array<std::string_view, 4> interfaces = { 1545 "xyz.openbmc_project.Inventory.Item", 1546 "xyz.openbmc_project.Inventory.Item.PowerSupply", 1547 "xyz.openbmc_project.Inventory.Decorator.Asset", 1548 "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 1549 1550 // Make call to ObjectMapper to find all inventory items 1551 dbus::utility::getSubTree( 1552 path, 0, interfaces, 1553 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1554 inventoryItems]( 1555 const boost::system::error_code& ec, 1556 const dbus::utility::MapperGetSubTreeResponse& subtree) { 1557 // Response handler for parsing output from GetSubTree 1558 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter"); 1559 if (ec) 1560 { 1561 messages::internalError(sensorsAsyncResp->asyncResp->res); 1562 BMCWEB_LOG_ERROR( 1563 "getInventoryItemsConnections respHandler DBus error {}", ec); 1564 return; 1565 } 1566 1567 // Make unique list of connections for desired inventory items 1568 std::shared_ptr<std::set<std::string>> invConnections = 1569 std::make_shared<std::set<std::string>>(); 1570 1571 // Loop through objects from GetSubTree 1572 for (const std::pair< 1573 std::string, 1574 std::vector<std::pair<std::string, std::vector<std::string>>>>& 1575 object : subtree) 1576 { 1577 // Check if object path is one of the specified inventory items 1578 const std::string& objPath = object.first; 1579 if (findInventoryItem(inventoryItems, objPath) != nullptr) 1580 { 1581 // Store all connections to inventory item 1582 for (const std::pair<std::string, std::vector<std::string>>& 1583 objData : object.second) 1584 { 1585 const std::string& invConnection = objData.first; 1586 invConnections->insert(invConnection); 1587 } 1588 } 1589 } 1590 1591 callback(invConnections); 1592 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit"); 1593 }); 1594 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit"); 1595 } 1596 1597 /** 1598 * @brief Gets associations from sensors to inventory items. 1599 * 1600 * Looks for ObjectMapper associations from the specified sensors to related 1601 * inventory items. Then finds the associations from those inventory items to 1602 * their LEDs, if any. 1603 * 1604 * Finds the inventory items asynchronously. Invokes callback when information 1605 * has been obtained. 1606 * 1607 * The callback must have the following signature: 1608 * @code 1609 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 1610 * @endcode 1611 * 1612 * @param sensorsAsyncResp Pointer to object holding response data. 1613 * @param sensorNames All sensors within the current chassis. 1614 * implements ObjectManager. 1615 * @param callback Callback to invoke when inventory items have been obtained. 1616 */ 1617 template <typename Callback> 1618 static void getInventoryItemAssociations( 1619 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1620 const std::shared_ptr<std::set<std::string>>& sensorNames, 1621 Callback&& callback) 1622 { 1623 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter"); 1624 1625 // Call GetManagedObjects on the ObjectMapper to get all associations 1626 sdbusplus::message::object_path path("/"); 1627 dbus::utility::getManagedObjects( 1628 "xyz.openbmc_project.ObjectMapper", path, 1629 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1630 sensorNames](const boost::system::error_code& ec, 1631 const dbus::utility::ManagedObjectType& resp) { 1632 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter"); 1633 if (ec) 1634 { 1635 BMCWEB_LOG_ERROR( 1636 "getInventoryItemAssociations respHandler DBus error {}", ec); 1637 messages::internalError(sensorsAsyncResp->asyncResp->res); 1638 return; 1639 } 1640 1641 // Create vector to hold list of inventory items 1642 std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1643 std::make_shared<std::vector<InventoryItem>>(); 1644 1645 // Loop through returned object paths 1646 std::string sensorAssocPath; 1647 sensorAssocPath.reserve(128); // avoid memory allocations 1648 for (const auto& objDictEntry : resp) 1649 { 1650 const std::string& objPath = 1651 static_cast<const std::string&>(objDictEntry.first); 1652 1653 // If path is inventory association for one of the specified sensors 1654 for (const std::string& sensorName : *sensorNames) 1655 { 1656 sensorAssocPath = sensorName; 1657 sensorAssocPath += "/inventory"; 1658 if (objPath == sensorAssocPath) 1659 { 1660 // Get Association interface for object path 1661 for (const auto& [interface, values] : objDictEntry.second) 1662 { 1663 if (interface == "xyz.openbmc_project.Association") 1664 { 1665 for (const auto& [valueName, value] : values) 1666 { 1667 if (valueName == "endpoints") 1668 { 1669 const std::vector<std::string>* endpoints = 1670 std::get_if<std::vector<std::string>>( 1671 &value); 1672 if ((endpoints != nullptr) && 1673 !endpoints->empty()) 1674 { 1675 // Add inventory item to vector 1676 const std::string& invItemPath = 1677 endpoints->front(); 1678 addInventoryItem(inventoryItems, 1679 invItemPath, 1680 sensorName); 1681 } 1682 } 1683 } 1684 } 1685 } 1686 break; 1687 } 1688 } 1689 } 1690 1691 // Now loop through the returned object paths again, this time to 1692 // find the leds associated with the inventory items we just found 1693 std::string inventoryAssocPath; 1694 inventoryAssocPath.reserve(128); // avoid memory allocations 1695 for (const auto& objDictEntry : resp) 1696 { 1697 const std::string& objPath = 1698 static_cast<const std::string&>(objDictEntry.first); 1699 1700 for (InventoryItem& inventoryItem : *inventoryItems) 1701 { 1702 inventoryAssocPath = inventoryItem.objectPath; 1703 inventoryAssocPath += "/leds"; 1704 if (objPath == inventoryAssocPath) 1705 { 1706 for (const auto& [interface, values] : objDictEntry.second) 1707 { 1708 if (interface == "xyz.openbmc_project.Association") 1709 { 1710 for (const auto& [valueName, value] : values) 1711 { 1712 if (valueName == "endpoints") 1713 { 1714 const std::vector<std::string>* endpoints = 1715 std::get_if<std::vector<std::string>>( 1716 &value); 1717 if ((endpoints != nullptr) && 1718 !endpoints->empty()) 1719 { 1720 // Add inventory item to vector 1721 // Store LED path in inventory item 1722 const std::string& ledPath = 1723 endpoints->front(); 1724 inventoryItem.ledObjectPath = ledPath; 1725 } 1726 } 1727 } 1728 } 1729 } 1730 1731 break; 1732 } 1733 } 1734 } 1735 callback(inventoryItems); 1736 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit"); 1737 }); 1738 1739 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit"); 1740 } 1741 1742 /** 1743 * @brief Gets D-Bus data for inventory item leds associated with sensors. 1744 * 1745 * Uses the specified connections (services) to obtain D-Bus data for inventory 1746 * item leds associated with sensors. Stores the resulting data in the 1747 * inventoryItems vector. 1748 * 1749 * This data is later used to provide sensor property values in the JSON 1750 * response. 1751 * 1752 * Finds the inventory item led data asynchronously. Invokes callback when data 1753 * has been obtained. 1754 * 1755 * The callback must have the following signature: 1756 * @code 1757 * callback() 1758 * @endcode 1759 * 1760 * This function is called recursively, obtaining data asynchronously from one 1761 * connection in each call. This ensures the callback is not invoked until the 1762 * last asynchronous function has completed. 1763 * 1764 * @param sensorsAsyncResp Pointer to object holding response data. 1765 * @param inventoryItems D-Bus inventory items associated with sensors. 1766 * @param ledConnections Connections that provide data for the inventory leds. 1767 * @param callback Callback to invoke when inventory data has been obtained. 1768 * @param ledConnectionsIndex Current index in ledConnections. Only specified 1769 * in recursive calls to this function. 1770 */ 1771 template <typename Callback> 1772 void getInventoryLedData( 1773 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1774 std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1775 std::shared_ptr<std::map<std::string, std::string>> ledConnections, 1776 Callback&& callback, size_t ledConnectionsIndex = 0) 1777 { 1778 BMCWEB_LOG_DEBUG("getInventoryLedData enter"); 1779 1780 // If no more connections left, call callback 1781 if (ledConnectionsIndex >= ledConnections->size()) 1782 { 1783 callback(); 1784 BMCWEB_LOG_DEBUG("getInventoryLedData exit"); 1785 return; 1786 } 1787 1788 // Get inventory item data from current connection 1789 auto it = ledConnections->begin(); 1790 std::advance(it, ledConnectionsIndex); 1791 if (it != ledConnections->end()) 1792 { 1793 const std::string& ledPath = (*it).first; 1794 const std::string& ledConnection = (*it).second; 1795 // Response handler for Get State property 1796 auto respHandler = 1797 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1798 callback{std::forward<Callback>(callback)}, ledConnectionsIndex]( 1799 const boost::system::error_code& ec, const std::string& state) { 1800 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter"); 1801 if (ec) 1802 { 1803 BMCWEB_LOG_ERROR( 1804 "getInventoryLedData respHandler DBus error {}", ec); 1805 messages::internalError(sensorsAsyncResp->asyncResp->res); 1806 return; 1807 } 1808 1809 BMCWEB_LOG_DEBUG("Led state: {}", state); 1810 // Find inventory item with this LED object path 1811 InventoryItem* inventoryItem = 1812 findInventoryItemForLed(*inventoryItems, ledPath); 1813 if (inventoryItem != nullptr) 1814 { 1815 // Store LED state in InventoryItem 1816 if (state.ends_with("On")) 1817 { 1818 inventoryItem->ledState = LedState::ON; 1819 } 1820 else if (state.ends_with("Blink")) 1821 { 1822 inventoryItem->ledState = LedState::BLINK; 1823 } 1824 else if (state.ends_with("Off")) 1825 { 1826 inventoryItem->ledState = LedState::OFF; 1827 } 1828 else 1829 { 1830 inventoryItem->ledState = LedState::UNKNOWN; 1831 } 1832 } 1833 1834 // Recurse to get LED data from next connection 1835 getInventoryLedData(sensorsAsyncResp, inventoryItems, 1836 ledConnections, std::move(callback), 1837 ledConnectionsIndex + 1); 1838 1839 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit"); 1840 }; 1841 1842 // Get the State property for the current LED 1843 sdbusplus::asio::getProperty<std::string>( 1844 *crow::connections::systemBus, ledConnection, ledPath, 1845 "xyz.openbmc_project.Led.Physical", "State", 1846 std::move(respHandler)); 1847 } 1848 1849 BMCWEB_LOG_DEBUG("getInventoryLedData exit"); 1850 } 1851 1852 /** 1853 * @brief Gets LED data for LEDs associated with given inventory items. 1854 * 1855 * Gets the D-Bus connections (services) that provide LED data for the LEDs 1856 * associated with the specified inventory items. Then gets the LED data from 1857 * each connection and stores it in the inventory item. 1858 * 1859 * This data is later used to provide sensor property values in the JSON 1860 * response. 1861 * 1862 * Finds the LED data asynchronously. Invokes callback when information has 1863 * been obtained. 1864 * 1865 * The callback must have the following signature: 1866 * @code 1867 * callback() 1868 * @endcode 1869 * 1870 * @param sensorsAsyncResp Pointer to object holding response data. 1871 * @param inventoryItems D-Bus inventory items associated with sensors. 1872 * @param callback Callback to invoke when inventory items have been obtained. 1873 */ 1874 template <typename Callback> 1875 void getInventoryLeds( 1876 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1877 std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1878 Callback&& callback) 1879 { 1880 BMCWEB_LOG_DEBUG("getInventoryLeds enter"); 1881 1882 const std::string path = "/xyz/openbmc_project"; 1883 constexpr std::array<std::string_view, 1> interfaces = { 1884 "xyz.openbmc_project.Led.Physical"}; 1885 1886 // Make call to ObjectMapper to find all inventory items 1887 dbus::utility::getSubTree( 1888 path, 0, interfaces, 1889 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1890 inventoryItems]( 1891 const boost::system::error_code& ec, 1892 const dbus::utility::MapperGetSubTreeResponse& subtree) { 1893 // Response handler for parsing output from GetSubTree 1894 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter"); 1895 if (ec) 1896 { 1897 messages::internalError(sensorsAsyncResp->asyncResp->res); 1898 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", ec); 1899 return; 1900 } 1901 1902 // Build map of LED object paths to connections 1903 std::shared_ptr<std::map<std::string, std::string>> ledConnections = 1904 std::make_shared<std::map<std::string, std::string>>(); 1905 1906 // Loop through objects from GetSubTree 1907 for (const std::pair< 1908 std::string, 1909 std::vector<std::pair<std::string, std::vector<std::string>>>>& 1910 object : subtree) 1911 { 1912 // Check if object path is LED for one of the specified inventory 1913 // items 1914 const std::string& ledPath = object.first; 1915 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 1916 { 1917 // Add mapping from ledPath to connection 1918 const std::string& connection = object.second.begin()->first; 1919 (*ledConnections)[ledPath] = connection; 1920 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, connection); 1921 } 1922 } 1923 1924 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 1925 std::move(callback)); 1926 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit"); 1927 }); 1928 BMCWEB_LOG_DEBUG("getInventoryLeds exit"); 1929 } 1930 1931 /** 1932 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 1933 * 1934 * Uses the specified connections (services) (currently assumes just one) to 1935 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 1936 * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 1937 * 1938 * This data is later used to provide sensor property values in the JSON 1939 * response. 1940 * 1941 * Finds the Power Supply Attributes data asynchronously. Invokes callback 1942 * when data has been obtained. 1943 * 1944 * The callback must have the following signature: 1945 * @code 1946 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 1947 * @endcode 1948 * 1949 * @param sensorsAsyncResp Pointer to object holding response data. 1950 * @param inventoryItems D-Bus inventory items associated with sensors. 1951 * @param psAttributesConnections Connections that provide data for the Power 1952 * Supply Attributes 1953 * @param callback Callback to invoke when data has been obtained. 1954 */ 1955 template <typename Callback> 1956 void getPowerSupplyAttributesData( 1957 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1958 std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1959 const std::map<std::string, std::string>& psAttributesConnections, 1960 Callback&& callback) 1961 { 1962 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter"); 1963 1964 if (psAttributesConnections.empty()) 1965 { 1966 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!"); 1967 callback(inventoryItems); 1968 return; 1969 } 1970 1971 // Assuming just one connection (service) for now 1972 auto it = psAttributesConnections.begin(); 1973 1974 const std::string& psAttributesPath = (*it).first; 1975 const std::string& psAttributesConnection = (*it).second; 1976 1977 // Response handler for Get DeratingFactor property 1978 auto respHandler = [sensorsAsyncResp, inventoryItems, 1979 callback{std::forward<Callback>(callback)}]( 1980 const boost::system::error_code& ec, 1981 const uint32_t value) { 1982 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter"); 1983 if (ec) 1984 { 1985 BMCWEB_LOG_ERROR( 1986 "getPowerSupplyAttributesData respHandler DBus error {}", ec); 1987 messages::internalError(sensorsAsyncResp->asyncResp->res); 1988 return; 1989 } 1990 1991 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value); 1992 // Store value in Power Supply Inventory Items 1993 for (InventoryItem& inventoryItem : *inventoryItems) 1994 { 1995 if (inventoryItem.isPowerSupply) 1996 { 1997 inventoryItem.powerSupplyEfficiencyPercent = 1998 static_cast<int>(value); 1999 } 2000 } 2001 2002 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit"); 2003 callback(inventoryItems); 2004 }; 2005 2006 // Get the DeratingFactor property for the PowerSupplyAttributes 2007 // Currently only property on the interface/only one we care about 2008 sdbusplus::asio::getProperty<uint32_t>( 2009 *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 2010 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 2011 std::move(respHandler)); 2012 2013 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit"); 2014 } 2015 2016 /** 2017 * @brief Gets the Power Supply Attributes such as EfficiencyPercent 2018 * 2019 * Gets the D-Bus connection (service) that provides Power Supply Attributes 2020 * data. Then gets the Power Supply Attributes data from the connection 2021 * (currently just assumes 1 connection) and stores the data in the inventory 2022 * item. 2023 * 2024 * This data is later used to provide sensor property values in the JSON 2025 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 2026 * 2027 * Finds the Power Supply Attributes data asynchronously. Invokes callback 2028 * when information has been obtained. 2029 * 2030 * The callback must have the following signature: 2031 * @code 2032 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2033 * @endcode 2034 * 2035 * @param sensorsAsyncResp Pointer to object holding response data. 2036 * @param inventoryItems D-Bus inventory items associated with sensors. 2037 * @param callback Callback to invoke when data has been obtained. 2038 */ 2039 template <typename Callback> 2040 void getPowerSupplyAttributes( 2041 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2042 std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2043 Callback&& callback) 2044 { 2045 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter"); 2046 2047 // Only need the power supply attributes when the Power Schema 2048 if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 2049 { 2050 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power"); 2051 callback(inventoryItems); 2052 return; 2053 } 2054 2055 constexpr std::array<std::string_view, 1> interfaces = { 2056 "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 2057 2058 // Make call to ObjectMapper to find the PowerSupplyAttributes service 2059 dbus::utility::getSubTree( 2060 "/xyz/openbmc_project", 0, interfaces, 2061 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 2062 inventoryItems]( 2063 const boost::system::error_code& ec, 2064 const dbus::utility::MapperGetSubTreeResponse& subtree) { 2065 // Response handler for parsing output from GetSubTree 2066 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter"); 2067 if (ec) 2068 { 2069 messages::internalError(sensorsAsyncResp->asyncResp->res); 2070 BMCWEB_LOG_ERROR( 2071 "getPowerSupplyAttributes respHandler DBus error {}", ec); 2072 return; 2073 } 2074 if (subtree.empty()) 2075 { 2076 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!"); 2077 callback(inventoryItems); 2078 return; 2079 } 2080 2081 // Currently we only support 1 power supply attribute, use this for 2082 // all the power supplies. Build map of object path to connection. 2083 // Assume just 1 connection and 1 path for now. 2084 std::map<std::string, std::string> psAttributesConnections; 2085 2086 if (subtree[0].first.empty() || subtree[0].second.empty()) 2087 { 2088 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); 2089 callback(inventoryItems); 2090 return; 2091 } 2092 2093 const std::string& psAttributesPath = subtree[0].first; 2094 const std::string& connection = subtree[0].second.begin()->first; 2095 2096 if (connection.empty()) 2097 { 2098 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); 2099 callback(inventoryItems); 2100 return; 2101 } 2102 2103 psAttributesConnections[psAttributesPath] = connection; 2104 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath, 2105 connection); 2106 2107 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 2108 psAttributesConnections, 2109 std::move(callback)); 2110 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit"); 2111 }); 2112 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit"); 2113 } 2114 2115 /** 2116 * @brief Gets inventory items associated with sensors. 2117 * 2118 * Finds the inventory items that are associated with the specified sensors. 2119 * Then gets D-Bus data for the inventory items, such as presence and VPD. 2120 * 2121 * This data is later used to provide sensor property values in the JSON 2122 * response. 2123 * 2124 * Finds the inventory items asynchronously. Invokes callback when the 2125 * inventory items have been obtained. 2126 * 2127 * The callback must have the following signature: 2128 * @code 2129 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2130 * @endcode 2131 * 2132 * @param sensorsAsyncResp Pointer to object holding response data. 2133 * @param sensorNames All sensors within the current chassis. 2134 * implements ObjectManager. 2135 * @param callback Callback to invoke when inventory items have been obtained. 2136 */ 2137 template <typename Callback> 2138 static void 2139 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2140 const std::shared_ptr<std::set<std::string>> sensorNames, 2141 Callback&& callback) 2142 { 2143 BMCWEB_LOG_DEBUG("getInventoryItems enter"); 2144 auto getInventoryItemAssociationsCb = 2145 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}]( 2146 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2147 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter"); 2148 auto getInventoryItemsConnectionsCb = 2149 [sensorsAsyncResp, inventoryItems, 2150 callback{std::forward<const Callback>(callback)}]( 2151 std::shared_ptr<std::set<std::string>> invConnections) { 2152 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter"); 2153 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems, 2154 callback{std::move(callback)}]() { 2155 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter"); 2156 2157 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems, 2158 callback{std::move(callback)}]() { 2159 BMCWEB_LOG_DEBUG("getInventoryLedsCb enter"); 2160 // Find Power Supply Attributes and get the data 2161 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems, 2162 std::move(callback)); 2163 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit"); 2164 }; 2165 2166 // Find led connections and get the data 2167 getInventoryLeds(sensorsAsyncResp, inventoryItems, 2168 std::move(getInventoryLedsCb)); 2169 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit"); 2170 }; 2171 2172 // Get inventory item data from connections 2173 getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2174 invConnections, 2175 std::move(getInventoryItemsDataCb)); 2176 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit"); 2177 }; 2178 2179 // Get connections that provide inventory item data 2180 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems, 2181 std::move(getInventoryItemsConnectionsCb)); 2182 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit"); 2183 }; 2184 2185 // Get associations from sensors to inventory items 2186 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, 2187 std::move(getInventoryItemAssociationsCb)); 2188 BMCWEB_LOG_DEBUG("getInventoryItems exit"); 2189 } 2190 2191 /** 2192 * @brief Returns JSON PowerSupply object for the specified inventory item. 2193 * 2194 * Searches for a JSON PowerSupply object that matches the specified inventory 2195 * item. If one is not found, a new PowerSupply object is added to the JSON 2196 * array. 2197 * 2198 * Multiple sensors are often associated with one power supply inventory item. 2199 * As a result, multiple sensor values are stored in one JSON PowerSupply 2200 * object. 2201 * 2202 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2203 * @param inventoryItem Inventory item for the power supply. 2204 * @param chassisId Chassis that contains the power supply. 2205 * @return JSON PowerSupply object for the specified inventory item. 2206 */ 2207 inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2208 const InventoryItem& inventoryItem, 2209 const std::string& chassisId) 2210 { 2211 std::string nameS; 2212 nameS.resize(inventoryItem.name.size()); 2213 std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' '); 2214 // Check if matching PowerSupply object already exists in JSON array 2215 for (nlohmann::json& powerSupply : powerSupplyArray) 2216 { 2217 nlohmann::json::iterator nameIt = powerSupply.find("Name"); 2218 if (nameIt == powerSupply.end()) 2219 { 2220 continue; 2221 } 2222 const std::string* name = nameIt->get_ptr<std::string*>(); 2223 if (name == nullptr) 2224 { 2225 continue; 2226 } 2227 if (nameS == *name) 2228 { 2229 return powerSupply; 2230 } 2231 } 2232 2233 // Add new PowerSupply object to JSON array 2234 powerSupplyArray.push_back({}); 2235 nlohmann::json& powerSupply = powerSupplyArray.back(); 2236 boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power", 2237 chassisId); 2238 url.set_fragment(("/PowerSupplies"_json_pointer).to_string()); 2239 powerSupply["@odata.id"] = std::move(url); 2240 std::string escaped; 2241 escaped.resize(inventoryItem.name.size()); 2242 std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' '); 2243 powerSupply["Name"] = std::move(escaped); 2244 powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2245 powerSupply["Model"] = inventoryItem.model; 2246 powerSupply["PartNumber"] = inventoryItem.partNumber; 2247 powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2248 setLedState(powerSupply, &inventoryItem); 2249 2250 if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 2251 { 2252 powerSupply["EfficiencyPercent"] = 2253 inventoryItem.powerSupplyEfficiencyPercent; 2254 } 2255 2256 powerSupply["Status"]["State"] = getState(&inventoryItem, true); 2257 const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2258 powerSupply["Status"]["Health"] = health; 2259 2260 return powerSupply; 2261 } 2262 2263 /** 2264 * @brief Gets the values of the specified sensors. 2265 * 2266 * Stores the results as JSON in the SensorsAsyncResp. 2267 * 2268 * Gets the sensor values asynchronously. Stores the results later when the 2269 * information has been obtained. 2270 * 2271 * The sensorNames set contains all requested sensors for the current chassis. 2272 * 2273 * To minimize the number of DBus calls, the DBus method 2274 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2275 * values of all sensors provided by a connection (service). 2276 * 2277 * The connections set contains all the connections that provide sensor values. 2278 * 2279 * The InventoryItem vector contains D-Bus inventory items associated with the 2280 * sensors. Inventory item data is needed for some Redfish sensor properties. 2281 * 2282 * @param SensorsAsyncResp Pointer to object holding response data. 2283 * @param sensorNames All requested sensors within the current chassis. 2284 * @param connections Connections that provide sensor values. 2285 * implements ObjectManager. 2286 * @param inventoryItems Inventory items associated with the sensors. 2287 */ 2288 inline void getSensorData( 2289 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2290 const std::shared_ptr<std::set<std::string>>& sensorNames, 2291 const std::set<std::string>& connections, 2292 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2293 { 2294 BMCWEB_LOG_DEBUG("getSensorData enter"); 2295 // Get managed objects from all services exposing sensors 2296 for (const std::string& connection : connections) 2297 { 2298 sdbusplus::message::object_path sensorPath( 2299 "/xyz/openbmc_project/sensors"); 2300 dbus::utility::getManagedObjects( 2301 connection, sensorPath, 2302 [sensorsAsyncResp, sensorNames, 2303 inventoryItems](const boost::system::error_code& ec, 2304 const dbus::utility::ManagedObjectType& resp) { 2305 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter"); 2306 if (ec) 2307 { 2308 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec); 2309 messages::internalError(sensorsAsyncResp->asyncResp->res); 2310 return; 2311 } 2312 // Go through all objects and update response with sensor data 2313 for (const auto& objDictEntry : resp) 2314 { 2315 const std::string& objPath = 2316 static_cast<const std::string&>(objDictEntry.first); 2317 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}", 2318 objPath); 2319 2320 std::vector<std::string> split; 2321 // Reserve space for 2322 // /xyz/openbmc_project/sensors/<name>/<subname> 2323 split.reserve(6); 2324 // NOLINTNEXTLINE 2325 bmcweb::split(split, objPath, '/'); 2326 if (split.size() < 6) 2327 { 2328 BMCWEB_LOG_ERROR("Got path that isn't long enough {}", 2329 objPath); 2330 continue; 2331 } 2332 // These indexes aren't intuitive, as split puts an empty 2333 // string at the beginning 2334 const std::string& sensorType = split[4]; 2335 const std::string& sensorName = split[5]; 2336 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName, 2337 sensorType); 2338 if (sensorNames->find(objPath) == sensorNames->end()) 2339 { 2340 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName); 2341 continue; 2342 } 2343 2344 // Find inventory item (if any) associated with sensor 2345 InventoryItem* inventoryItem = 2346 findInventoryItemForSensor(inventoryItems, objPath); 2347 2348 const std::string& sensorSchema = 2349 sensorsAsyncResp->chassisSubNode; 2350 2351 nlohmann::json* sensorJson = nullptr; 2352 2353 if (sensorSchema == sensors::node::sensors && 2354 !sensorsAsyncResp->efficientExpand) 2355 { 2356 std::string sensorTypeEscaped(sensorType); 2357 auto remove = std::ranges::remove(sensorTypeEscaped, '_'); 2358 2359 sensorTypeEscaped.erase(std::ranges::begin(remove), 2360 sensorTypeEscaped.end()); 2361 std::string sensorId(sensorTypeEscaped); 2362 sensorId += "_"; 2363 sensorId += sensorName; 2364 2365 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 2366 boost::urls::format("/redfish/v1/Chassis/{}/{}/{}", 2367 sensorsAsyncResp->chassisId, 2368 sensorsAsyncResp->chassisSubNode, 2369 sensorId); 2370 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 2371 } 2372 else 2373 { 2374 std::string fieldName; 2375 if (sensorsAsyncResp->efficientExpand) 2376 { 2377 fieldName = "Members"; 2378 } 2379 else if (sensorType == "temperature") 2380 { 2381 fieldName = "Temperatures"; 2382 } 2383 else if (sensorType == "fan" || sensorType == "fan_tach" || 2384 sensorType == "fan_pwm") 2385 { 2386 fieldName = "Fans"; 2387 } 2388 else if (sensorType == "voltage") 2389 { 2390 fieldName = "Voltages"; 2391 } 2392 else if (sensorType == "power") 2393 { 2394 if (sensorName == "total_power") 2395 { 2396 fieldName = "PowerControl"; 2397 } 2398 else if ((inventoryItem != nullptr) && 2399 (inventoryItem->isPowerSupply)) 2400 { 2401 fieldName = "PowerSupplies"; 2402 } 2403 else 2404 { 2405 // Other power sensors are in SensorCollection 2406 continue; 2407 } 2408 } 2409 else 2410 { 2411 BMCWEB_LOG_ERROR("Unsure how to handle sensorType {}", 2412 sensorType); 2413 continue; 2414 } 2415 2416 nlohmann::json& tempArray = 2417 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2418 if (fieldName == "PowerControl") 2419 { 2420 if (tempArray.empty()) 2421 { 2422 // Put multiple "sensors" into a single 2423 // PowerControl. Follows MemberId naming and 2424 // naming in power.hpp. 2425 nlohmann::json::object_t power; 2426 boost::urls::url url = boost::urls::format( 2427 "/redfish/v1/Chassis/{}/{}", 2428 sensorsAsyncResp->chassisId, 2429 sensorsAsyncResp->chassisSubNode); 2430 url.set_fragment((""_json_pointer / fieldName / "0") 2431 .to_string()); 2432 power["@odata.id"] = std::move(url); 2433 tempArray.emplace_back(std::move(power)); 2434 } 2435 sensorJson = &(tempArray.back()); 2436 } 2437 else if (fieldName == "PowerSupplies") 2438 { 2439 if (inventoryItem != nullptr) 2440 { 2441 sensorJson = 2442 &(getPowerSupply(tempArray, *inventoryItem, 2443 sensorsAsyncResp->chassisId)); 2444 } 2445 } 2446 else if (fieldName == "Members") 2447 { 2448 std::string sensorTypeEscaped(sensorType); 2449 auto remove = std::ranges::remove(sensorTypeEscaped, 2450 '_'); 2451 sensorTypeEscaped.erase(std::ranges::begin(remove), 2452 sensorTypeEscaped.end()); 2453 std::string sensorId(sensorTypeEscaped); 2454 sensorId += "_"; 2455 sensorId += sensorName; 2456 2457 nlohmann::json::object_t member; 2458 member["@odata.id"] = boost::urls::format( 2459 "/redfish/v1/Chassis/{}/{}/{}", 2460 sensorsAsyncResp->chassisId, 2461 sensorsAsyncResp->chassisSubNode, sensorId); 2462 tempArray.emplace_back(std::move(member)); 2463 sensorJson = &(tempArray.back()); 2464 } 2465 else 2466 { 2467 nlohmann::json::object_t member; 2468 boost::urls::url url = boost::urls::format( 2469 "/redfish/v1/Chassis/{}/{}", 2470 sensorsAsyncResp->chassisId, 2471 sensorsAsyncResp->chassisSubNode); 2472 url.set_fragment( 2473 (""_json_pointer / fieldName).to_string()); 2474 member["@odata.id"] = std::move(url); 2475 tempArray.emplace_back(std::move(member)); 2476 sensorJson = &(tempArray.back()); 2477 } 2478 } 2479 2480 if (sensorJson != nullptr) 2481 { 2482 objectInterfacesToJson(sensorName, sensorType, 2483 sensorsAsyncResp->chassisSubNode, 2484 objDictEntry.second, *sensorJson, 2485 inventoryItem); 2486 2487 std::string path = "/xyz/openbmc_project/sensors/"; 2488 path += sensorType; 2489 path += "/"; 2490 path += sensorName; 2491 sensorsAsyncResp->addMetadata(*sensorJson, path); 2492 } 2493 } 2494 if (sensorsAsyncResp.use_count() == 1) 2495 { 2496 sortJSONResponse(sensorsAsyncResp); 2497 if (sensorsAsyncResp->chassisSubNode == 2498 sensors::node::sensors && 2499 sensorsAsyncResp->efficientExpand) 2500 { 2501 sensorsAsyncResp->asyncResp->res 2502 .jsonValue["Members@odata.count"] = 2503 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] 2504 .size(); 2505 } 2506 else if (sensorsAsyncResp->chassisSubNode == 2507 sensors::node::thermal) 2508 { 2509 populateFanRedundancy(sensorsAsyncResp); 2510 } 2511 } 2512 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit"); 2513 }); 2514 } 2515 BMCWEB_LOG_DEBUG("getSensorData exit"); 2516 } 2517 2518 inline void 2519 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2520 const std::shared_ptr<std::set<std::string>>& sensorNames) 2521 { 2522 auto getConnectionCb = [sensorsAsyncResp, sensorNames]( 2523 const std::set<std::string>& connections) { 2524 BMCWEB_LOG_DEBUG("getConnectionCb enter"); 2525 auto getInventoryItemsCb = 2526 [sensorsAsyncResp, sensorNames, 2527 connections](const std::shared_ptr<std::vector<InventoryItem>>& 2528 inventoryItems) { 2529 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter"); 2530 // Get sensor data and store results in JSON 2531 getSensorData(sensorsAsyncResp, sensorNames, connections, 2532 inventoryItems); 2533 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit"); 2534 }; 2535 2536 // Get inventory items associated with sensors 2537 getInventoryItems(sensorsAsyncResp, sensorNames, 2538 std::move(getInventoryItemsCb)); 2539 2540 BMCWEB_LOG_DEBUG("getConnectionCb exit"); 2541 }; 2542 2543 // Get set of connections that provide sensor values 2544 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 2545 } 2546 2547 /** 2548 * @brief Entry point for retrieving sensors data related to requested 2549 * chassis. 2550 * @param SensorsAsyncResp Pointer to object holding response data 2551 */ 2552 inline void 2553 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 2554 { 2555 BMCWEB_LOG_DEBUG("getChassisData enter"); 2556 auto getChassisCb = 2557 [sensorsAsyncResp]( 2558 const std::shared_ptr<std::set<std::string>>& sensorNames) { 2559 BMCWEB_LOG_DEBUG("getChassisCb enter"); 2560 processSensorList(sensorsAsyncResp, sensorNames); 2561 BMCWEB_LOG_DEBUG("getChassisCb exit"); 2562 }; 2563 // SensorCollection doesn't contain the Redundancy property 2564 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors) 2565 { 2566 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 2567 nlohmann::json::array(); 2568 } 2569 // Get set of sensors in chassis 2570 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, 2571 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, 2572 std::move(getChassisCb)); 2573 BMCWEB_LOG_DEBUG("getChassisData exit"); 2574 } 2575 2576 /** 2577 * @brief Find the requested sensorName in the list of all sensors supplied by 2578 * the chassis node 2579 * 2580 * @param sensorName The sensor name supplied in the PATCH request 2581 * @param sensorsList The list of sensors managed by the chassis node 2582 * @param sensorsModified The list of sensors that were found as a result of 2583 * repeated calls to this function 2584 */ 2585 inline bool 2586 findSensorNameUsingSensorPath(std::string_view sensorName, 2587 const std::set<std::string>& sensorsList, 2588 std::set<std::string>& sensorsModified) 2589 { 2590 for (const auto& chassisSensor : sensorsList) 2591 { 2592 sdbusplus::message::object_path path(chassisSensor); 2593 std::string thisSensorName = path.filename(); 2594 if (thisSensorName.empty()) 2595 { 2596 continue; 2597 } 2598 if (thisSensorName == sensorName) 2599 { 2600 sensorsModified.emplace(chassisSensor); 2601 return true; 2602 } 2603 } 2604 return false; 2605 } 2606 2607 inline std::pair<std::string, std::string> 2608 splitSensorNameAndType(std::string_view sensorId) 2609 { 2610 size_t index = sensorId.find('_'); 2611 if (index == std::string::npos) 2612 { 2613 return std::make_pair<std::string, std::string>("", ""); 2614 } 2615 std::string sensorType{sensorId.substr(0, index)}; 2616 std::string sensorName{sensorId.substr(index + 1)}; 2617 // fan_pwm and fan_tach need special handling 2618 if (sensorType == "fantach" || sensorType == "fanpwm") 2619 { 2620 sensorType.insert(3, 1, '_'); 2621 } 2622 return std::make_pair(sensorType, sensorName); 2623 } 2624 2625 /** 2626 * @brief Entry point for overriding sensor values of given sensor 2627 * 2628 * @param sensorAsyncResp response object 2629 * @param allCollections Collections extract from sensors' request patch info 2630 * @param chassisSubNode Chassis Node for which the query has to happen 2631 */ 2632 inline void setSensorsOverride( 2633 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 2634 std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>& 2635 allCollections) 2636 { 2637 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}", 2638 sensorAsyncResp->chassisSubNode); 2639 2640 const char* propertyValueName = nullptr; 2641 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2642 std::string memberId; 2643 double value = 0.0; 2644 for (auto& collectionItems : allCollections) 2645 { 2646 if (collectionItems.first == "Temperatures") 2647 { 2648 propertyValueName = "ReadingCelsius"; 2649 } 2650 else if (collectionItems.first == "Fans") 2651 { 2652 propertyValueName = "Reading"; 2653 } 2654 else 2655 { 2656 propertyValueName = "ReadingVolts"; 2657 } 2658 for (auto& item : collectionItems.second) 2659 { 2660 if (!json_util::readJsonObject( 2661 item, sensorAsyncResp->asyncResp->res, "MemberId", memberId, 2662 propertyValueName, value)) 2663 { 2664 return; 2665 } 2666 overrideMap.emplace(memberId, 2667 std::make_pair(value, collectionItems.first)); 2668 } 2669 } 2670 2671 auto getChassisSensorListCb = 2672 [sensorAsyncResp, overrideMap]( 2673 const std::shared_ptr<std::set<std::string>>& sensorsList) { 2674 // Match sensor names in the PATCH request to those managed by the 2675 // chassis node 2676 const std::shared_ptr<std::set<std::string>> sensorNames = 2677 std::make_shared<std::set<std::string>>(); 2678 for (const auto& item : overrideMap) 2679 { 2680 const auto& sensor = item.first; 2681 std::pair<std::string, std::string> sensorNameType = 2682 splitSensorNameAndType(sensor); 2683 if (!findSensorNameUsingSensorPath(sensorNameType.second, 2684 *sensorsList, *sensorNames)) 2685 { 2686 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first); 2687 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2688 item.second.second, item.first); 2689 return; 2690 } 2691 } 2692 // Get the connection to which the memberId belongs 2693 auto getObjectsWithConnectionCb = 2694 [sensorAsyncResp, 2695 overrideMap](const std::set<std::string>& /*connections*/, 2696 const std::set<std::pair<std::string, std::string>>& 2697 objectsWithConnection) { 2698 if (objectsWithConnection.size() != overrideMap.size()) 2699 { 2700 BMCWEB_LOG_INFO( 2701 "Unable to find all objects with proper connection {} requested {}", 2702 objectsWithConnection.size(), overrideMap.size()); 2703 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2704 sensorAsyncResp->chassisSubNode == 2705 sensors::node::thermal 2706 ? "Temperatures" 2707 : "Voltages", 2708 "Count"); 2709 return; 2710 } 2711 for (const auto& item : objectsWithConnection) 2712 { 2713 sdbusplus::message::object_path path(item.first); 2714 std::string sensorName = path.filename(); 2715 if (sensorName.empty()) 2716 { 2717 messages::internalError(sensorAsyncResp->asyncResp->res); 2718 return; 2719 } 2720 std::string id = path.parent_path().filename(); 2721 auto remove = std::ranges::remove(id, '_'); 2722 id.erase(std::ranges::begin(remove), id.end()); 2723 id += "_"; 2724 id += sensorName; 2725 2726 const auto& iterator = overrideMap.find(id); 2727 if (iterator == overrideMap.end()) 2728 { 2729 BMCWEB_LOG_INFO("Unable to find sensor object{}", 2730 item.first); 2731 messages::internalError(sensorAsyncResp->asyncResp->res); 2732 return; 2733 } 2734 sdbusplus::asio::setProperty( 2735 *crow::connections::systemBus, item.second, item.first, 2736 "xyz.openbmc_project.Sensor.Value", "Value", 2737 iterator->second.first, 2738 [sensorAsyncResp](const boost::system::error_code& ec) { 2739 if (ec) 2740 { 2741 if (ec.value() == 2742 boost::system::errc::permission_denied) 2743 { 2744 BMCWEB_LOG_WARNING( 2745 "Manufacturing mode is not Enabled...can't " 2746 "Override the sensor value. "); 2747 2748 messages::insufficientPrivilege( 2749 sensorAsyncResp->asyncResp->res); 2750 return; 2751 } 2752 BMCWEB_LOG_DEBUG( 2753 "setOverrideValueStatus DBUS error: {}", ec); 2754 messages::internalError( 2755 sensorAsyncResp->asyncResp->res); 2756 } 2757 }); 2758 } 2759 }; 2760 // Get object with connection for the given sensor name 2761 getObjectsWithConnection(sensorAsyncResp, sensorNames, 2762 std::move(getObjectsWithConnectionCb)); 2763 }; 2764 // get full sensor list for the given chassisId and cross verify the sensor. 2765 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, 2766 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, 2767 std::move(getChassisSensorListCb)); 2768 } 2769 2770 /** 2771 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2772 * path of the sensor. 2773 * 2774 * Function builds valid Redfish response for sensor query of given chassis and 2775 * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2776 * it to caller in a callback. 2777 * 2778 * @param chassis Chassis for which retrieval should be performed 2779 * @param node Node (group) of sensors. See sensors::node for supported values 2780 * @param mapComplete Callback to be called with retrieval result 2781 */ 2782 template <typename Callback> 2783 inline void retrieveUriToDbusMap(const std::string& chassis, 2784 const std::string& node, 2785 Callback&& mapComplete) 2786 { 2787 decltype(sensors::paths)::const_iterator pathIt = 2788 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), 2789 [&node](auto&& val) { return val.first == node; }); 2790 if (pathIt == sensors::paths.cend()) 2791 { 2792 BMCWEB_LOG_ERROR("Wrong node provided : {}", node); 2793 std::map<std::string, std::string> noop; 2794 mapComplete(boost::beast::http::status::bad_request, noop); 2795 return; 2796 } 2797 2798 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 2799 auto callback = [asyncResp, 2800 mapCompleteCb{std::forward<Callback>(mapComplete)}]( 2801 const boost::beast::http::status status, 2802 const std::map<std::string, std::string>& uriToDbus) { 2803 mapCompleteCb(status, uriToDbus); 2804 }; 2805 2806 auto resp = std::make_shared<SensorsAsyncResp>( 2807 asyncResp, chassis, pathIt->second, node, std::move(callback)); 2808 getChassisData(resp); 2809 } 2810 2811 namespace sensors 2812 { 2813 2814 inline void getChassisCallback( 2815 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2816 std::string_view chassisId, std::string_view chassisSubNode, 2817 const std::shared_ptr<std::set<std::string>>& sensorNames) 2818 { 2819 BMCWEB_LOG_DEBUG("getChassisCallback enter "); 2820 2821 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 2822 for (const std::string& sensor : *sensorNames) 2823 { 2824 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor); 2825 2826 sdbusplus::message::object_path path(sensor); 2827 std::string sensorName = path.filename(); 2828 if (sensorName.empty()) 2829 { 2830 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor); 2831 messages::internalError(asyncResp->res); 2832 return; 2833 } 2834 std::string type = path.parent_path().filename(); 2835 // fan_tach has an underscore in it, so remove it to "normalize" the 2836 // type in the URI 2837 auto remove = std::ranges::remove(type, '_'); 2838 type.erase(std::ranges::begin(remove), type.end()); 2839 2840 nlohmann::json::object_t member; 2841 std::string id = type; 2842 id += "_"; 2843 id += sensorName; 2844 member["@odata.id"] = boost::urls::format( 2845 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id); 2846 2847 entriesArray.emplace_back(std::move(member)); 2848 } 2849 2850 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 2851 BMCWEB_LOG_DEBUG("getChassisCallback exit"); 2852 } 2853 2854 inline void handleSensorCollectionGet( 2855 App& app, const crow::Request& req, 2856 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2857 const std::string& chassisId) 2858 { 2859 query_param::QueryCapabilities capabilities = { 2860 .canDelegateExpandLevel = 1, 2861 }; 2862 query_param::Query delegatedQuery; 2863 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp, 2864 delegatedQuery, capabilities)) 2865 { 2866 return; 2867 } 2868 2869 if (delegatedQuery.expandType != query_param::ExpandType::None) 2870 { 2871 // we perform efficient expand. 2872 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>( 2873 asyncResp, chassisId, sensors::dbus::sensorPaths, 2874 sensors::node::sensors, 2875 /*efficientExpand=*/true); 2876 getChassisData(sensorsAsyncResp); 2877 2878 BMCWEB_LOG_DEBUG( 2879 "SensorCollection doGet exit via efficient expand handler"); 2880 return; 2881 } 2882 2883 // We get all sensors as hyperlinkes in the chassis (this 2884 // implies we reply on the default query parameters handler) 2885 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths, 2886 std::bind_front(sensors::getChassisCallback, asyncResp, 2887 chassisId, sensors::node::sensors)); 2888 } 2889 2890 inline void 2891 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2892 const std::string& sensorPath, 2893 const ::dbus::utility::MapperGetObject& mapperResponse) 2894 { 2895 if (mapperResponse.size() != 1) 2896 { 2897 messages::internalError(asyncResp->res); 2898 return; 2899 } 2900 const auto& valueIface = *mapperResponse.begin(); 2901 const std::string& connectionName = valueIface.first; 2902 BMCWEB_LOG_DEBUG("Looking up {}", connectionName); 2903 BMCWEB_LOG_DEBUG("Path {}", sensorPath); 2904 2905 sdbusplus::asio::getAllProperties( 2906 *crow::connections::systemBus, connectionName, sensorPath, "", 2907 [asyncResp, 2908 sensorPath](const boost::system::error_code& ec, 2909 const ::dbus::utility::DBusPropertiesMap& valuesDict) { 2910 if (ec) 2911 { 2912 messages::internalError(asyncResp->res); 2913 return; 2914 } 2915 sdbusplus::message::object_path path(sensorPath); 2916 std::string name = path.filename(); 2917 path = path.parent_path(); 2918 std::string type = path.filename(); 2919 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict, 2920 asyncResp->res.jsonValue, nullptr); 2921 }); 2922 } 2923 2924 inline void handleSensorGet(App& app, const crow::Request& req, 2925 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2926 const std::string& chassisId, 2927 const std::string& sensorId) 2928 { 2929 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2930 { 2931 return; 2932 } 2933 std::pair<std::string, std::string> nameType = 2934 splitSensorNameAndType(sensorId); 2935 if (nameType.first.empty() || nameType.second.empty()) 2936 { 2937 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 2938 return; 2939 } 2940 2941 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 2942 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId); 2943 2944 BMCWEB_LOG_DEBUG("Sensor doGet enter"); 2945 2946 constexpr std::array<std::string_view, 1> interfaces = { 2947 "xyz.openbmc_project.Sensor.Value"}; 2948 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first + 2949 '/' + nameType.second; 2950 // Get a list of all of the sensors that implement Sensor.Value 2951 // and get the path and service name associated with the sensor 2952 ::dbus::utility::getDbusObject( 2953 sensorPath, interfaces, 2954 [asyncResp, sensorId, 2955 sensorPath](const boost::system::error_code& ec, 2956 const ::dbus::utility::MapperGetObject& subtree) { 2957 BMCWEB_LOG_DEBUG("respHandler1 enter"); 2958 if (ec == boost::system::errc::io_error) 2959 { 2960 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths"); 2961 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 2962 return; 2963 } 2964 if (ec) 2965 { 2966 messages::internalError(asyncResp->res); 2967 BMCWEB_LOG_ERROR( 2968 "Sensor getSensorPaths resp_handler: Dbus error {}", ec); 2969 return; 2970 } 2971 getSensorFromDbus(asyncResp, sensorPath, subtree); 2972 BMCWEB_LOG_DEBUG("respHandler1 exit"); 2973 }); 2974 } 2975 2976 } // namespace sensors 2977 2978 inline void requestRoutesSensorCollection(App& app) 2979 { 2980 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 2981 .privileges(redfish::privileges::getSensorCollection) 2982 .methods(boost::beast::http::verb::get)( 2983 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); 2984 } 2985 2986 inline void requestRoutesSensor(App& app) 2987 { 2988 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 2989 .privileges(redfish::privileges::getSensor) 2990 .methods(boost::beast::http::verb::get)( 2991 std::bind_front(sensors::handleSensorGet, std::ref(app))); 2992 } 2993 2994 } // namespace redfish 2995