108777fb0SLewanczyk, Dawid /* 208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation 308777fb0SLewanczyk, Dawid // 408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License"); 508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License. 608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at 708777fb0SLewanczyk, Dawid // 808777fb0SLewanczyk, Dawid // http://www.apache.org/licenses/LICENSE-2.0 908777fb0SLewanczyk, Dawid // 1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software 1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS, 1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and 1408777fb0SLewanczyk, Dawid // limitations under the License. 1508777fb0SLewanczyk, Dawid */ 1608777fb0SLewanczyk, Dawid #pragma once 1708777fb0SLewanczyk, Dawid 183ccb3adbSEd Tanous #include "app.hpp" 193ccb3adbSEd Tanous #include "dbus_singleton.hpp" 207a1dbc48SGeorge Liu #include "dbus_utility.hpp" 21539d8c6bSEd Tanous #include "generated/enums/redundancy.hpp" 22aaf08ac7SMatt Simmering #include "generated/enums/resource.hpp" 233ccb3adbSEd Tanous #include "query.hpp" 243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 2550ebd4afSEd Tanous #include "str_utility.hpp" 263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 273ccb3adbSEd Tanous #include "utils/json_utils.hpp" 283ccb3adbSEd Tanous #include "utils/query_param.hpp" 291516c21bSJanet Adkins #include "utils/sensor_utils.hpp" 300ec8b83dSEd Tanous 31e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 32ef4c65b7SEd Tanous #include <boost/url/format.hpp> 331e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 3486d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 351214b7e7SGunnar Mills 367a1dbc48SGeorge Liu #include <array> 371214b7e7SGunnar Mills #include <cmath> 38fe04d49cSNan Zhou #include <iterator> 39283860f5SEd Tanous #include <limits> 40fe04d49cSNan Zhou #include <map> 413544d2a7SEd Tanous #include <ranges> 42fe04d49cSNan Zhou #include <set> 4318f8f608SEd Tanous #include <string> 447a1dbc48SGeorge Liu #include <string_view> 45b5a76932SEd Tanous #include <utility> 46abf2add6SEd Tanous #include <variant> 4708777fb0SLewanczyk, Dawid 481abe55efSEd Tanous namespace redfish 491abe55efSEd Tanous { 5008777fb0SLewanczyk, Dawid 51a0ec28b6SAdrian Ambrożewicz namespace sensors 52a0ec28b6SAdrian Ambrożewicz { 53a0ec28b6SAdrian Ambrożewicz 5402da7c5aSEd Tanous // clang-format off 55a0ec28b6SAdrian Ambrożewicz namespace dbus 56a0ec28b6SAdrian Ambrożewicz { 57cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({ 5802da7c5aSEd Tanous "/xyz/openbmc_project/sensors/voltage", 5902da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power" 6002da7c5aSEd Tanous }); 61c2bf7f99SWludzik, Jozef 6225b54dbaSEd Tanous constexpr auto getSensorPaths(){ 6325b54dbaSEd Tanous if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){ 6425b54dbaSEd Tanous return std::to_array<std::string_view>({ 6502da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power", 66a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/current", 677088690cSBasheer Ahmed Muddebihal "/xyz/openbmc_project/sensors/airflow", 685deabed9SGunnar Mills "/xyz/openbmc_project/sensors/humidity", 69e8204933SGeorge Liu "/xyz/openbmc_project/sensors/voltage", 70e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_tach", 71e8204933SGeorge Liu "/xyz/openbmc_project/sensors/temperature", 72e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_pwm", 73e8204933SGeorge Liu "/xyz/openbmc_project/sensors/altitude", 74e8204933SGeorge Liu "/xyz/openbmc_project/sensors/energy", 7525b54dbaSEd Tanous "/xyz/openbmc_project/sensors/utilization"}); 7625b54dbaSEd Tanous } else { 7725b54dbaSEd Tanous return std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power", 7825b54dbaSEd Tanous "/xyz/openbmc_project/sensors/current", 7925b54dbaSEd Tanous "/xyz/openbmc_project/sensors/airflow", 8025b54dbaSEd Tanous "/xyz/openbmc_project/sensors/humidity", 8125b54dbaSEd Tanous "/xyz/openbmc_project/sensors/utilization"}); 8225b54dbaSEd Tanous } 8325b54dbaSEd Tanous } 8425b54dbaSEd Tanous 8525b54dbaSEd Tanous constexpr auto sensorPaths = getSensorPaths(); 8602da7c5aSEd Tanous 87cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({ 8802da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_tach", 89a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/temperature", 9002da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_pwm" 9102da7c5aSEd Tanous }); 9202da7c5aSEd Tanous 93c2bf7f99SWludzik, Jozef } // namespace dbus 9402da7c5aSEd Tanous // clang-format on 9502da7c5aSEd Tanous 96*0c728b42SJanet Adkins constexpr std::string_view powerNodeStr = sensor_utils::chassisSubNodeToString( 97*0c728b42SJanet Adkins sensor_utils::ChassisSubNode::powerNode); 98*0c728b42SJanet Adkins constexpr std::string_view sensorsNodeStr = 99*0c728b42SJanet Adkins sensor_utils::chassisSubNodeToString( 100*0c728b42SJanet Adkins sensor_utils::ChassisSubNode::sensorsNode); 101*0c728b42SJanet Adkins constexpr std::string_view thermalNodeStr = 102*0c728b42SJanet Adkins sensor_utils::chassisSubNodeToString( 103*0c728b42SJanet Adkins sensor_utils::ChassisSubNode::thermalNode); 104*0c728b42SJanet Adkins 105cf9e417dSEd Tanous using sensorPair = 106cf9e417dSEd Tanous std::pair<std::string_view, std::span<const std::string_view>>; 10702da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = { 108*0c728b42SJanet Adkins {{sensors::powerNodeStr, dbus::powerPaths}, 109*0c728b42SJanet Adkins {sensors::sensorsNodeStr, dbus::sensorPaths}, 110*0c728b42SJanet Adkins {sensors::thermalNodeStr, dbus::thermalPaths}}}; 111c2bf7f99SWludzik, Jozef 112a0ec28b6SAdrian Ambrożewicz } // namespace sensors 113a0ec28b6SAdrian Ambrożewicz 11408777fb0SLewanczyk, Dawid /** 115588c3f0dSKowalski, Kamil * SensorsAsyncResp 11608777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 11708777fb0SLewanczyk, Dawid */ 1181abe55efSEd Tanous class SensorsAsyncResp 1191abe55efSEd Tanous { 12008777fb0SLewanczyk, Dawid public: 121a0ec28b6SAdrian Ambrożewicz using DataCompleteCb = std::function<void( 122a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 123fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus)>; 124a0ec28b6SAdrian Ambrożewicz 125a0ec28b6SAdrian Ambrożewicz struct SensorData 126a0ec28b6SAdrian Ambrożewicz { 127a0ec28b6SAdrian Ambrożewicz const std::string name; 128a0ec28b6SAdrian Ambrożewicz std::string uri; 129a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 130a0ec28b6SAdrian Ambrożewicz }; 131a0ec28b6SAdrian Ambrożewicz 1328a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 1338d1b46d7Szhanghch05 const std::string& chassisIdIn, 134cf9e417dSEd Tanous std::span<const std::string_view> typesIn, 13502da7c5aSEd Tanous std::string_view subNode) : 136bd79bce8SPatrick Williams asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), 137bd79bce8SPatrick Williams chassisSubNode(subNode), efficientExpand(false) 1381214b7e7SGunnar Mills {} 13908777fb0SLewanczyk, Dawid 140a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 1418a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 1428d1b46d7Szhanghch05 const std::string& chassisIdIn, 143cf9e417dSEd Tanous std::span<const std::string_view> typesIn, 14402da7c5aSEd Tanous std::string_view subNode, 145a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 146bd79bce8SPatrick Williams asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), 147bd79bce8SPatrick Williams chassisSubNode(subNode), efficientExpand(false), 148bd79bce8SPatrick Williams metadata{std::vector<SensorData>()}, 149a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 150a0ec28b6SAdrian Ambrożewicz {} 151a0ec28b6SAdrian Ambrożewicz 152928fefb9SNan Zhou // sensor collections expand 1538a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 154928fefb9SNan Zhou const std::string& chassisIdIn, 155cf9e417dSEd Tanous std::span<const std::string_view> typesIn, 1568a592810SEd Tanous const std::string_view& subNode, bool efficientExpandIn) : 157bd79bce8SPatrick Williams asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), 158bd79bce8SPatrick Williams chassisSubNode(subNode), efficientExpand(efficientExpandIn) 159928fefb9SNan Zhou {} 160928fefb9SNan Zhou 1611abe55efSEd Tanous ~SensorsAsyncResp() 1621abe55efSEd Tanous { 1638d1b46d7Szhanghch05 if (asyncResp->res.result() == 1648d1b46d7Szhanghch05 boost::beast::http::status::internal_server_error) 1651abe55efSEd Tanous { 1661abe55efSEd Tanous // Reset the json object to clear out any data that made it in 1671abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 1681abe55efSEd Tanous // proper code 1698d1b46d7Szhanghch05 asyncResp->res.jsonValue = nlohmann::json::object(); 17008777fb0SLewanczyk, Dawid } 171a0ec28b6SAdrian Ambrożewicz 172a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 173a0ec28b6SAdrian Ambrożewicz { 174fe04d49cSNan Zhou std::map<std::string, std::string> map; 1758d1b46d7Szhanghch05 if (asyncResp->res.result() == boost::beast::http::status::ok) 176a0ec28b6SAdrian Ambrożewicz { 177a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 178a0ec28b6SAdrian Ambrożewicz { 179c1d019a6SEd Tanous map.emplace(sensor.uri, sensor.dbusPath); 180a0ec28b6SAdrian Ambrożewicz } 181a0ec28b6SAdrian Ambrożewicz } 1828d1b46d7Szhanghch05 dataComplete(asyncResp->res.result(), map); 183a0ec28b6SAdrian Ambrożewicz } 18408777fb0SLewanczyk, Dawid } 185588c3f0dSKowalski, Kamil 186ecd6a3a2SEd Tanous SensorsAsyncResp(const SensorsAsyncResp&) = delete; 187ecd6a3a2SEd Tanous SensorsAsyncResp(SensorsAsyncResp&&) = delete; 188ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 189ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 190ecd6a3a2SEd Tanous 191a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 192c1d019a6SEd Tanous const std::string& dbusPath) 193a0ec28b6SAdrian Ambrożewicz { 194a0ec28b6SAdrian Ambrożewicz if (metadata) 195a0ec28b6SAdrian Ambrożewicz { 196c1d019a6SEd Tanous metadata->emplace_back(SensorData{ 197c1d019a6SEd Tanous sensorObject["Name"], sensorObject["@odata.id"], dbusPath}); 198a0ec28b6SAdrian Ambrożewicz } 199a0ec28b6SAdrian Ambrożewicz } 200a0ec28b6SAdrian Ambrożewicz 201a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 202a0ec28b6SAdrian Ambrożewicz { 203a0ec28b6SAdrian Ambrożewicz if (metadata) 204a0ec28b6SAdrian Ambrożewicz { 205a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 206a0ec28b6SAdrian Ambrożewicz { 207a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 208a0ec28b6SAdrian Ambrożewicz { 209a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 210a0ec28b6SAdrian Ambrożewicz } 211a0ec28b6SAdrian Ambrożewicz } 212a0ec28b6SAdrian Ambrożewicz } 213a0ec28b6SAdrian Ambrożewicz } 214a0ec28b6SAdrian Ambrożewicz 2158d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 216a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 217cf9e417dSEd Tanous const std::span<const std::string_view> types; 218a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 219928fefb9SNan Zhou const bool efficientExpand; 220a0ec28b6SAdrian Ambrożewicz 221a0ec28b6SAdrian Ambrożewicz private: 222a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 223a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 22408777fb0SLewanczyk, Dawid }; 22508777fb0SLewanczyk, Dawid 226c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem; 227adc4f0dbSShawn McCarney 228adc4f0dbSShawn McCarney /** 229413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 230588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 23108777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 23208777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 23308777fb0SLewanczyk, Dawid */ 23408777fb0SLewanczyk, Dawid template <typename Callback> 235413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 23681ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 237fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 2381abe55efSEd Tanous Callback&& callback) 2391abe55efSEd Tanous { 24062598e31SEd Tanous BMCWEB_LOG_DEBUG("getObjectsWithConnection enter"); 24103b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 242e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 24308777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 24408777fb0SLewanczyk, Dawid 245e99073f5SGeorge Liu // Make call to ObjectMapper to find all sensors objects 246e99073f5SGeorge Liu dbus::utility::getSubTree( 247e99073f5SGeorge Liu path, 2, interfaces, 2488cb2c024SEd Tanous [callback = std::forward<Callback>(callback), sensorsAsyncResp, 249e99073f5SGeorge Liu sensorNames](const boost::system::error_code& ec, 250002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 251e99073f5SGeorge Liu // Response handler for parsing objects subtree 25262598e31SEd Tanous BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter"); 2531abe55efSEd Tanous if (ec) 2541abe55efSEd Tanous { 2558d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 25662598e31SEd Tanous BMCWEB_LOG_ERROR( 25762598e31SEd Tanous "getObjectsWithConnection resp_handler: Dbus error {}", ec); 25808777fb0SLewanczyk, Dawid return; 25908777fb0SLewanczyk, Dawid } 26008777fb0SLewanczyk, Dawid 26162598e31SEd Tanous BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size()); 26208777fb0SLewanczyk, Dawid 263bd79bce8SPatrick Williams // Make unique list of connections only for requested sensor types 264bd79bce8SPatrick Williams // and found in the chassis 265fe04d49cSNan Zhou std::set<std::string> connections; 266413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 26708777fb0SLewanczyk, Dawid 26862598e31SEd Tanous BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size()); 26949c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 2701abe55efSEd Tanous { 27162598e31SEd Tanous BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor); 27208777fb0SLewanczyk, Dawid } 27308777fb0SLewanczyk, Dawid 274bd79bce8SPatrick Williams for (const std::pair<std::string, 275bd79bce8SPatrick Williams std::vector<std::pair< 276bd79bce8SPatrick Williams std::string, std::vector<std::string>>>>& 2771abe55efSEd Tanous object : subtree) 2781abe55efSEd Tanous { 27949c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 2801abe55efSEd Tanous { 28149c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 2821abe55efSEd Tanous objData : object.second) 2831abe55efSEd Tanous { 284bd79bce8SPatrick Williams BMCWEB_LOG_DEBUG("Adding connection: {}", 285bd79bce8SPatrick Williams objData.first); 28608777fb0SLewanczyk, Dawid connections.insert(objData.first); 287de629b6eSShawn McCarney objectsWithConnection.insert( 288de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 28908777fb0SLewanczyk, Dawid } 29008777fb0SLewanczyk, Dawid } 29108777fb0SLewanczyk, Dawid } 29262598e31SEd Tanous BMCWEB_LOG_DEBUG("Found {} connections", connections.size()); 293413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 29462598e31SEd Tanous BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit"); 295e99073f5SGeorge Liu }); 29662598e31SEd Tanous BMCWEB_LOG_DEBUG("getObjectsWithConnection exit"); 297413961deSRichard Marian Thomaiyar } 298413961deSRichard Marian Thomaiyar 299413961deSRichard Marian Thomaiyar /** 300413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 301413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 302413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 303413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 304413961deSRichard Marian Thomaiyar */ 305413961deSRichard Marian Thomaiyar template <typename Callback> 306fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 307fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 308413961deSRichard Marian Thomaiyar Callback&& callback) 309413961deSRichard Marian Thomaiyar { 310413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 3118cb2c024SEd Tanous [callback = std::forward<Callback>(callback)]( 3128cb2c024SEd Tanous const std::set<std::string>& connections, 313413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 3143174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 31581ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 316413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 31708777fb0SLewanczyk, Dawid } 31808777fb0SLewanczyk, Dawid 31908777fb0SLewanczyk, Dawid /** 32049c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 32149c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 32249c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 32349c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 32449c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 32549c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 32649c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 32749c53ac9SJohnathan Mantey */ 32823a21a1cSEd Tanous inline void reduceSensorList( 3297f1cc26dSEd Tanous crow::Response& res, std::string_view chassisSubNode, 330cf9e417dSEd Tanous std::span<const std::string_view> sensorTypes, 33149c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 332fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& activeSensors) 33349c53ac9SJohnathan Mantey { 33449c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 33549c53ac9SJohnathan Mantey { 3367f1cc26dSEd Tanous messages::resourceNotFound(res, chassisSubNode, 337*0c728b42SJanet Adkins chassisSubNode == sensors::thermalNodeStr 338a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 33949c53ac9SJohnathan Mantey : "Voltages"); 34049c53ac9SJohnathan Mantey 34149c53ac9SJohnathan Mantey return; 34249c53ac9SJohnathan Mantey } 34349c53ac9SJohnathan Mantey if (allSensors->empty()) 34449c53ac9SJohnathan Mantey { 34549c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 34649c53ac9SJohnathan Mantey return; 34749c53ac9SJohnathan Mantey } 34849c53ac9SJohnathan Mantey 3497f1cc26dSEd Tanous for (std::string_view type : sensorTypes) 35049c53ac9SJohnathan Mantey { 35149c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 35249c53ac9SJohnathan Mantey { 35311ba3979SEd Tanous if (sensor.starts_with(type)) 35449c53ac9SJohnathan Mantey { 35549c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 35649c53ac9SJohnathan Mantey } 35749c53ac9SJohnathan Mantey } 35849c53ac9SJohnathan Mantey } 35949c53ac9SJohnathan Mantey } 36049c53ac9SJohnathan Mantey 3617f1cc26dSEd Tanous /* 3627f1cc26dSEd Tanous *Populates the top level collection for a given subnode. Populates 3637f1cc26dSEd Tanous *SensorCollection, Power, or Thermal schemas. 3647f1cc26dSEd Tanous * 3657f1cc26dSEd Tanous * */ 3667f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue, 3677f1cc26dSEd Tanous std::string_view chassisSubNode) 3687f1cc26dSEd Tanous { 369*0c728b42SJanet Adkins if (chassisSubNode == sensors::powerNodeStr) 3707f1cc26dSEd Tanous { 3717f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; 3727f1cc26dSEd Tanous } 373*0c728b42SJanet Adkins else if (chassisSubNode == sensors::thermalNodeStr) 3747f1cc26dSEd Tanous { 3757f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; 3767f1cc26dSEd Tanous jsonValue["Fans"] = nlohmann::json::array(); 3777f1cc26dSEd Tanous jsonValue["Temperatures"] = nlohmann::json::array(); 3787f1cc26dSEd Tanous } 379*0c728b42SJanet Adkins else if (chassisSubNode == sensors::sensorsNodeStr) 3807f1cc26dSEd Tanous { 3817f1cc26dSEd Tanous jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; 3827f1cc26dSEd Tanous jsonValue["Description"] = "Collection of Sensors for this Chassis"; 3837f1cc26dSEd Tanous jsonValue["Members"] = nlohmann::json::array(); 3847f1cc26dSEd Tanous jsonValue["Members@odata.count"] = 0; 3857f1cc26dSEd Tanous } 3867f1cc26dSEd Tanous 387*0c728b42SJanet Adkins if (chassisSubNode != sensors::sensorsNodeStr) 3887f1cc26dSEd Tanous { 3897f1cc26dSEd Tanous jsonValue["Id"] = chassisSubNode; 3907f1cc26dSEd Tanous } 3917f1cc26dSEd Tanous jsonValue["Name"] = chassisSubNode; 3927f1cc26dSEd Tanous } 3937f1cc26dSEd Tanous 39449c53ac9SJohnathan Mantey /** 39508777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 396588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 39708777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 39808777fb0SLewanczyk, Dawid */ 39908777fb0SLewanczyk, Dawid template <typename Callback> 4007f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4017f1cc26dSEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 402cf9e417dSEd Tanous std::span<const std::string_view> sensorTypes, 403cf9e417dSEd Tanous Callback&& callback) 4041abe55efSEd Tanous { 40562598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassis enter"); 4067a1dbc48SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = { 40749c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 408adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 4097a1dbc48SGeorge Liu 4107a1dbc48SGeorge Liu // Get the Chassis Collection 4117a1dbc48SGeorge Liu dbus::utility::getSubTreePaths( 4127a1dbc48SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 4138cb2c024SEd Tanous [callback = std::forward<Callback>(callback), asyncResp, 4147f1cc26dSEd Tanous chassisIdStr{std::string(chassisId)}, 4157f1cc26dSEd Tanous chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( 4167a1dbc48SGeorge Liu const boost::system::error_code& ec, 417002d39b4SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { 41862598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassis respHandler enter"); 4191abe55efSEd Tanous if (ec) 4201abe55efSEd Tanous { 42162598e31SEd Tanous BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec); 4227f1cc26dSEd Tanous messages::internalError(asyncResp->res); 42308777fb0SLewanczyk, Dawid return; 42408777fb0SLewanczyk, Dawid } 42549c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 42649c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 4271abe55efSEd Tanous { 42828aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 429f8fe53e7SEd Tanous std::string chassisName = path.filename(); 43028aa8de5SGeorge Liu if (chassisName.empty()) 4311abe55efSEd Tanous { 43262598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis); 433daf36e2eSEd Tanous continue; 434daf36e2eSEd Tanous } 4357f1cc26dSEd Tanous if (chassisName == chassisIdStr) 4361abe55efSEd Tanous { 43749c53ac9SJohnathan Mantey chassisPath = &chassis; 43849c53ac9SJohnathan Mantey break; 439daf36e2eSEd Tanous } 44049c53ac9SJohnathan Mantey } 44149c53ac9SJohnathan Mantey if (chassisPath == nullptr) 4421abe55efSEd Tanous { 443bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Chassis", 444bd79bce8SPatrick Williams chassisIdStr); 44549c53ac9SJohnathan Mantey return; 4461abe55efSEd Tanous } 4477f1cc26dSEd Tanous populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); 44808777fb0SLewanczyk, Dawid 449ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 450ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode); 45195a3ecadSAnthony Wilson 4528fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 4538fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 4546c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 4556c3e9451SGeorge Liu sensorPath, 4567f1cc26dSEd Tanous [asyncResp, chassisSubNode, sensorTypes, 4578cb2c024SEd Tanous callback = std::forward<const Callback>(callback)]( 4588b24275dSEd Tanous const boost::system::error_code& ec2, 4596c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& nodeSensorList) { 4608b24275dSEd Tanous if (ec2) 46149c53ac9SJohnathan Mantey { 4628b24275dSEd Tanous if (ec2.value() != EBADR) 46349c53ac9SJohnathan Mantey { 4647f1cc26dSEd Tanous messages::internalError(asyncResp->res); 46549c53ac9SJohnathan Mantey return; 46649c53ac9SJohnathan Mantey } 46749c53ac9SJohnathan Mantey } 468bd79bce8SPatrick Williams const std::shared_ptr<std::set<std::string>> 469bd79bce8SPatrick Williams culledSensorList = 470fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 471bd79bce8SPatrick Williams reduceSensorList(asyncResp->res, chassisSubNode, 472bd79bce8SPatrick Williams sensorTypes, &nodeSensorList, 473bd79bce8SPatrick Williams culledSensorList); 474bd79bce8SPatrick Williams BMCWEB_LOG_DEBUG("Finishing with {}", 475bd79bce8SPatrick Williams culledSensorList->size()); 47649c53ac9SJohnathan Mantey callback(culledSensorList); 4771e1e598dSJonathan Doman }); 4787a1dbc48SGeorge Liu }); 47962598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassis exit"); 48008777fb0SLewanczyk, Dawid } 48108777fb0SLewanczyk, Dawid 48208777fb0SLewanczyk, Dawid /** 4831d7c0054SEd Tanous * @brief Builds a json sensor representation of a sensor. 4841d7c0054SEd Tanous * @param sensorName The name of the sensor to be built 4851d7c0054SEd Tanous * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 4861d7c0054SEd Tanous * build 4878ece0e45SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor 4881d7c0054SEd Tanous * @param interfacesDict A dictionary of the interfaces and properties of said 4891d7c0054SEd Tanous * interfaces to be built from 4901d7c0054SEd Tanous * @param sensorJson The json object to fill 4911d7c0054SEd Tanous * @param inventoryItem D-Bus inventory item associated with the sensor. Will 4921d7c0054SEd Tanous * be nullptr if no associated inventory item was found. 4931d7c0054SEd Tanous */ 4941d7c0054SEd Tanous inline void objectInterfacesToJson( 4951d7c0054SEd Tanous const std::string& sensorName, const std::string& sensorType, 496*0c728b42SJanet Adkins const sensor_utils::ChassisSubNode chassisSubNode, 49780f79a40SMichael Shen const dbus::utility::DBusInterfacesMap& interfacesDict, 4981d7c0054SEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 4991d7c0054SEd Tanous { 5001d7c0054SEd Tanous for (const auto& [interface, valuesDict] : interfacesDict) 5011d7c0054SEd Tanous { 502c9563608SJanet Adkins sensor_utils::objectPropertiesToJson( 503c9563608SJanet Adkins sensorName, sensorType, chassisSubNode, valuesDict, sensorJson, 504c9563608SJanet Adkins inventoryItem); 5051d7c0054SEd Tanous } 50662598e31SEd Tanous BMCWEB_LOG_DEBUG("Added sensor {}", sensorName); 5071d7c0054SEd Tanous } 5081d7c0054SEd Tanous 509b5a76932SEd Tanous inline void populateFanRedundancy( 510b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 5118bd25ccdSJames Feist { 512e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 513e99073f5SGeorge Liu "xyz.openbmc_project.Control.FanRedundancy"}; 514e99073f5SGeorge Liu dbus::utility::getSubTree( 515e99073f5SGeorge Liu "/xyz/openbmc_project/control", 2, interfaces, 516b9d36b47SEd Tanous [sensorsAsyncResp]( 517e99073f5SGeorge Liu const boost::system::error_code& ec, 518b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& resp) { 5198bd25ccdSJames Feist if (ec) 5208bd25ccdSJames Feist { 5218bd25ccdSJames Feist return; // don't have to have this interface 5228bd25ccdSJames Feist } 5236c3e9451SGeorge Liu for (const std::pair<std::string, dbus::utility::MapperServiceMap>& 524e278c18fSEd Tanous pathPair : resp) 5258bd25ccdSJames Feist { 526e278c18fSEd Tanous const std::string& path = pathPair.first; 527bd79bce8SPatrick Williams const dbus::utility::MapperServiceMap& objDict = 528bd79bce8SPatrick Williams pathPair.second; 5298bd25ccdSJames Feist if (objDict.empty()) 5308bd25ccdSJames Feist { 5318bd25ccdSJames Feist continue; // this should be impossible 5328bd25ccdSJames Feist } 5338bd25ccdSJames Feist 5348bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 5356c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 5366c3e9451SGeorge Liu path + "/chassis", 5376c3e9451SGeorge Liu [path, owner, sensorsAsyncResp]( 5388b24275dSEd Tanous const boost::system::error_code& ec2, 5396c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) { 5408b24275dSEd Tanous if (ec2) 5418bd25ccdSJames Feist { 5428bd25ccdSJames Feist return; // if they don't have an association we 5438bd25ccdSJames Feist // can't tell what chassis is 5448bd25ccdSJames Feist } 5453544d2a7SEd Tanous auto found = std::ranges::find_if( 546bd79bce8SPatrick Williams endpoints, 547bd79bce8SPatrick Williams [sensorsAsyncResp](const std::string& entry) { 548bd79bce8SPatrick Williams return entry.find( 549bd79bce8SPatrick Williams sensorsAsyncResp->chassisId) != 5508bd25ccdSJames Feist std::string::npos; 5518bd25ccdSJames Feist }); 5528bd25ccdSJames Feist 5531e1e598dSJonathan Doman if (found == endpoints.end()) 5548bd25ccdSJames Feist { 5558bd25ccdSJames Feist return; 5568bd25ccdSJames Feist } 55786d89ed7SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 55886d89ed7SKrzysztof Grobelny *crow::connections::systemBus, owner, path, 55986d89ed7SKrzysztof Grobelny "xyz.openbmc_project.Control.FanRedundancy", 5608bd25ccdSJames Feist [path, sensorsAsyncResp]( 5618b24275dSEd Tanous const boost::system::error_code& ec3, 56286d89ed7SKrzysztof Grobelny const dbus::utility::DBusPropertiesMap& ret) { 5638b24275dSEd Tanous if (ec3) 5648bd25ccdSJames Feist { 5658bd25ccdSJames Feist return; // don't have to have this 5668bd25ccdSJames Feist // interface 5678bd25ccdSJames Feist } 5688bd25ccdSJames Feist 56986d89ed7SKrzysztof Grobelny const uint8_t* allowedFailures = nullptr; 570bd79bce8SPatrick Williams const std::vector<std::string>* collection = 571bd79bce8SPatrick Williams nullptr; 57286d89ed7SKrzysztof Grobelny const std::string* status = nullptr; 57386d89ed7SKrzysztof Grobelny 574bd79bce8SPatrick Williams const bool success = 575bd79bce8SPatrick Williams sdbusplus::unpackPropertiesNoThrow( 57686d89ed7SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), ret, 577bd79bce8SPatrick Williams "AllowedFailures", allowedFailures, 578bd79bce8SPatrick Williams "Collection", collection, "Status", 579bd79bce8SPatrick Williams status); 58086d89ed7SKrzysztof Grobelny 58186d89ed7SKrzysztof Grobelny if (!success) 58286d89ed7SKrzysztof Grobelny { 58386d89ed7SKrzysztof Grobelny messages::internalError( 58486d89ed7SKrzysztof Grobelny sensorsAsyncResp->asyncResp->res); 58586d89ed7SKrzysztof Grobelny return; 58686d89ed7SKrzysztof Grobelny } 58786d89ed7SKrzysztof Grobelny 588bd79bce8SPatrick Williams if (allowedFailures == nullptr || 589bd79bce8SPatrick Williams collection == nullptr || status == nullptr) 5908bd25ccdSJames Feist { 591bd79bce8SPatrick Williams BMCWEB_LOG_ERROR( 592bd79bce8SPatrick Williams "Invalid redundancy interface"); 5938bd25ccdSJames Feist messages::internalError( 5948d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 5958bd25ccdSJames Feist return; 5968bd25ccdSJames Feist } 5978bd25ccdSJames Feist 598bd79bce8SPatrick Williams sdbusplus::message::object_path objectPath( 599bd79bce8SPatrick Williams path); 60028aa8de5SGeorge Liu std::string name = objectPath.filename(); 60128aa8de5SGeorge Liu if (name.empty()) 6028bd25ccdSJames Feist { 6038bd25ccdSJames Feist // this should be impossible 6048bd25ccdSJames Feist messages::internalError( 6058d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 6068bd25ccdSJames Feist return; 6078bd25ccdSJames Feist } 60818f8f608SEd Tanous std::ranges::replace(name, '_', ' '); 6098bd25ccdSJames Feist 6108bd25ccdSJames Feist std::string health; 6118bd25ccdSJames Feist 61211ba3979SEd Tanous if (status->ends_with("Full")) 6138bd25ccdSJames Feist { 6148bd25ccdSJames Feist health = "OK"; 6158bd25ccdSJames Feist } 61611ba3979SEd Tanous else if (status->ends_with("Degraded")) 6178bd25ccdSJames Feist { 6188bd25ccdSJames Feist health = "Warning"; 6198bd25ccdSJames Feist } 6208bd25ccdSJames Feist else 6218bd25ccdSJames Feist { 6228bd25ccdSJames Feist health = "Critical"; 6238bd25ccdSJames Feist } 6241476687dSEd Tanous nlohmann::json::array_t redfishCollection; 6258bd25ccdSJames Feist const auto& fanRedfish = 626bd79bce8SPatrick Williams sensorsAsyncResp->asyncResp->res 627bd79bce8SPatrick Williams .jsonValue["Fans"]; 6288bd25ccdSJames Feist for (const std::string& item : *collection) 6298bd25ccdSJames Feist { 630bd79bce8SPatrick Williams sdbusplus::message::object_path itemPath( 631bd79bce8SPatrick Williams item); 6328a592810SEd Tanous std::string itemName = itemPath.filename(); 63328aa8de5SGeorge Liu if (itemName.empty()) 63428aa8de5SGeorge Liu { 63528aa8de5SGeorge Liu continue; 63628aa8de5SGeorge Liu } 6378bd25ccdSJames Feist /* 6388bd25ccdSJames Feist todo(ed): merge patch that fixes the names 6398bd25ccdSJames Feist std::replace(itemName.begin(), 6408bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 6413544d2a7SEd Tanous auto schemaItem = std::ranges::find_if( 642bd79bce8SPatrick Williams fanRedfish, 643bd79bce8SPatrick Williams [itemName](const nlohmann::json& fan) { 6443e35c761SGeorge Liu return fan["Name"] == itemName; 6458bd25ccdSJames Feist }); 6468bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 6478bd25ccdSJames Feist { 6488a592810SEd Tanous nlohmann::json::object_t collectionId; 6498a592810SEd Tanous collectionId["@odata.id"] = 6501476687dSEd Tanous (*schemaItem)["@odata.id"]; 6511476687dSEd Tanous redfishCollection.emplace_back( 6528a592810SEd Tanous std::move(collectionId)); 6538bd25ccdSJames Feist } 6548bd25ccdSJames Feist else 6558bd25ccdSJames Feist { 656bd79bce8SPatrick Williams BMCWEB_LOG_ERROR( 657bd79bce8SPatrick Williams "failed to find fan in schema"); 6588bd25ccdSJames Feist messages::internalError( 6598d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 6608bd25ccdSJames Feist return; 6618bd25ccdSJames Feist } 6628bd25ccdSJames Feist } 6638bd25ccdSJames Feist 664bd79bce8SPatrick Williams size_t minNumNeeded = 665bd79bce8SPatrick Williams collection->empty() 66626f6976fSEd Tanous ? 0 667bd79bce8SPatrick Williams : collection->size() - *allowedFailures; 668bd79bce8SPatrick Williams nlohmann::json& jResp = 669bd79bce8SPatrick Williams sensorsAsyncResp->asyncResp->res 6708bd25ccdSJames Feist .jsonValue["Redundancy"]; 6711476687dSEd Tanous 6721476687dSEd Tanous nlohmann::json::object_t redundancy; 673bd79bce8SPatrick Williams boost::urls::url url = boost::urls::format( 674bd79bce8SPatrick Williams "/redfish/v1/Chassis/{}/{}", 675ef4c65b7SEd Tanous sensorsAsyncResp->chassisId, 676eddfc437SWilly Tu sensorsAsyncResp->chassisSubNode); 677bd79bce8SPatrick Williams url.set_fragment( 678bd79bce8SPatrick Williams ("/Redundancy"_json_pointer / jResp.size()) 679eddfc437SWilly Tu .to_string()); 680eddfc437SWilly Tu redundancy["@odata.id"] = std::move(url); 681bd79bce8SPatrick Williams redundancy["@odata.type"] = 682bd79bce8SPatrick Williams "#Redundancy.v1_3_2.Redundancy"; 6831476687dSEd Tanous redundancy["MinNumNeeded"] = minNumNeeded; 684bd79bce8SPatrick Williams redundancy["Mode"] = 685bd79bce8SPatrick Williams redundancy::RedundancyType::NPlusM; 6861476687dSEd Tanous redundancy["Name"] = name; 6871476687dSEd Tanous redundancy["RedundancySet"] = redfishCollection; 6881476687dSEd Tanous redundancy["Status"]["Health"] = health; 689bd79bce8SPatrick Williams redundancy["Status"]["State"] = 690bd79bce8SPatrick Williams resource::State::Enabled; 6911476687dSEd Tanous 692b2ba3072SPatrick Williams jResp.emplace_back(std::move(redundancy)); 69386d89ed7SKrzysztof Grobelny }); 6941e1e598dSJonathan Doman }); 6958bd25ccdSJames Feist } 696e99073f5SGeorge Liu }); 6978bd25ccdSJames Feist } 6988bd25ccdSJames Feist 699b5a76932SEd Tanous inline void 70081ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 70149c53ac9SJohnathan Mantey { 7028d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 70349c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 704*0c728b42SJanet Adkins if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr) 70549c53ac9SJohnathan Mantey { 70649c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 70749c53ac9SJohnathan Mantey } 70849c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 70949c53ac9SJohnathan Mantey { 71049c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 71149c53ac9SJohnathan Mantey if (entry != response.end()) 71249c53ac9SJohnathan Mantey { 71349c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 71402cad96eSEd Tanous [](const nlohmann::json& c1, const nlohmann::json& c2) { 71549c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 71649c53ac9SJohnathan Mantey }); 71749c53ac9SJohnathan Mantey 71849c53ac9SJohnathan Mantey // add the index counts to the end of each entry 71949c53ac9SJohnathan Mantey size_t count = 0; 72049c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 72149c53ac9SJohnathan Mantey { 72249c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 72349c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 72449c53ac9SJohnathan Mantey { 72549c53ac9SJohnathan Mantey continue; 72649c53ac9SJohnathan Mantey } 72749c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 72849c53ac9SJohnathan Mantey if (value != nullptr) 72949c53ac9SJohnathan Mantey { 730eddfc437SWilly Tu *value += "/" + std::to_string(count); 7313e35c761SGeorge Liu sensorJson["MemberId"] = std::to_string(count); 73249c53ac9SJohnathan Mantey count++; 73381ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 73449c53ac9SJohnathan Mantey } 73549c53ac9SJohnathan Mantey } 73649c53ac9SJohnathan Mantey } 73749c53ac9SJohnathan Mantey } 73849c53ac9SJohnathan Mantey } 73949c53ac9SJohnathan Mantey 74008777fb0SLewanczyk, Dawid /** 741adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 742adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 743adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 744adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 7458fb49dd6SShawn McCarney */ 74623a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 747b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 748adc4f0dbSShawn McCarney const std::string& invItemObjPath) 7498fb49dd6SShawn McCarney { 750adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 7518fb49dd6SShawn McCarney { 752adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 7538fb49dd6SShawn McCarney { 754adc4f0dbSShawn McCarney return &inventoryItem; 7558fb49dd6SShawn McCarney } 7568fb49dd6SShawn McCarney } 7578fb49dd6SShawn McCarney return nullptr; 7588fb49dd6SShawn McCarney } 7598fb49dd6SShawn McCarney 7608fb49dd6SShawn McCarney /** 761adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 762adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 763adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 764adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 7658fb49dd6SShawn McCarney */ 76623a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 767b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 768adc4f0dbSShawn McCarney const std::string& sensorObjPath) 769adc4f0dbSShawn McCarney { 770adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 771adc4f0dbSShawn McCarney { 772db0d36efSEd Tanous if (inventoryItem.sensors.contains(sensorObjPath)) 773adc4f0dbSShawn McCarney { 774adc4f0dbSShawn McCarney return &inventoryItem; 775adc4f0dbSShawn McCarney } 776adc4f0dbSShawn McCarney } 777adc4f0dbSShawn McCarney return nullptr; 778adc4f0dbSShawn McCarney } 779adc4f0dbSShawn McCarney 780adc4f0dbSShawn McCarney /** 781d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 782d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 783d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 784d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 785d500549bSAnthony Wilson */ 786bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed( 787bd79bce8SPatrick Williams std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath) 788d500549bSAnthony Wilson { 789d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 790d500549bSAnthony Wilson { 791d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 792d500549bSAnthony Wilson { 793d500549bSAnthony Wilson return &inventoryItem; 794d500549bSAnthony Wilson } 795d500549bSAnthony Wilson } 796d500549bSAnthony Wilson return nullptr; 797d500549bSAnthony Wilson } 798d500549bSAnthony Wilson 799d500549bSAnthony Wilson /** 800adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 801adc4f0dbSShawn McCarney * 802adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 803adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 804adc4f0dbSShawn McCarney * added to the vector. 805adc4f0dbSShawn McCarney * 806adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 807adc4f0dbSShawn McCarney * InventoryItem. 808adc4f0dbSShawn McCarney * 809adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 810adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 811adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 812adc4f0dbSShawn McCarney */ 813b5a76932SEd Tanous inline void addInventoryItem( 814b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 815b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 816adc4f0dbSShawn McCarney { 817adc4f0dbSShawn McCarney // Look for inventory item in vector 818bd79bce8SPatrick Williams InventoryItem* inventoryItem = 819bd79bce8SPatrick Williams findInventoryItem(inventoryItems, invItemObjPath); 820adc4f0dbSShawn McCarney 821adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 822adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 823adc4f0dbSShawn McCarney { 824adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 825adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 826adc4f0dbSShawn McCarney } 827adc4f0dbSShawn McCarney 828adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 829adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 830adc4f0dbSShawn McCarney } 831adc4f0dbSShawn McCarney 832adc4f0dbSShawn McCarney /** 833adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 834adc4f0dbSShawn McCarney * 835adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 836adc4f0dbSShawn McCarney * specified InventoryItem. 837adc4f0dbSShawn McCarney * 838adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 839adc4f0dbSShawn McCarney * response. 840adc4f0dbSShawn McCarney * 841adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 842adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 843adc4f0dbSShawn McCarney * for the specified inventory item. 844adc4f0dbSShawn McCarney */ 84523a21a1cSEd Tanous inline void storeInventoryItemData( 846adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 84780f79a40SMichael Shen const dbus::utility::DBusInterfacesMap& interfacesDict) 8488fb49dd6SShawn McCarney { 849adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 850711ac7a9SEd Tanous 8519eb808c1SEd Tanous for (const auto& [interface, values] : interfacesDict) 8528fb49dd6SShawn McCarney { 853711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 8548fb49dd6SShawn McCarney { 8559eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 856711ac7a9SEd Tanous { 857711ac7a9SEd Tanous if (name == "Present") 858711ac7a9SEd Tanous { 859711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 860adc4f0dbSShawn McCarney if (value != nullptr) 8618fb49dd6SShawn McCarney { 862adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 8638fb49dd6SShawn McCarney } 8648fb49dd6SShawn McCarney } 8658fb49dd6SShawn McCarney } 866711ac7a9SEd Tanous } 867adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 868711ac7a9SEd Tanous 869711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 8708fb49dd6SShawn McCarney { 871adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 8728fb49dd6SShawn McCarney } 873adc4f0dbSShawn McCarney 874adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 875711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 876adc4f0dbSShawn McCarney { 8779eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 878711ac7a9SEd Tanous { 879711ac7a9SEd Tanous if (name == "Manufacturer") 880adc4f0dbSShawn McCarney { 881adc4f0dbSShawn McCarney const std::string* value = 882711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 883adc4f0dbSShawn McCarney if (value != nullptr) 884adc4f0dbSShawn McCarney { 885adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 886adc4f0dbSShawn McCarney } 887adc4f0dbSShawn McCarney } 888711ac7a9SEd Tanous if (name == "Model") 889adc4f0dbSShawn McCarney { 890adc4f0dbSShawn McCarney const std::string* value = 891711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 892adc4f0dbSShawn McCarney if (value != nullptr) 893adc4f0dbSShawn McCarney { 894adc4f0dbSShawn McCarney inventoryItem.model = *value; 895adc4f0dbSShawn McCarney } 896adc4f0dbSShawn McCarney } 897711ac7a9SEd Tanous if (name == "SerialNumber") 898adc4f0dbSShawn McCarney { 899adc4f0dbSShawn McCarney const std::string* value = 900711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 901adc4f0dbSShawn McCarney if (value != nullptr) 902adc4f0dbSShawn McCarney { 903adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 904adc4f0dbSShawn McCarney } 905adc4f0dbSShawn McCarney } 906711ac7a9SEd Tanous if (name == "PartNumber") 907711ac7a9SEd Tanous { 908711ac7a9SEd Tanous const std::string* value = 909711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 910711ac7a9SEd Tanous if (value != nullptr) 911711ac7a9SEd Tanous { 912711ac7a9SEd Tanous inventoryItem.partNumber = *value; 913711ac7a9SEd Tanous } 914711ac7a9SEd Tanous } 915711ac7a9SEd Tanous } 916adc4f0dbSShawn McCarney } 917adc4f0dbSShawn McCarney 918711ac7a9SEd Tanous if (interface == 919711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 920adc4f0dbSShawn McCarney { 9219eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 922adc4f0dbSShawn McCarney { 923711ac7a9SEd Tanous if (name == "Functional") 924711ac7a9SEd Tanous { 925711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 926adc4f0dbSShawn McCarney if (value != nullptr) 927adc4f0dbSShawn McCarney { 928adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 9298fb49dd6SShawn McCarney } 9308fb49dd6SShawn McCarney } 9318fb49dd6SShawn McCarney } 9328fb49dd6SShawn McCarney } 933711ac7a9SEd Tanous } 934711ac7a9SEd Tanous } 9358fb49dd6SShawn McCarney 9368fb49dd6SShawn McCarney /** 937adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 9388fb49dd6SShawn McCarney * 939adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 940adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 941adc4f0dbSShawn McCarney * inventoryItems vector. 9428fb49dd6SShawn McCarney * 943adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 944adc4f0dbSShawn McCarney * response. 945adc4f0dbSShawn McCarney * 946adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 947adc4f0dbSShawn McCarney * been obtained. 948adc4f0dbSShawn McCarney * 949adc4f0dbSShawn McCarney * The callback must have the following signature: 950adc4f0dbSShawn McCarney * @code 951d500549bSAnthony Wilson * callback(void) 952adc4f0dbSShawn McCarney * @endcode 953adc4f0dbSShawn McCarney * 954adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 955adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 956adc4f0dbSShawn McCarney * last asynchronous function has completed. 9578fb49dd6SShawn McCarney * 9588fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 959adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 960adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 9618fb49dd6SShawn McCarney * implements ObjectManager. 962adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 963adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 964adc4f0dbSShawn McCarney * in recursive calls to this function. 9658fb49dd6SShawn McCarney */ 966adc4f0dbSShawn McCarney template <typename Callback> 967adc4f0dbSShawn McCarney static void getInventoryItemsData( 9688fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 969adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 970d0090733SEd Tanous std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback, 971d0090733SEd Tanous size_t invConnectionsIndex = 0) 9728fb49dd6SShawn McCarney { 97362598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsData enter"); 9748fb49dd6SShawn McCarney 975adc4f0dbSShawn McCarney // If no more connections left, call callback 976adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 9778fb49dd6SShawn McCarney { 978d500549bSAnthony Wilson callback(); 97962598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); 980adc4f0dbSShawn McCarney return; 981adc4f0dbSShawn McCarney } 982adc4f0dbSShawn McCarney 983adc4f0dbSShawn McCarney // Get inventory item data from current connection 984fe04d49cSNan Zhou auto it = invConnections->begin(); 985fe04d49cSNan Zhou std::advance(it, invConnectionsIndex); 986adc4f0dbSShawn McCarney if (it != invConnections->end()) 987adc4f0dbSShawn McCarney { 988adc4f0dbSShawn McCarney const std::string& invConnection = *it; 989adc4f0dbSShawn McCarney 9905eb468daSGeorge Liu // Get all object paths and their interfaces for current connection 9915eb468daSGeorge Liu sdbusplus::message::object_path path("/xyz/openbmc_project/inventory"); 9925eb468daSGeorge Liu dbus::utility::getManagedObjects( 9935eb468daSGeorge Liu invConnection, path, 9945eb468daSGeorge Liu [sensorsAsyncResp, inventoryItems, invConnections, 9958cb2c024SEd Tanous callback = std::forward<Callback>(callback), invConnectionsIndex]( 9965e7e2dc5SEd Tanous const boost::system::error_code& ec, 99702cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 99862598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter"); 9998fb49dd6SShawn McCarney if (ec) 10008fb49dd6SShawn McCarney { 100162598e31SEd Tanous BMCWEB_LOG_ERROR( 100262598e31SEd Tanous "getInventoryItemsData respHandler DBus error {}", ec); 10038d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 10048fb49dd6SShawn McCarney return; 10058fb49dd6SShawn McCarney } 10068fb49dd6SShawn McCarney 10078fb49dd6SShawn McCarney // Loop through returned object paths 10088fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 10098fb49dd6SShawn McCarney { 10108fb49dd6SShawn McCarney const std::string& objPath = 10118fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 10128fb49dd6SShawn McCarney 1013bd79bce8SPatrick Williams // If this object path is one of the specified inventory 1014bd79bce8SPatrick Williams // items 1015bd79bce8SPatrick Williams InventoryItem* inventoryItem = 1016bd79bce8SPatrick Williams findInventoryItem(inventoryItems, objPath); 1017adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 10188fb49dd6SShawn McCarney { 1019adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1020bd79bce8SPatrick Williams storeInventoryItemData(*inventoryItem, 1021bd79bce8SPatrick Williams objDictEntry.second); 10228fb49dd6SShawn McCarney } 10238fb49dd6SShawn McCarney } 10248fb49dd6SShawn McCarney 1025adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1026adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1027d0090733SEd Tanous invConnections, std::move(callback), 1028d0090733SEd Tanous invConnectionsIndex + 1); 1029adc4f0dbSShawn McCarney 103062598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit"); 10315eb468daSGeorge Liu }); 10328fb49dd6SShawn McCarney } 10338fb49dd6SShawn McCarney 103462598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); 10358fb49dd6SShawn McCarney } 10368fb49dd6SShawn McCarney 10378fb49dd6SShawn McCarney /** 1038adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 10398fb49dd6SShawn McCarney * 1040adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1041adc4f0dbSShawn McCarney * items that are associated with sensors. 10428fb49dd6SShawn McCarney * 10438fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 10448fb49dd6SShawn McCarney * been obtained. 10458fb49dd6SShawn McCarney * 10468fb49dd6SShawn McCarney * The callback must have the following signature: 10478fb49dd6SShawn McCarney * @code 1048fe04d49cSNan Zhou * callback(std::shared_ptr<std::set<std::string>> invConnections) 10498fb49dd6SShawn McCarney * @endcode 10508fb49dd6SShawn McCarney * 10518fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1052adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 10538fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 10548fb49dd6SShawn McCarney */ 10558fb49dd6SShawn McCarney template <typename Callback> 10568fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1057b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1058b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 10598fb49dd6SShawn McCarney Callback&& callback) 10608fb49dd6SShawn McCarney { 106162598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter"); 10628fb49dd6SShawn McCarney 10638fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1064e99073f5SGeorge Liu constexpr std::array<std::string_view, 4> interfaces = { 10658fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1066adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1067adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 10688fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 10698fb49dd6SShawn McCarney 1070e99073f5SGeorge Liu // Make call to ObjectMapper to find all inventory items 1071e99073f5SGeorge Liu dbus::utility::getSubTree( 1072e99073f5SGeorge Liu path, 0, interfaces, 10738cb2c024SEd Tanous [callback = std::forward<Callback>(callback), sensorsAsyncResp, 1074002d39b4SEd Tanous inventoryItems]( 1075e99073f5SGeorge Liu const boost::system::error_code& ec, 1076002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1077e99073f5SGeorge Liu // Response handler for parsing output from GetSubTree 107862598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter"); 10798fb49dd6SShawn McCarney if (ec) 10808fb49dd6SShawn McCarney { 10818d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 108262598e31SEd Tanous BMCWEB_LOG_ERROR( 1083bd79bce8SPatrick Williams "getInventoryItemsConnections respHandler DBus error {}", 1084bd79bce8SPatrick Williams ec); 10858fb49dd6SShawn McCarney return; 10868fb49dd6SShawn McCarney } 10878fb49dd6SShawn McCarney 10888fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 1089fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections = 1090fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 10918fb49dd6SShawn McCarney 10928fb49dd6SShawn McCarney // Loop through objects from GetSubTree 1093bd79bce8SPatrick Williams for (const std::pair<std::string, 1094bd79bce8SPatrick Williams std::vector<std::pair< 1095bd79bce8SPatrick Williams std::string, std::vector<std::string>>>>& 10968fb49dd6SShawn McCarney object : subtree) 10978fb49dd6SShawn McCarney { 1098adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 10998fb49dd6SShawn McCarney const std::string& objPath = object.first; 1100adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 11018fb49dd6SShawn McCarney { 11028fb49dd6SShawn McCarney // Store all connections to inventory item 11038fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 11048fb49dd6SShawn McCarney objData : object.second) 11058fb49dd6SShawn McCarney { 11068fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 11078fb49dd6SShawn McCarney invConnections->insert(invConnection); 11088fb49dd6SShawn McCarney } 11098fb49dd6SShawn McCarney } 11108fb49dd6SShawn McCarney } 1111d500549bSAnthony Wilson 11128fb49dd6SShawn McCarney callback(invConnections); 111362598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit"); 1114e99073f5SGeorge Liu }); 111562598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit"); 11168fb49dd6SShawn McCarney } 11178fb49dd6SShawn McCarney 11188fb49dd6SShawn McCarney /** 1119adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 11208fb49dd6SShawn McCarney * 11218fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1122d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1123d500549bSAnthony Wilson * their LEDs, if any. 11248fb49dd6SShawn McCarney * 11258fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 11268fb49dd6SShawn McCarney * has been obtained. 11278fb49dd6SShawn McCarney * 11288fb49dd6SShawn McCarney * The callback must have the following signature: 11298fb49dd6SShawn McCarney * @code 1130adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 11318fb49dd6SShawn McCarney * @endcode 11328fb49dd6SShawn McCarney * 11338fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 11348fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 11358fb49dd6SShawn McCarney * implements ObjectManager. 11368fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 11378fb49dd6SShawn McCarney */ 11388fb49dd6SShawn McCarney template <typename Callback> 1139adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1140b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1141fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 11428fb49dd6SShawn McCarney Callback&& callback) 11438fb49dd6SShawn McCarney { 114462598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter"); 11458fb49dd6SShawn McCarney 11465eb468daSGeorge Liu // Call GetManagedObjects on the ObjectMapper to get all associations 11475eb468daSGeorge Liu sdbusplus::message::object_path path("/"); 11485eb468daSGeorge Liu dbus::utility::getManagedObjects( 11495eb468daSGeorge Liu "xyz.openbmc_project.ObjectMapper", path, 11508cb2c024SEd Tanous [callback = std::forward<Callback>(callback), sensorsAsyncResp, 11515e7e2dc5SEd Tanous sensorNames](const boost::system::error_code& ec, 115202cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 115362598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter"); 11548fb49dd6SShawn McCarney if (ec) 11558fb49dd6SShawn McCarney { 115662598e31SEd Tanous BMCWEB_LOG_ERROR( 1157bd79bce8SPatrick Williams "getInventoryItemAssociations respHandler DBus error {}", 1158bd79bce8SPatrick Williams ec); 11598d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 11608fb49dd6SShawn McCarney return; 11618fb49dd6SShawn McCarney } 11628fb49dd6SShawn McCarney 1163adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1164adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1165adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1166adc4f0dbSShawn McCarney 11678fb49dd6SShawn McCarney // Loop through returned object paths 11688fb49dd6SShawn McCarney std::string sensorAssocPath; 11698fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 11708fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 11718fb49dd6SShawn McCarney { 11728fb49dd6SShawn McCarney const std::string& objPath = 11738fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 11748fb49dd6SShawn McCarney 1175bd79bce8SPatrick Williams // If path is inventory association for one of the specified 1176bd79bce8SPatrick Williams // sensors 11778fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 11788fb49dd6SShawn McCarney { 11798fb49dd6SShawn McCarney sensorAssocPath = sensorName; 11808fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 11818fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 11828fb49dd6SShawn McCarney { 11838fb49dd6SShawn McCarney // Get Association interface for object path 1184bd79bce8SPatrick Williams for (const auto& [interface, values] : 1185bd79bce8SPatrick Williams objDictEntry.second) 11868fb49dd6SShawn McCarney { 1187711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1188711ac7a9SEd Tanous { 1189711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1190711ac7a9SEd Tanous { 1191711ac7a9SEd Tanous if (valueName == "endpoints") 11928fb49dd6SShawn McCarney { 1193bd79bce8SPatrick Williams const std::vector<std::string>* 1194bd79bce8SPatrick Williams endpoints = std::get_if< 1195bd79bce8SPatrick Williams std::vector<std::string>>( 1196711ac7a9SEd Tanous &value); 1197711ac7a9SEd Tanous if ((endpoints != nullptr) && 1198711ac7a9SEd Tanous !endpoints->empty()) 11998fb49dd6SShawn McCarney { 1200adc4f0dbSShawn McCarney // Add inventory item to vector 1201adc4f0dbSShawn McCarney const std::string& invItemPath = 1202adc4f0dbSShawn McCarney endpoints->front(); 1203711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1204711ac7a9SEd Tanous invItemPath, 1205adc4f0dbSShawn McCarney sensorName); 12068fb49dd6SShawn McCarney } 12078fb49dd6SShawn McCarney } 12088fb49dd6SShawn McCarney } 1209711ac7a9SEd Tanous } 1210711ac7a9SEd Tanous } 12118fb49dd6SShawn McCarney break; 12128fb49dd6SShawn McCarney } 12138fb49dd6SShawn McCarney } 12148fb49dd6SShawn McCarney } 12158fb49dd6SShawn McCarney 1216d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1217d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1218d500549bSAnthony Wilson std::string inventoryAssocPath; 1219d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1220d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1221d500549bSAnthony Wilson { 1222d500549bSAnthony Wilson const std::string& objPath = 1223d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1224d500549bSAnthony Wilson 1225d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1226d500549bSAnthony Wilson { 1227d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1228d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1229d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1230d500549bSAnthony Wilson { 1231bd79bce8SPatrick Williams for (const auto& [interface, values] : 1232bd79bce8SPatrick Williams objDictEntry.second) 1233d500549bSAnthony Wilson { 1234711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1235711ac7a9SEd Tanous { 1236711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1237711ac7a9SEd Tanous { 1238711ac7a9SEd Tanous if (valueName == "endpoints") 1239d500549bSAnthony Wilson { 1240bd79bce8SPatrick Williams const std::vector<std::string>* 1241bd79bce8SPatrick Williams endpoints = std::get_if< 1242bd79bce8SPatrick Williams std::vector<std::string>>( 1243711ac7a9SEd Tanous &value); 1244711ac7a9SEd Tanous if ((endpoints != nullptr) && 1245711ac7a9SEd Tanous !endpoints->empty()) 1246d500549bSAnthony Wilson { 1247711ac7a9SEd Tanous // Add inventory item to vector 1248d500549bSAnthony Wilson // Store LED path in inventory item 1249711ac7a9SEd Tanous const std::string& ledPath = 1250711ac7a9SEd Tanous endpoints->front(); 1251bd79bce8SPatrick Williams inventoryItem.ledObjectPath = 1252bd79bce8SPatrick Williams ledPath; 1253d500549bSAnthony Wilson } 1254d500549bSAnthony Wilson } 1255d500549bSAnthony Wilson } 1256711ac7a9SEd Tanous } 1257711ac7a9SEd Tanous } 1258711ac7a9SEd Tanous 1259d500549bSAnthony Wilson break; 1260d500549bSAnthony Wilson } 1261d500549bSAnthony Wilson } 1262d500549bSAnthony Wilson } 1263adc4f0dbSShawn McCarney callback(inventoryItems); 126462598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit"); 12655eb468daSGeorge Liu }); 12668fb49dd6SShawn McCarney 126762598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit"); 12688fb49dd6SShawn McCarney } 12698fb49dd6SShawn McCarney 12708fb49dd6SShawn McCarney /** 1271d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1272d500549bSAnthony Wilson * 1273d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1274d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1275d500549bSAnthony Wilson * inventoryItems vector. 1276d500549bSAnthony Wilson * 1277d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1278d500549bSAnthony Wilson * response. 1279d500549bSAnthony Wilson * 1280d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1281d500549bSAnthony Wilson * has been obtained. 1282d500549bSAnthony Wilson * 1283d500549bSAnthony Wilson * The callback must have the following signature: 1284d500549bSAnthony Wilson * @code 128542cbe538SGunnar Mills * callback() 1286d500549bSAnthony Wilson * @endcode 1287d500549bSAnthony Wilson * 1288d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1289d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1290d500549bSAnthony Wilson * last asynchronous function has completed. 1291d500549bSAnthony Wilson * 1292d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1293d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1294d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1295d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1296d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1297d500549bSAnthony Wilson * in recursive calls to this function. 1298d500549bSAnthony Wilson */ 1299d500549bSAnthony Wilson template <typename Callback> 1300d500549bSAnthony Wilson void getInventoryLedData( 1301d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1302d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1303fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections, 1304d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1305d500549bSAnthony Wilson { 130662598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedData enter"); 1307d500549bSAnthony Wilson 1308d500549bSAnthony Wilson // If no more connections left, call callback 1309d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1310d500549bSAnthony Wilson { 131142cbe538SGunnar Mills callback(); 131262598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedData exit"); 1313d500549bSAnthony Wilson return; 1314d500549bSAnthony Wilson } 1315d500549bSAnthony Wilson 1316d500549bSAnthony Wilson // Get inventory item data from current connection 1317fe04d49cSNan Zhou auto it = ledConnections->begin(); 1318fe04d49cSNan Zhou std::advance(it, ledConnectionsIndex); 1319d500549bSAnthony Wilson if (it != ledConnections->end()) 1320d500549bSAnthony Wilson { 1321d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1322d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1323d500549bSAnthony Wilson // Response handler for Get State property 13241e1e598dSJonathan Doman auto respHandler = 13251e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 13268cb2c024SEd Tanous callback = std::forward<Callback>(callback), ledConnectionsIndex]( 13275e7e2dc5SEd Tanous const boost::system::error_code& ec, const std::string& state) { 132862598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter"); 1329d500549bSAnthony Wilson if (ec) 1330d500549bSAnthony Wilson { 133162598e31SEd Tanous BMCWEB_LOG_ERROR( 133262598e31SEd Tanous "getInventoryLedData respHandler DBus error {}", ec); 13338d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1334d500549bSAnthony Wilson return; 1335d500549bSAnthony Wilson } 1336d500549bSAnthony Wilson 133762598e31SEd Tanous BMCWEB_LOG_DEBUG("Led state: {}", state); 1338d500549bSAnthony Wilson // Find inventory item with this LED object path 1339d500549bSAnthony Wilson InventoryItem* inventoryItem = 1340d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1341d500549bSAnthony Wilson if (inventoryItem != nullptr) 1342d500549bSAnthony Wilson { 1343d500549bSAnthony Wilson // Store LED state in InventoryItem 134411ba3979SEd Tanous if (state.ends_with("On")) 1345d500549bSAnthony Wilson { 1346c9563608SJanet Adkins inventoryItem->ledState = sensor_utils::LedState::ON; 1347d500549bSAnthony Wilson } 134811ba3979SEd Tanous else if (state.ends_with("Blink")) 1349d500549bSAnthony Wilson { 1350c9563608SJanet Adkins inventoryItem->ledState = sensor_utils::LedState::BLINK; 1351d500549bSAnthony Wilson } 135211ba3979SEd Tanous else if (state.ends_with("Off")) 1353d500549bSAnthony Wilson { 1354c9563608SJanet Adkins inventoryItem->ledState = sensor_utils::LedState::OFF; 1355d500549bSAnthony Wilson } 1356d500549bSAnthony Wilson else 1357d500549bSAnthony Wilson { 1358c9563608SJanet Adkins inventoryItem->ledState = 1359c9563608SJanet Adkins sensor_utils::LedState::UNKNOWN; 1360d500549bSAnthony Wilson } 1361d500549bSAnthony Wilson } 1362d500549bSAnthony Wilson 1363d500549bSAnthony Wilson // Recurse to get LED data from next connection 1364d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1365d500549bSAnthony Wilson ledConnections, std::move(callback), 1366d500549bSAnthony Wilson ledConnectionsIndex + 1); 1367d500549bSAnthony Wilson 136862598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit"); 1369d500549bSAnthony Wilson }; 1370d500549bSAnthony Wilson 1371d500549bSAnthony Wilson // Get the State property for the current LED 13721e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 13731e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 13741e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 13751e1e598dSJonathan Doman std::move(respHandler)); 1376d500549bSAnthony Wilson } 1377d500549bSAnthony Wilson 137862598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedData exit"); 1379d500549bSAnthony Wilson } 1380d500549bSAnthony Wilson 1381d500549bSAnthony Wilson /** 1382d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1383d500549bSAnthony Wilson * 1384d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1385d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1386d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1387d500549bSAnthony Wilson * 1388d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1389d500549bSAnthony Wilson * response. 1390d500549bSAnthony Wilson * 1391d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1392d500549bSAnthony Wilson * been obtained. 1393d500549bSAnthony Wilson * 1394d500549bSAnthony Wilson * The callback must have the following signature: 1395d500549bSAnthony Wilson * @code 139642cbe538SGunnar Mills * callback() 1397d500549bSAnthony Wilson * @endcode 1398d500549bSAnthony Wilson * 1399d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1400d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1401d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1402d500549bSAnthony Wilson */ 1403d500549bSAnthony Wilson template <typename Callback> 1404d500549bSAnthony Wilson void getInventoryLeds( 1405d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1406d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1407d500549bSAnthony Wilson Callback&& callback) 1408d500549bSAnthony Wilson { 140962598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLeds enter"); 1410d500549bSAnthony Wilson 1411d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 1412e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1413d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 1414d500549bSAnthony Wilson 1415e99073f5SGeorge Liu // Make call to ObjectMapper to find all inventory items 1416e99073f5SGeorge Liu dbus::utility::getSubTree( 1417e99073f5SGeorge Liu path, 0, interfaces, 14188cb2c024SEd Tanous [callback = std::forward<Callback>(callback), sensorsAsyncResp, 1419002d39b4SEd Tanous inventoryItems]( 1420e99073f5SGeorge Liu const boost::system::error_code& ec, 1421002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1422e99073f5SGeorge Liu // Response handler for parsing output from GetSubTree 142362598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter"); 1424d500549bSAnthony Wilson if (ec) 1425d500549bSAnthony Wilson { 14268d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1427bd79bce8SPatrick Williams BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", 1428bd79bce8SPatrick Williams ec); 1429d500549bSAnthony Wilson return; 1430d500549bSAnthony Wilson } 1431d500549bSAnthony Wilson 1432d500549bSAnthony Wilson // Build map of LED object paths to connections 1433fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections = 1434fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 1435d500549bSAnthony Wilson 1436d500549bSAnthony Wilson // Loop through objects from GetSubTree 1437bd79bce8SPatrick Williams for (const std::pair<std::string, 1438bd79bce8SPatrick Williams std::vector<std::pair< 1439bd79bce8SPatrick Williams std::string, std::vector<std::string>>>>& 1440d500549bSAnthony Wilson object : subtree) 1441d500549bSAnthony Wilson { 1442bd79bce8SPatrick Williams // Check if object path is LED for one of the specified 1443bd79bce8SPatrick Williams // inventory items 1444d500549bSAnthony Wilson const std::string& ledPath = object.first; 1445bd79bce8SPatrick Williams if (findInventoryItemForLed(*inventoryItems, ledPath) != 1446bd79bce8SPatrick Williams nullptr) 1447d500549bSAnthony Wilson { 1448d500549bSAnthony Wilson // Add mapping from ledPath to connection 1449bd79bce8SPatrick Williams const std::string& connection = 1450bd79bce8SPatrick Williams object.second.begin()->first; 1451d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 1452bd79bce8SPatrick Williams BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, 1453bd79bce8SPatrick Williams connection); 1454d500549bSAnthony Wilson } 1455d500549bSAnthony Wilson } 1456d500549bSAnthony Wilson 1457bd79bce8SPatrick Williams getInventoryLedData(sensorsAsyncResp, inventoryItems, 1458bd79bce8SPatrick Williams ledConnections, std::move(callback)); 145962598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit"); 1460e99073f5SGeorge Liu }); 146162598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLeds exit"); 1462d500549bSAnthony Wilson } 1463d500549bSAnthony Wilson 1464d500549bSAnthony Wilson /** 146542cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 146642cbe538SGunnar Mills * 146742cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 146842cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 146942cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 147042cbe538SGunnar Mills * 147142cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 147242cbe538SGunnar Mills * response. 147342cbe538SGunnar Mills * 147442cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 147542cbe538SGunnar Mills * when data has been obtained. 147642cbe538SGunnar Mills * 147742cbe538SGunnar Mills * The callback must have the following signature: 147842cbe538SGunnar Mills * @code 147942cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 148042cbe538SGunnar Mills * @endcode 148142cbe538SGunnar Mills * 148242cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 148342cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 148442cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 148542cbe538SGunnar Mills * Supply Attributes 148642cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 148742cbe538SGunnar Mills */ 148842cbe538SGunnar Mills template <typename Callback> 148942cbe538SGunnar Mills void getPowerSupplyAttributesData( 1490b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 149142cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1492fe04d49cSNan Zhou const std::map<std::string, std::string>& psAttributesConnections, 149342cbe538SGunnar Mills Callback&& callback) 149442cbe538SGunnar Mills { 149562598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter"); 149642cbe538SGunnar Mills 149742cbe538SGunnar Mills if (psAttributesConnections.empty()) 149842cbe538SGunnar Mills { 149962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!"); 150042cbe538SGunnar Mills callback(inventoryItems); 150142cbe538SGunnar Mills return; 150242cbe538SGunnar Mills } 150342cbe538SGunnar Mills 150442cbe538SGunnar Mills // Assuming just one connection (service) for now 1505fe04d49cSNan Zhou auto it = psAttributesConnections.begin(); 150642cbe538SGunnar Mills 150742cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 150842cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 150942cbe538SGunnar Mills 151042cbe538SGunnar Mills // Response handler for Get DeratingFactor property 15115a39f77aSPatrick Williams auto respHandler = [sensorsAsyncResp, inventoryItems, 15128cb2c024SEd Tanous callback = std::forward<Callback>(callback)]( 15135a39f77aSPatrick Williams const boost::system::error_code& ec, 15145a39f77aSPatrick Williams const uint32_t value) { 151562598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter"); 151642cbe538SGunnar Mills if (ec) 151742cbe538SGunnar Mills { 151862598e31SEd Tanous BMCWEB_LOG_ERROR( 151962598e31SEd Tanous "getPowerSupplyAttributesData respHandler DBus error {}", ec); 15208d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 152142cbe538SGunnar Mills return; 152242cbe538SGunnar Mills } 152342cbe538SGunnar Mills 152462598e31SEd Tanous BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value); 152542cbe538SGunnar Mills // Store value in Power Supply Inventory Items 152642cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 152742cbe538SGunnar Mills { 152855f79e6fSEd Tanous if (inventoryItem.isPowerSupply) 152942cbe538SGunnar Mills { 153042cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 15311e1e598dSJonathan Doman static_cast<int>(value); 153242cbe538SGunnar Mills } 153342cbe538SGunnar Mills } 153442cbe538SGunnar Mills 153562598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit"); 153642cbe538SGunnar Mills callback(inventoryItems); 153742cbe538SGunnar Mills }; 153842cbe538SGunnar Mills 153942cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 154042cbe538SGunnar Mills // Currently only property on the interface/only one we care about 15411e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 15421e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 15431e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 15441e1e598dSJonathan Doman std::move(respHandler)); 154542cbe538SGunnar Mills 154662598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit"); 154742cbe538SGunnar Mills } 154842cbe538SGunnar Mills 154942cbe538SGunnar Mills /** 155042cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 155142cbe538SGunnar Mills * 155242cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 155342cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 155442cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 155542cbe538SGunnar Mills * item. 155642cbe538SGunnar Mills * 155742cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 155842cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 155942cbe538SGunnar Mills * 156042cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 156142cbe538SGunnar Mills * when information has been obtained. 156242cbe538SGunnar Mills * 156342cbe538SGunnar Mills * The callback must have the following signature: 156442cbe538SGunnar Mills * @code 156542cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 156642cbe538SGunnar Mills * @endcode 156742cbe538SGunnar Mills * 156842cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 156942cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 157042cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 157142cbe538SGunnar Mills */ 157242cbe538SGunnar Mills template <typename Callback> 157342cbe538SGunnar Mills void getPowerSupplyAttributes( 157442cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 157542cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 157642cbe538SGunnar Mills Callback&& callback) 157742cbe538SGunnar Mills { 157862598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter"); 157942cbe538SGunnar Mills 158042cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 1581*0c728b42SJanet Adkins if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr) 158242cbe538SGunnar Mills { 158362598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power"); 158442cbe538SGunnar Mills callback(inventoryItems); 158542cbe538SGunnar Mills return; 158642cbe538SGunnar Mills } 158742cbe538SGunnar Mills 1588e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 158942cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 159042cbe538SGunnar Mills 1591e99073f5SGeorge Liu // Make call to ObjectMapper to find the PowerSupplyAttributes service 1592e99073f5SGeorge Liu dbus::utility::getSubTree( 1593e99073f5SGeorge Liu "/xyz/openbmc_project", 0, interfaces, 15948cb2c024SEd Tanous [callback = std::forward<Callback>(callback), sensorsAsyncResp, 1595b9d36b47SEd Tanous inventoryItems]( 1596e99073f5SGeorge Liu const boost::system::error_code& ec, 1597b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1598e99073f5SGeorge Liu // Response handler for parsing output from GetSubTree 159962598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter"); 160042cbe538SGunnar Mills if (ec) 160142cbe538SGunnar Mills { 16028d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 160362598e31SEd Tanous BMCWEB_LOG_ERROR( 160462598e31SEd Tanous "getPowerSupplyAttributes respHandler DBus error {}", ec); 160542cbe538SGunnar Mills return; 160642cbe538SGunnar Mills } 160726f6976fSEd Tanous if (subtree.empty()) 160842cbe538SGunnar Mills { 160962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!"); 161042cbe538SGunnar Mills callback(inventoryItems); 161142cbe538SGunnar Mills return; 161242cbe538SGunnar Mills } 161342cbe538SGunnar Mills 161442cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 161542cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 161642cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 1617fe04d49cSNan Zhou std::map<std::string, std::string> psAttributesConnections; 161842cbe538SGunnar Mills 161942cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 162042cbe538SGunnar Mills { 162162598e31SEd Tanous BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); 162242cbe538SGunnar Mills callback(inventoryItems); 162342cbe538SGunnar Mills return; 162442cbe538SGunnar Mills } 162542cbe538SGunnar Mills 162642cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 162742cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 162842cbe538SGunnar Mills 162942cbe538SGunnar Mills if (connection.empty()) 163042cbe538SGunnar Mills { 163162598e31SEd Tanous BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); 163242cbe538SGunnar Mills callback(inventoryItems); 163342cbe538SGunnar Mills return; 163442cbe538SGunnar Mills } 163542cbe538SGunnar Mills 163642cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 163762598e31SEd Tanous BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath, 163862598e31SEd Tanous connection); 163942cbe538SGunnar Mills 164042cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 164142cbe538SGunnar Mills psAttributesConnections, 164242cbe538SGunnar Mills std::move(callback)); 164362598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit"); 1644e99073f5SGeorge Liu }); 164562598e31SEd Tanous BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit"); 164642cbe538SGunnar Mills } 164742cbe538SGunnar Mills 164842cbe538SGunnar Mills /** 1649adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 16508fb49dd6SShawn McCarney * 16518fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 1652adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 16538fb49dd6SShawn McCarney * 1654adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1655adc4f0dbSShawn McCarney * response. 16568fb49dd6SShawn McCarney * 1657adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 1658adc4f0dbSShawn McCarney * inventory items have been obtained. 1659adc4f0dbSShawn McCarney * 1660adc4f0dbSShawn McCarney * The callback must have the following signature: 1661adc4f0dbSShawn McCarney * @code 1662adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 1663adc4f0dbSShawn McCarney * @endcode 16648fb49dd6SShawn McCarney * 16658fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 16668fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 16678fb49dd6SShawn McCarney * implements ObjectManager. 1668adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 16698fb49dd6SShawn McCarney */ 1670adc4f0dbSShawn McCarney template <typename Callback> 1671d0090733SEd Tanous static void 1672d0090733SEd Tanous getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1673fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 1674adc4f0dbSShawn McCarney Callback&& callback) 16758fb49dd6SShawn McCarney { 167662598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItems enter"); 1677adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 16788cb2c024SEd Tanous [sensorsAsyncResp, callback = std::forward<Callback>(callback)]( 1679adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 168062598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter"); 16818fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 1682d0090733SEd Tanous [sensorsAsyncResp, inventoryItems, 16838cb2c024SEd Tanous callback = std::forward<const Callback>(callback)]( 1684fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections) { 168562598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter"); 1686bd79bce8SPatrick Williams auto getInventoryItemsDataCb = 1687bd79bce8SPatrick Williams [sensorsAsyncResp, inventoryItems, 1688d500549bSAnthony Wilson callback{std::move(callback)}]() { 168962598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter"); 169042cbe538SGunnar Mills 1691bd79bce8SPatrick Williams auto getInventoryLedsCb = 1692bd79bce8SPatrick Williams [sensorsAsyncResp, inventoryItems, 1693002d39b4SEd Tanous callback{std::move(callback)}]() { 1694bd79bce8SPatrick Williams BMCWEB_LOG_DEBUG( 1695bd79bce8SPatrick Williams "getInventoryLedsCb enter"); 1696bd79bce8SPatrick Williams // Find Power Supply Attributes and get the 1697bd79bce8SPatrick Williams // data 1698bd79bce8SPatrick Williams getPowerSupplyAttributes( 1699bd79bce8SPatrick Williams sensorsAsyncResp, inventoryItems, 170042cbe538SGunnar Mills std::move(callback)); 170162598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryLedsCb exit"); 170242cbe538SGunnar Mills }; 170342cbe538SGunnar Mills 1704d500549bSAnthony Wilson // Find led connections and get the data 1705d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 170642cbe538SGunnar Mills std::move(getInventoryLedsCb)); 170762598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit"); 1708d500549bSAnthony Wilson }; 17098fb49dd6SShawn McCarney 1710adc4f0dbSShawn McCarney // Get inventory item data from connections 1711adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1712d0090733SEd Tanous invConnections, 1713d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 171462598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit"); 17158fb49dd6SShawn McCarney }; 17168fb49dd6SShawn McCarney 1717adc4f0dbSShawn McCarney // Get connections that provide inventory item data 1718bd79bce8SPatrick Williams getInventoryItemsConnections( 1719bd79bce8SPatrick Williams sensorsAsyncResp, inventoryItems, 17208fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 172162598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit"); 17228fb49dd6SShawn McCarney }; 17238fb49dd6SShawn McCarney 1724adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 1725d0090733SEd Tanous getInventoryItemAssociations(sensorsAsyncResp, sensorNames, 1726adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 172762598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItems exit"); 1728adc4f0dbSShawn McCarney } 1729adc4f0dbSShawn McCarney 1730adc4f0dbSShawn McCarney /** 1731adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 1732adc4f0dbSShawn McCarney * 1733adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 1734adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 1735adc4f0dbSShawn McCarney * array. 1736adc4f0dbSShawn McCarney * 1737adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 1738adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 1739adc4f0dbSShawn McCarney * object. 1740adc4f0dbSShawn McCarney * 1741adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 1742adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 1743adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 1744adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 1745adc4f0dbSShawn McCarney */ 174623a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 1747adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 1748adc4f0dbSShawn McCarney const std::string& chassisId) 1749adc4f0dbSShawn McCarney { 175018f8f608SEd Tanous std::string nameS; 17516f4bd290SAlexander Hansen nameS.resize(inventoryItem.name.size()); 175218f8f608SEd Tanous std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' '); 1753adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 1754adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 1755adc4f0dbSShawn McCarney { 175618f8f608SEd Tanous nlohmann::json::iterator nameIt = powerSupply.find("Name"); 175718f8f608SEd Tanous if (nameIt == powerSupply.end()) 175818f8f608SEd Tanous { 175918f8f608SEd Tanous continue; 176018f8f608SEd Tanous } 176118f8f608SEd Tanous const std::string* name = nameIt->get_ptr<std::string*>(); 176218f8f608SEd Tanous if (name == nullptr) 176318f8f608SEd Tanous { 176418f8f608SEd Tanous continue; 176518f8f608SEd Tanous } 176618f8f608SEd Tanous if (nameS == *name) 1767adc4f0dbSShawn McCarney { 1768adc4f0dbSShawn McCarney return powerSupply; 1769adc4f0dbSShawn McCarney } 1770adc4f0dbSShawn McCarney } 1771adc4f0dbSShawn McCarney 1772adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 1773adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 1774adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 1775bd79bce8SPatrick Williams boost::urls::url url = 1776bd79bce8SPatrick Williams boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId); 1777eddfc437SWilly Tu url.set_fragment(("/PowerSupplies"_json_pointer).to_string()); 1778eddfc437SWilly Tu powerSupply["@odata.id"] = std::move(url); 177918f8f608SEd Tanous std::string escaped; 17806f4bd290SAlexander Hansen escaped.resize(inventoryItem.name.size()); 178118f8f608SEd Tanous std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' '); 178218f8f608SEd Tanous powerSupply["Name"] = std::move(escaped); 1783adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 1784adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 1785adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 1786adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 1787c9563608SJanet Adkins sensor_utils::setLedState(powerSupply, &inventoryItem); 1788adc4f0dbSShawn McCarney 178942cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 179042cbe538SGunnar Mills { 179142cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 179242cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 179342cbe538SGunnar Mills } 179442cbe538SGunnar Mills 1795c9563608SJanet Adkins powerSupply["Status"]["State"] = 1796c9563608SJanet Adkins sensor_utils::getState(&inventoryItem, true); 1797adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 1798adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 1799adc4f0dbSShawn McCarney 1800adc4f0dbSShawn McCarney return powerSupply; 18018fb49dd6SShawn McCarney } 18028fb49dd6SShawn McCarney 18038fb49dd6SShawn McCarney /** 1804de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 1805de629b6eSShawn McCarney * 1806de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 1807de629b6eSShawn McCarney * 1808de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 1809de629b6eSShawn McCarney * information has been obtained. 1810de629b6eSShawn McCarney * 1811adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 1812de629b6eSShawn McCarney * 1813de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 1814de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 1815de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 1816de629b6eSShawn McCarney * 1817de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 1818de629b6eSShawn McCarney * 1819adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 1820adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 1821adc4f0dbSShawn McCarney * 1822de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 1823adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 1824de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 1825de629b6eSShawn McCarney * implements ObjectManager. 1826adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 1827de629b6eSShawn McCarney */ 182823a21a1cSEd Tanous inline void getSensorData( 182981ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1830fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 1831fe04d49cSNan Zhou const std::set<std::string>& connections, 1832b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 1833de629b6eSShawn McCarney { 183462598e31SEd Tanous BMCWEB_LOG_DEBUG("getSensorData enter"); 1835de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 1836de629b6eSShawn McCarney for (const std::string& connection : connections) 1837de629b6eSShawn McCarney { 18385eb468daSGeorge Liu sdbusplus::message::object_path sensorPath( 18395eb468daSGeorge Liu "/xyz/openbmc_project/sensors"); 18405eb468daSGeorge Liu dbus::utility::getManagedObjects( 18415eb468daSGeorge Liu connection, sensorPath, 1842002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, 18435e7e2dc5SEd Tanous inventoryItems](const boost::system::error_code& ec, 184402cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 184562598e31SEd Tanous BMCWEB_LOG_DEBUG("getManagedObjectsCb enter"); 1846de629b6eSShawn McCarney if (ec) 1847de629b6eSShawn McCarney { 184862598e31SEd Tanous BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec); 18498d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1850de629b6eSShawn McCarney return; 1851de629b6eSShawn McCarney } 1852*0c728b42SJanet Adkins auto chassisSubNode = sensor_utils::chassisSubNodeFromString( 1853*0c728b42SJanet Adkins sensorsAsyncResp->chassisSubNode); 1854de629b6eSShawn McCarney // Go through all objects and update response with sensor data 1855de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 1856de629b6eSShawn McCarney { 1857de629b6eSShawn McCarney const std::string& objPath = 1858de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 185962598e31SEd Tanous BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}", 186062598e31SEd Tanous objPath); 1861de629b6eSShawn McCarney 1862de629b6eSShawn McCarney std::vector<std::string> split; 1863de629b6eSShawn McCarney // Reserve space for 1864de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 1865de629b6eSShawn McCarney split.reserve(6); 186650ebd4afSEd Tanous // NOLINTNEXTLINE 186750ebd4afSEd Tanous bmcweb::split(split, objPath, '/'); 1868de629b6eSShawn McCarney if (split.size() < 6) 1869de629b6eSShawn McCarney { 187062598e31SEd Tanous BMCWEB_LOG_ERROR("Got path that isn't long enough {}", 187162598e31SEd Tanous objPath); 1872de629b6eSShawn McCarney continue; 1873de629b6eSShawn McCarney } 187450ebd4afSEd Tanous // These indexes aren't intuitive, as split puts an empty 1875de629b6eSShawn McCarney // string at the beginning 1876de629b6eSShawn McCarney const std::string& sensorType = split[4]; 1877de629b6eSShawn McCarney const std::string& sensorName = split[5]; 187862598e31SEd Tanous BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName, 187962598e31SEd Tanous sensorType); 188049c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 1881de629b6eSShawn McCarney { 188262598e31SEd Tanous BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName); 1883de629b6eSShawn McCarney continue; 1884de629b6eSShawn McCarney } 1885de629b6eSShawn McCarney 1886adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 1887adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1888adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 1889adc4f0dbSShawn McCarney 189095a3ecadSAnthony Wilson const std::string& sensorSchema = 189181ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 189295a3ecadSAnthony Wilson 189395a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 189495a3ecadSAnthony Wilson 1895*0c728b42SJanet Adkins if (sensorSchema == sensors::sensorsNodeStr && 1896928fefb9SNan Zhou !sensorsAsyncResp->efficientExpand) 189795a3ecadSAnthony Wilson { 18981516c21bSJanet Adkins std::string sensorId = 18991516c21bSJanet Adkins redfish::sensor_utils::getSensorId(sensorName, 19001516c21bSJanet Adkins sensorType); 1901c1d019a6SEd Tanous 1902bd79bce8SPatrick Williams sensorsAsyncResp->asyncResp->res 1903bd79bce8SPatrick Williams .jsonValue["@odata.id"] = boost::urls::format( 1904bd79bce8SPatrick Williams "/redfish/v1/Chassis/{}/{}/{}", 1905c1d019a6SEd Tanous sensorsAsyncResp->chassisId, 1906bd79bce8SPatrick Williams sensorsAsyncResp->chassisSubNode, sensorId); 1907bd79bce8SPatrick Williams sensorJson = 1908bd79bce8SPatrick Williams &(sensorsAsyncResp->asyncResp->res.jsonValue); 190995a3ecadSAnthony Wilson } 191095a3ecadSAnthony Wilson else 191195a3ecadSAnthony Wilson { 1912271584abSEd Tanous std::string fieldName; 1913928fefb9SNan Zhou if (sensorsAsyncResp->efficientExpand) 1914928fefb9SNan Zhou { 1915928fefb9SNan Zhou fieldName = "Members"; 1916928fefb9SNan Zhou } 1917928fefb9SNan Zhou else if (sensorType == "temperature") 1918de629b6eSShawn McCarney { 1919de629b6eSShawn McCarney fieldName = "Temperatures"; 1920de629b6eSShawn McCarney } 1921bd79bce8SPatrick Williams else if (sensorType == "fan" || 1922bd79bce8SPatrick Williams sensorType == "fan_tach" || 1923de629b6eSShawn McCarney sensorType == "fan_pwm") 1924de629b6eSShawn McCarney { 1925de629b6eSShawn McCarney fieldName = "Fans"; 1926de629b6eSShawn McCarney } 1927de629b6eSShawn McCarney else if (sensorType == "voltage") 1928de629b6eSShawn McCarney { 1929de629b6eSShawn McCarney fieldName = "Voltages"; 1930de629b6eSShawn McCarney } 1931de629b6eSShawn McCarney else if (sensorType == "power") 1932de629b6eSShawn McCarney { 193355f79e6fSEd Tanous if (sensorName == "total_power") 1934028f7ebcSEddie James { 1935028f7ebcSEddie James fieldName = "PowerControl"; 1936028f7ebcSEddie James } 1937adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 1938adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 1939028f7ebcSEddie James { 1940de629b6eSShawn McCarney fieldName = "PowerSupplies"; 1941de629b6eSShawn McCarney } 1942adc4f0dbSShawn McCarney else 1943adc4f0dbSShawn McCarney { 1944adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 1945adc4f0dbSShawn McCarney continue; 1946adc4f0dbSShawn McCarney } 1947028f7ebcSEddie James } 1948de629b6eSShawn McCarney else 1949de629b6eSShawn McCarney { 1950bd79bce8SPatrick Williams BMCWEB_LOG_ERROR( 1951bd79bce8SPatrick Williams "Unsure how to handle sensorType {}", 195262598e31SEd Tanous sensorType); 1953de629b6eSShawn McCarney continue; 1954de629b6eSShawn McCarney } 1955de629b6eSShawn McCarney 1956de629b6eSShawn McCarney nlohmann::json& tempArray = 1957bd79bce8SPatrick Williams sensorsAsyncResp->asyncResp->res 1958bd79bce8SPatrick Williams .jsonValue[fieldName]; 1959adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 196049c53ac9SJohnathan Mantey { 1961adc4f0dbSShawn McCarney if (tempArray.empty()) 19627ab06f49SGunnar Mills { 196395a3ecadSAnthony Wilson // Put multiple "sensors" into a single 196495a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 196595a3ecadSAnthony Wilson // naming in power.hpp. 19661476687dSEd Tanous nlohmann::json::object_t power; 1967ef4c65b7SEd Tanous boost::urls::url url = boost::urls::format( 1968ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/{}", 1969eddfc437SWilly Tu sensorsAsyncResp->chassisId, 1970eddfc437SWilly Tu sensorsAsyncResp->chassisSubNode); 1971bd79bce8SPatrick Williams url.set_fragment( 1972bd79bce8SPatrick Williams (""_json_pointer / fieldName / "0") 1973eddfc437SWilly Tu .to_string()); 1974eddfc437SWilly Tu power["@odata.id"] = std::move(url); 1975b2ba3072SPatrick Williams tempArray.emplace_back(std::move(power)); 1976adc4f0dbSShawn McCarney } 1977adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 1978adc4f0dbSShawn McCarney } 1979adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 1980adc4f0dbSShawn McCarney { 1981adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 1982adc4f0dbSShawn McCarney { 1983bd79bce8SPatrick Williams sensorJson = &(getPowerSupply( 1984bd79bce8SPatrick Williams tempArray, *inventoryItem, 198581ce609eSEd Tanous sensorsAsyncResp->chassisId)); 1986adc4f0dbSShawn McCarney } 198749c53ac9SJohnathan Mantey } 1988928fefb9SNan Zhou else if (fieldName == "Members") 1989928fefb9SNan Zhou { 19901516c21bSJanet Adkins std::string sensorId = 19911516c21bSJanet Adkins redfish::sensor_utils::getSensorId(sensorName, 19921516c21bSJanet Adkins sensorType); 1993677bb756SEd Tanous 19941476687dSEd Tanous nlohmann::json::object_t member; 1995ef4c65b7SEd Tanous member["@odata.id"] = boost::urls::format( 1996ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/{}/{}", 1997677bb756SEd Tanous sensorsAsyncResp->chassisId, 1998677bb756SEd Tanous sensorsAsyncResp->chassisSubNode, sensorId); 1999b2ba3072SPatrick Williams tempArray.emplace_back(std::move(member)); 2000928fefb9SNan Zhou sensorJson = &(tempArray.back()); 2001928fefb9SNan Zhou } 200249c53ac9SJohnathan Mantey else 200349c53ac9SJohnathan Mantey { 20041476687dSEd Tanous nlohmann::json::object_t member; 2005ef4c65b7SEd Tanous boost::urls::url url = boost::urls::format( 2006ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/{}", 2007eddfc437SWilly Tu sensorsAsyncResp->chassisId, 2008eddfc437SWilly Tu sensorsAsyncResp->chassisSubNode); 2009eddfc437SWilly Tu url.set_fragment( 2010eddfc437SWilly Tu (""_json_pointer / fieldName).to_string()); 2011eddfc437SWilly Tu member["@odata.id"] = std::move(url); 2012b2ba3072SPatrick Williams tempArray.emplace_back(std::move(member)); 2013adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 201449c53ac9SJohnathan Mantey } 201595a3ecadSAnthony Wilson } 2016de629b6eSShawn McCarney 2017adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2018adc4f0dbSShawn McCarney { 2019*0c728b42SJanet Adkins objectInterfacesToJson( 2020*0c728b42SJanet Adkins sensorName, sensorType, chassisSubNode, 2021*0c728b42SJanet Adkins objDictEntry.second, *sensorJson, inventoryItem); 20221d7c0054SEd Tanous 20231d7c0054SEd Tanous std::string path = "/xyz/openbmc_project/sensors/"; 20241d7c0054SEd Tanous path += sensorType; 20251d7c0054SEd Tanous path += "/"; 20261d7c0054SEd Tanous path += sensorName; 2027c1d019a6SEd Tanous sensorsAsyncResp->addMetadata(*sensorJson, path); 2028adc4f0dbSShawn McCarney } 2029de629b6eSShawn McCarney } 203081ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 203149c53ac9SJohnathan Mantey { 203281ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 2033*0c728b42SJanet Adkins if (chassisSubNode == 2034*0c728b42SJanet Adkins sensor_utils::ChassisSubNode::sensorsNode && 2035928fefb9SNan Zhou sensorsAsyncResp->efficientExpand) 2036928fefb9SNan Zhou { 2037928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res 2038928fefb9SNan Zhou .jsonValue["Members@odata.count"] = 2039bd79bce8SPatrick Williams sensorsAsyncResp->asyncResp->res 2040bd79bce8SPatrick Williams .jsonValue["Members"] 2041928fefb9SNan Zhou .size(); 2042928fefb9SNan Zhou } 2043*0c728b42SJanet Adkins else if (chassisSubNode == 2044*0c728b42SJanet Adkins sensor_utils::ChassisSubNode::thermalNode) 20458bd25ccdSJames Feist { 204681ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 20478bd25ccdSJames Feist } 204849c53ac9SJohnathan Mantey } 204962598e31SEd Tanous BMCWEB_LOG_DEBUG("getManagedObjectsCb exit"); 20505eb468daSGeorge Liu }); 205123a21a1cSEd Tanous } 205262598e31SEd Tanous BMCWEB_LOG_DEBUG("getSensorData exit"); 2053de629b6eSShawn McCarney } 2054de629b6eSShawn McCarney 2055fe04d49cSNan Zhou inline void 2056fe04d49cSNan Zhou processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2057fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 20581abe55efSEd Tanous { 2059fe04d49cSNan Zhou auto getConnectionCb = [sensorsAsyncResp, sensorNames]( 2060fe04d49cSNan Zhou const std::set<std::string>& connections) { 206162598e31SEd Tanous BMCWEB_LOG_DEBUG("getConnectionCb enter"); 2062adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2063bd79bce8SPatrick Williams [sensorsAsyncResp, sensorNames, connections]( 2064bd79bce8SPatrick Williams const std::shared_ptr<std::vector<InventoryItem>>& 2065adc4f0dbSShawn McCarney inventoryItems) { 206662598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsCb enter"); 206749c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2068002d39b4SEd Tanous getSensorData(sensorsAsyncResp, sensorNames, connections, 2069d0090733SEd Tanous inventoryItems); 207062598e31SEd Tanous BMCWEB_LOG_DEBUG("getInventoryItemsCb exit"); 2071adc4f0dbSShawn McCarney }; 2072adc4f0dbSShawn McCarney 2073adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2074d0090733SEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, 2075adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2076adc4f0dbSShawn McCarney 207762598e31SEd Tanous BMCWEB_LOG_DEBUG("getConnectionCb exit"); 207808777fb0SLewanczyk, Dawid }; 2079de629b6eSShawn McCarney 2080de629b6eSShawn McCarney // Get set of connections that provide sensor values 208181ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 208295a3ecadSAnthony Wilson } 208395a3ecadSAnthony Wilson 208495a3ecadSAnthony Wilson /** 208595a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 208695a3ecadSAnthony Wilson * chassis. 208795a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 208895a3ecadSAnthony Wilson */ 2089b5a76932SEd Tanous inline void 209081ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 209195a3ecadSAnthony Wilson { 209262598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisData enter"); 209395a3ecadSAnthony Wilson auto getChassisCb = 209481ce609eSEd Tanous [sensorsAsyncResp]( 2095fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) { 209662598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisCb enter"); 209781ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 209862598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisCb exit"); 209908777fb0SLewanczyk, Dawid }; 2100928fefb9SNan Zhou // SensorCollection doesn't contain the Redundancy property 2101*0c728b42SJanet Adkins if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr) 2102928fefb9SNan Zhou { 21038d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 21048d1b46d7Szhanghch05 nlohmann::json::array(); 2105928fefb9SNan Zhou } 210626f03899SShawn McCarney // Get set of sensors in chassis 21077f1cc26dSEd Tanous getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, 21087f1cc26dSEd Tanous sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, 21097f1cc26dSEd Tanous std::move(getChassisCb)); 211062598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisData exit"); 2111271584abSEd Tanous } 211208777fb0SLewanczyk, Dawid 2113413961deSRichard Marian Thomaiyar /** 211449c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 211549c53ac9SJohnathan Mantey * the chassis node 211649c53ac9SJohnathan Mantey * 211749c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 211849c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 211949c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 212049c53ac9SJohnathan Mantey * repeated calls to this function 212149c53ac9SJohnathan Mantey */ 2122bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath( 2123bd79bce8SPatrick Williams std::string_view sensorName, const std::set<std::string>& sensorsList, 2124fe04d49cSNan Zhou std::set<std::string>& sensorsModified) 212549c53ac9SJohnathan Mantey { 2126fe04d49cSNan Zhou for (const auto& chassisSensor : sensorsList) 212749c53ac9SJohnathan Mantey { 212828aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2129b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 213028aa8de5SGeorge Liu if (thisSensorName.empty()) 213149c53ac9SJohnathan Mantey { 213249c53ac9SJohnathan Mantey continue; 213349c53ac9SJohnathan Mantey } 213449c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 213549c53ac9SJohnathan Mantey { 213649c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 213749c53ac9SJohnathan Mantey return true; 213849c53ac9SJohnathan Mantey } 213949c53ac9SJohnathan Mantey } 214049c53ac9SJohnathan Mantey return false; 214149c53ac9SJohnathan Mantey } 214249c53ac9SJohnathan Mantey 214349c53ac9SJohnathan Mantey /** 2144413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2145413961deSRichard Marian Thomaiyar * 21468d1b46d7Szhanghch05 * @param sensorAsyncResp response object 21474bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2148413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2149413961deSRichard Marian Thomaiyar */ 215023a21a1cSEd Tanous inline void setSensorsOverride( 2151b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 21520885057cSEd Tanous std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>& 2153397fd61fSjayaprakash Mutyala allCollections) 2154413961deSRichard Marian Thomaiyar { 215562598e31SEd Tanous BMCWEB_LOG_INFO("setSensorsOverride for subNode{}", 215662598e31SEd Tanous sensorAsyncResp->chassisSubNode); 2157413961deSRichard Marian Thomaiyar 2158d02aad39SEd Tanous std::string_view propertyValueName; 2159f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2160413961deSRichard Marian Thomaiyar std::string memberId; 2161543f4400SEd Tanous double value = 0.0; 2162f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2163f65af9e8SRichard Marian Thomaiyar { 2164f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2165f65af9e8SRichard Marian Thomaiyar { 2166f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2167f65af9e8SRichard Marian Thomaiyar } 2168f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2169f65af9e8SRichard Marian Thomaiyar { 2170f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2171f65af9e8SRichard Marian Thomaiyar } 2172f65af9e8SRichard Marian Thomaiyar else 2173f65af9e8SRichard Marian Thomaiyar { 2174f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2175f65af9e8SRichard Marian Thomaiyar } 2176f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2177f65af9e8SRichard Marian Thomaiyar { 21780885057cSEd Tanous if (!json_util::readJsonObject( 21790885057cSEd Tanous item, sensorAsyncResp->asyncResp->res, "MemberId", memberId, 21800885057cSEd Tanous propertyValueName, value)) 2181413961deSRichard Marian Thomaiyar { 2182413961deSRichard Marian Thomaiyar return; 2183413961deSRichard Marian Thomaiyar } 2184f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2185f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2186f65af9e8SRichard Marian Thomaiyar } 2187f65af9e8SRichard Marian Thomaiyar } 21884bb3dc34SCarol Wang 2189bd79bce8SPatrick Williams auto getChassisSensorListCb = [sensorAsyncResp, overrideMap, 2190bd79bce8SPatrick Williams propertyValueNameStr = 2191bd79bce8SPatrick Williams std::string(propertyValueName)]( 2192bd79bce8SPatrick Williams const std::shared_ptr< 2193bd79bce8SPatrick Williams std::set<std::string>>& sensorsList) { 219449c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 219549c53ac9SJohnathan Mantey // chassis node 2196fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames = 2197fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 2198f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2199413961deSRichard Marian Thomaiyar { 2200f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 2201c71d6125SEd Tanous std::pair<std::string, std::string> sensorNameType = 22021516c21bSJanet Adkins redfish::sensor_utils::splitSensorNameAndType(sensor); 2203c71d6125SEd Tanous if (!findSensorNameUsingSensorPath(sensorNameType.second, 2204c71d6125SEd Tanous *sensorsList, *sensorNames)) 2205f65af9e8SRichard Marian Thomaiyar { 220662598e31SEd Tanous BMCWEB_LOG_INFO("Unable to find memberId {}", item.first); 22078d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2208f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2209413961deSRichard Marian Thomaiyar return; 2210413961deSRichard Marian Thomaiyar } 2211f65af9e8SRichard Marian Thomaiyar } 2212413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2213bd79bce8SPatrick Williams auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap, 2214bd79bce8SPatrick Williams propertyValueNameStr]( 2215bd79bce8SPatrick Williams const std::set< 2216bd79bce8SPatrick Williams std::string>& /*connections*/, 2217bd79bce8SPatrick Williams const std::set<std::pair< 2218bd79bce8SPatrick Williams std::string, std::string>>& 2219413961deSRichard Marian Thomaiyar objectsWithConnection) { 2220f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2221413961deSRichard Marian Thomaiyar { 222262598e31SEd Tanous BMCWEB_LOG_INFO( 222362598e31SEd Tanous "Unable to find all objects with proper connection {} requested {}", 222462598e31SEd Tanous objectsWithConnection.size(), overrideMap.size()); 2225bd79bce8SPatrick Williams messages::resourceNotFound( 2226bd79bce8SPatrick Williams sensorAsyncResp->asyncResp->res, 2227*0c728b42SJanet Adkins sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr 2228413961deSRichard Marian Thomaiyar ? "Temperatures" 2229413961deSRichard Marian Thomaiyar : "Voltages", 2230f65af9e8SRichard Marian Thomaiyar "Count"); 2231f65af9e8SRichard Marian Thomaiyar return; 2232f65af9e8SRichard Marian Thomaiyar } 2233f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2234f65af9e8SRichard Marian Thomaiyar { 223528aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 223628aa8de5SGeorge Liu std::string sensorName = path.filename(); 223728aa8de5SGeorge Liu if (sensorName.empty()) 2238f65af9e8SRichard Marian Thomaiyar { 22394f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2240f65af9e8SRichard Marian Thomaiyar return; 2241f65af9e8SRichard Marian Thomaiyar } 22421516c21bSJanet Adkins std::string id = redfish::sensor_utils::getSensorId( 22431516c21bSJanet Adkins sensorName, path.parent_path().filename()); 2244f65af9e8SRichard Marian Thomaiyar 22453f5eb755SBan Feng const auto& iterator = overrideMap.find(id); 2246f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2247f65af9e8SRichard Marian Thomaiyar { 224862598e31SEd Tanous BMCWEB_LOG_INFO("Unable to find sensor object{}", 224962598e31SEd Tanous item.first); 22504f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2251413961deSRichard Marian Thomaiyar return; 2252413961deSRichard Marian Thomaiyar } 2253e93abac6SGinu George setDbusProperty(sensorAsyncResp->asyncResp, 2254e93abac6SGinu George propertyValueNameStr, item.second, item.first, 2255e93abac6SGinu George "xyz.openbmc_project.Sensor.Value", "Value", 2256d02aad39SEd Tanous iterator->second.first); 2257f65af9e8SRichard Marian Thomaiyar } 2258413961deSRichard Marian Thomaiyar }; 2259413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2260413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2261413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2262413961deSRichard Marian Thomaiyar }; 2263413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 22647f1cc26dSEd Tanous getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, 22657f1cc26dSEd Tanous sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, 22667f1cc26dSEd Tanous std::move(getChassisSensorListCb)); 2267413961deSRichard Marian Thomaiyar } 2268413961deSRichard Marian Thomaiyar 2269a0ec28b6SAdrian Ambrożewicz /** 2270a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2271a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2272a0ec28b6SAdrian Ambrożewicz * 2273a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2274a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2275a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2276a0ec28b6SAdrian Ambrożewicz * 2277a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2278c9563608SJanet Adkins * @param node Node (group) of sensors. See sensor_utils::node for supported 2279c9563608SJanet Adkins * values 2280a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2281a0ec28b6SAdrian Ambrożewicz */ 2282931edc79SEd Tanous template <typename Callback> 2283bd79bce8SPatrick Williams inline void retrieveUriToDbusMap( 2284bd79bce8SPatrick Williams const std::string& chassis, const std::string& node, Callback&& mapComplete) 2285a0ec28b6SAdrian Ambrożewicz { 228602da7c5aSEd Tanous decltype(sensors::paths)::const_iterator pathIt = 228702da7c5aSEd Tanous std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), 228802da7c5aSEd Tanous [&node](auto&& val) { return val.first == node; }); 228902da7c5aSEd Tanous if (pathIt == sensors::paths.cend()) 2290a0ec28b6SAdrian Ambrożewicz { 229162598e31SEd Tanous BMCWEB_LOG_ERROR("Wrong node provided : {}", node); 22926804b5c8SEd Tanous std::map<std::string, std::string> noop; 22936804b5c8SEd Tanous mapComplete(boost::beast::http::status::bad_request, noop); 2294a0ec28b6SAdrian Ambrożewicz return; 2295a0ec28b6SAdrian Ambrożewicz } 2296d51e072fSKrzysztof Grobelny 229772374eb7SNan Zhou auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 2298bd79bce8SPatrick Williams auto callback = 2299bd79bce8SPatrick Williams [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)]( 2300a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2301fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus) { 2302fe04d49cSNan Zhou mapCompleteCb(status, uriToDbus); 2303fe04d49cSNan Zhou }; 2304a0ec28b6SAdrian Ambrożewicz 2305a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2306d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2307a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2308a0ec28b6SAdrian Ambrożewicz } 2309a0ec28b6SAdrian Ambrożewicz 2310bacb2162SNan Zhou namespace sensors 2311bacb2162SNan Zhou { 2312928fefb9SNan Zhou 2313bacb2162SNan Zhou inline void getChassisCallback( 2314c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2315c1d019a6SEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 2316fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 2317bacb2162SNan Zhou { 231862598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisCallback enter "); 2319bacb2162SNan Zhou 2320c1d019a6SEd Tanous nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 2321c1d019a6SEd Tanous for (const std::string& sensor : *sensorNames) 2322bacb2162SNan Zhou { 232362598e31SEd Tanous BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor); 2324bacb2162SNan Zhou 2325bacb2162SNan Zhou sdbusplus::message::object_path path(sensor); 2326bacb2162SNan Zhou std::string sensorName = path.filename(); 2327bacb2162SNan Zhou if (sensorName.empty()) 2328bacb2162SNan Zhou { 232962598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor); 2330c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2331bacb2162SNan Zhou return; 2332bacb2162SNan Zhou } 2333c1d019a6SEd Tanous std::string type = path.parent_path().filename(); 23341516c21bSJanet Adkins std::string id = redfish::sensor_utils::getSensorId(sensorName, type); 2335c1d019a6SEd Tanous 23361476687dSEd Tanous nlohmann::json::object_t member; 2337ef4c65b7SEd Tanous member["@odata.id"] = boost::urls::format( 2338ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id); 2339c1d019a6SEd Tanous 2340b2ba3072SPatrick Williams entriesArray.emplace_back(std::move(member)); 2341bacb2162SNan Zhou } 2342bacb2162SNan Zhou 2343c1d019a6SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 234462598e31SEd Tanous BMCWEB_LOG_DEBUG("getChassisCallback exit"); 2345bacb2162SNan Zhou } 2346e6bd846dSNan Zhou 2347ac106bf6SEd Tanous inline void handleSensorCollectionGet( 2348ac106bf6SEd Tanous App& app, const crow::Request& req, 2349ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2350de167a6fSNan Zhou const std::string& chassisId) 2351de167a6fSNan Zhou { 2352de167a6fSNan Zhou query_param::QueryCapabilities capabilities = { 2353de167a6fSNan Zhou .canDelegateExpandLevel = 1, 2354de167a6fSNan Zhou }; 2355de167a6fSNan Zhou query_param::Query delegatedQuery; 2356ac106bf6SEd Tanous if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp, 2357de167a6fSNan Zhou delegatedQuery, capabilities)) 2358de167a6fSNan Zhou { 2359de167a6fSNan Zhou return; 2360de167a6fSNan Zhou } 2361de167a6fSNan Zhou 2362de167a6fSNan Zhou if (delegatedQuery.expandType != query_param::ExpandType::None) 2363de167a6fSNan Zhou { 2364de167a6fSNan Zhou // we perform efficient expand. 2365ac106bf6SEd Tanous auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>( 2366ac106bf6SEd Tanous asyncResp, chassisId, sensors::dbus::sensorPaths, 2367*0c728b42SJanet Adkins sensors::sensorsNodeStr, 2368de167a6fSNan Zhou /*efficientExpand=*/true); 2369ac106bf6SEd Tanous getChassisData(sensorsAsyncResp); 2370de167a6fSNan Zhou 237162598e31SEd Tanous BMCWEB_LOG_DEBUG( 237262598e31SEd Tanous "SensorCollection doGet exit via efficient expand handler"); 2373de167a6fSNan Zhou return; 23740bad320cSEd Tanous } 2375de167a6fSNan Zhou 2376de167a6fSNan Zhou // We get all sensors as hyperlinkes in the chassis (this 2377de167a6fSNan Zhou // implies we reply on the default query parameters handler) 2378*0c728b42SJanet Adkins getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths, 2379ac106bf6SEd Tanous std::bind_front(sensors::getChassisCallback, asyncResp, 2380*0c728b42SJanet Adkins chassisId, sensors::sensorsNodeStr)); 2381c1d019a6SEd Tanous } 23827f1cc26dSEd Tanous 2383c1d019a6SEd Tanous inline void 2384c1d019a6SEd Tanous getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2385c1d019a6SEd Tanous const std::string& sensorPath, 2386c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& mapperResponse) 2387c1d019a6SEd Tanous { 2388c1d019a6SEd Tanous if (mapperResponse.size() != 1) 2389c1d019a6SEd Tanous { 2390c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2391c1d019a6SEd Tanous return; 2392c1d019a6SEd Tanous } 2393c1d019a6SEd Tanous const auto& valueIface = *mapperResponse.begin(); 2394c1d019a6SEd Tanous const std::string& connectionName = valueIface.first; 239562598e31SEd Tanous BMCWEB_LOG_DEBUG("Looking up {}", connectionName); 239662598e31SEd Tanous BMCWEB_LOG_DEBUG("Path {}", sensorPath); 2397c1343bf6SKrzysztof Grobelny 2398c1343bf6SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 2399c1343bf6SKrzysztof Grobelny *crow::connections::systemBus, connectionName, sensorPath, "", 2400c1d019a6SEd Tanous [asyncResp, 24015e7e2dc5SEd Tanous sensorPath](const boost::system::error_code& ec, 2402c1d019a6SEd Tanous const ::dbus::utility::DBusPropertiesMap& valuesDict) { 2403c1d019a6SEd Tanous if (ec) 2404c1d019a6SEd Tanous { 2405c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2406c1d019a6SEd Tanous return; 2407c1d019a6SEd Tanous } 2408c1d019a6SEd Tanous sdbusplus::message::object_path path(sensorPath); 2409c1d019a6SEd Tanous std::string name = path.filename(); 2410c1d019a6SEd Tanous path = path.parent_path(); 2411c1d019a6SEd Tanous std::string type = path.filename(); 2412c9563608SJanet Adkins sensor_utils::objectPropertiesToJson( 2413*0c728b42SJanet Adkins name, type, sensor_utils::ChassisSubNode::sensorsNode, 2414*0c728b42SJanet Adkins valuesDict, asyncResp->res.jsonValue, nullptr); 2415c1343bf6SKrzysztof Grobelny }); 2416de167a6fSNan Zhou } 2417de167a6fSNan Zhou 2418e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req, 2419c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2420677bb756SEd Tanous const std::string& chassisId, 2421c1d019a6SEd Tanous const std::string& sensorId) 2422e6bd846dSNan Zhou { 2423c1d019a6SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2424e6bd846dSNan Zhou { 2425e6bd846dSNan Zhou return; 2426e6bd846dSNan Zhou } 2427c71d6125SEd Tanous std::pair<std::string, std::string> nameType = 24281516c21bSJanet Adkins redfish::sensor_utils::splitSensorNameAndType(sensorId); 2429c71d6125SEd Tanous if (nameType.first.empty() || nameType.second.empty()) 2430c1d019a6SEd Tanous { 2431c1d019a6SEd Tanous messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 2432c1d019a6SEd Tanous return; 2433c1d019a6SEd Tanous } 2434c71d6125SEd Tanous 2435ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 2436ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId); 2437c1d019a6SEd Tanous 243862598e31SEd Tanous BMCWEB_LOG_DEBUG("Sensor doGet enter"); 2439e6bd846dSNan Zhou 24402b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 2441e6bd846dSNan Zhou "xyz.openbmc_project.Sensor.Value"}; 2442c71d6125SEd Tanous std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first + 2443c71d6125SEd Tanous '/' + nameType.second; 2444e6bd846dSNan Zhou // Get a list of all of the sensors that implement Sensor.Value 2445e6bd846dSNan Zhou // and get the path and service name associated with the sensor 24462b73119cSGeorge Liu ::dbus::utility::getDbusObject( 24472b73119cSGeorge Liu sensorPath, interfaces, 2448aec0ec30SMyung Bae [asyncResp, sensorId, 24492b73119cSGeorge Liu sensorPath](const boost::system::error_code& ec, 2450c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& subtree) { 245162598e31SEd Tanous BMCWEB_LOG_DEBUG("respHandler1 enter"); 2452aec0ec30SMyung Bae if (ec == boost::system::errc::io_error) 2453aec0ec30SMyung Bae { 245462598e31SEd Tanous BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths"); 2455aec0ec30SMyung Bae messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 2456aec0ec30SMyung Bae return; 2457aec0ec30SMyung Bae } 2458e6bd846dSNan Zhou if (ec) 2459e6bd846dSNan Zhou { 2460c1d019a6SEd Tanous messages::internalError(asyncResp->res); 246162598e31SEd Tanous BMCWEB_LOG_ERROR( 246262598e31SEd Tanous "Sensor getSensorPaths resp_handler: Dbus error {}", ec); 2463e6bd846dSNan Zhou return; 2464e6bd846dSNan Zhou } 2465c1d019a6SEd Tanous getSensorFromDbus(asyncResp, sensorPath, subtree); 246662598e31SEd Tanous BMCWEB_LOG_DEBUG("respHandler1 exit"); 24672b73119cSGeorge Liu }); 2468e6bd846dSNan Zhou } 2469e6bd846dSNan Zhou 2470bacb2162SNan Zhou } // namespace sensors 2471bacb2162SNan Zhou 24727e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 247395a3ecadSAnthony Wilson { 24747e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 2475ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 2476002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2477de167a6fSNan Zhou std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); 247895a3ecadSAnthony Wilson } 247995a3ecadSAnthony Wilson 24807e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 248195a3ecadSAnthony Wilson { 24827e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 2483ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 2484002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2485e6bd846dSNan Zhou std::bind_front(sensors::handleSensorGet, std::ref(app))); 248695a3ecadSAnthony Wilson } 248795a3ecadSAnthony Wilson 248808777fb0SLewanczyk, Dawid } // namespace redfish 2489