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